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

178 lines
5.3 KiB
C++
Raw Normal View History

2018-03-18 23:35:58 +00:00
/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 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
#include <unordered_map>
2018-07-31 17:59:48 +02: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-03-26 12:16:47 +01:00
uint32_t HookEngine::Subscribe(HookType type, std::shared_ptr<Plugin> owner, const DukValue& 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;
}
}
}
2018-03-23 22:39:12 +00:00
void HookEngine::UnsubscribeAll(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)
{
_scriptEngine.ExecutePluginCall(hook.Owner, hook.Function, {}, isGameStateMutable);
2018-03-18 23:35:58 +00:00
}
}
2025-03-26 12:16:47 +01:00
void HookEngine::Call(HookType type, const DukValue& arg, bool isGameStateMutable)
2020-02-10 23:33:00 +00:00
{
auto& hookList = GetHookList(type);
for (auto& hook : hookList.Hooks)
{
_scriptEngine.ExecutePluginCall(hook.Owner, hook.Function, { arg }, isGameStateMutable);
2020-02-10 23:33:00 +00:00
}
}
2020-02-23 12:22:15 +00:00
void HookEngine::Call(
2025-03-26 12:16:47 +01:00
HookType type, const std::initializer_list<std::pair<std::string_view, std::any>>& args, bool isGameStateMutable)
2019-07-25 22:59:26 +01:00
{
auto& hookList = GetHookList(type);
for (auto& hook : hookList.Hooks)
{
auto ctx = _scriptEngine.GetContext();
2019-07-25 22:59:26 +01:00
// Convert key/value pairs into an object
2019-07-25 22:59:26 +01:00
auto objIdx = duk_push_object(ctx);
for (const auto& arg : args)
{
if (arg.second.type() == typeid(int32_t))
{
auto val = std::any_cast<int32_t>(arg.second);
duk_push_int(ctx, val);
}
else if (arg.second.type() == typeid(std::string))
{
const auto& val = std::any_cast<std::string>(arg.second);
duk_push_string(ctx, val.c_str());
}
else
{
throw std::runtime_error("Not implemented");
}
duk_put_prop_string(ctx, objIdx, arg.first.data());
}
std::vector<DukValue> dukArgs;
dukArgs.push_back(DukValue::take_from_stack(ctx));
_scriptEngine.ExecutePluginCall(hook.Owner, hook.Function, dukArgs, 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