From 321afbebe49cb4c46eefa7a36bbaa4d7ba002455 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 16 Feb 2020 14:38:05 +0100 Subject: [PATCH] Use namespaces to differentiate between different categories of scripts. --- .../AngelScript/AngelScripResolver.cpp | 21 +++++++++++++----- .../AngelScript/AngelScripResolver.hpp | 7 ------ tests/ScriptTests/BaseScriptClassTests.cpp | 9 ++------ tests/ScriptTests/ScriptResolverTests.cpp | 22 +++++-------------- .../ScriptTypeTests/Battle/PokemonTests.cpp | 10 +++------ .../ScriptTypeTests/Library/FormesTests.cpp | 10 +++------ .../ScriptTypeTests/Library/ItemDataTests.cpp | 10 +++------ .../ScriptTypeTests/Library/MoveTests.cpp | 10 +++------ .../ScriptTypeTests/Library/SpeciesTests.cpp | 10 +++------ .../Library/StaticLibraryTests.cpp | 10 +++------ 10 files changed, 41 insertions(+), 78 deletions(-) diff --git a/src/ScriptResolving/AngelScript/AngelScripResolver.cpp b/src/ScriptResolving/AngelScript/AngelScripResolver.cpp index e9f7395..a9e33ce 100644 --- a/src/ScriptResolving/AngelScript/AngelScripResolver.cpp +++ b/src/ScriptResolving/AngelScript/AngelScripResolver.cpp @@ -96,8 +96,23 @@ void AngelScripResolver::MessageCallback(const asSMessageInfo* msg, void* param) type = "INFO"; printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message); } + +static constexpr const char* GetCategoryNamespace(AngelScripResolver::ScriptCategory category){ + switch (category){ + case CreatureLib::Battling::ScriptResolver::ScriptCategory::Attack: return "Moves"; + case CreatureLib::Battling::ScriptResolver::ScriptCategory::Talent: return "Abilities"; + case CreatureLib::Battling::ScriptResolver::ScriptCategory::Status: return "Status"; + case CreatureLib::Battling::ScriptResolver::ScriptCategory::Creature: return "Pokemon"; + case CreatureLib::Battling::ScriptResolver::ScriptCategory::Battle: return "Battle"; + case CreatureLib::Battling::ScriptResolver::ScriptCategory::Side: return "Side"; + default: throw CreatureException("Unknown script category"); + } +} + CreatureLib::Battling::Script* AngelScripResolver::LoadScript(ScriptCategory category, const std::string& scriptName) { - auto typeInfo = GetTypeInfo(scriptName); + std::stringstream decl; + decl << GetCategoryNamespace(category) << "::" << scriptName; + auto typeInfo = GetTypeInfo(decl.str()); if (typeInfo == nullptr) return nullptr; auto ctx = _contextPool->RequestContext(); @@ -110,10 +125,6 @@ void AngelScripResolver::FinalizeModule() { if (r < 0) throw CreatureException("Building Script Module failed."); } -void AngelScripResolver::CreateScript(ScriptCategory category, const char* scriptName) { - auto scriptString = _loadFunc(category, scriptName); - _mainModule->AddScriptSection(scriptName, scriptString); -} void AngelScripResolver::CreateScript(const char* name, const char* script) { _mainModule->AddScriptSection(name, script); } diff --git a/src/ScriptResolving/AngelScript/AngelScripResolver.hpp b/src/ScriptResolving/AngelScript/AngelScripResolver.hpp index e5ef710..933757d 100644 --- a/src/ScriptResolving/AngelScript/AngelScripResolver.hpp +++ b/src/ScriptResolving/AngelScript/AngelScripResolver.hpp @@ -15,7 +15,6 @@ private: asIScriptEngine* _engine = nullptr; asIScriptModule* _mainModule = nullptr; ContextPool* _contextPool = nullptr; - const char* (*_loadFunc)(ScriptCategory category, const char*) = nullptr; std::unordered_map _types; static void MessageCallback(const asSMessageInfo* msg, void* param); @@ -35,12 +34,6 @@ public: } void Initialize(CreatureLib::Battling::BattleLibrary* library) override; - - void SetCreateFunction(const char* (*loadFunc)(ScriptCategory category, const char* scriptName)) { - _loadFunc = loadFunc; - } - - void CreateScript(ScriptCategory category, const char* scriptName); void CreateScript(const char* name, const char* script); void FinalizeModule(); diff --git a/tests/ScriptTests/BaseScriptClassTests.cpp b/tests/ScriptTests/BaseScriptClassTests.cpp index 7a79240..94614f1 100644 --- a/tests/ScriptTests/BaseScriptClassTests.cpp +++ b/tests/ScriptTests/BaseScriptClassTests.cpp @@ -5,7 +5,7 @@ #include "../TestLibrary/TestLibrary.hpp" #define AS_CLASS(name, contents) \ - { #name, "class " #name " : PkmnScript { " contents "}" } + { #name, "namespace Pokemon{ class " #name " : PkmnScript { " contents "}}" } static std::unordered_map _scripts = std::unordered_map{ AS_CLASS(blankScript, ), @@ -62,18 +62,13 @@ void StopBeforeAttack(ExecutingMove@ attack, bool& result) override{ }; -static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) { - return _scripts[name]; -} - static AngelScripResolver* _resolverCache = nullptr; static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) { if (_resolverCache == nullptr) { _resolverCache = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache->Initialize(mainLib); - _resolverCache->SetCreateFunction(&_testLoadFunc); for (auto kv : _scripts) { - _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack, kv.first); + _resolverCache->CreateScript(kv.first , kv.second); } _resolverCache->FinalizeModule(); } diff --git a/tests/ScriptTests/ScriptResolverTests.cpp b/tests/ScriptTests/ScriptResolverTests.cpp index 79cc501..ea36391 100644 --- a/tests/ScriptTests/ScriptResolverTests.cpp +++ b/tests/ScriptTests/ScriptResolverTests.cpp @@ -5,6 +5,7 @@ static std::unordered_map _scripts = std::unordered_map{{"testScript1", R"( +namespace Pokemon{ class testScript1 { int add(int a, int b) { return a + b; @@ -16,11 +17,8 @@ class testScript1 { } } } -)"}}; - -static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) { - return _scripts[name]; } +)"}}; TEST_CASE("Get a script resolver, initialize it, then delete it") { auto lib = PkmnLib::Battling::BattleLibrary::CreateScriptResolver(); @@ -28,18 +26,10 @@ TEST_CASE("Get a script resolver, initialize it, then delete it") { delete lib; } -TEST_CASE("Get a script resolver, set script load function, then delete it") { - auto lib = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); - lib->Initialize(TestLibrary::GetLibrary()); - lib->SetCreateFunction(&_testLoadFunc); - delete lib; -} - TEST_CASE("Get a script resolver, set script load function, load script, then build module") { auto lib = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); lib->Initialize(TestLibrary::GetLibrary()); - lib->SetCreateFunction(&_testLoadFunc); - lib->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1"); + lib->CreateScript("testScript1" , _scripts["testScript1"]); lib->FinalizeModule(); delete lib; } @@ -47,8 +37,7 @@ TEST_CASE("Get a script resolver, set script load function, load script, then bu TEST_CASE("Build script resolver, then create object") { auto lib = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); lib->Initialize(TestLibrary::GetLibrary()); - lib->SetCreateFunction(&_testLoadFunc); - lib->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1"); + lib->CreateScript("testScript1" , _scripts["testScript1"]); lib->FinalizeModule(); auto obj = lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1"); @@ -60,8 +49,7 @@ TEST_CASE("Build script resolver, then create object") { TEST_CASE("Build script resolver, create object, invoke addition method") { auto lib = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); lib->Initialize(TestLibrary::GetLibrary()); - lib->SetCreateFunction(&_testLoadFunc); - lib->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1"); + lib->CreateScript("testScript1" , _scripts["testScript1"]); lib->FinalizeModule(); auto obj = diff --git a/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp b/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp index edf7da6..a17ceca 100644 --- a/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp @@ -6,6 +6,7 @@ static std::unordered_map _scripts = std::unordered_map{{"testScript1", R"( +namespace Pokemon{ class testScript1 { bool testSpecies(Pokemon@ p, const Species@ species){ return p.Species is species; } bool testForme(Pokemon@ p, const Forme@ forme){ return p.Forme is forme; } @@ -24,13 +25,9 @@ class testScript1 { void testHeal(Pokemon@ p, uint32 amount){ p.Heal(amount); } bool testMove(Pokemon@ p, uint index, LearnedMove@ move){ return p.GetMoves()[index] is move; } -} +}} )"}}; -static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) { - return _scripts[name]; -} - struct ScriptData { AngelScriptScript* Script = nullptr; AngelScripResolver* Resolver = nullptr; @@ -48,8 +45,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m if (_resolverCache == nullptr) { _resolverCache = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache->Initialize(mainLib); - _resolverCache->SetCreateFunction(&_testLoadFunc); - _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1"); + _resolverCache->CreateScript("testScript1" , _scripts["testScript1"]); _resolverCache->FinalizeModule(); } return _resolverCache; diff --git a/tests/ScriptTests/ScriptTypeTests/Library/FormesTests.cpp b/tests/ScriptTests/ScriptTypeTests/Library/FormesTests.cpp index 3dae182..09ed7ed 100644 --- a/tests/ScriptTests/ScriptTypeTests/Library/FormesTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Library/FormesTests.cpp @@ -5,6 +5,7 @@ static std::unordered_map _scripts = std::unordered_map{{"testScript1", R"( +namespace Pokemon{ class testScript1 { bool testName(const Forme@ s, const string &in name){ return s.Name == name; } bool testWeight(const Forme@ s, float weight){ return s.Weight == weight; } @@ -14,13 +15,9 @@ class testScript1 { bool testGetType(const Forme@ s, uint8 type){ return s.GetType(0) == type; } bool testGetStatistic(const Forme@ s, Statistic stat, uint value){ return s.GetStatistic(stat) == value; } bool testGetAbility(const Forme@ s, const string &in ability){ return s.GetAbility(0) == ability; } -} +}} )"}}; -static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) { - return _scripts[name]; -} - struct ScriptData { AngelScriptScript* Script = nullptr; AngelScripResolver* Resolver = nullptr; @@ -38,8 +35,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m if (_resolverCache == nullptr) { _resolverCache = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache->Initialize(mainLib); - _resolverCache->SetCreateFunction(&_testLoadFunc); - _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1"); + _resolverCache->CreateScript("testScript1" , _scripts["testScript1"]); _resolverCache->FinalizeModule(); } return _resolverCache; diff --git a/tests/ScriptTests/ScriptTypeTests/Library/ItemDataTests.cpp b/tests/ScriptTests/ScriptTypeTests/Library/ItemDataTests.cpp index 8875330..278964b 100644 --- a/tests/ScriptTests/ScriptTypeTests/Library/ItemDataTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Library/ItemDataTests.cpp @@ -5,18 +5,15 @@ static std::unordered_map _scripts = std::unordered_map{{"testScript1", R"( +namespace Pokemon{ class testScript1 { bool testName(const Item@ i, const string &in name){ return i.Name == name; } bool testCategory(const Item@ i, ItemCategory category){ return i.Category == category; } bool testBattleCategory(const Item@ i, BattleItemCategory category){ return i.BattleCategory == category; } bool testPrice(const Item@ i, int price){ return i.Price == price; } -} +}} )"}}; -static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) { - return _scripts[name]; -} - struct ScriptData { AngelScriptScript* Script = nullptr; AngelScripResolver* Resolver = nullptr; @@ -34,8 +31,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m if (_resolverCache == nullptr){ _resolverCache = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache->Initialize(mainLib); - _resolverCache->SetCreateFunction(&_testLoadFunc); - _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1"); + _resolverCache->CreateScript("testScript1" , _scripts["testScript1"]); _resolverCache->FinalizeModule(); } return _resolverCache; diff --git a/tests/ScriptTests/ScriptTypeTests/Library/MoveTests.cpp b/tests/ScriptTests/ScriptTypeTests/Library/MoveTests.cpp index 7b08c3f..82121a4 100644 --- a/tests/ScriptTests/ScriptTypeTests/Library/MoveTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Library/MoveTests.cpp @@ -5,6 +5,7 @@ static std::unordered_map _scripts = std::unordered_map{{"testScript1", R"( +namespace Pokemon{ class testScript1 { bool testName(const MoveData@ s, const string &in name){ return s.Name == name; } bool testType(const MoveData@ s, uint8 type){ return s.Type == type; } @@ -14,13 +15,9 @@ class testScript1 { bool testBaseUsages(const MoveData@ s, uint8 baseUsages){ return s.BaseUsages == baseUsages; } bool testTarget(const MoveData@ s, MoveTarget target){ return s.Target == target; } bool testPriority(const MoveData@ s, int8 priority){ return s.Priority == priority; } -} +}} )"}}; -static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) { - return _scripts[name]; -} - struct ScriptData { AngelScriptScript* Script = nullptr; AngelScripResolver* Resolver = nullptr; @@ -38,8 +35,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m if (_resolverCache == nullptr){ _resolverCache = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache->Initialize(mainLib); - _resolverCache->SetCreateFunction(&_testLoadFunc); - _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1"); + _resolverCache->CreateScript("testScript1" , _scripts["testScript1"]); _resolverCache->FinalizeModule(); } return _resolverCache; diff --git a/tests/ScriptTests/ScriptTypeTests/Library/SpeciesTests.cpp b/tests/ScriptTests/ScriptTypeTests/Library/SpeciesTests.cpp index 7c524bc..bc4bb4b 100644 --- a/tests/ScriptTests/ScriptTypeTests/Library/SpeciesTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Library/SpeciesTests.cpp @@ -5,6 +5,7 @@ static std::unordered_map _scripts = std::unordered_map{{"testScript1", R"( +namespace Pokemon{ class testScript1 { bool testName(const Species@ s, const string &in name){ return s.Name == name; } bool testId(const Species@ s, uint16 id){ return s.Id == id; } @@ -12,13 +13,9 @@ class testScript1 { bool testCaptureRate(const Species@ s, uint8 rate){ return s.CaptureRate == rate; } bool testGetForme(const Species@ s, const Forme@ forme){ return s.GetForme("default") is forme; } bool testGetDefaultForme(const Species@ s, const Forme@ forme){ return s.GetDefaultForme() is forme; } -} +}} )"}}; -static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) { - return _scripts[name]; -} - struct ScriptData { AngelScriptScript* Script; AngelScripResolver* Resolver; @@ -36,8 +33,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m if (_resolverCache == nullptr){ _resolverCache = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache->Initialize(mainLib); - _resolverCache->SetCreateFunction(&_testLoadFunc); - _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1"); + _resolverCache->CreateScript("testScript1" , _scripts["testScript1"]); _resolverCache->FinalizeModule(); } return _resolverCache; diff --git a/tests/ScriptTests/ScriptTypeTests/Library/StaticLibraryTests.cpp b/tests/ScriptTests/ScriptTypeTests/Library/StaticLibraryTests.cpp index 6a2e672..379e7f5 100644 --- a/tests/ScriptTests/ScriptTypeTests/Library/StaticLibraryTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Library/StaticLibraryTests.cpp @@ -5,6 +5,7 @@ static std::unordered_map _scripts = std::unordered_map{{"testScript1", R"( +namespace Pokemon{ class testScript1 { bool testMaximumLevel(const StaticLibrary@ s, uint8 level){ return s.Settings.MaximalLevel == level; } bool testMaximumMoves(const StaticLibrary@ s, uint8 moveCount){ return s.Settings.MaximalMoves == moveCount; } @@ -13,13 +14,9 @@ class testScript1 { bool testItemLibrary(const StaticLibrary@ s, const ItemLibrary@ itemLib){ return s.ItemLibrary is itemLib; } bool testGrowthRateLibrary(const StaticLibrary@ s, const GrowthRateLibrary@ gl){ return s.GrowthRateLibrary is gl; } bool testTypeLibrary(const StaticLibrary@ s, const TypeLibrary@ tl){ return s.TypeLibrary is tl; } -} +}} )"}}; -static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) { - return _scripts[name]; -} - struct ScriptData { AngelScriptScript* Script; AngelScripResolver* Resolver; @@ -37,8 +34,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m if (_resolverCache == nullptr){ _resolverCache = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache->Initialize(mainLib); - _resolverCache->SetCreateFunction(&_testLoadFunc); - _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1"); + _resolverCache->CreateScript("testScript1" , _scripts["testScript1"]); _resolverCache->FinalizeModule(); } return _resolverCache;