diff --git a/CMakeLists.txt b/CMakeLists.txt index 984d99d..3198183 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/conanfile.py b/conanfile.py index c76a18b..9e977de 100644 --- a/conanfile.py +++ b/conanfile.py @@ -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") diff --git a/src/Precompiled.hxx b/src/Precompiled.hxx new file mode 100644 index 0000000..316af3c --- /dev/null +++ b/src/Precompiled.hxx @@ -0,0 +1,6 @@ +#ifndef PKMNLIBTOOLS_PRECOMPILED_HXX +#define PKMNLIBTOOLS_PRECOMPILED_HXX + +#include + +#endif // PKMNLIBTOOLS_PRECOMPILED_HXX diff --git a/src/Tools/ScriptCompiler.cpp b/src/Tools/ScriptCompiler.cpp new file mode 100644 index 0000000..5846f0b --- /dev/null +++ b/src/Tools/ScriptCompiler.cpp @@ -0,0 +1,50 @@ +#include "ScriptCompiler.hpp" +#include +#include + +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(t)), + std::istreambuf_iterator()); + 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; +} diff --git a/src/Tools/ScriptCompiler.hpp b/src/Tools/ScriptCompiler.hpp new file mode 100644 index 0000000..9d246c0 --- /dev/null +++ b/src/Tools/ScriptCompiler.hpp @@ -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 diff --git a/src/main.cpp b/src/main.cpp index 04c9dfa..464d667 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 tool(parser, "tool", "The tool to execute"); - args::ValueFlag 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 outPathExportScriptHeaders(exportScriptHeaders, "outpath", "The path to output to.", + {'o'}); + args::ValueFlag inPathCompileScripts(compileScripts, "inpath", "The path to read scripts from.", + {'i'}); + args::ValueFlag 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;