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

79 lines
2.3 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-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) {
if (md.macro == MODULE_DEPENDENCY) {
oss << "\t{";
for (auto& args : md.args) {
if (args[0] != '{') {
oss << '"' << args << "\", ";
}
else {
oss << args;
}
}
oss << "},\n";
}
}
oss << '}';
writeFile(file_path, oss.str());
}
2024-07-27 14:24:46 +08:00
void genPlugin(const char* file_path, const pmr::vector<MacroData>& mdList) {
std::string_view text = readFile(file_path);
api::ModuleInfo info;
api::JsonDeserialize<api::ModuleInfo>(text, &info);
bool bchange = false;
for (auto& md : mdList) {
if (md.macro == IMPLEMENT_DYNAMIC_MODULE || md.macro == IMPLEMENT_STATIC_MODULE) {
api::Name name = md.args[1].c_str();
bchange = info.name != name;
info.name = name;
2024-07-24 14:42:14 +08:00
}
2024-07-27 14:24:46 +08:00
else if (md.macro == MODULE_DEPENDENCY) {
for (auto& args : md.args) {
if (args[0] != '{') {
}
}
2024-07-24 14:42:14 +08:00
}
2024-07-27 14:24:46 +08:00
}
if (bchange) {
writeFile(file_path, api::JsonSerialize(info));
2024-07-24 14:42:14 +08:00
}
2024-07-20 18:04:19 +08:00
}
2024-07-27 14:24:46 +08:00
int main(int argc, char* argv[]) {
if (argc < 6) {
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];
auto mdList = readMacroFile(module_file.c_str());
genLua(lua_file.c_str(), mdList);
genPlugin(plugin_file.c_str(), mdList);
2024-07-20 18:04:19 +08:00
return 0;
}