Adds support for tool to compile scripts to single binary file.

This commit is contained in:
Deukhoofd 2021-01-16 19:49:06 +01:00
parent e3d23b582f
commit 74ccdb1759
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
6 changed files with 85 additions and 8 deletions

View File

@ -11,6 +11,8 @@ SetupConan()
file(GLOB_RECURSE SRC_FILES src/*.cpp src/*.hpp)
add_executable(PkmnLibTools ${SRC_FILES})
target_precompile_headers(PkmnLibTools PUBLIC src/Precompiled.hxx)
add_definitions(-DLEVEL_U8)
SET(_LINKS Arbutils CreatureLib pkmnLib)
target_link_libraries(PkmnLibTools PUBLIC ${_LINKS})

View File

@ -16,4 +16,4 @@ class PkmnLibConan(ConanFile):
self.requires("Arbutils/latest@epsilon/master")
self.requires("CreatureLib/latest@epsilon/master")
self.requires("PkmnLib/latest@epsilon/master")
self.requires("AngelScript/2.34@AngelScript/Deukhoofd")
self.requires("AngelScript/2.35@AngelScript/Deukhoofd")

6
src/Precompiled.hxx Normal file
View File

@ -0,0 +1,6 @@
#ifndef PKMNLIBTOOLS_PRECOMPILED_HXX
#define PKMNLIBTOOLS_PRECOMPILED_HXX
#include <PkmnLib/Precompiled.hxx>
#endif // PKMNLIBTOOLS_PRECOMPILED_HXX

View File

@ -0,0 +1,50 @@
#include "ScriptCompiler.hpp"
#include <PkmnLib/ScriptResolving/AngelScript/AngelScriptResolver.hpp>
#include <filesystem>
static PkmnLib::Library::PokemonLibrary* BuildStaticLibrary() {
return new PkmnLib::Library::PokemonLibrary(
new PkmnLib::Library::LibrarySettings(100, 4, 4096), new PkmnLib::Library::SpeciesLibrary(),
new PkmnLib::Library::MoveLibrary(), new PkmnLib::Library::ItemLibrary(),
new CreatureLib::Library::GrowthRateLibrary(), new CreatureLib::Library::TypeLibrary(),
new PkmnLib::Library::NatureLibrary());
}
static PkmnLib::Battling::BattleLibrary* BuildLibrary(AngelScriptResolver* scriptResolver) {
auto statCalc = new PkmnLib::Battling::StatCalculator();
auto lib = new PkmnLib::Battling::BattleLibrary(
BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(),
new PkmnLib::Battling::ExperienceLibrary(), scriptResolver, new PkmnLib::Battling::MiscLibrary());
return lib;
}
using recursive_directory_iterator = std::filesystem::recursive_directory_iterator;
void ScriptCompiler::Compile(const std::string& inPath, const std::string& outPath) {
auto resolver = new AngelScriptResolver();
auto library = BuildLibrary(resolver);
resolver->Initialize(library);
for (const auto& dirEntry : recursive_directory_iterator(inPath)) {
if (dirEntry.is_regular_file()) {
if (dirEntry.path().extension() != ".as") {
continue;
}
if (dirEntry.path().parent_path().stem() == "Interfaces") {
continue;
}
std::cout << dirEntry.path().stem() << std::endl;
std::ifstream t(dirEntry.path().c_str());
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
resolver->CreateScript(dirEntry.path().stem().c_str(), str.c_str());
}
}
resolver->FinalizeModule();
std::filesystem::path dir(outPath);
std::cout << dir << std::endl;
resolver->WriteByteCodeToFile(outPath.c_str());
delete library;
}

View File

@ -0,0 +1,9 @@
#ifndef PKMNLIBTOOLS_SCRIPTCOMPILER_HPP
#define PKMNLIBTOOLS_SCRIPTCOMPILER_HPP
class ScriptCompiler {
public:
static void Compile(const std::string& inPath, const std::string& outPath);
};
#endif // PKMNLIBTOOLS_SCRIPTCOMPILER_HPP

View File

@ -6,6 +6,7 @@
#include "BuildData/BuildSpecies.hpp"
#include "BuildData/BuildTypes.hpp"
#include "BuildData/GrowthRatesBuilder.hpp"
#include "Tools/ScriptCompiler.hpp"
#include "Tools/ScriptHeadersExporter.hpp"
[[maybe_unused]] static PkmnLib::Library::PokemonLibrary* BuildDataLibrary() {
@ -34,8 +35,15 @@
int main(int argc, const char* argv[]) {
args::ArgumentParser parser("PkmnLib tool executable.", "");
args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"});
args::Positional<std::string> tool(parser, "tool", "The tool to execute");
args::ValueFlag<std::string> outPath(parser, "outpath", "The path to output to.", {'o'});
args::Group tools(parser, "Tools");
args::Command exportScriptHeaders(tools, "export-script-headers", "Exports the Angelscript script headers");
args::Command compileScripts(tools, "compile-scripts", "Compile the given scripts");
args::ValueFlag<std::string> outPathExportScriptHeaders(exportScriptHeaders, "outpath", "The path to output to.",
{'o'});
args::ValueFlag<std::string> inPathCompileScripts(compileScripts, "inpath", "The path to read scripts from.",
{'i'});
args::ValueFlag<std::string> outPathCompileScripts(compileScripts, "outpath", "The path to output to.", {'o'});
try {
parser.ParseCLI(argc, argv);
} catch (const args::Help&) {
@ -46,14 +54,16 @@ int main(int argc, const char* argv[]) {
std::cerr << parser;
return 1;
}
std::string toolValue = args::get(tool);
if (toolValue == "export-script-headers") {
std::string outPathValue = args::get(outPath);
if (exportScriptHeaders) {
std::string outPathValue = args::get(outPathExportScriptHeaders);
ScriptHeadersExporter::Export(outPathValue);
} else if (compileScripts) {
std::string inPathValue = args::get(inPathCompileScripts);
std::string outPathValue = args::get(outPathCompileScripts);
ScriptCompiler::Compile(inPathValue, outPathValue);
} else {
std::stringstream ss;
ss << "Unknown tool called '" << toolValue << "'.";
throw std::logic_error(ss.str());
return 1;
}
return 0;