3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "extension.h"
37 CallHelper s_Teleport;
38 CallHelper s_GetVelocity;
39 CallHelper s_EyeAngles;
41 class CTraceFilterSimple : public CTraceFilterEntitiesOnly
44 CTraceFilterSimple(const IHandleEntity *passentity): m_pPassEnt(passentity)
47 virtual bool ShouldHitEntity(IHandleEntity *pServerEntity, int contentsMask)
49 if (pServerEntity == m_pPassEnt)
56 const IHandleEntity *m_pPassEnt;
63 return s_Teleport.supported;
68 if (g_pGameConf->GetOffset("Teleport", &offset))
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;
75 s_Teleport.call = g_pBinTools->CreateVCall(offset, 0, 0, NULL, info, 3);
77 if (s_Teleport.call != NULL)
79 s_Teleport.supported = true;
83 s_Teleport.setup = true;
85 return s_Teleport.supported;
88 void Teleport(CBaseEntity *pEntity, Vector *origin, QAngle *ang, Vector *velocity)
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;
100 s_Teleport.call->Execute(params, NULL);
103 bool IsTeleportSupported()
105 return SetupTeleport();
108 bool SetupGetVelocity()
110 if (s_GetVelocity.setup)
112 return s_GetVelocity.supported;
116 if (g_pGameConf->GetOffset("GetVelocity", &offset))
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;
123 s_GetVelocity.call = g_pBinTools->CreateVCall(offset, 0, 0, NULL, info, 2);
125 if (s_GetVelocity.call != NULL)
127 s_GetVelocity.supported = true;
131 s_GetVelocity.setup = true;
133 return s_GetVelocity.supported;
136 void GetVelocity(CBaseEntity *pEntity, Vector *velocity, AngularImpulse *angvelocity)
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;
146 s_GetVelocity.call->Execute(params, NULL);
149 bool IsGetVelocitySupported()
151 return SetupGetVelocity();
154 bool SetupGetEyeAngles()
156 if (s_EyeAngles.setup)
158 return s_EyeAngles.supported;
162 if (g_pGameConf->GetOffset("EyeAngles", &offset))
165 info[0].flags = PASSFLAG_BYVAL;
166 info[0].size = sizeof(void *);
167 info[0].type = PassType_Basic;
169 s_EyeAngles.call = g_pBinTools->CreateVCall(offset, 0, 0, info, NULL, 0);
171 if (s_EyeAngles.call != NULL)
173 s_EyeAngles.supported = true;
177 s_EyeAngles.setup = true;
179 return s_EyeAngles.supported;
182 bool GetEyeAngles(CBaseEntity *pEntity, QAngle *pAngles)
184 if (!IsEyeAnglesSupported())
189 QAngle *pRetAngle = NULL;
190 unsigned char params[sizeof(void *)];
191 unsigned char *vptr = params;
193 *(CBaseEntity **)vptr = pEntity;
195 s_EyeAngles.call->Execute(params, &pRetAngle);
197 if (pRetAngle == NULL)
202 *pAngles = *pRetAngle;
207 int GetClientAimTarget(edict_t *pEdict, bool only_players)
209 CBaseEntity *pEntity = pEdict->GetUnknown() ? pEdict->GetUnknown()->GetBaseEntity() : NULL;
219 /* Get the private information we need */
220 serverClients->ClientEarPosition(pEdict, &eye_position);
221 if (!GetEyeAngles(pEntity, &eye_angles))
227 AngleVectors(eye_angles, &aim_dir);
228 VectorNormalize(aim_dir);
230 Vector vec_end = eye_position + aim_dir * 8000;
233 ray.Init(eye_position, vec_end);
236 CTraceFilterSimple simple(pEdict->GetIServerEntity());
238 enginetrace->TraceRay(ray, MASK_SOLID|CONTENTS_DEBRIS|CONTENTS_HITBOX, &simple, &tr);
240 if (tr.fraction == 1.0f || tr.m_pEnt == NULL)
245 int ent_ref = gamehelpers->EntityToBCompatRef(tr.m_pEnt);
246 int ent_index = gamehelpers->ReferenceToIndex(ent_ref);
248 IGamePlayer *pTargetPlayer = playerhelpers->GetGamePlayer(ent_index);
249 if (pTargetPlayer != NULL && !pTargetPlayer->IsInGame())
253 else if (only_players && pTargetPlayer == NULL)
261 bool IsEyeAnglesSupported()
263 return SetupGetEyeAngles();
266 bool GetPlayerInfo(int client, player_info_t *info)
268 #if SOURCE_ENGINE >= SE_ORANGEBOX
269 return engine->GetPlayerInfo(client, info);
271 return (iserver) ? iserver->GetPlayerInfo(client-1, info) : false;
275 void ShutdownHelpers()
277 s_Teleport.Shutdown();
278 s_GetVelocity.Shutdown();
279 s_EyeAngles.Shutdown();
282 bool FindNestedDataTable(SendTable *pTable, const char *name)
284 if (strcmp(pTable->GetName(), name) == 0)
289 int props = pTable->GetNumProps();
292 for (int i=0; i<props; i++)
294 prop = pTable->GetProp(i);
295 if (prop->GetDataTable())
297 if (FindNestedDataTable(prop->GetDataTable(), name))
307 void UTIL_DrawSendTable_XML(FILE *fp, SendTable *pTable, int space_count)
311 for (int i = 0; i < space_count; i++)
315 spaces[space_count] = '\0';
317 const char *type_name;
318 SendTable *pOtherTable;
321 fprintf(fp, " %s<sendtable name=\"%s\">\n", spaces, pTable->GetName());
322 for (int i = 0; i < pTable->GetNumProps(); i++)
324 pProp = pTable->GetProp(i);
326 fprintf(fp, " %s<property name=\"%s\">\n", spaces, pProp->GetName());
328 if ((type_name = GetDTTypeName(pProp->GetType())) != NULL)
330 fprintf(fp, " %s<type>%s</type>\n", spaces, type_name);
334 fprintf(fp, " %s<type>%d</type>\n", spaces, pProp->GetType());
337 fprintf(fp, " %s<offset>%d</offset>\n", spaces, pProp->GetOffset());
338 fprintf(fp, " %s<bits>%d</bits>\n", spaces, pProp->m_nBits);
340 if ((pOtherTable = pTable->GetProp(i)->GetDataTable()) != NULL)
342 UTIL_DrawSendTable_XML(fp, pOtherTable, space_count + 3);
345 fprintf(fp, " %s</property>\n", spaces);
347 fprintf(fp, " %s</sendtable>\n", spaces);
350 void UTIL_DrawServerClass_XML(FILE *fp, ServerClass *sc)
352 fprintf(fp, "<serverclass name=\"%s\">\n", sc->GetName());
353 UTIL_DrawSendTable_XML(fp, sc->m_pTable, 0);
354 fprintf(fp, "</serverclass>\n");
357 void UTIL_DrawSendTable(FILE *fp, SendTable *pTable, int level = 1)
362 for (int i = 0; i < pTable->GetNumProps(); i++)
364 pProp = pTable->GetProp(i);
365 if (pProp->GetDataTable())
367 fprintf(fp, "%*sTable: %s (offset %d) (type %s)\n",
371 pProp->GetDataTable()->GetName());
373 UTIL_DrawSendTable(fp, pProp->GetDataTable(), level + 1);
377 type = GetDTTypeName(pProp->GetType());
382 "%*sMember: %s (offset %d) (type %s) (bits %d)\n",
392 "%*sMember: %s (offset %d) (type %d) (bits %d)\n",
403 CON_COMMAND(sm_dump_netprops_xml, "Dumps the networkable property table as an XML file")
405 #if SOURCE_ENGINE <= SE_DARKMESSIAH
411 META_CONPRINT("Usage: sm_dump_netprops_xml <file>\n");
415 const char *file = args.Arg(1);
416 if (!file || file[0] == '\0')
418 META_CONPRINT("Usage: sm_dump_netprops_xml <file>\n");
422 char path[PLATFORM_MAX_PATH];
423 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
426 if ((fp = fopen(path, "wt")) == NULL)
428 META_CONPRINTF("Could not open file \"%s\"\n", path);
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());
435 ServerClass *pBase = gamedll->GetAllServerClasses();
436 while (pBase != NULL)
438 UTIL_DrawServerClass_XML(fp, pBase);
439 pBase = pBase->m_pNext;
445 CON_COMMAND(sm_dump_netprops, "Dumps the networkable property table as a text file")
447 #if SOURCE_ENGINE <= SE_DARKMESSIAH
453 META_CONPRINT("Usage: sm_dump_netprops <file>\n");
457 const char *file = args.Arg(1);
458 if (!file || file[0] == '\0')
460 META_CONPRINT("Usage: sm_dump_netprops <file>\n");
464 char path[PLATFORM_MAX_PATH];
465 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
468 if ((fp = fopen(path, "wt")) == NULL)
470 META_CONPRINTF("Could not open file \"%s\"\n", path);
474 fprintf(fp, "// Dump of all network properties for \"%s\" follows\n//\n\n", g_pSM->GetGameFolderName());
476 ServerClass *pBase = gamedll->GetAllServerClasses();
477 while (pBase != NULL)
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;
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,
496 /* Wow we don't care, thanks Microsoft. */
500 CON_COMMAND(sm_dump_classes, "Dumps the class list as a text file")
502 #if SOURCE_ENGINE <= SE_DARKMESSIAH
508 META_CONPRINT("Usage: sm_dump_classes <file>\n");
512 const char *file = args.Arg(1);
513 if (!file || file[0] == '\0')
515 META_CONPRINT("Usage: sm_dump_classes <file>\n");
519 ICallWrapper *pWrapper = NULL;
524 retData.flags = PASSFLAG_BYVAL;
525 retData.size = sizeof(void *);
526 retData.type = PassType_Basic;
529 if (!g_pGameConf->GetMemSig("EntityFactory", &addr) || addr == NULL)
531 META_CONPRINT("Failed to locate function\n");
535 pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retData, NULL, 0);
539 void *returnData = NULL;
541 pWrapper->Execute(NULL, &returnData);
545 if (returnData == NULL)
550 CEntityFactoryDictionary *dict = ( CEntityFactoryDictionary * )returnData;
557 char path[PLATFORM_MAX_PATH];
558 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
561 if ((fp = fopen(path, "wt")) == NULL)
563 META_CONPRINTF("Could not open file \"%s\"\n", path);
570 #if defined SUBPLATFORM_SECURECRT
571 _invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
574 time_t t = g_pSM->GetAdjustedTime();
575 size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
577 #if defined SUBPLATFORM_SECURECRT
578 _set_invalid_parameter_handler(handler);
581 fprintf(fp, "// Dump of all classes for \"%s\" as at %s\n//\n\n", g_pSM->GetGameFolderName(), buffer);
583 for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
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));
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
598 char *UTIL_FlagsToString(int flags)
600 static char str[1024];
603 if (flags & FTYPEDESC_GLOBAL)
605 strcat(str, "Global|");
607 if (flags & FTYPEDESC_SAVE)
609 strcat(str, "Save|");
611 if (flags & FTYPEDESC_KEY)
615 if (flags & FTYPEDESC_INPUT)
617 strcat(str, "Input|");
619 if (flags & FTYPEDESC_OUTPUT)
621 strcat(str, "Output|");
623 if (flags & FTYPEDESC_FUNCTIONTABLE)
625 strcat(str, "FunctionTable|");
627 if (flags & FTYPEDESC_PTR)
631 if (flags & FTYPEDESC_OVERRIDE)
633 strcat(str, "Override|");
636 int len = strlen(str) - 1;
639 str[len] = 0; // Strip the final '|'
645 void UTIL_DrawDataTable(FILE *fp, datamap_t *pMap, int level)
649 for (int i=0; i<level; i++)
654 spaces[level] = '\0';
656 const char *externalname;
661 for (int i=0; i<pMap->dataNumFields; i++)
663 if (pMap->dataDesc[i].fieldName == NULL)
668 if (pMap->dataDesc[i].td)
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);
675 externalname = pMap->dataDesc[i].externalName;
676 flags = UTIL_FlagsToString(pMap->dataDesc[i].flags);
678 if (externalname == NULL)
680 fprintf(fp,"%s- %s (%s)(%i Bytes)\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes);
684 fprintf(fp,"%s- %s (%s)(%i Bytes) - %s\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes, externalname);
688 pMap = pMap->baseMap;
692 CON_COMMAND(sm_dump_datamaps, "Dumps the data map list as a text file")
694 #if SOURCE_ENGINE <= SE_DARKMESSIAH
700 META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
704 const char *file = args.Arg(1);
705 if (!file || file[0] == '\0')
707 META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
711 static CEntityFactoryDictionary *dict = NULL;
715 if (g_pGameConf->GetMemSig("EntityFactoryFinder", (void **)&addr) && addr)
718 if (!g_pGameConf->GetOffset("EntityFactoryOffset", &offset) || !offset)
722 dict = *reinterpret_cast<CEntityFactoryDictionary **>((intptr_t)addr + offset);
728 ICallWrapper *pWrapper = NULL;
731 retData.flags = PASSFLAG_BYVAL;
732 retData.size = sizeof(void *);
733 retData.type = PassType_Basic;
736 if (g_pGameConf->GetMemSig("EntityFactory", &addr) && addr != NULL)
738 pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retData, NULL, 0);
743 void *returnData = NULL;
745 pWrapper->Execute(NULL, &returnData);
749 if (returnData == NULL)
754 dict = ( CEntityFactoryDictionary * )returnData;
760 META_CONPRINT("Failed to locate function\n");
764 char path[PLATFORM_MAX_PATH];
765 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
768 if ((fp = fopen(path, "wt")) == NULL)
770 META_CONPRINTF("Could not open file \"%s\"\n", path);
777 #if defined SUBPLATFORM_SECURECRT
778 _invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
781 time_t t = g_pSM->GetAdjustedTime();
782 size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
784 #if defined SUBPLATFORM_SECURECRT
785 _set_invalid_parameter_handler(handler);
788 fprintf(fp, "// Dump of all datamaps for \"%s\" as at %s\n//\n//\n", g_pSM->GetGameFolderName(), buffer);
791 fprintf(fp, "// Flag Details:\n//\n");
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");
802 fprintf(fp, "//\n\n");
804 static int offsEFlags = -1;
805 for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
807 IServerNetworkable *entity = dict->Create(dict->m_Factories.GetElementName(i));
808 ServerClass *sclass = entity->GetServerClass();
809 datamap_t *pMap = gamehelpers->GetDataMap(entity->GetBaseEntity());
811 fprintf(fp,"%s - %s\n", sclass->GetName(), dict->m_Factories.GetElementName(i));
813 UTIL_DrawDataTable(fp, pMap, 0);
815 if (offsEFlags == -1)
817 typedescription_t *datamap = gamehelpers->FindInDataMap(pMap, "m_iEFlags");
824 offsEFlags = GetTypeDescOffs(datamap);
827 int *eflags = (int *)((char *)entity->GetBaseEntity() + offsEFlags);
828 *eflags |= (1<<0); // EFL_KILLME