extensions/sdktools/vdecoder.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Sep 14 02:58:58 2008 -0500 (2008-09-14)
changeset 2287 017a725c9d17
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Eliminated head for dead branch: sourcemod-1.1.0
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod SDKTools Extension
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include "smsdk_ext.h"
ds@1952
    33
#include "extension.h"
ds@1952
    34
#include "vdecoder.h"
ds@1952
    35
#include "vcallbuilder.h"
ds@1952
    36
ds@1952
    37
using namespace SourceMod;
ds@1952
    38
using namespace SourcePawn;
ds@1952
    39
ds@1952
    40
/**
ds@1952
    41
 * For object pointers, the data looks like this instead:
ds@1952
    42
 * 4 bytes: POINTER TO LATER
ds@1952
    43
 * + bytes: Object internal data
ds@1952
    44
 *
ds@1952
    45
 * We use the virtual stack as extra fake stack space and create a temp object.
ds@1952
    46
 * If these objects had destructors, we'd need to fake destroy toom of course.
ds@1952
    47
 * Of course, BinTools only reads the first four bytes and passes the pointer.
ds@1952
    48
 */
ds@1952
    49
ds@1952
    50
size_t ValveParamToBinParam(ValveType type, 
ds@1952
    51
						  PassType pass,
ds@1952
    52
						  unsigned int flags,
ds@1952
    53
						  PassInfo *info,
ds@1952
    54
						  bool &needs_extra)
ds@1952
    55
{
ds@1952
    56
	needs_extra = false;
ds@1952
    57
	switch (type)
ds@1952
    58
	{
ds@1952
    59
	case Valve_Vector:
ds@1952
    60
		{
ds@1952
    61
			size_t mySize = sizeof(Vector *);
ds@1952
    62
			if (pass == PassType_Basic)
ds@1952
    63
			{
ds@1952
    64
				if (flags & PASSFLAG_BYREF)
ds@1952
    65
				{
ds@1952
    66
					return 0;
ds@1952
    67
				}
ds@1952
    68
				info->type = PassType_Basic;
ds@1952
    69
				info->flags = flags;
ds@1952
    70
				info->size = sizeof(Vector *);
ds@1952
    71
				mySize = sizeof(Vector);
ds@1952
    72
				needs_extra = true;
ds@1952
    73
			} else if (pass == PassType_Object) {
ds@1952
    74
				info->type = PassType_Object;
ds@1952
    75
				info->flags = flags | PASSFLAG_OASSIGNOP | PASSFLAG_OCTOR;
ds@1952
    76
				info->size = sizeof(Vector);
ds@1952
    77
			} else {
ds@1952
    78
				return 0;
ds@1952
    79
			}
ds@1952
    80
			return mySize;
ds@1952
    81
		}
ds@1952
    82
	case Valve_QAngle:
ds@1952
    83
		{
ds@1952
    84
			size_t mySize = sizeof(QAngle *);
ds@1952
    85
			if (pass == PassType_Basic)
ds@1952
    86
			{
ds@1952
    87
				if (flags & PASSFLAG_BYREF)
ds@1952
    88
				{
ds@1952
    89
					return 0;
ds@1952
    90
				}
ds@1952
    91
				info->type = PassType_Basic;
ds@1952
    92
				info->flags = flags;
ds@1952
    93
				info->size = sizeof(QAngle *);
ds@1952
    94
				mySize = sizeof(QAngle);
ds@1952
    95
				needs_extra = true;
ds@1952
    96
			} else if (pass == PassType_Object) {
ds@1952
    97
				info->type = PassType_Object;
ds@1952
    98
				info->flags = flags | PASSFLAG_OASSIGNOP | PASSFLAG_OCTOR;
ds@1952
    99
				info->size = sizeof(QAngle);
ds@1952
   100
			} else {
ds@1952
   101
				return 0;
ds@1952
   102
			}
ds@1952
   103
			return mySize;
ds@1952
   104
		}
ds@1952
   105
	case Valve_CBaseEntity:
ds@1952
   106
	case Valve_CBasePlayer:
ds@1952
   107
	case Valve_Edict:
ds@1952
   108
	case Valve_String:
ds@1952
   109
		{
ds@1952
   110
			if (pass != PassType_Basic || (flags & PASSFLAG_BYREF))
ds@1952
   111
			{
ds@1952
   112
				return 0;
ds@1952
   113
			}
ds@1952
   114
			info->type = PassType_Basic;
ds@1952
   115
			info->flags = flags;
ds@1952
   116
			info->size = sizeof(void *);
ds@1952
   117
			return sizeof(void *);
ds@1952
   118
		}
ds@1952
   119
	case Valve_POD:
ds@1952
   120
		{
ds@1952
   121
			info->type = PassType_Basic;
ds@1952
   122
			info->flags = flags;
ds@1952
   123
			if (flags & PASSFLAG_ASPOINTER)
ds@1952
   124
			{
ds@1952
   125
				needs_extra = true;
ds@1952
   126
				info->size = sizeof(int *);
ds@1952
   127
				return sizeof(int *) + sizeof(int);
ds@1952
   128
			} else {
ds@1952
   129
				info->size = sizeof(int);
ds@1952
   130
				return sizeof(int);
ds@1952
   131
			}
ds@1952
   132
		}
ds@1952
   133
	case Valve_Bool:
ds@1952
   134
		{
ds@1952
   135
			info->type = PassType_Basic;
ds@1952
   136
			info->flags = flags;
ds@1952
   137
			if (flags & PASSFLAG_ASPOINTER)
ds@1952
   138
			{
ds@1952
   139
				needs_extra = true;
ds@1952
   140
				info->size = sizeof(bool *);
ds@1952
   141
				return sizeof(bool *) + sizeof(bool);
ds@1952
   142
			} else {
ds@1952
   143
				info->size = sizeof(bool);
ds@1952
   144
				return sizeof(bool);
ds@1952
   145
			}
ds@1952
   146
		}
ds@1952
   147
	case Valve_Float:
ds@1952
   148
		{
ds@1952
   149
			info->flags = flags;
ds@1952
   150
			if (flags & PASSFLAG_ASPOINTER)
ds@1952
   151
			{
ds@1952
   152
				needs_extra = true;
ds@1952
   153
				info->type = PassType_Basic;
ds@1952
   154
				info->size = sizeof(float *);
ds@1952
   155
				return sizeof(float *) + sizeof(float);
ds@1952
   156
			} else {
ds@1952
   157
				info->type = PassType_Float;
ds@1952
   158
				info->size = sizeof(float);
ds@1952
   159
				return sizeof(float);
ds@1952
   160
			}
ds@1952
   161
		}
ds@1952
   162
	}
ds@1952
   163
ds@1952
   164
	return 0;
ds@1952
   165
}
ds@1952
   166
