Updated for latest hl2sdk-dota changes.
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 #if SOURCE_ENGINE == SE_DOTA
221 serverClients->ClientEarPosition(IndexOfEdict(pEdict), &eye_position);
223 serverClients->ClientEarPosition(pEdict, &eye_position);
226 if (!GetEyeAngles(pEntity, &eye_angles))
232 AngleVectors(eye_angles, &aim_dir);
233 VectorNormalize(aim_dir);
235 Vector vec_end = eye_position + aim_dir * 8000;
238 ray.Init(eye_position, vec_end);
241 CTraceFilterSimple simple(pEdict->GetIServerEntity());
243 enginetrace->TraceRay(ray, MASK_SOLID|CONTENTS_DEBRIS|CONTENTS_HITBOX, &simple, &tr);
245 if (tr.fraction == 1.0f || tr.m_pEnt == NULL)
250 int ent_ref = gamehelpers->EntityToBCompatRef(tr.m_pEnt);
251 int ent_index = gamehelpers->ReferenceToIndex(ent_ref);
253 IGamePlayer *pTargetPlayer = playerhelpers->GetGamePlayer(ent_index);
254 if (pTargetPlayer != NULL && !pTargetPlayer->IsInGame())
258 else if (only_players && pTargetPlayer == NULL)
266 bool IsEyeAnglesSupported()
268 return SetupGetEyeAngles();
271 bool GetPlayerInfo(int client, player_info_t *info)
273 #if SOURCE_ENGINE >= SE_ORANGEBOX
274 return engine->GetPlayerInfo(client, info);
276 return (iserver) ? iserver->GetPlayerInfo(client-1, info) : false;
280 void ShutdownHelpers()
282 s_Teleport.Shutdown();
283 s_GetVelocity.Shutdown();
284 s_EyeAngles.Shutdown();
287 bool FindNestedDataTable(SendTable *pTable, const char *name)
289 if (strcmp(pTable->GetName(), name) == 0)
294 int props = pTable->GetNumProps();
297 for (int i=0; i<props; i++)
299 prop = pTable->GetProp(i);
300 if (prop->GetDataTable())
302 if (FindNestedDataTable(prop->GetDataTable(), name))
312 void UTIL_DrawSendTable_XML(FILE *fp, SendTable *pTable, int space_count)
316 for (int i = 0; i < space_count; i++)
320 spaces[space_count] = '\0';
322 const char *type_name;
323 SendTable *pOtherTable;
326 fprintf(fp, " %s<sendtable name=\"%s\">\n", spaces, pTable->GetName());
327 for (int i = 0; i < pTable->GetNumProps(); i++)
329 pProp = pTable->GetProp(i);
331 fprintf(fp, " %s<property name=\"%s\">\n", spaces, pProp->GetName());
333 if ((type_name = GetDTTypeName(pProp->GetType())) != NULL)
335 fprintf(fp, " %s<type>%s</type>\n", spaces, type_name);
339 fprintf(fp, " %s<type>%d</type>\n", spaces, pProp->GetType());
342 fprintf(fp, " %s<offset>%d</offset>\n", spaces, pProp->GetOffset());
343 fprintf(fp, " %s<bits>%d</bits>\n", spaces, pProp->m_nBits);
345 if ((pOtherTable = pTable->GetProp(i)->GetDataTable()) != NULL)
347 UTIL_DrawSendTable_XML(fp, pOtherTable, space_count + 3);
350 fprintf(fp, " %s</property>\n", spaces);
352 fprintf(fp, " %s</sendtable>\n", spaces);
355 void UTIL_DrawServerClass_XML(FILE *fp, ServerClass *sc)
357 fprintf(fp, "<serverclass name=\"%s\">\n", sc->GetName());
358 UTIL_DrawSendTable_XML(fp, sc->m_pTable, 0);
359 fprintf(fp, "</serverclass>\n");
362 void UTIL_DrawSendTable(FILE *fp, SendTable *pTable, int level = 1)
367 for (int i = 0; i < pTable->GetNumProps(); i++)
369 pProp = pTable->GetProp(i);
370 if (pProp->GetDataTable())
372 fprintf(fp, "%*sTable: %s (offset %d) (type %s)\n",
376 pProp->GetDataTable()->GetName());
378 UTIL_DrawSendTable(fp, pProp->GetDataTable(), level + 1);
382 type = GetDTTypeName(pProp->GetType());
387 "%*sMember: %s (offset %d) (type %s) (bits %d)\n",
397 "%*sMember: %s (offset %d) (type %d) (bits %d)\n",
408 CON_COMMAND(sm_dump_netprops_xml, "Dumps the networkable property table as an XML file")
410 #if SOURCE_ENGINE <= SE_DARKMESSIAH
416 META_CONPRINT("Usage: sm_dump_netprops_xml <file>\n");
420 const char *file = args.Arg(1);
421 if (!file || file[0] == '\0')
423 META_CONPRINT("Usage: sm_dump_netprops_xml <file>\n");
427 char path[PLATFORM_MAX_PATH];
428 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
431 if ((fp = fopen(path, "wt")) == NULL)
433 META_CONPRINTF("Could not open file \"%s\"\n", path);
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());
440 ServerClass *pBase = gamedll->GetAllServerClasses();
441 while (pBase != NULL)
443 UTIL_DrawServerClass_XML(fp, pBase);
444 pBase = pBase->m_pNext;
450 CON_COMMAND(sm_dump_netprops, "Dumps the networkable property table as a text file")
452 #if SOURCE_ENGINE <= SE_DARKMESSIAH
458 META_CONPRINT("Usage: sm_dump_netprops <file>\n");
462 const char *file = args.Arg(1);
463 if (!file || file[0] == '\0')
465 META_CONPRINT("Usage: sm_dump_netprops <file>\n");
469 char path[PLATFORM_MAX_PATH];
470 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
473 if ((fp = fopen(path, "wt")) == NULL)
475 META_CONPRINTF("Could not open file \"%s\"\n", path);
479 fprintf(fp, "// Dump of all network properties for \"%s\" follows\n//\n\n", g_pSM->GetGameFolderName());
481 ServerClass *pBase = gamedll->GetAllServerClasses();
482 while (pBase != NULL)
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;
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,
501 /* Wow we don't care, thanks Microsoft. */
505 CON_COMMAND(sm_dump_classes, "Dumps the class list as a text file")
507 #if SOURCE_ENGINE <= SE_DARKMESSIAH
513 META_CONPRINT("Usage: sm_dump_classes <file>\n");
517 const char *file = args.Arg(1);
518 if (!file || file[0] == '\0')
520 META_CONPRINT("Usage: sm_dump_classes <file>\n");
524 ICallWrapper *pWrapper = NULL;
529 retData.flags = PASSFLAG_BYVAL;
530 retData.size = sizeof(void *);
531 retData.type = PassType_Basic;
534 if (!g_pGameConf->GetMemSig("EntityFactory", &addr) || addr == NULL)
536 META_CONPRINT("Failed to locate function\n");
540 pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retData, NULL, 0);
544 void *returnData = NULL;
546 pWrapper->Execute(NULL, &returnData);
550 if (returnData == NULL)
555 CEntityFactoryDictionary *dict = ( CEntityFactoryDictionary * )returnData;
562 char path[PLATFORM_MAX_PATH];
563 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
566 if ((fp = fopen(path, "wt")) == NULL)
568 META_CONPRINTF("Could not open file \"%s\"\n", path);
575 #if defined SUBPLATFORM_SECURECRT
576 _invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
579 time_t t = g_pSM->GetAdjustedTime();
580 size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
582 #if defined SUBPLATFORM_SECURECRT
583 _set_invalid_parameter_handler(handler);
586 fprintf(fp, "// Dump of all classes for \"%s\" as at %s\n//\n\n", g_pSM->GetGameFolderName(), buffer);
588 for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
590 IServerNetworkable *entity = dict->Create(dict->m_Factories.GetElementName(i));
591 ServerClass *sclass = entity->GetServerClass();
592 fprintf(fp,"%s - %s\n",sclass->GetName(), dict->m_Factories.GetElementName(i));
594 typedescription_t *datamap = gamehelpers->FindInDataMap(gamehelpers->GetDataMap(entity->GetBaseEntity()), "m_iEFlags");
595 int *eflags = (int *)((char *)entity->GetBaseEntity() + GetTypeDescOffs(datamap));
596 *eflags |= (1<<0); // EFL_KILLME
603 char *UTIL_FlagsToString(int flags)
605 static char str[1024];
608 if (flags & FTYPEDESC_GLOBAL)
610 strcat(str, "Global|");
612 if (flags & FTYPEDESC_SAVE)
614 strcat(str, "Save|");
616 if (flags & FTYPEDESC_KEY)
620 if (flags & FTYPEDESC_INPUT)
622 strcat(str, "Input|");
624 if (flags & FTYPEDESC_OUTPUT)
626 strcat(str, "Output|");
628 if (flags & FTYPEDESC_FUNCTIONTABLE)
630 strcat(str, "FunctionTable|");
632 if (flags & FTYPEDESC_PTR)
636 if (flags & FTYPEDESC_OVERRIDE)
638 strcat(str, "Override|");
641 int len = strlen(str) - 1;
644 str[len] = 0; // Strip the final '|'
650 void UTIL_DrawDataTable(FILE *fp, datamap_t *pMap, int level)
654 for (int i=0; i<level; i++)
659 spaces[level] = '\0';
661 const char *externalname;
666 for (int i=0; i<pMap->dataNumFields; i++)
668 if (pMap->dataDesc[i].fieldName == NULL)
673 if (pMap->dataDesc[i].td)
675 fprintf(fp, " %sSub-Class Table (%d Deep): %s - %s\n", spaces, level+1, pMap->dataDesc[i].fieldName, pMap->dataDesc[i].td->dataClassName);
676 UTIL_DrawDataTable(fp, pMap->dataDesc[i].td, level+1);
680 externalname = pMap->dataDesc[i].externalName;
681 flags = UTIL_FlagsToString(pMap->dataDesc[i].flags);
683 if (externalname == NULL)
685 fprintf(fp,"%s- %s (%s)(%i Bytes)\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes);
689 fprintf(fp,"%s- %s (%s)(%i Bytes) - %s\n", spaces, pMap->dataDesc[i].fieldName, flags, pMap->dataDesc[i].fieldSizeInBytes, externalname);
693 pMap = pMap->baseMap;
697 CON_COMMAND(sm_dump_datamaps, "Dumps the data map list as a text file")
699 #if SOURCE_ENGINE <= SE_DARKMESSIAH
705 META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
709 const char *file = args.Arg(1);
710 if (!file || file[0] == '\0')
712 META_CONPRINT("Usage: sm_dump_datamaps <file>\n");
716 static CEntityFactoryDictionary *dict = NULL;
720 if (g_pGameConf->GetMemSig("EntityFactoryFinder", (void **)&addr) && addr)
723 if (!g_pGameConf->GetOffset("EntityFactoryOffset", &offset) || !offset)
727 dict = *reinterpret_cast<CEntityFactoryDictionary **>((intptr_t)addr + offset);
733 ICallWrapper *pWrapper = NULL;
736 retData.flags = PASSFLAG_BYVAL;
737 retData.size = sizeof(void *);
738 retData.type = PassType_Basic;
741 if (g_pGameConf->GetMemSig("EntityFactory", &addr) && addr != NULL)
743 pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retData, NULL, 0);
748 void *returnData = NULL;
750 pWrapper->Execute(NULL, &returnData);
754 if (returnData == NULL)
759 dict = ( CEntityFactoryDictionary * )returnData;
765 META_CONPRINT("Failed to locate function\n");
769 char path[PLATFORM_MAX_PATH];
770 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
773 if ((fp = fopen(path, "wt")) == NULL)
775 META_CONPRINTF("Could not open file \"%s\"\n", path);
782 #if defined SUBPLATFORM_SECURECRT
783 _invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
786 time_t t = g_pSM->GetAdjustedTime();
787 size_t written = strftime(buffer, sizeof(buffer), "%d/%m/%Y", localtime(&t));
789 #if defined SUBPLATFORM_SECURECRT
790 _set_invalid_parameter_handler(handler);
793 fprintf(fp, "// Dump of all datamaps for \"%s\" as at %s\n//\n//\n", g_pSM->GetGameFolderName(), buffer);
796 fprintf(fp, "// Flag Details:\n//\n");
798 fprintf(fp, "// Global: This field is masked for global entity save/restore\n");
799 fprintf(fp, "// Save: This field is saved to disk\n");
800 fprintf(fp, "// Key: This field can be requested and written to by string name at load time\n");
801 fprintf(fp, "// Input: This field can be written to by string name at run time, and a function called\n");
802 fprintf(fp, "// Output: This field propogates it's value to all targets whenever it changes\n");
803 fprintf(fp, "// FunctionTable: This is a table entry for a member function pointer\n");
804 fprintf(fp, "// Ptr: This field is a pointer, not an embedded object\n");
805 fprintf(fp, "// Override: The field is an override for one in a base class (only used by prediction system for now)\n");
807 fprintf(fp, "//\n\n");
809 static int offsEFlags = -1;
810 for ( int i = dict->m_Factories.First(); i != dict->m_Factories.InvalidIndex(); i = dict->m_Factories.Next( i ) )
812 IServerNetworkable *entity = dict->Create(dict->m_Factories.GetElementName(i));
813 ServerClass *sclass = entity->GetServerClass();
814 datamap_t *pMap = gamehelpers->GetDataMap(entity->GetBaseEntity());
816 fprintf(fp,"%s - %s\n", sclass->GetName(), dict->m_Factories.GetElementName(i));
818 UTIL_DrawDataTable(fp, pMap, 0);
820 if (offsEFlags == -1)
822 typedescription_t *datamap = gamehelpers->FindInDataMap(pMap, "m_iEFlags");
829 offsEFlags = GetTypeDescOffs(datamap);
832 int *eflags = (int *)((char *)entity->GetBaseEntity() + offsEFlags);
833 *eflags |= (1<<0); // EFL_KILLME