Files
OpenRCT2-Web/src/openrct2/scripting/HookEngine.cpp
T

170 lines
5.3 KiB
C++
Raw Normal View History

2018-03-18 23:35:58 +00:00
/*****************************************************************************
2026-01-05 14:42:59 +01:00
* Copyright (c) 2014-2026 OpenRCT2 developers
2018-03-18 23:35:58 +00:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#ifdef ENABLE_SCRIPTING
2018-07-31 17:59:48 +02:00
#include "HookEngine.h"
2018-03-18 23:35:58 +00:00
#include "../core/EnumMap.hpp"
#include "ScriptEngine.h"
2020-02-23 12:55:48 +00:00
2018-03-18 23:35:58 +00:00
using namespace OpenRCT2::Scripting;
2025-07-31 11:01:48 +02:00
static const EnumMap<HookType> HooksLookupTable(
{
{ "action.query", HookType::actionQuery },
{ "action.execute", HookType::actionExecute },
{ "interval.tick", HookType::intervalTick },
{ "interval.day", HookType::intervalDay },
{ "network.chat", HookType::networkChat },
{ "network.authenticate", HookType::networkAuthenticate },
{ "network.join", HookType::networkJoin },
{ "network.leave", HookType::networkLeave },
{ "ride.ratings.calculate", HookType::rideRatingsCalculate },
{ "action.location", HookType::actionLocation },
{ "guest.generation", HookType::guestGeneration },
{ "vehicle.crash", HookType::vehicleCrash },
{ "map.change", HookType::mapChange },
{ "map.changed", HookType::mapChanged },
{ "map.save", HookType::mapSave },
{ "park.guest.softcap.calculate", HookType::parkCalculateGuestCap },
});
2021-07-22 20:51:15 +03:00
2025-03-26 12:16:47 +01:00
HookType OpenRCT2::Scripting::GetHookType(const std::string& name)
2018-03-18 23:35:58 +00:00
{
2021-07-22 20:51:15 +03:00
auto result = HooksLookupTable.find(name);
2025-03-26 12:16:47 +01:00
return (result != HooksLookupTable.end()) ? result->second : HookType::notDefined;
2018-03-18 23:35:58 +00:00
}
2020-03-07 22:40:45 +00:00
HookEngine::HookEngine(ScriptEngine& scriptEngine)
: _scriptEngine(scriptEngine)
2018-03-18 23:35:58 +00:00
{
2025-03-26 12:16:47 +01:00
_hookMap.resize(NUM_HookTypeS);
for (size_t i = 0; i < NUM_HookTypeS; i++)
2018-03-18 23:35:58 +00:00
{
2025-03-26 12:16:47 +01:00
_hookMap[i].Type = static_cast<HookType>(i);
2018-03-18 23:35:58 +00:00
}
}
2025-09-29 02:05:00 +10:00
uint32_t HookEngine::Subscribe(HookType type, const std::shared_ptr<Plugin>& owner, const JSCallback& function)
2018-03-18 23:35:58 +00:00
{
auto& hookList = GetHookList(type);
auto cookie = _nextCookie++;
hookList.Hooks.emplace_back(cookie, owner, function);
2018-03-18 23:35:58 +00:00
return cookie;
}
2025-03-26 12:16:47 +01:00
void HookEngine::Unsubscribe(HookType type, uint32_t cookie)
2018-03-18 23:35:58 +00:00
{
auto& hookList = GetHookList(type);
auto& hooks = hookList.Hooks;
for (auto it = hooks.begin(); it != hooks.end(); it++)
{
if (it->Cookie == cookie)
{
hooks.erase(it);
break;
}
}
}
2025-09-29 02:05:00 +10:00
void HookEngine::UnsubscribeAll(const std::shared_ptr<const Plugin>& owner)
2018-03-18 23:35:58 +00:00
{
for (auto& hookList : _hookMap)
{
auto& hooks = hookList.Hooks;
2020-04-23 19:31:56 +01:00
auto isOwner = [&](auto& obj) { return obj.Owner == owner; };
hooks.erase(std::remove_if(hooks.begin(), hooks.end(), isOwner), hooks.end());
2018-03-18 23:35:58 +00:00
}
}
2020-02-06 23:15:20 +00:00
void HookEngine::UnsubscribeAll()
{
for (auto& hookList : _hookMap)
{
auto& hooks = hookList.Hooks;
hooks.clear();
}
}
2025-03-26 12:16:47 +01:00
bool HookEngine::HasSubscriptions(HookType type) const
2020-02-10 23:33:00 +00:00
{
auto& hookList = GetHookList(type);
return !hookList.Hooks.empty();
}
2025-03-26 12:16:47 +01:00
bool HookEngine::IsValidHookForPlugin(HookType type, Plugin& plugin) const
2022-02-20 02:05:24 +00:00
{
2025-03-26 12:16:47 +01:00
if (type == HookType::mapChanged && plugin.GetMetadata().Type != PluginType::Intransient)
2022-02-20 02:05:24 +00:00
{
return false;
}
return true;
}
2025-03-26 12:16:47 +01:00
void HookEngine::Call(HookType type, bool isGameStateMutable)
2018-03-18 23:35:58 +00:00
{
auto& hookList = GetHookList(type);
for (auto& hook : hookList.Hooks)
{
2025-09-29 02:05:00 +10:00
_scriptEngine.ExecutePluginCall(hook.Owner, hook.Function.callback, {}, isGameStateMutable);
2018-03-18 23:35:58 +00:00
}
}
2025-09-29 02:05:00 +10:00
void HookEngine::Call(HookType type, const JSValue arg, bool isGameStateMutable, bool keepArgsAlive)
2020-02-10 23:33:00 +00:00
{
auto& hookList = GetHookList(type);
for (auto& hook : hookList.Hooks)
{
2025-09-29 02:05:00 +10:00
_scriptEngine.ExecutePluginCall(hook.Owner, hook.Function.callback, { arg }, isGameStateMutable, keepArgsAlive);
2020-02-10 23:33:00 +00:00
}
}
2020-02-23 12:22:15 +00:00
void HookEngine::Call(
2025-09-29 02:05:00 +10:00
HookType type, const std::initializer_list<std::pair<std::string, HookValue>>& args, bool isGameStateMutable)
2019-07-25 22:59:26 +01:00
{
auto& hookList = GetHookList(type);
for (auto& hook : hookList.Hooks)
{
2025-09-29 02:05:00 +10:00
JSContext* ctx = hook.Owner.get()->GetContext();
2019-07-25 22:59:26 +01:00
// Convert key/value pairs into an object
2025-09-29 02:05:00 +10:00
JSValue obj = JS_NewObject(ctx);
2019-07-25 22:59:26 +01:00
for (const auto& arg : args)
{
2025-09-29 02:05:00 +10:00
JSValue member = std::visit(
HookValuesToJS{
[ctx](int64_t v) { return JS_NewInt64(ctx, v); },
[ctx](uint32_t v) { return JS_NewInt64(ctx, v); },
[ctx](int32_t v) { return JS_NewInt32(ctx, v); },
[ctx](uint16_t v) { return JS_NewInt32(ctx, v); },
[ctx](int16_t v) { return JS_NewInt32(ctx, v); },
[ctx](const std::string& v) { return JSFromStdString(ctx, v); },
},
arg.second);
JS_SetPropertyStr(ctx, obj, arg.first.c_str(), member);
2019-07-25 22:59:26 +01:00
}
2025-09-29 02:05:00 +10:00
_scriptEngine.ExecutePluginCall(hook.Owner, hook.Function.callback, { obj }, isGameStateMutable);
2019-07-25 22:59:26 +01:00
}
}
2025-03-26 12:16:47 +01:00
HookList& HookEngine::GetHookList(HookType type)
2018-03-18 23:35:58 +00:00
{
auto index = static_cast<size_t>(type);
return _hookMap[index];
}
2020-02-10 23:33:00 +00:00
2025-03-26 12:16:47 +01:00
const HookList& HookEngine::GetHookList(HookType type) const
2020-02-10 23:33:00 +00:00
{
auto index = static_cast<size_t>(type);
return _hookMap[index];
}
2020-02-23 12:55:48 +00:00
#endif