1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sdktools/tenatives.cpp Sun Jul 06 04:49:55 2008 +0000
1.3 @@ -0,0 +1,561 @@
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 +#include "CellRecipientFilter.h"
1.37 +#include <IForwardSys.h>
1.38 +
1.39 +SH_DECL_HOOK5_void(IVEngineServer, PlaybackTempEntity, SH_NOATTRIB, 0, IRecipientFilter &, float, const void *, const SendTable *, int);
1.40 +
1.41 +CellRecipientFilter g_TERecFilter;
1.42 +TempEntityInfo *g_CurrentTE = NULL;
1.43 +int g_TEPlayers[256];
1.44 +bool tenatives_initialized = false;
1.45 +
1.46 +/*************************
1.47 +* *
1.48 +* Temp Entity Hook Class *
1.49 +* *
1.50 +**************************/
1.51 +
1.52 +void TempEntHooks::Initialize()
1.53 +{
1.54 + m_TEHooks = adtfactory->CreateBasicTrie();
1.55 + plsys->AddPluginsListener(this);
1.56 + tenatives_initialized = true;
1.57 +}
1.58 +
1.59 +void TempEntHooks::Shutdown()
1.60 +{
1.61 + if (!tenatives_initialized)
1.62 + {
1.63 + return;
1.64 + }
1.65 +
1.66 + plsys->RemovePluginsListener(this);
1.67 + SourceHook::List<TEHookInfo *>::iterator iter;
1.68 + for (iter=m_HookInfo.begin(); iter!=m_HookInfo.end(); iter++)
1.69 + {
1.70 + delete (*iter);
1.71 + }
1.72 + if (m_HookCount)
1.73 + {
1.74 + m_HookCount = 1;
1.75 + _DecRefCounter();
1.76 + }
1.77 + m_TEHooks->Destroy();
1.78 + tenatives_initialized = false;
1.79 +}
1.80 +
1.81 +void TempEntHooks::OnPluginUnloaded(IPlugin *plugin)
1.82 +{
1.83 + SourceHook::List<TEHookInfo *>::iterator iter = m_HookInfo.begin();
1.84 + IPluginContext *pContext = plugin->GetBaseContext();
1.85 +
1.86 + /* For each hook list... */
1.87 + while (iter != m_HookInfo.end())
1.88 + {
1.89 + SourceHook::List<IPluginFunction *>::iterator f_iter = (*iter)->lst.begin();
1.90 +
1.91 + /* Find the hooks on the given temp entity */
1.92 + while (f_iter != (*iter)->lst.end())
1.93 + {
1.94 + /* If it matches, remove it and dec the ref count */
1.95 + if ((*f_iter)->GetParentContext() == pContext)
1.96 + {
1.97 + f_iter = (*iter)->lst.erase(f_iter);
1.98 + _DecRefCounter();
1.99 + }
1.100 + else
1.101 + {
1.102 + f_iter++;
1.103 + }
1.104 + }
1.105 +
1.106 + /* If there are no more hooks left, we can safely
1.107 + * remove it from the cache and remove its list.
1.108 + */
1.109 + if ((*iter)->lst.size() == 0)
1.110 + {
1.111 + m_TEHooks->Delete((*iter)->te->GetName());
1.112 + delete (*iter);
1.113 + iter = m_HookInfo.erase(iter);
1.114 + }
1.115 + else
1.116 + {
1.117 + iter++;
1.118 + }
1.119 + }
1.120 +}
1.121 +
1.122 +void TempEntHooks::_IncRefCounter()
1.123 +{
1.124 + if (m_HookCount++ == 0)
1.125 + {
1.126 + SH_ADD_HOOK_MEMFUNC(IVEngineServer, PlaybackTempEntity, engine, this, &TempEntHooks::OnPlaybackTempEntity, false);
1.127 + }
1.128 +}
1.129 +
1.130 +void TempEntHooks::_DecRefCounter()
1.131 +{
1.132 + if (--m_HookCount == 0)
1.133 + {
1.134 + SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, PlaybackTempEntity, engine, this, &TempEntHooks::OnPlaybackTempEntity, false);
1.135 + }
1.136 +}
1.137 +
1.138 +size_t TempEntHooks::_FillInPlayers(int *pl_array, IRecipientFilter *pFilter)
1.139 +{
1.140 + size_t size = static_cast<size_t>(pFilter->GetRecipientCount());
1.141 +
1.142 + for (size_t i=0; i<size; i++)
1.143 + {
1.144 + pl_array[i] = pFilter->GetRecipientIndex(i);
1.145 + }
1.146 +
1.147 + return size;
1.148 +}
1.149 +
1.150 +bool TempEntHooks::AddHook(const char *name, IPluginFunction *pFunc)
1.151 +{
1.152 + TEHookInfo *pInfo;
1.153 +
1.154 + if (m_TEHooks->Retrieve(name, reinterpret_cast<void **>(&pInfo)))
1.155 + {
1.156 + pInfo->lst.push_back(pFunc);
1.157 + } else {
1.158 + TempEntityInfo *te;
1.159 + if (!(te=g_TEManager.GetTempEntityInfo(name)))
1.160 + {
1.161 + return false;
1.162 + }
1.163 +
1.164 + pInfo = new TEHookInfo;
1.165 + pInfo->te = te;
1.166 + pInfo->lst.push_back(pFunc);
1.167 +
1.168 + m_TEHooks->Insert(name, reinterpret_cast<void *>(pInfo));
1.169 + m_HookInfo.push_back(pInfo);
1.170 + }
1.171 +
1.172 + _IncRefCounter();
1.173 +
1.174 + return true;
1.175 +}
1.176 +
1.177 +bool TempEntHooks::RemoveHook(const char *name, IPluginFunction *pFunc)
1.178 +{
1.179 + TEHookInfo *pInfo;
1.180 +
1.181 + if (m_TEHooks->Retrieve(name, reinterpret_cast<void **>(&pInfo)))
1.182 + {
1.183 + SourceHook::List<IPluginFunction *>::iterator iter;
1.184 + if ((iter=pInfo->lst.find(pFunc)) != pInfo->lst.end())
1.185 + {
1.186 + pInfo->lst.erase(iter);
1.187 + if (pInfo->lst.empty())
1.188 + {
1.189 + m_HookInfo.remove(pInfo);
1.190 + m_TEHooks->Delete(name);
1.191 + delete pInfo;
1.192 + }
1.193 + _DecRefCounter();
1.194 + } else {
1.195 + return false;
1.196 + }
1.197 + } else {
1.198 + return false;
1.199 + }
1.200 +
1.201 + return true;
1.202 +}
1.203 +
1.204 +void TempEntHooks::OnPlaybackTempEntity(IRecipientFilter &filter, float delay, const void *pSender, const SendTable *pST, int classID)
1.205 +{
1.206 + TEHookInfo *pInfo;
1.207 + const char *name = g_TEManager.GetNameFromThisPtr(const_cast<void *>(pSender));
1.208 +
1.209 + if (m_TEHooks->Retrieve(name, reinterpret_cast<void **>(&pInfo)))
1.210 + {
1.211 + SourceHook::List<IPluginFunction *>::iterator iter;
1.212 + IPluginFunction *pFunc;
1.213 + size_t size;
1.214 + cell_t res = static_cast<ResultType>(Pl_Continue);
1.215 +
1.216 + TempEntityInfo *oldinfo = g_CurrentTE;
1.217 + g_CurrentTE = pInfo->te;
1.218 + size = _FillInPlayers(g_TEPlayers, &filter);
1.219 +
1.220 + for (iter=pInfo->lst.begin(); iter!=pInfo->lst.end(); iter++)
1.221 + {
1.222 + pFunc = (*iter);
1.223 + pFunc->PushString(name);
1.224 + pFunc->PushArray(g_TEPlayers, size);
1.225 + pFunc->PushCell(size);
1.226 + pFunc->PushFloat(delay);
1.227 + pFunc->Execute(&res);
1.228 +
1.229 + if (res != Pl_Continue)
1.230 + {
1.231 + g_CurrentTE = oldinfo;
1.232 + RETURN_META(MRES_SUPERCEDE);
1.233 + }
1.234 + }
1.235 +
1.236 + g_CurrentTE = oldinfo;
1.237 + RETURN_META(MRES_IGNORED);
1.238 + }
1.239 +}
1.240 +
1.241 +/**********************
1.242 +* *
1.243 +* Temp Entity Natives *
1.244 +* *
1.245 +***********************/
1.246 +
1.247 +TempEntHooks s_TempEntHooks;
1.248 +
1.249 +static cell_t smn_TEStart(IPluginContext *pContext, const cell_t *params)
1.250 +{
1.251 + if (!g_TEManager.IsAvailable())
1.252 + {
1.253 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.254 + }
1.255 +
1.256 + char *name;
1.257 + pContext->LocalToString(params[1], &name);
1.258 +
1.259 + g_CurrentTE = g_TEManager.GetTempEntityInfo(name);
1.260 + if (!g_CurrentTE)
1.261 + {
1.262 + return pContext->ThrowNativeError("Invalid TempEntity name: \"%s\"", name);
1.263 + }
1.264 +
1.265 + return 1;
1.266 +}
1.267 +
1.268 +static cell_t smn_TEWriteNum(IPluginContext *pContext, const cell_t *params)
1.269 +{
1.270 + if (!g_TEManager.IsAvailable())
1.271 + {
1.272 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.273 + }
1.274 + if (!g_CurrentTE)
1.275 + {
1.276 + return pContext->ThrowNativeError("No TempEntity call is in progress");
1.277 + }
1.278 +
1.279 + char *prop;
1.280 + pContext->LocalToString(params[1], &prop);
1.281 +
1.282 + if (!g_CurrentTE->TE_SetEntData(prop, params[2]))
1.283 + {
1.284 + return pContext->ThrowNativeError("Temp entity property \"%s\" not found", prop);
1.285 + }
1.286 +
1.287 + return 1;
1.288 +}
1.289 +
1.290 +static cell_t smn_TEReadNum(IPluginContext *pContext, const cell_t *params)
1.291 +{
1.292 + if (!g_TEManager.IsAvailable())
1.293 + {
1.294 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.295 + }
1.296 + if (!g_CurrentTE)
1.297 + {
1.298 + return pContext->ThrowNativeError("No TempEntity call is in progress");
1.299 + }
1.300 +
1.301 + char *prop;
1.302 + int val;
1.303 + pContext->LocalToString(params[1], &prop);
1.304 +
1.305 + if (!g_CurrentTE->TE_GetEntData(prop, &val))
1.306 + {
1.307 + return pContext->ThrowNativeError("Temp entity property \"%s\" not found", prop);
1.308 + }
1.309 +
1.310 + return val;
1.311 +}
1.312 +
1.313 +static cell_t smn_TE_WriteFloat(IPluginContext *pContext, const cell_t *params)
1.314 +{
1.315 + if (!g_TEManager.IsAvailable())
1.316 + {
1.317 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.318 + }
1.319 + if (!g_CurrentTE)
1.320 + {
1.321 + return pContext->ThrowNativeError("No TempEntity call is in progress");
1.322 + }
1.323 +
1.324 + char *prop;
1.325 + pContext->LocalToString(params[1], &prop);
1.326 +
1.327 + if (!g_CurrentTE->TE_SetEntDataFloat(prop, sp_ctof(params[2])))
1.328 + {
1.329 + return pContext->ThrowNativeError("Temp entity property \"%s\" not found", prop);
1.330 + }
1.331 +
1.332 + return 1;
1.333 +}
1.334 +
1.335 +static cell_t smn_TE_ReadFloat(IPluginContext *pContext, const cell_t *params)
1.336 +{
1.337 + if (!g_TEManager.IsAvailable())
1.338 + {
1.339 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.340 + }
1.341 + if (!g_CurrentTE)
1.342 + {
1.343 + return pContext->ThrowNativeError("No TempEntity call is in progress");
1.344 + }
1.345 +
1.346 + char *prop;
1.347 + float val;
1.348 + pContext->LocalToString(params[1], &prop);
1.349 +
1.350 + if (!g_CurrentTE->TE_GetEntDataFloat(prop, &val))
1.351 + {
1.352 + return pContext->ThrowNativeError("Temp entity property \"%s\" not found", prop);
1.353 + }
1.354 +
1.355 + return sp_ftoc(val);
1.356 +}
1.357 +
1.358 +static cell_t smn_TEWriteVector(IPluginContext *pContext, const cell_t *params)
1.359 +{
1.360 + if (!g_TEManager.IsAvailable())
1.361 + {
1.362 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.363 + }
1.364 + if (!g_CurrentTE)
1.365 + {
1.366 + return pContext->ThrowNativeError("No TempEntity call is in progress");
1.367 + }
1.368 +
1.369 + char *prop;
1.370 + pContext->LocalToString(params[1], &prop);
1.371 +
1.372 + cell_t *addr;
1.373 + pContext->LocalToPhysAddr(params[2], &addr);
1.374 + float vec[3] = {sp_ctof(addr[0]), sp_ctof(addr[1]), sp_ctof(addr[2])};
1.375 +
1.376 + if (!g_CurrentTE->TE_SetEntDataVector(prop, vec))
1.377 + {
1.378 + return pContext->ThrowNativeError("Temp entity property \"%s\" not found", prop);
1.379 + }
1.380 +
1.381 + return 1;
1.382 +}
1.383 +
1.384 +static cell_t smn_TEReadVector(IPluginContext *pContext, const cell_t *params)
1.385 +{
1.386 + if (!g_TEManager.IsAvailable())
1.387 + {
1.388 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.389 + }
1.390 + if (!g_CurrentTE)
1.391 + {
1.392 + return pContext->ThrowNativeError("No TempEntity call is in progress");
1.393 + }
1.394 +
1.395 + char *prop;
1.396 + pContext->LocalToString(params[1], &prop);
1.397 +
1.398 + cell_t *addr;
1.399 + float vec[3];
1.400 + pContext->LocalToPhysAddr(params[2], &addr);
1.401 +
1.402 + if (!g_CurrentTE->TE_GetEntDataVector(prop, vec))
1.403 + {
1.404 + return pContext->ThrowNativeError("Temp entity property \"%s\" not found", prop);
1.405 + }
1.406 +
1.407 + addr[0] = sp_ftoc(vec[0]);
1.408 + addr[1] = sp_ftoc(vec[1]);
1.409 + addr[2] = sp_ftoc(vec[2]);
1.410 +
1.411 + return 1;
1.412 +}
1.413 +
1.414 +static cell_t smn_TEWriteFloatArray(IPluginContext *pContext, const cell_t *params)
1.415 +{
1.416 + if (!g_TEManager.IsAvailable())
1.417 + {
1.418 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.419 + }
1.420 + if (!g_CurrentTE)
1.421 + {
1.422 + return pContext->ThrowNativeError("No TempEntity call is in progress");
1.423 + }
1.424 +
1.425 + char *prop;
1.426 + pContext->LocalToString(params[1], &prop);
1.427 +
1.428 + cell_t *addr;
1.429 + pContext->LocalToPhysAddr(params[2], &addr);
1.430 +
1.431 + if (!g_CurrentTE->TE_SetEntDataFloatArray(prop, addr, params[3]))
1.432 + {
1.433 + return pContext->ThrowNativeError("Temp entity property \"%s\" not found", prop);
1.434 + }
1.435 +
1.436 + return 1;
1.437 +}
1.438 +
1.439 +static cell_t smn_TESend(IPluginContext *pContext, const cell_t *params)
1.440 +{
1.441 + if (!g_TEManager.IsAvailable())
1.442 + {
1.443 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.444 + }
1.445 + if (!g_CurrentTE)
1.446 + {
1.447 + return pContext->ThrowNativeError("No TempEntity call is in progress");
1.448 + }
1.449 +
1.450 + cell_t *cl_array;
1.451 + unsigned int numClients;
1.452 + int client;
1.453 + IGamePlayer *pPlayer = NULL;
1.454 +
1.455 + pContext->LocalToPhysAddr(params[1], &cl_array);
1.456 + numClients = params[2];
1.457 +
1.458 + /* Client validation */
1.459 + for (unsigned int i = 0; i < numClients; i++)
1.460 + {
1.461 + client = cl_array[i];
1.462 + pPlayer = playerhelpers->GetGamePlayer(client);
1.463 +
1.464 + if (!pPlayer)
1.465 + {
1.466 + return pContext->ThrowNativeError("Client index %d is invalid", client);
1.467 + } else if (!pPlayer->IsInGame()) {
1.468 + return pContext->ThrowNativeError("Client %d is not connected", client);
1.469 + }
1.470 + }
1.471 +
1.472 + g_TERecFilter.Reset();
1.473 + g_TERecFilter.Initialize(cl_array, numClients);
1.474 +
1.475 + g_CurrentTE->Send(g_TERecFilter, sp_ctof(params[3]));
1.476 + g_CurrentTE = NULL;
1.477 +
1.478 + return 1;
1.479 +}
1.480 +
1.481 +static cell_t smn_TEIsValidProp(IPluginContext *pContext, const cell_t *params)
1.482 +{
1.483 + if (!g_TEManager.IsAvailable())
1.484 + {
1.485 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.486 + }
1.487 + if (!g_CurrentTE)
1.488 + {
1.489 + return pContext->ThrowNativeError("No TempEntity call is in progress");
1.490 + }
1.491 +
1.492 + char *prop;
1.493 + pContext->LocalToString(params[1], &prop);
1.494 +
1.495 + return g_CurrentTE->IsValidProp(prop) ? 1 : 0;
1.496 +}
1.497 +
1.498 +static cell_t smn_AddTempEntHook(IPluginContext *pContext, const cell_t *params)
1.499 +{
1.500 + char *name;
1.501 + IPluginFunction *pFunc;
1.502 +
1.503 + if (!g_TEManager.IsAvailable())
1.504 + {
1.505 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.506 + }
1.507 +
1.508 + pContext->LocalToString(params[1], &name);
1.509 + pFunc = pContext->GetFunctionById(params[2]);
1.510 + if (!pFunc)
1.511 + {
1.512 + return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
1.513 + }
1.514 +
1.515 + if (!s_TempEntHooks.AddHook(name, pFunc))
1.516 + {
1.517 + return pContext->ThrowNativeError("Invalid TempEntity name: \"%s\"", name);
1.518 + }
1.519 +
1.520 + return 1;
1.521 +}
1.522 +
1.523 +static cell_t smn_RemoveTempEntHook(IPluginContext *pContext, const cell_t *params)
1.524 +{
1.525 + char *name;
1.526 + IPluginFunction *pFunc;
1.527 +
1.528 + if (!g_TEManager.IsAvailable())
1.529 + {
1.530 + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report");
1.531 + }
1.532 +
1.533 + pContext->LocalToString(params[1], &name);
1.534 + pFunc = pContext->GetFunctionById(params[2]);
1.535 + if (!pFunc)
1.536 + {
1.537 + return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
1.538 + }
1.539 +
1.540 + if (!s_TempEntHooks.RemoveHook(name, pFunc))
1.541 + {
1.542 + return pContext->ThrowNativeError("Invalid hooked TempEntity name or function");
1.543 + }
1.544 +
1.545 + return 1;
1.546 +}
1.547 +
1.548 +sp_nativeinfo_t g_TENatives[] =
1.549 +{
1.550 + {"TE_Start", smn_TEStart},
1.551 + {"TE_WriteNum", smn_TEWriteNum},
1.552 + {"TE_ReadNum", smn_TEReadNum},
1.553 + {"TE_WriteFloat", smn_TE_WriteFloat},
1.554 + {"TE_ReadFloat", smn_TE_ReadFloat},
1.555 + {"TE_WriteVector", smn_TEWriteVector},
1.556 + {"TE_ReadVector", smn_TEReadVector},
1.557 + {"TE_WriteAngles", smn_TEWriteVector},
1.558 + {"TE_Send", smn_TESend},
1.559 + {"TE_IsValidProp", smn_TEIsValidProp},
1.560 + {"TE_WriteFloatArray", smn_TEWriteFloatArray},
1.561 + {"AddTempEntHook", smn_AddTempEntHook},
1.562 + {"RemoveTempEntHook", smn_RemoveTempEntHook},
1.563 + {NULL, NULL}
1.564 +};