Add script category to script creation function in AngelScript.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-02-13 13:59:07 +01:00
parent eeedbdac03
commit 6032610de6
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
11 changed files with 45 additions and 29 deletions

View File

@ -44,7 +44,7 @@ class PkmnLibConan(ConanFile):
self.options["AngelScript"].link_std_statically = True self.options["AngelScript"].link_std_statically = True
def requirements(self): def requirements(self):
self.requires("CreatureLib/42050de18a9315921397d1da75b99671013203be@creaturelib/master") self.requires("CreatureLib/2dcb197191f8e26a106c032f09476073cce0f2df@creaturelib/master")
if self.options.script_handler == "angelscript": if self.options.script_handler == "angelscript":
self.requires("AngelScript/2.34@AngelScript/Deukhoofd") self.requires("AngelScript/2.34@AngelScript/Deukhoofd")
else: else:

View File

@ -110,7 +110,7 @@ void AngelScripResolver::FinalizeModule() {
if (r < 0) if (r < 0)
throw CreatureException("Building Script Module failed."); throw CreatureException("Building Script Module failed.");
} }
void AngelScripResolver::CreateScript(const char* scriptName) { void AngelScripResolver::CreateScript(ScriptCategory category, const char* scriptName) {
auto scriptString = _loadFunc(scriptName); auto scriptString = _loadFunc(category, scriptName);
_mainModule->AddScriptSection(scriptName, scriptString); _mainModule->AddScriptSection(scriptName, scriptString);
} }

View File

@ -15,7 +15,7 @@ private:
asIScriptEngine* _engine = nullptr; asIScriptEngine* _engine = nullptr;
asIScriptModule* _mainModule = nullptr; asIScriptModule* _mainModule = nullptr;
ContextPool* _contextPool = nullptr; ContextPool* _contextPool = nullptr;
const char* (*_loadFunc)(const char*) = nullptr; const char* (*_loadFunc)(ScriptCategory category, const char*) = nullptr;
std::unordered_map<std::string, AngelScriptTypeInfo*> _types; std::unordered_map<std::string, AngelScriptTypeInfo*> _types;
static void MessageCallback(const asSMessageInfo* msg, void* param); static void MessageCallback(const asSMessageInfo* msg, void* param);
@ -36,9 +36,11 @@ public:
void Initialize(CreatureLib::Battling::BattleLibrary* library) override; void Initialize(CreatureLib::Battling::BattleLibrary* library) override;
void SetCreateFunction(const char* (*loadFunc)(const char* scriptName)) { _loadFunc = loadFunc; } void SetCreateFunction(const char* (*loadFunc)(ScriptCategory category, const char* scriptName)) {
_loadFunc = loadFunc;
}
void CreateScript(const char* scriptName); void CreateScript(ScriptCategory category, const char* scriptName);
void FinalizeModule(); void FinalizeModule();

View File

@ -59,7 +59,9 @@ void StopBeforeAttack(ExecutingMove@ attack, bool& result) override{
}; };
static const char* _testLoadFunc(const char* name) { return _scripts[name]; } static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
static AngelScripResolver* _resolverCache = nullptr; static AngelScripResolver* _resolverCache = nullptr;
static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) { static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) {
@ -68,7 +70,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
_resolverCache->Initialize(mainLib); _resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc); _resolverCache->SetCreateFunction(&_testLoadFunc);
for (auto kv : _scripts) { for (auto kv : _scripts) {
_resolverCache->CreateScript(kv.first); _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack, kv.first);
} }
_resolverCache->FinalizeModule(); _resolverCache->FinalizeModule();
} }

View File

