forked from WeSTManCoder/VoiceHook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.cpp
144 lines (109 loc) · 4.71 KB
/
extension.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Sample 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 "extension.h"
#include "CDetour/detours.h"
#include <iclient.h>
#include <iserver.h>
#include <icvar.h>
#include "netmessages.h"
#include <tier1/interface.h>
#define GAMECONFIG_FILE "voicehook"
VoiceHook g_VoiceHook;
SMEXT_LINK(&g_VoiceHook);
IGameConfig *g_pGameConf = nullptr;
CDetour *g_pSV_BroadcastVoiceDataDetor = nullptr;
IForward *g_pVoiceToClientForward = nullptr;
IServer *g_pIServer = nullptr;
ConVar *g_pVoiceEnable = nullptr;
DETOUR_DECL_STATIC3(SV_BroadcastVoiceData, void, IClient *, cl, int, nBytes, char *, data) {
if (!g_pVoiceEnable->GetInt()) return;
SVC_VoiceData voiceData;
int iClient = cl->GetPlayerSlot();
int iBytes = nBytes * 8;
voiceData.m_nFromClient = iClient;
voiceData.m_DataOut = data;
for (int i = 0; i < g_pIServer->GetClientCount(); i++) {
IClient *pDestClient = g_pIServer->GetClient(i);
if (pDestClient == cl || !pDestClient->IsActive() || !pDestClient->IsHearingClient(iClient)) continue;
//FORWARD
g_pVoiceToClientForward->PushCell(iClient+1);
g_pVoiceToClientForward->PushCell(pDestClient->GetPlayerSlot()+1);
int bResult = 1;
g_pVoiceToClientForward->PushCellByRef(&bResult);
cell_t result = Pl_Continue;
g_pVoiceToClientForward->Execute(&result);
if (result >= Pl_Handled) return;
if (!bResult && result == Pl_Changed) continue;
voiceData.m_nLength = iBytes;
pDestClient->SendNetMsg(voiceData);
}
}
bool VoiceHook::SDK_OnLoad(char *error, size_t maxlength, bool late) {
if (!gameconfs->LoadGameConfigFile(GAMECONFIG_FILE, &g_pGameConf, error, maxlength)) {
g_pSM->Format(error, maxlength, "Failed to load gameconfig: %s", GAMECONFIG_FILE);
return false;
}
CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
void *pAddr = nullptr;
if (!g_pGameConf->GetMemSig("sv", &pAddr) || !pAddr) {
g_pSM->Format(error, maxlength, "Failed get offset of sv variable");
return false;
}
g_pIServer = reinterpret_cast<IServer *>(pAddr);
ICvar *cvar = nullptr;
Sys_LoadInterface("engine", VENGINE_CVAR_INTERFACE_VERSION, nullptr, (void**)&cvar);
if (!cvar) {
g_pSM->Format(error, maxlength, "ICVAR not found (%s)", VENGINE_CVAR_INTERFACE_VERSION);
return false;
}
g_pVoiceEnable = cvar->FindVar("sv_voiceenable");
if (!g_pVoiceEnable) {
g_pSM->Format(error, maxlength, "Failed to find cvar: sv_voiceenable");
return false;
}
return true;
}
void VoiceHook::SDK_OnAllLoaded() {
if (g_pSV_BroadcastVoiceDataDetor == nullptr) {
g_pSV_BroadcastVoiceDataDetor = DETOUR_CREATE_STATIC(SV_BroadcastVoiceData, "SV_BroadcastVoiceData");
if (g_pSV_BroadcastVoiceDataDetor) g_pSV_BroadcastVoiceDataDetor->EnableDetour();
else g_pSM->LogError(myself, "Failed to setup ProcessVoiceData detour");
g_pVoiceToClientForward = forwards->CreateForward("VoiceHook_OnClientVoiceTo", ET_Event, 3, nullptr, Param_Cell, Param_Cell, Param_CellByRef);
}
}
void VoiceHook::SDK_OnUnload() {
forwards->ReleaseForward(g_pVoiceToClientForward);
if (g_pSV_BroadcastVoiceDataDetor) {
g_pSV_BroadcastVoiceDataDetor->Destroy();
g_pSV_BroadcastVoiceDataDetor = nullptr;
}
gameconfs->CloseGameConfigFile(g_pGameConf);
}