Fixes issue where GetAngelscriptOwner would return an invalid value.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-11-05 13:45:51 +01:00
parent d4ece51866
commit 5334ad14da
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 52 additions and 17 deletions

View File

@ -23,10 +23,7 @@ AngelScriptScript::AngelScriptScript(const ArbUt::OptionalBorrowedPtr<void>& 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;
}

View File

@ -17,26 +17,45 @@ static std::unordered_map<const char*, const char*> _scripts =
}
}
}
})"},
{"volatile_test",
R"(namespace Pokemon {
[Pokemon effect="volatile_test"]
shared class volatile_test : PkmnScript {
void Test(){
auto mon = cast<Pokemon@>(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<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);
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<AngelScriptScript*>(s);
auto* lib = GetScriptResolver(mainLib);
auto* s = lib->LoadScript(owner, ScriptCategory::Creature, scriptName);
auto* script = dynamic_cast<AngelScriptScript*>(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<AngelScriptResolver*>(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