Support for saving compiled AngelScript to either file or RAM, so we can reuse it.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-04-11 14:42:49 +02:00
parent 0b045db811
commit bf36103c11
6 changed files with 228 additions and 15 deletions

View File

@@ -79,4 +79,68 @@ TEST_CASE("Build script resolver, create object, invoke addition method") {
delete lib;
}
TEST_CASE("Get a script resolver, save the byte code to memory, create new script resolver from it") {
auto originLib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
originLib->Initialize(TestLibrary::GetLibrary());
originLib->CreateScript("testScript1" , _scripts["testScript1"]);
originLib->FinalizeModule();
size_t size;
auto byteCode = originLib->WriteByteCodeToMemory(size);
auto typeDatabase = originLib->GetTypeDatabase();
Dictionary<ScriptCategory, Dictionary<ConstString, const char*>> types;
for (auto& innerDb: typeDatabase){
Dictionary<ConstString, const char*> newInnerDb;
for (auto& kv : innerDb.second){
INFO(kv.second->GetDecl());
newInnerDb.Insert(kv.first, kv.second->GetDecl());
}
types.Insert(innerDb.first, newInnerDb);
}
auto newLib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
newLib->Initialize(TestLibrary::GetLibrary());
newLib->LoadByteCodeFromMemory(byteCode, size, types);
auto obj =
dynamic_cast<AngelScriptScript*>(newLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
REQUIRE(obj != nullptr);
delete obj;
delete originLib;
delete newLib;
free(byteCode);
}
TEST_CASE("Get a script resolver, save the byte code to file, create new script resolver from it") {
const char* TestFileName = "compiledAngelScriptTestFile.bin";
auto originLib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
originLib->Initialize(TestLibrary::GetLibrary());
originLib->CreateScript("testScript1" , _scripts["testScript1"]);
originLib->FinalizeModule();
originLib->WriteByteCodeToFile(TestFileName);
auto typeDatabase = originLib->GetTypeDatabase();
Dictionary<ScriptCategory, Dictionary<ConstString, const char*>> types;
for (auto& innerDb: typeDatabase){
Dictionary<ConstString, const char*> newInnerDb;
for (auto& kv : innerDb.second){
INFO(kv.second->GetDecl());
newInnerDb.Insert(kv.first, kv.second->GetDecl());
}
types.Insert(innerDb.first, newInnerDb);
}
auto newLib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
newLib->Initialize(TestLibrary::GetLibrary());
newLib->LoadByteCodeFromFile(TestFileName, types);
auto obj =
dynamic_cast<AngelScriptScript*>(newLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
REQUIRE(obj != nullptr);
delete obj;
delete originLib;
delete newLib;
remove(TestFileName);
}
#endif