@ -4,8 +4,7 @@
#include "../TestLibrary/TestLibrary.hpp" #include "../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts = static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{ std::unordered_map<const char*, const char*>{{"testScript1", R"(
{"testScript1", R"(
class testScript1 { class testScript1 {
int add(int a, int b) { int add(int a, int b) {
return a + b; return a + b;
@ -17,10 +16,11 @@ class testScript1 {
} }
} }
} }
)"} )"}};
};
static const char* _testLoadFunc(const char* name) { return _scripts[name]; } 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") { TEST_CASE("Get a script resolver, initialize it, then delete it") {
auto lib = PkmnLib::Battling::BattleLibrary::CreateScriptResolver(); auto lib = PkmnLib::Battling::BattleLibrary::CreateScriptResolver();
@ -39,7 +39,7 @@ TEST_CASE("Get a script resolver, set script load function, load script, then bu
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary()); lib->Initialize(TestLibrary::GetLibrary());
lib->SetCreateFunction(&_testLoadFunc); lib->SetCreateFunction(&_testLoadFunc);
lib->CreateScript("testScript1"); lib->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
lib->FinalizeModule(); lib->FinalizeModule();
delete lib; delete lib;
} }
@ -48,7 +48,7 @@ TEST_CASE("Build script resolver, then create object") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary()); lib->Initialize(TestLibrary::GetLibrary());
lib->SetCreateFunction(&_testLoadFunc); lib->SetCreateFunction(&_testLoadFunc);
lib->CreateScript("testScript1"); lib->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
lib->FinalizeModule(); lib->FinalizeModule();
auto obj = lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1"); auto obj = lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1");
@ -61,7 +61,7 @@ TEST_CASE("Build script resolver, create object, invoke addition method") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary()); lib->Initialize(TestLibrary::GetLibrary());
lib->SetCreateFunction(&_testLoadFunc); lib->SetCreateFunction(&_testLoadFunc);
lib->CreateScript("testScript1"); lib->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
lib->FinalizeModule(); lib->FinalizeModule();
auto obj = auto obj =

View File

@ -26,7 +26,9 @@ class testScript1 {
} }
)"}}; )"}};
static const char* _testLoadFunc(const char* name) { return _scripts[name]; } static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData { struct ScriptData {
AngelScriptScript* Script = nullptr; AngelScriptScript* Script = nullptr;
@ -46,7 +48,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib); _resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc); _resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript("testScript1"); _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->FinalizeModule(); _resolverCache->FinalizeModule();
} }
return _resolverCache; return _resolverCache;

View File

@ -17,7 +17,9 @@ class testScript1 {
} }
)"}}; )"}};
static const char* _testLoadFunc(const char* name) { return _scripts[name]; } static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData { struct ScriptData {
AngelScriptScript* Script = nullptr; AngelScriptScript* Script = nullptr;
@ -37,7 +39,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib); _resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc); _resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript("testScript1"); _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->FinalizeModule(); _resolverCache->FinalizeModule();
} }
return _resolverCache; return _resolverCache;

View File

@ -13,7 +13,9 @@ class testScript1 {
} }
)"}}; )"}};
static const char* _testLoadFunc(const char* name) { return _scripts[name]; } static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData { struct ScriptData {
AngelScriptScript* Script = nullptr; AngelScriptScript* Script = nullptr;
@ -33,7 +35,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib); _resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc); _resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript("testScript1"); _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->FinalizeModule(); _resolverCache->FinalizeModule();
} }
return _resolverCache; return _resolverCache;

View File

@ -17,7 +17,9 @@ class testScript1 {
} }
)"}}; )"}};
static const char* _testLoadFunc(const char* name) { return _scripts[name]; } static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData { struct ScriptData {
AngelScriptScript* Script = nullptr; AngelScriptScript* Script = nullptr;
@ -37,7 +39,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib); _resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc); _resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript("testScript1"); _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->FinalizeModule(); _resolverCache->FinalizeModule();
} }
return _resolverCache; return _resolverCache;

View File

@ -15,7 +15,9 @@ class testScript1 {
} }
)"}}; )"}};
static const char* _testLoadFunc(const char* name) { return _scripts[name]; } static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData { struct ScriptData {
AngelScriptScript* Script; AngelScriptScript* Script;
@ -35,7 +37,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib); _resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc); _resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript("testScript1"); _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->FinalizeModule(); _resolverCache->FinalizeModule();
} }
return _resolverCache; return _resolverCache;

View File

@ -16,7 +16,9 @@ class testScript1 {
} }
)"}}; )"}};
static const char* _testLoadFunc(const char* name) { return _scripts[name]; } static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData { struct ScriptData {
AngelScriptScript* Script; AngelScriptScript* Script;
@ -36,7 +38,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib); _resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc); _resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript("testScript1"); _resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->FinalizeModule(); _resolverCache->FinalizeModule();
} }
return _resolverCache; return _resolverCache;