Skip to content

Commit 0552d62

Browse files
authored
Event binding refactor (#514)
1 parent 52d3c75 commit 0552d62

21 files changed

+548
-542
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: 112 additions & 213 deletions
Large diffs are not rendered by default.

LuaEngine.h

Lines changed: 25 additions & 25 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;
@@ -150,8 +151,6 @@ enum MethodFlags : uint32
150151
class ELUNA_GAME_API Eluna
151152
{
152153
public:
153-
typedef std::list<LuaScript> ScriptList;
154-
typedef std::recursive_mutex LockType;
155154

156155
void ReloadEluna() { reload = true; }
157156
bool ExecuteCall(int params, int res);
@@ -183,6 +182,14 @@ class ELUNA_GAME_API Eluna
183182
// Map from map ID -> Lua table ref
184183
std::unordered_map<uint32, int> continentDataRefs;
185184

185+
std::unordered_map<std::underlying_type_t<Hooks::RegisterTypes>, std::unique_ptr<BaseBindingMap>> bindingMaps;
186+
187+
template<typename T>
188+
void CreateBinding(Hooks::RegisterTypes type)
189+
{
190+
bindingMaps[std::underlying_type_t<Hooks::RegisterTypes>(type)] = std::make_unique<BindingMap<T>>(L);
191+
}
192+
186193
void OpenLua();
187194
void CloseLua();
188195
void DestroyBindStores();
@@ -240,34 +247,13 @@ class ELUNA_GAME_API Eluna
240247
public:
241248

242249
lua_State* L;
243-
EventMgr* eventMgr;
250+
std::unique_ptr<EventMgr> eventMgr;
244251

245252
#if defined ELUNA_TRINITY
246253
QueryCallbackProcessor queryProcessor;
247254
QueryCallbackProcessor& GetQueryProcessor() { return queryProcessor; }
248255
#endif
249256

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;
270-
271257
static int StackTrace(lua_State* _L);
272258
static void Report(lua_State* _L);
273259

@@ -337,7 +323,7 @@ class ELUNA_GAME_API Eluna
337323
#if !defined TRACKABLE_PTR_NAMESPACE
338324
uint64 GetCallstackId() const { return callstackid; }
339325
#endif
340-
int Register(uint8 reg, uint32 entry, ObjectGuid guid, uint32 instanceId, uint32 event_id, int functionRef, uint32 shots);
326+
int Register(std::underlying_type_t<Hooks::RegisterTypes> regtype, uint32 entry, ObjectGuid guid, uint32 instanceId, uint32 event_id, int functionRef, uint32 shots);
341327
void UpdateEluna(uint32 diff);
342328

343329
// Checks
@@ -374,6 +360,20 @@ class ELUNA_GAME_API Eluna
374360
return 0;
375361
}
376362

363+
template<typename T>
364+
BindingMap<T>* GetBinding(std::underlying_type_t<Hooks::RegisterTypes> type)
365+
{
366+
auto it = bindingMaps.find(type);
367+
if (it == bindingMaps.end()) return nullptr;
368+
return dynamic_cast<BindingMap<T>*>(it->second.get());
369+
}
370+
371+
template<typename T>
372+
BindingMap<T>* GetBinding(Hooks::RegisterTypes type)
373+
{
374+
return GetBinding<T>(static_cast<std::underlying_type_t<Hooks::RegisterTypes>>(type));
375+
}
376+
377377
Eluna(Map * map);
378378
~Eluna();
379379

hooks/BattleGroundHooks.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
using namespace Hooks;
1414

1515
#define START_HOOK(EVENT) \
16+
auto binding = GetBinding<EventKey<BGEvents>>(REGTYPE_BG);\
1617
auto key = EventKey<BGEvents>(EVENT);\
17-
if (!BGEventBindings->HasBindingsFor(key))\
18+
if (!binding->HasBindingsFor(key))\
1819
return;
1920

2021
void Eluna::OnBGStart(BattleGround* bg, BattleGroundTypeId bgId, uint32 instanceId)
@@ -23,7 +24,7 @@ void Eluna::OnBGStart(BattleGround* bg, BattleGroundTypeId bgId, uint32 instance
2324
HookPush(bg);
2425
HookPush(bgId);
2526
HookPush(instanceId);
26-
CallAllFunctions(BGEventBindings, key);
27+
CallAllFunctions(binding, key);
2728
}
2829

2930
void Eluna::OnBGEnd(BattleGround* bg, BattleGroundTypeId bgId, uint32 instanceId, Team winner)
@@ -33,7 +34,7 @@ void Eluna::OnBGEnd(BattleGround* bg, BattleGroundTypeId bgId, uint32 instanceId
3334
HookPush(bgId);
3435
HookPush(instanceId);
3536
HookPush(winner);
36-
CallAllFunctions(BGEventBindings, key);
37+
CallAllFunctions(binding, key);
3738
}
3839

3940
void Eluna::OnBGCreate(BattleGround* bg, BattleGroundTypeId bgId, uint32 instanceId)
@@ -42,7 +43,7 @@ void Eluna::OnBGCreate(BattleGround* bg, BattleGroundTypeId bgId, uint32 instanc
4243
HookPush(bg);
4344
HookPush(bgId);
4445
HookPush(instanceId);
45-
CallAllFunctions(BGEventBindings, key);
46+
CallAllFunctions(binding, key);
4647
}
4748

4849
void Eluna::OnBGDestroy(BattleGround* bg, BattleGroundTypeId bgId, uint32 instanceId)
@@ -51,5 +52,5 @@ void Eluna::OnBGDestroy(BattleGround* bg, BattleGroundTypeId bgId, uint32 instan
5152
HookPush(bg);
5253
HookPush(bgId);
5354
HookPush(instanceId);
54-
CallAllFunctions(BGEventBindings, key);
55+
CallAllFunctions(binding, key);
5556
}

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>>(REGTYPE_CREATURE);\
18+
auto CreatureUniqueBindings = GetBinding<UniqueObjectKey<CreatureEvents>>(REGTYPE_CREATURE_UNIQUE);\
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>>(REGTYPE_CREATURE);\
27+
auto CreatureUniqueBindings = GetBinding<UniqueObjectKey<CreatureEvents>>(REGTYPE_CREATURE_UNIQUE);\
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: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
using namespace Hooks;
1616

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

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

2729
void Eluna::OnDummyEffect(WorldObject* pCaster, uint32 spellId, SpellEffIndex effIndex, GameObject* pTarget)
@@ -31,15 +33,15 @@ void Eluna::OnDummyEffect(WorldObject* pCaster, uint32 spellId, SpellEffIndex ef
3133
HookPush(spellId);
3234
HookPush(effIndex);
3335
HookPush(pTarget);
34-
CallAllFunctions(GameObjectEventBindings, key);
36+
CallAllFunctions(binding, key);
3537
}
3638

3739
void Eluna::UpdateAI(GameObject* pGameObject, uint32 diff)
3840
{
3941
START_HOOK(GAMEOBJECT_EVENT_ON_AIUPDATE, pGameObject->GetEntry());
4042
HookPush(pGameObject);
4143
HookPush(diff);
42-
CallAllFunctions(GameObjectEventBindings, key);
44+
CallAllFunctions(binding, key);
4345
}
4446

4547
bool Eluna::OnQuestAccept(Player* pPlayer, GameObject* pGameObject, Quest const* pQuest)
@@ -48,7 +50,7 @@ bool Eluna::OnQuestAccept(Player* pPlayer, GameObject* pGameObject, Quest const*
4850
HookPush(pPlayer);
4951
HookPush(pGameObject);
5052
HookPush(pQuest);
51-
return CallAllFunctionsBool(GameObjectEventBindings, key);
53+
return CallAllFunctionsBool(binding, key);
5254
}
5355

5456
bool Eluna::OnQuestReward(Player* pPlayer, GameObject* pGameObject, Quest const* pQuest, uint32 opt)
@@ -58,15 +60,15 @@ bool Eluna::OnQuestReward(Player* pPlayer, GameObject* pGameObject, Quest const*
5860
HookPush(pGameObject);
5961
HookPush(pQuest);
6062
HookPush(opt);
61-
return CallAllFunctionsBool(GameObjectEventBindings, key);
63+
return CallAllFunctionsBool(binding, key);
6264
}
6365

6466
void Eluna::GetDialogStatus(const Player* pPlayer, const GameObject* pGameObject)
6567
{
6668
START_HOOK(GAMEOBJECT_EVENT_ON_DIALOG_STATUS, pGameObject->GetEntry());
6769
HookPush(pPlayer);
6870
HookPush(pGameObject);
69-
CallAllFunctions(GameObjectEventBindings, key);
71+
CallAllFunctions(binding, key);
7072
}
7173

7274
#if ELUNA_EXPANSION >= EXP_WOTLK
@@ -75,15 +77,15 @@ void Eluna::OnDestroyed(GameObject* pGameObject, WorldObject* attacker)
7577
START_HOOK(GAMEOBJECT_EVENT_ON_DESTROYED, pGameObject->GetEntry());
7678
HookPush(pGameObject);
7779
HookPush(attacker);
78-
CallAllFunctions(GameObjectEventBindings, key);
80+
CallAllFunctions(binding, key);
7981
}
8082

