extensions/sdktools/vhelpers.cpp
author Scott Ehlert <ds@alliedmods.net>
Mon Mar 03 05:20:40 2014 -0600 (2014-03-03)
changeset 4272 8b39aa02b05b
parent 4169 65eebc76e836
child 4304 bff9916aa9b2
permissions -rw-r--r--
Add gamedata for EntityFactoryDictionary and IServer for Dota 2 on Linux (bug 6068, r=psychonic).
     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 CEntityFactoryDictionary *GetEntityFactoryDictionary()
   506 {
   507 	static CEntityFactoryDictionary *dict = NULL;
   508 
   509 #if SOURCE_ENGINE == SE_TF2 || SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_SDK2013
   510 	dict = (CEntityFactoryDictionary *) servertools->GetEntityFactoryDictionary();
   511 #else
   512 	if (dict == NULL)
   513 	{
   514 		void *addr;
   515 		if (g_pGameConf->GetMemSig("EntityFactoryFinder", (void **) &addr) && addr)
   516 		{
   517 			int offset;
   518 			if (!g_pGameConf->GetOffset("EntityFactoryOffset", &offset) || !offset)
   519 			{
   520 				return NULL;
   521 			}
   522 			dict = *reinterpret_cast<CEntityFactoryDictionary **>((intptr_t) addr + offset);
   523 		}
   524 	}
   525 
   526 	if (dict == NULL)
   527 	{
   528 		ICallWrapper *pWrapper = NULL;
   529 
   530 		PassInfo retData;
   531 		retData.flags = PASSFLAG_BYVAL;
   532 		retData.size = sizeof(void *);
   533 		retData.type = PassType_Basic;
   534 
   535 		void *addr;
   536 		if (!g_pGameConf->GetMemSig("EntityFactory", &addr) || !addr)
   537 		{
   538 			int offset;
   539 
   540 			if (!g_pGameConf->GetMemSig("EntityFactoryCaller", &addr) || !addr)
   541 				return NULL;
   542 
   543 			if (!g_pGameConf->GetOffset("EntityFactoryCallOffset", &offset))
   544 				return NULL;
   545 
   546 			// Get relative offset to function
   547 			int32_t funcOffset = *(int32_t *)((intptr_t)addr + offset);
   548 
   549 			// Get real address of function
   550 			// Address of signature + offset of relative offset + sizeof(int32_t) offset + relative offset
   551 			addr = (void *)((intptr_t)addr + offset + 4 + funcOffset);
   552 		}
   553 
   554 		pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retData, NULL, 0);
   555 
   556 		if (pWrapper)
   557 		{
   558 			void *returnData = NULL;
   559 
   560 			pWrapper->Execute(NULL, &returnData);
   561 
   562 			pWrapper->Destroy();
   563 
   564 			if (returnData == NULL)
   565 			{
   566 				return NULL;
   567 			}
   568 
   569 			dict = (CEntityFactoryDictionary *) returnData;
   570 		}
   571 	}
   572 #endif
   573 
   574 	return dict;
   575 }
   576 
   577 CON_COMMAND(sm_dump_classes, "Dumps the class list as a text file")
   578 {
   579 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   580 	CCommand args;
   581 #endif
   582 
   583 	if (args.ArgC() < 2)
   584 	{
   585 		META_CONPRINT("Usage: sm_dump_classes <file>\n");
   586 		return;
   587 	}
   588 
   589 	const char *file = args.Arg(1);
   590 	if (!file || file[0] == '\0')
   591 	{
   592 		META_CONPRINT("Usage: sm_dump_classes <file>\n");
   593 		return;
   594 	}
   595 
   596 	CEntityFactoryDictionary *dict = GetEntityFactoryDictionary();
   597 	if (dict == NULL)
   598 	{
   599 		META_CONPRINT("Failed to locate function\n");
   600 		return;
   601 	}
   602 
   603 	char path[PLATFORM_MAX_PATH];
   604 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
   605 
   606 	FILE *fp = NULL;
   607 	if ((fp = fopen(path, "wt")) == NULL)
   608 	{
   609 		META_CONPRINTF("Could not open file \"%s\"\n", path);
   610 		return;
   611 	}
   612 
   613 	char buffer[80];
   614 	buffer[0] = 0;
   615 
   616 #if defined SUBPLATFORM_SECURECRT
   617 	_invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
   618 #endif
   619 
   620 	time_t t = g_pSM->GetAdjustedTime();
   621 	size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
   622 
   623 #if defined SUBPLATFORM_SECURECRT
   624 	_set_invalid_parameter_handler(handler);
   625 #endif
   626 
   627 	fprintf(fp, "// Dump of all classes for \"%s\" as at %s\n//\n\n", g_pSM->GetGameFolderName(), buffer);
   628 
   629 	sm_datatable_info_t info;
   630 	for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
   631 	{
   632 		IServerNetworkable *entity = dict->Create(dict->m_Factories.GetElementName(i));
   633 		ServerClass *sclass = entity->GetServerClass();
   634 		fprintf(fp,"%s - %s\n",sclass->GetName(), dict->m_Factories.GetElementName(i));
   635 		
   636 		if (!gamehelpers->FindDataMapInfo(gamehelpers->GetDataMap(entity->GetBaseEntity()), "m_iEFlags", &info))
   637 			continue;
   638 		
   639 		int *eflags = (int *)((char *)entity->GetBaseEntity() + info.actual_offset);
   640 		*eflags |= (1<<0); // EFL_KILLME
   641 	}
   642 
   643 	fclose(fp);
   644 
   645 }
   646 
   647 char *UTIL_FlagsToString(int flags)
   648 {
   649 	static char str[1024];
   650 	str[0] = 0;
   651 
   652 	if (flags & FTYPEDESC_GLOBAL)
   653 	{
   654 		strcat(str,	"Global|");
   655 	}
   656 	if (flags & FTYPEDESC_SAVE)
   657 	{
   658 		strcat(str,	"Save|");
   659 	}
   660 	if (flags & FTYPEDESC_KEY)
   661 	{
   662 		strcat(str,	"Key|");
   663 	}
   664 	if (flags & FTYPEDESC_INPUT)
   665 	{
   666 		strcat(str,	"Input|");
   667 	}
   668 	if (flags & FTYPEDESC_OUTPUT)
   669 	{
   670 		strcat(str,	"Output|");
   671 	}
   672 	if (flags & FTYPEDESC_FUNCTIONTABLE)
   673 	{
   674 		strcat(str,	"FunctionTable|");
   675 	}
   676 	if (flags & FTYPEDESC_PTR)
   677 	{
   678 		strcat(str,	"Ptr|");
   679 	}
   680 	if (flags & FTYPEDESC_OVERRIDE)
   681 	{
   682 		strcat(str,	"Override|");
   683 	}
   684 
   685 	int len = strlen(str) - 1;
   686 	if (len > 0)
   687 	{
   688 		str[len] = 0; // Strip the final '|'
   689 	}
   690 
   691 	return str;
   692 }
   693 
   694 void UTIL_DrawDataTable(FILE *fp, datamap_t *pMap, int level)
   695 {
   696 	char spaces[255];
   697 
   698 	for (int i=0; i<level; i++)
   699 	{
   700 		spaces[i] = ' ';
   701 	}
   702 
   703 	spaces[level] = '\0';
   704 
   705 	const char *externalname;
   706 	char *flags;
   707 
   708 	while (pMap)
   709 	{
   710 		for (int i=0; i<pMap->dataNumFields; i++)
   711 		{
   712 			if (pMap->dataDesc[i].fieldName == NULL)
   713 			{
   714 				continue;
   715 			}
   716 
   717 			if (pMap->dataDesc[i].td)
   718 			{
   719 				fprintf(fp, " %sSub-Class Table (%d Deep): %s - %s\n", spaces, level+1, pMap->dataDesc[i].fieldName, pMap->dataDesc[i].td->dataClassName);
   720 				UTIL_DrawDataTable(fp, pMap->dataDesc[i].td, level+1);
   721 			}
   722 			else
   723 			{
   724 				externalname  = pMap->dataDesc[i].externalName;
   725 				flags = UTIL_FlagsToString(pMap->dataDesc[i].flags);
   726 
   727 				if (externalname == NULL)
   728 				{
   729 					fprintf(fp,"%s- %s (%s)(%i Bytes)\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes);
   730 				}
   731 				else
   732 				{
   733 					fprintf(fp,"%s- %s (%s)(%i Bytes) - %s\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes, externalname);
   734 				}
   735 			}
   736 		}
   737 		pMap = pMap->baseMap;
   738 	}
   739 }
   740 
   741 CON_COMMAND(sm_dump_datamaps, "Dumps the data map list as a text file")
   742 {
   743 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   744 	CCommand args;
   745 #endif
   746 
   747 	if (args.ArgC() < 2)
   748 	{
   749 		META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
   750 		return;
   751 	}
   752 
   753 	const char *file = args.Arg(1);
   754 	if (!file || file[0] == '\0')
   755 	{
   756 		META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
   757 		return;
   758 	}
   759 
   760 	CEntityFactoryDictionary *dict = GetEntityFactoryDictionary();
   761 	if ( dict == NULL )
   762 	{
   763 		META_CONPRINT("Failed to locate function\n");
   764 		return;
   765 	}
   766 
   767 	char path[PLATFORM_MAX_PATH];
   768 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
   769 
   770 	FILE *fp = NULL;
   771 	if ((fp = fopen(path, "wt")) == NULL)
   772 	{
   773 		META_CONPRINTF("Could not open file \"%s\"\n", path);
   774 		return;
   775 	}
   776 
   777 	char buffer[80];
   778 	buffer[0] = 0;
   779 
   780 #if defined SUBPLATFORM_SECURECRT
   781 	_invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
   782 #endif
   783 
   784 	time_t t = g_pSM->GetAdjustedTime();
   785 	size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
   786 
   787 #if defined SUBPLATFORM_SECURECRT
   788 	_set_invalid_parameter_handler(handler);
   789 #endif
   790 
   791 	fprintf(fp, "// Dump of all datamaps for \"%s\" as at %s\n//\n//\n", g_pSM->GetGameFolderName(), buffer);
   792 
   793 
   794 	fprintf(fp, "// Flag Details:\n//\n");
   795 
   796 	fprintf(fp, "// Global: This field is masked for global entity save/restore\n");
   797 	fprintf(fp, "// Save: This field is saved to disk\n");
   798 	fprintf(fp, "// Key: This field can be requested and written to by string name at load time\n");
   799 	fprintf(fp, "// Input: This field can be written to by string name at run time, and a function called\n");
   800 	fprintf(fp, "// Output: This field propogates it's value to all targets whenever it changes\n");
   801 	fprintf(fp, "// FunctionTable: This is a table entry for a member function pointer\n");
   802 	fprintf(fp, "// Ptr: This field is a pointer, not an embedded object\n");
   803 	fprintf(fp, "// Override: The field is an override for one in a base class (only used by prediction system for now)\n");
   804 
   805 	fprintf(fp, "//\n\n");
   806 
   807 	static int offsEFlags = -1;
   808 	for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
   809 	{
   810 		IServerNetworkable *entity = dict->Create(dict->m_Factories.GetElementName(i));
   811 		ServerClass *sclass = entity->GetServerClass();
   812 		datamap_t *pMap = gamehelpers->GetDataMap(entity->GetBaseEntity());
   813 
   814 		fprintf(fp,"%s - %s\n", sclass->GetName(), dict->m_Factories.GetElementName(i));
   815 
   816 		UTIL_DrawDataTable(fp, pMap, 0);
   817 
   818 		if (offsEFlags == -1)
   819 		{
   820 			sm_datatable_info_t info;
   821 			if (!gamehelpers->FindDataMapInfo(pMap, "m_iEFlags", &info))
   822 			{
   823 				continue;
   824 			}
   825 
   826 			offsEFlags = info.actual_offset;
   827 		}
   828 		
   829 		int *eflags = (int *)((char *)entity->GetBaseEntity() + offsEFlags);
   830 		*eflags |= (1<<0); // EFL_KILLME
   831 	}
   832 
   833 	fclose(fp);
   834 
   835 }