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 SendProp *prop = g_pGameHelpers->FindInSendTable(m_Sc->GetName(), name);
127 offset = prop->GetOffset();
130 *size = 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()
288 /* Read our sigs and offsets from the config file */
289 #if defined PLATFORM_WINDOWS
290 if (!g_pGameConf->GetMemSig("CBaseTempEntity", &addr) || !addr)
294 if (!g_pGameConf->GetOffset("s_pTempEntities", &offset))
298 /* Store the head of the TE linked list */
299 m_ListHead = **(void ***)((unsigned char *)addr + offset);
301 if (!g_pGameConf->GetMemSig("s_pTempEntities", &addr) || !addr)
305 /* Store the head of the TE linked list */
306 m_ListHead = *(void **)addr;
308 if (!g_pGameConf->GetOffset("GetTEName", &m_NameOffs))
312 if (!g_pGameConf->GetOffset("GetTENext", &m_NextOffs))
316 if (!g_pGameConf->GetOffset("TE_GetServerClass", &m_GetClassNameOffs))
321 /* Create our trie */
322 m_TempEntInfo = adtfactory->CreateBasicTrie();
324 /* Create the GetServerClass call */
326 retinfo.flags = PASSFLAG_BYVAL;
327 retinfo.type = PassType_Basic;
328 retinfo.size = sizeof(ServerClass *);
329 g_GetServerClass = g_pBinTools->CreateVCall(m_GetClassNameOffs, 0, 0, &retinfo, NULL, 0);
335 bool TempEntityManager::IsAvailable()
340 void TempEntityManager::Shutdown()
347 SourceHook::List<TempEntityInfo *>::iterator iter;
348 for (iter=m_TEList.begin(); iter!=m_TEList.end(); iter++)
354 m_TempEntInfo->Destroy();
355 g_GetServerClass->Destroy();
356 g_GetServerClass = NULL;
358 m_NextOffs = m_NameOffs = m_GetClassNameOffs = 0;
362 TempEntityInfo *TempEntityManager::GetTempEntityInfo(const char *name)
364 /* If the system is unloaded skip the search */
370 TempEntityInfo *te = NULL;
372 /* Start searching for the TE inside the engine */
373 if (!m_TempEntInfo->Retrieve(name, reinterpret_cast<void **>(&te)))
375 void *iter = m_ListHead;
378 const char *realname = *(const char **)((unsigned char *)iter + m_NameOffs);
383 if (strcmp(name, realname) == 0)
385 te = new TempEntityInfo(name, iter);
386 m_TempEntInfo->Insert(name, (void *)te);
387 m_TEList.push_back(te);
390 iter = *(void **)((unsigned char *)iter + m_NextOffs);
398 const char *TempEntityManager::GetNameFromThisPtr(void *me)
400 return *(const char **)((unsigned char *)me + m_NameOffs);
403 void TempEntityManager::DumpList()
405 unsigned int index = 0;
406 META_CONPRINT("Listing temp entities:\n");
407 void *iter = m_ListHead;
410 const char *realname = *(const char **)((unsigned char *)iter + m_NameOffs);
415 TempEntityInfo *info = GetTempEntityInfo(realname);
420 ServerClass *sc = info->GetServerClass();
421 META_CONPRINTF("[%02d] %s (%s)\n", index++, realname, sc->GetName());
422 iter = *(void **)((unsigned char *)iter + m_NextOffs);
424 META_CONPRINTF("%d tempent%s found.\n", index, (index == 1) ? " was" : "s were");
427 const char *SendPropTypeToString(SendPropType type)
432 } else if (type == DPT_Float) {
434 } else if (type == DPT_Vector) {
436 } else if (type == DPT_String) {
438 } else if (type == DPT_Array) {
440 } else if (type == DPT_DataTable) {
447 void _DumpProps(FILE *fp, SendTable *pTable)
450 for (int i=0; i<pTable->GetNumProps(); i++)
452 SendProp *prop = pTable->GetProp(i);
453 if ((pOther = prop->GetDataTable()) != NULL)
455 _DumpProps(fp, pOther);
457 fprintf(fp, "\t\t\t\"%s\"\t\t\"%s\"\n",
458 prop->GetName() ? prop->GetName() : "unknown",
459 SendPropTypeToString(prop->GetType()));
464 void TempEntityManager::DumpProps(FILE *fp)
466 unsigned int index = 0;
467 void *iter = m_ListHead;
468 fprintf(fp, "\"TempEnts\"\n{\n");
471 const char *realname = *(const char **)((unsigned char *)iter + m_NameOffs);
476 TempEntityInfo *info = GetTempEntityInfo(realname);
481 ServerClass *sc = info->GetServerClass();
482 fprintf(fp, "\t\"%s\"\n", sc->GetName());
483 fprintf(fp, "\t{\n");
484 fprintf(fp, "\t\t\"name\"\t\t\"%s\"\n", realname);
485 fprintf(fp, "\t\t\"index\"\t\t\"%d\"\n", index++);
486 fprintf(fp, "\t\t\"SendTable\"\n\t\t{\n");
487 _DumpProps(fp, sc->m_pTable);
488 fprintf(fp, "\t\t}\n\t}\n");
489 iter = *(void **)((unsigned char *)iter + m_NextOffs);
492 META_CONPRINTF("%d tempent%s written to file.\n", index, (index == 1) ? " was" : "s were");