From 5334ad14daa97170a596455e17ad356356008725 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 5 Nov 2021 13:45:51 +0100 Subject: [PATCH] Fixes issue where GetAngelscriptOwner would return an invalid value. --- .../AngelScript/AngelScriptScript.cpp | 7 +-- tests/ScriptTests/ScriptOwnerTest.cpp | 62 +++++++++++++++---- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/ScriptResolving/AngelScript/AngelScriptScript.cpp b/src/ScriptResolving/AngelScript/AngelScriptScript.cpp index f82eec8..838d69b 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptScript.cpp +++ b/src/ScriptResolving/AngelScript/AngelScriptScript.cpp @@ -23,10 +23,7 @@ AngelScriptScript::AngelScriptScript(const ArbUt::OptionalBorrowedPtr& own if (ownerType == nullptr) { THROW("Script was created with owner value, but with unknown owner type.") } - CScriptHandle* handle = nullptr; - AngelScriptUtils::AngelscriptFunctionCall( - _type->GetGetOwner().Function, _ctxPool, _obj, _resolver, GetName(), [&](asIScriptContext*) {}, - [&](asIScriptContext* ctx) { handle = (CScriptHandle*)ctx->GetReturnAddress(); }); + auto* handle = GetAngelscriptOwner(); handle->Set(owner.GetValue(), ownerType); } } @@ -35,7 +32,7 @@ CScriptHandle* AngelScriptScript::GetAngelscriptOwner() { CScriptHandle* handle = nullptr; AngelScriptUtils::AngelscriptFunctionCall( _type->GetGetOwner().Function, _ctxPool, _obj, _resolver, GetName(), [&](asIScriptContext*) {}, - [&](asIScriptContext* ctx) { handle = (CScriptHandle*)ctx->GetReturnObject(); }); + [&](asIScriptContext* ctx) { handle = (CScriptHandle*)ctx->GetReturnAddress(); }); return handle; } diff --git a/tests/ScriptTests/ScriptOwnerTest.cpp b/tests/ScriptTests/ScriptOwnerTest.cpp index 69c9191..8e447a9 100644 --- a/tests/ScriptTests/ScriptOwnerTest.cpp +++ b/tests/ScriptTests/ScriptOwnerTest.cpp @@ -17,26 +17,45 @@ static std::unordered_map _scripts = } } } +})"}, + {"volatile_test", + R"(namespace Pokemon { + [Pokemon effect="volatile_test"] + shared class volatile_test : PkmnScript { + void Test(){ + auto mon = cast(GetOwner()); + if (mon is null){ + throw("Owner was null!"); + } + } + } })"}}; -static AngelScriptResolver* _resolverCache = nullptr; +void MessageCallback(const asSMessageInfo* msg, void*) { + const char* type = "ERR "; + if (msg->type == asMSGTYPE_WARNING) + type = "WARN"; + else if (msg->type == asMSGTYPE_INFORMATION) + type = "INFO"; + printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message); +} + static AngelScriptResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) { - if (_resolverCache == nullptr) { - _resolverCache = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); - _resolverCache->Initialize(mainLib); - for (auto kv : _scripts) { - _resolverCache->CreateScript(kv.first, kv.second); - } - _resolverCache->FinalizeModule(); + auto res = dynamic_cast(mainLib->GetScriptResolver().get()); + res->Reset(mainLib); + res->GetEngine()->SetMessageCallback(asFUNCTION(MessageCallback), nullptr, asCALL_CDECL); + 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, PkmnLib::Battling::Pokemon* owner) { - auto lib = GetScriptResolver(mainLib); - auto s = lib->LoadScript(owner, ScriptCategory::Creature, scriptName); - auto script = dynamic_cast(s); + auto* lib = GetScriptResolver(mainLib); + auto* s = lib->LoadScript(owner, ScriptCategory::Creature, scriptName); + auto* script = dynamic_cast(s); REQUIRE(script != nullptr); return script; @@ -60,4 +79,23 @@ TEST_CASE("Basic script owner tests.") { delete mon; } +TEST_CASE("Add volatile and get owner.") { + auto lib = TestLibrary::GetLibrary(); + auto mon = PkmnLib::Battling::CreatePokemon(lib, "testSpecies"_cnc, 1).Build(); + + auto res = dynamic_cast(lib->GetScriptResolver().get()); + res->Reset(lib); + for (auto kv : _scripts) { + res->CreateScript(kv.first, kv.second); + } + res->FinalizeModule(); + auto script = (AngelScriptScript*)mon->AddVolatileScript("volatile_test"_cnc); + REQUIRE(script != nullptr); + + auto owner = script->GetAngelscriptOwner(); + REQUIRE_EQ(owner->GetType(), res->GetEngine()->GetTypeInfoByName("Pokemon")); + + delete mon; +} + #endif \ No newline at end of file