Fix #14853: [Plug-in] Subscribing to a non-existing event crashes game

This commit is contained in:
Ted John 2023-06-25 23:49:44 +01:00
parent caa21b740c
commit 30664df8e9
1 changed files with 14 additions and 2 deletions

View File

@ -301,6 +301,19 @@ namespace OpenRCT2::Scripting
return 1;
}
# ifdef _MSC_VER
// HACK workaround to resolve issue #14853
// The exception thrown in duk_error was causing a crash when RAII kicked in for this lambda.
// Only ensuring it was not in the same generated method fixed it.
__declspec(noinline)
# endif
std::shared_ptr<ScDisposable> CreateSubscription(HOOK_TYPE hookType, const DukValue& callback)
{
auto owner = _execInfo.GetCurrentPlugin();
auto cookie = _hookEngine.Subscribe(hookType, owner, callback);
return std::make_shared<ScDisposable>([this, hookType, cookie]() { _hookEngine.Unsubscribe(hookType, cookie); });
}
std::shared_ptr<ScDisposable> subscribe(const std::string& hook, const DukValue& callback)
{
auto& scriptEngine = GetContext()->GetScriptEngine();
@ -328,8 +341,7 @@ namespace OpenRCT2::Scripting
duk_error(ctx, DUK_ERR_ERROR, "Hook type not available for this plugin type.");
}
auto cookie = _hookEngine.Subscribe(hookType, owner, callback);
return std::make_shared<ScDisposable>([this, hookType, cookie]() { _hookEngine.Unsubscribe(hookType, cookie); });
return CreateSubscription(hookType, callback);
}
void queryAction(const std::string& action, const DukValue& args, const DukValue& callback)