gameshim/stripper_mm.cpp
author Dr!fter <drifter01620@gmail.com>
Fri Sep 12 10:44:35 2014 -0400 (2014-09-12)
changeset 82 75e4accf708e
parent 76 852afac56c3b
permissions -rw-r--r--
Fix CSGO build.
     1 /** vim: set ts=4 sw=4 et tw=99:
     2  * 
     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
     7  *
     8  * License: see LICENSE.TXT
     9  * ===================================
    10  */
    11 
    12 #include <stdio.h>
    13 #include <stddef.h>
    14 #ifdef WIN32
    15 #include <windows.h>
    16 #define snprintf _snprintf
    17 #define vsnprintf _vsnprintf
    18 #endif
    19 #include "stripper_mm.h"
    20 #include "intercom.h"
    21 #include "icommandline.h"
    22 #include <stripper_version_auto.h>
    23 
    24 using namespace SourceHook;
    25 
    26 StripperPlugin g_Plugin;
    27 
    28 PLUGIN_EXPOSE(StripperPlugin, g_Plugin);
    29 
    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];
    38 
    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);
    42 
    43 #if !defined ORANGEBOX_BUILD
    44 ICvar* g_pCVar = NULL;
    45 #endif
    46 
    47 ICvar*
    48 GetICVar()
    49 {
    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));
    54 #else
    55     return (ICvar *)((g_SMAPI->GetEngineFactory())(VENGINE_CVAR_INTERFACE_VERSION, NULL));
    56 #endif
    57 #else
    58     return (ICvar *)((g_SMAPI->engineFactory())(VENGINE_CVAR_INTERFACE_VERSION, NULL));
    59 #endif
    60 }
    61 
    62 #if defined WIN32
    63 #define dlopen(x, y)    LoadLibrary(x)
    64 #define dlclose(x)      FreeLibrary(x)
    65 #define dlsym(x, y)     GetProcAddress(x, y)
    66 #else
    67 #if SOURCE_ENGINE==SE_CSGO
    68 typedef void*           HMODULE;
    69 #endif
    70 #endif
    71 
    72 static HMODULE stripper_lib;
    73 
    74 static void
    75 SetCommandClient(int client);
    76 
    77 static void
    78 log_message(const char* fmt, ...)
    79 {
    80     va_list ap;
    81     char buffer[1024];
    82 
    83     va_start(ap, fmt);
    84     vsnprintf(buffer, sizeof(buffer), fmt, ap);
    85     va_end(ap);
    86 
    87     buffer[sizeof(buffer) - 1] = '\0';
    88 
    89     g_SMAPI->LogMsg(g_PLAPI, "%s", buffer);
    90 }
    91 
    92 static void
    93 path_format(char* buffer, size_t maxlength, const char* fmt, ...)
    94 {
    95     va_list ap;
    96     char new_buffer[1024];
    97 
    98     va_start(ap, fmt);
    99     vsnprintf(new_buffer, sizeof(new_buffer), fmt, ap);
   100     va_end(ap);
   101 
   102     new_buffer[sizeof(new_buffer) - 1] = '\0';
   103 
   104     g_SMAPI->PathFormat(buffer, maxlength, "%s", new_buffer);
   105 }
   106 
   107 static const char*
   108 get_map_name()
   109 {
   110 #if SOURCE_ENGINE==SE_EPISODEONE
   111     return STRING(g_SMAPI->pGlobals()->mapname);
   112 #else
   113     return STRING(g_SMAPI->GetCGlobals()->mapname);
   114 #endif
   115 }
   116 
   117 static stripper_game_t stripper_game =
   118 {
   119     NULL,
   120     NULL,
   121     NULL,
   122     log_message,
   123     path_format,
   124     get_map_name,
   125 };
   126 
   127 ConVar cvar_stripper_cfg_path("stripper_cfg_path", "addons/stripper", FCVAR_NONE, "Stripper Config Path");
   128 
   129 #if SOURCE_ENGINE >= SE_ORANGEBOX
   130 void stripper_cfg_path_changed(IConVar *var, const char *pOldValue, float flOldValue)
   131 #else
   132 void stripper_cfg_path_changed(ConVar *var, const char *pOldValue)
   133 #endif
   134 {
   135     strncpy(stripper_cfg_path, cvar_stripper_cfg_path.GetString(), sizeof(stripper_cfg_path));
   136 }
   137 
   138 
   139 bool
   140 StripperPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   141 {
   142     PLUGIN_SAVEVARS();
   143 
   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);
   148 #else
   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);
   152 #endif
   153 
   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));
   159 
   160     cvar_stripper_cfg_path.InstallChangeCallback( stripper_cfg_path_changed );
   161 
   162 #if SOURCE_ENGINE==SE_DARKMESSIAH
   163 	ICvar* cvar = GetICVar();
   164 	const char* temp = (cvar == NULL) ? NULL : cvar->GetCommandLineValue("+stripper_path");
   165 #else
   166     const char* temp = CommandLine()->ParmValue("+stripper_path");
   167 #endif
   168     if (temp != NULL && temp[0] != '\0')
   169     {
   170         g_SMAPI->PathFormat(stripper_path, sizeof(stripper_path), "%s", temp);
   171         stripper_game.stripper_path = stripper_path;
   172     }
   173 
   174 #if defined __linux__
   175 #define PLATFORM_EXT    ".so"
   176 #elif defined __APPLE__
   177 #define PLATFORM_EXT    ".dylib"
   178 #else
   179 #define PLATFORM_EXT    ".dll"
   180 #endif
   181 
   182     char path[256];
   183     g_SMAPI->PathFormat(path,
   184             sizeof(path),
   185             "%s/%s/bin/stripper.core" PLATFORM_EXT,
   186             game_path,
   187             stripper_game.stripper_path);
   188 
   189 #undef PLATFORM_EXT
   190 
   191     stripper_lib = dlopen(path, RTLD_NOW);
   192     if (stripper_lib == NULL)
   193     {
   194 #if defined __linux__ || defined __APPLE__
   195         snprintf(error, maxlen, "%s", dlerror());
   196 #elif defined WIN32
   197         DWORD dw = GetLastError();
   198         if (FormatMessageA(
   199                 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
   200                 NULL,
   201                 dw,
   202                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   203                 error,
   204                 maxlen,
   205                 NULL) == 0)
   206         {
   207             _snprintf(error, maxlen, "Unknown error: %d", dw);
   208             error[maxlen - 1] = '\0';
   209         }
   210 #endif
   211         return false;
   212     }
   213 
   214     STRIPPER_LOAD stripper_load = (STRIPPER_LOAD)dlsym(stripper_lib, "LoadStripper");
   215     if (stripper_load == NULL)
   216     {
   217         dlclose(stripper_lib);
   218         stripper_load = NULL;
   219         snprintf(error, maxlen, "Could not find LoadStripper function");
   220         error[maxlen - 1] = '\0';
   221         return false;
   222     }
   223 
   224     stripper_load(&stripper_game, &stripper_core);
   225 
   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);
   229 
   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);
   234 #else
   235     ConCommandBaseMgr::OneTimeInit(this);
   236 #endif
   237 
   238     return true;
   239 }
   240 
   241 bool
   242 StripperPlugin::Unload(char *error, size_t maxlen)
   243 {
   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);
   249     stripper_lib = NULL;
   250 
   251     return true;
   252 }
   253 
   254 const char*
   255 GetMapEntitiesString_handler()
   256 {
   257     RETURN_META_VALUE(MRES_SUPERCEDE, stripper_core.ent_string());
   258 }
   259 
   260 bool
   261 LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *c, char const *d, bool e, bool f)
   262 {
   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));
   266 }
   267 
   268 bool
   269 StripperPlugin::Pause(char *error, size_t maxlen)
   270 {
   271     return true;
   272 }
   273 
   274 bool
   275 StripperPlugin::Unpause(char *error, size_t maxlen)
   276 {
   277     return true;
   278 }
   279 
   280 void
   281 StripperPlugin::AllPluginsLoaded()
   282 {
   283 }
   284 
   285 const char*
   286 StripperPlugin::GetAuthor()
   287 {
   288     return "BAILOPAN";
   289 }
   290 
   291 const char*
   292 StripperPlugin::GetName()
   293 {
   294     return "Stripper";
   295 }
   296 
   297 const char*
   298 StripperPlugin::GetDescription()
   299 {
   300     return "Strips/Adds Map Entities";
   301 }
   302 
   303 const char*
   304 StripperPlugin::GetURL()
   305 {
   306     return "http://www.bailopan.net/";
   307 }
   308 
   309 const char*
   310 StripperPlugin::GetLicense()
   311 {
   312     return "GPL v3";
   313 }
   314 
   315 const char*
   316 StripperPlugin::GetVersion()
   317 {
   318     return STRIPPER_FULL_VERSION;
   319 }
   320 
   321 const char*
   322 StripperPlugin::GetDate()
   323 {
   324     return __DATE__;
   325 }
   326 
   327 const char*
   328 StripperPlugin::GetLogTag()
   329 {
   330     return "STRIPPER";
   331 }
   332 
   333 bool
   334 StripperPlugin::RegisterConCommandBase(ConCommandBase *pVar)
   335 {
   336     return META_REGCVAR(pVar);
   337 }
   338 
   339 static int last_command_client = 1;
   340 
   341 static void
   342 SetCommandClient(int client)
   343 {
   344     last_command_client = client;
   345 }
   346 
   347 ConVar stripper_version("stripper_version", STRIPPER_FULL_VERSION, FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY, "Stripper Version");
   348 
   349 CON_COMMAND(stripper_dump, "Dumps the map entity list to a file")
   350 {
   351     if (last_command_client != -1)
   352         return;
   353 
   354     stripper_core.command_dump();
   355 }
   356