ds@1952
   167
DataStatus EncodeValveParam(IPluginContext *pContext,
ds@1952
   168
							cell_t param, 
ds@1952
   169
							const ValveCall *pCall,
ds@1952
   170
							const ValvePassInfo *data,
ds@1952
   171
							const void *_buffer)
ds@1952
   172
{
ds@1952
   173
	const void *buffer = (const unsigned char *)_buffer + data->offset;
ds@1952
   174
	switch (data->vtype)
ds@1952
   175
	{
ds@1952
   176
	case Valve_Vector:
ds@1952
   177
		{
ds@1952
   178
			Vector *v = NULL;
ds@1952
   179
ds@1952
   180
			if (data->type == PassType_Basic)
ds@1952
   181
			{
ds@1952
   182
				v = *(Vector **)buffer;
ds@1952
   183
			} else if (data->type == PassType_Object) {
ds@1952
   184
				v = (Vector *)buffer;
ds@1952
   185
			}
ds@1952
   186
ds@1952
   187
			cell_t *addr;
ds@1952
   188
			pContext->LocalToPhysAddr(param, &addr);
ds@1952
   189
ds@1952
   190
			addr[0] = sp_ftoc(v->x);
ds@1952
   191
			addr[1] = sp_ftoc(v->y);
ds@1952
   192
			addr[2] = sp_ftoc(v->z);
ds@1952
   193
ds@1952
   194
			return Data_Okay;
ds@1952
   195
		}
ds@1952
   196
	case Valve_QAngle:
ds@1952
   197
		{
ds@1952
   198
			QAngle *q = NULL;
ds@1952
   199
ds@1952
   200
			if (data->type == PassType_Basic)
ds@1952
   201
			{
ds@1952
   202
				q = *(QAngle **)buffer;
ds@1952
   203
			} else if (data->type == PassType_Object) {
ds@1952
   204
				q = (QAngle *)buffer;
ds@1952
   205
			}
ds@1952
   206
ds@1952
   207
			cell_t *addr;
ds@1952
   208
			pContext->LocalToPhysAddr(param, &addr);
ds@1952
   209
ds@1952
   210
			addr[0] = sp_ftoc(q->x);
ds@1952
   211
			addr[1] = sp_ftoc(q->y);
ds@1952
   212
			addr[2] = sp_ftoc(q->z);
ds@1952
   213
ds@1952
   214
			return Data_Okay;
ds@1952
   215
		}
ds@1952
   216
	case Valve_CBaseEntity:
ds@1952
   217
	case Valve_CBasePlayer:
ds@1952
   218
		{
ds@1952
   219
			cell_t *addr;
ds@1952
   220
			pContext->LocalToPhysAddr(param, &addr);
ds@1952
   221
ds@1952
   222
			CBaseEntity *pEntity = *(CBaseEntity **)buffer;
ds@1952
   223
			if (pEntity)
ds@1952
   224
			{
ds@1952
   225
				edict_t *pEdict = gameents->BaseEntityToEdict(pEntity);
ds@1952
   226
				*addr = engine->IndexOfEdict(pEdict);
ds@1952
   227
			} else {
ds@1952
   228
				*addr = -1;
ds@1952
   229
			}
ds@1952
   230
ds@1952
   231
			return Data_Okay;
ds@1952
   232
		}
ds@1952
   233
	case Valve_Edict:
ds@1952
   234
		{
ds@1952
   235
			cell_t *addr;
ds@1952
   236
			pContext->LocalToPhysAddr(param, &addr);
ds@1952
   237
ds@1952
   238
			edict_t *pEdict = *(edict_t **)buffer;
ds@1952
   239
			if (pEdict)
ds@1952
   240
			{
ds@1952
   241
				*addr = engine->IndexOfEdict(pEdict);
ds@1952
   242
			} else {
ds@1952
   243
				*addr = -1;
ds@1952
   244
			}
ds@1952
   245
ds@1952
   246
			return Data_Okay;
ds@1952
   247
		}
ds@1952
   248
	case Valve_POD:
ds@1952
   249
	case Valve_Float:
ds@1952
   250
		{
ds@1952
   251
			cell_t *addr;
ds@1952
   252
			pContext->LocalToPhysAddr(param, &addr);
ds@1952
   253
ds@1952
   254
			if (data->flags & PASSFLAG_ASPOINTER)
ds@1952
   255
			{
ds@1952
   256
				buffer = *(cell_t **)buffer;
ds@1952
   257
			}
ds@1952
   258
ds@1952
   259
			*addr = *(cell_t *)buffer;
ds@1952
   260
ds@1952
   261
			return Data_Okay;
ds@1952
   262
		}
ds@1952
   263
	case Valve_Bool:
ds@1952
   264
		{
ds@1952
   265
			cell_t *addr;
ds@1952
   266
			pContext->LocalToPhysAddr(param, &addr);
ds@1952
   267
ds@1952
   268
			if (data->flags & PASSFLAG_ASPOINTER)
ds@1952
   269
			{
ds@1952
   270
				buffer = *(bool **)buffer;
ds@1952
   271
			}
ds@1952
   272
ds@1952
   273
			*addr = *(bool *)buffer ? 1 : 0;
ds@1952
   274
ds@1952
   275
			return Data_Okay;
ds@1952
   276
		}
ds@1952
   277
	}
ds@1952
   278
ds@1952
   279
	return Data_Fail;
ds@1952
   280
}
ds@1952
   281
