-
Notifications
You must be signed in to change notification settings - Fork 366
/
ElunaUtility.cpp
212 lines (185 loc) · 7.01 KB
/
ElunaUtility.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/
#include "ElunaUtility.h"
#if !defined ELUNA_CMANGOS
#include "World.h"
#include "Object.h"
#include "Unit.h"
#include "GameObject.h"
#include "DBCStores.h"
#else
#include "World/World.h"
#include "Entities/Object.h"
#include "Entities/Unit.h"
#include "Entities/GameObject.h"
#include "Server/DBCStores.h"
#include "Util/Timer.h"
#endif
uint32 ElunaUtil::GetCurrTime()
{
#if defined ELUNA_TRINITY || ELUNA_MANGOS
return getMSTime();
#else
return WorldTimer::getMSTime();
#endif
}
uint32 ElunaUtil::GetTimeDiff(uint32 oldMSTime)
{
#if defined ELUNA_TRINITY || ELUNA_MANGOS
return GetMSTimeDiffToNow(oldMSTime);
#else
return WorldTimer::getMSTimeDiff(oldMSTime, WorldTimer::getMSTime());
#endif
}
ElunaUtil::ObjectGUIDCheck::ObjectGUIDCheck(ObjectGuid guid) : _guid(guid)
{
}
bool ElunaUtil::ObjectGUIDCheck::operator()(WorldObject* object)
{
return object->GET_GUID() == _guid;
}
ElunaUtil::ObjectDistanceOrderPred::ObjectDistanceOrderPred(WorldObject const* pRefObj, bool ascending) : m_refObj(pRefObj), m_ascending(ascending)
{
}
bool ElunaUtil::ObjectDistanceOrderPred::operator()(WorldObject const* pLeft, WorldObject const* pRight) const
{
return m_ascending ? m_refObj->GetDistanceOrder(pLeft, pRight) : !m_refObj->GetDistanceOrder(pLeft, pRight);
}
ElunaUtil::WorldObjectInRangeCheck::WorldObjectInRangeCheck(bool nearest, WorldObject const* obj, float range,
uint16 typeMask, uint32 entry, uint32 hostile, uint32 dead) :
i_obj(obj), i_obj_unit(nullptr), i_obj_fact(nullptr), i_hostile(hostile), i_entry(entry), i_range(range), i_typeMask(typeMask), i_dead(dead), i_nearest(nearest)
{
i_obj_unit = i_obj->ToUnit();
if (!i_obj_unit)
if (GameObject const* go = i_obj->ToGameObject())
i_obj_unit = go->GetOwner();
if (!i_obj_unit)
#if !defined ELUNA_VMANGOS
i_obj_fact = sFactionTemplateStore.LookupEntry(14);
#else
i_obj_fact = sObjectMgr.GetFactionTemplateEntry(14);
#endif
}
WorldObject const& ElunaUtil::WorldObjectInRangeCheck::GetFocusObject() const
{
return *i_obj;
}
bool ElunaUtil::WorldObjectInRangeCheck::operator()(WorldObject* u)
{
if (i_typeMask && !u->isType(TypeMask(i_typeMask)))
return false;
if (i_entry && u->GetEntry() != i_entry)
return false;
if (i_obj->GET_GUID() == u->GET_GUID())
return false;
if (!i_obj->IsWithinDistInMap(u, i_range))
return false;
Unit const* target = u->ToUnit();
if (!target)
if (GameObject const* go = u->ToGameObject())
target = go->GetOwner();
if (target)
{
if (i_dead && (i_dead == 1) != target->IsAlive())
return false;
if (i_hostile)
{
if (!i_obj_unit)
{
if (i_obj_fact)
{
#if !defined ELUNA_MANGOS
if ((i_obj_fact->IsHostileTo(*target->GetFactionTemplateEntry())) != (i_hostile == 1))
#else
if ((i_obj_fact->IsHostileTo(*target->getFactionTemplateEntry())) != (i_hostile == 1))
#endif
return false;
}
else if (i_hostile == 1)
return false;
}
else if ((i_hostile == 1) != i_obj_unit->IsHostileTo(target))
return false;
}
}
if (i_nearest)
i_range = i_obj->GetDistance(u);
return true;
}
static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};
static char decoding_table[256];
static int mod_table[] = {0, 2, 1};
static void build_decoding_table()
{
for (int i = 0; i < 64; i++)
decoding_table[(unsigned char)encoding_table[i]] = i;
}
void ElunaUtil::EncodeData(const unsigned char* data, size_t input_length, std::string& output)
{
size_t output_length = 4 * ((input_length + 2) / 3);
char* buffer = new char[output_length];
for (size_t i = 0, j = 0; i < input_length;)
{
uint32 octet_a = i < input_length ? (unsigned char)data[i++] : 0;
uint32 octet_b = i < input_length ? (unsigned char)data[i++] : 0;
uint32 octet_c = i < input_length ? (unsigned char)data[i++] : 0;
uint32 triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
buffer[j++] = encoding_table[(triple >> (3 * 6)) & 0x3F];
buffer[j++] = encoding_table[(triple >> (2 * 6)) & 0x3F];
buffer[j++] = encoding_table[(triple >> (1 * 6)) & 0x3F];
buffer[j++] = encoding_table[(triple >> (0 * 6)) & 0x3F];
}
for (int i = 0; i < mod_table[input_length % 3]; i++)
buffer[output_length - 1 - i] = '=';
output.assign(buffer, output_length); // Need length because `buffer` is not terminated!
delete[] buffer;
}
unsigned char* ElunaUtil::DecodeData(const char *data, size_t *output_length)
{
if (decoding_table[(unsigned char)'B'] == 0)
build_decoding_table();
size_t input_length = strlen(data);
if (input_length % 4 != 0)
return NULL;
// Make sure there's no invalid characters in the data.
for (size_t i = 0; i < input_length; ++i)
{
unsigned char byte = data[i];
if (byte == '=')
continue;
// Every invalid character (and 'A') will map to 0 (due to `calloc`).
if (decoding_table[byte] == 0 && byte != 'A')
return NULL;
}
*output_length = input_length / 4 * 3;
if (data[input_length - 1] == '=') (*output_length)--;
if (data[input_length - 2] == '=') (*output_length)--;
unsigned char *decoded_data = new unsigned char[*output_length];
if (!decoded_data)
return NULL;
for (size_t i = 0, j = 0; i < input_length;)
{
uint32 sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char)data[i++]];
uint32 sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char)data[i++]];
uint32 sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char)data[i++]];
uint32 sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char)data[i++]];
uint32 triple = (sextet_a << (3 * 6))
+ (sextet_b << (2 * 6))
+ (sextet_c << (1 * 6))
+ (sextet_d << (0 * 6));
if (j < *output_length) decoded_data[j++] = (triple >> (2 * 8)) & 0xFF;
if (j < *output_length) decoded_data[j++] = (triple >> (1 * 8)) & 0xFF;
if (j < *output_length) decoded_data[j++] = (triple >> (0 * 8)) & 0xFF;
}
return decoded_data;
}