This commit is contained in:
blueloveTH 2023-05-15 10:46:50 -07:00
parent ec7e0a6862
commit ed690b4376
4 changed files with 38 additions and 20 deletions

BIN
prebuild Executable file

Binary file not shown.

View File

@ -2,8 +2,8 @@
#include <string> #include <string>
#include <map> #include <map>
#include <algorithm> #include <algorithm>
#include <filesystem> #include <cstdio>
#include <fstream> #include <dirent.h>
#include <ctime> #include <ctime>
std::string to_hex_string(const std::string& input) { std::string to_hex_string(const std::string& input) {
@ -20,16 +20,34 @@ std::string to_hex_string(const std::string& input) {
std::map<std::string, std::string> generate_python_sources() { std::map<std::string, std::string> generate_python_sources() {
std::map<std::string, std::string> sources; std::map<std::string, std::string> sources;
DIR *dir;
for (const auto& file : std::filesystem::directory_iterator("python")) { struct dirent *ent;
if (file.path().extension() == ".py") { if ((dir = opendir ("python")) != NULL)
std::string key = file.path().stem().string(); {
std::ifstream f(file.path()); while ((ent = readdir (dir)) != NULL)
std::string content((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); {
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); sources[key] = to_hex_string(content);
} }
} }
closedir (dir);
}else{
exit(1);
}
return sources; return sources;
} }
@ -46,8 +64,8 @@ std::string generate_header(const std::map<std::string, std::string>& sources) {
header += "\n#include <map>\n#include <string>\n\nnamespace pkpy{\n"; header += "\n#include <map>\n#include <string>\n\nnamespace pkpy{\n";
header += " inline static std::map<std::string, std::string> kPythonLibs = {\n"; header += " inline static std::map<std::string, std::string> kPythonLibs = {\n";
for (const auto& [key, value] : sources) { for (auto it=sources.begin(); it!=sources.end(); ++it) {
header += " {\"" + key + "\", \"" + value + "\"},\n"; header += " {\"" + it->first + "\", \"" + it->second + "\"},\n";
} }
header += " };\n"; header += " };\n";
@ -59,7 +77,8 @@ std::string generate_header(const std::map<std::string, std::string>& sources) {
int main() { int main() {
auto sources = generate_python_sources(); auto sources = generate_python_sources();
std::string header = generate_header(sources); std::string header = generate_header(sources);
std::ofstream header_file("src/_generated.h"); FILE* f = fopen("src/_generated.h", "w");
header_file << header; fprintf(f, "%s", header.c_str());
fclose(f);
return 0; return 0;
} }

View File

@ -1,3 +0,0 @@
g++ -o prebuild --std=c++17 prebuild.cpp
./prebuild
rm prebuild

View File

@ -8,9 +8,11 @@ def generate_python_sources():
key = file.split(".")[0] key = file.split(".")[0]
with open("python/" + file) as f: with open("python/" + file) as f:
value = f.read() value = f.read()
value = value.encode('utf-8').hex(':') value = value.encode('utf-8').hex()
value = '\\x' + value.replace(':', '\\x') new_value = []
sources[key] = 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") timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")