Skip to content

Commit 0ab65f1

Browse files
committed
Moved all conversion functions from sp_util.h to conversions_wrap.h
1 parent 7f69407 commit 0ab65f1

File tree

7 files changed

+153
-221
lines changed

7 files changed

+153
-221
lines changed

src/core/modules/commands/say_command_wrap.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "boost/shared_array.hpp"
3939
#include "core/sp_main.h"
4040
#include "modules/listeners/listenermanager.h"
41+
#include "modules/conversions/conversions_wrap.h"
4142

4243
//-----------------------------------------------------------------------------
4344
// Global say command mapping.
@@ -222,7 +223,7 @@ void SayConCommand::Dispatch( const CCommand& command )
222223
int iIndex = GetCommandIndex();
223224

224225
// Get the IPlayerInfo instance of the player
225-
IPlayerInfo* pPlayerInfo = playerinfomanager->GetPlayerInfo(PEntityOfEntIndex(iIndex));
226+
IPlayerInfo* pPlayerInfo = PlayerInfoFromIndex(iIndex);
226227

227228
// Get whether the command was say or say_team
228229
bool bTeamOnly = strcmp(command.Arg(0), "say_team") == 0;

src/core/modules/conversions/conversions_wrap.h

Lines changed: 135 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* all respects for all other code used. Additionally, the Source.Python
2424
* Development Team grants this exception to all derivative works.
2525
*/
26+
2627
#ifndef _CONVERSIONS_WRAP_H
2728
#define _CONVERSIONS_WRAP_H
2829

@@ -31,79 +32,155 @@
3132
//-----------------------------------------------------------------------------
3233
#include "edict.h"
3334
#include "basehandle.h"
35+
#include "eiface.h"
3436
#include "public/game/server/iplayerinfo.h"
3537
#include "modules/memory/memory_tools.h"
36-
#include "utility/sp_util.h"
38+
39+
40+
// Externals
41+
extern IVEngineServer* engine;
42+
extern CGlobalVars* gpGlobals;
43+
extern IPlayerInfoManager* playerinfomanager;
44+
45+
extern unsigned long ExtractPyPtr(object obj);
46+
47+
// Forward declarations
48+
edict_t* EdictFromIndex( unsigned int iEntIndex );
49+
edict_t* EdictFromUserid( unsigned int userid );
50+
edict_t* EdictFromPlayerInfo( IPlayerInfo* playerinfo );
51+
52+
IPlayerInfo* PlayerInfoFromIndex( unsigned int index );
53+
IPlayerInfo* PlayerInfoFromUserid( unsigned int userid );
3754

3855
//-----------------------------------------------------------------------------
3956
//-----------------------------------------------------------------------------
40-
inline unsigned int IndexFromEdict( edict_t* edict )
57+
inline unsigned int IndexFromEdict( edict_t* pEdict )
4158
{
42-
return IndexOfEdict(edict);
59+
#if( SOURCE_ENGINE >= 2 )
60+
return (unsigned int) (pEdict - gpGlobals->pEdicts);
61+
#else
62+
return (unsigned int) engine->IndexOfEdict(pEdict);
63+
#endif
4364
}
4465

4566
inline unsigned int IndexFromBaseHandle( CBaseHandle* basehandle )
4667
{
4768
return basehandle->GetEntryIndex();
4869
}
4970

