1.1 --- a/extensions/sdktools/vsound.cpp Sat Dec 01 00:37:47 2007 +0000
1.2 +++ b/extensions/sdktools/vsound.cpp Sun Dec 02 01:51:48 2007 +0000
1.3 @@ -29,8 +29,333 @@
1.4 * Version: $Id$
1.5 */
1.6
1.7 -#include "extension.h"
1.8 -#include "CellRecipientFilter.h"
1.9 +#include "vsound.h"
1.10 +#include <IForwardSys.h>
1.11 +
1.12 +SH_DECL_HOOK8_void(IVEngineServer, EmitAmbientSound, SH_NOATTRIB, 0, int, const Vector &, const char *, float, soundlevel_t, int, int, float);
1.13 +SH_DECL_HOOK14_void(IEngineSound, EmitSound, SH_NOATTRIB, 0, IRecipientFilter &, int, int, const char *, float, float, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int);
1.14 +SH_DECL_HOOK14_void(IEngineSound, EmitSound, SH_NOATTRIB, 1, IRecipientFilter &, int, int, const char *, float, soundlevel_t, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int);
1.15 +
1.16 +bool g_InSoundHook = false;
1.17 +
1.18 +/***************************
1.19 +* *
1.20 +* Sound Related Hook Class *
1.21 +* *
1.22 +****************************/
1.23 +
1.24 +size_t SoundHooks::_FillInPlayers(int *pl_array, IRecipientFilter *pFilter)
1.25 +{
1.26 + size_t size = static_cast<size_t>(pFilter->GetRecipientCount());
1.27 +
1.28 + for (size_t i=0; i<size; i++)
1.29 + {
1.30 + pl_array[i] = pFilter->GetRecipientIndex(i);
1.31 + }
1.32 +
1.33 + return size;
1.34 +}
1.35 +
1.36 +void SoundHooks::_IncRefCounter(int type)
1.37 +{
1.38 + if (type == NORMAL_SOUND_HOOK)
1.39 + {
1.40 + if (m_NormalCount++ == 0)
1.41 + {
1.42 + SH_ADD_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound, false);
1.43 + SH_ADD_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound2, false);
1.44 + }
1.45 + }
1.46 + else if (type == AMBIENT_SOUND_HOOK)
1.47 + {
1.48 + if (m_AmbientCount++ == 0)
1.49 + {
1.50 + SH_ADD_HOOK_MEMFUNC(IVEngineServer, EmitAmbientSound, engine, this, &SoundHooks::OnEmitAmbientSound, false);
1.51 + }
1.52 + }
1.53 +}
1.54 +
1.55 +void SoundHooks::_DecRefCounter(int type)
1.56 +{
1.57 + if (type == NORMAL_SOUND_HOOK)
1.58 + {
1.59 + if (--m_NormalCount == 0)
1.60 + {
1.61 + SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound, false);
1.62 + SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound2, false);
1.63 + }
1.64 + }
1.65 + else if (type == AMBIENT_SOUND_HOOK)
1.66 + {
1.67 + if (--m_AmbientCount == 0)
1.68 + {
1.69 + SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, EmitAmbientSound, engine, this, &SoundHooks::OnEmitAmbientSound, false);
1.70 + }
1.71 + }
1.72 +}
1.73 +
1.74 +void SoundHooks::Initialize()
1.75 +{
1.76 + plsys->AddPluginsListener(this);
1.77 +}
1.78 +
1.79 +void SoundHooks::Shutdown()
1.80 +{
1.81 + plsys->RemovePluginsListener(this);
1.82 + if (m_NormalCount)
1.83 + {
1.84 + SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound, false);
1.85 + SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound2, false);
1.86 + }
1.87 + if (m_AmbientCount)
1.88 + {
1.89 + SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, EmitAmbientSound, engine, this, &SoundHooks::OnEmitAmbientSound, false);
1.90 + }
1.91 +}
1.92 +
1.93 +void SoundHooks::OnPluginUnloaded(IPlugin *plugin)
1.94 +{
1.95 + SoundHookIter iter;
1.96 + IPluginContext *pContext = plugin->GetBaseContext();
1.97 +
1.98 + if (m_AmbientCount)
1.99 + {
1.100 + for (iter=m_AmbientFuncs.begin(); iter!=m_AmbientFuncs.end(); )
1.101 + {
1.102 + if ((*iter)->GetParentContext() == pContext)
1.103 + {
1.104 + iter = m_AmbientFuncs.erase(iter);
1.105 + _DecRefCounter(AMBIENT_SOUND_HOOK);
1.106 + }
1.107 + else
1.108 + {
1.109 + iter++;
1.110 + }
1.111 + }
1.112 + }
1.113 + if (m_NormalCount)
1.114 + {
1.115 + for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); )
1.116 + {
1.117 + if ((*iter)->GetParentContext() == pContext)
1.118 + {
1.119 + iter = m_NormalFuncs.erase(iter);
1.120 + _DecRefCounter(NORMAL_SOUND_HOOK);
1.121 + }
1.122 + else
1.123 + {
1.124 + iter++;
1.125 + }
1.126 + }
1.127 + }
1.128 +}
1.129 +
1.130 +void SoundHooks::AddHook(int type, IPluginFunction *pFunc)
1.131 +{
1.132 + if (type == NORMAL_SOUND_HOOK)
1.133 + {
1.134 + m_NormalFuncs.push_back(pFunc);
1.135 + _IncRefCounter(NORMAL_SOUND_HOOK);
1.136 + }
1.137 + else if (type == AMBIENT_SOUND_HOOK)
1.138 + {
1.139 + m_AmbientFuncs.push_back(pFunc);
1.140 + _IncRefCounter(AMBIENT_SOUND_HOOK);
1.141 + }
1.142 +}
1.143 +
1.144 +bool SoundHooks::RemoveHook(int type, IPluginFunction *pFunc)
1.145 +{
1.146 + SoundHookIter iter;
1.147 + if (type == NORMAL_SOUND_HOOK)
1.148 + {
1.149 + if ((iter=m_NormalFuncs.find(pFunc)) != m_NormalFuncs.end())
1.150 + {
1.151 + m_NormalFuncs.erase(iter);
1.152 + _DecRefCounter(NORMAL_SOUND_HOOK);
1.153 + return true;
1.154 + }
1.155 + else
1.156 + {
1.157 + return false;
1.158 + }
1.159 + }
1.160 + else if (type == AMBIENT_SOUND_HOOK)
1.161 + {
1.162 + if ((iter=m_AmbientFuncs.find(pFunc)) != m_AmbientFuncs.end())
1.163 + {
1.164 + m_AmbientFuncs.erase(iter);
1.165 + _DecRefCounter(AMBIENT_SOUND_HOOK);
1.166 + return true;
1.167 + }
1.168 + else
1.169 + {
1.170 + return false;
1.171 + }
1.172 + }
1.173 +
1.174 + return false;
1.175 +}
1.176 +
1.177 +void SoundHooks::OnEmitAmbientSound(int entindex, const Vector &pos, const char *samp, float vol,
1.178 + soundlevel_t soundlevel, int fFlags, int pitch, float delay)
1.179 +{
1.180 + SoundHookIter iter;
1.181 + IPluginFunction *pFunc;
1.182 + cell_t vec[3] = {sp_ftoc(pos.x), sp_ftoc(pos.y), sp_ftoc(pos.z)};
1.183 + cell_t res = static_cast<ResultType>(Pl_Continue);
1.184 + char buffer[PLATFORM_MAX_PATH];
1.185 + strcpy(buffer, samp);
1.186 +
1.187 + for (iter=m_AmbientFuncs.begin(); iter!=m_AmbientFuncs.end(); iter++)
1.188 + {
1.189 + pFunc = (*iter);
1.190 + pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
1.191 + pFunc->PushCellByRef(&entindex);
1.192 + pFunc->PushFloatByRef(&vol);
1.193 + pFunc->PushCellByRef(reinterpret_cast<cell_t *>(&soundlevel));
1.194 + pFunc->PushCellByRef(&pitch);
1.195 + pFunc->PushArray(vec, 3, SM_PARAM_COPYBACK);
1.196 + pFunc->PushCellByRef(&fFlags);
1.197 + pFunc->PushFloatByRef(&delay);
1.198 + g_InSoundHook = true;
1.199 + pFunc->Execute(&res);
1.200 + g_InSoundHook = false;
1.201 +
1.202 + switch (res)
1.203 + {
1.204 + case Pl_Handled:
1.205 + case Pl_Stop:
1.206 + {
1.207 + RETURN_META(MRES_SUPERCEDE);
1.208 + }
1.209 + case Pl_Changed:
1.210 + {
1.211 + Vector vec2;
1.212 + vec2.x = sp_ctof(vec[0]);
1.213 + vec2.y = sp_ctof(vec[1]);
1.214 + vec2.z = sp_ctof(vec[2]);
1.215 + RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::EmitAmbientSound,
1.216 + (entindex, vec2, buffer, vol, soundlevel, fFlags, pitch, delay));
1.217 + }
1.218 + }
1.219 + }
1.220 +}
1.221 +
1.222 +void SoundHooks::OnEmitSound(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSample,
1.223 + float flVolume, soundlevel_t iSoundlevel, int iFlags, int iPitch, const Vector *pOrigin,
1.224 + const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions,
1.225 + float soundtime, int speakerentity)
1.226 +{
1.227 + SoundHookIter iter;
1.228 + IPluginFunction *pFunc;
1.229 + cell_t res = static_cast<ResultType>(Pl_Continue);
1.230 + char buffer[PLATFORM_MAX_PATH];
1.231 + strcpy(buffer, pSample);
1.232 +
1.233 + for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); iter++)
1.234 + {
1.235 + int players[64], size;
1.236 + size = _FillInPlayers(players, &filter);
1.237 + pFunc = (*iter);
1.238 +
1.239 + pFunc->PushArray(players, 64, SM_PARAM_COPYBACK);
1.240 + pFunc->PushCellByRef(&size);
1.241 + pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
1.242 + pFunc->PushCellByRef(&iEntIndex);
1.243 + pFunc->PushCellByRef(&iChannel);
1.244 + pFunc->PushFloatByRef(&flVolume);
1.245 + pFunc->PushCellByRef(reinterpret_cast<cell_t *>(&iSoundlevel));
1.246 + pFunc->PushCellByRef(&iPitch);
1.247 + pFunc->PushCellByRef(&iFlags);
1.248 + g_InSoundHook = true;
1.249 + pFunc->Execute(&res);
1.250 + g_InSoundHook = false;
1.251 +
1.252 + switch (res)
1.253 + {
1.254 + case Pl_Handled:
1.255 + case Pl_Stop:
1.256 + {
1.257 + RETURN_META(MRES_SUPERCEDE);
1.258 + }
1.259 + case Pl_Changed:
1.260 + {
1.261 + CellRecipientFilter crf;
1.262 + crf.Initialize(players, size);
1.263 + RETURN_META_NEWPARAMS(
1.264 + MRES_IGNORED,
1.265 + static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float, soundlevel_t,
1.266 + int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound),
1.267 + (crf, iEntIndex, iChannel, buffer, flVolume, iSoundlevel, iFlags, iPitch, pOrigin,
1.268 + pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
1.269 + );
1.270 + }
1.271 + }
1.272 + }
1.273 +}
1.274 +
1.275 +void SoundHooks::OnEmitSound2(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSample,
1.276 + float flVolume, float flAttenuation, int iFlags, int iPitch, const Vector *pOrigin,
1.277 + const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions,
1.278 + float soundtime, int speakerentity)
1.279 +{
1.280 + SoundHookIter iter;
1.281 + IPluginFunction *pFunc;
1.282 + cell_t res = static_cast<ResultType>(Pl_Continue);
1.283 + cell_t sndlevel = static_cast<cell_t>(ATTN_TO_SNDLVL(flAttenuation));
1.284 + char buffer[PLATFORM_MAX_PATH];
1.285 + strcpy(buffer, pSample);
1.286 +
1.287 + for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); iter++)
1.288 + {
1.289 + int players[64], size;
1.290 + size = _FillInPlayers(players, &filter);
1.291 + pFunc = (*iter);
1.292 +
1.293 + pFunc->PushArray(players, 64, SM_PARAM_COPYBACK);
1.294 + pFunc->PushCellByRef(&size);
1.295 + pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
1.296 + pFunc->PushCellByRef(&iEntIndex);
1.297 + pFunc->PushCellByRef(&iChannel);
1.298 + pFunc->PushFloatByRef(&flVolume);
1.299 + pFunc->PushCellByRef(&sndlevel);
1.300 + pFunc->PushCellByRef(&iPitch);
1.301 + pFunc->PushCellByRef(&iFlags);
1.302 + g_InSoundHook = true;
1.303 + pFunc->Execute(&res);
1.304 + g_InSoundHook = false;
1.305 +
1.306 + switch (res)
1.307 + {
1.308 + case Pl_Handled:
1.309 + case Pl_Stop:
1.310 + {
1.311 + RETURN_META(MRES_SUPERCEDE);
1.312 + }
1.313 + case Pl_Changed:
1.314 + {
1.315 + CellRecipientFilter crf;
1.316 + crf.Initialize(players, size);
1.317 + RETURN_META_NEWPARAMS(
1.318 + MRES_IGNORED,
1.319 + static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float, float,
1.320 + int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound),
1.321 + (crf, iEntIndex, iChannel, buffer, flVolume, SNDLVL_TO_ATTN(static_cast<soundlevel_t>(sndlevel)),
1.322 + iFlags, iPitch, pOrigin, pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
1.323 + );
1.324 + }
1.325 + }
1.326 + }
1.327 +}
1.328 +
1.329 +/************************
1.330 +* *
1.331 +* Sound Related Natives *
1.332 +* *
1.333 +*************************/
1.334 +
1.335 +SoundHooks s_SoundHooks;
1.336
1.337 static cell_t PrefetchSound(IPluginContext *pContext, const cell_t *params)
1.338 {
1.339 @@ -74,7 +399,14 @@
1.340 pitch = params[7];
1.341 delay = sp_ctof(params[8]);
1.342
1.343 - engine->EmitAmbientSound(entity, pos, name, vol, (soundlevel_t)level, flags, pitch, delay);
1.344 + if (g_InSoundHook)
1.345 + {
1.346 + ENGINE_CALL(EmitAmbientSound)(entity, pos, name, vol, (soundlevel_t)level, flags, pitch, delay);
1.347 + }
1.348 + else
1.349 + {
1.350 + engine->EmitAmbientSound(entity, pos, name, vol, (soundlevel_t)level, flags, pitch, delay);
1.351 + }
1.352
1.353 return 1;
1.354 }
1.355 @@ -186,8 +518,54 @@
1.356 player[0] = pl_addr[i];
1.357 crf.Reset();
1.358 crf.Initialize(player, 1);
1.359 - engsound->EmitSound(crf,
1.360 - player[0],
1.361 + if (g_InSoundHook)
1.362 + {
1.363 + SH_CALL(enginesoundPatch,
1.364 + static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float,
1.365 + soundlevel_t, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
1.366 + (&IEngineSound::EmitSound))
1.367 + (crf,
1.368 + player[0],
1.369 + channel,
1.370 + sample,
1.371 + vol,
1.372 + (soundlevel_t)level,
1.373 + flags,
1.374 + pitch,
1.375 + pOrigin,
1.376 + pDir,
1.377 + pOrigVec,
1.378 + updatePos,
1.379 + soundtime,
1.380 + speakerentity);
1.381 + }
1.382 + else
1.383 + {
1.384 + engsound->EmitSound(crf,
1.385 + player[0],
1.386 + channel,
1.387 + sample,
1.388 + vol,
1.389 + (soundlevel_t)level,
1.390 + flags,
1.391 + pitch,
1.392 + pOrigin,
1.393 + pDir,
1.394 + pOrigVec,
1.395 + updatePos,
1.396 + soundtime,
1.397 + speakerentity);
1.398 + }
1.399 + }
1.400 + } else {
1.401 + if (g_InSoundHook)
1.402 + {
1.403 + SH_CALL(enginesoundPatch,
1.404 + static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float,
1.405 + soundlevel_t, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
1.406 + (&IEngineSound::EmitSound))
1.407 + (crf,
1.408 + entity,
1.409 channel,
1.410 sample,
1.411 vol,
1.412 @@ -201,21 +579,23 @@
1.413 soundtime,
1.414 speakerentity);
1.415 }
1.416 - } else {
1.417 - engsound->EmitSound(crf,
1.418 - entity,
1.419 - channel,
1.420 - sample,
1.421 - vol,
1.422 - (soundlevel_t)level,
1.423 - flags,
1.424 - pitch,
1.425 - pOrigin,
1.426 - pDir,
1.427 - pOrigVec,
1.428 - updatePos,
1.429 - soundtime,
1.430 - speakerentity);
1.431 + else
1.432 + {
1.433 + engsound->EmitSound(crf,
1.434 + entity,
1.435 + channel,
1.436 + sample,
1.437 + vol,
1.438 + (soundlevel_t)level,
1.439 + flags,
1.440 + pitch,
1.441 + pOrigin,
1.442 + pDir,
1.443 + pOrigVec,
1.444 + updatePos,
1.445 + soundtime,
1.446 + speakerentity);
1.447 + }
1.448 }
1.449
1.450 return 1;
1.451 @@ -296,14 +676,76 @@
1.452 return 1;
1.453 }
1.454
1.455 +static cell_t smn_AddAmbientSoundHook(IPluginContext *pContext, const cell_t *params)
1.456 +{
1.457 + IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
1.458 + if (!pFunc)
1.459 + {
1.460 + return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
1.461 + }
1.462 +
1.463 + s_SoundHooks.AddHook(AMBIENT_SOUND_HOOK, pFunc);
1.464 +
1.465 + return 1;
1.466 +}
1.467 +
1.468 +static cell_t smn_AddNormalSoundHook(IPluginContext *pContext, const cell_t *params)
1.469 +{
1.470 + IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
1.471 + if (!pFunc)
1.472 + {
1.473 + return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
1.474 + }
1.475 +
1.476 + s_SoundHooks.AddHook(NORMAL_SOUND_HOOK, pFunc);
1.477 +
1.478 + return 1;
1.479 +}
1.480 +
1.481 +static cell_t smn_RemoveAmbientSoundHook(IPluginContext *pContext, const cell_t *params)
1.482 +{
1.483 + IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
1.484 + if (!pFunc)
1.485 + {
1.486 + return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
1.487 + }
1.488 +
1.489 + if (!s_SoundHooks.RemoveHook(AMBIENT_SOUND_HOOK, pFunc))
1.490 + {
1.491 + return pContext->ThrowNativeError("Invalid hooked function");
1.492 + }
1.493 +
1.494 + return 1;
1.495 +}
1.496 +
1.497 +static cell_t smn_RemoveNormalSoundHook(IPluginContext *pContext, const cell_t *params)
1.498 +{
1.499 + IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
1.500 + if (!pFunc)
1.501 + {
1.502 + return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
1.503 + }
1.504 +
1.505 + if (!s_SoundHooks.RemoveHook(NORMAL_SOUND_HOOK, pFunc))
1.506 + {
1.507 + return pContext->ThrowNativeError("Invalid hooked function");
1.508 + }
1.509 +
1.510 + return 1;
1.511 +}
1.512 +
1.513 sp_nativeinfo_t g_SoundNatives[] =
1.514 {
1.515 - {"EmitAmbientSound", EmitAmbientSound},
1.516 - {"EmitSentence", EmitSentence},
1.517 - {"EmitSound", EmitSound},
1.518 - {"FadeClientVolume", FadeClientVolume},
1.519 - {"GetSoundDuration", GetSoundDuration},
1.520 - {"PrefetchSound", PrefetchSound},
1.521 - {"StopSound", StopSound},
1.522 - {NULL, NULL},
1.523 + {"EmitAmbientSound", EmitAmbientSound},
1.524 + {"EmitSentence", EmitSentence},
1.525 + {"EmitSound", EmitSound},
1.526 + {"FadeClientVolume", FadeClientVolume},
1.527 + {"GetSoundDuration", GetSoundDuration},
1.528 + {"PrefetchSound", PrefetchSound},
1.529 + {"StopSound", StopSound},
1.530 + {"AddAmbientSoundHook", smn_AddAmbientSoundHook},
1.531 + {"AddNormalSoundHook", smn_AddNormalSoundHook},
1.532 + {"RemoveAmbientSoundHook", smn_RemoveAmbientSoundHook},
1.533 + {"RemoveNormalSoundHook", smn_RemoveNormalSoundHook},
1.534 + {NULL, NULL},
1.535 };