zengine/engine/tools/make_plugin/src/main.cpp

81 lines
2.8 KiB
C++
Raw Normal View History

2024-07-27 14:24:46 +08:00
#include "macro_parse.h"
#include "archive/json.h"
#include "module/module.h"
2024-07-20 18:04:19 +08:00
#include <iostream>
2024-07-27 14:24:46 +08:00
#include <fstream>
2024-07-20 18:04:19 +08:00
#include <sstream>
2024-07-31 10:48:28 +08:00
#include <algorithm>
2024-07-27 14:24:46 +08:00
std::string_view readFile(const char* file_path) {
std::ifstream file(file_path, std::ios::ate);
2024-07-20 18:04:19 +08:00
if (!file.is_open()) {
2024-07-27 14:24:46 +08:00
return "";
2024-07-20 18:04:19 +08:00
}
2024-07-27 14:24:46 +08:00
size_t size = file.tellg();
file.seekg(0);
char* ptr = new(FramePool)char[size];
file.read(ptr, size);
return std::string_view(ptr, size);
2024-07-20 18:04:19 +08:00
}
void writeFile(const char* file_path, std::string_view data) {
std::ofstream file(file_path, 0);
file.write(data.data(), data.size());
file.close();
}
void genLua(const char* file_path, const pmr::vector<MacroData>& mdList) {
std::ostringstream oss;
oss << "{\n";
for (auto& md : mdList) {
2024-07-31 10:48:28 +08:00
if (md.macro == PUBLIC_MODULE_DEPENDENCY || md.macro == PRIVATE_MODULE_DEPENDENCY) {
2024-07-20 18:04:19 +08:00
oss << "\t{";
for (auto& args : md.args) {
2024-07-31 10:48:28 +08:00
oss << '"' << args << "\", ";
2024-07-20 18:04:19 +08:00
}
2024-07-31 10:48:28 +08:00
oss << (md.macro == PUBLIC_MODULE_DEPENDENCY ? "{public = true}},\n" : "{public = false}},\n");
2024-07-20 18:04:19 +08:00
}
}
oss << '}';
writeFile(file_path, oss.str());
}
2024-07-31 10:48:28 +08:00
void genPlugin(const char* file_path, ParseData& pd) {
readMacroFile(file_path, pd);
2024-07-27 14:24:46 +08:00
api::ModuleInfo info;
2024-07-31 10:48:28 +08:00
std::string_view text = pd.exportText;
2024-07-27 14:24:46 +08:00
api::JsonDeserialize<api::ModuleInfo>(text, &info);
2024-07-31 10:48:28 +08:00
info.dependencies.clear();
for (auto& md : pd.mdList) {
if (md.macro == IMPLEMENT_DYNAMIC_MODULE) {
info.name = md.args[1];
2024-07-24 14:42:14 +08:00
}
2024-07-31 10:48:28 +08:00
else if (md.macro == PUBLIC_MODULE_DEPENDENCY) {
2024-07-27 14:24:46 +08:00
for (auto& args : md.args) {
2024-07-31 10:48:28 +08:00
std::pmr::string upname{ args, FramePool};
std::transform(upname.begin(), upname.end(), upname.begin(), std::toupper);
info.dependencies.push_back({args, upname + "_VERSION", upname + "_KIND"});
2024-07-27 14:24:46 +08:00
}
2024-07-24 14:42:14 +08:00
}
2024-07-27 14:24:46 +08:00
}
2024-07-31 10:48:28 +08:00
std::ostringstream oss;
std::string name = info.name.ToString();
std::transform(name.begin(), name.end(), name.begin(), std::toupper);
oss << "#include \"" << pd.moduleFile << "\"\n";
oss << "#define EXPORT_MODULE(json)" << name << "_API const char* __module_meta_" << info.name.data() << "__ = json\n";
oss << "EXPORT_MODULE(R\"(" << api::JsonSerialize(info) << ")\");\n";
writeFile(file_path, oss.str());
2024-07-20 18:04:19 +08:00
}
2024-07-31 10:48:28 +08:00
//已废弃
2024-07-27 14:24:46 +08:00
int main(int argc, char* argv[]) {
2024-07-31 10:48:28 +08:00
if (argc < 100) {
2024-07-27 14:24:46 +08:00
return -1;
}
std::string project_dir = argv[1];
std::string lua_file = project_dir + "\\" + argv[2];
std::string script_dir = argv[3];
std::string module_file = script_dir + "\\" + argv[4];
std::string plugin_file = script_dir + "\\" + argv[5];
2024-07-31 10:48:28 +08:00
ParseData pd;
pd.moduleFile = argv[4];
readMacroFile(module_file.c_str(), pd);
genLua(lua_file.c_str(), pd.mdList);
genPlugin(plugin_file.c_str(), pd);
2024-07-20 18:04:19 +08:00
return 0;
}