Skip to content

Commit c8ddffb

Browse files
committed
Binding store refactor
1 parent 5c644c2 commit c8ddffb

17 files changed

+247
-216
lines changed

BindingMap.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ extern "C"
1818
#include "lauxlib.h"
1919
};
2020

21+
class BaseBindingMap {
22+
public:
23+
virtual ~BaseBindingMap() = default;
24+
};
2125

2226
/*
2327
* A set of bindings from keys of type `K` to Lua references.
2428
*/
2529
template<typename K>
26-
class BindingMap
30+
class BindingMap : public BaseBindingMap
2731
{
2832
private:
2933
lua_State* L;

LuaEngine.cpp

Lines changed: 130 additions & 141 deletions
Large diffs are not rendered by default.

LuaEngine.h

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ typedef VehicleInfo Vehicle;
103103
struct lua_State;
104104
class EventMgr;
105105
class ElunaObject;
106+
class BaseBindingMap;
106107
template<typename T> class ElunaTemplate;
107108

108109
template<typename K> class BindingMap;
@@ -246,27 +247,21 @@ class ELUNA_GAME_API Eluna
246247
QueryCallbackProcessor queryProcessor;
247248
QueryCallbackProcessor& GetQueryProcessor() { return queryProcessor; }
248249
#endif
250+
std::unordered_map<std::string, std::unique_ptr<BaseBindingMap>> bindingMaps;
249251

250-
BindingMap< EventKey<Hooks::ServerEvents> >* ServerEventBindings;
251-
BindingMap< EventKey<Hooks::PlayerEvents> >* PlayerEventBindings;
252-
BindingMap< EventKey<Hooks::GuildEvents> >* GuildEventBindings;
253-
BindingMap< EventKey<Hooks::GroupEvents> >* GroupEventBindings;
254-
BindingMap< EventKey<Hooks::VehicleEvents> >* VehicleEventBindings;
255-
BindingMap< EventKey<Hooks::BGEvents> >* BGEventBindings;
256-
257-
BindingMap< EntryKey<Hooks::PacketEvents> >* PacketEventBindings;
258-
BindingMap< EntryKey<Hooks::CreatureEvents> >* CreatureEventBindings;
259-
BindingMap< EntryKey<Hooks::GossipEvents> >* CreatureGossipBindings;
260-
BindingMap< EntryKey<Hooks::GameObjectEvents> >* GameObjectEventBindings;
261-
BindingMap< EntryKey<Hooks::GossipEvents> >* GameObjectGossipBindings;
262-
BindingMap< EntryKey<Hooks::SpellEvents> >* SpellEventBindings;
263-
BindingMap< EntryKey<Hooks::ItemEvents> >* ItemEventBindings;
264-
BindingMap< EntryKey<Hooks::GossipEvents> >* ItemGossipBindings;
265-
BindingMap< EntryKey<Hooks::GossipEvents> >* PlayerGossipBindings;
266-
BindingMap< EntryKey<Hooks::InstanceEvents> >* MapEventBindings;
267-
BindingMap< EntryKey<Hooks::InstanceEvents> >* InstanceEventBindings;
268-
269-
BindingMap< UniqueObjectKey<Hooks::CreatureEvents> >* CreatureUniqueBindings;
252+
template<typename T>
253+
void CreateBinding(const std::string& name)
254+
{
255+
bindingMaps[name] = std::make_unique<BindingMap<T>>(L);
256+
}
257+
258+
template<typename T>
259+
BindingMap<T>* GetBinding(const std::string& name)
260+
{
261+
auto it = bindingMaps.find(name);
262+
if (it == bindingMaps.end()) return nullptr;
263+
return dynamic_cast<BindingMap<T>*>(it->second.get());
264+
}
270265

271266
static int StackTrace(lua_State* _L);
272267
static void Report(lua_State* _L);

hooks/BattleGroundHooks.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using namespace Hooks;
1414

1515
#define START_HOOK(EVENT) \
16+
auto BGEventBindings = GetBinding<EventKey<BGEvents>>("BGEvents");\
1617
auto key = EventKey<BGEvents>(EVENT);\
1718
if (!BGEventBindings->HasBindingsFor(key))\
1819
return;

hooks/CreatureHooks.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
using namespace Hooks;
1515

1616
#define START_HOOK(EVENT, CREATURE) \
17+
auto CreatureEventBindings = GetBinding<EntryKey<CreatureEvents>>("CreatureEvents");\
18+
auto CreatureUniqueBindings = GetBinding<UniqueObjectKey<CreatureEvents>>("CreatureUnique");\
1719
auto entry_key = EntryKey<CreatureEvents>(EVENT, CREATURE->GetEntry());\
1820
auto unique_key = UniqueObjectKey<CreatureEvents>(EVENT, CREATURE->GET_GUID(), CREATURE->GetInstanceId());\
1921
if (!CreatureEventBindings->HasBindingsFor(entry_key))\
2022
if (!CreatureUniqueBindings->HasBindingsFor(unique_key))\
2123
return;
2224

2325
#define START_HOOK_WITH_RETVAL(EVENT, CREATURE, RETVAL) \
26+
auto CreatureEventBindings = GetBinding<EntryKey<CreatureEvents>>("CreatureEvents");\
27+
auto CreatureUniqueBindings = GetBinding<UniqueObjectKey<CreatureEvents>>("CreatureUnique");\
2428
auto entry_key = EntryKey<CreatureEvents>(EVENT, CREATURE->GetEntry());\
2529
auto unique_key = UniqueObjectKey<CreatureEvents>(EVENT, CREATURE->GET_GUID(), CREATURE->GetInstanceId());\
2630
if (!CreatureEventBindings->HasBindingsFor(entry_key))\

hooks/GameObjectHooks.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
using namespace Hooks;
1616

1717
#define START_HOOK(EVENT, ENTRY) \
18+
auto GameObjectEventBindings = GetBinding<EntryKey<GameObjectEvents>>("GameObjectEvents");\
1819
auto key = EntryKey<GameObjectEvents>(EVENT, ENTRY);\
1920
if (!GameObjectEventBindings->HasBindingsFor(key))\
2021
return;
2122

2223
#define START_HOOK_WITH_RETVAL(EVENT, ENTRY, RETVAL) \
24+
auto GameObjectEventBindings = GetBinding<EntryKey<GameObjectEvents>>("GameObjectEvents");\
2325
auto key = EntryKey<GameObjectEvents>(EVENT, ENTRY);\
2426
if (!GameObjectEventBindings->HasBindingsFor(key))\
2527
return RETVAL;

hooks/GossipHooks.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,34 @@
1313

1414
using namespace Hooks;
1515

16-
#define START_HOOK(BINDINGS, EVENT, ENTRY) \
16+
#define START_HOOK(BINDINGSTORE, EVENT, ENTRY) \
17+
auto binding = GetBinding<EntryKey<GossipEvents>>(BINDINGSTORE);\
1718
auto key = EntryKey<GossipEvents>(EVENT, ENTRY);\
18-
if (!BINDINGS->HasBindingsFor(key))\
19+
if (!binding->HasBindingsFor(key))\
1920
return;
2021

21-
#define START_HOOK_WITH_RETVAL(BINDINGS, EVENT, ENTRY, RETVAL) \
22+
#define START_HOOK_WITH_RETVAL(BINDINGSTORE, EVENT, ENTRY, RETVAL) \
23+
auto binding = GetBinding<EntryKey<GossipEvents>>(BINDINGSTORE);\
2224
auto key = EntryKey<GossipEvents>(EVENT, ENTRY);\
23-
if (!BINDINGS->HasBindingsFor(key))\
25+
if (!binding->HasBindingsFor(key))\
2426
return RETVAL;
2527

2628
bool Eluna::OnGossipHello(Player* pPlayer, GameObject* pGameObject)
2729
{
28-
START_HOOK_WITH_RETVAL(GameObjectGossipBindings, GOSSIP_EVENT_ON_HELLO, pGameObject->GetEntry(), false);
30+
START_HOOK_WITH_RETVAL("GameObjectGossip", GOSSIP_EVENT_ON_HELLO, pGameObject->GetEntry(), false);
2931
#if defined ELUNA_CMANGOS && ELUNA_EXPANSION < EXP_CATA
3032
pPlayer->GetPlayerMenu()->ClearMenus();
3133
#else
3234
pPlayer->PlayerTalkClass->ClearMenus();
3335
#endif
3436
HookPush(pPlayer);
3537
HookPush(pGameObject);
36-
return CallAllFunctionsBool(GameObjectGossipBindings, key, true);
38+
return CallAllFunctionsBool(binding, key, true);
3739
}
3840

3941
bool Eluna::OnGossipSelect(Player* pPlayer, GameObject* pGameObject, uint32 sender, uint32 action)
4042
{
41-
START_HOOK_WITH_RETVAL(GameObjectGossipBindings, GOSSIP_EVENT_ON_SELECT, pGameObject->GetEntry(), false);
43+
START_HOOK_WITH_RETVAL("GameObjectGossip", GOSSIP_EVENT_ON_SELECT, pGameObject->GetEntry(), false);
4244
#if defined ELUNA_CMANGOS && ELUNA_EXPANSION < EXP_CATA
4345
pPlayer->GetPlayerMenu()->ClearMenus();
4446
#else
@@ -48,12 +50,12 @@ bool Eluna::OnGossipSelect(Player* pPlayer, GameObject* pGameObject, uint32 send
4850
HookPush(pGameObject);
4951
HookPush(sender);
5052
HookPush(action);
51-
return CallAllFunctionsBool(GameObjectGossipBindings, key, true);
53+
return CallAllFunctionsBool(binding, key, true);
5254
}
5355

5456
bool Eluna::OnGossipSelectCode(Player* pPlayer, GameObject* pGameObject, uint32 sender, uint32 action, const char* code)
5557
{
56-
START_HOOK_WITH_RETVAL(GameObjectGossipBindings, GOSSIP_EVENT_ON_SELECT, pGameObject->GetEntry(), false);
58+
START_HOOK_WITH_RETVAL("GameObjectGossip", GOSSIP_EVENT_ON_SELECT, pGameObject->GetEntry(), false);
5759
#if defined ELUNA_CMANGOS && ELUNA_EXPANSION < EXP_CATA
5860
pPlayer->GetPlayerMenu()->ClearMenus();
5961
#else
@@ -64,12 +66,12 @@ bool Eluna::OnGossipSelectCode(Player* pPlayer, GameObject* pGameObject, uint32
6466
HookPush(sender);
6567
HookPush(action);
6668
HookPush(code);
67-
return CallAllFunctionsBool(GameObjectGossipBindings, key, true);
69+
return CallAllFunctionsBool(binding, key, true);
6870
}
6971

7072
void Eluna::HandleGossipSelectOption(Player* pPlayer, uint32 menuId, uint32 sender, uint32 action, const std::string& code)
7173
{
72-
START_HOOK(PlayerGossipBindings, GOSSIP_EVENT_ON_SELECT, menuId);
74+
START_HOOK("PlayerGossip", GOSSIP_EVENT_ON_SELECT, menuId);
7375
#if defined ELUNA_CMANGOS && ELUNA_EXPANSION < EXP_CATA
7476
pPlayer->GetPlayerMenu()->ClearMenus();
7577
#else
@@ -85,25 +87,25 @@ void Eluna::HandleGossipSelectOption(Player* pPlayer, uint32 menuId, uint32 send
8587
else
8688
HookPush(code);
8789

88-
CallAllFunctions(PlayerGossipBindings, key);
90+
CallAllFunctions(binding, key);
8991
}
9092

9193
bool Eluna::OnItemGossip(Player* pPlayer, Item* pItem, SpellCastTargets const& /*targets*/)
9294
{
93-
START_HOOK_WITH_RETVAL(ItemGossipBindings, GOSSIP_EVENT_ON_HELLO, pItem->GetEntry(), true);
95+
START_HOOK_WITH_RETVAL("ItemGossip", GOSSIP_EVENT_ON_HELLO, pItem->GetEntry(), true);
9496
#if defined ELUNA_CMANGOS && ELUNA_EXPANSION < EXP_CATA
9597
pPlayer->GetPlayerMenu()->ClearMenus();
9698
#else
9799
pPlayer->PlayerTalkClass->ClearMenus();
98100
#endif
99101
HookPush(pPlayer);
100102
HookPush(pItem);
101-
return CallAllFunctionsBool(ItemGossipBindings, key, true);
103+
return CallAllFunctionsBool(binding, key, true);
102104
}
103105

104106
void Eluna::HandleGossipSelectOption(Player* pPlayer, Item* pItem, uint32 sender, uint32 action, const std::string& code)
105107
{
106-
START_HOOK(ItemGossipBindings, GOSSIP_EVENT_ON_SELECT, pItem->GetEntry());
108+
START_HOOK("ItemGossip", GOSSIP_EVENT_ON_SELECT, pItem->GetEntry());
107109
#if defined ELUNA_CMANGOS && ELUNA_EXPANSION < EXP_CATA
108110
pPlayer->GetPlayerMenu()->ClearMenus();
109111
#else
@@ -119,25 +121,25 @@ void Eluna::HandleGossipSelectOption(Player* pPlayer, Item* pItem, uint32 sender
119121
else
120122
HookPush(code);
121123

122-
CallAllFunctions(ItemGossipBindings, key);
124+
CallAllFunctions(binding, key);
123125
}
124126

125127
bool Eluna::OnGossipHello(Player* pPlayer, Creature* pCreature)
126128
{
127-
START_HOOK_WITH_RETVAL(CreatureGossipBindings, GOSSIP_EVENT_ON_HELLO, pCreature->GetEntry(), false);
129+
START_HOOK_WITH_RETVAL("CreatureGossip", GOSSIP_EVENT_ON_HELLO, pCreature->GetEntry(), false);
128130
#if defined ELUNA_CMANGOS && ELUNA_EXPANSION < EXP_CATA
129131
pPlayer->GetPlayerMenu()->ClearMenus();
130132
#else
131133
pPlayer->PlayerTalkClass->ClearMenus();
132134
#endif
133135
HookPush(pPlayer);
134136
HookPush(pCreature);
135-
return CallAllFunctionsBool(CreatureGossipBindings, key, true);
137+
return CallAllFunctionsBool(binding, key, true);
136138
}
137139

138140
bool Eluna::OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 sender, uint32 action)
139141
{
140-
START_HOOK_WITH_RETVAL(CreatureGossipBindings, GOSSIP_EVENT_ON_SELECT, pCreature->GetEntry(), false);
142+
START_HOOK_WITH_RETVAL("CreatureGossip", GOSSIP_EVENT_ON_SELECT, pCreature->GetEntry(), false);
141143
#if defined ELUNA_CMANGOS && ELUNA_EXPANSION < EXP_CATA
142144
auto original_menu = *pPlayer->GetPlayerMenu();
143145
pPlayer->GetPlayerMenu()->ClearMenus();
@@ -149,7 +151,7 @@ bool Eluna::OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 sender,
149151
HookPush(pCreature);
150152
HookPush(sender);
151153
HookPush(action);
152-
auto preventDefault = CallAllFunctionsBool(CreatureGossipBindings, key, true);
154+
auto preventDefault = CallAllFunctionsBool(binding, key, true);
153155
if (!preventDefault) {
154156
#if defined ELUNA_CMANGOS && ELUNA_EXPANSION < EXP_CATA
155157
*pPlayer->GetPlayerMenu() = original_menu;
@@ -162,7 +164,7 @@ bool Eluna::OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 sender,
162164

163165
bool Eluna::OnGossipSelectCode(Player* pPlayer, Creature* pCreature, uint32 sender, uint32 action, const char* code)
164166
{
165-
START_HOOK_WITH_RETVAL(CreatureGossipBindings, GOSSIP_EVENT_ON_SELECT, pCreature->GetEntry(), false);
167+
START_HOOK_WITH_RETVAL("CreatureGossip", GOSSIP_EVENT_ON_SELECT, pCreature->GetEntry(), false);
166168
#if defined ELUNA_CMANGOS && ELUNA_EXPANSION < EXP_CATA
167169
auto original_menu = *pPlayer->GetPlayerMenu();
168170
pPlayer->GetPlayerMenu()->ClearMenus();
@@ -175,7 +177,7 @@ bool Eluna::OnGossipSelectCode(Player* pPlayer, Creature* pCreature, uint32 send
175177
HookPush(sender);
176178
HookPush(action);
177179
HookPush(code);
178-
auto preventDefault = CallAllFunctionsBool(CreatureGossipBindings, key, true);
180+
auto preventDefault = CallAllFunctionsBool(binding, key, true);
179181
if (!preventDefault) {
180182
#if defined ELUNA_CMANGOS && ELUNA_EXPANSION < EXP_CATA
181183
*pPlayer->GetPlayerMenu() = original_menu;

hooks/GroupHooks.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
using namespace Hooks;
1414

1515
#define START_HOOK(EVENT) \
16+
auto GroupEventBindings = GetBinding<EventKey<GroupEvents>>("GroupEvents");\
1617
auto key = EventKey<GroupEvents>(EVENT);\
1718
if (!GroupEventBindings->HasBindingsFor(key))\
1819
return;
1920

2021
#define START_HOOK_WITH_RETVAL(EVENT, RETVAL) \
22+
auto GroupEventBindings = GetBinding<EventKey<GroupEvents>>("GroupEvents");\
2123
auto key = EventKey<GroupEvents>(EVENT);\
2224
if (!GroupEventBindings->HasBindingsFor(key))\
2325
return RETVAL;

hooks/GuildHooks.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using namespace Hooks;
1414

1515
#define START_HOOK(EVENT) \
16+
auto GuildEventBindings = GetBinding<EventKey<GuildEvents>>("GuildEvents");\
1617
auto key = EventKey<GuildEvents>(EVENT);\
1718
if (!GuildEventBindings->HasBindingsFor(key))\
1819
return;

hooks/InstanceHooks.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
using namespace Hooks;
1616

1717
#define START_HOOK(EVENT, AI) \
18+
auto MapEventBindings = GetBinding<EntryKey<InstanceEvents>>("MapEvents");\
19+
auto InstanceEventBindings = GetBinding<EntryKey<InstanceEvents>>("InstanceEvents");\
1820
auto mapKey = EntryKey<InstanceEvents>(EVENT, AI->instance->GetId());\
1921
auto instanceKey = EntryKey<InstanceEvents>(EVENT, AI->instance->GetInstanceId());\
2022
if (!MapEventBindings->HasBindingsFor(mapKey) && !InstanceEventBindings->HasBindingsFor(instanceKey))\
@@ -23,6 +25,8 @@ using namespace Hooks;
2325
HookPush<Map>(AI->instance)
2426

2527
#define START_HOOK_WITH_RETVAL(EVENT, AI, RETVAL) \
28+
auto MapEventBindings = GetBinding<EntryKey<InstanceEvents>>("MapEvents");\
29+
auto InstanceEventBindings = GetBinding<EntryKey<InstanceEvents>>("InstanceEvents");\
2630
auto mapKey = EntryKey<InstanceEvents>(EVENT, AI->instance->GetId());\
2731
auto instanceKey = EntryKey<InstanceEvents>(EVENT, AI->instance->GetInstanceId());\
2832
if (!MapEventBindings->HasBindingsFor(mapKey) && !InstanceEventBindings->HasBindingsFor(instanceKey))\

0 commit comments

Comments
 (0)