Adds support for including angelscript files using #include pragma, as long as they're within a set source directory.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2021-05-13 12:47:02 +02:00
parent 5c6bbf485f
commit 109042ff52
2 changed files with 36 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
#include "AngelScriptResolver.hpp"
#include <CreatureLib/Battling/Models/Creature.hpp>
#include <filesystem>
#include <regex>
#include "../../../extern/angelscript_addons/scriptdictionary/scriptdictionary.h"
#include "../../../extern/angelscript_addons/scripthandle/scripthandle.h"
@@ -85,6 +86,7 @@ void AngelScriptResolver::Initialize(CreatureLib::Battling::BattleLibrary* arg,
r = _engine->RegisterGlobalFunction("void print(const constString &in)", asFUNCTION(PrintConst), asCALL_CDECL);
if (r < 0)
throw ArbUt::Exception("Registering print function failed.");
_builder.SetIncludeCallback(IncludeCallback, this);
}
_builder.StartNewModule(_engine, "pkmn");
@@ -401,4 +403,27 @@ void AngelScriptResolver::InitializeByteCode(
}
}
_typeDatabase = typeDatabase;
}
}
i32 AngelScriptResolver::IncludeCallback(const char* include, const char*, CScriptBuilder* builder, void* userParam) {
auto* r = reinterpret_cast<AngelScriptResolver*>(userParam);
// If source directory is not set, bail out.
if (r->_sourceDirectory.empty()) {
return -100;
}
auto root = std::filesystem::path(r->_sourceDirectory);
// Resolve any special operators, to get the actual path.
auto path = (root / std::filesystem::path(include)).lexically_normal();
// Validate the path is inside the root directory. If not, bail out.
auto [rootEnd, nothing] = std::mismatch(root.begin(), root.end(), path.begin());
if (rootEnd != root.end()) {
return -101;
}
// If the file doesn't exist, bail out.
if (!std::filesystem::exists(path)) {
return -102;
}
return builder->AddSectionFromFile(path.c_str());
}