From ed72d9d14741de0bd206732cd338b1788cebdece Mon Sep 17 00:00:00 2001 From: Seemann Date: Mon, 26 Dec 2022 12:54:51 -0500 Subject: [PATCH] TextLoader 1.1: generate typings file for TS --- loaders/TextLoader/dllmain.cpp | 106 ++++++++++++++++++++++----------- 1 file changed, 70 insertions(+), 36 deletions(-) diff --git a/loaders/TextLoader/dllmain.cpp b/loaders/TextLoader/dllmain.cpp index 878a524..8a57bf9 100644 --- a/loaders/TextLoader/dllmain.cpp +++ b/loaders/TextLoader/dllmain.cpp @@ -3,50 +3,84 @@ #include #include #include +#include #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(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(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; \ No newline at end of file +} \ No newline at end of file