-
Notifications
You must be signed in to change notification settings - Fork 16
/
stripper_mm.cpp
389 lines (325 loc) · 10.7 KB
/
stripper_mm.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
/** vim: set ts=4 sw=4 et tw=99:
*
* === Stripper for Metamod:Source ===
* Copyright (C) 2005-2009 David "BAILOPAN" Anderson
* No warranties of any kind.
* Based on the original concept of Stripper2 by botman
*
* License: see LICENSE.TXT
* ===================================
*/
#include <stdio.h>
#include <stddef.h>
#ifdef WIN32
#include <windows.h>
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#endif
#include "stripper_mm.h"
#include "intercom.h"
#include "icommandline.h"
#include <stripper_version_auto.h>
using namespace SourceHook;
StripperPlugin g_Plugin;
PLUGIN_EXPOSE(StripperPlugin, g_Plugin);
static IVEngineServer *engine = NULL;
static IServerGameDLL *server = NULL;
static IServerGameClients* clients = NULL;
static SourceHook::String g_mapname;
static stripper_core_t stripper_core;
static char game_path[256];
static char stripper_path[256];
static char stripper_cfg_path[256];
SH_DECL_HOOK0(IVEngineServer, GetMapEntitiesString, SH_NOATTRIB, 0, const char *);
SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const *, char const *, char const *, char const *, bool, bool);
SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, 0, int);
#if !defined ORANGEBOX_BUILD
ICvar* g_pCVar = NULL;
#endif
ICvar*
GetICVar()
{
#if defined METAMOD_PLAPI_VERSION
#if SOURCE_ENGINE==SE_ORANGEBOX || SOURCE_ENGINE==SE_LEFT4DEAD || SOURCE_ENGINE==SE_LEFT4DEAD2 || SOURCE_ENGINE==SE_TF2 || SOURCE_ENGINE==SE_DODS || SOURCE_ENGINE==SE_HL2DM || SOURCE_ENGINE==SE_NUCLEARDAWN || \
SOURCE_ENGINE==SE_ALIENSWARM || SOURCE_ENGINE==SE_BLOODYGOODTIME || SOURCE_ENGINE==SE_CSGO || SOURCE_ENGINE==SE_CSS || SOURCE_ENGINE==SE_INSURGENCY || SOURCE_ENGINE==SE_SDK2013 || SOURCE_ENGINE== SE_BMS
return (ICvar *)((g_SMAPI->GetEngineFactory())(CVAR_INTERFACE_VERSION, NULL));
#else
return (ICvar *)((g_SMAPI->GetEngineFactory())(VENGINE_CVAR_INTERFACE_VERSION, NULL));
#endif
#else
return (ICvar *)((g_SMAPI->engineFactory())(VENGINE_CVAR_INTERFACE_VERSION, NULL));
#endif
}
#if defined WIN32
#define dlopen(x, y) LoadLibrary(x)
#define dlclose(x) FreeLibrary(x)
#define dlsym(x, y) GetProcAddress(x, y)
typedef HMODULE LibraryHandle;
#else
typedef void * LibraryHandle;
#endif
static LibraryHandle stripper_lib;
static void
SetCommandClient(int client);
static void
log_message(const char* fmt, ...)
{
va_list ap;
char buffer[1024];
va_start(ap, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
buffer[sizeof(buffer) - 1] = '\0';
g_SMAPI->LogMsg(g_PLAPI, "%s", buffer);
}
static void
path_format(char* buffer, size_t maxlength, const char* fmt, ...)
{
va_list ap;
char new_buffer[1024];
va_start(ap, fmt);
vsnprintf(new_buffer, sizeof(new_buffer), fmt, ap);
va_end(ap);
new_buffer[sizeof(new_buffer) - 1] = '\0';
g_SMAPI->PathFormat(buffer, maxlength, "%s", new_buffer);
}
static const char*
get_map_name()
{
#if SOURCE_ENGINE==SE_EPISODEONE
return STRING(g_SMAPI->pGlobals()->mapname);
#else
return STRING(g_SMAPI->GetCGlobals()->mapname);
#endif
}
static stripper_game_t stripper_game =
{
NULL,
NULL,
NULL,
log_message,
path_format,
get_map_name,
};
ConVar cvar_stripper_cfg_path("stripper_cfg_path", "addons/stripper", FCVAR_NONE, "Stripper Config Path");
ConVar stripper_curfile("stripper_current_file", "", FCVAR_SPONLY | FCVAR_REPLICATED | FCVAR_NOTIFY, "Stripper for current map");
ConVar stripper_nextfile("stripper_next_file", "", FCVAR_PROTECTED | FCVAR_SPONLY, "Stripper for next map");
#if SOURCE_ENGINE >= SE_ORANGEBOX
void stripper_cfg_path_changed(IConVar *var, const char *pOldValue, float flOldValue)
#else
void stripper_cfg_path_changed(ConVar *var, const char *pOldValue)
#endif
{
strncpy(stripper_cfg_path, cvar_stripper_cfg_path.GetString(), sizeof(stripper_cfg_path));
}
bool
StripperPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
PLUGIN_SAVEVARS();
#if defined METAMOD_PLAPI_VERSION
GET_V_IFACE_ANY(GetServerFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
#if SOURCE_ENGINE == SE_TF2 || SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_SDK2013 || SOURCE_ENGINE == SE_BMS
// Shim to avoid hooking shims
engine = (IVEngineServer *)ismm->GetEngineFactory()("VEngineServer023", nullptr);
if (!engine)
{
engine = (IVEngineServer *)ismm->GetEngineFactory()("VEngineServer022", nullptr);
if (!engine)
{
engine = (IVEngineServer *)ismm->GetEngineFactory()("VEngineServer021", nullptr);
if (!engine)
{
if (error && maxlen)
{
ismm->Format(error, maxlen, "Could not find interface: VEngineServer023 or VEngineServer022 or VEngineServer021");
}
return false;
}
}
}
#else
GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
#endif
GET_V_IFACE_ANY(GetServerFactory, clients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
#else
GET_V_IFACE_ANY(serverFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
GET_V_IFACE_CURRENT(engineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
GET_V_IFACE_ANY(serverFactory, clients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
#endif
engine->GetGameDir(game_path, sizeof(game_path));
stripper_game.game_path = game_path;
stripper_game.stripper_path = "addons/stripper";
stripper_game.stripper_cfg_path = stripper_cfg_path;
strncpy(stripper_cfg_path, cvar_stripper_cfg_path.GetString(), sizeof(stripper_cfg_path));
cvar_stripper_cfg_path.InstallChangeCallback( stripper_cfg_path_changed );
#if SOURCE_ENGINE==SE_DARKMESSIAH
ICvar* cvar = GetICVar();
const char* temp = (cvar == NULL) ? NULL : cvar->GetCommandLineValue("+stripper_path");
#else
const char* temp = CommandLine()->ParmValue("+stripper_path");
#endif
if (temp != NULL && temp[0] != '\0')
{
g_SMAPI->PathFormat(stripper_path, sizeof(stripper_path), "%s", temp);
stripper_game.stripper_path = stripper_path;
}
#if defined __linux__
#define PLATFORM_EXT ".so"
#elif defined __APPLE__
#define PLATFORM_EXT ".dylib"
#else
#define PLATFORM_EXT ".dll"
#endif
char path[256];
g_SMAPI->PathFormat(path,
sizeof(path),
"%s/%s/bin/stripper.core" PLATFORM_EXT,
game_path,
stripper_game.stripper_path);
#undef PLATFORM_EXT
stripper_lib = dlopen(path, RTLD_NOW);
if (stripper_lib == NULL)
{
#if defined __linux__ || defined __APPLE__
snprintf(error, maxlen, "%s", dlerror());
#elif defined WIN32
DWORD dw = GetLastError();
if (FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
error,
maxlen,
NULL) == 0)
{
_snprintf(error, maxlen, "Unknown error: %d", dw);
error[maxlen - 1] = '\0';
}
#endif
return false;
}
STRIPPER_LOAD stripper_load = (STRIPPER_LOAD)dlsym(stripper_lib, "LoadStripper");
if (stripper_load == NULL)
{
dlclose(stripper_lib);
stripper_load = NULL;
snprintf(error, maxlen, "Could not find LoadStripper function");
error[maxlen - 1] = '\0';
return false;
}
stripper_load(&stripper_game, &stripper_core);
SH_ADD_HOOK_STATICFUNC(IVEngineServer, GetMapEntitiesString, engine, GetMapEntitiesString_handler, false);
SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelInit, server, LevelInit_handler, false);
SH_ADD_HOOK_STATICFUNC(IServerGameClients, SetCommandClient, clients, SetCommandClient, false);
#if SOURCE_ENGINE==SE_ORANGEBOX || SOURCE_ENGINE==SE_LEFT4DEAD || SOURCE_ENGINE==SE_LEFT4DEAD2 || SOURCE_ENGINE==SE_TF2 || SOURCE_ENGINE==SE_DODS || SOURCE_ENGINE==SE_HL2DM || SOURCE_ENGINE==SE_NUCLEARDAWN || \
SOURCE_ENGINE==SE_ALIENSWARM || SOURCE_ENGINE==SE_BLOODYGOODTIME || SOURCE_ENGINE==SE_CSGO || SOURCE_ENGINE==SE_CSS || SOURCE_ENGINE==SE_INSURGENCY || SOURCE_ENGINE==SE_SDK2013 || SOURCE_ENGINE== SE_BMS
g_pCVar = GetICVar();
ConVar_Register(0, this);
#else
ConCommandBaseMgr::OneTimeInit(this);
#endif
return true;
}
bool
StripperPlugin::Unload(char *error, size_t maxlen)
{
SH_REMOVE_HOOK_STATICFUNC(IVEngineServer, GetMapEntitiesString, engine, GetMapEntitiesString_handler, false);
SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, LevelInit, server, LevelInit_handler, false);
SH_REMOVE_HOOK_STATICFUNC(IServerGameClients, SetCommandClient, clients, SetCommandClient, false);
stripper_core.unload();
dlclose(stripper_lib);
stripper_lib = NULL;
return true;
}
const char*
GetMapEntitiesString_handler()
{
RETURN_META_VALUE(MRES_SUPERCEDE, stripper_core.ent_string());
}
bool
LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *c, char const *d, bool e, bool f)
{
if (strlen(stripper_nextfile.GetString()) > 0) {
g_mapname.assign(stripper_nextfile.GetString());
log_message("Loading %s for map \"%s\"", g_mapname.c_str(), pMapName);
} else {
g_mapname.assign(pMapName);
}
stripper_nextfile.SetValue("");
stripper_curfile.SetValue(g_mapname.c_str());
const char *ents = stripper_core.parse_map(g_mapname.c_str(), pMapEntities);
RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, true, &IServerGameDLL::LevelInit, (pMapName, ents, c, d, e, f));
}
bool
StripperPlugin::Pause(char *error, size_t maxlen)
{
return true;
}
bool
StripperPlugin::Unpause(char *error, size_t maxlen)
{
return true;
}
void
StripperPlugin::AllPluginsLoaded()
{
}
const char*
StripperPlugin::GetAuthor()
{
return "BAILOPAN";
}
const char*
StripperPlugin::GetName()
{
return "Stripper";
}
const char*
StripperPlugin::GetDescription()
{
return "Strips/Adds Map Entities";
}
const char*
StripperPlugin::GetURL()
{
return "http://www.bailopan.net/";
}
const char*
StripperPlugin::GetLicense()
{
return "GPL v3";
}
const char*
StripperPlugin::GetVersion()
{
return STRIPPER_FULL_VERSION;
}
const char*
StripperPlugin::GetDate()
{
return __DATE__;
}
const char*
StripperPlugin::GetLogTag()
{
return "STRIPPER";
}
bool
StripperPlugin::RegisterConCommandBase(ConCommandBase *pVar)
{
return META_REGCVAR(pVar);
}
static int last_command_client = 1;
static void
SetCommandClient(int client)
{
last_command_client = client;
}
ConVar stripper_version("stripper_version", STRIPPER_FULL_VERSION, FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY, "Stripper Version");
CON_COMMAND(stripper_dump, "Dumps the map entity list to a file")
{
if (last_command_client != -1)
return;
stripper_core.command_dump();
}