3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2008 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>.
34 TempEntityManager g_TEManager;
35 ICallWrapper *g_GetServerClass = NULL;
37 CON_COMMAND(sm_print_telist, "Prints the temp entity list")
39 if (!g_TEManager.IsAvailable())
41 META_CONPRINT("The tempent portion of SDKTools failed to load.\n");
42 META_CONPRINT("Check that you have the latest sdktools.games.txt file!\n");
45 g_TEManager.DumpList();
48 CON_COMMAND(sm_dump_teprops, "Dumps tempentity props to a file")
50 #if SOURCE_ENGINE <= SE_DARKMESSIAH
53 if (!g_TEManager.IsAvailable())
55 META_CONPRINT("The tempent portion of SDKTools failed to load.\n");
56 META_CONPRINT("Check that you have the latest sdktools.games.txt file!\n");
59 int argc = args.ArgC();
62 META_CONPRINT("Usage: sm_dump_teprops <file>\n");
66 const char *arg = args.Arg(1);
68 if (!arg || arg[0] == '\0')
70 META_CONPRINTF("Usage: sm_dump_teprops <file>\n");
74 char path[PLATFORM_MAX_PATH];
75 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", arg);
78 if ((fp = fopen(path, "wt")) == NULL)
80 META_CONPRINTF("Could not open file \"%s\"\n", path);
84 g_TEManager.DumpProps(fp);
89 /*************************
91 * Temp Entities Wrappers *
93 **************************/
95 TempEntityInfo::TempEntityInfo(const char *name, void *me)
99 g_GetServerClass->Execute(&m_Me, &m_Sc);
102 const char *TempEntityInfo::GetName()
104 return m_Name.c_str();
107 ServerClass *TempEntityInfo::GetServerClass()
112 bool TempEntityInfo::IsValidProp(const char *name)
114 return (g_pGameHelpers->FindInSendTable(m_Sc->GetName(), name)) ? true : false;
117 int TempEntityInfo::_FindOffset(const char *name, int *size)
121 sm_sendprop_info_t info;
122 if (!g_pGameHelpers->FindSendPropInfo(m_Sc->GetName(), name, &info))
127 offset = info.actual_offset;
130 *size = info.prop->m_nBits;
136 bool TempEntityInfo::TE_SetEntData(const char *name, int value)
138 /* Search for our offset */
140 int offset = _FindOffset(name, &size);
149 *((uint8_t *)m_Me + offset) = value;
150 } else if (size <= 16) {
151 *(short *)((uint8_t *)m_Me + offset) = value;
152 } else if (size <= 32) {
153 *(int *)((uint8_t *)m_Me + offset) = value;
161 bool TempEntityInfo::TE_GetEntData(const char *name, int *value)
163 /* Search for our offset */
165 int offset = _FindOffset(name, &size);
174 *value = *((uint8_t *)m_Me + offset);
175 } else if (size <= 16) {
176 *value = *(short *)((uint8_t *)m_Me + offset);
177 } else if (size <= 32) {
178 *value = *(int *)((uint8_t *)m_Me + offset);
186 bool TempEntityInfo::TE_SetEntDataFloat(const char *name, float value)
188 /* Search for our offset */
189 int offset = _FindOffset(name);
196 *(float *)((uint8_t *)m_Me + offset) = value;
201 bool TempEntityInfo::TE_GetEntDataFloat(const char *name, float *value)
203 /* Search for our offset */
204 int offset = _FindOffset(name);
211 *value = *(float *)((uint8_t *)m_Me + offset);
216 bool TempEntityInfo::TE_SetEntDataVector(const char *name, float vector[3])
218 /* Search for our offset */
219 int offset = _FindOffset(name);
226 Vector *v = (Vector *)((uint8_t *)m_Me + offset);
234 bool TempEntityInfo::TE_GetEntDataVector(const char *name, float vector[3])
236 /* Search for our offset */
237 int offset = _FindOffset(name);
244 Vector *v = (Vector *)((uint8_t *)m_Me + offset);
252 bool TempEntityInfo::TE_SetEntDataFloatArray(const char *name, cell_t *array, int size)
254 /* Search for our offset */
255 int offset = _FindOffset(name);
262 float *base = (float *)((uint8_t *)m_Me + offset);
263 for (int i=0; i<size; i++)
265 base[i] = sp_ctof(array[i]);
271 void TempEntityInfo::Send(IRecipientFilter &filter, float delay)
273 engine->PlaybackTempEntity(filter, delay, m_Me, m_Sc->m_pTable, m_Sc->m_ClassID);
276 /**********************
278 * Temp Entity Manager *
280 ***********************/
282 void TempEntityManager::Initialize()
289 * First try to lookup s_pTempEntities directly for platforms with symbols.
290 * If symbols aren't present (Windows or stripped Linux/Mac),
291 * attempt find via CBaseTempEntity ctor + offset
294 /* Read our sigs and offsets from the config file */
295 if (g_pGameConf->GetMemSig("s_pTempEntities", &addr) && addr)
298 /* Store the head of the TE linked list */
299 m_ListHead = *(void **)addr;
301 else if (g_pGameConf->GetMemSig("CBaseTempEntity", &addr) && addr)
303 if (!g_pGameConf->GetOffset("s_pTempEntities", &offset))
307 /* Store the head of the TE linked list */
308 m_ListHead = **(void ***)((unsigned char *)addr + offset);
315 if (!g_pGameConf->GetOffset("GetTEName", &m_NameOffs))
319 if (!g_pGameConf->GetOffset("GetTENext", &m_NextOffs))
323 if (!g_pGameConf->GetOffset("TE_GetServerClass", &m_GetClassNameOffs))
328 /* Create our trie */
329 m_TempEntInfo = adtfactory->CreateBasicTrie();
331 /* Create the GetServerClass call */
333 retinfo.flags = PASSFLAG_BYVAL;
334 retinfo.type = PassType_Basic;
335 retinfo.size = sizeof(ServerClass *);
336 g_GetServerClass = g_pBinTools->CreateVCall(m_GetClassNameOffs, 0, 0, &retinfo, NULL, 0);
342 bool TempEntityManager::IsAvailable()
347 void TempEntityManager::Shutdown()
354 SourceHook::List<TempEntityInfo *>::iterator iter;
355 for (iter=m_TEList.begin(); iter!=m_TEList.end(); iter++)
361 m_TempEntInfo->Destroy();
362 g_GetServerClass->Destroy();
363 g_GetServerClass = NULL;
365 m_NextOffs = m_NameOffs = m_GetClassNameOffs = 0;
369 TempEntityInfo *TempEntityManager::GetTempEntityInfo(const char *name)
371 /* If the system is unloaded skip the search */
377 TempEntityInfo *te = NULL;
379 /* Start searching for the TE inside the engine */
380 if (!m_TempEntInfo->Retrieve(name, reinterpret_cast<void **>(&te)))
382 void *iter = m_ListHead;
385 const char *realname = *(const char **)((unsigned char *)iter + m_NameOffs);
390 if (strcmp(name, realname) == 0)
392 te = new TempEntityInfo(name, iter);
393 m_TempEntInfo->Insert(name, (void *)te);
394 m_TEList.push_back(te);
397 iter = *(void **)((unsigned char *)iter + m_NextOffs);
405 const char *TempEntityManager::GetNameFromThisPtr(void *me)
407 return *(const char **)((unsigned char *)me + m_NameOffs);
410 void TempEntityManager::DumpList()
412 unsigned int index = 0;
413 META_CONPRINT("Listing temp entities:\n");
414 void *iter = m_ListHead;
417 const char *realname = *(const char **)((unsigned char *)iter + m_NameOffs);
422 TempEntityInfo *info = GetTempEntityInfo(realname);
427 ServerClass *sc = info->GetServerClass();
428 META_CONPRINTF("[%02d] %s (%s)\n", index++, realname, sc->GetName());
429 iter = *(void **)((unsigned char *)iter + m_NextOffs);
431 META_CONPRINTF("%d tempent%s found.\n", index, (index == 1) ? " was" : "s were");
434 const char *SendPropTypeToString(SendPropType type)
439 } else if (type == DPT_Float) {
441 } else if (type == DPT_Vector) {
443 } else if (type == DPT_String) {
445 } else if (type == DPT_Array) {
447 } else if (type == DPT_DataTable) {
454 void _DumpProps(FILE *fp, SendTable *pTable)
457 for (int i=0; i<pTable->GetNumProps(); i++)
459 SendProp *prop = pTable->GetProp(i);
460 if ((pOther = prop->GetDataTable()) != NULL)
462 _DumpProps(fp, pOther);
464 fprintf(fp, "\t\t\t\"%s\"\t\t\"%s\"\n",
465 prop->GetName() ? prop->GetName() : "unknown",
466 SendPropTypeToString(prop->GetType()));
471 void TempEntityManager::DumpProps(FILE *fp)
473 unsigned int index = 0;
474 void *iter = m_ListHead;
475 fprintf(fp, "\"TempEnts\"\n{\n");
478 const char *realname = *(const char **)((unsigned char *)iter + m_NameOffs);
483 TempEntityInfo *info = GetTempEntityInfo(realname);
488 ServerClass *sc = info->GetServerClass();
489 fprintf(fp, "\t\"%s\"\n", sc->GetName());
490 fprintf(fp, "\t{\n");
491 fprintf(fp, "\t\t\"name\"\t\t\"%s\"\n", realname);
492 fprintf(fp, "\t\t\"index\"\t\t\"%d\"\n", index++);
493 fprintf(fp, "\t\t\"SendTable\"\n\t\t{\n");
494 _DumpProps(fp, sc->m_pTable);
495 fprintf(fp, "\t\t}\n\t}\n");
496 iter = *(void **)((unsigned char *)iter + m_NextOffs);
499 META_CONPRINTF("%d tempent%s written to file.\n", index, (index == 1) ? " was" : "s were");