Merge pull request #20473 from IntelOrca/fix-14853

Fix #14853: [Plug-in] Subscribing to a non-existing event crashes game
This commit is contained in:
Matthias Moninger 2023-06-27 23:07:24 +03:00 committed by GitHub
commit b347cbce03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 2 deletions

View File

@ -255,6 +255,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();
@ -282,8 +295,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)