/
amxxapi.cpp
282 lines (226 loc) · 8.68 KB
/
amxxapi.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
//
// Engine Module
//
#include "engine.h"
BOOL CheckForPublic(const char *publicname);
void CreateDetours();
void DestroyDetours();
CDetour *LightStyleDetour = NULL;
HLTypeConversion TypeConversion;
int AmxStringToEngine(AMX *amx, cell param, int &len)
{
char *szString = MF_GetAmxString(amx, param, 0, &len);
return ALLOC_STRING(szString);
}
void ClearHooks()
{
size_t i;
for (i=0; i<Touches.length(); i++)
delete Touches[i];
for (i=0; i<Impulses.length(); i++)
delete Impulses[i];
for (i=0; i<Thinks.length(); i++)
delete Thinks[i];
Touches.clear();
Impulses.clear();
Thinks.clear();
}
void OnAmxxAttach()
{
pfnTouchForward = 0;
pfnThinkForward = 0;
PlayerPreThinkForward = 0;
PlayerPostThinkForward = 0;
ClientKillForward = 0;
ClientImpulseForward = 0;
CmdStartForward = 0;
StartFrameForward = 0;
MF_AddNatives(ent_Natives);
MF_AddNewNatives(ent_NewNatives);
MF_AddNatives(engine_Natives);
MF_AddNewNatives(engine_NewNatives);
MF_AddNatives(global_Natives);
memset(glinfo.szLastLights, 0x0, 128);
memset(glinfo.szRealLights, 0x0, 128);
glinfo.bCheckLights = false;
CreateDetours();
}
void OnAmxxDetach()
{
DestroyDetours();
}
void OnPluginsLoaded()
{
TypeConversion.init();
g_CameraCount=0;
pfnThinkForward = MF_RegisterForward("pfn_think", ET_STOP, FP_CELL, FP_DONE); // done
PlayerPreThinkForward = MF_RegisterForward("client_PreThink", ET_STOP, FP_CELL, FP_DONE); // done
PlayerPostThinkForward = MF_RegisterForward("client_PostThink", ET_STOP, FP_CELL, FP_DONE); // done
ClientKillForward = MF_RegisterForward("client_kill", ET_STOP, FP_CELL, FP_DONE); // done
ClientImpulseForward = MF_RegisterForward("client_impulse", ET_STOP, FP_CELL, FP_CELL, FP_DONE); // done
CmdStartForward = MF_RegisterForward("client_cmdStart", ET_STOP, FP_CELL, FP_DONE); // done
StartFrameForward = MF_RegisterForward("server_frame", ET_IGNORE, FP_DONE); // done
DispatchKeyForward = MF_RegisterForward("pfn_keyvalue", ET_STOP, FP_CELL, FP_DONE); // done
PlaybackForward = MF_RegisterForward("pfn_playbackevent", ET_STOP, FP_CELL, FP_CELL, FP_CELL, FP_FLOAT, FP_ARRAY, FP_ARRAY, FP_FLOAT, FP_FLOAT, FP_CELL, FP_CELL, FP_CELL, FP_CELL, FP_DONE); // done
SpawnForward = MF_RegisterForward("pfn_spawn", ET_STOP, FP_CELL, FP_DONE); // done
pfnTouchForward = MF_RegisterForward("pfn_touch", ET_STOP, FP_CELL, FP_CELL, FP_DONE); // done
VexdTouchForward = MF_RegisterForward("vexd_pfntouch", ET_IGNORE, FP_CELL, FP_CELL, FP_DONE); // done
VexdServerForward = MF_RegisterForward("ServerFrame", ET_IGNORE, FP_DONE); // done
// Reset all standard engine callbacks
// These will be reset through native calls, if need be
g_pFunctionTable->pfnAddToFullPack=NULL;
g_pFunctionTable->pfnKeyValue=NULL;
if (CheckForPublic("pfn_keyvalue"))
g_pFunctionTable->pfnKeyValue=KeyValue;
g_pengfuncsTable->pfnPlaybackEvent=NULL; // "pfn_playbackevent"
if (CheckForPublic("pfn_playbackevent"))
g_pengfuncsTable->pfnPlaybackEvent=PlaybackEvent;
g_pFunctionTable->pfnPlayerPreThink=NULL; // "client_PreThink"
if (CheckForPublic("client_PreThink"))
g_pFunctionTable->pfnPlayerPreThink=PlayerPreThink;
g_pFunctionTable_Post->pfnPlayerPostThink=NULL; // "client_PostThink"
if (CheckForPublic("client_PostThink"))
g_pFunctionTable->pfnPlayerPostThink=PlayerPostThink_Post;
g_pFunctionTable->pfnSpawn=NULL; // "pfn_spawn"
//if (CheckForPublic("pfn_spawn")) // JGHG: I commented this if out because we always need the Spawn to precache the rocket mdl used with SetView native
g_pFunctionTable->pfnSpawn=Spawn;
g_pFunctionTable->pfnClientKill=NULL; // "client_kill"
if (CheckForPublic("client_kill"))
g_pFunctionTable->pfnClientKill=ClientKill;
g_pFunctionTable->pfnCmdStart=NULL; // "client_impulse","register_impulse","client_cmdStart"
if (CheckForPublic("client_impulse") || CheckForPublic("client_cmdStart"))
g_pFunctionTable->pfnCmdStart=CmdStart;
g_pFunctionTable->pfnThink=NULL; // "pfn_think", "register_think"
if (CheckForPublic("pfn_think"))
g_pFunctionTable->pfnThink=Think;
g_pFunctionTable->pfnStartFrame=NULL; // "server_frame","ServerFrame"
if (CheckForPublic("server_frame"))
g_pFunctionTable->pfnStartFrame=StartFrame;
if (CheckForPublic("ServerFrame"))
g_pFunctionTable->pfnStartFrame=StartFrame;
g_pFunctionTable->pfnTouch=NULL; // "pfn_touch","vexd_pfntouch"
if (CheckForPublic("pfn_touch"))
g_pFunctionTable->pfnTouch=pfnTouch;
if (CheckForPublic("vexd_pfntouch"))
g_pFunctionTable->pfnTouch=pfnTouch;
}
qboolean Voice_SetClientListening(int iReceiver, int iSender, qboolean bListen)
{
if((plinfo[iSender].iSpeakFlags & SPEAK_MUTED) != 0) {
(g_engfuncs.pfnVoice_SetClientListening)(iReceiver, iSender, false);
RETURN_META_VALUE(MRES_SUPERCEDE, false);
}
if((plinfo[iSender].iSpeakFlags & SPEAK_ALL) != 0) {
(g_engfuncs.pfnVoice_SetClientListening)(iReceiver, iSender, true);
RETURN_META_VALUE(MRES_SUPERCEDE, true);
}
if((plinfo[iReceiver].iSpeakFlags & SPEAK_LISTENALL) != 0) {
(g_engfuncs.pfnVoice_SetClientListening)(iReceiver, iSender, true);
RETURN_META_VALUE(MRES_SUPERCEDE, true);
}
RETURN_META_VALUE(MRES_IGNORED, bListen);
}
int AddToFullPack_Post(struct entity_state_s *state, int e, edict_t *ent, edict_t *host, int hostflags, int player, unsigned char *pSet)
{
if( player && ent && ent == host && plinfo[TypeConversion.edict_to_id(ent)].iViewType != CAMERA_NONE )
{
state->rendermode = kRenderTransTexture;
state->renderamt = 100;
}
RETURN_META_VALUE(MRES_IGNORED, 0);
}
void ClientDisconnect(edict_t *pEntity)
{
auto id = TypeConversion.edict_to_id(pEntity);
if (plinfo[id].iViewType != CAMERA_NONE) // Verify that they were originally in a modified view
{
g_CameraCount--;
if (g_CameraCount < 0)
g_CameraCount=0;
if (g_CameraCount==0) // Reset the AddToFullPack pointer if there's no more cameras in use...
g_pFunctionTable->pfnAddToFullPack=NULL;
}
plinfo[id].iSpeakFlags = SPEAK_NORMAL;
plinfo[id].iViewType = CAMERA_NONE;
RETURN_META(MRES_IGNORED);
}
BOOL ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char szRejectReason[128])
{
auto id = TypeConversion.edict_to_id(pEntity);
plinfo[id].iSpeakFlags = SPEAK_NORMAL;
plinfo[id].iViewType = CAMERA_NONE;
plinfo[id].pViewEnt = NULL;
RETURN_META_VALUE(MRES_IGNORED, 0);
}
void ServerDeactivate()
{
memset(glinfo.szLastLights, 0x0, 128);
memset(glinfo.szRealLights, 0x0, 128);
glinfo.bCheckLights = false;
// Reset all forwarding function tables (so that forwards won't be called before plugins are initialized)
g_pFunctionTable->pfnAddToFullPack=NULL;
g_pFunctionTable->pfnKeyValue=NULL;
g_pengfuncsTable->pfnPlaybackEvent=NULL; // "pfn_playbackevent"
g_pFunctionTable->pfnPlayerPreThink=NULL; // "client_PreThink"
g_pFunctionTable_Post->pfnPlayerPostThink=NULL; // "client_PostThink"
g_pFunctionTable->pfnSpawn=NULL; // "pfn_spawn"
g_pFunctionTable->pfnClientKill=NULL; // "client_kill"
g_pFunctionTable->pfnCmdStart=NULL; // "client_impulse","register_impulse"
g_pFunctionTable->pfnThink=NULL; // "pfn_think", "register_think"
g_pFunctionTable->pfnStartFrame=NULL; // "server_frame","ServerFrame"
g_pFunctionTable->pfnTouch=NULL; // "pfn_touch","vexd_pfntouch"
ClearHooks();
RETURN_META(MRES_IGNORED);
}
DETOUR_DECL_STATIC2(LightStyle, void, int, style, const char *, val) // void (*pfnLightStyle) (int style, const char* val);
{
DETOUR_STATIC_CALL(LightStyle)(style, val);
if (!style && strcmp(val, glinfo.szRealLights)) {
ke::SafeStrcpy(glinfo.szRealLights, sizeof(glinfo.szRealLights), val);
}
if (glinfo.bCheckLights && strcmp(val, glinfo.szLastLights))
g_pFunctionTable_Post->pfnStartFrame = StartFrame_Post;
}
void StartFrame_Post()
{
g_pFunctionTable_Post->pfnStartFrame = NULL;
LightStyleDetour->DisableDetour();
LIGHT_STYLE(0, glinfo.szLastLights);
LightStyleDetour->EnableDetour();
RETURN_META(MRES_IGNORED);
}
BOOL CheckForPublic(const char *publicname)
{
AMX* amx;
char blah[64];
strncpy(blah,publicname,63);
int iFunctionIndex;
int i=0;
// Loop through all running scripts
while((amx=MF_GetScriptAmx(i++))!=NULL)
{
// Scan for public
if (MF_AmxFindPublic(amx, blah, &iFunctionIndex) == AMX_ERR_NONE)
{
// Public was found.
return TRUE;
}
}
return FALSE; // no public found in any loaded script
}
void CreateDetours()
{
LightStyleDetour = DETOUR_CREATE_STATIC_FIXED(LightStyle, (void*)(g_engfuncs.pfnLightStyle));
}
void DestroyDetours()
{
LightStyleDetour->Destroy();
}