sourcemod/extensions/sdktools/vsound.cpp
Scott Ehlert 251cced1f8 Spring Cleaning, Part Ichi (1)
Various minor things done to project files
Updated sample extension project file and updated makefile to the new unified version (more changes likely on the way)
Updated regex project file and makefile

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401971
2008-03-30 07:00:22 +00:00

752 lines
20 KiB
C++

/**
* vim: set ts=4 :
* =============================================================================
* SourceMod SDKTools Extension
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include "vsound.h"
#include <IForwardSys.h>
SH_DECL_HOOK8_void(IVEngineServer, EmitAmbientSound, SH_NOATTRIB, 0, int, const Vector &, const char *, float, soundlevel_t, int, int, float);
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);
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);
bool g_InSoundHook = false;
/***************************
* *
* Sound Related Hook Class *
* *
****************************/
size_t SoundHooks::_FillInPlayers(int *pl_array, IRecipientFilter *pFilter)
{
size_t size = static_cast<size_t>(pFilter->GetRecipientCount());
for (size_t i=0; i<size; i++)
{
pl_array[i] = pFilter->GetRecipientIndex(i);
}
return size;
}
void SoundHooks::_IncRefCounter(int type)
{
if (type == NORMAL_SOUND_HOOK)
{
if (m_NormalCount++ == 0)
{
SH_ADD_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound, false);
SH_ADD_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound2, false);
}
}
else if (type == AMBIENT_SOUND_HOOK)
{
if (m_AmbientCount++ == 0)
{
SH_ADD_HOOK_MEMFUNC(IVEngineServer, EmitAmbientSound, engine, this, &SoundHooks::OnEmitAmbientSound, false);
}
}
}
void SoundHooks::_DecRefCounter(int type)
{
if (type == NORMAL_SOUND_HOOK)
{
if (--m_NormalCount == 0)
{
SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound, false);
SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound2, false);
}
}
else if (type == AMBIENT_SOUND_HOOK)
{
if (--m_AmbientCount == 0)
{
SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, EmitAmbientSound, engine, this, &SoundHooks::OnEmitAmbientSound, false);
}
}
}
void SoundHooks::Initialize()
{
plsys->AddPluginsListener(this);
}
void SoundHooks::Shutdown()
{
plsys->RemovePluginsListener(this);
if (m_NormalCount)
{
SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound, false);
SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound2, false);
}
if (m_AmbientCount)
{
SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, EmitAmbientSound, engine, this, &SoundHooks::OnEmitAmbientSound, false);
}
}
void SoundHooks::OnPluginUnloaded(IPlugin *plugin)
{
SoundHookIter iter;
IPluginContext *pContext = plugin->GetBaseContext();
if (m_AmbientCount)
{
for (iter=m_AmbientFuncs.begin(); iter!=m_AmbientFuncs.end(); )
{
if ((*iter)->GetParentContext() == pContext)
{
iter = m_AmbientFuncs.erase(iter);
_DecRefCounter(AMBIENT_SOUND_HOOK);
}
else
{
iter++;
}
}
}
if (m_NormalCount)
{
for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); )
{
if ((*iter)->GetParentContext() == pContext)
{
iter = m_NormalFuncs.erase(iter);
_DecRefCounter(NORMAL_SOUND_HOOK);
}
else
{
iter++;
}
}
}
}
void SoundHooks::AddHook(int type, IPluginFunction *pFunc)
{
if (type == NORMAL_SOUND_HOOK)
{
m_NormalFuncs.push_back(pFunc);
_IncRefCounter(NORMAL_SOUND_HOOK);
}
else if (type == AMBIENT_SOUND_HOOK)
{
m_AmbientFuncs.push_back(pFunc);
_IncRefCounter(AMBIENT_SOUND_HOOK);
}
}
bool SoundHooks::RemoveHook(int type, IPluginFunction *pFunc)
{
SoundHookIter iter;
if (type == NORMAL_SOUND_HOOK)
{
if ((iter=m_NormalFuncs.find(pFunc)) != m_NormalFuncs.end())
{
m_NormalFuncs.erase(iter);
_DecRefCounter(NORMAL_SOUND_HOOK);
return true;
}
else
{
return false;
}
}
else if (type == AMBIENT_SOUND_HOOK)
{
if ((iter=m_AmbientFuncs.find(pFunc)) != m_AmbientFuncs.end())
{
m_AmbientFuncs.erase(iter);
_DecRefCounter(AMBIENT_SOUND_HOOK);
return true;
}
else
{
return false;
}
}
return false;
}
void SoundHooks::OnEmitAmbientSound(int entindex, const Vector &pos, const char *samp, float vol,
soundlevel_t soundlevel, int fFlags, int pitch, float delay)
{
SoundHookIter iter;
IPluginFunction *pFunc;
cell_t vec[3] = {sp_ftoc(pos.x), sp_ftoc(pos.y), sp_ftoc(pos.z)};
cell_t res = static_cast<ResultType>(Pl_Continue);
char buffer[PLATFORM_MAX_PATH];
strcpy(buffer, samp);
for (iter=m_AmbientFuncs.begin(); iter!=m_AmbientFuncs.end(); iter++)
{
pFunc = (*iter);
pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
pFunc->PushCellByRef(&entindex);
pFunc->PushFloatByRef(&vol);
pFunc->PushCellByRef(reinterpret_cast<cell_t *>(&soundlevel));
pFunc->PushCellByRef(&pitch);
pFunc->PushArray(vec, 3, SM_PARAM_COPYBACK);
pFunc->PushCellByRef(&fFlags);
pFunc->PushFloatByRef(&delay);
g_InSoundHook = true;
pFunc->Execute(&res);
g_InSoundHook = false;
switch (res)
{
case Pl_Handled:
case Pl_Stop:
{
RETURN_META(MRES_SUPERCEDE);
}
case Pl_Changed:
{
Vector vec2;
vec2.x = sp_ctof(vec[0]);
vec2.y = sp_ctof(vec[1]);
vec2.z = sp_ctof(vec[2]);
RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::EmitAmbientSound,
(entindex, vec2, buffer, vol, soundlevel, fFlags, pitch, delay));
}
}
}
}
void SoundHooks::OnEmitSound(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSample,
float flVolume, soundlevel_t iSoundlevel, int iFlags, int iPitch, const Vector *pOrigin,
const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions,
float soundtime, int speakerentity)
{
SoundHookIter iter;
IPluginFunction *pFunc;
cell_t res = static_cast<ResultType>(Pl_Continue);
char buffer[PLATFORM_MAX_PATH];
strcpy(buffer, pSample);
for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); iter++)
{
int players[64], size;
size = _FillInPlayers(players, &filter);
pFunc = (*iter);
pFunc->PushArray(players, 64, SM_PARAM_COPYBACK);
pFunc->PushCellByRef(&size);
pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
pFunc->PushCellByRef(&iEntIndex);
pFunc->PushCellByRef(&iChannel);
pFunc->PushFloatByRef(&flVolume);
pFunc->PushCellByRef(reinterpret_cast<cell_t *>(&iSoundlevel));
pFunc->PushCellByRef(&iPitch);
pFunc->PushCellByRef(&iFlags);
g_InSoundHook = true;
pFunc->Execute(&res);
g_InSoundHook = false;
switch (res)
{
case Pl_Handled:
case Pl_Stop:
{
RETURN_META(MRES_SUPERCEDE);
}
case Pl_Changed:
{
CellRecipientFilter crf;
crf.Initialize(players, size);
RETURN_META_NEWPARAMS(
MRES_IGNORED,
static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float, soundlevel_t,
int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound),
(crf, iEntIndex, iChannel, buffer, flVolume, iSoundlevel, iFlags, iPitch, pOrigin,
pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
);
}
}
}
}
void SoundHooks::OnEmitSound2(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSample,
float flVolume, float flAttenuation, int iFlags, int iPitch, const Vector *pOrigin,
const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions,
float soundtime, int speakerentity)
{
SoundHookIter iter;
IPluginFunction *pFunc;
cell_t res = static_cast<ResultType>(Pl_Continue);
cell_t sndlevel = static_cast<cell_t>(ATTN_TO_SNDLVL(flAttenuation));
char buffer[PLATFORM_MAX_PATH];
strcpy(buffer, pSample);
for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); iter++)
{
int players[64], size;
size = _FillInPlayers(players, &filter);
pFunc = (*iter);
pFunc->PushArray(players, 64, SM_PARAM_COPYBACK);
pFunc->PushCellByRef(&size);
pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
pFunc->PushCellByRef(&iEntIndex);
pFunc->PushCellByRef(&iChannel);
pFunc->PushFloatByRef(&flVolume);
pFunc->PushCellByRef(&sndlevel);
pFunc->PushCellByRef(&iPitch);
pFunc->PushCellByRef(&iFlags);
g_InSoundHook = true;
pFunc->Execute(&res);
g_InSoundHook = false;
switch (res)
{
case Pl_Handled:
case Pl_Stop:
{
RETURN_META(MRES_SUPERCEDE);
}
case Pl_Changed:
{
CellRecipientFilter crf;
crf.Initialize(players, size);
RETURN_META_NEWPARAMS(
MRES_IGNORED,
static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float, float,
int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound),
(crf, iEntIndex, iChannel, buffer, flVolume, SNDLVL_TO_ATTN(static_cast<soundlevel_t>(sndlevel)),
iFlags, iPitch, pOrigin, pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
);
}
}
}
}
/************************
* *
* Sound Related Natives *
* *
*************************/
SoundHooks s_SoundHooks;
static cell_t PrefetchSound(IPluginContext *pContext, const cell_t *params)
{
char *name;
pContext->LocalToString(params[1], &name);
engsound->PrefetchSound(name);
return 1;
}
static cell_t GetSoundDuration(IPluginContext *pContext, const cell_t *params)
{
char *name;
pContext->LocalToString(params[1], &name);
return sp_ftoc(engsound->GetSoundDuration(name));
}
static cell_t EmitAmbientSound(IPluginContext *pContext, const cell_t *params)
{
cell_t entity;
Vector pos;
char *name;
float vol, delay;
int pitch, flags, level;
entity = params[3];
cell_t *addr;
pContext->LocalToPhysAddr(params[2], &addr);
pos.x = sp_ctof(addr[0]);
pos.y = sp_ctof(addr[1]);
pos.z = sp_ctof(addr[2]);
pContext->LocalToString(params[1], &name);
vol = sp_ctof(params[6]);
level = params[4];
flags = params[5];
pitch = params[7];
delay = sp_ctof(params[8]);
if (g_InSoundHook)
{
ENGINE_CALL(EmitAmbientSound)(entity, pos, name, vol, (soundlevel_t)level, flags, pitch, delay);
}
else
{
engine->EmitAmbientSound(entity, pos, name, vol, (soundlevel_t)level, flags, pitch, delay);
}
return 1;
}
static cell_t FadeClientVolume(IPluginContext *pContext, const cell_t *params)
{
int client = params[1];
if (client < 1 || client > playerhelpers->GetMaxClients())
{
return pContext->ThrowNativeError("Client index %d is not valid", client);
}
IGamePlayer *player = playerhelpers->GetGamePlayer(client);
if (!player->IsInGame())
{
return pContext->ThrowNativeError("Client index %d is not in game", client);
}
engine->FadeClientVolume(player->GetEdict(),
sp_ctof(params[2]),
sp_ctof(params[3]),
sp_ctof(params[4]),
sp_ctof(params[5]));
return 1;
}
static cell_t StopSound(IPluginContext *pContext, const cell_t *params)
{
int entity = params[1];
int channel = params[2];
char *name;
pContext->LocalToString(params[3], &name);
engsound->StopSound(entity, channel, name);
return 1;
}
static cell_t EmitSound(IPluginContext *pContext, const cell_t *params)
{
cell_t *addr, *pl_addr;
CellRecipientFilter crf;
pContext->LocalToPhysAddr(params[1], &pl_addr);
crf.Initialize(pl_addr, params[2]);
char *sample;
pContext->LocalToString(params[3], &sample);
int entity = params[4];
int channel = params[5];
int level = params[6];
int flags = params[7];
float vol = sp_ctof(params[8]);
int pitch = params[9];
int speakerentity = params[10];
Vector *pOrigin = NULL, origin;
Vector *pDir = NULL, dir;
pContext->LocalToPhysAddr(params[11], &addr);
if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
{
pOrigin = &origin;
origin.x = sp_ctof(addr[0]);
origin.y = sp_ctof(addr[1]);
origin.z = sp_ctof(addr[2]);
}
pContext->LocalToPhysAddr(params[12], &addr);
if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
{
pDir = &dir;
dir.x = sp_ctof(addr[0]);
dir.y = sp_ctof(addr[1]);
dir.z = sp_ctof(addr[2]);
}
bool updatePos = params[13] ? true : false;
float soundtime = sp_ctof(params[14]);
CUtlVector<Vector> *pOrigVec = NULL;
CUtlVector<Vector> origvec;
if (params[0] > 14)
{
pOrigVec = &origvec;
for (cell_t i = 15; i <= params[0]; i++)
{
Vector vec;
pContext->LocalToPhysAddr(params[i], &addr);
vec.x = sp_ctof(addr[0]);
vec.y = sp_ctof(addr[1]);
vec.z = sp_ctof(addr[2]);
origvec.AddToTail(vec);
}
}
/* If we're going to a "local player" and this is a dedicated server,
* intelligently redirect each sound.
*/
if (entity == -2 && engine->IsDedicatedServer())
{
for (cell_t i=0; i<params[2]; i++)
{
cell_t player[1];
player[0] = pl_addr[i];
crf.Reset();
crf.Initialize(player, 1);
if (g_InSoundHook)
{
SH_CALL(enginesoundPatch,
static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float,
soundlevel_t, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
(&IEngineSound::EmitSound))
(crf,
player[0],
channel,
sample,
vol,
(soundlevel_t)level,
flags,
pitch,
pOrigin,
pDir,
pOrigVec,
updatePos,
soundtime,
speakerentity);
}
else
{
engsound->EmitSound(crf,
player[0],
channel,
sample,
vol,
(soundlevel_t)level,
flags,
pitch,
pOrigin,
pDir,
pOrigVec,
updatePos,
soundtime,
speakerentity);
}
}
} else {
if (g_InSoundHook)
{
SH_CALL(enginesoundPatch,
static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float,
soundlevel_t, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
(&IEngineSound::EmitSound))
(crf,
entity,
channel,
sample,
vol,
(soundlevel_t)level,
flags,
pitch,
pOrigin,
pDir,
pOrigVec,
updatePos,
soundtime,
speakerentity);
}
else
{
engsound->EmitSound(crf,
entity,
channel,
sample,
vol,
(soundlevel_t)level,
flags,
pitch,
pOrigin,
pDir,
pOrigVec,
updatePos,
soundtime,
speakerentity);
}
}
return 1;
}
static cell_t EmitSentence(IPluginContext *pContext, const cell_t *params)
{
cell_t *addr;
CellRecipientFilter crf;
pContext->LocalToPhysAddr(params[1], &addr);
crf.Initialize(addr, params[2]);
int sentence = params[3];
int entity = params[4];
int channel = params[5];
int level = params[6];
int flags = params[7];
float vol = sp_ctof(params[8]);
int pitch = params[9];
int speakerentity = params[10];
Vector *pOrigin = NULL, origin;
Vector *pDir = NULL, dir;
pContext->LocalToPhysAddr(params[11], &addr);
if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
{
pOrigin = &origin;
origin.x = sp_ctof(addr[0]);
origin.y = sp_ctof(addr[1]);
origin.z = sp_ctof(addr[2]);
}
pContext->LocalToPhysAddr(params[12], &addr);
if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
{
pDir = &dir;
dir.x = sp_ctof(addr[0]);
dir.y = sp_ctof(addr[1]);
dir.z = sp_ctof(addr[2]);
}
bool updatePos = params[13] ? true : false;
float soundtime = sp_ctof(params[14]);
CUtlVector<Vector> *pOrigVec = NULL;
CUtlVector<Vector> origvec;
if (params[0] > 14)
{
pOrigVec = &origvec;
for (cell_t i = 15; i <= params[0]; i++)
{
Vector vec;
pContext->LocalToPhysAddr(params[i], &addr);
vec.x = sp_ctof(addr[0]);
vec.y = sp_ctof(addr[1]);
vec.z = sp_ctof(addr[2]);
origvec.AddToTail(vec);
}
}
engsound->EmitSentenceByIndex(crf,
entity,
channel,
sentence,
vol,
(soundlevel_t)level,
flags,
pitch,
pOrigin,
pDir,
pOrigVec,
updatePos,
soundtime,
speakerentity);
return 1;
}
static cell_t smn_AddAmbientSoundHook(IPluginContext *pContext, const cell_t *params)
{
IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
if (!pFunc)
{
return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
}
s_SoundHooks.AddHook(AMBIENT_SOUND_HOOK, pFunc);
return 1;
}
static cell_t smn_AddNormalSoundHook(IPluginContext *pContext, const cell_t *params)
{
IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
if (!pFunc)
{
return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
}
s_SoundHooks.AddHook(NORMAL_SOUND_HOOK, pFunc);
return 1;
}
static cell_t smn_RemoveAmbientSoundHook(IPluginContext *pContext, const cell_t *params)
{
IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
if (!pFunc)
{
return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
}
if (!s_SoundHooks.RemoveHook(AMBIENT_SOUND_HOOK, pFunc))
{
return pContext->ThrowNativeError("Invalid hooked function");
}
return 1;
}
static cell_t smn_RemoveNormalSoundHook(IPluginContext *pContext, const cell_t *params)
{
IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
if (!pFunc)
{
return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
}
if (!s_SoundHooks.RemoveHook(NORMAL_SOUND_HOOK, pFunc))
{
return pContext->ThrowNativeError("Invalid hooked function");
}
return 1;
}
sp_nativeinfo_t g_SoundNatives[] =
{
{"EmitAmbientSound", EmitAmbientSound},
{"EmitSentence", EmitSentence},
{"EmitSound", EmitSound},
{"FadeClientVolume", FadeClientVolume},
{"GetSoundDuration", GetSoundDuration},
{"PrefetchSound", PrefetchSound},
{"StopSound", StopSound},
{"AddAmbientSoundHook", smn_AddAmbientSoundHook},
{"AddNormalSoundHook", smn_AddNormalSoundHook},
{"RemoveAmbientSoundHook", smn_RemoveAmbientSoundHook},
{"RemoveNormalSoundHook", smn_RemoveNormalSoundHook},
{NULL, NULL},
};