Fixes issue where GetAngelscriptOwner would return an invalid value.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
d4ece51866
commit
5334ad14da
|
@ -23,10 +23,7 @@ AngelScriptScript::AngelScriptScript(const ArbUt::OptionalBorrowedPtr<void>& own
|
||||||
if (ownerType == nullptr) {
|
if (ownerType == nullptr) {
|
||||||
THROW("Script was created with owner value, but with unknown owner type.")
|
THROW("Script was created with owner value, but with unknown owner type.")
|
||||||
}
|
}
|
||||||
CScriptHandle* handle = nullptr;
|
auto* handle = GetAngelscriptOwner();
|
||||||
AngelScriptUtils::AngelscriptFunctionCall(
|
|
||||||
_type->GetGetOwner().Function, _ctxPool, _obj, _resolver, GetName(), [&](asIScriptContext*) {},
|
|
||||||
[&](asIScriptContext* ctx) { handle = (CScriptHandle*)ctx->GetReturnAddress(); });
|
|
||||||
handle->Set(owner.GetValue(), ownerType);
|
handle->Set(owner.GetValue(), ownerType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +32,7 @@ CScriptHandle* AngelScriptScript::GetAngelscriptOwner() {
|
||||||
CScriptHandle* handle = nullptr;
|
CScriptHandle* handle = nullptr;
|
||||||
AngelScriptUtils::AngelscriptFunctionCall(
|
AngelScriptUtils::AngelscriptFunctionCall(
|
||||||
_type->GetGetOwner().Function, _ctxPool, _obj, _resolver, GetName(), [&](asIScriptContext*) {},
|
_type->GetGetOwner().Function, _ctxPool, _obj, _resolver, GetName(), [&](asIScriptContext*) {},
|
||||||
[&](asIScriptContext* ctx) { handle = (CScriptHandle*)ctx->GetReturnObject(); });
|
[&](asIScriptContext* ctx) { handle = (CScriptHandle*)ctx->GetReturnAddress(); });
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
static AngelScriptResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) {
|
||||||
if (_resolverCache == nullptr) {
|
auto res = dynamic_cast<AngelScriptResolver*>(mainLib->GetScriptResolver().get());
|
||||||
_resolverCache = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
res->Reset(mainLib);
|
||||||
_resolverCache->Initialize(mainLib);
|
res->GetEngine()->SetMessageCallback(asFUNCTION(MessageCallback), nullptr, asCALL_CDECL);
|
||||||
for (auto kv : _scripts) {
|
for (auto kv : _scripts) {
|
||||||
_resolverCache->CreateScript(kv.first, kv.second);
|
res->CreateScript(kv.first, kv.second);
|
||||||
}
|
|
||||||
_resolverCache->FinalizeModule();
|
|
||||||
}
|
}
|
||||||
return _resolverCache;
|
res->FinalizeModule();
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static AngelScriptScript* GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const ArbUt::StringView& scriptName,
|
static AngelScriptScript* GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const ArbUt::StringView& scriptName,
|
||||||
PkmnLib::Battling::Pokemon* owner) {
|
PkmnLib::Battling::Pokemon* owner) {
|
||||||
auto lib = GetScriptResolver(mainLib);
|
auto* lib = GetScriptResolver(mainLib);
|
||||||
auto s = lib->LoadScript(owner, ScriptCategory::Creature, scriptName);
|
auto* s = lib->LoadScript(owner, ScriptCategory::Creature, scriptName);
|
||||||
auto script = dynamic_cast<AngelScriptScript*>(s);
|
auto* script = dynamic_cast<AngelScriptScript*>(s);
|
||||||
REQUIRE(script != nullptr);
|
REQUIRE(script != nullptr);
|
||||||
|
|
||||||
return script;
|
return script;
|
||||||
|
@ -60,4 +79,23 @@ TEST_CASE("Basic script owner tests.") {
|
||||||
delete mon;
|
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
|
#endif
|
Loading…
Reference in New Issue