Bunch of fixes for Owners of scripts.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -98,17 +98,14 @@ int GetValue() { return value; }
|
||||
|
||||
};
|
||||
|
||||
static AngelScriptResolver* _resolverCache = nullptr;
|
||||
static AngelScriptResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) {
|
||||
if (_resolverCache == nullptr) {
|
||||
_resolverCache = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
||||
_resolverCache->Initialize(mainLib);
|
||||
for (auto kv : _scripts) {
|
||||
_resolverCache->CreateScript(kv.first, kv.second);
|
||||
}
|
||||
_resolverCache->FinalizeModule();
|
||||
auto res = dynamic_cast<AngelScriptResolver*>(mainLib->GetScriptResolver().get());
|
||||
res->Reset(mainLib);
|
||||
for (auto kv : _scripts) {
|
||||
res->CreateScript(kv.first, kv.second);
|
||||
}
|
||||
return _resolverCache;
|
||||
res->FinalizeModule();
|
||||
return res;
|
||||
}
|
||||
|
||||
static AngelScriptScript* GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const ArbUt::StringView& scriptName) {
|
||||
|
||||
63
tests/ScriptTests/ScriptOwnerTest.cpp
Normal file
63
tests/ScriptTests/ScriptOwnerTest.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifdef TESTS_BUILD
|
||||
#include "../../extern/doctest.hpp"
|
||||
#include "../../src/Battling/Pokemon/CreatePokemon.hpp"
|
||||
#include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
|
||||
#include "../../src/ScriptResolving/AngelScript/ContextPool.hpp"
|
||||
#include "../TestLibrary/TestLibrary.hpp"
|
||||
|
||||
static std::unordered_map<const char*, const char*> _scripts =
|
||||
std::unordered_map<const char*, const char*>{{"basic_ownership_test",
|
||||
R"(namespace Pokemon {
|
||||
[Pokemon effect="basic_ownership_test"]
|
||||
shared class basic_ownership_test : PkmnScript {
|
||||
void Test(){
|
||||
auto mon = cast<Pokemon@>(GetOwner());
|
||||
if (mon is null){
|
||||
throw("Owner was null!");
|
||||
}
|
||||
}
|
||||
}
|
||||
})"}};
|
||||
|
||||
static AngelScriptResolver* _resolverCache = nullptr;
|
||||
static AngelScriptResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) {
|
||||
if (_resolverCache == nullptr) {
|
||||
_resolverCache = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
||||
_resolverCache->Initialize(mainLib);
|
||||
for (auto kv : _scripts) {
|
||||
_resolverCache->CreateScript(kv.first, kv.second);
|
||||
}
|
||||
_resolverCache->FinalizeModule();
|
||||
}
|
||||
return _resolverCache;
|
||||
}
|
||||
|
||||
static AngelScriptScript* GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const ArbUt::StringView& scriptName,
|
||||
PkmnLib::Battling::Pokemon* owner) {
|
||||
auto lib = GetScriptResolver(mainLib);
|
||||
auto s = lib->LoadScript(owner, ScriptCategory::Creature, scriptName);
|
||||
auto script = dynamic_cast<AngelScriptScript*>(s);
|
||||
REQUIRE(script != nullptr);
|
||||
|
||||
return script;
|
||||
}
|
||||
|
||||
TEST_CASE("Basic script owner tests.") {
|
||||
auto lib = TestLibrary::GetLibrary();
|
||||
auto mon = PkmnLib::Battling::CreatePokemon(lib, "testSpecies"_cnc, 1).Build();
|
||||
auto script = GetScript(lib, "basic_ownership_test"_cnc, mon);
|
||||
REQUIRE(script != nullptr);
|
||||
|
||||
script->OnRemove();
|
||||
|
||||
auto ctxPool = script->GetContextPool();
|
||||
auto ctx = ctxPool->RequestContext();
|
||||
script->PrepareMethod("Test"_cnc, ctx);
|
||||
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
||||
ctxPool->ReturnContextToPool(ctx);
|
||||
|
||||
delete script;
|
||||
delete mon;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user