forked from Source-Python-Dev-Team/Source.Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentities_props.cpp
More file actions
282 lines (234 loc) · 8.69 KB
/
entities_props.cpp
File metadata and controls
282 lines (234 loc) · 8.69 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2016 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/
// ============================================================================
// >> INCLUDES
// ============================================================================
// Boost
#include "boost/unordered_map.hpp"
// Source.Python
#include "modules/memory/memory_pointer.h"
#include "entities_props.h"
#include ENGINE_INCLUDE_PATH(entities_props.h)
// SDK
#include "eiface.h"
#include "public/server_class.h"
//-----------------------------------------------------------------------------
// External variables.
//-----------------------------------------------------------------------------
extern IServerGameDLL* servergamedll;
// ============================================================================
// >> TYPEDEFS
// ============================================================================
typedef boost::unordered_map<std::string, int> OffsetsMap;
typedef boost::unordered_map<std::string, OffsetsMap > SendTableMap;
// ============================================================================
// >> GLOBAL VARIABLES
// ============================================================================
SendTableMap g_SendTableCache;
// ============================================================================
// >> HELPER FUNCTIONS
// ============================================================================
SendTable* GetNextSendTable(SendTable* pTable)
{
for (int i=0; i < pTable->GetNumProps(); ++i)
{
SendProp* pProp = pTable->GetProp(i);
if (strcmp(pProp->GetName(), "baseclass") != 0)
continue;
return pProp->GetDataTable();
}
return NULL;
}
void AddSendTable(SendTable* pTable, OffsetsMap& offsets, int offset=0, const char* baseName=NULL)
{
for (int i=0; i < pTable->GetNumProps(); ++i)
{
SendProp* pProp = pTable->GetProp(i);
if (strcmp(pProp->GetName(), "baseclass") == 0 ||
pProp->IsExcludeProp() ||
pProp->GetFlags() & SPROP_COLLAPSIBLE
)
continue;
int currentOffset = offset + pProp->GetOffset();
char* currentName = NULL;
if (baseName == NULL) {
currentName = (char*) pProp->GetName();
}
else {
char tempName[256];
sprintf(tempName, "%s.%s", baseName, pProp->GetName());
currentName = strdup(tempName);
}
if (pProp->GetType() == DPT_DataTable)
{
AddSendTable(pProp->GetDataTable(), offsets, currentOffset, currentName);
}
else
{
offsets.insert(std::make_pair(currentName, currentOffset));
}
}
}
// ============================================================================
// >> ServerClassExt
// ============================================================================
ServerClass* ServerClassExt::find_server_class(const char* name)
{
ServerClass* current = servergamedll->GetAllServerClasses();
while (current)
{
if (V_strcmp(current->GetName(), name) == 0)
{
return current;
}
current = current->m_pNext;
}
BOOST_RAISE_EXCEPTION(PyExc_NameError, "Unable to find server class %s.", name);
return NULL;
}
// ============================================================================
// >> SendTableSharedExt
// ============================================================================
SendProp* SendTableSharedExt::__getitem__(SendTable *pSendTable, int iIndex)
{
if (iIndex < 0 || iIndex > (pSendTable->m_nProps - 1))
{
BOOST_RAISE_EXCEPTION(PyExc_IndexError, "Invalid index.");
}
return pSendTable->GetProp(iIndex);
}
int SendTableSharedExt::find_offset(SendTable* pTable, const char* name)
{
while(pTable)
{
SendTableMap::iterator offsets = g_SendTableCache.find(pTable->GetName());
if (offsets == g_SendTableCache.end())
{
offsets = g_SendTableCache.insert(std::make_pair(pTable->GetName(), OffsetsMap())).first;
AddSendTable(pTable, offsets->second);
}
OffsetsMap::iterator result = offsets->second.find(name);
if (result == offsets->second.end())
{
pTable = GetNextSendTable(pTable);
}
else {
return result->second;
}
}
return -1;
}
// ============================================================================
// >> SendPropSharedExt
// ============================================================================
BoostSendVarProxyFn SendPropSharedExt::get_proxy_function(SendProp *pSendProp)
{
if (pSendProp->IsExcludeProp())
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "%s is excluded.", pSendProp->GetName());
if (pSendProp->GetType() == DPT_DataTable)
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "%s is a DataTable.", pSendProp->GetName());
if (!pSendProp->GetProxyFn()) {
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "%s has no proxy function.", pSendProp->GetName());
}
return BoostSendVarProxyFn(pSendProp->GetProxyFn());
}
BoostDataTableProxyFn SendPropSharedExt::get_data_table_proxy_function(SendProp *pSendProp)
{
if (pSendProp->IsExcludeProp())
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "%s is excluded.", pSendProp->GetName());
if (pSendProp->GetType() != DPT_DataTable)
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "%s is not a DataTable.", pSendProp->GetName());
if (!pSendProp->GetDataTableProxyFn()) {
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "%s has no data table proxy function.", pSendProp->GetName());
}
return BoostDataTableProxyFn(pSendProp->GetDataTableProxyFn());
}
BoostArrayLengthProxyFn SendPropSharedExt::get_array_length_proxy_function(SendProp *pSendProp)
{
if (pSendProp->IsExcludeProp())
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "%s is excluded.", pSendProp->GetName());
if (pSendProp->GetType() != DPT_Array)
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "%s is not an array.", pSendProp->GetName());
if (!pSendProp->GetArrayLengthProxy()) {
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "%s has no array length proxy function.", pSendProp->GetName());
}
return BoostArrayLengthProxyFn(pSendProp->GetArrayLengthProxy());
}
// ============================================================================
// >> SendPropVariantExt
// ============================================================================
const char* SendPropVariantExt::get_string(DVariant *pVariant)
{
if (pVariant->m_Type != DPT_String)
{
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Unable to cast to the specified type.");
}
return pVariant->m_pString;
}
void SendPropVariantExt::set_string(DVariant *pVariant, const char *pString)
{
pVariant->m_Type = DPT_String;
*(char**)&pVariant->m_pString = (char*)pString;
}
int SendPropVariantExt::get_int(DVariant* pVariant)
{
if (pVariant->m_Type != DPT_Int)
{
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Unable to cast to the specified type.");
}
return pVariant->m_Int;
}
void SendPropVariantExt::set_int(DVariant* pVariant, int iValue)
{
if (pVariant->m_Type != DPT_Int)
{
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Unable to cast to the specified type.");
}
pVariant->m_Int = iValue;
}
CPointer* SendPropVariantExt::get_data(DVariant *pVariant)
{
return new CPointer((unsigned long)get_typed_value<DPT_DataTable, void *, &DVariant::m_pData>(pVariant));
}
void SendPropVariantExt::set_data(DVariant *pVariant, CPointer *pData)
{
set_typed_value<DPT_DataTable, void *, &DVariant::m_pData>(pVariant, (void *)pData->m_ulAddr);
}
Vector* SendPropVariantExt::get_vector(DVariant *pVariant)
{
if (pVariant->m_Type != DPT_Vector)
{
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Unable to cast to the specified type.");
}
return new Vector(pVariant->m_Vector[0], pVariant->m_Vector[1], pVariant->m_Vector[2]);
}
void SendPropVariantExt::set_vector(DVariant *pVariant, Vector *pVector)
{
pVariant->m_Type = DPT_Vector;
pVariant->m_Vector[0] = pVector->x;
pVariant->m_Vector[1] = pVector->y;
pVariant->m_Vector[2] = pVector->z;
}