ds@1952
   282
DataStatus DecodeValveParam(IPluginContext *pContext,
ds@1952
   283
					  cell_t param,
ds@1952
   284
					  const ValveCall *pCall,
ds@1952
   285
					  const ValvePassInfo *data,
ds@1952
   286
					  void *_buffer)
ds@1952
   287
{
ds@1952
   288
	void *buffer = (unsigned char *)_buffer + data->offset;
ds@1952
   289
	switch (data->vtype)
ds@1952
   290
	{
ds@1952
   291
	case Valve_Vector:
ds@1952
   292
		{
ds@1952
   293
			cell_t *addr;
ds@1952
   294
			int err;
ds@1952
   295
			err = pContext->LocalToPhysAddr(param, &addr);
ds@1952
   296
ds@1952
   297
			unsigned char *mem = (unsigned char *)buffer;
ds@1952
   298
			if (data->type == PassType_Basic)
ds@1952
   299
			{
ds@1952
   300
				/* Store the object in the next N bytes, and store
ds@1952
   301
				 * a pointer to that object right beforehand.
ds@1952
   302
				 */
ds@1952
   303
				Vector **realPtr = (Vector **)buffer;
ds@1952
   304
ds@1952
   305
				if (addr == pContext->GetNullRef(SP_NULL_VECTOR))
ds@1952
   306
				{
ds@1952
   307
					if (data->decflags & VDECODE_FLAG_ALLOWNULL)
ds@1952
   308
					{
ds@1952
   309
						*realPtr = NULL;
ds@1952
   310
						return Data_Okay;
ds@1952
   311
					} else {
ds@1952
   312
						pContext->ThrowNativeError("NULL not allowed");
ds@1952
   313
						return Data_Fail;
ds@1952
   314
					}
ds@1952
   315
				} else {
ds@1952
   316
					mem = (unsigned char *)_buffer + pCall->stackEnd + data->obj_offset;
ds@1952
   317
					*realPtr = (Vector *)mem;
ds@1952
   318
				}
ds@1952
   319
			}
ds@1952
   320
ds@1952
   321
			if (err != SP_ERROR_NONE)
ds@1952
   322
			{
ds@1952
   323
				pContext->ThrowNativeErrorEx(err, "Could not read plugin data");
ds@1952
   324
				return Data_Fail;
ds@1952
   325
			}
ds@1952
   326
ds@1952
   327
			/* Use placement new to initialize the object cleanly
ds@1952
   328
			 * This has no destructor so we don't need to do 
ds@1952
   329
			 * DestroyValveParam() or something :]
ds@1952
   330
			 */
ds@1952
   331
			Vector *v = new (mem) Vector(
ds@1952
   332
				sp_ctof(addr[0]),
ds@1952
   333
				sp_ctof(addr[1]),
ds@1952
   334
				sp_ctof(addr[2]));
ds@1952
   335
ds@1952
   336
			return Data_Okay;
ds@1952
   337
		}
ds@1952
   338
	case Valve_QAngle:
ds@1952
   339
		{
ds@1952
   340
			cell_t *addr;
ds@1952
   341
			int err;
ds@1952
   342
			err = pContext->LocalToPhysAddr(param, &addr);
ds@1952
   343
ds@1952
   344
			unsigned char *mem = (unsigned char *)buffer;
ds@1952
   345
			if (data->type == PassType_Basic)
ds@1952
   346
			{
ds@1952
   347
				/* Store the object in the next N bytes, and store
ds@1952
   348
				 * a pointer to that object right beforehand.
ds@1952
   349
				 */
ds@1952
   350
				QAngle **realPtr = (QAngle **)buffer;
ds@1952
   351
ds@1952
   352
				if (addr == pContext->GetNullRef(SP_NULL_VECTOR))
ds@1952
   353
				{
ds@1952
   354
					if (!(data->decflags & VDECODE_FLAG_ALLOWNULL))
ds@1952
   355
					{
ds@1952
   356
						pContext->ThrowNativeError("NULL not allowed");
ds@1952
   357
						return Data_Fail;
ds@1952
   358
					} else {
ds@1952
   359
						*realPtr = NULL;
ds@1952
   360
						return Data_Okay;
ds@1952
   361
					}
ds@1952
   362
				} else {
ds@1952
   363
					mem = (unsigned char *)_buffer + pCall->stackEnd + data->obj_offset;
ds@1952
   364
					*realPtr = (QAngle *)mem;
ds@1952
   365
				}
ds@1952
   366
			}
ds@1952
   367
ds@1952
   368
			if (err != SP_ERROR_NONE)
ds@1952
   369
			{
ds@1952
   370
				pContext->ThrowNativeErrorEx(err, "Could not read plugin data");
ds@1952
   371
				return Data_Fail;
ds@1952
   372
			}
ds@1952
   373
ds@1952
   374
			/* Use placement new to initialize the object cleanly
ds@1952
   375
			 * This has no destructor so we don't need to do 
ds@1952
   376
			 * DestroyValveParam() or something :]
ds@1952
   377
			 */
ds@1952
   378
			QAngle *v = new (mem) QAngle(
ds@1952
   379
				sp_ctof(addr[0]),
ds@1952
   380
				sp_ctof(addr[1]),
ds@1952
   381
				sp_ctof(addr[2]));
ds@1952
   382
ds@1952
   383
			return Data_Okay;
ds@1952
   384
		}
ds@1952
   385
	case Valve_CBasePlayer:
ds@1952
   386
		{
ds@1952
   387
			edict_t *pEdict;
ds@1952
   388
			if (data->decflags & VDECODE_FLAG_BYREF)
ds@1952
   389
			{
ds@1952
   390
				cell_t *addr;
ds@1952
   391
				pContext->LocalToPhysAddr(param, &addr);
ds@1952
   392
				param = *addr;
ds@1952
   393
			}
ds@1952
   394
			if (param >= 1 && param <= playerhelpers->GetMaxClients())
ds@1952
   395
			{
ds@1952
   396
				IGamePlayer *player = playerhelpers->GetGamePlayer(param);
ds@1952
   397
				if ((data->decflags & VDECODE_FLAG_ALLOWNOTINGAME)
ds@1952
   398
					&& !player->IsConnected())
ds@1952
   399
				{
ds@1952
   400
					pContext->ThrowNativeError("Client %d is not connected", param);
ds@1952
   401
					return Data_Fail;
ds@1952
   402
				} else if (!player->IsInGame()) {
ds@1952
   403
					pContext->ThrowNativeError("Client %d is not in game", param);
ds@1952
   404
					return Data_Fail;
ds@1952
   405
				}
ds@1952
   406
				pEdict = player->GetEdict();
ds@1952
   407
			} else if (param == -1) {
ds@1952
   408
				if (data->decflags & VDECODE_FLAG_ALLOWNULL)
ds@1952
   409
				{
ds@1952
   410
					pEdict = NULL;
ds@1952
   411
				} else {
ds@1952
   412
					pContext->ThrowNativeError("NULL not allowed");
ds@1952
   413
					return Data_Fail;
ds@1952
   414
				}
ds@1952
   415
			} else if (param == 0) {
ds@1952
   416
				if (data->decflags & VDECODE_FLAG_ALLOWWORLD)
ds@1952
   417
				{
ds@1952
   418
					pEdict = engine->PEntityOfEntIndex(0);
ds@1952
   419
				} else {
ds@1952
   420
					pContext->ThrowNativeError("World not allowed");
ds@1952
   421
					return Data_Fail;
ds@1952
   422
				}
ds@1952
   423
			} else {
ds@1952
   424
				pContext->ThrowNativeError("Entity index %d is not a valid client", param);
ds@1952
   425
				return Data_Fail;
ds@1952
   426
			}
ds@1952
   427
			CBaseEntity *pEntity = NULL;
ds@1952
   428
			if (pEdict)
ds@1952
   429
			{
ds@1952
   430
				IServerUnknown *pUnknown = pEdict->GetUnknown();
ds@1952
   431
				if (!pUnknown)
ds@1952
   432
				{
ds@1952
   433
					pContext->ThrowNativeError("Entity %d is a not an IServerUnknown", param);
ds@1952
   434
					return Data_Fail;
ds@1952
   435
				}
ds@1952
   436
				pEntity = pUnknown->GetBaseEntity();
ds@1952
   437
				if (!pEntity)
ds@1952
   438
				{
ds@1952
   439
					pContext->ThrowNativeError("Entity %d is not a CBaseEntity", param);
ds@1952
   440
					return Data_Fail;
ds@1952
   441
				}
ds@1952
   442
			}
ds@1952
   443
ds@1952
   444
			CBaseEntity **ebuf = (CBaseEntity **)buffer;
ds@1952
   445
			*ebuf = pEntity;
ds@1952
   446
ds@1952
   447
			return Data_Okay;
ds@1952
   448
		}
ds@1952
   449
	case Valve_CBaseEntity:
ds@1952
   450
		{
ds@1952
   451
			edict_t *pEdict;
ds@1952
   452
			if (data->decflags & VDECODE_FLAG_BYREF)
ds@1952
   453
			{
ds@1952
   454
				cell_t *addr;
ds@1952
   455
				pContext->LocalToPhysAddr(param, &addr);
ds@1952
   456
				param = *addr;
ds@1952
   457
			}
ds@1952
   458
			if (param >= 1 && param <= playerhelpers->GetMaxClients())
ds@1952
   459
			{
ds@1952
   460
				IGamePlayer *player = playerhelpers->GetGamePlayer(param);
ds@1952
   461
				if ((data->decflags & VDECODE_FLAG_ALLOWNOTINGAME)
ds@1952
   462
					&& !player->IsConnected())
ds@1952
   463
				{
ds@1952
   464
					pContext->ThrowNativeError("Client %d is not connected", param);
ds@1952
   465
					return Data_Fail;
ds@1952
   466
				} else if (!player->IsInGame()) {
ds@1952
   467
					pContext->ThrowNativeError("Client %d is not in game", param);
ds@1952
   468
					return Data_Fail;
ds@1952
   469
				}
ds@1952
   470
				pEdict = player->GetEdict();
ds@1952
   471
			} else if (param == -1) {
ds@1952
   472
				if (data->decflags & VDECODE_FLAG_ALLOWNULL)
ds@1952
   473
				{
ds@1952
   474
					pEdict = NULL;
ds@1952
   475
				} else {
ds@1952
   476
					pContext->ThrowNativeError("NULL not allowed");
ds@1952
   477
					return Data_Fail;
ds@1952
   478
				}
ds@1952
   479
			} else if (param == 0) {
ds@1952
   480
				if (data->decflags & VDECODE_FLAG_ALLOWWORLD)
ds@1952
   481
				{
ds@1952
   482
					pEdict = engine->PEntityOfEntIndex(0);
ds@1952
   483
				} else {
ds@1952
   484
					pContext->ThrowNativeError("World not allowed");
ds@1952
   485
					return Data_Fail;
ds@1952
   486
				}
ds@1952
   487
			} else {
ds@1952
   488
				pEdict = engine->PEntityOfEntIndex(param);
ds@1952
   489
				if (!pEdict || pEdict->IsFree())
ds@1952
   490
				{
ds@1952
   491
					pContext->ThrowNativeError("Entity %d is not valid or is freed", param);
ds@1952
   492
					return Data_Fail;
ds@1952
   493
				}
ds@1952
   494
			}
ds@1952
   495
			CBaseEntity *pEntity = NULL;
ds@1952
   496
			if (pEdict)
ds@1952
   497
			{
ds@1952
   498
				IServerUnknown *pUnknown = pEdict->GetUnknown();
ds@1952
   499
				if (!pUnknown)
ds@1952
   500
				{
ds@1952
   501
					pContext->ThrowNativeError("Entity %d is a not an IServerUnknown", param);
ds@1952
   502
					return Data_Fail;
ds@1952
   503
				}
ds@1952
   504
				pEntity = pUnknown->GetBaseEntity();
ds@1952
   505
				if (!pEntity)
ds@1952
   506
				{
ds@1952
   507
					pContext->ThrowNativeError("Entity %d is not a CBaseEntity", param);
ds@1952
   508
					return Data_Fail;
ds@1952
   509
				}
ds@1952
   510
			}
ds@1952
   511
ds@1952
   512
			CBaseEntity **ebuf = (CBaseEntity **)buffer;
ds@1952
   513
			*ebuf = pEntity;
ds@1952
   514
ds@1952
   515
			return Data_Okay;
ds@1952
   516
		}
ds@1952
   517
	case Valve_Edict:
ds@1952
   518
		{
ds@1952
   519
			edict_t *pEdict;
ds@1952
   520
			if (data->decflags & VDECODE_FLAG_BYREF)
ds@1952
   521
			{
ds@1952
   522
				cell_t *addr;
ds@1952
   523
				pContext->LocalToPhysAddr(param, &addr);
ds@1952
   524
				param = *addr;
ds@1952
   525
			}
ds@1952
   526
			if (param >= 1 && param <= playerhelpers->GetMaxClients())
ds@1952
   527
			{
ds@1952
   528
				IGamePlayer *player = playerhelpers->GetGamePlayer(param);
ds@1952
   529
				if ((data->decflags & VDECODE_FLAG_ALLOWNOTINGAME)
ds@1952
   530
					&& !player->IsConnected())
ds@1952
   531
				{
ds@1952
   532
					pContext->ThrowNativeError("Client %d is not connected", param);
ds@1952
   533
					return Data_Fail;
ds@1952
   534
				} else if (!player->IsInGame()) {
ds@1952
   535
					pContext->ThrowNativeError("Client %d is not in game", param);
ds@1952
   536
					return Data_Fail;
ds@1952
   537
				}
ds@1952
   538
				pEdict = player->GetEdict();
ds@1952
   539
			} else if (param == -1) {
ds@1952
   540
				if (data->decflags & VDECODE_FLAG_ALLOWNULL)
ds@1952
   541
				{
ds@1952
   542
					pEdict = NULL;
ds@1952
   543
				} else {
ds@1952
   544
					pContext->ThrowNativeError("NULL not allowed");
ds@1952
   545
					return Data_Fail;
ds@1952
   546
				}
ds@1952
   547
			} else if (param == 0) {
ds@1952
   548
				if (data->decflags & VDECODE_FLAG_ALLOWWORLD)
ds@1952
   549
				{
ds@1952
   550
					pEdict = engine->PEntityOfEntIndex(0);
ds@1952
   551
				} else {
ds@1952
   552
					pContext->ThrowNativeError("World not allowed");
ds@1952
   553
					return Data_Fail;
ds@1952
   554
				}
ds@1952
   555
			} else {
ds@1952
   556
				pEdict = engine->PEntityOfEntIndex(param);
ds@1952
   557
				if (!pEdict || pEdict->IsFree())
ds@1952
   558
				{
ds@1952
   559
					pContext->ThrowNativeError("Entity %d is not valid or is freed", param);
ds@1952
   560
					return Data_Fail;
ds@1952
   561
				}
ds@1952
   562
			}
ds@1952
   563
ds@1952
   564
			edict_t **ebuf = (edict_t **)buffer;
ds@1952
   565
			*ebuf = pEdict;
ds@1952
   566
ds@1952
   567
			return Data_Okay;
ds@1952
   568
		}
ds@1952
   569
	case Valve_POD:
ds@1952
   570
	case Valve_Float:
ds@1952
   571
		{
ds@1952
   572
			if (data->decflags & VDECODE_FLAG_BYREF)
ds@1952
   573
			{
ds@1952
   574
				cell_t *addr;
ds@1952
   575
				pContext->LocalToPhysAddr(param, &addr);
ds@1952
   576
				param = *addr;
ds@1952
   577
			}
ds@1952
   578
			if (data->flags & PASSFLAG_ASPOINTER)
ds@1952
   579
			{
ds@1952
   580
				*(void **)buffer = (unsigned char *)_buffer + pCall->stackEnd + data->obj_offset;
ds@1952
   581
				buffer = *(void **)buffer;
ds@1952
   582
			}
ds@1952
   583
			*(cell_t *)buffer = param;
ds@1952
   584
			return Data_Okay;
ds@1952
   585
		}
ds@1952
   586
	case Valve_Bool:
ds@1952
   587
		{
ds@1952
   588
			if (data->decflags & VDECODE_FLAG_BYREF)
ds@1952
   589
			{
ds@1952
   590
				cell_t *addr;
ds@1952
   591
				pContext->LocalToPhysAddr(param, &addr);
ds@1952
   592
				param = *addr;
ds@1952
   593
			}
ds@1952
   594
			if (data->flags & PASSFLAG_ASPOINTER)
ds@1952
   595
			{
ds@1952
   596
				*(bool **)buffer = (bool *)((unsigned char *)_buffer + pCall->stackEnd + data->obj_offset);
ds@1952
   597
				buffer = *(bool **)buffer;
ds@1952
   598
			}
ds@1952
   599
			*(bool *)buffer = param ? true : false;
ds@1952
   600
			return Data_Okay;
ds@1952
   601
		}
ds@1952
   602
	case Valve_String:
ds@1952
   603
		{
ds@1952
   604
			char *addr;
ds@1952
   605
			pContext->LocalToString(param, &addr);
ds@1952
   606
			*(char **)buffer = addr;
ds@1952
   607
			return Data_Okay;
ds@1952
   608
		}
ds@1952
   609
	}
ds@1952
   610
ds@1952
   611
	return Data_Fail;
ds@1952
   612
}