50-
inline unsigned int IndexFromIntHandle( int inthandle )
51-
{
52-
return IndexOfIntHandle(inthandle);
53-
}
54-
55-
inline unsigned int IndexFromPointer( object pointer )
56-
{
57-
return IndexOfPointer(pointer);
71+
inline unsigned int IndexFromIntHandle( int iHandle )
72+
{
73+
CBaseHandle hHandle(iHandle);
74+
unsigned int iIndex = hHandle.GetEntryIndex();
75+
edict_t *pEntity = EdictFromIndex(iIndex);
76+
if (!pEntity || pEntity->IsFree())
77+
{
78+
return NULL;
79+
}
80+
IServerNetworkable *pNetworkable = pEntity->GetNetworkable();
81+
IHandleEntity *pEnt = pNetworkable->GetEntityHandle();
82+
if (!pEnt)
83+
{
84+
return NULL;
85+
}
86+
const CBaseHandle hTestHandle = pEnt->GetRefEHandle();
87+
if (!hTestHandle.IsValid())
88+
{
89+
return NULL;
90+
}
91+
if (hHandle.GetSerialNumber() != hTestHandle.GetSerialNumber())
92+
{
93+
return NULL;
94+
}
95+
return iIndex;
96+
}
97+
98+
inline unsigned int IndexFromPointer( object oPtr )
99+
{
100+
unsigned long ulPointer = ExtractPyPtr(oPtr);
101+
IServerUnknown *pUnknown = (IServerUnknown *) ulPointer;
102+
IServerNetworkable *pNetworkable = pUnknown->GetNetworkable();
103+
if (!pNetworkable)
104+
{
105+
return NULL;
106+
}
107+
edict_t* pEdict = pNetworkable->GetEdict();
108+
if (!pEdict)
109+
{
110+
return NULL;
111+
}
112+
return IndexFromEdict(pEdict);
58113
}
59114

60115
inline unsigned int IndexFromUserid( unsigned int userid )
61116
{
62-
return IndexOfUserid(userid);
117+
edict_t* pEdict = EdictFromUserid(userid);
118+
return pEdict ? IndexFromEdict(pEdict) : NULL;
63119
}
64120

65121
inline unsigned int IndexFromPlayerInfo( IPlayerInfo* playerinfo )
66122
{
67-
return IndexOfEdict(EdictOfPlayer(playerinfo));
123+
return IndexFromEdict(EdictFromPlayerInfo(playerinfo));
68124
}
69125

70126
//-----------------------------------------------------------------------------
71127
//-----------------------------------------------------------------------------
72-
inline edict_t* EdictFromIndex( unsigned int index )
128+
inline edict_t* EdictFromIndex( unsigned int iEntIndex )
73129
{
74-
return PEntityOfEntIndex(index);
130+
#if( SOURCE_ENGINE >= 2 )
131+
if(iEntIndex < (unsigned int) gpGlobals->maxEntities)
132+
{
133+
return (edict_t *) (gpGlobals->pEdicts + iEntIndex);
134+
}
135+
136+
return NULL;
137+
#else
138+
return engine->PEntityOfEntIndex(iEntIndex);
139+
#endif
75140
}
76141

77142
inline edict_t* EdictFromBaseHandle( CBaseHandle* basehandle )
78143
{
79-
return PEntityOfEntIndex(basehandle->GetEntryIndex());
144+
return EdictFromIndex(basehandle->GetEntryIndex());
80145
}
81146

82147
inline edict_t* EdictFromIntHandle( int inthandle )
83148
{
84-
return PEntityOfEntIndex(IndexOfIntHandle(inthandle));
149+
return EdictFromIndex(IndexFromIntHandle(inthandle));
85150
}
86151

87152
inline edict_t* EdictFromPointer( object pointer )
88153
{
89-
return PEntityOfEntIndex(IndexOfPointer(pointer));
154+
return EdictFromIndex(IndexFromPointer(pointer));
90155
}
91156

92157
inline edict_t* EdictFromUserid( unsigned int userid )
93158
{
94-
return EdictOfPlayer(PlayerOfUserid(userid));
159+
edict_t* pEdict;
160+
for(int i=1; i <= gpGlobals->maxClients; ++i)
161+
{
162+
pEdict = EdictFromIndex(i);
163+
if (pEdict && !pEdict->IsFree() &&
164+
strcmp(pEdict->GetClassName(), "player") == 0 &&
165+
engine->GetPlayerUserId(pEdict) == userid)
166+
{
167+
return pEdict;
168+
}
169+
}
170+
171+
return NULL;
95172
}
96173

97174
inline edict_t* EdictFromPlayerInfo( IPlayerInfo* playerinfo )
98175
{
99-
return EdictOfPlayer(playerinfo);
176+
return playerinfo ? EdictFromUserid(playerinfo->GetUserID()) : NULL;
100177
}
101178

102179
//-----------------------------------------------------------------------------
103180
//-----------------------------------------------------------------------------
104181
inline const CBaseHandle BaseHandleFromIndex( unsigned int index )
105182
{
106-
return PEntityOfEntIndex(index)->GetNetworkable()->GetEntityHandle()->GetRefEHandle();
183+
return EdictFromIndex(index)->GetNetworkable()->GetEntityHandle()->GetRefEHandle();
107184
}
108185

109186
inline const CBaseHandle BaseHandleFromEdict( edict_t* edict )
@@ -118,24 +195,24 @@ inline const CBaseHandle BaseHandleFromIntHandle( int inthandle )
118195

119196
inline const CBaseHandle BaseHandleFromPointer( object pointer )
120197
{
121-
return PEntityOfEntIndex(IndexOfPointer(pointer))->GetNetworkable()->GetEntityHandle()->GetRefEHandle();
198+
return EdictFromIndex(IndexFromPointer(pointer))->GetNetworkable()->GetEntityHandle()->GetRefEHandle();
122199
}
123200

124201
inline const CBaseHandle BaseHandleFromUserid( unsigned int userid )
125202
{
126-
return EdictOfUserid(userid)->GetNetworkable()->GetEntityHandle()->GetRefEHandle();
203+
return EdictFromUserid(userid)->GetNetworkable()->GetEntityHandle()->GetRefEHandle();
127204
}
128205

129206
inline const CBaseHandle BaseHandleFromPlayerInfo( IPlayerInfo* playerinfo )
130207
{
131-
return EdictOfPlayer(playerinfo)->GetNetworkable()->GetEntityHandle()->GetRefEHandle();
208+
return EdictFromPlayerInfo(playerinfo)->GetNetworkable()->GetEntityHandle()->GetRefEHandle();
132209
}
133210

134211
//-----------------------------------------------------------------------------
135212
//-----------------------------------------------------------------------------
136213
inline int IntHandleFromIndex( unsigned int index )
137214
{
138-
return PEntityOfEntIndex(index)->GetNetworkable()->GetEntityHandle()->GetRefEHandle().ToInt();
215+
return EdictFromIndex(index)->GetNetworkable()->GetEntityHandle()->GetRefEHandle().ToInt();
139216
}
140217

141218
inline int IntHandleFromEdict( edict_t* edict )
@@ -150,76 +227,76 @@ inline int IntHandleFromBaseHandle( CBaseHandle* basehandle )
150227

151228
inline int IntHandleFromPointer( object pointer )
152229
{
153-
return PEntityOfEntIndex(IndexOfPointer(pointer))->GetNetworkable()->GetEntityHandle()->GetRefEHandle().ToInt();
230+
return EdictFromIndex(IndexFromPointer(pointer))->GetNetworkable()->GetEntityHandle()->GetRefEHandle().ToInt();
154231
}
155232

156233
inline int IntHandleFromUserid( unsigned int userid )
157234
{
158-
return EdictOfUserid(userid)->GetNetworkable()->GetEntityHandle()->GetRefEHandle().ToInt();
235+
return EdictFromUserid(userid)->GetNetworkable()->GetEntityHandle()->GetRefEHandle().ToInt();
159236
}
160237

161238
inline int IntHandleFromPlayerInfo( IPlayerInfo* playerinfo )
162239
{
163-
return EdictOfPlayer(playerinfo)->GetNetworkable()->GetEntityHandle()->GetRefEHandle().ToInt();
240+
return EdictFromPlayerInfo(playerinfo)->GetNetworkable()->GetEntityHandle()->GetRefEHandle().ToInt();
164241
}
165242

166243
//-----------------------------------------------------------------------------
167244
//-----------------------------------------------------------------------------
168245
inline CPointer* PointerFromIndex( unsigned int index )
169246
{
170-
return new CPointer((unsigned int)(PEntityOfEntIndex(index)->GetNetworkable()->GetBaseEntity()));
247+
return new CPointer((unsigned long)(EdictFromIndex(index)->GetNetworkable()->GetBaseEntity()));
171248
}
172249

173250
inline CPointer* PointerFromEdict( edict_t* edict )
174251
{
175-
return new CPointer((unsigned int)(edict->GetNetworkable()->GetBaseEntity()));
252+
return new CPointer((unsigned long)(edict->GetNetworkable()->GetBaseEntity()));
176253
}
177254

178255
inline CPointer* PointerFromBaseHandle( CBaseHandle* basehandle )
179256
{
180-
return new CPointer((unsigned int)(PEntityOfEntIndex(basehandle->GetEntryIndex())->GetNetworkable()->GetBaseEntity()));
257+
return new CPointer((unsigned long)(EdictFromIndex(basehandle->GetEntryIndex())->GetNetworkable()->GetBaseEntity()));
181258
}
182259

183260
inline CPointer* PointerFromIntHandle( int inthandle )
184261
{
185-
return new CPointer((unsigned int)(PEntityOfEntIndex(IndexOfIntHandle(inthandle))->GetNetworkable()->GetBaseEntity()));
262+
return new CPointer((unsigned long)(EdictFromIndex(IndexFromIntHandle(inthandle))->GetNetworkable()->GetBaseEntity()));
186263
}
187264

188265
inline CPointer* PointerFromUserid( unsigned int userid )
189266
{
190-
return new CPointer((unsigned int)(EdictOfPlayer(PlayerOfUserid(userid))->GetNetworkable()->GetBaseEntity()));
267+
return new CPointer((unsigned long)(EdictFromPlayerInfo(PlayerInfoFromUserid(userid))->GetNetworkable()->GetBaseEntity()));
191268
}
192269

193270
inline CPointer* PointerFromPlayerInfo( IPlayerInfo* playerinfo )
194271
{
195-
return new CPointer((unsigned int)(EdictOfPlayer(playerinfo)->GetNetworkable()->GetBaseEntity()));
272+
return new CPointer((unsigned long)(EdictFromPlayerInfo(playerinfo)->GetNetworkable()->GetBaseEntity()));
196273
}
197274

198275
//-----------------------------------------------------------------------------
199276
//-----------------------------------------------------------------------------
200277
inline unsigned int UseridFromIndex( unsigned int index )
201278
{
202-
return PlayerOfIndex(index)->GetUserID();
279+
return PlayerInfoFromIndex(index)->GetUserID();
203280
}
204281

205282
inline unsigned int UseridFromEdict( edict_t* edict )
206283
{
207-
return PlayerOfIndex(IndexOfEdict(edict))->GetUserID();
284+
return PlayerInfoFromIndex(IndexFromEdict(edict))->GetUserID();
208285
}
209286

210287
inline unsigned int UseridFromBaseHandle( CBaseHandle* basehandle )
211288
{
212-
return PlayerOfIndex(basehandle->GetEntryIndex())->GetUserID();
289+
return PlayerInfoFromIndex(basehandle->GetEntryIndex())->GetUserID();
213290
}
214291

215292
inline unsigned int UseridFromIntHandle( int inthandle )
216293
{
217-
return PlayerOfIndex(IndexOfIntHandle(inthandle))->GetUserID();
294+
return PlayerInfoFromIndex(IndexFromIntHandle(inthandle))->GetUserID();
218295
}
219296

220297
inline unsigned int UseridFromPointer( object pointer )
221298
{
222-
return PlayerOfIndex(IndexOfPointer(pointer))->GetUserID();
299+
return PlayerInfoFromIndex(IndexFromPointer(pointer))->GetUserID();
223300
}
224301

225302
inline unsigned int UseridFromPlayerInfo( IPlayerInfo* playerinfo )
@@ -231,32 +308,45 @@ inline unsigned int UseridFromPlayerInfo( IPlayerInfo* playerinfo )
231308
//-----------------------------------------------------------------------------
232309
inline IPlayerInfo* PlayerInfoFromIndex( unsigned int index )
233310
{
234-
return PlayerOfIndex(index);
311+
if (index < 1 || index > (unsigned int) gpGlobals->maxClients)
312+
{
313+
return NULL;
314+
}
315+
316+
edict_t* pEdict = EdictFromIndex(index);
317+
if (pEdict && !pEdict->IsFree() &&
318+
strcmp(pEdict->GetClassName(), "player") == 0)
319+
{
320+
return playerinfomanager->GetPlayerInfo(pEdict);
321+
}
322+
323+
return NULL;
235324
}
236325

237326
inline IPlayerInfo* PlayerInfoFromEdict( edict_t* edict )
238327
{
239-
return PlayerOfIndex(IndexOfEdict(edict));
328+
return PlayerInfoFromIndex(IndexFromEdict(edict));
240329
}
241330

242331
inline IPlayerInfo* PlayerInfoFromBaseHandle( CBaseHandle* basehandle )
243332
{
244-
return PlayerOfIndex(basehandle->GetEntryIndex());
333+
return PlayerInfoFromIndex(basehandle->GetEntryIndex());
245334
}
246335

247336
inline IPlayerInfo* PlayerInfoFromIntHandle( int inthandle )
248337
{
249-
return PlayerOfIndex(IndexOfIntHandle(inthandle));
338+
return PlayerInfoFromIndex(IndexFromIntHandle(inthandle));
250339
}
251340

252341
inline IPlayerInfo* PlayerInfoFromPointer( object pointer )
253342
{
254-
return PlayerOfIndex(IndexOfPointer(pointer));
343+
return PlayerInfoFromIndex(IndexFromPointer(pointer));
255344
}
256345

257346
inline IPlayerInfo* PlayerInfoFromUserid( unsigned int userid )
258347
{
259-
return PlayerOfUserid(userid);
348+
edict_t* pEdict = EdictFromUserid(userid);
349+
return pEdict ? playerinfomanager->GetPlayerInfo(pEdict) : NULL;
260350
}
261351

262352
#endif // _CONVERSIONS_WRAP_H

0 commit comments

Comments
 (0)