forked from ElunaLuaEngine/Eluna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PacketHooks.cpp
139 lines (112 loc) · 3.49 KB
/
PacketHooks.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
/*
* Copyright (C) 2010 - 2016 Eluna Lua Engine <http://emudevs.com/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/
#include "Hooks.h"
#include "HookHelpers.h"
#include "LuaEngine.h"
#include "BindingMap.h"
#include "ElunaIncludes.h"
#include "ElunaTemplate.h"
using namespace Hooks;
#define START_HOOK_SERVER(EVENT) \
if (!IsEnabled())\
return;\
auto key = EventKey<ServerEvents>(EVENT);\
if (!ServerEventBindings->HasBindingsFor(key))\
return;\
LOCK_ELUNA
#define START_HOOK_PACKET(EVENT, OPCODE) \
if (!IsEnabled())\
return;\
auto key = EntryKey<PacketEvents>(EVENT, OPCODE);\
if (!PacketEventBindings->HasBindingsFor(key))\
return;\
LOCK_ELUNA
bool Eluna::OnPacketSend(WorldSession* session, const WorldPacket& packet)
{
bool result = true;
Player* player = NULL;
if (session)
player = session->GetPlayer();
OnPacketSendAny(player, packet, result);
OnPacketSendOne(player, packet, result);
return result;
}
void Eluna::OnPacketSendAny(Player* player, const WorldPacket& packet, bool& result)
{
START_HOOK_SERVER(SERVER_EVENT_ON_PACKET_SEND);
Push(new WorldPacket(packet));
Push(player);
int n = SetupStack(ServerEventBindings, key, 2);
while (n > 0)
{
int r = CallOneFunction(n--, 2, 1);
if (lua_isboolean(L, r + 0) && !lua_toboolean(L, r + 0))
result = false;
lua_pop(L, 1);
}
CleanUpStack(2);
}
void Eluna::OnPacketSendOne(Player* player, const WorldPacket& packet, bool& result)
{
START_HOOK_PACKET(PACKET_EVENT_ON_PACKET_SEND, packet.GetOpcode());
Push(new WorldPacket(packet));
Push(player);
int n = SetupStack(PacketEventBindings, key, 2);
while (n > 0)
{
int r = CallOneFunction(n--, 2, 1);
if (lua_isboolean(L, r + 0) && !lua_toboolean(L, r + 0))
result = false;
lua_pop(L, 1);
}
CleanUpStack(2);
}
bool Eluna::OnPacketReceive(WorldSession* session, WorldPacket& packet)
{
bool result = true;
Player* player = NULL;
if (session)
player = session->GetPlayer();
OnPacketReceiveAny(player, packet, result);
OnPacketReceiveOne(player, packet, result);
return result;
}
void Eluna::OnPacketReceiveAny(Player* player, WorldPacket& packet, bool& result)
{
START_HOOK_SERVER(SERVER_EVENT_ON_PACKET_RECEIVE);
Push(new WorldPacket(packet));
Push(player);
int n = SetupStack(ServerEventBindings, key, 2);
while (n > 0)
{
int r = CallOneFunction(n--, 2, 2);
if (lua_isboolean(L, r + 0) && !lua_toboolean(L, r + 0))
result = false;
if (lua_isuserdata(L, r + 1))
if (WorldPacket* data = CHECKOBJ<WorldPacket>(L, r + 1, false))
packet = *data;
lua_pop(L, 2);
}
CleanUpStack(2);
}
void Eluna::OnPacketReceiveOne(Player* player, WorldPacket& packet, bool& result)
{
START_HOOK_PACKET(PACKET_EVENT_ON_PACKET_SEND, packet.GetOpcode());
Push(new WorldPacket(packet));
Push(player);
int n = SetupStack(PacketEventBindings, key, 2);
while (n > 0)
{
int r = CallOneFunction(n--, 2, 2);
if (lua_isboolean(L, r + 0) && !lua_toboolean(L, r + 0))
result = false;
if (lua_isuserdata(L, r + 1))
if (WorldPacket* data = CHECKOBJ<WorldPacket>(L, r + 1, false))
packet = *data;
lua_pop(L, 2);
}
CleanUpStack(2);
}