Fix CSGO build.
1 /** vim: set ts=4 sw=4 et tw=99:
3 * === Stripper for Metamod:Source ===
4 * Copyright (C) 2005-2009 David "BAILOPAN" Anderson
5 * No warranties of any kind.
6 * Based on the original concept of Stripper2 by botman
8 * License: see LICENSE.TXT
9 * ===================================
16 #define snprintf _snprintf
17 #define vsnprintf _vsnprintf
19 #include "stripper_mm.h"
21 #include "icommandline.h"
22 #include <stripper_version_auto.h>
24 using namespace SourceHook;
26 StripperPlugin g_Plugin;
28 PLUGIN_EXPOSE(StripperPlugin, g_Plugin);
30 static IVEngineServer *engine = NULL;
31 static IServerGameDLL *server = NULL;
32 static IServerGameClients* clients = NULL;
33 static SourceHook::String g_mapname;
34 static stripper_core_t stripper_core;
35 static char game_path[256];
36 static char stripper_path[256];
37 static char stripper_cfg_path[256];
39 SH_DECL_HOOK0(IVEngineServer, GetMapEntitiesString, SH_NOATTRIB, 0, const char *);
40 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const *, char const *, char const *, char const *, bool, bool);
41 SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, 0, int);
43 #if !defined ORANGEBOX_BUILD
44 ICvar* g_pCVar = NULL;
50 #if defined METAMOD_PLAPI_VERSION
51 #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 || \
52 SOURCE_ENGINE==SE_ALIENSWARM || SOURCE_ENGINE==SE_BLOODYGOODTIME || SOURCE_ENGINE==SE_CSGO || SOURCE_ENGINE==SE_CSS
53 return (ICvar *)((g_SMAPI->GetEngineFactory())(CVAR_INTERFACE_VERSION, NULL));
55 return (ICvar *)((g_SMAPI->GetEngineFactory())(VENGINE_CVAR_INTERFACE_VERSION, NULL));
58 return (ICvar *)((g_SMAPI->engineFactory())(VENGINE_CVAR_INTERFACE_VERSION, NULL));
63 #define dlopen(x, y) LoadLibrary(x)
64 #define dlclose(x) FreeLibrary(x)
65 #define dlsym(x, y) GetProcAddress(x, y)
67 #if SOURCE_ENGINE==SE_CSGO
68 typedef void* HMODULE;
72 static HMODULE stripper_lib;
75 SetCommandClient(int client);
78 log_message(const char* fmt, ...)
84 vsnprintf(buffer, sizeof(buffer), fmt, ap);
87 buffer[sizeof(buffer) - 1] = '\0';
89 g_SMAPI->LogMsg(g_PLAPI, "%s", buffer);
93 path_format(char* buffer, size_t maxlength, const char* fmt, ...)
96 char new_buffer[1024];
99 vsnprintf(new_buffer, sizeof(new_buffer), fmt, ap);
102 new_buffer[sizeof(new_buffer) - 1] = '\0';
104 g_SMAPI->PathFormat(buffer, maxlength, "%s", new_buffer);
110 #if SOURCE_ENGINE==SE_EPISODEONE
111 return STRING(g_SMAPI->pGlobals()->mapname);
113 return STRING(g_SMAPI->GetCGlobals()->mapname);
117 static stripper_game_t stripper_game =
127 ConVar cvar_stripper_cfg_path("stripper_cfg_path", "addons/stripper", FCVAR_NONE, "Stripper Config Path");
129 #if SOURCE_ENGINE >= SE_ORANGEBOX
130 void stripper_cfg_path_changed(IConVar *var, const char *pOldValue, float flOldValue)
132 void stripper_cfg_path_changed(ConVar *var, const char *pOldValue)
135 strncpy(stripper_cfg_path, cvar_stripper_cfg_path.GetString(), sizeof(stripper_cfg_path));
140 StripperPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
144 #if defined METAMOD_PLAPI_VERSION
145 GET_V_IFACE_ANY(GetServerFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
146 GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
147 GET_V_IFACE_ANY(GetServerFactory, clients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
149 GET_V_IFACE_ANY(serverFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
150 GET_V_IFACE_CURRENT(engineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
151 GET_V_IFACE_ANY(serverFactory, clients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
154 engine->GetGameDir(game_path, sizeof(game_path));
155 stripper_game.game_path = game_path;
156 stripper_game.stripper_path = "addons/stripper";
157 stripper_game.stripper_cfg_path = stripper_cfg_path;
158 strncpy(stripper_cfg_path, cvar_stripper_cfg_path.GetString(), sizeof(stripper_cfg_path));
160 cvar_stripper_cfg_path.InstallChangeCallback( stripper_cfg_path_changed );
162 #if SOURCE_ENGINE==SE_DARKMESSIAH
163 ICvar* cvar = GetICVar();
164 const char* temp = (cvar == NULL) ? NULL : cvar->GetCommandLineValue("+stripper_path");
166 const char* temp = CommandLine()->ParmValue("+stripper_path");
168 if (temp != NULL && temp[0] != '\0')
170 g_SMAPI->PathFormat(stripper_path, sizeof(stripper_path), "%s", temp);
171 stripper_game.stripper_path = stripper_path;
174 #if defined __linux__
175 #define PLATFORM_EXT ".so"
176 #elif defined __APPLE__
177 #define PLATFORM_EXT ".dylib"
179 #define PLATFORM_EXT ".dll"
183 g_SMAPI->PathFormat(path,
185 "%s/%s/bin/stripper.core" PLATFORM_EXT,
187 stripper_game.stripper_path);
191 stripper_lib = dlopen(path, RTLD_NOW);
192 if (stripper_lib == NULL)
194 #if defined __linux__ || defined __APPLE__
195 snprintf(error, maxlen, "%s", dlerror());
197 DWORD dw = GetLastError();
199 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
202 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
207 _snprintf(error, maxlen, "Unknown error: %d", dw);
208 error[maxlen - 1] = '\0';
214 STRIPPER_LOAD stripper_load = (STRIPPER_LOAD)dlsym(stripper_lib, "LoadStripper");
215 if (stripper_load == NULL)
217 dlclose(stripper_lib);
218 stripper_load = NULL;
219 snprintf(error, maxlen, "Could not find LoadStripper function");
220 error[maxlen - 1] = '\0';
224 stripper_load(&stripper_game, &stripper_core);
226 SH_ADD_HOOK_STATICFUNC(IVEngineServer, GetMapEntitiesString, engine, GetMapEntitiesString_handler, false);
227 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelInit, server, LevelInit_handler, false);
228 SH_ADD_HOOK_STATICFUNC(IServerGameClients, SetCommandClient, clients, SetCommandClient, false);
230 #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 || \
231 SOURCE_ENGINE==SE_ALIENSWARM || SOURCE_ENGINE==SE_BLOODYGOODTIME || SOURCE_ENGINE==SE_CSGO || SOURCE_ENGINE==SE_CSS
232 g_pCVar = GetICVar();
233 ConVar_Register(0, this);
235 ConCommandBaseMgr::OneTimeInit(this);
242 StripperPlugin::Unload(char *error, size_t maxlen)
244 SH_REMOVE_HOOK_STATICFUNC(IVEngineServer, GetMapEntitiesString, engine, GetMapEntitiesString_handler, false);
245 SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, LevelInit, server, LevelInit_handler, false);
246 SH_REMOVE_HOOK_STATICFUNC(IServerGameClients, SetCommandClient, clients, SetCommandClient, false);
247 stripper_core.unload();
248 dlclose(stripper_lib);
255 GetMapEntitiesString_handler()
257 RETURN_META_VALUE(MRES_SUPERCEDE, stripper_core.ent_string());
261 LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *c, char const *d, bool e, bool f)
263 g_mapname.assign(pMapName);
264 const char *ents = stripper_core.parse_map(g_mapname.c_str(), pMapEntities);
265 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, true, &IServerGameDLL::LevelInit, (pMapName, ents, c, d, e, f));
269 StripperPlugin::Pause(char *error, size_t maxlen)
275 StripperPlugin::Unpause(char *error, size_t maxlen)
281 StripperPlugin::AllPluginsLoaded()
286 StripperPlugin::GetAuthor()
292 StripperPlugin::GetName()
298 StripperPlugin::GetDescription()
300 return "Strips/Adds Map Entities";
304 StripperPlugin::GetURL()
306 return "http://www.bailopan.net/";
310 StripperPlugin::GetLicense()
316 StripperPlugin::GetVersion()
318 return STRIPPER_FULL_VERSION;
322 StripperPlugin::GetDate()
328 StripperPlugin::GetLogTag()
334 StripperPlugin::RegisterConCommandBase(ConCommandBase *pVar)
336 return META_REGCVAR(pVar);
339 static int last_command_client = 1;
342 SetCommandClient(int client)
344 last_command_client = client;
347 ConVar stripper_version("stripper_version", STRIPPER_FULL_VERSION, FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY, "Stripper Version");
349 CON_COMMAND(stripper_dump, "Dumps the map entity list to a file")
351 if (last_command_client != -1)
354 stripper_core.command_dump();