extensions/sdktools/vhelpers.cpp
author Peace-Maker <januk@freenet.de>
Tue Mar 19 11:25:04 2013 -0400 (2013-03-19)
changeset 3859 4e43bcc83a54
parent 3858 1f2595deaf77
child 3913 a73cf1f26984
permissions -rw-r--r--
Add alternative lookup method for EntityFactoryDictionary (bug 5658, r=asherkin).
     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 	serverClients->ClientEarPosition(pEdict, &eye_position);
   221 	if (!GetEyeAngles(pEntity, &eye_angles))
   222 	{
   223 		return -2;
   224 	}
   225 
   226 	Vector aim_dir;
   227 	AngleVectors(eye_angles, &aim_dir);
   228 	VectorNormalize(aim_dir);
   229 
   230 	Vector vec_end = eye_position + aim_dir * 8000;
   231 
   232 	Ray_t ray;
   233 	ray.Init(eye_position, vec_end);
   234 
   235 	trace_t tr;
   236 	CTraceFilterSimple simple(pEdict->GetIServerEntity());
   237 
   238 	enginetrace->TraceRay(ray, MASK_SOLID|CONTENTS_DEBRIS|CONTENTS_HITBOX, &simple, &tr);
   239 
   240 	if (tr.fraction == 1.0f || tr.m_pEnt == NULL)
   241 	{
   242 		return -1;
   243 	}
   244 
   245 	int ent_ref = gamehelpers->EntityToBCompatRef(tr.m_pEnt);
   246 	int ent_index = gamehelpers->ReferenceToIndex(ent_ref);
   247 
   248 	IGamePlayer *pTargetPlayer = playerhelpers->GetGamePlayer(ent_index);
   249 	if (pTargetPlayer != NULL && !pTargetPlayer->IsInGame())
   250 	{
   251 		return -1;
   252 	}
   253 	else if (only_players && pTargetPlayer == NULL)
   254 	{
   255 		return -1;
   256 	}
   257 
   258 	return ent_ref;
   259 }
   260 
   261 bool IsEyeAnglesSupported()
   262 {
   263 	return SetupGetEyeAngles();
   264 }
   265 
   266 bool GetPlayerInfo(int client, player_info_t *info)
   267 {
   268 #if SOURCE_ENGINE >= SE_ORANGEBOX
   269 	return engine->GetPlayerInfo(client, info);
   270 #else
   271 	return (iserver) ? iserver->GetPlayerInfo(client-1, info) : false;
   272 #endif
   273 }
   274 
   275 void ShutdownHelpers()
   276 {
   277 	s_Teleport.Shutdown();
   278 	s_GetVelocity.Shutdown();
   279 	s_EyeAngles.Shutdown();
   280 }
   281 
   282 bool FindNestedDataTable(SendTable *pTable, const char *name)
   283 {
   284 	if (strcmp(pTable->GetName(), name) == 0)
   285 	{
   286 		return true;
   287 	}
   288 
   289 	int props = pTable->GetNumProps();
   290 	SendProp *prop;
   291 
   292 	for (int i=0; i<props; i++)
   293 	{
   294 		prop = pTable->GetProp(i);
   295 		if (prop->GetDataTable())
   296 		{
   297 			if (FindNestedDataTable(prop->GetDataTable(), name))
   298 			{
   299 				return true;
   300 			}
   301 		}
   302 	}
   303 
   304 	return false;
   305 }
   306 
   307 void UTIL_DrawSendTable_XML(FILE *fp, SendTable *pTable, int space_count)
   308 {
   309 	char spaces[255];
   310 
   311 	for (int i = 0; i < space_count; i++)
   312 	{
   313 		spaces[i] = ' ';
   314 	}
   315 	spaces[space_count] = '\0';
   316 
   317 	const char *type_name;
   318 	SendTable *pOtherTable;
   319 	SendProp *pProp;
   320 
   321 	fprintf(fp, " %s<sendtable name=\"%s\">\n", spaces, pTable->GetName());
   322 	for (int i = 0; i < pTable->GetNumProps(); i++)
   323 	{
   324 		pProp = pTable->GetProp(i);
   325 
   326 		fprintf(fp, "  %s<property name=\"%s\">\n", spaces, pProp->GetName());
   327 
   328 		if ((type_name = GetDTTypeName(pProp->GetType())) != NULL)
   329 		{
   330 			fprintf(fp, "   %s<type>%s</type>\n", spaces, type_name);
   331 		}
   332 		else
   333 		{
   334 			fprintf(fp, "   %s<type>%d</type>\n", spaces, pProp->GetType());
   335 		}
   336 
   337 		fprintf(fp, "   %s<offset>%d</offset>\n", spaces, pProp->GetOffset());
   338 		fprintf(fp, "   %s<bits>%d</bits>\n", spaces, pProp->m_nBits);
   339 
   340 		if ((pOtherTable = pTable->GetProp(i)->GetDataTable()) != NULL)
   341 		{
   342 			UTIL_DrawSendTable_XML(fp, pOtherTable, space_count + 3);
   343 		}
   344 
   345 		fprintf(fp, "  %s</property>\n", spaces);
   346 	}
   347 	fprintf(fp, " %s</sendtable>\n", spaces);
   348 }
   349 
   350 void UTIL_DrawServerClass_XML(FILE *fp, ServerClass *sc)
   351 {
   352 	fprintf(fp, "<serverclass name=\"%s\">\n", sc->GetName());
   353 	UTIL_DrawSendTable_XML(fp, sc->m_pTable, 0);
   354 	fprintf(fp, "</serverclass>\n");
   355 }
   356 
   357 void UTIL_DrawSendTable(FILE *fp, SendTable *pTable, int level = 1)
   358 {
   359 	SendProp *pProp;
   360 	const char *type;
   361 
   362 	for (int i = 0; i < pTable->GetNumProps(); i++)
   363 	{
   364 		pProp = pTable->GetProp(i);
   365 		if (pProp->GetDataTable())
   366 		{
   367 			fprintf(fp, "%*sTable: %s (offset %d) (type %s)\n", 
   368 				level, "", 
   369 				pProp->GetName(), 
   370 				pProp->GetOffset(), 
   371 				pProp->GetDataTable()->GetName());
   372 			
   373 			UTIL_DrawSendTable(fp, pProp->GetDataTable(), level + 1);
   374 		}
   375 		else
   376 		{
   377 			type = GetDTTypeName(pProp->GetType());
   378 
   379 			if (type != NULL)
   380 			{
   381 				fprintf(fp,
   382 					"%*sMember: %s (offset %d) (type %s) (bits %d)\n", 
   383 					level, "", 
   384 					pProp->GetName(),
   385 					pProp->GetOffset(),
   386 					type,
   387 					pProp->m_nBits);
   388 			}
   389 			else
   390 			{
   391 				fprintf(fp,
   392 					"%*sMember: %s (offset %d) (type %d) (bits %d)\n", 
   393 					level, "", 
   394 					pProp->GetName(),
   395 					pProp->GetOffset(),
   396 					pProp->GetType(),
   397 					pProp->m_nBits);
   398 			}
   399 		}
   400 	}
   401 }
   402 
   403 CON_COMMAND(sm_dump_netprops_xml, "Dumps the networkable property table as an XML file")
   404 {
   405 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   406 	CCommand args;
   407 #endif
   408 
   409 	if (args.ArgC() < 2)
   410 	{
   411 		META_CONPRINT("Usage: sm_dump_netprops_xml <file>\n");
   412 		return;
   413 	}
   414 
   415 	const char *file = args.Arg(1);
   416 	if (!file || file[0] == '\0')
   417 	{
   418 		META_CONPRINT("Usage: sm_dump_netprops_xml <file>\n");
   419 		return;
   420 	}
   421 
   422 	char path[PLATFORM_MAX_PATH];
   423 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
   424 
   425 	FILE *fp = NULL;
   426 	if ((fp = fopen(path, "wt")) == NULL)
   427 	{
   428 		META_CONPRINTF("Could not open file \"%s\"\n", path);
   429 		return;
   430 	}
   431 
   432 	fprintf(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
   433 	fprintf(fp, "<!-- Dump of all network properties for \"%s\" follows -->\n\n", g_pSM->GetGameFolderName());
   434 
   435 	ServerClass *pBase = gamedll->GetAllServerClasses();
   436 	while (pBase != NULL)
   437 	{
   438 		UTIL_DrawServerClass_XML(fp, pBase);
   439 		pBase = pBase->m_pNext;
   440 	}
   441 
   442 	fclose(fp);
   443 }
   444 
   445 CON_COMMAND(sm_dump_netprops, "Dumps the networkable property table as a text file")
   446 {
   447 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   448 	CCommand args;
   449 #endif
   450 
   451 	if (args.ArgC() < 2)
   452 	{
   453 		META_CONPRINT("Usage: sm_dump_netprops <file>\n");
   454 		return;
   455 	}
   456 
   457 	const char *file = args.Arg(1);
   458 	if (!file || file[0] == '\0')
   459 	{
   460 		META_CONPRINT("Usage: sm_dump_netprops <file>\n");
   461 		return;
   462 	}
   463 
   464 	char path[PLATFORM_MAX_PATH];
   465 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
   466 
   467 	FILE *fp = NULL;
   468 	if ((fp = fopen(path, "wt")) == NULL)
   469 	{
   470 		META_CONPRINTF("Could not open file \"%s\"\n", path);
   471 		return;
   472 	}
   473 
   474 	fprintf(fp, "// Dump of all network properties for \"%s\" follows\n//\n\n", g_pSM->GetGameFolderName());
   475 
   476 	ServerClass *pBase = gamedll->GetAllServerClasses();
   477 	while (pBase != NULL)
   478 	{
   479 		fprintf(fp, "%s (type %s)\n", pBase->GetName(), pBase->m_pTable->GetName());
   480 		UTIL_DrawSendTable(fp, pBase->m_pTable);
   481 		pBase = pBase->m_pNext;
   482 	}
   483 
   484 	fclose(fp);
   485 }
   486 
   487 #if defined SUBPLATFORM_SECURECRT
   488 void _ignore_invalid_parameter(
   489 						const wchar_t * expression,
   490 						const wchar_t * function, 
   491 						const wchar_t * file, 
   492 						unsigned int line,
   493 						uintptr_t pReserved
   494 						)
   495 {
   496 	/* Wow we don't care, thanks Microsoft. */
   497 }
   498 #endif
   499 
   500 CON_COMMAND(sm_dump_classes, "Dumps the class list as a text file")
   501 {
   502 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   503 	CCommand args;
   504 #endif
   505 
   506 	if (args.ArgC() < 2)
   507 	{
   508 		META_CONPRINT("Usage: sm_dump_classes <file>\n");
   509 		return;
   510 	}
   511 
   512 	const char *file = args.Arg(1);
   513 	if (!file || file[0] == '\0')
   514 	{
   515 		META_CONPRINT("Usage: sm_dump_classes <file>\n");
   516 		return;
   517 	}
   518 
   519 	ICallWrapper *pWrapper = NULL;
   520 
   521 	if (!pWrapper)
   522 	{
   523 		PassInfo retData;
   524 		retData.flags = PASSFLAG_BYVAL;
   525 		retData.size = sizeof(void *);
   526 		retData.type = PassType_Basic;
   527 
   528 		void *addr;
   529 		if (!g_pGameConf->GetMemSig("EntityFactory", &addr) || addr == NULL)
   530 		{
   531 			META_CONPRINT("Failed to locate function\n");
   532 			return;
   533 		}
   534 
   535 		pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retData, NULL, 0);
   536 	}
   537 
   538 
   539 	void *returnData = NULL;
   540 
   541 	pWrapper->Execute(NULL, &returnData);
   542 
   543 	pWrapper->Destroy();
   544 
   545 	if (returnData == NULL)
   546 	{
   547 		return;
   548 	}
   549 
   550 	CEntityFactoryDictionary *dict = ( CEntityFactoryDictionary * )returnData;
   551 
   552 	if ( !dict )
   553 	{
   554 		return;
   555 	}
   556 
   557 	char path[PLATFORM_MAX_PATH];
   558 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
   559 
   560 	FILE *fp = NULL;
   561 	if ((fp = fopen(path, "wt")) == NULL)
   562 	{
   563 		META_CONPRINTF("Could not open file \"%s\"\n", path);
   564 		return;
   565 	}
   566 
   567 	char buffer[80];
   568 	buffer[0] = 0;
   569 
   570 #if defined SUBPLATFORM_SECURECRT
   571 	_invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
   572 #endif
   573 
   574 	time_t t = g_pSM->GetAdjustedTime();
   575 	size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
   576 
   577 #if defined SUBPLATFORM_SECURECRT
   578 	_set_invalid_parameter_handler(handler);
   579 #endif
   580 
   581 	fprintf(fp, "// Dump of all classes for \"%s\" as at %s\n//\n\n", g_pSM->GetGameFolderName(), buffer);
   582 
   583 	for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
   584 	{
   585 		IServerNetworkable *entity = dict->Create(dict->m_Factories.GetElementName(i));
   586 		ServerClass *sclass = entity->GetServerClass();
   587 		fprintf(fp,"%s - %s\n",sclass->GetName(), dict->m_Factories.GetElementName(i));
   588 
   589 		typedescription_t *datamap = gamehelpers->FindInDataMap(gamehelpers->GetDataMap(entity->GetBaseEntity()), "m_iEFlags");
   590 		int *eflags = (int *)((char *)entity->GetBaseEntity() + GetTypeDescOffs(datamap));
   591 		*eflags |= (1<<0); // EFL_KILLME
   592 	}
   593 
   594 	fclose(fp);
   595 
   596 }
   597 
   598 char *UTIL_FlagsToString(int flags)
   599 {
   600 	static char str[1024];
   601 	str[0] = 0;
   602 
   603 	if (flags & FTYPEDESC_GLOBAL)
   604 	{
   605 		strcat(str,	"Global|");
   606 	}
   607 	if (flags & FTYPEDESC_SAVE)
   608 	{
   609 		strcat(str,	"Save|");
   610 	}
   611 	if (flags & FTYPEDESC_KEY)
   612 	{
   613 		strcat(str,	"Key|");
   614 	}
   615 	if (flags & FTYPEDESC_INPUT)
   616 	{
   617 		strcat(str,	"Input|");
   618 	}
   619 	if (flags & FTYPEDESC_OUTPUT)
   620 	{
   621 		strcat(str,	"Output|");
   622 	}
   623 	if (flags & FTYPEDESC_FUNCTIONTABLE)
   624 	{
   625 		strcat(str,	"FunctionTable|");
   626 	}
   627 	if (flags & FTYPEDESC_PTR)
   628 	{
   629 		strcat(str,	"Ptr|");
   630 	}
   631 	if (flags & FTYPEDESC_OVERRIDE)
   632 	{
   633 		strcat(str,	"Override|");
   634 	}
   635 
   636 	int len = strlen(str) - 1;
   637 	if (len > 0)
   638 	{
   639 		str[len] = 0; // Strip the final '|'
   640 	}
   641 
   642 	return str;
   643 }
   644 
   645 void UTIL_DrawDataTable(FILE *fp, datamap_t *pMap, int level)
   646 {
   647 	char spaces[255];
   648 
   649 	for (int i=0; i<level; i++)
   650 	{
   651 		spaces[i] = ' ';
   652 	}
   653 
   654 	spaces[level] = '\0';
   655 
   656 	const char *externalname;
   657 	char *flags;
   658 
   659 	while (pMap)
   660 	{
   661 		for (int i=0; i<pMap->dataNumFields; i++)
   662 		{
   663 			if (pMap->dataDesc[i].fieldName == NULL)
   664 			{
   665 				continue;
   666 			}
   667 
   668 			if (pMap->dataDesc[i].td)
   669 			{
   670 				fprintf(fp, " %sSub-Class Table (%d Deep): %s - %s\n", spaces, level+1, pMap->dataDesc[i].fieldName, pMap->dataDesc[i].td->dataClassName);
   671 				UTIL_DrawDataTable(fp, pMap->dataDesc[i].td, level+1);
   672 			}
   673 			else
   674 			{
   675 				externalname  = pMap->dataDesc[i].externalName;
   676 				flags = UTIL_FlagsToString(pMap->dataDesc[i].flags);
   677 
   678 				if (externalname == NULL)
   679 				{
   680 					fprintf(fp,"%s- %s (%s)(%i Bytes)\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes);
   681 				}
   682 				else
   683 				{
   684 					fprintf(fp,"%s- %s (%s)(%i Bytes) - %s\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes, externalname);
   685 				}
   686 			}
   687 		}
   688 		pMap = pMap->baseMap;
   689 	}
   690 }
   691 
   692 CON_COMMAND(sm_dump_datamaps, "Dumps the data map list as a text file")
   693 {
   694 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   695 	CCommand args;
   696 #endif
   697 
   698 	if (args.ArgC() < 2)
   699 	{
   700 		META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
   701 		return;
   702 	}
   703 
   704 	const char *file = args.Arg(1);
   705 	if (!file || file[0] == '\0')
   706 	{
   707 		META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
   708 		return;
   709 	}
   710 
   711 	static CEntityFactoryDictionary *dict = NULL;
   712 	if (dict == NULL)
   713 	{
   714 		void *addr;
   715 		if (g_pGameConf->GetMemSig("EntityFactoryFinder", (void **)&addr) && addr)
   716 		{
   717 			int offset;
   718 			if (!g_pGameConf->GetOffset("EntityFactoryOffset", &offset) || !offset)
   719 			{
   720 				return;
   721 			}
   722 			dict = *reinterpret_cast<CEntityFactoryDictionary **>((intptr_t)addr + offset);
   723 		}
   724 	}
   725 
   726 	if (dict == NULL)
   727 	{
   728 		ICallWrapper *pWrapper = NULL;
   729 
   730 		PassInfo retData;
   731 		retData.flags = PASSFLAG_BYVAL;
   732 		retData.size = sizeof(void *);
   733 		retData.type = PassType_Basic;
   734 
   735 		void *addr;
   736 		if (g_pGameConf->GetMemSig("EntityFactory", &addr) && addr != NULL)
   737 		{
   738 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retData, NULL, 0);
   739 		}
   740 
   741 		if (pWrapper)
   742 		{
   743 			void *returnData = NULL;
   744 
   745 			pWrapper->Execute(NULL, &returnData);
   746 
   747 			pWrapper->Destroy();
   748 
   749 			if (returnData == NULL)
   750 			{
   751 				return;
   752 			}
   753 
   754 			dict = ( CEntityFactoryDictionary * )returnData;
   755 		}
   756 	}
   757 
   758 	if ( dict == NULL )
   759 	{
   760 		META_CONPRINT("Failed to locate function\n");
   761 		return;
   762 	}
   763 
   764 	char path[PLATFORM_MAX_PATH];
   765 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
   766 
   767 	FILE *fp = NULL;
   768 	if ((fp = fopen(path, "wt")) == NULL)
   769 	{
   770 		META_CONPRINTF("Could not open file \"%s\"\n", path);
   771 		return;
   772 	}
   773 
   774 	char buffer[80];
   775 	buffer[0] = 0;
   776 
   777 #if defined SUBPLATFORM_SECURECRT
   778 	_invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
   779 #endif
   780 
   781 	time_t t = g_pSM->GetAdjustedTime();
   782 	size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
   783 
   784 #if defined SUBPLATFORM_SECURECRT
   785 	_set_invalid_parameter_handler(handler);
   786 #endif
   787 
   788 	fprintf(fp, "// Dump of all datamaps for \"%s\" as at %s\n//\n//\n", g_pSM->GetGameFolderName(), buffer);
   789 
   790 
   791 	fprintf(fp, "// Flag Details:\n//\n");
   792 
   793 	fprintf(fp, "// Global: This field is masked for global entity save/restore\n");
   794 	fprintf(fp, "// Save: This field is saved to disk\n");
   795 	fprintf(fp, "// Key: This field can be requested and written to by string name at load time\n");
   796 	fprintf(fp, "// Input: This field can be written to by string name at run time, and a function called\n");
   797 	fprintf(fp, "// Output: This field propogates it's value to all targets whenever it changes\n");
   798 	fprintf(fp, "// FunctionTable: This is a table entry for a member function pointer\n");
   799 	fprintf(fp, "// Ptr: This field is a pointer, not an embedded object\n");
   800 	fprintf(fp, "// Override: The field is an override for one in a base class (only used by prediction system for now)\n");
   801 
   802 	fprintf(fp, "//\n\n");
   803 
   804 	static int offsEFlags = -1;
   805 	for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
   806 	{
   807 		IServerNetworkable *entity = dict->Create(dict->m_Factories.GetElementName(i));
   808 		ServerClass *sclass = entity->GetServerClass();
   809 		datamap_t *pMap = gamehelpers->GetDataMap(entity->GetBaseEntity());
   810 
   811 		fprintf(fp,"%s - %s\n", sclass->GetName(), dict->m_Factories.GetElementName(i));
   812 
   813 		UTIL_DrawDataTable(fp, pMap, 0);
   814 
   815 		if (offsEFlags == -1)
   816 		{
   817 			typedescription_t *datamap = gamehelpers->FindInDataMap(pMap, "m_iEFlags");
   818 		
   819 			if (!datamap)
   820 			{
   821 				continue;
   822 			}
   823 
   824 			offsEFlags = GetTypeDescOffs(datamap);
   825 		}
   826 		
   827 		int *eflags = (int *)((char *)entity->GetBaseEntity() + offsEFlags);
   828 		*eflags |= (1<<0); // EFL_KILLME
   829 	}
   830 
   831 	fclose(fp);
   832 
   833 }