-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.cpp
437 lines (328 loc) · 11.4 KB
/
plugin.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <Windows.h>
#include <string>
#include <IClientPlugin.h>
#include <IDetoursAPI.h>
#include <IMemoryUtils.h>
#include <IHooks.h>
#include <interface.h>
#include <hl_sdk/common/protocol.h>
#include <convar.h>
#include <dbg.h>
#include "patterns.h"
//-----------------------------------------------------------------------------
// Soundcache plugin interface
//-----------------------------------------------------------------------------
class CSoundcache : public IClientPlugin
{
public:
virtual api_version_s GetAPIVersion();
virtual bool Load(CreateInterfaceFn pfnSvenModFactory, ISvenModAPI *pSvenModAPI, IPluginHelpers *pPluginHelpers);
virtual void PostLoad(bool bGlobalLoad);
virtual void Unload(void);
virtual bool Pause(void);
virtual void Unpause(void);
virtual void OnFirstClientdataReceived(client_data_t *pcldata, float flTime);
virtual void OnBeginLoading(void);
virtual void OnEndLoading(void);
virtual void OnDisconnect(void);
virtual void GameFrame(client_state_t state, double frametime, bool bPostRunCmd);
virtual void Draw(void);
virtual void DrawHUD(float time, int intermission);
virtual const char *GetName(void);
virtual const char *GetAuthor(void);
virtual const char *GetVersion(void);
virtual const char *GetDescription(void);
virtual const char *GetURL(void);
virtual const char *GetDate(void);
virtual const char *GetLogTag(void);
private:
void *m_pfnCClient_SoundEngine__LoadSoundList;
void *m_pfnCClient_SoundEngine__FlushCache;
DetourHandle_t m_hCClient_SoundEngine__LoadSoundList;
DetourHandle_t m_hCClient_SoundEngine__FlushCache;
DetourHandle_t m_hNetMsgHook_ResourceList;
};
//-----------------------------------------------------------------------------
// Declare Hooks
//-----------------------------------------------------------------------------
DECLARE_CLASS_HOOK(bool, CClient_SoundEngine__LoadSoundList, void *thisptr);
DECLARE_CLASS_HOOK(void, CClient_SoundEngine__FlushCache, void *thisptr, bool host);
//-----------------------------------------------------------------------------
// Vars
//-----------------------------------------------------------------------------
NetMsgHookFn ORIG_NetMsgHook_ResourceList = NULL;
static char mapname_buffer[MAX_PATH];
static char szMapName[MAX_PATH];
static char szSoundcacheDir[MAX_PATH];
static char szServerSoundcacheDir[MAX_PATH];
static char szServerSoundcacheFolder[MAX_PATH];
bool bHasSoundcache = false;
bool bUseSavedSoundcache = false;
bool paused = false;
//-----------------------------------------------------------------------------
// ConCommands, CVars..
//-----------------------------------------------------------------------------
ConVar soundcache_autosave("soundcache_autosave", "1", FCVAR_CLIENTDLL, "Automatically save soundcache");
ConVar soundcache_dont_flush("soundcache_dont_flush", "0", FCVAR_CLIENTDLL, "Don't flush soundcache");
ConVar soundcache_ignore("soundcache_ignore", "0", FCVAR_CLIENTDLL, "Don't download/save/use soundcache");
//-----------------------------------------------------------------------------
// Hooks
//-----------------------------------------------------------------------------
void HOOKED_NetMsgHook_ResourceList(void)
{
bUseSavedSoundcache = false;
if ( ( !soundcache_autosave.GetBool() && !soundcache_ignore.GetBool() ) || paused )
return ORIG_NetMsgHook_ResourceList();
constexpr int localhost = 0x7F000001; // 127.0.0.1
netadr_t addr;
int port;
g_pEngineClient->GetServerAddress( &addr );
port = (addr.port << 8) | (addr.port >> 8); // it's swapped
if ( *(int *)addr.ip == 0 || *(int *)addr.ip == localhost )
return ORIG_NetMsgHook_ResourceList();
char mapname_buffer[MAX_PATH];
char *pszMapName = mapname_buffer;
char *pszExt = NULL;
strncpy(mapname_buffer, g_pEngineFuncs->GetLevelName(), MAX_PATH);
// maps/<mapname>.bsp to <mapname>
while (*pszMapName)
{
if (*pszMapName == '/')
{
pszMapName++;
break;
}
pszMapName++;
}
pszExt = pszMapName;
while (*pszExt)
{
if (*pszExt == '.')
{
*pszExt = 0;
break;
}
pszExt++;
}
strncpy(szMapName, pszMapName, MAX_PATH);
snprintf(szSoundcacheDir, MAX_PATH, "maps\\soundcache\\%s.txt", szMapName);
snprintf(szServerSoundcacheDir, MAX_PATH, "maps\\soundcache\\%hhu.%hhu.%hhu.%hhu %hu\\%s.txt", addr.ip[0], addr.ip[1], addr.ip[2], addr.ip[3], port, szMapName);
snprintf(szServerSoundcacheFolder, MAX_PATH, "maps\\soundcache\\%hhu.%hhu.%hhu.%hhu %hu", addr.ip[0], addr.ip[1], addr.ip[2], addr.ip[3], port);
// Skip download of soundcache
if ( soundcache_ignore.GetBool() )
{
FileHandle_t hFile = g_pFileSystem->Open(szSoundcacheDir, "a+", "GAMEDOWNLOAD"); // dummy
if ( hFile )
{
// Empty infos
g_pFileSystem->FPrintf( hFile, "%s\n", szMapName );
g_pFileSystem->FPrintf( hFile, "%hhu.%hhu.%hhu.%hhu\n", addr.ip[0], addr.ip[1], addr.ip[2], addr.ip[3] );
g_pFileSystem->FPrintf( hFile, "SOUNDLIST {\n" );
g_pFileSystem->FPrintf( hFile, "}\n" );
g_pFileSystem->FPrintf( hFile, "SENTENCELIST {\n" );
g_pFileSystem->FPrintf( hFile, "}\n" );
g_pFileSystem->FPrintf( hFile, "CUSTOMMATERIALS {\n" );
g_pFileSystem->FPrintf( hFile, "}\n" );
g_pFileSystem->Close( hFile );
}
ORIG_NetMsgHook_ResourceList();
return;
}
// Uh we have the soundcache that wasn't deleted
if ( g_pFileSystem->FileExists(szSoundcacheDir) )
{
std::string sDirectory = SvenModAPI()->GetBaseDirectory();
SetFileAttributesA((sDirectory + "\\svencoop_downloads\\" + szSoundcacheDir).c_str(), FILE_ATTRIBUTE_NORMAL); // WinAPI
//DeleteFileA(szFullSoundcacheDir);
g_pFileSystem->RemoveFile(szSoundcacheDir, "GAMEDOWNLOAD");
}
// If client already has the saved soundcache then create a dummy file so we won't download anything..
if ( g_pFileSystem->FileExists(szServerSoundcacheDir) )
{
FileHandle_t hFile = g_pFileSystem->Open(szSoundcacheDir, "a+", "GAMEDOWNLOAD"); // dummy
if ( hFile )
{
g_pFileSystem->Close(hFile);
}
bHasSoundcache = true;
}
else
{
bHasSoundcache = false;
}
bUseSavedSoundcache = true;
ORIG_NetMsgHook_ResourceList();
}
DECLARE_CLASS_FUNC(bool, HOOKED_CClient_SoundEngine__LoadSoundList, void *thisptr)
{
if ( !soundcache_autosave.GetBool() || soundcache_ignore.GetBool() || !bUseSavedSoundcache || paused)
return ORIG_CClient_SoundEngine__LoadSoundList(thisptr);
if ( g_pFileSystem->FileExists(szSoundcacheDir) )
{
std::string sDirectory = SvenModAPI()->GetBaseDirectory();
std::string sSoundcacheDir = sDirectory + "\\svencoop_downloads\\" + szSoundcacheDir;
std::string sServerSoundcacheDir = sDirectory + "\\svencoop_downloads\\" + szServerSoundcacheDir;
SetFileAttributesA(sSoundcacheDir.c_str(), FILE_ATTRIBUTE_NORMAL);
if ( !CreateDirectoryA((sDirectory + "\\svencoop_downloads\\" + szServerSoundcacheFolder).c_str(), NULL) && GetLastError() != ERROR_ALREADY_EXISTS )
{
Warning("Failed to create directory \"..\\%s\"\n", szServerSoundcacheFolder);
return ORIG_CClient_SoundEngine__LoadSoundList(thisptr);
}
if ( bHasSoundcache )
{
g_pFileSystem->RemoveFile(szSoundcacheDir, "GAMEDOWNLOAD");
if ( CopyFileA(sServerSoundcacheDir.c_str(), sSoundcacheDir.c_str(), true) )
{
Msg("Used saved soundcache for the current map \"%s\"\n", szMapName);
}
else
{
Warning("Failed to replace soundcache for the current map \"%s\"\n", szMapName);
}
}
else
{
if ( CopyFileA(sSoundcacheDir.c_str(), sServerSoundcacheDir.c_str(), true) )
{
Msg("Saved soundcache for the current map \"%s\"\n", szMapName);
}
else
{
Warning("Failed to save soundcache for the current map \"%s\"\n", szMapName);
}
}
}
else
{
Warning("Unable to find soundcache for the current map \"%s\"\n", szMapName);
}
return ORIG_CClient_SoundEngine__LoadSoundList(thisptr);
}
DECLARE_CLASS_FUNC(void, HOOKED_CClient_SoundEngine__FlushCache, void *thisptr, bool host)
{
if ( soundcache_dont_flush.GetBool() )
return;
return ORIG_CClient_SoundEngine__FlushCache(thisptr, host);
}
//-----------------------------------------------------------------------------
// Plugin's implementation
//-----------------------------------------------------------------------------
api_version_s CSoundcache::GetAPIVersion()
{
return SVENMOD_API_VER;
}
bool CSoundcache::Load(CreateInterfaceFn pfnSvenModFactory, ISvenModAPI *pSvenModAPI, IPluginHelpers *pPluginHelpers)
{
BindApiToGlobals(pSvenModAPI);
m_pfnCClient_SoundEngine__LoadSoundList = MemoryUtils()->FindPattern( SvenModAPI()->Modules()->Client, Patterns::Client::CClient_SoundEngine__LoadSoundList );
if ( !m_pfnCClient_SoundEngine__LoadSoundList )
{
Warning("Couldn't find function \"CClient_SoundEngine::LoadSoundList\"\n");
Warning("[Soundcache] Failed to initialize\n");
return false;
}
m_pfnCClient_SoundEngine__FlushCache = MemoryUtils()->FindPattern( SvenModAPI()->Modules()->Client, Patterns::Client::CClient_SoundEngine__FlushCache );
if ( !m_pfnCClient_SoundEngine__FlushCache )
{
Warning("Couldn't find function \"CClient_SoundEngine::FlushCache\"\n");
Warning("[Soundcache] Failed to initialize\n");
return false;
}
ConVar_Register();
g_pEngineFuncs->ClientCmd("exec soundcache.cfg\n");
ConColorMsg({ 40, 255, 40, 255 }, "[Soundcache] Successfully loaded\n");
return true;
}
void CSoundcache::PostLoad(bool bGlobalLoad)
{
if (bGlobalLoad)
{
}
else
{
}
m_hNetMsgHook_ResourceList = Hooks()->HookNetworkMessage( SVC_RESOURCELIST,
HOOKED_NetMsgHook_ResourceList,
&ORIG_NetMsgHook_ResourceList );
m_hCClient_SoundEngine__LoadSoundList = DetoursAPI()->DetourFunction( m_pfnCClient_SoundEngine__LoadSoundList,
HOOKED_CClient_SoundEngine__LoadSoundList,
GET_FUNC_PTR(ORIG_CClient_SoundEngine__LoadSoundList) );
m_hCClient_SoundEngine__FlushCache = DetoursAPI()->DetourFunction( m_pfnCClient_SoundEngine__FlushCache,
HOOKED_CClient_SoundEngine__FlushCache,
GET_FUNC_PTR(ORIG_CClient_SoundEngine__FlushCache) );
}
void CSoundcache::Unload(void)
{
DetoursAPI()->RemoveDetour( m_hNetMsgHook_ResourceList );
DetoursAPI()->RemoveDetour( m_hCClient_SoundEngine__LoadSoundList );
DetoursAPI()->RemoveDetour( m_hCClient_SoundEngine__FlushCache );
ConVar_Unregister();
}
bool CSoundcache::Pause(void)
{
paused = true;
return true;
}
void CSoundcache::Unpause(void)
{
paused = false;
}
void CSoundcache::OnFirstClientdataReceived(client_data_t *pcldata, float flTime)
{
}
void CSoundcache::OnBeginLoading(void)
{
}
void CSoundcache::OnEndLoading(void)
{
}
void CSoundcache::OnDisconnect(void)
{
}
void CSoundcache::GameFrame(client_state_t state, double frametime, bool bPostRunCmd)
{
if (bPostRunCmd)
{
}
else
{
}
}
void CSoundcache::Draw(void)
{
}
void CSoundcache::DrawHUD(float time, int intermission)
{
}
const char *CSoundcache::GetName(void)
{
return "Soundcache";
}
const char *CSoundcache::GetAuthor(void)
{
return "Sw1ft";
}
const char *CSoundcache::GetVersion(void)
{
return "1.0.2";
}
const char *CSoundcache::GetDescription(void)
{
return "Automatically saves soundcache";
}
const char *CSoundcache::GetURL(void)
{
return "https://github.com/sw1ft747/";
}
const char *CSoundcache::GetDate(void)
{
return SVENMOD_BUILD_TIMESTAMP;
}
const char *CSoundcache::GetLogTag(void)
{
return "SNDC";
}
//-----------------------------------------------------------------------------
// Export the interface
//-----------------------------------------------------------------------------
EXPOSE_SINGLE_INTERFACE(CSoundcache, IClientPlugin, CLIENT_PLUGIN_INTERFACE_VERSION);