8183
void Eluna::OnDamaged(GameObject* pGameObject, WorldObject* attacker)
8284
{
8385
START_HOOK(GAMEOBJECT_EVENT_ON_DAMAGED, pGameObject->GetEntry());
8486
HookPush(pGameObject);
8587
HookPush(attacker);
86-
CallAllFunctions(GameObjectEventBindings, key);
88+
CallAllFunctions(binding, key);
8789
}
8890
#endif
8991

@@ -92,42 +94,42 @@ void Eluna::OnLootStateChanged(GameObject* pGameObject, uint32 state)
9294
START_HOOK(GAMEOBJECT_EVENT_ON_LOOT_STATE_CHANGE, pGameObject->GetEntry());
9395
HookPush(pGameObject);
9496
HookPush(state);
95-
CallAllFunctions(GameObjectEventBindings, key);
97+
CallAllFunctions(binding, key);
9698
}
9799

98100
void Eluna::OnGameObjectStateChanged(GameObject* pGameObject, uint32 state)
99101
{
100102
START_HOOK(GAMEOBJECT_EVENT_ON_GO_STATE_CHANGED, pGameObject->GetEntry());
101103
HookPush(pGameObject);
102104
HookPush(state);
103-
CallAllFunctions(GameObjectEventBindings, key);
105+
CallAllFunctions(binding, key);
104106
}
105107

