PkmnLibTools/src/Tools/ScriptHeadersExporter.cpp

94 lines
3.3 KiB
C++

#include "ScriptHeadersExporter.hpp"
#include <PkmnLib/ScriptResolving/AngelScript/AngelScriptResolver.hpp>
#include <filesystem>
static bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if (start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
static void PrintObjectType(asITypeInfo* type, const std::filesystem::path& dir){
auto name = std::string(type->GetName());
if (name == "string" || name == "array" || name == "ref")
return;
std::fstream fs;
fs.open((dir / name).concat(".as"), std::fstream::out);
auto isAbstract = (type->GetFlags() & asEObjTypeFlags::asOBJ_ABSTRACT) != 0;
fs << "shared ";
if (isAbstract)
fs << "abstract class ";
else
fs << "interface ";
fs << type->GetName() << " {" << std::endl;
auto propertyCount = type->GetMethodCount();
for (asUINT j = 0; j < propertyCount; j++) {
auto method = type->GetMethodByIndex(j);
if (method->IsProperty()) {
auto name = std::string(method->GetName());
auto decl = std::string(method->GetDeclaration(false, true, false));
auto realName = name.substr(4);
replace(decl, name, realName);
replace(decl, "() const", " { get const; }");
replace(decl, "()", " { get; }");
fs << "\t" << decl << std::endl;
} else {
auto name = std::string(method->GetName());
if (name == "opAssign")
continue;
auto decl = std::string(method->GetDeclaration(false, true, true));
replace(decl, "&in", " &in");
replace(decl, "&out", " &out");
fs << "\t" << decl;
if (isAbstract)
fs << "{};" << std::endl;
else
fs << ";" << std::endl;
}
}
fs << "}" << std::endl;
fs.close();
}
void ScriptHeadersExporter::Export(const std::string& outPath) {
auto resolver = new AngelScriptResolver();
resolver->Initialize(nullptr);
resolver->FinalizeModule();
auto module = resolver->GetMainModule();
auto engine = module->GetEngine();
auto typesCount = engine->GetObjectTypeCount();
std::filesystem::path dir(outPath);
for (asUINT i = 0; i < typesCount; i++) {
auto type = engine->GetObjectTypeByIndex(i);
PrintObjectType(type, dir);
}
auto moduleTypesCount = module->GetObjectTypeCount();
for (asUINT i = 0; i < moduleTypesCount; i++) {
auto type = module->GetObjectTypeByIndex(i);
PrintObjectType(type, dir);
}
auto enumCount = engine->GetEnumCount();
for (asUINT i = 0; i < enumCount; i++) {
auto en = engine->GetEnumByIndex(i);
auto name = en->GetName();
std::fstream fs;
fs.open((dir / name).concat(".as"), std::fstream::out);
fs << "shared enum " << en->GetName() << " {" << std::endl;
auto valueCount = en->GetEnumValueCount();
for (asUINT j = 0; j < valueCount; j++) {
int val;
auto name = en->GetEnumValueByIndex(j, &val);
fs << "\t" << name << " = " << val << "," << std::endl;
}
fs << "}" << std::endl;
fs.close();
}
}