forked from OpenRCT2/OpenRCT2
-
Notifications
You must be signed in to change notification settings - Fork 4
/
HookEngine.cpp
165 lines (144 loc) · 4.87 KB
/
HookEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*****************************************************************************
* Copyright (c) 2014-2020 OpenRCT2 developers
*
* 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
# include "HookEngine.h"
# include "../core/EnumMap.hpp"
# include "ScriptEngine.h"
# include <unordered_map>
using namespace OpenRCT2::Scripting;
static const EnumMap<HOOK_TYPE> HooksLookupTable({
{ "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 },
{ "guest.generation", HOOK_TYPE::GUEST_GENERATION },
{ "vehicle.crash", HOOK_TYPE::VEHICLE_CRASH },
{ "map.save", HOOK_TYPE::MAP_SAVE },
});
HOOK_TYPE OpenRCT2::Scripting::GetHookType(const std::string& name)
{
auto result = HooksLookupTable.find(name);
return (result != HooksLookupTable.end()) ? result->second : HOOK_TYPE::UNDEFINED;
}
HookEngine::HookEngine(ScriptEngine& scriptEngine)
: _scriptEngine(scriptEngine)
{
_hookMap.resize(NUM_HOOK_TYPES);
for (size_t i = 0; i < NUM_HOOK_TYPES; i++)
{
_hookMap[i].Type = static_cast<HOOK_TYPE>(i);
}
}
uint32_t HookEngine::Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue& function)
{
auto& hookList = GetHookList(type);
auto cookie = _nextCookie++;
hookList.Hooks.emplace_back(cookie, owner, function);
return cookie;
}
void HookEngine::Unsubscribe(HOOK_TYPE type, uint32_t cookie)
{
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;
}
}
}
void HookEngine::UnsubscribeAll(std::shared_ptr<const Plugin> owner)
{
for (auto& hookList : _hookMap)
{
auto& hooks = hookList.Hooks;
auto isOwner = [&](auto& obj) { return obj.Owner == owner; };
hooks.erase(std::remove_if(hooks.begin(), hooks.end(), isOwner), hooks.end());
}
}
void HookEngine::UnsubscribeAll()
{
for (auto& hookList : _hookMap)
{
auto& hooks = hookList.Hooks;
hooks.clear();
}
}
bool HookEngine::HasSubscriptions(HOOK_TYPE type) const
{
auto& hookList = GetHookList(type);
return !hookList.Hooks.empty();
}
void HookEngine::Call(HOOK_TYPE type, bool isGameStateMutable)
{
auto& hookList = GetHookList(type);
for (auto& hook : hookList.Hooks)
{
_scriptEngine.ExecutePluginCall(hook.Owner, hook.Function, {}, isGameStateMutable);
}
}
void HookEngine::Call(HOOK_TYPE type, const DukValue& arg, bool isGameStateMutable)
{
auto& hookList = GetHookList(type);
for (auto& hook : hookList.Hooks)
{
_scriptEngine.ExecutePluginCall(hook.Owner, hook.Function, { arg }, isGameStateMutable);
}
}
void HookEngine::Call(
HOOK_TYPE type, const std::initializer_list<std::pair<std::string_view, std::any>>& args, bool isGameStateMutable)
{
auto& hookList = GetHookList(type);
for (auto& hook : hookList.Hooks)
{
auto ctx = _scriptEngine.GetContext();
// Convert key/value pairs into an object
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);
}
}
HookList& HookEngine::GetHookList(HOOK_TYPE type)
{
auto index = static_cast<size_t>(type);
return _hookMap[index];
}
const HookList& HookEngine::GetHookList(HOOK_TYPE type) const
{
auto index = static_cast<size_t>(type);
return _hookMap[index];
}
#endif