106108
void Eluna::OnSpawn(GameObject* pGameObject)
107109
{
108110
START_HOOK(GAMEOBJECT_EVENT_ON_SPAWN, pGameObject->GetEntry());
109111
HookPush(pGameObject);
110-
CallAllFunctions(GameObjectEventBindings, key);
112+
CallAllFunctions(binding, key);
111113
}
112114

113115
void Eluna::OnAddToWorld(GameObject* pGameObject)
114116
{
115117
START_HOOK(GAMEOBJECT_EVENT_ON_ADD, pGameObject->GetEntry());
116118
HookPush(pGameObject);
117-
CallAllFunctions(GameObjectEventBindings, key);
119+
CallAllFunctions(binding, key);
118120
}
119121

120122
void Eluna::OnRemoveFromWorld(GameObject* pGameObject)
121123
{
122124
START_HOOK(GAMEOBJECT_EVENT_ON_REMOVE, pGameObject->GetEntry());
123125
HookPush(pGameObject);
124-
CallAllFunctions(GameObjectEventBindings, key);
126+
CallAllFunctions(binding, key);
125127
}
126128

127129
bool Eluna::OnGameObjectUse(Player* pPlayer, GameObject* pGameObject)
128130
{
129131
START_HOOK_WITH_RETVAL(GAMEOBJECT_EVENT_ON_USE, pGameObject->GetEntry(), false);
130132
HookPush(pGameObject);
131133
HookPush(pPlayer);
132-
return CallAllFunctionsBool(GameObjectEventBindings, key);
134+
return CallAllFunctionsBool(binding, key);
133135
}

0 commit comments

Comments
 (0)