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

160 lines
4.8 KiB
C++
Raw Normal View History

2018-03-18 23:35:58 +00:00
/*****************************************************************************
2020-02-23 12:55:48 +00:00
* Copyright (c) 2014-2020 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
2020-02-23 12:55:48 +00:00
# include "HookEngine.h"
2018-03-18 23:35:58 +00:00
2020-02-23 12:55:48 +00:00
# include "ScriptEngine.h"
# include <unordered_map>
2018-07-31 17:59:48 +02:00
2018-03-18 23:35:58 +00:00
using namespace OpenRCT2::Scripting;
2018-07-31 17:59:48 +02:00
HOOK_TYPE OpenRCT2::Scripting::GetHookType(const std::string& name)
2018-03-18 23:35:58 +00:00
{
static const std::unordered_map<std::string, HOOK_TYPE> LookupTable(
{ { "action.query", HOOK_TYPE::ACTION_QUERY },
{ "action.execute", HOOK_TYPE::ACTION_EXECUTE },
{ "interval.tick", HOOK_TYPE::INTERVAL_TICK },
{ "interval.day", HOOK_TYPE::INTERVAL_DAY },
{ "network.chat", HOOK_TYPE::NETWORK_CHAT },
{ "network.authenticate", HOOK_TYPE::NETWORK_AUTHENTICATE },
{ "network.join", HOOK_TYPE::NETWORK_JOIN },
{ "network.leave", HOOK_TYPE::NETWORK_LEAVE },
{ "ride.ratings.calculate", HOOK_TYPE::RIDE_RATINGS_CALCULATE },
{ "action.location", HOOK_TYPE::ACTION_LOCATION } });
2018-03-18 23:35:58 +00:00
auto result = LookupTable.find(name);
2018-07-31 17:59:48 +02:00
return (result != LookupTable.end()) ? result->second : HOOK_TYPE::UNDEFINED;
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
{
_hookMap.resize(NUM_HOOK_TYPES);
for (size_t i = 0; i < NUM_HOOK_TYPES; i++)
{
_hookMap[i].Type = static_cast<HOOK_TYPE>(i);
}
}
2018-07-31 17:59:48 +02:00
uint32_t HookEngine::Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue& function)
2018-03-18 23:35:58 +00:00
{
auto& hookList = GetHookList(type);
auto cookie = _nextCookie++;
Hook hook(cookie, owner, function);
hookList.Hooks.push_back(hook);
return cookie;
}
2018-07-31 16:25:50 +02:00
void HookEngine::Unsubscribe(HOOK_TYPE 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();
}
}
2020-02-10 23:33:00 +00:00
bool HookEngine::HasSubscriptions(HOOK_TYPE type) const
{
auto& hookList = GetHookList(type);
return !hookList.Hooks.empty();
}
2020-02-23 02:00:22 +00:00
void HookEngine::Call(HOOK_TYPE 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
}
}
void HookEngine::Call(HOOK_TYPE 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(
HOOK_TYPE 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
}
}
2018-03-18 23:35:58 +00:00
HookList& HookEngine::GetHookList(HOOK_TYPE type)
{
auto index = static_cast<size_t>(type);
return _hookMap[index];
}
2020-02-10 23:33:00 +00:00
const HookList& HookEngine::GetHookList(HOOK_TYPE type) const
{
auto index = static_cast<size_t>(type);
return _hookMap[index];
}
2020-02-23 12:55:48 +00:00
#endif