Skip to content

Commit

Permalink
TextLoader 1.1: generate typings file for TS
Browse files Browse the repository at this point in the history
  • Loading branch information
x87 committed Dec 26, 2022
1 parent 0f30722 commit ed72d9d
Showing 1 changed file with 70 additions and 36 deletions.
106 changes: 70 additions & 36 deletions loaders/TextLoader/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,84 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <windows.h>

#include "../../SDK/cleo_redux_sdk.h"

class TxtLoader {
public:
TxtLoader()
{
Log("TXT Loader 1.0");
RegisterLoader("*.txt", Loader);
RegisterLoader("*.text", Loader);
}

static void* Loader(const char* fileName)
{
std::ifstream input_file(fileName);

// return nullptr if we can't open the file
if (!input_file.is_open()) {
return nullptr;
namespace std {

class TxtLoader {
public:
TxtLoader()
{
Log("TXT Loader 1.1");
RegisterLoader("*.txt", Loader);
RegisterLoader("*.text", Loader);
GenerateTypings();
}

std::ostringstream ss;
std::string line;
static void* Loader(const char* fileName)
{
ifstream input_file(fileName);

// construct a JSON array where every item is a line from the source file
ss << "[";
if (std::getline(input_file, line)) {
ss << "\"" << line << "\"";
}
while (std::getline(input_file, line)) {
ss << ",\"" << line << "\"";
// return nullptr if we can't open the file
if (!input_file.is_open()) {
return nullptr;
}

ostringstream ss;
string line;

// construct a JSON array where every item is a line from the source file
ss << "[";
if (getline(input_file, line)) {
ss << "\"" << line << "\"";
}
while (getline(input_file, line)) {
ss << ",\"" << line << "\"";
}
ss << "]";
input_file.close();

// allocate enough space to put the serialized string
auto content = ss.str();
char* buf = reinterpret_cast<char*>(AllocMem(content.length() + 1));

// copy serialized string to the buffer
sprintf(buf, content.c_str());

// let CLEO read from the buffer and free up the memory
return buf;
}
ss << "]";
input_file.close();

// allocate enough space to put the serialized string
auto content = ss.str();
char* buf = reinterpret_cast<char*>(AllocMem(content.length() + 1));
static void GenerateTypings()
{
char path[MAX_PATH];
GetDirectoryPath(Directory::CONFIG, path);
string p(path);
p += "\\txt_loader.d.ts";

ofstream typing_file(p);

if (!typing_file.is_open()) {
Log("Failed to write txt_loader.d.ts");
return;
}

// copy serialized string to the buffer
sprintf(buf, content.c_str());
typing_file << R"(declare module "*.txt" {
const value: string[];
export default value;
}
declare module "*.text" {
const value: string[];
export default value;
}
)";

typing_file.close();
}

// let CLEO read from the buffer and free up the memory
return buf;
}
} TxtLoader;

} TxtLoader;
}

0 comments on commit ed72d9d

Please sign in to comment.