extensions/sdktools/vhelpers.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed Aug 14 14:20:57 2013 -0400 (2013-08-14)
changeset 4008 490b01aa0683
parent 3997 59bcfc51eb79
child 4159 08f0f6c1e452
permissions -rw-r--r--
Updated some CS:GO gamedata.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod SDKTools Extension
     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 "extension.h"
    33 #include "util.h"
    34 #include "vhelpers.h"
    35 #include "vglobals.h"
    36 
    37 CallHelper s_Teleport;
    38 CallHelper s_GetVelocity;
    39 CallHelper s_EyeAngles;
    40 
    41 class CTraceFilterSimple : public CTraceFilterEntitiesOnly
    42 {
    43 public:
    44 	CTraceFilterSimple(const IHandleEntity *passentity): m_pPassEnt(passentity)
    45 	{
    46 	}
    47 	virtual bool ShouldHitEntity(IHandleEntity *pServerEntity, int contentsMask)
    48 	{
    49 		if (pServerEntity == m_pPassEnt)
    50 		{
    51 			return false;
    52 		}
    53 		return true;
    54 	}
    55 private:
    56 	const IHandleEntity *m_pPassEnt;
    57 };
    58 
    59 bool SetupTeleport()
    60 {
    61 	if (s_Teleport.setup)
    62 	{
    63 		return s_Teleport.supported;
    64 	}
    65 
    66 	/* Setup Teleport */
    67 	int offset;
    68 	if (g_pGameConf->GetOffset("Teleport", &offset))
    69 	{
    70 		PassInfo info[3];
    71 		info[0].flags = info[1].flags = info[2].flags = PASSFLAG_BYVAL;
    72 		info[0].size = info[1].size = info[2].size = sizeof(void *);
    73 		info[0].type = info[1].type = info[2].type = PassType_Basic;
    74 
    75 		s_Teleport.call = g_pBinTools->CreateVCall(offset, 0, 0, NULL, info, 3);
    76 
    77 		if (s_Teleport.call != NULL)
    78 		{
    79 			s_Teleport.supported = true;
    80 		}
    81 	}
    82 
    83 	s_Teleport.setup = true;
    84 
    85 	return s_Teleport.supported;
    86 }
    87 
    88 void Teleport(CBaseEntity *pEntity, Vector *origin, QAngle *ang, Vector *velocity)
    89 {
    90 	unsigned char params[sizeof(void *) * 4];
    91 	unsigned char *vptr = params;
    92 	*(CBaseEntity **)vptr = pEntity;
    93 	vptr += sizeof(CBaseEntity *);
    94 	*(Vector **)vptr = origin;
    95 	vptr += sizeof(Vector *);
    96 	*(QAngle **)vptr = ang;
    97 	vptr += sizeof(QAngle *);
    98 	*(Vector **)vptr = velocity;
    99 	
   100 	s_Teleport.call->Execute(params, NULL);
   101 }
   102 
   103 bool IsTeleportSupported()
   104 {
   105 	return SetupTeleport();
   106 }
   107 
   108 bool SetupGetVelocity()
   109 {
   110 	if (s_GetVelocity.setup)
   111 	{
   112 		return s_GetVelocity.supported;
   113 	}
   114 
   115 	int offset;
   116 	if (g_pGameConf->GetOffset("GetVelocity", &offset))
   117 	{
   118 		PassInfo info[2];
   119 		info[0].flags = info[1].flags = PASSFLAG_BYVAL;
   120 		info[0].size = info[1].size = sizeof(void *);
   121 		info[0].type = info[1].type = PassType_Basic;
   122 
   123 		s_GetVelocity.call = g_pBinTools->CreateVCall(offset, 0, 0, NULL, info, 2);
   124 
   125 		if (s_GetVelocity.call != NULL)
   126 		{
   127 			s_GetVelocity.supported = true;
   128 		}
   129 	}
   130 
   131 	s_GetVelocity.setup = true;
   132 
   133 	return s_GetVelocity.supported;
   134 }
   135 
   136 void GetVelocity(CBaseEntity *pEntity, Vector *velocity, AngularImpulse *angvelocity)
   137 {
   138 	unsigned char params[sizeof(void *) * 3];
   139 	unsigned char *vptr = params;
   140 	*(CBaseEntity **)vptr = pEntity;
   141 	vptr += sizeof(CBaseEntity *);
   142 	*(Vector **)vptr = velocity;
   143 	vptr += sizeof(Vector *);
   144 	*(AngularImpulse **)vptr = angvelocity;
   145 
   146 	s_GetVelocity.call->Execute(params, NULL);
   147 }
   148 
   149 bool IsGetVelocitySupported()
   150 {
   151 	return SetupGetVelocity();
   152 }
   153 
   154 bool SetupGetEyeAngles()
   155 {
   156 	if (s_EyeAngles.setup)
   157 	{
   158 		return s_EyeAngles.supported;
   159 	}
   160 
   161 	int offset;
   162 	if (g_pGameConf->GetOffset("EyeAngles", &offset))
   163 	{
   164 		PassInfo info[1];
   165 		info[0].flags = PASSFLAG_BYVAL;
   166 		info[0].size = sizeof(void *);
   167 		info[0].type = PassType_Basic;
   168 
   169 		s_EyeAngles.call = g_pBinTools->CreateVCall(offset, 0, 0, info, NULL, 0);
   170 
   171 		if (s_EyeAngles.call != NULL)
   172 		{
   173 			s_EyeAngles.supported = true;
   174 		}
   175 	}
   176 
   177 	s_EyeAngles.setup = true;
   178 
   179 	return s_EyeAngles.supported;
   180 }
   181 
   182 bool GetEyeAngles(CBaseEntity *pEntity, QAngle *pAngles)
   183 {
   184 	if (!IsEyeAnglesSupported())
   185 	{
   186 		return false;
   187 	}
   188 
   189 	QAngle *pRetAngle = NULL;
   190 	unsigned char params[sizeof(void *)];
   191 	unsigned char *vptr = params;
   192 
   193 	*(CBaseEntity **)vptr = pEntity;
   194 
   195 	s_EyeAngles.call->Execute(params, &pRetAngle);
   196 
   197 	if (pRetAngle == NULL)
   198 	{
   199 		return false;
   200 	}
   201 
   202 	*pAngles = *pRetAngle;
   203 
   204 	return true;
   205 }
   206 
   207 int GetClientAimTarget(edict_t *pEdict, bool only_players)
   208 {
   209 	CBaseEntity *pEntity = pEdict->GetUnknown() ? pEdict->GetUnknown()->GetBaseEntity() : NULL;
   210 
   211 	if (pEntity == NULL)
   212 	{
   213 		return -1;
   214 	}
   215 
   216 	Vector eye_position;
   217 	QAngle eye_angles;
   218 
   219 	/* Get the private information we need */
   220 #if SOURCE_ENGINE == SE_DOTA
   221 	serverClients->ClientEarPosition(IndexOfEdict(pEdict), &eye_position);
   222 #else
   223 	serverClients->ClientEarPosition(pEdict, &eye_position);
   224 #endif
   225 
   226 	if (!GetEyeAngles(pEntity, &eye_angles))
   227 	{
   228 		return -2;
   229 	}
   230 
   231 	Vector aim_dir;
   232 	AngleVectors(eye_angles, &aim_dir);
   233 	VectorNormalize(aim_dir);
   234 
   235 	Vector vec_end = eye_position + aim_dir * 8000;
   236 
   237 	Ray_t ray;
   238 	ray.Init(eye_position, vec_end);
   239 
   240 	trace_t tr;
   241 	CTraceFilterSimple simple(pEdict->GetIServerEntity());
   242 
   243 	enginetrace->TraceRay(ray, MASK_SOLID|CONTENTS_DEBRIS|CONTENTS_HITBOX, &simple, &tr);
   244 
   245 	if (tr.fraction == 1.0f || tr.m_pEnt == NULL)
   246 	{
   247 		return -1;
   248 	}
   249 
   250 	int ent_ref = gamehelpers->EntityToBCompatRef(tr.m_pEnt);
   251 	int ent_index = gamehelpers->ReferenceToIndex(ent_ref);
   252 
   253 	IGamePlayer *pTargetPlayer = playerhelpers->GetGamePlayer(ent_index);
   254 	if (pTargetPlayer != NULL && !pTargetPlayer->IsInGame())
   255 	{
   256 		return -1;
   257 	}
   258 	else if (only_players && pTargetPlayer == NULL)
   259 	{
   260 		return -1;
   261 	}
   262 
   263 	return ent_ref;
   264 }
   265 
   266 bool IsEyeAnglesSupported()
   267 {
   268 	return SetupGetEyeAngles();
   269 }
   270 
   271 bool GetPlayerInfo(int client, player_info_t *info)
   272 {
   273 #if SOURCE_ENGINE >= SE_ORANGEBOX
   274 	return engine->GetPlayerInfo(client, info);
   275 #else
   276 	return (iserver) ? iserver->GetPlayerInfo(client-1, info) : false;
   277 #endif
   278 }
   279 
   280 void ShutdownHelpers()
   281 {
   282 	s_Teleport.Shutdown();
   283 	s_GetVelocity.Shutdown();
   284 	s_EyeAngles.Shutdown();
   285 }
   286 
   287 bool FindNestedDataTable(SendTable *pTable, const char *name)
   288 {
   289 	if (strcmp(pTable->GetName(), name) == 0)
   290 	{
   291 		return true;
   292 	}
   293 
   294 	int props = pTable->GetNumProps();
   295 	SendProp *prop;
   296 
   297 	for (int i=0; i<props; i++)
   298 	{
   299 		prop = pTable->GetProp(i);
   300 		if (prop->GetDataTable())
   301 		{
   302 			if (FindNestedDataTable(prop->GetDataTable(), name))
   303 			{
   304 				return true;
   305 			}
   306 		}
   307 	}
   308 
   309 	return false;
   310 }
   311 
   312 void UTIL_DrawSendTable_XML(FILE *fp, SendTable *pTable, int space_count)
   313 {
   314 	char spaces[255];
   315 
   316 	for (int i = 0; i < space_count; i++)
   317 	{
   318 		spaces[i] = ' ';
   319 	}
   320 	spaces[space_count] = '\0';
   321 
   322 	const char *type_name;
   323 	SendTable *pOtherTable;
   324 	SendProp *pProp;
   325 
   326 	fprintf(fp, " %s<sendtable name=\"%s\">\n", spaces, pTable->GetName());
   327 	for (int i = 0; i < pTable->GetNumProps(); i++)
   328 	{
   329 		pProp = pTable->GetProp(i);
   330 
   331 		fprintf(fp, "  %s<property name=\"%s\">\n", spaces, pProp->GetName());
   332 
   333 		if ((type_name = GetDTTypeName(pProp->GetType())) != NULL)
   334 		{
   335 			fprintf(fp, "   %s<type>%s</type>\n", spaces, type_name);
   336 		}
   337 		else
   338 		{
   339 			fprintf(fp, "   %s<type>%d</type>\n", spaces, pProp->GetType());
   340 		}
   341 
   342 		fprintf(fp, "   %s<offset>%d</offset>\n", spaces, pProp->GetOffset());
   343 		fprintf(fp, "   %s<bits>%d</bits>\n", spaces, pProp->m_nBits);
   344 
   345 		if ((pOtherTable = pTable->GetProp(i)->GetDataTable()) != NULL)
   346 		{
   347 			UTIL_DrawSendTable_XML(fp, pOtherTable, space_count + 3);
   348 		}
   349 
   350 		fprintf(fp, "  %s</property>\n", spaces);
   351 	}
   352 	fprintf(fp, " %s</sendtable>\n", spaces);
   353 }
   354 
   355 void UTIL_DrawServerClass_XML(FILE *fp, ServerClass *sc)
   356 {
   357 	fprintf(fp, "<serverclass name=\"%s\">\n", sc->GetName());
   358 	UTIL_DrawSendTable_XML(fp, sc->m_pTable, 0);
   359 	fprintf(fp, "</serverclass>\n");
   360 }
   361 
   362 void UTIL_DrawSendTable(FILE *fp, SendTable *pTable, int level = 1)
   363 {
   364 	SendProp *pProp;
   365 	const char *type;
   366 
   367 	for (int i = 0; i < pTable->GetNumProps(); i++)
   368 	{
   369 		pProp = pTable->GetProp(i);
   370 		if (pProp->GetDataTable())
   371 		{
   372 			fprintf(fp, "%*sTable: %s (offset %d) (type %s)\n", 
   373 				level, "", 
   374 				pProp->GetName(), 
   375 				pProp->GetOffset(), 
   376 				pProp->GetDataTable()->GetName());
   377 			
   378 			UTIL_DrawSendTable(fp, pProp->GetDataTable(), level + 1);
   379 		}
   380 		else
   381 		{
   382 			type = GetDTTypeName(pProp->GetType());
   383 
   384 			if (type != NULL)
   385 			{
   386 				fprintf(fp,
   387 					"%*sMember: %s (offset %d) (type %s) (bits %d)\n", 
   388 					level, "", 
   389 					pProp->GetName(),
   390 					pProp->GetOffset(),
   391 					type,
   392 					pProp->m_nBits);
   393 			}
   394 			else
   395 			{
   396 				fprintf(fp,
   397 					"%*sMember: %s (offset %d) (type %d) (bits %d)\n", 
   398 					level, "", 
   399 					pProp->GetName(),
   400 					pProp->GetOffset(),
   401 					pProp->GetType(),
   402 					pProp->m_nBits);
   403 			}
   404 		}
   405 	}
   406 }
   407 
   408 CON_COMMAND(sm_dump_netprops_xml, "Dumps the networkable property table as an XML file")
   409 {
   410 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   411 	CCommand args;
   412 #endif
   413 
   414 	if (args.ArgC() < 2)
   415 	{
   416 		META_CONPRINT("Usage: sm_dump_netprops_xml <file>\n");
   417 		return;
   418 	}
   419 
   420 	const char *file = args.Arg(1);
   421 	if (!file || file[0] == '\0')
   422 	{
   423 		META_CONPRINT("Usage: sm_dump_netprops_xml <file>\n");
   424 		return;
   425 	}
   426 
   427 	char path[PLATFORM_MAX_PATH];
   428 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
   429 
   430 	FILE *fp = NULL;
   431 	if ((fp = fopen(path, "wt")) == NULL)
   432 	{
   433 		META_CONPRINTF("Could not open file \"%s\"\n", path);
   434 		return;
   435 	}
   436 
   437 	fprintf(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
   438 	fprintf(fp, "<!-- Dump of all network properties for \"%s\" follows -->\n\n", g_pSM->GetGameFolderName());
   439 
   440 	ServerClass *pBase = gamedll->GetAllServerClasses();
   441 	while (pBase != NULL)
   442 	{
   443 		UTIL_DrawServerClass_XML(fp, pBase);
   444 		pBase = pBase->m_pNext;
   445 	}
   446 
   447 	fclose(fp);
   448 }
   449 
   450 CON_COMMAND(sm_dump_netprops, "Dumps the networkable property table as a text file")
   451 {
   452 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   453 	CCommand args;
   454 #endif
   455 
   456 	if (args.ArgC() < 2)
   457 	{
   458 		META_CONPRINT("Usage: sm_dump_netprops <file>\n");
   459 		return;
   460 	}
   461 
   462 	const char *file = args.Arg(1);
   463 	if (!file || file[0] == '\0')
   464 	{
   465 		META_CONPRINT("Usage: sm_dump_netprops <file>\n");
   466 		return;
   467 	}
   468 
   469 	char path[PLATFORM_MAX_PATH];
   470 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
   471 
   472 	FILE *fp = NULL;
   473 	if ((fp = fopen(path, "wt")) == NULL)
   474 	{
   475 		META_CONPRINTF("Could not open file \"%s\"\n", path);
   476 		return;
   477 	}
   478 
   479 	fprintf(fp, "// Dump of all network properties for \"%s\" follows\n//\n\n", g_pSM->GetGameFolderName());
   480 
   481 	ServerClass *pBase = gamedll->GetAllServerClasses();
   482 	while (pBase != NULL)
   483 	{
   484 		fprintf(fp, "%s (type %s)\n", pBase->GetName(), pBase->m_pTable->GetName());
   485 		UTIL_DrawSendTable(fp, pBase->m_pTable);
   486 		pBase = pBase->m_pNext;
   487 	}
   488 
   489 	fclose(fp);
   490 }
   491 
   492 #if defined SUBPLATFORM_SECURECRT
   493 void _ignore_invalid_parameter(
   494 						const wchar_t * expression,
   495 						const wchar_t * function, 
   496 						const wchar_t * file, 
   497 						unsigned int line,
   498 						uintptr_t pReserved
   499 						)
   500 {
   501 	/* Wow we don't care, thanks Microsoft. */
   502 }
   503 #endif
   504 
   505 CON_COMMAND(sm_dump_classes, "Dumps the class list as a text file")
   506 {
   507 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   508 	CCommand args;
   509 #endif
   510 
   511 	if (args.ArgC() < 2)
   512 	{
   513 		META_CONPRINT("Usage: sm_dump_classes <file>\n");
   514 		return;
   515 	}
   516 
   517 	const char *file = args.Arg(1);
   518 	if (!file || file[0] == '\0')
   519 	{
   520 		META_CONPRINT("Usage: sm_dump_classes <file>\n");
   521 		return;
   522 	}
   523 
   524 	ICallWrapper *pWrapper = NULL;
   525 
   526 	if (!pWrapper)
   527 	{
   528 		PassInfo retData;
   529 		retData.flags = PASSFLAG_BYVAL;
   530 		retData.size = sizeof(void *);
   531 		retData.type = PassType_Basic;
   532 
   533 		void *addr;
   534 		if (!g_pGameConf->GetMemSig("EntityFactory", &addr) || addr == NULL)
   535 		{
   536 			META_CONPRINT("Failed to locate function\n");
   537 			return;
   538 		}
   539 
   540 		pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retData, NULL, 0);
   541 	}
   542 
   543 
   544 	void *returnData = NULL;
   545 
   546 	pWrapper->Execute(NULL, &returnData);
   547 
   548 	pWrapper->Destroy();
   549 
   550 	if (returnData == NULL)
   551 	{
   552 		return;
   553 	}
   554 
   555 	CEntityFactoryDictionary *dict = ( CEntityFactoryDictionary * )returnData;
   556 
   557 	if ( !dict )
   558 	{
   559 		return;
   560 	}
   561 
   562 	char path[PLATFORM_MAX_PATH];
   563 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
   564 
   565 	FILE *fp = NULL;
   566 	if ((fp = fopen(path, "wt")) == NULL)
   567 	{
   568 		META_CONPRINTF("Could not open file \"%s\"\n", path);
   569 		return;
   570 	}
   571 
   572 	char buffer[80];
   573 	buffer[0] = 0;
   574 
   575 #if defined SUBPLATFORM_SECURECRT
   576 	_invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
   577 #endif
   578 
   579 	time_t t = g_pSM->GetAdjustedTime();
   580 	size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
   581 
   582 #if defined SUBPLATFORM_SECURECRT
   583 	_set_invalid_parameter_handler(handler);
   584 #endif
   585 
   586 	fprintf(fp, "// Dump of all classes for \"%s\" as at %s\n//\n\n", g_pSM->GetGameFolderName(), buffer);
   587 
   588 	sm_datatable_info_t info;
   589 	for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
   590 	{
   591 		IServerNetworkable *entity = dict->Create(dict->m_Factories.GetElementName(i));
   592 		ServerClass *sclass = entity->GetServerClass();
   593 		fprintf(fp,"%s - %s\n",sclass->GetName(), dict->m_Factories.GetElementName(i));
   594 		
   595 		if (!gamehelpers->FindDataMapInfo(gamehelpers->GetDataMap(entity->GetBaseEntity()), "m_iEFlags", &info))
   596 			continue;
   597 		
   598 		int *eflags = (int *)((char *)entity->GetBaseEntity() + info.actual_offset);
   599 		*eflags |= (1<<0); // EFL_KILLME
   600 	}
   601 
   602 	fclose(fp);
   603 
   604 }
   605 
   606 char *UTIL_FlagsToString(int flags)
   607 {
   608 	static char str[1024];
   609 	str[0] = 0;
   610 
   611 	if (flags & FTYPEDESC_GLOBAL)
   612 	{
   613 		strcat(str,	"Global|");
   614 	}
   615 	if (flags & FTYPEDESC_SAVE)
   616 	{
   617 		strcat(str,	"Save|");
   618 	}
   619 	if (flags & FTYPEDESC_KEY)
   620 	{
   621 		strcat(str,	"Key|");
   622 	}
   623 	if (flags & FTYPEDESC_INPUT)
   624 	{
   625 		strcat(str,	"Input|");
   626 	}
   627 	if (flags & FTYPEDESC_OUTPUT)
   628 	{
   629 		strcat(str,	"Output|");
   630 	}
   631 	if (flags & FTYPEDESC_FUNCTIONTABLE)
   632 	{
   633 		strcat(str,	"FunctionTable|");
   634 	}
   635 	if (flags & FTYPEDESC_PTR)
   636 	{
   637 		strcat(str,	"Ptr|");
   638 	}
   639 	if (flags & FTYPEDESC_OVERRIDE)
   640 	{
   641 		strcat(str,	"Override|");
   642 	}
   643 
   644 	int len = strlen(str) - 1;
   645 	if (len > 0)
   646 	{
   647 		str[len] = 0; // Strip the final '|'
   648 	}
   649 
   650 	return str;
   651 }
   652 
   653 void UTIL_DrawDataTable(FILE *fp, datamap_t *pMap, int level)
   654 {
   655 	char spaces[255];
   656 
   657 	for (int i=0; i<level; i++)
   658 	{
   659 		spaces[i] = ' ';
   660 	}
   661 
   662 	spaces[level] = '\0';
   663 
   664 	const char *externalname;
   665 	char *flags;
   666 
   667 	while (pMap)
   668 	{
   669 		for (int i=0; i<pMap->dataNumFields; i++)
   670 		{
   671 			if (pMap->dataDesc[i].fieldName == NULL)
   672 			{
   673 				continue;
   674 			}
   675 
   676 			if (pMap->dataDesc[i].td)
   677 			{
   678 				fprintf(fp, " %sSub-Class Table (%d Deep): %s - %s\n", spaces, level+1, pMap->dataDesc[i].fieldName, pMap->dataDesc[i].td->dataClassName);
   679 				UTIL_DrawDataTable(fp, pMap->dataDesc[i].td, level+1);
   680 			}
   681 			else
   682 			{
   683 				externalname  = pMap->dataDesc[i].externalName;
   684 				flags = UTIL_FlagsToString(pMap->dataDesc[i].flags);
   685 
   686 				if (externalname == NULL)
   687 				{
   688 					fprintf(fp,"%s- %s (%s)(%i Bytes)\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes);
   689 				}
   690 				else
   691 				{
   692 					fprintf(fp,"%s- %s (%s)(%i Bytes) - %s\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes, externalname);
   693 				}
   694 			}
   695 		}
   696 		pMap = pMap->baseMap;
   697 	}
   698 }
   699 
   700 CON_COMMAND(sm_dump_datamaps, "Dumps the data map list as a text file")
   701 {
   702 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   703 	CCommand args;
   704 #endif
   705 
   706 	if (args.ArgC() < 2)
   707 	{
   708 		META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
   709 		return;
   710 	}
   711 
   712 	const char *file = args.Arg(1);
   713 	if (!file || file[0] == '\0')
   714 	{
   715 		META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
   716 		return;
   717 	}
   718 
   719 	static CEntityFactoryDictionary *dict = NULL;
   720 	if (dict == NULL)
   721 	{
   722 		void *addr;
   723 		if (g_pGameConf->GetMemSig("EntityFactoryFinder", (void **)&addr) && addr)
   724 		{
   725 			int offset;
   726 			if (!g_pGameConf->GetOffset("EntityFactoryOffset", &offset) || !offset)
   727 			{
   728 				return;
   729 			}
   730 			dict = *reinterpret_cast<CEntityFactoryDictionary **>((intptr_t)addr + offset);
   731 		}
   732 	}
   733 
   734 	if (dict == NULL)
   735 	{
   736 		ICallWrapper *pWrapper = NULL;
   737 
   738 		PassInfo retData;
   739 		retData.flags = PASSFLAG_BYVAL;
   740 		retData.size = sizeof(void *);
   741 		retData.type = PassType_Basic;
   742 
   743 		void *addr;
   744 		if (g_pGameConf->GetMemSig("EntityFactory", &addr) && addr != NULL)
   745 		{
   746 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retData, NULL, 0);
   747 		}
   748 
   749 		if (pWrapper)
   750 		{
   751 			void *returnData = NULL;
   752 
   753 			pWrapper->Execute(NULL, &returnData);
   754 
   755 			pWrapper->Destroy();
   756 
   757 			if (returnData == NULL)
   758 			{
   759 				return;
   760 			}
   761 
   762 			dict = ( CEntityFactoryDictionary * )returnData;
   763 		}
   764 	}
   765 
   766 	if ( dict == NULL )
   767 	{
   768 		META_CONPRINT("Failed to locate function\n");
   769 		return;
   770 	}
   771 
   772 	char path[PLATFORM_MAX_PATH];
   773 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
   774 
   775 	FILE *fp = NULL;
   776 	if ((fp = fopen(path, "wt")) == NULL)
   777 	{
   778 		META_CONPRINTF("Could not open file \"%s\"\n", path);
   779 		return;
   780 	}
   781 
   782 	char buffer[80];
   783 	buffer[0] = 0;
   784 
   785 #if defined SUBPLATFORM_SECURECRT
   786 	_invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
   787 #endif
   788 
   789 	time_t t = g_pSM->GetAdjustedTime();
   790 	size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
   791 
   792 #if defined SUBPLATFORM_SECURECRT
   793 	_set_invalid_parameter_handler(handler);
   794 #endif
   795 
   796 	fprintf(fp, "// Dump of all datamaps for \"%s\" as at %s\n//\n//\n", g_pSM->GetGameFolderName(), buffer);
   797 
   798 
   799 	fprintf(fp, "// Flag Details:\n//\n");
   800 
   801 	fprintf(fp, "// Global: This field is masked for global entity save/restore\n");
   802 	fprintf(fp, "// Save: This field is saved to disk\n");
   803 	fprintf(fp, "// Key: This field can be requested and written to by string name at load time\n");
   804 	fprintf(fp, "// Input: This field can be written to by string name at run time, and a function called\n");
   805 	fprintf(fp, "// Output: This field propogates it's value to all targets whenever it changes\n");
   806 	fprintf(fp, "// FunctionTable: This is a table entry for a member function pointer\n");
   807 	fprintf(fp, "// Ptr: This field is a pointer, not an embedded object\n");
   808 	fprintf(fp, "// Override: The field is an override for one in a base class (only used by prediction system for now)\n");
   809 
   810 	fprintf(fp, "//\n\n");
   811 
   812 	static int offsEFlags = -1;
   813 	for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
   814 	{
   815 		IServerNetworkable *entity = dict->Create(dict->m_Factories.GetElementName(i));
   816 		ServerClass *sclass = entity->GetServerClass();
   817 		datamap_t *pMap = gamehelpers->GetDataMap(entity->GetBaseEntity());
   818 
   819 		fprintf(fp,"%s - %s\n", sclass->GetName(), dict->m_Factories.GetElementName(i));
   820 
   821 		UTIL_DrawDataTable(fp, pMap, 0);
   822 
   823 		if (offsEFlags == -1)
   824 		{
   825 			sm_datatable_info_t info;
   826 			if (!gamehelpers->FindDataMapInfo(pMap, "m_iEFlags", &info))
   827 			{
   828 				continue;
   829 			}
   830 
   831 			offsEFlags = info.actual_offset;
   832 		}
   833 		
   834 		int *eflags = (int *)((char *)entity->GetBaseEntity() + offsEFlags);
   835 		*eflags |= (1<<0); // EFL_KILLME
   836 	}
   837 
   838 	fclose(fp);
   839 
   840 }