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 void UTIL_DrawSendTable_XML(FILE *fp, SendTable *pTable, int space_count)
286 for (int i = 0; i < space_count; i++)
290 spaces[space_count] = '\0';
292 const char *type_name;
293 SendTable *pOtherTable;
296 fprintf(fp, " %s<sendtable name=\"%s\">\n", spaces, pTable->GetName());
297 for (int i = 0; i < pTable->GetNumProps(); i++)
299 pProp = pTable->GetProp(i);
301 fprintf(fp, " %s<property name=\"%s\">\n", spaces, pProp->GetName());
303 if ((type_name = GetDTTypeName(pProp->GetType())) != NULL)
305 fprintf(fp, " %s<type>%s</type>\n", spaces, type_name);
309 fprintf(fp, " %s<type>%d</type>\n", spaces, pProp->GetType());
312 fprintf(fp, " %s<offset>%d</offset>\n", spaces, pProp->GetOffset());
313 fprintf(fp, " %s<bits>%d</bits>\n", spaces, pProp->m_nBits);
315 if ((pOtherTable = pTable->GetProp(i)->GetDataTable()) != NULL)
317 UTIL_DrawSendTable_XML(fp, pOtherTable, space_count + 3);
320 fprintf(fp, " %s</property>\n", spaces);
322 fprintf(fp, " %s</sendtable>\n", spaces);
325 void UTIL_DrawServerClass_XML(FILE *fp, ServerClass *sc)
327 fprintf(fp, "<serverclass name=\"%s\">\n", sc->GetName());
328 UTIL_DrawSendTable_XML(fp, sc->m_pTable, 0);
329 fprintf(fp, "</serverclass>\n");
332 void UTIL_DrawSendTable(FILE *fp, SendTable *pTable, int level)
335 for (int i=0; i<level; i++)
337 spaces[level] = '\0';
339 const char *name, *type;
342 fprintf(fp, "%sSub-Class Table (%d Deep): %s\n", spaces, level, pTable->GetName());
344 for (int i=0; i<pTable->GetNumProps(); i++)
346 pProp = pTable->GetProp(i);
347 name = pProp->GetName();
348 if (pProp->GetDataTable())
350 UTIL_DrawSendTable(fp, pProp->GetDataTable(), level + 1);
354 type = GetDTTypeName(pProp->GetType());
359 "%s-Member: %s (offset %d) (type %s) (bits %d)\n",
369 "%s-Member: %s (offset %d) (type %d) (bits %d)\n",
380 CON_COMMAND(sm_dump_netprops_xml, "Dumps the networkable property table as an XML file")
382 #if SOURCE_ENGINE <= SE_DARKMESSIAH
388 META_CONPRINT("Usage: sm_dump_netprops_xml <file>\n");
392 const char *file = args.Arg(1);
393 if (!file || file[0] == '\0')
395 META_CONPRINT("Usage: sm_dump_netprops_xml <file>\n");
399 char path[PLATFORM_MAX_PATH];
400 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
403 if ((fp = fopen(path, "wt")) == NULL)
405 META_CONPRINTF("Could not open file \"%s\"\n", path);
409 fprintf(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
410 fprintf(fp, "<!-- Dump of all network properties for \"%s\" follows -->\n\n", g_pSM->GetGameFolderName());
412 ServerClass *pBase = gamedll->GetAllServerClasses();
413 while (pBase != NULL)
415 UTIL_DrawServerClass_XML(fp, pBase);
416 pBase = pBase->m_pNext;
422 CON_COMMAND(sm_dump_netprops, "Dumps the networkable property table as a text file")
424 #if SOURCE_ENGINE <= SE_DARKMESSIAH
430 META_CONPRINT("Usage: sm_dump_netprops <file>\n");
434 const char *file = args.Arg(1);
435 if (!file || file[0] == '\0')
437 META_CONPRINT("Usage: sm_dump_netprops <file>\n");
441 char path[PLATFORM_MAX_PATH];
442 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
445 if ((fp = fopen(path, "wt")) == NULL)
447 META_CONPRINTF("Could not open file \"%s\"\n", path);
451 fprintf(fp, "// Dump of all network properties for \"%s\" follows\n//\n\n", g_pSM->GetGameFolderName());
453 ServerClass *pBase = gamedll->GetAllServerClasses();
454 while (pBase != NULL)
456 fprintf(fp, "%s:\n", pBase->GetName());
457 UTIL_DrawSendTable(fp, pBase->m_pTable, 1);
458 pBase = pBase->m_pNext;
464 #if defined SUBPLATFORM_SECURECRT
465 void _ignore_invalid_parameter(
466 const wchar_t * expression,
467 const wchar_t * function,
468 const wchar_t * file,
473 /* Wow we don't care, thanks Microsoft. */
477 CON_COMMAND(sm_dump_classes, "Dumps the class list as a text file")
479 #if SOURCE_ENGINE <= SE_DARKMESSIAH
485 META_CONPRINT("Usage: sm_dump_classes <file>\n");
489 const char *file = args.Arg(1);
490 if (!file || file[0] == '\0')
492 META_CONPRINT("Usage: sm_dump_classes <file>\n");
496 ICallWrapper *pWrapper = NULL;
501 retData.flags = PASSFLAG_BYVAL;
502 retData.size = sizeof(void *);
503 retData.type = PassType_Basic;
506 if (!g_pGameConf->GetMemSig("EntityFactory", &addr) || addr == NULL)
508 META_CONPRINT("Failed to locate function\n");
512 pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retData, NULL, 0);
516 void *returnData = NULL;
518 pWrapper->Execute(NULL, &returnData);
522 if (returnData == NULL)
527 CEntityFactoryDictionary *dict = ( CEntityFactoryDictionary * )returnData;
534 char path[PLATFORM_MAX_PATH];
535 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
538 if ((fp = fopen(path, "wt")) == NULL)
540 META_CONPRINTF("Could not open file \"%s\"\n", path);
547 #if defined SUBPLATFORM_SECURECRT
548 _invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
551 time_t t = g_pSM->GetAdjustedTime();
552 size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
554 #if defined SUBPLATFORM_SECURECRT
555 _set_invalid_parameter_handler(handler);
558 fprintf(fp, "// Dump of all classes for \"%s\" as at %s\n//\n\n", g_pSM->GetGameFolderName(), buffer);
560 for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
562 IServerNetworkable *entity = dict->Create(dict->m_Factories.GetElementName(i));
563 ServerClass *sclass = entity->GetServerClass();
564 fprintf(fp,"%s - %s\n",sclass->GetName(), dict->m_Factories.GetElementName(i));
566 typedescription_t *datamap = gamehelpers->FindInDataMap(gamehelpers->GetDataMap(entity->GetBaseEntity()), "m_iEFlags");
567 int *eflags = (int *)((char *)entity->GetBaseEntity() + GetTypeDescOffs(datamap));
568 *eflags |= (1<<0); // EFL_KILLME
575 char *UTIL_FlagsToString(int flags)
577 static char str[1024];
580 if (flags & FTYPEDESC_GLOBAL)
582 strcat(str, "Global|");
584 if (flags & FTYPEDESC_SAVE)
586 strcat(str, "Save|");
588 if (flags & FTYPEDESC_KEY)
592 if (flags & FTYPEDESC_INPUT)
594 strcat(str, "Input|");
596 if (flags & FTYPEDESC_OUTPUT)
598 strcat(str, "Output|");
600 if (flags & FTYPEDESC_FUNCTIONTABLE)
602 strcat(str, "FunctionTable|");
604 if (flags & FTYPEDESC_PTR)
608 if (flags & FTYPEDESC_OVERRIDE)
610 strcat(str, "Override|");
613 int len = strlen(str) - 1;
616 str[len] = 0; // Strip the final '|'
622 void UTIL_DrawDataTable(FILE *fp, datamap_t *pMap, int level)
626 for (int i=0; i<level; i++)
631 spaces[level] = '\0';
633 const char *externalname;
638 for (int i=0; i<pMap->dataNumFields; i++)
640 if (pMap->dataDesc[i].fieldName == NULL)
645 if (pMap->dataDesc[i].td)
647 fprintf(fp, " %sSub-Class Table (%d Deep): %s - %s\n", spaces, level+1, pMap->dataDesc[i].fieldName, pMap->dataDesc[i].td->dataClassName);
648 UTIL_DrawDataTable(fp, pMap->dataDesc[i].td, level+1);
652 externalname = pMap->dataDesc[i].externalName;
653 flags = UTIL_FlagsToString(pMap->dataDesc[i].flags);
655 if (externalname == NULL)
657 fprintf(fp,"%s- %s (%s)(%i Bytes)\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes);
661 fprintf(fp,"%s- %s (%s)(%i Bytes) - %s\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes, externalname);
665 pMap = pMap->baseMap;
669 CON_COMMAND(sm_dump_datamaps, "Dumps the data map list as a text file")
671 #if SOURCE_ENGINE <= SE_DARKMESSIAH
677 META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
681 const char *file = args.Arg(1);
682 if (!file || file[0] == '\0')
684 META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
688 ICallWrapper *pWrapper = NULL;
693 retData.flags = PASSFLAG_BYVAL;
694 retData.size = sizeof(void *);
695 retData.type = PassType_Basic;
698 if (!g_pGameConf->GetMemSig("EntityFactory", &addr) || addr == NULL)
700 META_CONPRINT("Failed to locate function\n");
704 pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retData, NULL, 0);
708 void *returnData = NULL;
710 pWrapper->Execute(NULL, &returnData);
714 if (returnData == NULL)
719 CEntityFactoryDictionary *dict = ( CEntityFactoryDictionary * )returnData;
726 char path[PLATFORM_MAX_PATH];
727 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
730 if ((fp = fopen(path, "wt")) == NULL)
732 META_CONPRINTF("Could not open file \"%s\"\n", path);
739 #if defined SUBPLATFORM_SECURECRT
740 _invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
743 time_t t = g_pSM->GetAdjustedTime();
744 size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
746 #if defined SUBPLATFORM_SECURECRT
747 _set_invalid_parameter_handler(handler);
750 fprintf(fp, "// Dump of all datamaps for \"%s\" as at %s\n//\n//\n", g_pSM->GetGameFolderName(), buffer);
753 fprintf(fp, "// Flag Details:\n//\n");
755 fprintf(fp, "// Global: This field is masked for global entity save/restore\n");
756 fprintf(fp, "// Save: This field is saved to disk\n");
757 fprintf(fp, "// Key: This field can be requested and written to by string name at load time\n");
758 fprintf(fp, "// Input: This field can be written to by string name at run time, and a function called\n");
759 fprintf(fp, "// Output: This field propogates it's value to all targets whenever it changes\n");
760 fprintf(fp, "// FunctionTable: This is a table entry for a member function pointer\n");
761 fprintf(fp, "// Ptr: This field is a pointer, not an embedded object\n");
762 fprintf(fp, "// Override: The field is an override for one in a base class (only used by prediction system for now)\n");
764 fprintf(fp, "//\n\n");
766 for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
768 IServerNetworkable *entity = dict->Create(dict->m_Factories.GetElementName(i));
769 ServerClass *sclass = entity->GetServerClass();
770 datamap_t *pMap = gamehelpers->GetDataMap(entity->GetBaseEntity());
772 fprintf(fp,"%s - %s\n", sclass->GetName(), dict->m_Factories.GetElementName(i));
774 UTIL_DrawDataTable(fp, pMap, 0);
776 typedescription_t *datamap = gamehelpers->FindInDataMap(pMap, "m_iEFlags");
783 int *eflags = (int *)((char *)entity->GetBaseEntity() + GetTypeDescOffs(datamap));
784 *eflags |= (1<<0); // EFL_KILLME