extensions/sdktools/tempents.cpp
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/sdktools/tempents.cpp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,493 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod SDKTools Extension
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include "tempents.h"
    1.36 +
    1.37 +TempEntityManager g_TEManager;
    1.38 +ICallWrapper *g_GetServerClass = NULL;
    1.39 +
    1.40 +CON_COMMAND(sm_print_telist, "Prints the temp entity list")
    1.41 +{
    1.42 +	if (!g_TEManager.IsAvailable())
    1.43 +	{
    1.44 +		META_CONPRINT("The tempent portion of SDKTools failed to load.\n");
    1.45 +		META_CONPRINT("Check that you have the latest sdktools.games.txt file!\n");
    1.46 +		return;
    1.47 +	}
    1.48 +	g_TEManager.DumpList();
    1.49 +}
    1.50 +
    1.51 +CON_COMMAND(sm_dump_teprops, "Dumps tempentity props to a file")
    1.52 +{
    1.53 +#if !defined ORANGEBOX_BUILD
    1.54 +	CCommand args;
    1.55 +#endif
    1.56 +	if (!g_TEManager.IsAvailable())
    1.57 +	{
    1.58 +		META_CONPRINT("The tempent portion of SDKTools failed to load.\n");
    1.59 +		META_CONPRINT("Check that you have the latest sdktools.games.txt file!\n");
    1.60 +		return;
    1.61 +	}
    1.62 +	int argc = args.ArgC();
    1.63 +	if (argc < 2)
    1.64 +	{
    1.65 +		META_CONPRINT("Usage: sm_dump_teprops <file>\n");
    1.66 +		return;
    1.67 +	}
    1.68 +
    1.69 +	const char *arg = args.Arg(1);
    1.70 +
    1.71 +	if (!arg || arg[0] == '\0')
    1.72 +	{
    1.73 +		META_CONPRINTF("Usage: sm_dump_teprops <file>\n");
    1.74 +		return;
    1.75 +	}
    1.76 +
    1.77 +	char path[PLATFORM_MAX_PATH];
    1.78 +	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", arg);
    1.79 +
    1.80 +	FILE *fp = NULL;
    1.81 +	if ((fp = fopen(path, "wt")) == NULL)
    1.82 +	{
    1.83 +		META_CONPRINTF("Could not open file \"%s\"\n", path);
    1.84 +		return;
    1.85 +	}
    1.86 +
    1.87 +	g_TEManager.DumpProps(fp);
    1.88 +
    1.89 +	fclose(fp);
    1.90 +}
    1.91 +
    1.92 +/*************************
    1.93 +*                        *
    1.94 +* Temp Entities Wrappers *
    1.95 +*                        *
    1.96 +**************************/
    1.97 +
    1.98 +TempEntityInfo::TempEntityInfo(const char *name, void *me)
    1.99 +{
   1.100 +	m_Name.assign(name);
   1.101 +	m_Me = me;
   1.102 +	g_GetServerClass->Execute(&m_Me, &m_Sc);
   1.103 +}
   1.104 +
   1.105 +const char *TempEntityInfo::GetName()
   1.106 +{
   1.107 +	return m_Name.c_str();
   1.108 +}
   1.109 +
   1.110 +ServerClass *TempEntityInfo::GetServerClass()
   1.111 +{
   1.112 +	return m_Sc;
   1.113 +}
   1.114 +
   1.115 +bool TempEntityInfo::IsValidProp(const char *name)
   1.116 +{
   1.117 +	return (g_pGameHelpers->FindInSendTable(m_Sc->GetName(), name)) ? true : false;
   1.118 +}
   1.119 +
   1.120 +int TempEntityInfo::_FindOffset(const char *name, int *size)
   1.121 +{
   1.122 +	int offset;
   1.123 +
   1.124 +	SendProp *prop = g_pGameHelpers->FindInSendTable(m_Sc->GetName(), name);
   1.125 +	if (!prop)
   1.126 +	{
   1.127 +		return -1;
   1.128 +	}
   1.129 +
   1.130 +	offset = prop->GetOffset();
   1.131 +	if (size)
   1.132 +	{
   1.133 +		*size = prop->m_nBits;
   1.134 +	}
   1.135 +
   1.136 +	return offset;
   1.137 +}
   1.138 +
   1.139 +bool TempEntityInfo::TE_SetEntData(const char *name, int value)
   1.140 +{
   1.141 +	/* Search for our offset */
   1.142 +	int size;
   1.143 +	int offset = _FindOffset(name, &size);
   1.144 +
   1.145 +	if (offset < 0)
   1.146 +	{
   1.147 +		return false;
   1.148 +	}
   1.149 +
   1.150 +	if (size <= 8)
   1.151 +	{
   1.152 +		*((uint8_t *)m_Me + offset) = value;
   1.153 +	} else if (size <= 16) {
   1.154 +		*(short *)((uint8_t *)m_Me + offset) = value;
   1.155 +	} else if (size <= 32) {
   1.156 +		*(int *)((uint8_t *)m_Me + offset) = value;
   1.157 +	} else {
   1.158 +		return false;
   1.159 +	}
   1.160 +
   1.161 +	return true;
   1.162 +}
   1.163 +
   1.164 +bool TempEntityInfo::TE_GetEntData(const char *name, int *value)
   1.165 +{
   1.166 +	/* Search for our offset */
   1.167 +	int size;
   1.168 +	int offset = _FindOffset(name, &size);
   1.169 +
   1.170 +	if (offset < 0)
   1.171 +	{
   1.172 +		return false;
   1.173 +	}
   1.174 +
   1.175 +	if (size <= 8)
   1.176 +	{
   1.177 +		*value = *((uint8_t *)m_Me + offset);
   1.178 +	} else if (size <= 16) {
   1.179 +		*value = *(short *)((uint8_t *)m_Me + offset);
   1.180 +	} else if (size <= 32) {
   1.181 +		*value = *(int *)((uint8_t *)m_Me + offset);
   1.182 +	} else {
   1.183 +		return false;
   1.184 +	}
   1.185 +
   1.186 +	return true;
   1.187 +}
   1.188 +
   1.189 +bool TempEntityInfo::TE_SetEntDataFloat(const char *name, float value)
   1.190 +{
   1.191 +	/* Search for our offset */
   1.192 +	int offset = _FindOffset(name);
   1.193 +
   1.194 +	if (offset < 0)
   1.195 +	{
   1.196 +		return false;
   1.197 +	}
   1.198 +
   1.199 +	*(float *)((uint8_t *)m_Me + offset) = value;
   1.200 +
   1.201 +	return true;
   1.202 +}
   1.203 +
   1.204 +bool TempEntityInfo::TE_GetEntDataFloat(const char *name, float *value)
   1.205 +{
   1.206 +	/* Search for our offset */
   1.207 +	int offset = _FindOffset(name);
   1.208 +
   1.209 +	if (offset < 0)
   1.210 +	{
   1.211 +		return false;
   1.212 +	}
   1.213 +
   1.214 +	*value = *(float *)((uint8_t *)m_Me + offset);
   1.215 +
   1.216 +	return true;
   1.217 +}
   1.218 +
   1.219 +bool TempEntityInfo::TE_SetEntDataVector(const char *name, float vector[3])
   1.220 +{
   1.221 +	/* Search for our offset */
   1.222 +	int offset = _FindOffset(name);
   1.223 +
   1.224 +	if (offset < 0)
   1.225 +	{
   1.226 +		return false;
   1.227 +	}
   1.228 +
   1.229 +	Vector *v = (Vector *)((uint8_t *)m_Me + offset);
   1.230 +	v->x = vector[0];
   1.231 +	v->y = vector[1];
   1.232 +	v->z = vector[2];
   1.233 +
   1.234 +	return true;
   1.235 +}
   1.236 +
   1.237 +bool TempEntityInfo::TE_GetEntDataVector(const char *name, float vector[3])
   1.238 +{
   1.239 +	/* Search for our offset */
   1.240 +	int offset = _FindOffset(name);
   1.241 +
   1.242 +	if (offset < 0)
   1.243 +	{
   1.244 +		return false;
   1.245 +	}
   1.246 +
   1.247 +	Vector *v = (Vector *)((uint8_t *)m_Me + offset);
   1.248 +	vector[0] = v->x;
   1.249 +	vector[1] = v->y;
   1.250 +	vector[2] = v->z;
   1.251 +
   1.252 +	return true;
   1.253 +}
   1.254 +
   1.255 +bool TempEntityInfo::TE_SetEntDataFloatArray(const char *name, cell_t *array, int size)
   1.256 +{
   1.257 +	/* Search for our offset */
   1.258 +	int offset = _FindOffset(name);
   1.259 +
   1.260 +	if (offset < 0)
   1.261 +	{
   1.262 +		return false;
   1.263 +	}
   1.264 +
   1.265 +	float *base = (float *)((uint8_t *)m_Me + offset);
   1.266 +	for (int i=0; i<size; i++)
   1.267 +	{
   1.268 +		base[i] = sp_ctof(array[i]);
   1.269 +	}
   1.270 +
   1.271 +	return true;
   1.272 +}
   1.273 +
   1.274 +void TempEntityInfo::Send(IRecipientFilter &filter, float delay)
   1.275 +{
   1.276 +	engine->PlaybackTempEntity(filter, delay, m_Me, m_Sc->m_pTable, m_Sc->m_ClassID);
   1.277 +}
   1.278 +
   1.279 +/**********************
   1.280 +*                     *
   1.281 +* Temp Entity Manager *
   1.282 +*                     *
   1.283 +***********************/
   1.284 +
   1.285 +void TempEntityManager::Initialize()
   1.286 +{
   1.287 +	void *addr;
   1.288 +	int offset;
   1.289 +	m_Loaded = false;
   1.290 +
   1.291 +	/* Read our sigs and offsets from the config file */
   1.292 +#if defined PLATFORM_WINDOWS
   1.293 +	if (!g_pGameConf->GetMemSig("CBaseTempEntity", &addr) || !addr)
   1.294 +	{
   1.295 +		return;
   1.296 +	}
   1.297 +	if (!g_pGameConf->GetOffset("s_pTempEntities", &offset))
   1.298 +	{
   1.299 +		return;
   1.300 +	}
   1.301 +	/* Store the head of the TE linked list */
   1.302 +	m_ListHead = **(void ***)((unsigned char *)addr + offset);
   1.303 +#else
   1.304 +	if (!g_pGameConf->GetMemSig("s_pTempEntities", &addr) || !addr)
   1.305 +	{
   1.306 +		return;
   1.307 +	}
   1.308 +	/* Store the head of the TE linked list */
   1.309 +	m_ListHead = *(void **)addr;
   1.310 +#endif
   1.311 +	if (!g_pGameConf->GetOffset("GetTEName", &m_NameOffs))
   1.312 +	{
   1.313 +		return;
   1.314 +	}
   1.315 +	if (!g_pGameConf->GetOffset("GetTENext", &m_NextOffs))
   1.316 +	{
   1.317 +		return;
   1.318 +	}
   1.319 +	if (!g_pGameConf->GetOffset("TE_GetServerClass", &m_GetClassNameOffs))
   1.320 +	{
   1.321 +		return;
   1.322 +	}
   1.323 +
   1.324 +	/* Create our trie */
   1.325 +	m_TempEntInfo = adtfactory->CreateBasicTrie();
   1.326 +
   1.327 +	/* Create the GetServerClass call */
   1.328 +	PassInfo retinfo;
   1.329 +	retinfo.flags = PASSFLAG_BYVAL;
   1.330 +	retinfo.type = PassType_Basic;
   1.331 +	retinfo.size = sizeof(ServerClass *);
   1.332 +	g_GetServerClass = g_pBinTools->CreateVCall(m_GetClassNameOffs, 0, 0, &retinfo, NULL, 0);
   1.333 +
   1.334 +	/* We're done */
   1.335 +	m_Loaded = true;
   1.336 +}
   1.337 +
   1.338 +bool TempEntityManager::IsAvailable()
   1.339 +{
   1.340 +	return m_Loaded;
   1.341 +}
   1.342 +
   1.343 +void TempEntityManager::Shutdown()
   1.344 +{
   1.345 +	if (!IsAvailable())
   1.346 +	{
   1.347 +		return;
   1.348 +	}
   1.349 +
   1.350 +	SourceHook::List<TempEntityInfo *>::iterator iter;
   1.351 +	for (iter=m_TEList.begin(); iter!=m_TEList.end(); iter++)
   1.352 +	{
   1.353 +		delete (*iter);
   1.354 +	}
   1.355 +	m_TEList.clear();
   1.356 +
   1.357 +	m_TempEntInfo->Destroy();
   1.358 +	g_GetServerClass->Destroy();
   1.359 +	g_GetServerClass = NULL;
   1.360 +	m_ListHead = NULL;
   1.361 +	m_NextOffs = m_NameOffs = m_GetClassNameOffs = 0;
   1.362 +	m_Loaded = false;
   1.363 +}
   1.364 +
   1.365 +TempEntityInfo *TempEntityManager::GetTempEntityInfo(const char *name)
   1.366 +{
   1.367 +	/* If the system is unloaded skip the search */
   1.368 +	if (!IsAvailable())
   1.369 +	{
   1.370 +		return NULL;
   1.371 +	}
   1.372 +
   1.373 +	TempEntityInfo *te = NULL;
   1.374 +
   1.375 +	/* Start searching for the TE inside the engine */
   1.376 +	if (!m_TempEntInfo->Retrieve(name, reinterpret_cast<void **>(&te)))
   1.377 +	{
   1.378 +		void *iter = m_ListHead;
   1.379 +		while (iter)
   1.380 +		{
   1.381 +			const char *realname = *(const char **)((unsigned char *)iter + m_NameOffs);
   1.382 +			if (!realname)
   1.383 +			{
   1.384 +				continue;
   1.385 +			}
   1.386 +			if (strcmp(name, realname) == 0)
   1.387 +			{
   1.388 +				te = new TempEntityInfo(name, iter);
   1.389 +				m_TempEntInfo->Insert(name, (void *)te);
   1.390 +				m_TEList.push_back(te);
   1.391 +				return te;
   1.392 +			}
   1.393 +			iter = *(void **)((unsigned char *)iter + m_NextOffs);
   1.394 +		}
   1.395 +		return NULL;
   1.396 +	}
   1.397 +
   1.398 +	return te;
   1.399 +}
   1.400 +
   1.401 +const char *TempEntityManager::GetNameFromThisPtr(void *me)
   1.402 +{
   1.403 +	return *(const char **)((unsigned char *)me + m_NameOffs);
   1.404 +}
   1.405 +
   1.406 +void TempEntityManager::DumpList()
   1.407 +{
   1.408 +	unsigned int index = 0;
   1.409 +	META_CONPRINT("Listing temp entities:\n");
   1.410 +	void *iter = m_ListHead;
   1.411 +	while (iter)
   1.412 +	{
   1.413 +		const char *realname = *(const char **)((unsigned char *)iter + m_NameOffs);
   1.414 +		if (!realname)
   1.415 +		{
   1.416 +			break;
   1.417 +		}
   1.418 +		TempEntityInfo *info = GetTempEntityInfo(realname);
   1.419 +		if (!info)
   1.420 +		{
   1.421 +			continue;
   1.422 +		}
   1.423 +		ServerClass *sc = info->GetServerClass();
   1.424 +		META_CONPRINTF("[%02d] %s (%s)\n", index++, realname, sc->GetName());
   1.425 +		iter = *(void **)((unsigned char *)iter + m_NextOffs);
   1.426 +	}
   1.427 +	META_CONPRINTF("%d tempent%s found.\n", index, (index == 1) ? " was" : "s were");
   1.428 +}
   1.429 +
   1.430 +const char *SendPropTypeToString(SendPropType type)
   1.431 +{
   1.432 +	if (type == DPT_Int)
   1.433 +	{
   1.434 +		return "int";
   1.435 +	} else if (type == DPT_Float) {
   1.436 +		return "float";
   1.437 +	} else if (type == DPT_Vector) {
   1.438 +		return "vector";
   1.439 +	} else if (type == DPT_String) {
   1.440 +		return "string";
   1.441 +	} else if (type == DPT_Array) {
   1.442 +		return "array";
   1.443 +	} else if (type == DPT_DataTable) {
   1.444 +		return "datatable";
   1.445 +	} else {
   1.446 +		return "unknown";
   1.447 +	}
   1.448 +}
   1.449 +
   1.450 +void _DumpProps(FILE *fp, SendTable *pTable)
   1.451 +{
   1.452 +	SendTable *pOther;
   1.453 +	for (int i=0; i<pTable->GetNumProps(); i++)
   1.454 +	{
   1.455 +		SendProp *prop = pTable->GetProp(i);
   1.456 +		if ((pOther = prop->GetDataTable()) != NULL)
   1.457 +		{
   1.458 +			_DumpProps(fp, pOther);
   1.459 +		} else {
   1.460 +			fprintf(fp, "\t\t\t\"%s\"\t\t\"%s\"\n", 
   1.461 +				prop->GetName() ? prop->GetName() : "unknown",
   1.462 +				SendPropTypeToString(prop->GetType()));
   1.463 +		}
   1.464 +	}
   1.465 +}
   1.466 +
   1.467 +void TempEntityManager::DumpProps(FILE *fp)
   1.468 +{
   1.469 +	unsigned int index = 0;
   1.470 +	void *iter = m_ListHead;
   1.471 +	fprintf(fp, "\"TempEnts\"\n{\n");
   1.472 +	while (iter)
   1.473 +	{
   1.474 +		const char *realname = *(const char **)((unsigned char *)iter + m_NameOffs);
   1.475 +		if (!realname)
   1.476 +		{
   1.477 +			break;
   1.478 +		}
   1.479 +		TempEntityInfo *info = GetTempEntityInfo(realname);
   1.480 +		if (!info)
   1.481 +		{
   1.482 +			continue;
   1.483 +		}
   1.484 +		ServerClass *sc = info->GetServerClass();
   1.485 +		fprintf(fp, "\t\"%s\"\n", sc->GetName());
   1.486 +		fprintf(fp, "\t{\n");
   1.487 +		fprintf(fp, "\t\t\"name\"\t\t\"%s\"\n", realname);
   1.488 +		fprintf(fp, "\t\t\"index\"\t\t\"%d\"\n", index++);
   1.489 +		fprintf(fp, "\t\t\"SendTable\"\n\t\t{\n");
   1.490 +		_DumpProps(fp, sc->m_pTable);
   1.491 +		fprintf(fp, "\t\t}\n\t}\n");
   1.492 +		iter = *(void **)((unsigned char *)iter + m_NextOffs);
   1.493 +	}
   1.494 +	fprintf(fp, "}\n");
   1.495 +	META_CONPRINTF("%d tempent%s written to file.\n", index, (index == 1) ? " was" : "s were");
   1.496 +}