core/smn_entities.cpp
author Asher Baker <asherkin@gmail.com>
Mon Sep 03 20:26:39 2012 +0100 (2012-09-03)
changeset 3806 c713139e894c
parent 3762 0f6b4d692ed9
child 3961 0dc477c29792
permissions -rw-r--r--
Error on access to nested datamap props (bug 5446, r=psychonic).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "sm_globals.h"
    33 #include "sourcemod.h"
    34 #include "sourcemm_api.h"
    35 #include "PlayerManager.h"
    36 #include "HalfLife2.h"
    37 #include <IGameConfigs.h>
    38 #include "sm_stringutil.h"
    39 #include "logic_bridge.h"
    40 
    41 // These values need to mirror the values in entity_prop_stocks
    42 #define ENTFLAG_ONGROUND		(1 << 0)
    43 #define ENTFLAG_DUCKING			(1 << 1)
    44 #define ENTFLAG_WATERJUMP		(1 << 2)
    45 #define ENTFLAG_ONTRAIN			(1 << 3)
    46 #define ENTFLAG_INRAIN			(1 << 4)
    47 #define ENTFLAG_FROZEN			(1 << 5)
    48 #define ENTFLAG_ATCONTROLS		(1 << 6)
    49 #define ENTFLAG_CLIENT			(1 << 7)
    50 #define ENTFLAG_FAKECLIENT		(1 << 8)
    51 #define ENTFLAG_INWATER			(1 << 9)
    52 #define ENTFLAG_FLY				(1 << 10)
    53 #define ENTFLAG_SWIM			(1 << 11)
    54 #define ENTFLAG_CONVEYOR		(1 << 12)
    55 #define ENTFLAG_NPC				(1 << 13)
    56 #define ENTFLAG_GODMODE			(1 << 14)
    57 #define ENTFLAG_NOTARGET		(1 << 15)
    58 #define ENTFLAG_AIMTARGET		(1 << 16)
    59 #define ENTFLAG_PARTIALGROUND	(1 << 17)
    60 #define ENTFLAG_STATICPROP		(1 << 18)
    61 #define ENTFLAG_GRAPHED			(1 << 19)
    62 #define ENTFLAG_GRENADE			(1 << 20)
    63 #define ENTFLAG_STEPMOVEMENT	(1 << 21)
    64 #define ENTFLAG_DONTTOUCH		(1 << 22)
    65 #define ENTFLAG_BASEVELOCITY	(1 << 23)
    66 #define ENTFLAG_WORLDBRUSH		(1 << 24)
    67 #define ENTFLAG_OBJECT			(1 << 25)
    68 #define ENTFLAG_KILLME			(1 << 26)
    69 #define ENTFLAG_ONFIRE			(1 << 27)
    70 #define ENTFLAG_DISSOLVING		(1 << 28)
    71 #define ENTFLAG_TRANSRAGDOLL	(1 << 29)
    72 #define ENTFLAG_UNBLOCKABLE_BY_PLAYER	(1 << 30)
    73 #define ENTFLAG_FREEZING		(1 << 31)
    74 #define ENTFLAG_EP2V_UNKNOWN1	(1 << 31)
    75 
    76 // Not defined in the sdk as we have no clue what it is
    77 #define FL_EP2V_UNKNOWN			(1 << 2)
    78 
    79 enum PropType
    80 {
    81 	Prop_Send = 0,
    82 	Prop_Data
    83 };
    84 
    85 enum PropFieldType
    86 {
    87 	PropField_Unsupported,		/**< The type is unsupported. */
    88 	PropField_Integer,			/**< Valid for SendProp and Data fields */
    89 	PropField_Float,			/**< Valid for SendProp and Data fields */
    90 	PropField_Entity,			/**< Valid for Data fields only (SendProp shows as int) */
    91 	PropField_Vector,			/**< Valid for SendProp and Data fields */
    92 	PropField_String,			/**< Valid for SendProp and Data fields */
    93 	PropField_String_T,			/**< Valid for Data fields.  Read only! */
    94 };
    95 
    96 inline edict_t *BaseEntityToEdict(CBaseEntity *pEntity)
    97 {
    98 	IServerUnknown *pUnk = (IServerUnknown *)pEntity;
    99 	IServerNetworkable *pNet = pUnk->GetNetworkable();
   100 
   101 	if (!pNet)
   102 	{
   103 		return NULL;
   104 	}
   105 
   106 	return pNet->GetEdict();
   107 }
   108 
   109 inline edict_t *GetEdict(cell_t num)
   110 {
   111 	edict_t *pEdict;
   112 	if (!IndexToAThings(num, NULL, &pEdict))
   113 	{
   114 		return NULL;
   115 	}
   116 	
   117 	return pEdict;
   118 }
   119 
   120 inline CBaseEntity *GetEntity(cell_t num)
   121 {
   122 	CBaseEntity *pEntity;
   123 	if (!IndexToAThings(num, &pEntity, NULL))
   124 	{
   125 		return NULL;
   126 	}
   127 
   128 	return pEntity;
   129 }
   130 
   131 /* Given an entity reference or index, fill out a CBaseEntity and/or edict.
   132    If lookup is successful, returns true and writes back the two parameters.
   133    If lookup fails, returns false and doesn't touch the params.  */
   134 bool IndexToAThings(cell_t num, CBaseEntity **pEntData, edict_t **pEdictData)
   135 {
   136 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(num);
   137 
   138 	if (!pEntity)
   139 	{
   140 		return false;
   141 	}
   142 
   143 	int index = g_HL2.ReferenceToIndex(num);
   144 	if (index > 0 && index <= g_Players.GetMaxClients())
   145 	{
   146 		CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
   147 		if (!pPlayer || !pPlayer->IsConnected())
   148 		{
   149 			return false;
   150 		}
   151 	}
   152 
   153 	if (pEntData)
   154 	{
   155 		*pEntData = pEntity;
   156 	}
   157 
   158 	if (pEdictData)
   159 	{
   160 		edict_t *pEdict = ::BaseEntityToEdict(pEntity);
   161 		if (!pEdict || pEdict->IsFree())
   162 		{
   163 			pEdict = NULL;
   164 		}
   165 
   166 		*pEdictData = pEdict;
   167 	}
   168 
   169 	return true;
   170 }
   171 /*
   172   <predcrab> AThings is like guaranteed r+ by dvander though
   173   <predcrab> he wont even read the rest of the patch to nit stuff
   174   <Fyren> Like I wasn't going to r? you anyway!
   175   <predcrab> he still sees!
   176   <predcrab> he's everywhere
   177 */
   178 
   179 class VEmptyClass {};
   180 datamap_t *VGetDataDescMap(CBaseEntity *pThisPtr, int offset)
   181 {
   182 	void **this_ptr = *reinterpret_cast<void ***>(&pThisPtr);
   183 	void **vtable = *reinterpret_cast<void ***>(pThisPtr);
   184 	void *vfunc = vtable[offset];
   185 
   186 	union
   187 	{
   188 		datamap_t *(VEmptyClass::*mfpnew)();
   189 #ifndef PLATFORM_POSIX
   190 		void *addr;
   191 	} u;
   192 	u.addr = vfunc;
   193 #else
   194 		struct  
   195 		{
   196 			void *addr;
   197 			intptr_t adjustor;
   198 		} s;
   199 	} u;
   200 	u.s.addr = vfunc;
   201 	u.s.adjustor = 0;
   202 #endif
   203 
   204 	return (datamap_t *)(reinterpret_cast<VEmptyClass *>(this_ptr)->*u.mfpnew)();
   205 }
   206 
   207 inline datamap_t *CBaseEntity_GetDataDescMap(CBaseEntity *pEntity)
   208 {
   209 	int offset;
   210 
   211 	if (!g_pGameConf->GetOffset("GetDataDescMap", &offset) || !offset)
   212 	{
   213 		return NULL;
   214 	}
   215 
   216 	return VGetDataDescMap(pEntity, offset);
   217 }
   218 
   219 /* :TODO: Move this! */
   220 datamap_t *CHalfLife2::GetDataMap(CBaseEntity *pEntity)
   221 {
   222 	return CBaseEntity_GetDataDescMap(pEntity);
   223 }
   224 
   225 static cell_t GetMaxEntities(IPluginContext *pContext, const cell_t *params)
   226 {
   227 	return gpGlobals->maxEntities;
   228 }
   229 
   230 static cell_t CreateEdict(IPluginContext *pContext, const cell_t *params)
   231 {
   232 	edict_t *pEdict = engine->CreateEdict();
   233 
   234 	if (!pEdict)
   235 	{
   236 		return 0;
   237 	}
   238 
   239 	return IndexOfEdict(pEdict);
   240 }
   241 
   242 static cell_t RemoveEdict(IPluginContext *pContext, const cell_t *params)
   243 {
   244 	edict_t *pEdict = GetEdict(params[1]);
   245 
   246 	if (!pEdict)
   247 	{
   248 		return pContext->ThrowNativeError("Edict %d (%d) is not a valid edict", g_HL2.ReferenceToIndex(params[1]), params[1]);
   249 	}
   250 
   251 	engine->RemoveEdict(pEdict);
   252 
   253 	return 1;
   254 }
   255 
   256 static cell_t IsValidEdict(IPluginContext *pContext, const cell_t *params)
   257 {
   258 	edict_t *pEdict = GetEdict(params[1]);
   259 
   260 	if (!pEdict)
   261 	{
   262 		return 0;
   263 	}
   264 
   265 	/* Shouldn't be necessary... not sure though */
   266 	return pEdict->IsFree() ? 0 : 1;
   267 }
   268 
   269 static cell_t IsValidEntity(IPluginContext *pContext, const cell_t *params)
   270 {
   271 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
   272 
   273 	return (pEntity != NULL) ? 1 : 0;
   274 }
   275 
   276 static cell_t IsEntNetworkable(IPluginContext *pContext, const cell_t *params)
   277 {
   278 	edict_t *pEdict = GetEdict(params[1]);
   279 
   280 	if (!pEdict)
   281 	{
   282 		return 0;
   283 	}
   284 
   285 	return (pEdict->GetNetworkable() != NULL) ? 1 : 0;
   286 }
   287 
   288 static cell_t GetEntityCount(IPluginContext *pContext, const cell_t *params)
   289 {
   290 	return engine->GetEntityCount();
   291 }
   292 
   293 static cell_t GetEdictFlags(IPluginContext *pContext, const cell_t *params)
   294 {
   295 	edict_t *pEdict = GetEdict(params[1]);
   296 
   297 	if (!pEdict)
   298 	{
   299 		return pContext->ThrowNativeError("Invalid edict (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
   300 	}
   301 
   302 	return pEdict->m_fStateFlags;
   303 }
   304 
   305 static cell_t SetEdictFlags(IPluginContext *pContext, const cell_t *params)
   306 {
   307 	edict_t *pEdict = GetEdict(params[1]);
   308 
   309 	if (!pEdict)
   310 	{
   311 		return pContext->ThrowNativeError("Invalid edict (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
   312 	}
   313 
   314 	pEdict->m_fStateFlags = params[2];
   315 
   316 	return 1;
   317 }
   318 
   319 static cell_t GetEdictClassname(IPluginContext *pContext, const cell_t *params)
   320 {
   321 	edict_t *pEdict = GetEdict(params[1]);
   322 
   323 	if (!pEdict)
   324 	{
   325 		return pContext->ThrowNativeError("Invalid edict (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
   326 	}
   327 
   328 	const char *cls = g_HL2.GetEntityClassname(pEdict);
   329 
   330 	if (!cls || cls[0] == '\0')
   331 	{
   332 		return 0;
   333 	}
   334 
   335 	pContext->StringToLocal(params[2], params[3], cls);
   336 
   337 	return 1;
   338 }
   339 
   340 static cell_t GetEntityNetClass(IPluginContext *pContext, const cell_t *params)
   341 {
   342 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
   343 
   344 	IServerUnknown *pUnk = (IServerUnknown *)pEntity;
   345 
   346 	if (!pEntity)
   347 	{
   348 		return pContext->ThrowNativeError("Invalid entity (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
   349 	}
   350 
   351 	IServerNetworkable *pNet = pUnk->GetNetworkable();
   352 	if (!pNet)
   353 	{
   354 		return 0;
   355 	}
   356 
   357 	ServerClass *pClass = pNet->GetServerClass();
   358 
   359 	pContext->StringToLocal(params[2], params[3], pClass->GetName());
   360 
   361 	return 1;
   362 }
   363 
   364 static cell_t GetEntData(IPluginContext *pContext, const cell_t *params)
   365 {
   366 	CBaseEntity *pEntity = GetEntity(params[1]);
   367 
   368 	if (!pEntity)
   369 	{
   370 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   371 	}
   372 
   373 	int offset = params[2];
   374 	if (offset <= 0 || offset > 32768)
   375 	{
   376 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   377 	}
   378 
   379 	switch (params[3])
   380 	{
   381 	case 4:
   382 		return *(int *)((uint8_t *)pEntity + offset);
   383 	case 2:
   384 		return *(short *)((uint8_t *)pEntity + offset);
   385 	case 1:
   386 		return *((uint8_t *)pEntity + offset);
   387 	default:
   388 		return pContext->ThrowNativeError("Integer size %d is invalid", params[3]);
   389 	}
   390 }
   391 
   392 static cell_t SetEntData(IPluginContext *pContext, const cell_t *params)
   393 {
   394 	CBaseEntity *pEntity;
   395 	edict_t *pEdict;
   396 
   397 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   398 	{
   399 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   400 	}
   401 
   402 	int offset = params[2];
   403 	if (offset <= 0 || offset > 32768)
   404 	{
   405 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   406 	}
   407 
   408 	if (params[5] && (pEdict != NULL))
   409 	{
   410 		g_HL2.SetEdictStateChanged(pEdict, offset);
   411 	}
   412 
   413 	switch (params[4])
   414 	{
   415 	case 4:
   416 		{
   417 			*(int *)((uint8_t *)pEntity + offset) = params[3];
   418 			break;
   419 		}
   420 	case 2:
   421 		{
   422 			*(short *)((uint8_t *)pEntity + offset) = params[3];
   423 			break;
   424 		}
   425 	case 1:
   426 		{
   427 			*((uint8_t *)pEntity + offset) = params[3];
   428 			break;
   429 		}
   430 	default:
   431 		return pContext->ThrowNativeError("Integer size %d is invalid", params[4]);
   432 	}
   433 
   434 	return 1;
   435 }
   436 
   437 static cell_t GetEntDataFloat(IPluginContext *pContext, const cell_t *params)
   438 {
   439 	CBaseEntity *pEntity = GetEntity(params[1]);
   440 
   441 	if (!pEntity)
   442 	{
   443 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   444 	}
   445 
   446 	int offset = params[2];
   447 	if (offset <= 0 || offset > 32768)
   448 	{
   449 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   450 	}
   451 
   452 	float f = *(float *)((uint8_t *)pEntity + offset);
   453 
   454 	return sp_ftoc(f);
   455 }
   456 
   457 static cell_t SetEntDataFloat(IPluginContext *pContext, const cell_t *params)
   458 {
   459 	CBaseEntity *pEntity;
   460 	edict_t *pEdict;
   461 
   462 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   463 	{
   464 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   465 	}
   466 
   467 	int offset = params[2];
   468 	if (offset <= 0 || offset > 32768)
   469 	{
   470 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   471 	}
   472 
   473 	*(float *)((uint8_t *)pEntity + offset) = sp_ctof(params[3]);
   474 
   475 	if (params[4] && (pEdict != NULL))
   476 	{
   477 		g_HL2.SetEdictStateChanged(pEdict, offset);
   478 	}
   479 
   480 	return 1;
   481 }
   482 
   483 static cell_t GetEntDataVector(IPluginContext *pContext, const cell_t *params)
   484 {
   485 	CBaseEntity *pEntity = GetEntity(params[1]);
   486 
   487 	if (!pEntity)
   488 	{
   489 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   490 	}
   491 
   492 	int offset = params[2];
   493 	if (offset <= 0 || offset > 32768)
   494 	{
   495 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   496 	}
   497 
   498 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
   499 
   500 	cell_t *vec;
   501 	pContext->LocalToPhysAddr(params[3], &vec);
   502 
   503 	vec[0] = sp_ftoc(v->x);
   504 	vec[1] = sp_ftoc(v->y);
   505 	vec[2] = sp_ftoc(v->z);
   506 
   507 	return 1;
   508 }
   509 
   510 static cell_t SetEntDataVector(IPluginContext *pContext, const cell_t *params)
   511 {
   512 	CBaseEntity *pEntity;
   513 	edict_t *pEdict;
   514 
   515 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   516 	{
   517 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   518 	}
   519 
   520 	int offset = params[2];
   521 	if (offset <= 0 || offset > 32768)
   522 	{
   523 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   524 	}
   525 
   526 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
   527 
   528 	cell_t *vec;
   529 	pContext->LocalToPhysAddr(params[3], &vec);
   530 
   531 	v->x = sp_ctof(vec[0]);
   532 	v->y = sp_ctof(vec[1]);
   533 	v->z = sp_ctof(vec[2]);
   534 
   535 	if (params[4] && (pEdict != NULL))
   536 	{
   537 		g_HL2.SetEdictStateChanged(pEdict, offset);
   538 	}
   539 
   540 	return 1;
   541 }
   542 
   543 /* THIS GUY IS DEPRECATED. */
   544 static cell_t GetEntDataEnt(IPluginContext *pContext, const cell_t *params)
   545 {
   546 	CBaseEntity *pEntity = GetEntity(params[1]);
   547 
   548 	if (!pEntity)
   549 	{
   550 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   551 	}
   552 
   553 	int offset = params[2];
   554 	if (offset <= 0 || offset > 32768)
   555 	{
   556 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   557 	}
   558 
   559 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
   560 
   561 	if (!hndl.IsValid())
   562 	{
   563 		return 0;
   564 	}
   565 
   566 	int ref = g_HL2.IndexToReference(hndl.GetEntryIndex());
   567 	return g_HL2.ReferenceToBCompatRef(ref);
   568 }
   569 
   570 int CheckBaseHandle(CBaseHandle &hndl)
   571 {
   572 	if (!hndl.IsValid())
   573 	{
   574 		return -1;
   575 	}
   576 
   577 	int index = hndl.GetEntryIndex();
   578 
   579 	edict_t *pStoredEdict;
   580 	CBaseEntity *pStoredEntity;
   581 
   582 	if (!IndexToAThings(index, &pStoredEntity, &pStoredEdict))
   583 	{
   584 		return -1;
   585 	}
   586 
   587 	if (pStoredEdict == NULL || pStoredEntity == NULL)
   588 	{
   589 		return -1;
   590 	}
   591 
   592 	IServerEntity *pSE = pStoredEdict->GetIServerEntity();
   593 
   594 	if (pSE == NULL)
   595 	{
   596 		return -1;
   597 	}
   598 
   599 	if (pSE->GetRefEHandle() != hndl)
   600 	{
   601 		return -1;
   602 	}
   603 
   604 	return index;
   605 }
   606 
   607 static cell_t GetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
   608 {
   609 	CBaseEntity *pEntity = GetEntity(params[1]);
   610 
   611 	if (pEntity == NULL)
   612 	{
   613 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   614 	}
   615 
   616 	int offset = params[2];
   617 	if (offset <= 0 || offset > 32768)
   618 	{
   619 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   620 	}
   621 
   622 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
   623 	CBaseEntity *pHandleEntity = g_HL2.ReferenceToEntity(hndl.GetEntryIndex());
   624 
   625 	if (!pHandleEntity || hndl != reinterpret_cast<IHandleEntity *>(pHandleEntity)->GetRefEHandle())
   626 		return -1;
   627 
   628 	return g_HL2.EntityToBCompatRef(pHandleEntity);
   629 }
   630 
   631 /* THIS GUY IS DEPRECATED. */
   632 static cell_t SetEntDataEnt(IPluginContext *pContext, const cell_t *params)
   633 {
   634 	CBaseEntity *pEntity;
   635 	edict_t *pEdict;
   636 
   637 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   638 	{
   639 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   640 	}
   641 
   642 	int offset = params[2];
   643 	if (offset <= 0 || offset > 32768)
   644 	{
   645 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   646 	}
   647 
   648 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
   649 
   650 	if (params[3] == 0 || (unsigned)params[3] == INVALID_EHANDLE_INDEX)
   651 	{
   652 		hndl.Set(NULL);
   653 	} else {
   654 		CBaseEntity *pOther = GetEntity(params[3]);
   655 
   656 		if (!pOther)
   657 		{
   658 			return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[3]), params[3]);
   659 		}
   660 
   661 		IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
   662 		hndl.Set(pHandleEnt);
   663 	}
   664 
   665 	if (params[4] && (pEdict != NULL))
   666 	{
   667 		g_HL2.SetEdictStateChanged(pEdict, offset);
   668 	}
   669 
   670 	return 1;
   671 }
   672 
   673 static cell_t SetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
   674 {
   675 	CBaseEntity *pEntity;
   676 	edict_t *pEdict;
   677 
   678 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   679 	{
   680 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   681 	}
   682 
   683 	int offset = params[2];
   684 	if (offset <= 0 || offset > 32768)
   685 	{
   686 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   687 	}
   688 
   689 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
   690 
   691 	if ((unsigned)params[3] == INVALID_EHANDLE_INDEX)
   692 	{
   693 		hndl.Set(NULL);
   694 	}
   695 	else
   696 	{
   697 		CBaseEntity *pOther = GetEntity(params[3]);
   698 
   699 		if (!pOther)
   700 		{
   701 			return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[3]), params[3]);
   702 		}
   703 
   704 		IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
   705 		hndl.Set(pHandleEnt);
   706 	}
   707 
   708 	if (params[4] && (pEdict != NULL))
   709 	{
   710 		g_HL2.SetEdictStateChanged(pEdict, offset);
   711 	}
   712 
   713 	return 1;
   714 }
   715 
   716 static cell_t ChangeEdictState(IPluginContext *pContext, const cell_t *params)
   717 {
   718 	edict_t *pEdict = GetEdict(params[1]);
   719 
   720 	if (!pEdict)
   721 	{
   722 		return pContext->ThrowNativeError("Edict %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   723 	}
   724 
   725 	g_HL2.SetEdictStateChanged(pEdict, params[2]);
   726 
   727 	return 1;
   728 }
   729 
   730 static cell_t FindSendPropOffs(IPluginContext *pContext, const cell_t *params)
   731 {
   732 	char *cls, *prop;
   733 	pContext->LocalToString(params[1], &cls);
   734 	pContext->LocalToString(params[2], &prop);
   735 
   736 	SendProp *pSend = g_HL2.FindInSendTable(cls, prop);
   737 
   738 	if (!pSend)
   739 	{
   740 		return -1;
   741 	}
   742 
   743 	return pSend->GetOffset();
   744 }
   745 
   746 static cell_t FindSendPropInfo(IPluginContext *pContext, const cell_t *params)
   747 {
   748 	char *cls, *prop;
   749 	sm_sendprop_info_t info;
   750 	cell_t *pType, *pBits, *pLocal;
   751 
   752 	pContext->LocalToString(params[1], &cls);
   753 	pContext->LocalToString(params[2], &prop);
   754 
   755 	if (!g_HL2.FindSendPropInfo(cls, prop, &info))
   756 	{
   757 		return -1;
   758 	}
   759 
   760 	pContext->LocalToPhysAddr(params[3], &pType);
   761 	pContext->LocalToPhysAddr(params[4], &pBits);
   762 	pContext->LocalToPhysAddr(params[5], &pLocal);
   763 
   764 	switch (info.prop->GetType())
   765 	{
   766 	case DPT_Int:
   767 		{
   768 			*pType = PropField_Integer;
   769 			break;
   770 		}
   771 	case DPT_Float:
   772 		{
   773 			*pType = PropField_Float;
   774 			break;
   775 		}
   776 	case DPT_String:
   777 		{
   778 			*pType = PropField_String;
   779 			break;
   780 		}
   781 	case DPT_Vector:
   782 		{
   783 			*pType = PropField_Vector;
   784 			break;
   785 		}
   786 	default:
   787 		{
   788 			*pType = PropField_Unsupported;
   789 			break;
   790 		}
   791 	}
   792 
   793 	*pBits = info.prop->m_nBits;
   794 	*pLocal = info.prop->GetOffset();
   795 
   796 	return info.actual_offset;
   797 }
   798 
   799 static cell_t FindDataMapOffs(IPluginContext *pContext, const cell_t *params)
   800 {
   801 	CBaseEntity *pEntity;
   802 	datamap_t *pMap;
   803 	typedescription_t *td;
   804 	char *offset;
   805 	pEntity = GetEntity(params[1]);
   806 
   807 	if (!pEntity)
   808 	{
   809 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   810 	}
   811 
   812 	if ((pMap=CBaseEntity_GetDataDescMap(pEntity)) == NULL)
   813 	{
   814 		return pContext->ThrowNativeError("Unable to retrieve GetDataDescMap offset");
   815 	}
   816 
   817 	pContext->LocalToString(params[2], &offset);
   818 	bool isNested = false;
   819 	if ((td=g_HL2.FindInDataMap(pMap, offset, &isNested)) == NULL)
   820 	{
   821 		if (isNested)
   822 			return pContext->ThrowNativeError("Property \"%s\" is not safe to access for entity %d", offset, params[1]);
   823 		else
   824 			return -1;
   825 	}
   826 
   827 	if (params[0] == 4)
   828 	{
   829 		cell_t *pType, *pSize;
   830 
   831 		pContext->LocalToPhysAddr(params[3], &pType);
   832 		pContext->LocalToPhysAddr(params[4], &pSize);
   833 
   834 		switch (td->fieldType)
   835 		{
   836 		case FIELD_TICK:
   837 		case FIELD_MODELINDEX:
   838 		case FIELD_MATERIALINDEX:
   839 		case FIELD_INTEGER:
   840 		case FIELD_COLOR32:
   841 			{
   842 				*pType = PropField_Integer;
   843 				*pSize = 32;
   844 				break;
   845 			}
   846 		case FIELD_VECTOR:
   847 		case FIELD_POSITION_VECTOR:
   848 			{
   849 				*pType = PropField_Vector;
   850 				*pSize = 12;
   851 				break;
   852 			}
   853 		case FIELD_SHORT:
   854 			{
   855 				*pType = PropField_Integer;
   856 				*pSize = 16;
   857 				break;
   858 			}
   859 		case FIELD_BOOLEAN:
   860 			{
   861 				*pType = PropField_Integer;
   862 				*pSize = 1;
   863 				break;
   864 			}
   865 		case FIELD_CHARACTER:
   866 			{
   867 				if (td->fieldSize == 1)
   868 				{
   869 					*pType = PropField_Integer;
   870 					*pSize = 8;
   871 				}
   872 				else
   873 				{
   874 					*pType = PropField_String;
   875 					*pSize = 8 * td->fieldSize;
   876 				}
   877 				break;
   878 			}
   879 		case FIELD_MODELNAME:
   880 		case FIELD_SOUNDNAME:
   881 		case FIELD_STRING:
   882 			{
   883 				*pSize = sizeof(string_t);
   884 				*pType = PropField_String_T;
   885 				break;
   886 			}
   887 		case FIELD_FLOAT:
   888 		case FIELD_TIME:
   889 			{
   890 				*pType = PropField_Float;
   891 				*pSize = 32;
   892 				break;
   893 			}
   894 		case FIELD_EHANDLE:
   895 			{
   896 				*pType = PropField_Entity;
   897 				*pSize = 32;
   898 				break;
   899 			}
   900 		default:
   901 			{
   902 				*pType = PropField_Unsupported;
   903 				*pSize = 0;
   904 			}
   905 		}
   906 	}
   907 
   908 	return GetTypeDescOffs(td);
   909 }
   910 
   911 static cell_t GetEntDataString(IPluginContext *pContext, const cell_t *params)
   912 {
   913 	CBaseEntity *pEntity = GetEntity(params[1]);
   914 
   915 	if (!pEntity)
   916 	{
   917 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   918 	}
   919 
   920 	int offset = params[2];
   921 	if (offset < 0 || offset > 32768)
   922 	{
   923 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   924 	}
   925 
   926 	size_t len;
   927 	char *src = (char *)((uint8_t *)pEntity + offset);
   928 	pContext->StringToLocalUTF8(params[3], params[4], src, &len);
   929 
   930 	return len;
   931 }
   932 
   933 static cell_t SetEntDataString(IPluginContext *pContext, const cell_t *params)
   934 {
   935 	CBaseEntity *pEntity;
   936 	edict_t *pEdict;
   937 
   938 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   939 	{
   940 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   941 	}
   942 
   943 	int offset = params[2];
   944 	if (offset < 0 || offset > 32768)
   945 	{
   946 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   947 	}
   948 
   949 	char *src;
   950 	char *dest = (char *)((uint8_t *)pEntity + offset);
   951 
   952 	pContext->LocalToString(params[3], &src);
   953 	size_t len = strncopy(dest, src, params[4]);
   954 
   955 	if (params[5] && (pEdict != NULL))
   956 	{
   957 		g_HL2.SetEdictStateChanged(pEdict, offset);
   958 	}
   959 
   960 	return len;
   961 }
   962 
   963 #define FIND_PROP_DATA(td) \
   964 	datamap_t *pMap; \
   965 	if ((pMap = CBaseEntity_GetDataDescMap(pEntity)) == NULL) \
   966 	{ \
   967 		return pContext->ThrowNativeError("Could not retrieve datamap"); \
   968 	} \
   969 	bool isNested = false; \
   970 	if ((td = g_HL2.FindInDataMap(pMap, prop, &isNested)) == NULL) \
   971 	{ \
   972 		const char *class_name = g_HL2.GetEntityClassname(pEntity); \
   973 		if (isNested) \
   974 		{ \
   975 			return pContext->ThrowNativeError("Property \"%s\" not safe to access (entity %d/%s)", \
   976 				prop, \
   977 				params[1], \
   978 				((class_name) ? class_name : "")); \
   979 		} else { \
   980 			return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)", \
   981 				prop, \
   982 				params[1], \
   983 				((class_name) ? class_name : "")); \
   984 		} \
   985 	}
   986 
   987 #define CHECK_SET_PROP_DATA_OFFSET() \
   988 	if (element < 0 || element >= td->fieldSize) \
   989 	{ \
   990 		return pContext->ThrowNativeError("Element %d is out of bounds (Prop %s has %d elements).", \
   991 			element, \
   992 			prop, \
   993 			td->fieldSize); \
   994 	} \
   995 	\
   996 	offset = GetTypeDescOffs(td) + (element * (td->fieldSizeInBytes / td->fieldSize));
   997 
   998 #define FIND_PROP_SEND(type, type_name) \
   999 	sm_sendprop_info_t info;\
  1000 	SendProp *pProp; \
  1001 	IServerUnknown *pUnk = (IServerUnknown *)pEntity; \
  1002 	IServerNetworkable *pNet = pUnk->GetNetworkable(); \
  1003 	if (!pNet) \
  1004 	{ \
  1005 		return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]); \
  1006 	} \
  1007 	if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, &info)) \
  1008 	{ \
  1009 		const char *class_name = g_HL2.GetEntityClassname(pEntity); \
  1010 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)", \
  1011 			prop, \
  1012 			params[1], \
  1013 			((class_name) ? class_name : "")); \
  1014 	} \
  1015 	\
  1016 	offset = info.actual_offset; \
  1017 	pProp = info.prop; \
  1018 	bit_count = pProp->m_nBits; \
  1019 	\
  1020 	switch (pProp->GetType()) \
  1021 	{ \
  1022 	case type: \
  1023 		{ \
  1024 			if (element != 0) \
  1025 			{ \
  1026 				return pContext->ThrowNativeError("SendProp %s is not an array. Element %d is invalid.", \
  1027 					prop, \
  1028 					element); \
  1029 			} \
  1030 			break; \
  1031 		} \
  1032 	case DPT_DataTable: \
  1033 		{ \
  1034 			FIND_PROP_SEND_IN_SENDTABLE(info, pProp, element, type, type_name); \
  1035 			\
  1036 			offset += pProp->GetOffset(); \
  1037 			bit_count = pProp->m_nBits; \
  1038 			break; \
  1039 		} \
  1040 	default: \
  1041 		{ \
  1042 			return pContext->ThrowNativeError("SendProp %s type is not " type_name " (%d != %d)", \
  1043 				prop, \
  1044 				pProp->GetType(), \
  1045 				type); \
  1046 		} \
  1047 	} \
  1048 
  1049 #define FIND_PROP_SEND_IN_SENDTABLE(info, pProp, element, type, type_name) \
  1050 	SendTable *pTable = pProp->GetDataTable(); \
  1051 	if (!pTable) \
  1052 	{ \
  1053 		return pContext->ThrowNativeError("Error looking up DataTable for prop %s", \
  1054 			prop); \
  1055 	} \
  1056 	\
  1057 	int elementCount = pTable->GetNumProps(); \
  1058 	if (element < 0 || element >= elementCount) \
  1059 	{ \
  1060 		return pContext->ThrowNativeError("Element %d is out of bounds (Prop %s has %d elements).", \
  1061 			element, \
  1062 			prop, \
  1063 			elementCount); \
  1064 	} \
  1065 	\
  1066 	pProp = pTable->GetProp(element); \
  1067 	if (pProp->GetType() != type) \
  1068 	{ \
  1069 		return pContext->ThrowNativeError("SendProp %s type is not " type_name " ([%d,%d] != %d)", \
  1070 			prop, \
  1071 			pProp->GetType(), \
  1072 			pProp->m_nBits, \
  1073 			type); \
  1074 	}
  1075 
  1076 
  1077 inline int MatchFieldAsInteger(int field_type)
  1078 {
  1079 	switch (field_type)
  1080 	{
  1081 	case FIELD_TICK:
  1082 	case FIELD_MODELINDEX:
  1083 	case FIELD_MATERIALINDEX:
  1084 	case FIELD_INTEGER:
  1085 	case FIELD_COLOR32:
  1086 		return 32;
  1087 	case FIELD_SHORT:
  1088 		return 16;
  1089 	case FIELD_CHARACTER:
  1090 		return 8;
  1091 	case FIELD_BOOLEAN:
  1092 		return 1;
  1093 	default:
  1094 		return 0;
  1095 	}
  1096 
  1097 	return 0;
  1098 }
  1099 
  1100 static cell_t GetEntPropArraySize(IPluginContext *pContext, const cell_t *params)
  1101 {
  1102 	CBaseEntity *pEntity;
  1103 	char *prop;
  1104 	edict_t *pEdict;
  1105 
  1106 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1107 	{
  1108 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1109 	}
  1110 
  1111 	pContext->LocalToString(params[3], &prop);
  1112 
  1113 	switch (params[2])
  1114 	{
  1115 	case Prop_Data:
  1116 		{
  1117 			typedescription_t *td;
  1118 
  1119 			FIND_PROP_DATA(td);
  1120 
  1121 			return td->fieldSize;
  1122 		}
  1123 	case Prop_Send:
  1124 		{
  1125 			sm_sendprop_info_t info;
  1126 			
  1127 			IServerUnknown *pUnk = (IServerUnknown *)pEntity;
  1128 			IServerNetworkable *pNet = pUnk->GetNetworkable();
  1129 			if (!pNet)
  1130 			{
  1131 				return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1132 			}
  1133 			if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, &info))
  1134 			{
  1135 				const char *class_name = g_HL2.GetEntityClassname(pEntity);
  1136 				return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)",
  1137 					prop,
  1138 					params[1],
  1139 					((class_name) ? class_name : ""));
  1140 			}
  1141 
  1142 			if (info.prop->GetType() != DPT_DataTable)
  1143 			{
  1144 				return 0;
  1145 			}
  1146 
  1147 			SendTable *pTable = info.prop->GetDataTable();
  1148 			if (!pTable)
  1149 			{
  1150 				return pContext->ThrowNativeError("Error looking up DataTable for prop %s", prop);
  1151 			}
  1152 			
  1153 			return pTable->GetNumProps();
  1154 		}
  1155 	default:
  1156 		{
  1157 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1158 		}
  1159 	}
  1160 
  1161 	return 1;
  1162 }
  1163 
  1164 static cell_t GetEntProp(IPluginContext *pContext, const cell_t *params)
  1165 {
  1166 	CBaseEntity *pEntity;
  1167 	char *prop;
  1168 	int offset;
  1169 	edict_t *pEdict;
  1170 	int bit_count;
  1171 	bool is_unsigned = false;
  1172 
  1173 	int element = 0;
  1174 	if (params[0] >= 5)
  1175 	{
  1176 		element = params[5];
  1177 	}
  1178 
  1179 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1180 	{
  1181 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1182 	}
  1183 
  1184 	pContext->LocalToString(params[3], &prop);
  1185 
  1186 	switch (params[2])
  1187 	{
  1188 	case Prop_Data:
  1189 		{
  1190 			typedescription_t *td;
  1191 
  1192 			FIND_PROP_DATA(td);
  1193 
  1194 			if ((bit_count = MatchFieldAsInteger(td->fieldType)) == 0)
  1195 			{
  1196 				return pContext->ThrowNativeError("Data field %s is not an integer (%d)", 
  1197 					prop,
  1198 					td->fieldType);
  1199 			}
  1200 
  1201 			CHECK_SET_PROP_DATA_OFFSET();
  1202 
  1203 			break;
  1204 		}
  1205 	case Prop_Send:
  1206 		{
  1207 			FIND_PROP_SEND(DPT_Int, "integer");
  1208 			is_unsigned = ((pProp->GetFlags() & SPROP_UNSIGNED) == SPROP_UNSIGNED);
  1209 
  1210 			// This isn't in CS:S yet, but will be, doesn't hurt to add now, and will save us a build later
  1211 #if SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_ORANGEBOXVALVE
  1212 			if (pProp->GetFlags() & SPROP_VARINT)
  1213 			{
  1214 				bit_count = sizeof(int) * 8;
  1215 			}
  1216 #endif
  1217 			break;
  1218 		}
  1219 	default:
  1220 		{
  1221 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1222 		}
  1223 	}
  1224 
  1225 	if (bit_count < 1)
  1226 	{
  1227 		bit_count = params[4] * 8;
  1228 	}
  1229 
  1230 	if (bit_count >= 17)
  1231 	{
  1232 		return *(int32_t *)((uint8_t *)pEntity + offset);
  1233 	}
  1234 	else if (bit_count >= 9)
  1235 	{
  1236 		if (is_unsigned)
  1237 		{
  1238 			return *(uint16_t *)((uint8_t *)pEntity + offset);
  1239 		}
  1240 		else
  1241 		{
  1242 			return *(int16_t *)((uint8_t *)pEntity + offset);
  1243 		}
  1244 	}
  1245 	else if (bit_count >= 2)
  1246 	{
  1247 		if (is_unsigned)
  1248 		{
  1249 			return *(uint8_t *)((uint8_t *)pEntity + offset);
  1250 		}
  1251 		else
  1252 		{
  1253 			return *(int8_t *)((uint8_t *)pEntity + offset);
  1254 		}
  1255 	}
  1256 	else
  1257 	{
  1258 		return *(bool *)((uint8_t *)pEntity + offset) ? 1 : 0;
  1259 	}
  1260 
  1261 	return 0;
  1262 }
  1263 
  1264 static cell_t SetEntProp(IPluginContext *pContext, const cell_t *params)
  1265 {
  1266 	CBaseEntity *pEntity;
  1267 	char *prop;
  1268 	int offset;
  1269 	edict_t *pEdict;
  1270 	int bit_count;
  1271 
  1272 	int element = 0;
  1273 	if (params[0] >= 6)
  1274 	{
  1275 		element = params[6];
  1276 	}
  1277 
  1278 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1279 	{
  1280 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1281 	}
  1282 
  1283 	pContext->LocalToString(params[3], &prop);
  1284 
  1285 	switch (params[2])
  1286 	{
  1287 	case Prop_Data:
  1288 		{
  1289 			typedescription_t *td;
  1290 
  1291 			FIND_PROP_DATA(td);
  1292 
  1293 			if ((bit_count = MatchFieldAsInteger(td->fieldType)) == 0)
  1294 			{
  1295 				return pContext->ThrowNativeError("Data field %s is not an integer (%d)", 
  1296 					prop,
  1297 					td->fieldType);
  1298 			}
  1299 
  1300 			CHECK_SET_PROP_DATA_OFFSET();
  1301 
  1302 			break;
  1303 		}
  1304 	case Prop_Send:
  1305 		{
  1306 			FIND_PROP_SEND(DPT_Int, "integer");
  1307 
  1308 			// This isn't in CS:S yet, but will be, doesn't hurt to add now, and will save us a build later
  1309 #if SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_ORANGEBOXVALVE
  1310 			if (pProp->GetFlags() & SPROP_VARINT)
  1311 			{
  1312 				bit_count = sizeof(int) * 8;
  1313 			}
  1314 #endif
  1315 			break;
  1316 		}
  1317 	default:
  1318 		{
  1319 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1320 		}
  1321 	}
  1322 
  1323 	if (bit_count < 1)
  1324 	{
  1325 		bit_count = params[5] * 8;
  1326 	}
  1327 
  1328 	if (bit_count >= 17)
  1329 	{
  1330 		*(int32_t *)((uint8_t *)pEntity + offset) = params[4];
  1331 	}
  1332 	else if (bit_count >= 9)
  1333 	{
  1334 		*(int16_t *)((uint8_t *)pEntity + offset) = (int16_t)params[4];
  1335 	}
  1336 	else if (bit_count >= 2)
  1337 	{
  1338 		*(int8_t *)((uint8_t *)pEntity + offset) = (int8_t)params[4];
  1339 	}
  1340 	else
  1341 	{
  1342 		*(bool *)((uint8_t *)pEntity + offset) = params[4] ? true : false;
  1343 	}
  1344 	
  1345 	if (params[2] == Prop_Send && (pEdict != NULL))
  1346 	{
  1347 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1348 	}
  1349 
  1350 	return 0;
  1351 }
  1352 
  1353 static cell_t GetEntPropFloat(IPluginContext *pContext, const cell_t *params)
  1354 {
  1355 	CBaseEntity *pEntity;
  1356 	char *prop;
  1357 	int offset;
  1358 	int bit_count;
  1359 	edict_t *pEdict;
  1360 
  1361 	int element = 0;
  1362 	if (params[0] >= 4)
  1363 	{
  1364 		element = params[4];
  1365 	}
  1366 
  1367 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1368 	{
  1369 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1370 	}
  1371 
  1372 	pContext->LocalToString(params[3], &prop);
  1373 
  1374 	switch (params[2])
  1375 	{
  1376 	case Prop_Data:
  1377 		{
  1378 			typedescription_t *td;
  1379 
  1380 			FIND_PROP_DATA(td);
  1381 
  1382 			if (td->fieldType != FIELD_FLOAT
  1383 				&& td->fieldType != FIELD_TIME)
  1384 			{
  1385 				return pContext->ThrowNativeError("Data field %s is not a float (%d != [%d,%d])", 
  1386 					prop,
  1387 					td->fieldType,
  1388 					FIELD_FLOAT,
  1389 					FIELD_TIME);
  1390 			}
  1391 
  1392 			CHECK_SET_PROP_DATA_OFFSET();
  1393 
  1394 			break;
  1395 		}
  1396 	case Prop_Send:
  1397 		{
  1398 			FIND_PROP_SEND(DPT_Float, "float");
  1399 			break;
  1400 		}
  1401 	default:
  1402 		{
  1403 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1404 		}
  1405 	}
  1406 
  1407 	float val = *(float *)((uint8_t *)pEntity + offset);
  1408 
  1409 	return sp_ftoc(val);
  1410 }
  1411 
  1412 static cell_t SetEntPropFloat(IPluginContext *pContext, const cell_t *params)
  1413 {
  1414 	CBaseEntity *pEntity;
  1415 	char *prop;
  1416 	int offset;
  1417 	int bit_count;
  1418 	edict_t *pEdict;
  1419 
  1420 	int element = 0;
  1421 	if (params[0] >= 5)
  1422 	{
  1423 		element = params[5];
  1424 	}
  1425 
  1426 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1427 	{
  1428 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1429 	}
  1430 
  1431 	pContext->LocalToString(params[3], &prop);
  1432 
  1433 	switch (params[2])
  1434 	{
  1435 	case Prop_Data:
  1436 		{
  1437 			typedescription_t *td;
  1438 
  1439 			FIND_PROP_DATA(td);
  1440 
  1441 			if (td->fieldType != FIELD_FLOAT
  1442 				&& td->fieldType != FIELD_TIME)
  1443 			{
  1444 				return pContext->ThrowNativeError("Data field %s is not a float (%d != [%d,%d])", 
  1445 					prop,
  1446 					td->fieldType,
  1447 					FIELD_FLOAT,
  1448 					FIELD_TIME);
  1449 			}
  1450 
  1451 			CHECK_SET_PROP_DATA_OFFSET();
  1452 
  1453 			break;
  1454 		}
  1455 	case Prop_Send:
  1456 		{
  1457 			FIND_PROP_SEND(DPT_Float, "float");
  1458 			break;
  1459 		}
  1460 	default:
  1461 		{
  1462 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1463 		}
  1464 	}
  1465 
  1466 	*(float *)((uint8_t *)pEntity + offset) = sp_ctof(params[4]);
  1467 
  1468 	if (params[2] == Prop_Send && (pEdict != NULL))
  1469 	{
  1470 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1471 	}
  1472 
  1473 	return 1;
  1474 }
  1475 
  1476 static cell_t GetEntPropEnt(IPluginContext *pContext, const cell_t *params)
  1477 {
  1478 	CBaseEntity *pEntity;
  1479 	char *prop;
  1480 	int offset;
  1481 	int bit_count;
  1482 	edict_t *pEdict;
  1483 
  1484 	int element = 0;
  1485 	if (params[0] >= 4)
  1486  	{
  1487 		element = params[4];
  1488  	}
  1489 
  1490 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1491 	{
  1492 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1493 	}
  1494 
  1495 	pContext->LocalToString(params[3], &prop);
  1496 
  1497 	switch (params[2])
  1498 	{
  1499 	case Prop_Data:
  1500 		{
  1501 			typedescription_t *td;
  1502 
  1503 			FIND_PROP_DATA(td);
  1504 
  1505 			if (td->fieldType != FIELD_EHANDLE)
  1506 			{
  1507 				return pContext->ThrowNativeError("Data field %s is not an entity (%d != %d)", 
  1508 					prop,
  1509 					td->fieldType,
  1510 					FIELD_EHANDLE);
  1511 			}
  1512 
  1513 			CHECK_SET_PROP_DATA_OFFSET();
  1514 
  1515 			break;
  1516 		}
  1517 	case Prop_Send:
  1518 		{
  1519 			FIND_PROP_SEND(DPT_Int, "integer");
  1520 			break;
  1521 		}
  1522 	default:
  1523 		{
  1524 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1525 		}
  1526 	}
  1527 
  1528 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
  1529 	CBaseEntity *pHandleEntity = g_HL2.ReferenceToEntity(hndl.GetEntryIndex());
  1530 
  1531 	if (!pHandleEntity || hndl != reinterpret_cast<IHandleEntity *>(pHandleEntity)->GetRefEHandle())
  1532 		return -1;
  1533 
  1534 	return g_HL2.EntityToBCompatRef(pHandleEntity);
  1535 }
  1536 
  1537 static cell_t SetEntPropEnt(IPluginContext *pContext, const cell_t *params)
  1538 {
  1539 	CBaseEntity *pEntity;
  1540 	char *prop;
  1541 	int offset;
  1542 	int bit_count;
  1543 	edict_t *pEdict;
  1544 
  1545 	int element = 0;
  1546 	if (params[0] >= 5)
  1547  	{
  1548 		element = params[5];
  1549  	}
  1550 
  1551 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1552 	{
  1553 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1554 	}
  1555 
  1556 	pContext->LocalToString(params[3], &prop);
  1557 
  1558 	switch (params[2])
  1559 	{
  1560 	case Prop_Data:
  1561 		{
  1562 			typedescription_t *td;
  1563 
  1564 			FIND_PROP_DATA(td);
  1565 
  1566 			if (td->fieldType != FIELD_EHANDLE)
  1567 			{
  1568 				return pContext->ThrowNativeError("Data field %s is not an entity (%d != %d)", 
  1569 					prop,
  1570 					td->fieldType,
  1571 					FIELD_EHANDLE);
  1572 			}
  1573 
  1574 			CHECK_SET_PROP_DATA_OFFSET();
  1575 
  1576 			break;
  1577 		}
  1578 	case Prop_Send:
  1579 		{
  1580 			FIND_PROP_SEND(DPT_Int, "integer");
  1581 			break;
  1582 		}
  1583 	default:
  1584 		{
  1585 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1586 		}
  1587 	}
  1588 
  1589 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
  1590 
  1591 	if (params[4] == -1)
  1592 	{
  1593 		hndl.Set(NULL);
  1594 	}
  1595 	else
  1596 	{
  1597 		CBaseEntity *pOther = GetEntity(params[4]);
  1598 
  1599 		if (!pOther)
  1600 		{
  1601 			return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[4]), params[4]);
  1602 		}
  1603 
  1604 		IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
  1605 		hndl.Set(pHandleEnt);
  1606 	}
  1607 
  1608 	if (params[2] == Prop_Send && (pEdict != NULL))
  1609 	{
  1610 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1611 	}
  1612 
  1613 	return 1;
  1614 }
  1615 
  1616 static cell_t GetEntPropVector(IPluginContext *pContext, const cell_t *params)
  1617 {
  1618 	CBaseEntity *pEntity;
  1619 	char *prop;
  1620 	int offset;
  1621 	int bit_count;
  1622 	edict_t *pEdict;
  1623 
  1624 	int element = 0;
  1625 	if (params[0] >= 5)
  1626  	{
  1627 		element = params[5];
  1628  	}
  1629 	
  1630 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1631 	{
  1632 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1633 	}
  1634 
  1635 	pContext->LocalToString(params[3], &prop);
  1636 
  1637 	switch (params[2])
  1638 	{
  1639 	case Prop_Data:
  1640 		{
  1641 			typedescription_t *td;
  1642 			
  1643 			FIND_PROP_DATA(td);
  1644 
  1645 			if (td->fieldType != FIELD_VECTOR
  1646 				&& td->fieldType != FIELD_POSITION_VECTOR)
  1647 			{
  1648 				return pContext->ThrowNativeError("Data field %s is not a vector (%d != [%d,%d])", 
  1649 					prop,
  1650 					td->fieldType,
  1651 					FIELD_VECTOR,
  1652 					FIELD_POSITION_VECTOR);
  1653 			}
  1654 
  1655 			CHECK_SET_PROP_DATA_OFFSET();
  1656 
  1657 			break;
  1658 		}
  1659 	case Prop_Send:
  1660 		{
  1661 			FIND_PROP_SEND(DPT_Vector, "vector");
  1662 			break;
  1663 		}
  1664 	default:
  1665 		{
  1666 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1667 		}
  1668 	}
  1669 
  1670 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
  1671 
  1672 	cell_t *vec;
  1673 	pContext->LocalToPhysAddr(params[4], &vec);
  1674 
  1675 	vec[0] = sp_ftoc(v->x);
  1676 	vec[1] = sp_ftoc(v->y);
  1677 	vec[2] = sp_ftoc(v->z);
  1678 
  1679 	return 1;
  1680 }
  1681 
  1682 static cell_t SetEntPropVector(IPluginContext *pContext, const cell_t *params)
  1683 {
  1684 	CBaseEntity *pEntity;
  1685 	char *prop;
  1686 	int offset;
  1687 	int bit_count;
  1688 	edict_t *pEdict;
  1689 
  1690 	int element = 0;
  1691 	if (params[0] >= 5)
  1692  	{
  1693 		element = params[5];
  1694  	}
  1695 	
  1696 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1697 	{
  1698 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1699 	}
  1700 
  1701 	pContext->LocalToString(params[3], &prop);
  1702 
  1703 	switch (params[2])
  1704 	{
  1705 	case Prop_Data:
  1706 		{
  1707 			typedescription_t *td;
  1708 			
  1709 			FIND_PROP_DATA(td);
  1710 
  1711 			if (td->fieldType != FIELD_VECTOR
  1712 				&& td->fieldType != FIELD_POSITION_VECTOR)
  1713 			{
  1714 				return pContext->ThrowNativeError("Data field %s is not a vector (%d != [%d,%d])", 
  1715 					prop,
  1716 					td->fieldType,
  1717 					FIELD_VECTOR,
  1718 					FIELD_POSITION_VECTOR);
  1719 			}
  1720 
  1721 			CHECK_SET_PROP_DATA_OFFSET();
  1722 
  1723 			break;
  1724 		}
  1725 	case Prop_Send:
  1726 		{
  1727 			FIND_PROP_SEND(DPT_Vector, "vector");			
  1728 			break;
  1729 		}
  1730 	default:
  1731 		{
  1732 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1733 		}
  1734 	}
  1735 
  1736 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
  1737 
  1738 	cell_t *vec;
  1739 	pContext->LocalToPhysAddr(params[4], &vec);
  1740 
  1741 	v->x = sp_ctof(vec[0]);
  1742 	v->y = sp_ctof(vec[1]);
  1743 	v->z = sp_ctof(vec[2]);
  1744 
  1745 	if (params[2] == Prop_Send && (pEdict != NULL))
  1746 	{
  1747 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1748 	}
  1749 
  1750 	return 1;
  1751 }
  1752 
  1753 static cell_t GetEntPropString(IPluginContext *pContext, const cell_t *params)
  1754 {
  1755 	CBaseEntity *pEntity;
  1756 	char *prop;
  1757 	int offset;
  1758 	edict_t *pEdict;
  1759 	bool bIsStringIndex;
  1760 
  1761 	int element = 0;
  1762 	if (params[0] >= 6)
  1763  	{
  1764 		element = params[6];
  1765  	}
  1766 	
  1767 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1768 	{
  1769 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1770 	}
  1771 
  1772 	pContext->LocalToString(params[3], &prop);
  1773 
  1774 	bIsStringIndex = false;
  1775 
  1776 	switch (params[2])
  1777 	{
  1778 	case Prop_Data:
  1779 		{
  1780 			typedescription_t *td;
  1781 			
  1782 			FIND_PROP_DATA(td);
  1783 
  1784 			if (td->fieldType != FIELD_CHARACTER
  1785 				&& td->fieldType != FIELD_STRING
  1786 				&& td->fieldType != FIELD_MODELNAME 
  1787 				&& td->fieldType != FIELD_SOUNDNAME)
  1788 			{
  1789 				return pContext->ThrowNativeError("Data field %s is not a string (%d != %d)", 
  1790 					prop,
  1791 					td->fieldType,
  1792 					FIELD_CHARACTER);
  1793 			}
  1794 
  1795 			bIsStringIndex = (td->fieldType != FIELD_CHARACTER);
  1796 
  1797 			if (bIsStringIndex && (element < 0 || element >= td->fieldSize))
  1798 			{
  1799 				return pContext->ThrowNativeError("Element %d is out of bounds (Prop %s has %d elements).",
  1800 					element,
  1801 					prop,
  1802 					td->fieldSize);
  1803 			}
  1804 			else if (element != 0)
  1805 			{
  1806 				return pContext->ThrowNativeError("Prop %s is not an array. Element %d is invalid.",
  1807 					prop,
  1808 					element);
  1809 			}
  1810 
  1811 			offset = GetTypeDescOffs(td);
  1812 			if (bIsStringIndex)
  1813 			{
  1814 				offset += (element * (td->fieldSizeInBytes / td->fieldSize));
  1815 			}
  1816 			break;
  1817 		}
  1818 	case Prop_Send:
  1819 		{
  1820 			sm_sendprop_info_t info;
  1821 			
  1822 			IServerUnknown *pUnk = (IServerUnknown *)pEntity;
  1823 			IServerNetworkable *pNet = pUnk->GetNetworkable();
  1824 			if (!pNet)
  1825 			{
  1826 				return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1827 			}
  1828 			if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, &info))
  1829 			{
  1830 				const char *class_name = g_HL2.GetEntityClassname(pEntity);
  1831 				return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)",
  1832 					prop,
  1833 					params[1],
  1834 					((class_name) ? class_name : ""));
  1835 			}
  1836 
  1837 			offset = info.actual_offset;
  1838 
  1839 			if (info.prop->GetType() != DPT_String)
  1840 			{
  1841 				return pContext->ThrowNativeError("SendProp %s is not a string (%d != %d)",
  1842 					prop,
  1843 					info.prop->GetType(),
  1844 					DPT_String);
  1845 			}
  1846 			else if (element != 0)
  1847 			{
  1848 				return pContext->ThrowNativeError("SendProp %s is not an array. Element %d is invalid.",
  1849 					prop,
  1850 					element);
  1851  			}
  1852 			
  1853 			break;
  1854 		}
  1855 	default:
  1856 		{
  1857 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1858 		}
  1859 	}
  1860 
  1861 	size_t len;
  1862 	const char *src; 
  1863 	
  1864 	if (!bIsStringIndex)
  1865 	{
  1866 		src = (char *)((uint8_t *)pEntity + offset);
  1867 	}
  1868 	else
  1869 	{
  1870 		string_t idx;
  1871 
  1872 		idx = *(string_t *)((uint8_t *)pEntity + offset);
  1873 		src = (idx == NULL_STRING) ? "" : STRING(idx);
  1874 	}
  1875 
  1876 	pContext->StringToLocalUTF8(params[4], params[5], src, &len);
  1877 
  1878 	return len;
  1879 }
  1880 
  1881 static cell_t SetEntPropString(IPluginContext *pContext, const cell_t *params)
  1882 {
  1883 	CBaseEntity *pEntity;
  1884 	char *prop;
  1885 	int offset;
  1886 	int maxlen;
  1887 	edict_t *pEdict;
  1888 
  1889 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1890 	{
  1891 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1892 	}
  1893 
  1894 	switch (params[2])
  1895 	{
  1896 	case Prop_Data:
  1897 		{
  1898 			datamap_t *pMap;
  1899 			typedescription_t *td;
  1900 			if ((pMap=CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  1901 			{
  1902 				return pContext->ThrowNativeError("Unable to retrieve GetDataDescMap offset");
  1903 			}
  1904 			pContext->LocalToString(params[3], &prop);
  1905 			bool isNested = false;
  1906 			if ((td=g_HL2.FindInDataMap(pMap, prop, &isNested)) == NULL)
  1907 			{
  1908 				if (isNested)
  1909 					return pContext->ThrowNativeError("Property \"%s\" is not safe to access for entity %d", prop, params[1]);
  1910 				else
  1911 					return pContext->ThrowNativeError("Property \"%s\" not found for entity %d", prop, params[1]);
  1912 			}
  1913 			if (td->fieldType != FIELD_CHARACTER)
  1914 			{
  1915 				return pContext->ThrowNativeError("Property \"%s\" is not a valid string", prop);
  1916 			}
  1917 			offset = GetTypeDescOffs(td);
  1918 			maxlen = td->fieldSize;
  1919 			break;
  1920 		}
  1921 	case Prop_Send:
  1922 		{
  1923 			char *prop;
  1924 			IServerUnknown *pUnk = (IServerUnknown *)pEntity;
  1925 			IServerNetworkable *pNet = pUnk->GetNetworkable();
  1926 			if (!pNet)
  1927 			{
  1928 				return pContext->ThrowNativeError("The edict is not networkable");
  1929 			}
  1930 			pContext->LocalToString(params[3], &prop);
  1931 			SendProp *pSend = g_HL2.FindInSendTable(pNet->GetServerClass()->GetName(), prop);
  1932 			if (!pSend)
  1933 			{
  1934 				return pContext->ThrowNativeError("Property \"%s\" not found for entity %d", prop, params[1]);
  1935 			}
  1936 			if (pSend->GetType() != DPT_String)
  1937 			{
  1938 				return pContext->ThrowNativeError("Property \"%s\" is not a valid string", prop);
  1939 			}
  1940 			offset = pSend->GetOffset();
  1941 			maxlen = DT_MAX_STRING_BUFFERSIZE;
  1942 			break;
  1943 		}
  1944 	default:
  1945 		{
  1946 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1947 		}
  1948 	}
  1949 
  1950 	char *src;
  1951 	char *dest = (char *)((uint8_t *)pEntity + offset);
  1952 
  1953 	pContext->LocalToString(params[4], &src);
  1954 	size_t len = strncopy(dest, src, maxlen);
  1955 
  1956 	if (params[2] == Prop_Send && (pEdict != NULL))
  1957 	{
  1958 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1959 	}
  1960 
  1961 	return len;
  1962 }
  1963 
  1964 static int32_t SDKEntFlagToSMEntFlag(int flag)
  1965 {
  1966 	switch (flag)
  1967 	{
  1968 		case FL_ONGROUND:
  1969 			return ENTFLAG_ONGROUND;
  1970 		case FL_DUCKING:
  1971 			return ENTFLAG_DUCKING;
  1972 		case FL_WATERJUMP:
  1973 			return ENTFLAG_WATERJUMP;
  1974 		case FL_ONTRAIN:
  1975 			return ENTFLAG_ONTRAIN;
  1976 		case FL_INRAIN:
  1977 			return ENTFLAG_INRAIN;
  1978 		case FL_FROZEN:
  1979 			return ENTFLAG_FROZEN;
  1980 		case FL_ATCONTROLS:
  1981 			return ENTFLAG_ATCONTROLS;
  1982 		case FL_CLIENT:
  1983 			return ENTFLAG_CLIENT;
  1984 		case FL_FAKECLIENT:
  1985 			return ENTFLAG_FAKECLIENT;
  1986 		case FL_INWATER:
  1987 			return ENTFLAG_INWATER;
  1988 		case FL_FLY:
  1989 			return ENTFLAG_FLY;
  1990 		case FL_SWIM:
  1991 			return ENTFLAG_SWIM;
  1992 		case FL_CONVEYOR:
  1993 			return ENTFLAG_CONVEYOR;
  1994 		case FL_NPC:
  1995 			return ENTFLAG_NPC;
  1996 		case FL_GODMODE:
  1997 			return ENTFLAG_GODMODE;
  1998 		case FL_NOTARGET:
  1999 			return ENTFLAG_NOTARGET;
  2000 		case FL_AIMTARGET:
  2001 			return ENTFLAG_AIMTARGET;
  2002 		case FL_PARTIALGROUND:
  2003 			return ENTFLAG_PARTIALGROUND;
  2004 		case FL_STATICPROP:
  2005 			return ENTFLAG_STATICPROP;
  2006 		case FL_GRAPHED:
  2007 			return ENTFLAG_GRAPHED;
  2008 		case FL_GRENADE:
  2009 			return ENTFLAG_GRENADE;
  2010 		case FL_STEPMOVEMENT:
  2011 			return ENTFLAG_STEPMOVEMENT;
  2012 		case FL_DONTTOUCH:
  2013 			return ENTFLAG_DONTTOUCH;
  2014 		case FL_BASEVELOCITY:
  2015 			return ENTFLAG_BASEVELOCITY;
  2016 		case FL_WORLDBRUSH:
  2017 			return ENTFLAG_WORLDBRUSH;
  2018 		case FL_OBJECT:
  2019 			return ENTFLAG_OBJECT;
  2020 		case FL_KILLME:
  2021 			return ENTFLAG_KILLME;
  2022 		case FL_ONFIRE:
  2023 			return ENTFLAG_ONFIRE;
  2024 		case FL_DISSOLVING:
  2025 			return ENTFLAG_DISSOLVING;
  2026 		case FL_TRANSRAGDOLL:
  2027 			return ENTFLAG_TRANSRAGDOLL;
  2028 		case FL_UNBLOCKABLE_BY_PLAYER:
  2029 			return ENTFLAG_UNBLOCKABLE_BY_PLAYER;
  2030 #if SOURCE_ENGINE == SE_ALIENSWARM
  2031 		case FL_FREEZING:
  2032 			return ENTFLAG_FREEZING;
  2033 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE || SOURCE_ENGINE == SE_CSS
  2034 		case FL_EP2V_UNKNOWN:
  2035 			return ENTFLAG_EP2V_UNKNOWN1;
  2036 #endif
  2037 		default:
  2038 			return 0;
  2039 	}
  2040 }
  2041 
  2042 static int32_t SMEntFlagToSDKEntFlag(int32_t flag)
  2043 {
  2044 	switch (flag)
  2045 	{
  2046 		case ENTFLAG_ONGROUND:
  2047 			return FL_ONGROUND;
  2048 		case ENTFLAG_DUCKING:
  2049 			return FL_DUCKING;
  2050 		case ENTFLAG_WATERJUMP:
  2051 			return FL_WATERJUMP;
  2052 		case ENTFLAG_ONTRAIN:
  2053 			return FL_ONTRAIN;
  2054 		case ENTFLAG_INRAIN:
  2055 			return FL_INRAIN;
  2056 		case ENTFLAG_FROZEN:
  2057 			return FL_FROZEN;
  2058 		case ENTFLAG_ATCONTROLS:
  2059 			return FL_ATCONTROLS;
  2060 		case ENTFLAG_CLIENT:
  2061 			return FL_CLIENT;
  2062 		case ENTFLAG_FAKECLIENT:
  2063 			return FL_FAKECLIENT;
  2064 		case ENTFLAG_INWATER:
  2065 			return FL_INWATER;
  2066 		case ENTFLAG_FLY:
  2067 			return FL_FLY;
  2068 		case ENTFLAG_SWIM:
  2069 			return FL_SWIM;
  2070 		case ENTFLAG_CONVEYOR:
  2071 			return FL_CONVEYOR;
  2072 		case ENTFLAG_NPC:
  2073 			return FL_NPC;
  2074 		case ENTFLAG_GODMODE:
  2075 			return FL_GODMODE;
  2076 		case ENTFLAG_NOTARGET:
  2077 			return FL_NOTARGET;
  2078 		case ENTFLAG_AIMTARGET:
  2079 			return FL_AIMTARGET;
  2080 		case ENTFLAG_PARTIALGROUND:
  2081 			return FL_PARTIALGROUND;
  2082 		case ENTFLAG_STATICPROP:
  2083 			return FL_STATICPROP;
  2084 		case ENTFLAG_GRAPHED:
  2085 			return FL_GRAPHED;
  2086 		case ENTFLAG_GRENADE:
  2087 			return FL_GRENADE;
  2088 		case ENTFLAG_STEPMOVEMENT:
  2089 			return FL_STEPMOVEMENT;
  2090 		case ENTFLAG_DONTTOUCH:
  2091 			return FL_DONTTOUCH;
  2092 		case ENTFLAG_BASEVELOCITY:
  2093 			return FL_BASEVELOCITY;
  2094 		case ENTFLAG_WORLDBRUSH:
  2095 			return FL_WORLDBRUSH;
  2096 		case ENTFLAG_OBJECT:
  2097 			return FL_OBJECT;
  2098 		case ENTFLAG_KILLME:
  2099 			return FL_KILLME;
  2100 		case ENTFLAG_ONFIRE:
  2101 			return FL_ONFIRE;
  2102 		case ENTFLAG_DISSOLVING:
  2103 			return FL_DISSOLVING;
  2104 		case ENTFLAG_TRANSRAGDOLL:
  2105 			return FL_TRANSRAGDOLL;
  2106 		case ENTFLAG_UNBLOCKABLE_BY_PLAYER:
  2107 			return FL_UNBLOCKABLE_BY_PLAYER;
  2108 #if SOURCE_ENGINE == SE_ALIENSWARM
  2109 		case ENTFLAG_FREEZING:
  2110 			return FL_FREEZING;
  2111 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE || SOURCE_ENGINE == SE_CSS
  2112 		case ENTFLAG_EP2V_UNKNOWN1:
  2113 			return FL_EP2V_UNKNOWN;
  2114 #endif
  2115 		default:
  2116 			return 0;
  2117 	}
  2118 }
  2119 
  2120 static cell_t GetEntityFlags(IPluginContext *pContext, const cell_t *params)
  2121 {
  2122 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
  2123 	if (!pEntity)
  2124 	{
  2125 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  2126 	}
  2127 
  2128 	const char *prop = g_pGameConf->GetKeyValue("m_fFlags");
  2129 	if (!prop)
  2130 	{
  2131 		return pContext->ThrowNativeError("Could not find m_fFlags prop in gamedata");
  2132 	}
  2133 
  2134 	typedescription_t *td;
  2135 	datamap_t *pMap;
  2136 
  2137 	if ((pMap = CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  2138 	{
  2139 		return pContext->ThrowNativeError("Could not retrieve datamap");
  2140 	}
  2141 	if ((td = g_HL2.FindInDataMap(pMap, prop)) == NULL)
  2142 	{
  2143 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d)",
  2144 			prop,
  2145 			params[1]);
  2146 	}
  2147 
  2148 	int offset = GetTypeDescOffs(td);
  2149 
  2150 	int32_t actual_flags = *(int32_t *)((uint8_t *)pEntity + offset);
  2151 	int32_t sm_flags = 0;
  2152 
  2153 	for (int32_t i = 0; i < 32; i++)
  2154 	{
  2155 		int32_t flag = (1<<i);
  2156 		if ((actual_flags & flag) == flag)
  2157 		{
  2158 			sm_flags |= SDKEntFlagToSMEntFlag(flag);
  2159 		}
  2160 	}
  2161 
  2162 	return sm_flags;
  2163 }
  2164 
  2165 static cell_t SetEntityFlags(IPluginContext *pContext, const cell_t *params)
  2166 {
  2167 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
  2168 	if (!pEntity)
  2169 	{
  2170 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  2171 	}
  2172 
  2173 	const char *prop = g_pGameConf->GetKeyValue("m_fFlags");
  2174 	if (!prop)
  2175 	{
  2176 		return pContext->ThrowNativeError("Could not find m_fFlags prop in gamedata");
  2177 	}
  2178 
  2179 	typedescription_t *td;
  2180 	datamap_t *pMap;
  2181 
  2182 	if ((pMap = CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  2183 	{
  2184 		return pContext->ThrowNativeError("Could not retrieve datamap");
  2185 	}
  2186 	if ((td = g_HL2.FindInDataMap(pMap, prop)) == NULL)
  2187 	{
  2188 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d)",
  2189 			prop,
  2190 			params[1]);
  2191 	}
  2192 
  2193 	int offset = GetTypeDescOffs(td);
  2194 
  2195 	int32_t sm_flags = params[2];
  2196 	int32_t actual_flags = 0;
  2197 
  2198 	for (int32_t i = 0; i < 32; i++)
  2199 	{
  2200 		int32_t flag = (1<<i);
  2201 		if ((sm_flags & flag) == flag)
  2202 		{
  2203 			actual_flags |= SMEntFlagToSDKEntFlag(flag);
  2204 		}
  2205 	}
  2206 
  2207 	*(int32_t *)((uint8_t *)pEntity + offset) = actual_flags;
  2208 
  2209 	return 0;
  2210 }
  2211 
  2212 static cell_t GetEntityAddress(IPluginContext *pContext, const cell_t *params)
  2213 {
  2214 	CBaseEntity * pEntity = GetEntity(params[1]);
  2215 	if (!pEntity)
  2216 	{
  2217 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  2218 	}
  2219 	return reinterpret_cast<cell_t>(pEntity);
  2220 }
  2221 
  2222 REGISTER_NATIVES(entityNatives)
  2223 {
  2224 	{"ChangeEdictState",		ChangeEdictState},
  2225 	{"CreateEdict",				CreateEdict},
  2226 	{"FindDataMapOffs",			FindDataMapOffs},
  2227 	{"FindSendPropInfo",		FindSendPropInfo},
  2228 	{"FindSendPropOffs",		FindSendPropOffs},
  2229 	{"GetEdictClassname",		GetEdictClassname},
  2230 	{"GetEdictFlags",			GetEdictFlags},
  2231 	{"GetEntData",				GetEntData},
  2232 	{"GetEntDataEnt",			GetEntDataEnt},		/** DEPRECATED */
  2233 	{"GetEntDataEnt2",			GetEntDataEnt2},
  2234 	{"GetEntDataFloat",			GetEntDataFloat},
  2235 	{"GetEntDataString",		GetEntDataString},
  2236 	{"GetEntDataVector",		GetEntDataVector},
  2237 	{"GetEntProp",				GetEntProp},
  2238 	{"GetEntPropArraySize",		GetEntPropArraySize},
  2239 	{"GetEntPropEnt",			GetEntPropEnt},
  2240 	{"GetEntPropFloat",			GetEntPropFloat},
  2241 	{"GetEntPropString",		GetEntPropString},
  2242 	{"GetEntPropVector",		GetEntPropVector},
  2243 	{"GetEntityCount",			GetEntityCount},
  2244 	{"GetEntityFlags",			GetEntityFlags},
  2245 	{"GetEntityNetClass",		GetEntityNetClass},
  2246 	{"GetMaxEntities",			GetMaxEntities},
  2247 	{"IsEntNetworkable",		IsEntNetworkable},
  2248 	{"IsValidEdict",			IsValidEdict},
  2249 	{"IsValidEntity",			IsValidEntity},
  2250 	{"RemoveEdict",				RemoveEdict},
  2251 	{"SetEdictFlags",			SetEdictFlags},
  2252 	{"SetEntData",				SetEntData},
  2253 	{"SetEntDataEnt",			SetEntDataEnt},		/** DEPRECATED */
  2254 	{"SetEntDataEnt2",			SetEntDataEnt2},
  2255 	{"SetEntDataFloat",			SetEntDataFloat},
  2256 	{"SetEntDataVector",		SetEntDataVector},
  2257 	{"SetEntDataString",		SetEntDataString},
  2258 	{"SetEntityFlags",			SetEntityFlags},
  2259 	{"SetEntProp",				SetEntProp},
  2260 	{"SetEntPropEnt",			SetEntPropEnt},
  2261 	{"SetEntPropFloat",			SetEntPropFloat},
  2262 	{"SetEntPropString",		SetEntPropString},
  2263 	{"SetEntPropVector",		SetEntPropVector},
  2264 	{"GetEntityAddress",		GetEntityAddress},
  2265 	{NULL,						NULL}
  2266 };