diff --git a/prebuild b/prebuild new file mode 100755 index 00000000..9082cf95 Binary files /dev/null and b/prebuild differ diff --git a/prebuild.cpp b/prebuild.cpp index fb04720c..5d80d015 100644 --- a/prebuild.cpp +++ b/prebuild.cpp @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include #include std::string to_hex_string(const std::string& input) { @@ -20,16 +20,34 @@ std::string to_hex_string(const std::string& input) { std::map generate_python_sources() { std::map sources; - - for (const auto& file : std::filesystem::directory_iterator("python")) { - if (file.path().extension() == ".py") { - std::string key = file.path().stem().string(); - std::ifstream f(file.path()); - std::string content((std::istreambuf_iterator(f)), std::istreambuf_iterator()); - sources[key] = to_hex_string(content); + DIR *dir; + struct dirent *ent; + if ((dir = opendir ("python")) != NULL) + { + while ((ent = readdir (dir)) != NULL) + { + std::string filename = ent->d_name; + size_t pos = filename.rfind(".py"); + if (pos != std::string::npos) + { + std::string key = filename.substr(0, filename.length() - 3); + std::string filepath = "python/" + filename; + FILE* file = fopen(filepath.c_str(), "r"); + if(file == NULL) exit(2); + std::string content; + char buffer[1024]; + while (fgets(buffer, sizeof(buffer), file) != NULL) + { + content += buffer; + } + fclose(file); + sources[key] = to_hex_string(content); + } } + closedir (dir); + }else{ + exit(1); } - return sources; } @@ -46,8 +64,8 @@ std::string generate_header(const std::map& sources) { header += "\n#include \n#include \n\nnamespace pkpy{\n"; header += " inline static std::map kPythonLibs = {\n"; - for (const auto& [key, value] : sources) { - header += " {\"" + key + "\", \"" + value + "\"},\n"; + for (auto it=sources.begin(); it!=sources.end(); ++it) { + header += " {\"" + it->first + "\", \"" + it->second + "\"},\n"; } header += " };\n"; @@ -59,7 +77,8 @@ std::string generate_header(const std::map& sources) { int main() { auto sources = generate_python_sources(); std::string header = generate_header(sources); - std::ofstream header_file("src/_generated.h"); - header_file << header; + FILE* f = fopen("src/_generated.h", "w"); + fprintf(f, "%s", header.c_str()); + fclose(f); return 0; } \ No newline at end of file diff --git a/prebuild.sh b/prebuild.sh deleted file mode 100644 index 327997bc..00000000 --- a/prebuild.sh +++ /dev/null @@ -1,3 +0,0 @@ -g++ -o prebuild --std=c++17 prebuild.cpp -./prebuild -rm prebuild \ No newline at end of file diff --git a/preprocess.py b/preprocess.py index d4fc1119..232d125e 100644 --- a/preprocess.py +++ b/preprocess.py @@ -8,9 +8,11 @@ def generate_python_sources(): key = file.split(".")[0] with open("python/" + file) as f: value = f.read() - value = value.encode('utf-8').hex(':') - value = '\\x' + value.replace(':', '\\x') - sources[key] = value + value = value.encode('utf-8').hex() + new_value = [] + for i in range(0, len(value), 2): + new_value.append("\\x" + value[i:i+2]) + sources[key] = "".join(new_value) timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")