forked from ValveSoftware/source-sdk-2013
-
Notifications
You must be signed in to change notification settings - Fork 6
/
_utils.py
308 lines (265 loc) · 15.9 KB
/
_utils.py
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
from srcpy.module_generators import SemiSharedModuleGenerator
import pygccxml
from pyplusplus.module_builder import call_policies
from pyplusplus import function_transformers as FT
from pygccxml.declarations import reference_t, declarated_t, const_t
from srcpy.matchers import calldef_withtypes
class Utils(SemiSharedModuleGenerator):
module_name = '_utils'
required_files = [
'$SRCDIR\game\shared\python\srcpy_util.cpp',
'$SRCDIR\game\shared\python\srcpy_util.h',
]
files = [
'cbase.h',
'srcpy_util.h',
'util_shared.h',
'gametrace.h',
'engine/IEngineTrace.h',
'#mathlib/vmatrix.h',
'#utlvector.h',
'#shareddefs.h',
'#util.h',
'#explode.h',
'$cdll_util.h',
'$iclientshadowmgr.h',
'$viewrender.h',
'$view.h',
]
def ParseServer(self, mb):
# Entity index functions
mb.free_functions('INDEXENT').include()
mb.free_functions('ENTINDEX').include()
mb.free_functions('INDEXENT').call_policies = call_policies.return_internal_reference()
# Don't care about the following functions
mb.free_functions('UTIL_VarArgs').exclude()
mb.free_functions('UTIL_LogPrintf').exclude()
mb.free_functions('UTIL_FunctionToName').exclude()
if self.settings.branch == 'swarm':
mb.free_functions('UTIL_FunctionFromName').exclude()
mb.free_function('UTIL_GetModDir').exclude()
# Exclude for now
mb.free_functions('UTIL_GetDebugColorForRelationship').exclude()
mb.free_functions('UTIL_Beam').exclude()
mb.free_functions('UTIL_StringFieldToInt').exclude()
mb.free_functions('UTIL_EntitiesAlongRay').exclude()
# Replace the following
mb.free_functions('UTIL_SetSize').exclude()
mb.free_functions('UTIL_PySetSize').rename('UTIL_SetSize')
mb.free_functions('UTIL_SetModel').exclude()
mb.free_functions('UTIL_PySetModel').rename('UTIL_SetModel')
# Must use return_by_value. Then the converter will be used to wrap the vgui element in a safe handl
mb.free_functions('UTIL_GetLocalPlayer').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
mb.free_functions('UTIL_GetCommandClient').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
mb.free_functions('UTIL_EntityByIndex').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
mb.free_functions('UTIL_GetListenServerHost').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
mb.free_functions('UTIL_PlayerByName').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
mb.free_functions('UTIL_PlayerByUserId').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
mb.free_functions('UTIL_FindClientInVisibilityPVS').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
mb.free_functions('UTIL_EntitiesInPVS').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
mb.free_functions('UTIL_FindClientInPVS').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
if self.settings.branch == 'swarm':
mb.free_functions('UTIL_FindClientInPVSGuts').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
mb.free_functions('UTIL_GetLocalPlayerOrListenServerHost').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
# Helper for message stuff
cls = mb.class_('hudtextparms_s')
cls.include()
cls.rename('hudtextparms')
# Tracefilters
mb.class_('CTraceFilterMelee').include()
mb.class_('CTraceFilterMelee').calldefs('CTraceFilterMelee').exclude()
mb.class_('CTraceFilterMelee').add_wrapper_code(
"CTraceFilterMelee_wrapper(::CBaseEntity const * passentity, int collisionGroup, ::CTakeDamageInfo * dmgInfo, float flForceScale, bool bDamageAnyNPC )\r\n" + \
" : CTraceFilterMelee( boost::python::ptr(passentity), collisionGroup, boost::python::ptr(dmgInfo), flForceScale, bDamageAnyNPC )\r\n" + \
" , bp::wrapper< CTraceFilterMelee >(){\r\n" + \
" // constructor\r\n" + \
"}\r\n"
)
mb.class_('CTraceFilterMelee').add_registration_code(
'def( bp::init< CBaseEntity const *, int, CTakeDamageInfo *, float, bool >(( bp::arg("passentity"), bp::arg("collisionGroup"), bp::arg("dmgInfo"), bp::arg("flForceScale"), bp::arg("bDamageAnyNPC") )) )\r\n'
)
# Include explode functions
mb.free_functions('ExplosionCreate').include()
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_NODAMAGE\" ) = SF_ENVEXPLOSION_NODAMAGE;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_REPEATABLE\" ) = SF_ENVEXPLOSION_REPEATABLE;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_NOFIREBALL\" ) = SF_ENVEXPLOSION_NOFIREBALL;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_NOSMOKE\" ) = SF_ENVEXPLOSION_NOSMOKE;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_NODECAL\" ) = SF_ENVEXPLOSION_NODECAL;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_NOSPARKS\" ) = SF_ENVEXPLOSION_NOSPARKS;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_NOSOUND\" ) = SF_ENVEXPLOSION_NOSOUND;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_RND_ORIENT\" ) = SF_ENVEXPLOSION_RND_ORIENT;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_NOFIREBALLSMOKE\" ) = SF_ENVEXPLOSION_NOFIREBALLSMOKE;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_NOPARTICLES\" ) = SF_ENVEXPLOSION_NOPARTICLES;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_NODLIGHTS\" ) = SF_ENVEXPLOSION_NODLIGHTS;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_NOCLAMPMIN\" ) = SF_ENVEXPLOSION_NOCLAMPMIN;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_NOCLAMPMAX\" ) = SF_ENVEXPLOSION_NOCLAMPMAX;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_SURFACEONLY\" ) = SF_ENVEXPLOSION_SURFACEONLY;" )
mb.add_registration_code( "bp::scope().attr( \"SF_ENVEXPLOSION_GENERIC_DAMAGE\" ) = SF_ENVEXPLOSION_GENERIC_DAMAGE;" )
def ParseClient(self, mb):
# Free functions that don't start with 'UTIL_'
mb.free_functions('ScreenHeight').include()
mb.free_functions('ScreenWidth').include()
mb.free_functions('GetVectorInScreenSpace').include()
mb.free_functions('GetTargetInScreenSpace').include()
# Main view
mb.free_function('MainViewOrigin').include()
mb.free_function('MainViewAngles').include()
mb.free_function('MainViewForward').include()
mb.free_function('MainViewRight').include()
mb.free_function('MainViewUp').include()
mb.free_function('MainWorldToViewMatrix').include()
# Call policies and excludes
if self.settings.branch == 'swarm':
mb.free_function('UTIL_GetLocalizedKeyString').exclude()
mb.free_function('UTIL_MessageText').exclude()
mb.free_functions('UTIL_EntityFromUserMessageEHandle').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
mb.free_functions('UTIL_PlayerByUserId').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
# Transformations
mb.free_functions( 'GetVectorInScreenSpace' ).add_transformation( FT.output('iX'), FT.output('iY') )
mb.free_functions( 'GetTargetInScreenSpace' ).add_transformation( FT.output('iX'), FT.output('iY') )
def RecursiveFindDecl(self, cls, fnname):
try:
return cls.member_function('ShouldHitEntity')
except pygccxml.declarations.matcher.declaration_not_found_t:
for b in cls.bases:
ret = self.RecursiveFindDecl(b.related_class, fnname)
if ret:
return ret
return None
def SetupTraceFilter(self, mb, clsname):
cls = mb.class_(clsname)
cls.include()
cls.member_functions(allow_empty=True).virtuality = 'not virtual'
cls.member_functions('ShouldHitEntity', allow_empty=True).virtuality = 'virtual'
def Parse(self, mb):
# Exclude everything by default
mb.decls().exclude()
# By default include all functions starting with "UTIL_". Rest we will do manually
mb.free_functions( lambda decl: 'UTIL_' in decl.name ).include()
# Exclude and replace
mb.free_functions('UTIL_TraceLine').include()
mb.free_functions('UTIL_TraceHull').include()
mb.free_functions('UTIL_TraceEntity').include()
mb.free_functions('UTIL_TraceRay').exclude()
mb.free_functions('UTIL_PyTraceRay').rename('UTIL_TraceRay')
mb.free_functions('UTIL_PyEntitiesInSphere').rename('UTIL_EntitiesInSphere')
mb.free_functions('UTIL_PyEntitiesInBox').rename('UTIL_EntitiesInBox')
mb.free_functions('UTIL_EntitiesAlongRay').exclude()
mb.free_functions('UTIL_PyEntitiesAlongRay').rename('UTIL_EntitiesAlongRay')
# Enums
mb.enumeration('ShakeCommand_t').include()
# Call policies
mb.free_functions('UTIL_PlayerByIndex').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
# Exclude
# Don't care about the following functions
mb.free_functions('UTIL_LoadFileForMe').exclude()
mb.free_functions('UTIL_FreeFile').exclude()
# Exclude for now
mb.free_functions('UTIL_EntitiesInSphere').exclude()
mb.free_functions('UTIL_EntitiesInBox').exclude()
# Add
mb.free_function('StandardFilterRules').include()
mb.free_function('PassServerEntityFilter').include()
# //--------------------------------------------------------------------------------------------------------------------------------
# Tracing
cls = mb.class_('CBaseTrace')
cls.include()
cls = mb.class_('CGameTrace')
cls.include()
cls.rename('trace_t')
cls.variable('m_pEnt').rename('ent')
cls.variable('m_pEnt').getter_call_policies = call_policies.return_value_policy(call_policies.return_by_value)
cls = mb.class_('PyRay_t')
cls.include()
cls.rename('Ray_t')
cls.variables('m_Start').rename('start')
cls.variables('m_Delta').rename('delta')
cls.variables('m_StartOffset').rename('startoffset')
cls.variables('m_Extents').rename('extents')
cls.variables('m_IsRay').rename('isray')
cls.variables('m_IsSwept').rename('isswept')
# //--------------------------------------------------------------------------------------------------------------------------------
# Trace Filters
# By default, it's not possible to override TraceFilter methods
cls = mb.class_('ITraceFilter')
cls.include()
cls.calldefs().exclude()
tracefilters = [
'CTraceFilter',
'CTraceFilterEntitiesOnly',
'CTraceFilterWorldOnly',
'CTraceFilterWorldAndPropsOnly',
'CTraceFilterHitAll',
'CTraceFilterSimple',
'CTraceFilterSkipTwoEntities',
'CTraceFilterSimpleList',
'CTraceFilterOnlyNPCsAndPlayer',
'CTraceFilterNoNPCsOrPlayer',
'CTraceFilterLOS',
'CTraceFilterSkipClassname',
'CTraceFilterSkipTwoClassnames',
'CTraceFilterSimpleClassnameList',
'CTraceFilterChain',
'CPyTraceFilterSimple',
]
for clsname in tracefilters:
self.SetupTraceFilter(mb, clsname)
mb.class_('CTraceFilterSimple').rename('CTraceFilterSimpleInternal')
mb.class_('CPyTraceFilterSimple').rename('CTraceFilterSimple')
mb.member_functions('GetPassEntity').call_policies = call_policies.return_value_policy(call_policies.return_by_value)
mb.class_('csurface_t').include()
# //--------------------------------------------------------------------------------------------------------------------------------
# Collision utils
mb.free_functions('PyIntersectRayWithTriangle').include()
mb.free_functions('PyIntersectRayWithTriangle').rename('IntersectRayWithTriangle')
mb.free_functions('ComputeIntersectionBarycentricCoordinates').include()
mb.free_functions('IntersectRayWithRay').include()
mb.free_functions('IntersectRayWithSphere').include()
mb.free_functions('IntersectInfiniteRayWithSphere').include()
mb.free_functions('IsSphereIntersectingCone').include()
mb.free_functions('IntersectRayWithPlane').include()
mb.free_functions('IntersectRayWithAAPlane').include()
mb.free_functions('IntersectRayWithBox').include()
mb.class_('BoxTraceInfo_t').include()
mb.free_functions('IntersectRayWithBox').include()
mb.free_functions('IntersectRayWithOBB').include()
mb.free_functions('IsSphereIntersectingSphere').include()
if self.settings.branch != 'swarm': # IsBoxIntersectingSphere gives a problem
mb.free_functions('IsBoxIntersectingSphere').include()
mb.free_functions('IsBoxIntersectingSphereExtents').include()
mb.free_functions('IsRayIntersectingSphere').include()
mb.free_functions('IsCircleIntersectingRectangle').include()
mb.free_functions('IsOBBIntersectingOBB').include()
mb.free_functions('IsPointInCone').include()
mb.free_functions('IntersectTriangleWithPlaneBarycentric').include()
mb.enumeration('QuadBarycentricRetval_t').include()
mb.free_functions('PointInQuadToBarycentric').include()
mb.free_functions('PointInQuadFromBarycentric').include()
mb.free_functions('TexCoordInQuadFromBarycentric').include()
mb.free_functions('ComputePointFromBarycentric').include()
mb.free_functions('IsRayIntersectingOBB').include()
mb.free_functions('ComputeSeparatingPlane').include()
mb.free_functions('IsBoxIntersectingTriangle').include()
#mb.free_functions('CalcClosestPointOnTriangle').include()
mb.free_functions('OBBHasFullyContainedIntersectionWithQuad').include()
mb.free_functions('RayHasFullyContainedIntersectionWithQuad').include()
vectorcls = mb.class_('Vector')
excludetypes = [
declarated_t(vectorcls),
reference_t(declarated_t(vectorcls)),
reference_t(const_t(declarated_t(vectorcls))),
]
vectormatcher = calldef_withtypes( excludetypes )
mb.free_functions('IsBoxIntersectingBox', vectormatcher).include()
mb.free_functions('IsBoxIntersectingBoxExtents', vectormatcher).include()
mb.free_functions('IsBoxIntersectingRay', vectormatcher).include()
mb.free_functions('IsPointInBox', vectormatcher).include()
# Prediction functions
mb.free_function('GetSuppressHost').include()
mb.free_function('GetSuppressHost').call_policies = call_policies.return_value_policy( call_policies.return_by_value )
if self.isserver:
self.ParseServer(mb)
else:
self.ParseClient(mb)
# Finally apply common rules to all includes functions and classes, etc.
self.ApplyCommonRules(mb)