1.1 --- a/sourcemm/sourcemm.cpp Wed Oct 05 21:51:02 2005 +0000
1.2 +++ b/sourcemm/sourcemm.cpp Thu Oct 06 00:59:01 2005 +0000
1.3 @@ -12,10 +12,6 @@
1.4
1.5 #include <interface.h>
1.6 #include <eiface.h>
1.7 -#include "CServerGameDLL.h"
1.8 -#include "CServerGameEnts.h"
1.9 -#include "CServerGameClients.h"
1.10 -#include "CHLTVDirector.h"
1.11 #include "sourcemm.h"
1.12 #include "concommands.h"
1.13 #include "CSmmAPI.h"
1.14 @@ -26,262 +22,389 @@
1.15 * @file sourcemm.cpp
1.16 */
1.17
1.18 -CServerGameDLL g_TempGameDLL;
1.19 -CServerGameEnts g_TempGameEnts;
1.20 -CServerGameClients g_TempGameClients;
1.21 -CHLTVDirector g_TempDirector;
1.22 -GameDllInfo g_GameDll = {false, NULL, NULL};
1.23 -EngineInfo g_Engine = {NULL, NULL, NULL, NULL};
1.24 +SH_DECL_HOOK4(IServerGameDLL, DLLInit, SH_NOATTRIB, false, bool, CreateInterfaceFn, CreateInterfaceFn, CreateInterfaceFn, CGlobalVars *);
1.25 +SH_DECL_HOOK0_void(IServerGameDLL, DLLShutdown, SH_NOATTRIB, false);
1.26 +SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
1.27 +SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
1.28 +bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
1.29 +void DLLShutdown_handler();
1.30 +void LevelShutdown_handler();
1.31 +bool LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background);
1.32 +
1.33 +GameDllInfo g_GameDll = {false, NULL, NULL, NULL};
1.34 +EngineInfo g_Engine;
1.35 SourceHook::CSourceHookImpl g_SourceHook;
1.36 -SourceHook::ISourceHook *g_SHPtr;
1.37 +SourceHook::ISourceHook *g_SHPtr = &g_SourceHook;
1.38 SourceHook::String g_ModPath;
1.39 SourceHook::String g_BinPath;
1.40 PluginId g_PLID = Pl_Console; //Technically, SourceMM is the "Console" plugin... :p
1.41 bool bInFirstLevel = true;
1.42 +bool gParsedGameInfo = false;
1.43 +SourceHook::List<GameDllInfo *> gamedll_list;
1.44 +SourceHook::CallClass<IServerGameDLL> *dllExec;
1.45 +
1.46 +void ClearGamedllList();
1.47
1.48 ///////////////////////////////////
1.49 // Main code for HL2 Interaction //
1.50 ///////////////////////////////////
1.51
1.52 +//Initialize everything here
1.53 +void InitMainStates()
1.54 +{
1.55 + char full_path[260] = {0};
1.56 + GetFileOfAddress(g_GameDll.factory, full_path, sizeof(full_path)-1);
1.57 + g_BinPath.assign(full_path);
1.58 +
1.59 + UTIL_PathFmt(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), GetPluginsFile());
1.60 +
1.61 + //Like metamod, reload plugins at the end of the map.
1.62 + //This is so plugins can hook everything on load, BUT, new plugins will be reloaded
1.63 + // if the server is shut down (silly, but rare case).
1.64 + bInFirstLevel = true;
1.65 +
1.66 + SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit, false);
1.67 + SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, g_GameDll.pGameDLL, DLLShutdown_handler, false);
1.68 + SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, g_GameDll.pGameDLL, LevelShutdown_handler, true);
1.69 + SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelInit, g_GameDll.pGameDLL, LevelInit_handler, true);
1.70 +}
1.71 +
1.72 +bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
1.73 +{
1.74 + g_Engine.engineFactory = engineFactory;
1.75 + g_Engine.fileSystemFactory = filesystemFactory;
1.76 + g_Engine.physicsFactory = physicsFactory;
1.77 + g_Engine.pGlobals = pGlobals;
1.78 +
1.79 + g_Engine.engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
1.80 + if (!g_Engine.engine)
1.81 + {
1.82 + Error("Could not find IVEngineServer! Metamod cannot load.");
1.83 + return false;
1.84 + }
1.85 + g_Engine.icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION , NULL));
1.86 + if (!g_Engine.icvar)
1.87 + {
1.88 + Error("Could not find ICvar! Metamod cannot load.");
1.89 + return false;
1.90 + }
1.91 +
1.92 + g_Engine.loaded = true;
1.93 +
1.94 + //Initialize our console hooks
1.95 + ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
1.96 +
1.97 + if (!g_SmmAPI.CacheCmds())
1.98 + {
1.99 + LogMessage("[META] Warning: Failed to initialize Con_Printf. Defaulting to Msg().");
1.100 + LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
1.101 + }
1.102 +
1.103 + char full_path[260];
1.104 + UTIL_PathFmt(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), GetPluginsFile());
1.105 +
1.106 + LoadPluginsFromFile(full_path);
1.107 +
1.108 + g_PluginMngr.SetAllLoaded();
1.109 +
1.110 + bInFirstLevel = true;
1.111 +
1.112 + dllExec = SH_GET_CALLCLASS(g_GameDll.pGameDLL);
1.113 +
1.114 + RETURN_META_VALUE(MRES_IGNORED, true);
1.115 +}
1.116 +
1.117 //This is where the magic happens
1.118 SMM_API void *CreateInterface(const char *name, int *ret)
1.119 {
1.120 - if (!g_GameDll.loaded)
1.121 + if (!gParsedGameInfo)
1.122 {
1.123 - //State of the Mod:
1.124 - // Currently, HL2 Engine has loaded Metamod:Source
1.125 - // It is now asking it to get an interface. We don't have one,
1.126 - // so we're gonna try to give it a fake one to get the information we need.
1.127 - // Then, the the interface will forward the calls to the original interface
1.128 + gParsedGameInfo = true;
1.129 + char curpath[260] = {0};
1.130 + char dllpath[260] = {0};
1.131 + getcwd(curpath, sizeof(curpath)-1);
1.132 + if (!GetFileOfAddress((void *)CreateInterface, dllpath, sizeof(dllpath)-1))
1.133 + {
1.134 + Error("GetFileOfAddress() failed! Metamod cannot load.");
1.135 + return NULL;
1.136 + }
1.137 + SourceHook::String s_dllpath(dllpath);
1.138 + //begin the heuristics for searching for the mod path (these are quite ugly).
1.139 + //for OS compatibility purposes, we're going to do case insensitivity on windows.
1.140 + size_t path_len = strlen(curpath);
1.141 + size_t dll_len = strlen(dllpath);
1.142
1.143 - if (strcmp(name, INTERFACEVERSION_SERVERGAMEDLL) == 0)
1.144 + //strip the dll path off
1.145 + //:TODO: with path stuff - in Linux, \ can exist in a file path as a non-seperator!
1.146 + for (size_t i=dll_len-1; i>0; i--)
1.147 {
1.148 - //We're in. Give the server our fake class as bait.
1.149 - if (ret)
1.150 - *ret = IFACE_OK;
1.151 - return static_cast<void *>(&g_TempGameDLL);
1.152 - } else if (strcmp(name, INTERFACEVERSION_SERVERGAMEENTS) == 0) {
1.153 - if (ret)
1.154 - *ret = IFACE_OK;
1.155 + if (dllpath[i] == '\\' || dllpath[i] == '/')
1.156 + {
1.157 + if (i == dll_len-1)
1.158 + break;
1.159 + //save path by stripping off file name and ending terminator
1.160 + dllpath[i] = '\0';
1.161 + dll_len = i;
1.162 + break;
1.163 + }
1.164 + }
1.165 +
1.166 + //strip absolute path terminators if any!
1.167 + if (curpath[path_len-1] == '/' || curpath[path_len-1] == '\\')
1.168 + curpath[--path_len] = '\0';
1.169
1.170 - return static_cast<void *>(&g_TempGameEnts);
1.171 - } else if (strcmp(name, INTERFACEVERSION_SERVERGAMECLIENTS) == 0) {
1.172 - if (ret)
1.173 - *ret = IFACE_OK;
1.174 + //if the base path doesn't fit into the module path, something is wrong!
1.175 + // ex: c:\games\srcds
1.176 + // c:\gaben.dll
1.177 + if (path_len > dll_len)
1.178 + {
1.179 + Error("Could not detect GameDLL path! Metamod cannot load[1].");
1.180 + return NULL;
1.181 + }
1.182
1.183 - return static_cast<void *>(&g_TempGameClients);
1.184 - } else if (strcmp(name, INTERFACEVERSION_HLTVDIRECTOR) == 0) {
1.185 - if (ret)
1.186 - *ret = IFACE_OK;
1.187 -
1.188 - return static_cast<void *>(&g_TempDirector);
1.189 - } else {
1.190 - if (ret)
1.191 - *ret = IFACE_FAILED;
1.192 -
1.193 + //we are now in such a position that the two dir names SHOULD MATCH!
1.194 + typedef int (*STRNCMP)(const char *str1, const char *str2, size_t n);
1.195 + STRNCMP cmp =
1.196 +#ifdef WIN32
1.197 + strnicmp;
1.198 +#else
1.199 + strncmp;
1.200 +#endif
1.201 + //are they equal?
1.202 + if ( ((cmp)(curpath, dllpath, path_len)) != 0 )
1.203 + {
1.204 + //:TODO: In this case, we should read /proc/self/maps and find srcds!
1.205 + Error("Could not detect GameDLL path! Metamod cannot load[2].");
1.206 return NULL;
1.207 }
1.208 - } else {
1.209 - META_INTERFACE_MACRO(server, g_GameDll.factory);
1.210 - }
1.211 -}
1.212 + //this will skip past the dir and its separator char
1.213 + char *ptr = &(dllpath[path_len+1]);
1.214 + path_len = strlen(ptr);
1.215 + for (size_t i=0; i<path_len; i++)
1.216 + {
1.217 + if (ptr[i] == '/' || ptr[i] == '\\')
1.218 + {
1.219 + if (i == 0)
1.220 + {
1.221 + Error("Could not detect GameDLL path! Metamod cannot load[3].");
1.222 + return NULL;
1.223 + } else {
1.224 + ptr[i] = '\0';
1.225 + path_len = i+1;
1.226 + break;
1.227 + }
1.228 + }
1.229 + }
1.230 + //WE NOW HAVE A GUESS AT THE MOD DIR. OH MY GOD.
1.231 + char temp_path[260];
1.232 + snprintf(temp_path, sizeof(temp_path)-1,
1.233 + "%s%s%s",
1.234 + curpath,
1.235 + PATH_SEP_STR,
1.236 + ptr);
1.237 +
1.238 + g_ModPath.assign(temp_path);
1.239 +
1.240 + snprintf(temp_path, sizeof(temp_path)-1,
1.241 + "%s%s%s",
1.242 + g_ModPath.c_str(),
1.243 + PATH_SEP_STR,
1.244 + "gameinfo.txt");
1.245
1.246 -bool CServerGameDLL::DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn fileSystemFactory, CGlobalVars *pGlobals)
1.247 -{
1.248 - if (m_pOrig)
1.249 - return m_pOrig->DLLInit(engineFactory, physicsFactory, fileSystemFactory, pGlobals);
1.250 + FILE *fp = fopen(temp_path, "rt");
1.251 + if (!fp)
1.252 + {
1.253 + Error("Unable to open gameinfo.txt! Metamod cannot load.");
1.254 + return NULL;
1.255 + }
1.256 + char buffer[255];
1.257 + char key[128], val[128];
1.258 + size_t len = 0;
1.259 + const char *gameinfo = "|gameinfo_path|";
1.260 + size_t gameinfo_len = strlen(gameinfo);
1.261 + bool search = false;
1.262 + bool gamebin = false;
1.263 + const char *lptr;
1.264 + while (!feof(fp))
1.265 + {
1.266 + buffer[0] = '\0';
1.267 + fgets(buffer, sizeof(buffer)-1, fp);
1.268 + len = strlen(buffer);
1.269 + if (buffer[len-1] == '\n')
1.270 + buffer[--len] = '\0';
1.271 + UTIL_TrimComments(buffer);
1.272 + UTIL_TrimLeft(buffer);
1.273 + UTIL_TrimRight(buffer);
1.274 + if (stricmp(buffer, "SearchPaths") == 0)
1.275 + search = true;
1.276 + if (!search)
1.277 + continue;
1.278 + UTIL_KeySplit(buffer, key, sizeof(key)-1, val, sizeof(val)-1);
1.279 + if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
1.280 + {
1.281 + if (stricmp(key, "Game") == 0)
1.282 + gamebin = false;
1.283 + else
1.284 + gamebin = true;
1.285 +
1.286 + if (strncmp(val, gameinfo, gameinfo_len) == 0)
1.287 + {
1.288 + ptr = &(val[gameinfo_len]);
1.289 + if (ptr[0] == '.')
1.290 + ptr++;
1.291 + lptr = g_ModPath.c_str();
1.292 + } else {
1.293 + lptr = curpath;
1.294 + ptr = val;
1.295 + }
1.296 + size_t ptr_len = strlen(ptr);
1.297 + if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
1.298 + ptr[--ptr_len] = '\0';
1.299 + //no need to append "bin"
1.300 + if (gamebin)
1.301 + {
1.302 + UTIL_PathFmt(temp_path, sizeof(temp_path)-1, "%s/%s/%s", lptr, ptr, SERVER_DLL);
1.303 + } else {
1.304 + UTIL_PathFmt(temp_path, sizeof(temp_path)-1, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
1.305 + }
1.306 + if (!UTIL_PathCmp(s_dllpath.c_str(), temp_path))
1.307 + {
1.308 + FILE *fp = fopen(temp_path, "rb");
1.309 + if (!fp)
1.310 + continue;
1.311 + //:TODO: Optimize this a bit!
1.312 + SourceHook::List<GameDllInfo *>::iterator iter;
1.313 + GameDllInfo *pCheck;
1.314 + bool found = false;
1.315 + for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
1.316 + {
1.317 + pCheck = (*iter);
1.318 + if (GetFileOfAddress(pCheck->factory, buffer, sizeof(buffer)-1))
1.319 + {
1.320 + if (UTIL_PathCmp(temp_path, buffer))
1.321 + {
1.322 + found = true;
1.323 + break;
1.324 + }
1.325 + }
1.326 + }
1.327 + if (found)
1.328 + continue;
1.329 + fclose(fp);
1.330 + HINSTANCE gamedll = dlmount(temp_path);
1.331 + if (gamedll == NULL)
1.332 + continue;
1.333 + CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
1.334 + if (fn == NULL)
1.335 + {
1.336 + dlclose(gamedll);
1.337 + continue;
1.338 + }
1.339 + GameDllInfo *pInfo = new GameDllInfo;
1.340 + pInfo->factory = fn;
1.341 + pInfo->lib = gamedll;
1.342 + pInfo->loaded = true;
1.343 + pInfo->pGameDLL = NULL;
1.344 + gamedll_list.push_back(pInfo);
1.345 + }
1.346 + }
1.347 + }
1.348 + fclose(fp);
1.349 + }
1.350
1.351 if (!g_GameDll.loaded)
1.352 {
1.353 - //Initialize SourceHook
1.354 - g_SHPtr = static_cast<SourceHook::ISourceHook *>(&g_SourceHook);
1.355 -
1.356 - //The gamedll isn't loaded yet. We need to find out where it's hiding.
1.357 - IVEngineServer *ive = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
1.358 - if (!ive)
1.359 - {
1.360 - Error("Metamod:Source could not load %s", INTERFACEVERSION_VENGINESERVER);
1.361 - return false;
1.362 - }
1.363 -
1.364 - //Guess the file name
1.365 - //We'll have better heurestics[sp?] later on
1.366 - char mod_path[128], full_path[255];
1.367 - ive->GetGameDir(mod_path, sizeof(mod_path)-1);
1.368 - g_ModPath.assign(mod_path);
1.369 -#if defined WIN32 || defined _WIN32
1.370 - snprintf(full_path, sizeof(full_path)-1, "%s\\bin\\server.dll", mod_path);
1.371 -#else
1.372 - snprintf(full_path, sizeof(full_path)-1, "%s/bin/server_i486.so", mod_path);
1.373 -#endif
1.374 -
1.375 - g_BinPath.assign(full_path);
1.376 -
1.377 - //See if the file even exists
1.378 - FILE *fp = fopen(g_BinPath.c_str(), "r");
1.379 - if (!fp)
1.380 - {
1.381 - Error("Metamod:Source could not read %s", g_BinPath.c_str());
1.382 - return false;
1.383 - }
1.384 - fclose(fp);
1.385 - fp = NULL;
1.386 + const char *str = "ServerGameDLL";
1.387 + size_t len = strlen(str);
1.388
1.389 - //Load the DLL
1.390 - g_GameDll.lib = dlmount(g_BinPath.c_str());
1.391 - if (!g_GameDll.lib)
1.392 + if (strncmp(name, str, len) == 0)
1.393 {
1.394 - Error("Metamod:Source could not load GameDLL: %s", dlerror());
1.395 - return false;
1.396 - } else {
1.397 - //Find its factory
1.398 - g_GameDll.factory = (CreateInterfaceFn)(dlsym(g_GameDll.lib, "CreateInterface"));
1.399 - if (!g_GameDll.factory)
1.400 + //This is the interface we want! Right now we support versions 3 and 4.
1.401 + int version = atoi(&(name[len]));
1.402 + if (version < MIN_GAMEDLL_VERSION || version > MAX_GAMEDLL_VERSION)
1.403 {
1.404 - Error("Metamod:Source could not find an entry point in GameDLL: %s", g_BinPath.c_str());
1.405 - dlclose(g_GameDll.lib);
1.406 - return false;
1.407 - }
1.408 -
1.409 - //Find the new IServerGameDLL pointer
1.410 - IServerGameDLL *serverDll;
1.411 - serverDll = (IServerGameDLL *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEDLL, NULL));
1.412 - if (!serverDll)
1.413 - {
1.414 - Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEDLL, g_BinPath.c_str());
1.415 - dlclose(g_GameDll.lib);
1.416 - return false;
1.417 - }
1.418 -
1.419 - //Set this information early in case our wrappers are called somehow
1.420 - g_Engine.engineFactory = engineFactory;
1.421 - g_Engine.icvar = (ICvar *)(g_Engine.engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL);
1.422 - if (!g_Engine.icvar)
1.423 - {
1.424 - Error("Metamod:Source could not find %s in engine!", VENGINE_CVAR_INTERFACE_VERSION);
1.425 - dlclose(g_GameDll.lib);
1.426 - return false;
1.427 + Error("GameDLL version %d is not supported by Metamod!", version);
1.428 + return NULL;
1.429 }
1.430 - g_Engine.fileSystemFactory = fileSystemFactory;
1.431 - g_Engine.pGlobals = pGlobals;
1.432 - g_Engine.physicsFactory = physicsFactory;
1.433 - g_Engine.engine = ive;
1.434 -
1.435 - //Attempt to load the GameDLL
1.436 - // Note that nothing will be intercepting yet.
1.437 - // This is the one and only call that plugins have no chance of seeing.
1.438 - // Likewise, you won't be able to trick the Server DLL into loading random things.
1.439 - // Luckily, because of SourceHook, this really isn't a problem - you can change
1.440 - // the virtual interfaces in anything it requests.
1.441 - if (!serverDll->DLLInit(EngineFactory, PhysicsFactory, FileSystemFactory, pGlobals))
1.442 + SourceHook::List<GameDllInfo *>::iterator iter;
1.443 + GameDllInfo *pInfo = NULL;
1.444 + void *ptr;
1.445 + for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
1.446 {
1.447 - //For some reason, the GameDLL failed to load.
1.448 - Error("Metamod:Source: GameDLL %s refused to load.", g_BinPath.c_str());
1.449 - dlclose(g_GameDll.lib);
1.450 - return false;
1.451 - }
1.452 -
1.453 - //Retrieve the pointers we'll need from the GameDLL
1.454 - IServerGameEnts *serverEnts;
1.455 - IServerGameClients *serverClients;
1.456 - IHLTVDirector *serverHLTV;
1.457 -
1.458 - serverEnts = (IServerGameEnts *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEENTS, NULL));
1.459 - if (!serverEnts)
1.460 - {
1.461 - Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEENTS, g_BinPath.c_str());
1.462 - dlclose(g_GameDll.lib);
1.463 - return false;
1.464 - }
1.465 -
1.466 - serverClients = (IServerGameClients *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMECLIENTS, NULL));
1.467 - if (!serverClients)
1.468 - {
1.469 - Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMECLIENTS, g_BinPath.c_str());
1.470 - dlclose(g_GameDll.lib);
1.471 - return false;
1.472 + pInfo = (*iter);
1.473 + ptr = (pInfo->factory)(name, ret);
1.474 + if (ptr)
1.475 + {
1.476 + //this is our gamedll. unload the others.
1.477 + gamedll_list.erase(iter);
1.478 + ClearGamedllList();
1.479 + pInfo->pGameDLL = static_cast<IServerGameDLL *>(ptr);
1.480 + g_GameDll = *pInfo;
1.481 + delete pInfo;
1.482 + break;
1.483 + }
1.484 }
1.485 -
1.486 - serverHLTV = (IHLTVDirector *)((g_GameDll.factory)(INTERFACEVERSION_HLTVDIRECTOR, NULL));
1.487 - if (!serverHLTV)
1.488 - {
1.489 - Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_HLTVDIRECTOR, g_BinPath.c_str());
1.490 - dlclose(g_GameDll.lib);
1.491 - return false;
1.492 - }
1.493 -
1.494 - // Now tell the global temp classes that they can call the original functions
1.495 - g_TempDirector.SetOrig(serverHLTV);
1.496 - g_TempGameClients.SetOrig(serverClients);
1.497 - g_TempGameEnts.SetOrig(serverEnts);
1.498 - g_TempGameDLL.SetOrig(serverDll);
1.499 -
1.500 - //Everything's done.
1.501 - g_GameDll.loaded = true;
1.502 -
1.503 - //Initialize our console hooks
1.504 - ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
1.505 -
1.506 - if (!g_SmmAPI.CacheCmds())
1.507 + if (g_GameDll.loaded)
1.508 {
1.509 - LogMessage("[META] Warning: Failed to initialize Con_Printf. Defaulting to Msg().");
1.510 - LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
1.511 + InitMainStates();
1.512 + } else {
1.513 + if (ret)
1.514 + *ret = IFACE_FAILED;
1.515 + return NULL;
1.516 }
1.517 -
1.518 - //Now it's safe to load plugins.
1.519 -#if defined WIN32 || defined _WIN32
1.520 - snprintf(full_path, sizeof(full_path)-1, "%s\\%s", g_ModPath.c_str(), GetPluginsFile());
1.521 -#else
1.522 - snprintf(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), GetPluginsFile());
1.523 -#endif
1.524 -
1.525 - LoadPluginsFromFile(full_path);
1.526 -
1.527 - //All plugins are now loaded.
1.528 - g_PluginMngr.SetAllLoaded();
1.529 -
1.530 - //Like metamod, reload plugins at the end of the map.
1.531 - //This is so plugins can hook everything on load, BUT, new plugins will be reloaded
1.532 - // if the server is shut down (silly, but rare case).
1.533 - bInFirstLevel = true;
1.534 -
1.535 - return true;
1.536 + } else {
1.537 + //wtf do we do...
1.538 + //:TODO: .. something a bit more intelligent?
1.539 + Error("Engine requested unknown interface before GameDLL was known!");
1.540 + return NULL;
1.541 }
1.542 }
1.543
1.544 - //Somehow, the function got here. This should be impossible.
1.545 - Error("Metamod:Source fatal error - IServerGameDLL::DLLInit() called inappropriately");
1.546 + SetUnhandledExceptionFilter(NULL);
1.547
1.548 - return false;
1.549 + //if we got here, there's definitely a gamedll.
1.550 + //META_INTERFACE_MACRO(server, g_GameDll.factory);
1.551 + return (g_GameDll.factory)(name, ret);
1.552 }
1.553
1.554 -void Shutdown()
1.555 +void ClearGamedllList()
1.556 +{
1.557 + SourceHook::List<GameDllInfo *>::iterator iter;
1.558 +
1.559 + GameDllInfo *pInfo;
1.560 + for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
1.561 + {
1.562 + pInfo = (*iter);
1.563 + dlclose(pInfo->lib);
1.564 + delete pInfo;
1.565 + }
1.566 +
1.567 + gamedll_list.clear();
1.568 +}
1.569 +
1.570 +void DLLShutdown_handler()
1.571 {
1.572 //Unload plugins
1.573 g_PluginMngr.UnloadAll();
1.574
1.575 - // Shutdown sourcehook now
1.576 - g_SourceHook.CompleteShutdown();
1.577 -
1.578 // Add the FCVAR_GAMEDLL flag to our cvars so the engine removes them properly
1.579 g_SMConVarAccessor.MarkCommandsAsGameDLL();
1.580 -}
1.581 + g_SMConVarAccessor.UnregisterGameDLLCommands();
1.582
1.583 -void CServerGameDLL::DLLShutdown()
1.584 -{
1.585 - Shutdown();
1.586 + SH_CALL(dllExec, &IServerGameDLL::DLLShutdown)();
1.587
1.588 - //Call the original function
1.589 - m_pOrig->DLLShutdown();
1.590 + SH_RELEASE_CALLCLASS(dllExec);
1.591 + dllExec = NULL;
1.592
1.593 - //Unregister all commands marked as GameDLL now
1.594 - //This prevents crashes when the engine tries to unregister them once we have already unloaded the gamedll
1.595 - //(we used to unload the gamedll in a __attribute__((destructor)) function but I've had problems with that (crashes in dlclose)
1.596 - g_SMConVarAccessor.UnregisterGameDLLCommands();
1.597 + //right now this will crash when the function returns!
1.598 + // :TODO: remove this warning once PM fixes it.
1.599 + g_SourceHook.CompleteShutdown();
1.600
1.601 if (g_GameDll.lib && g_GameDll.loaded)
1.602 dlclose(g_GameDll.lib);
1.603 memset(&g_GameDll, 0, sizeof(GameDllInfo));
1.604 +
1.605 + RETURN_META(MRES_SUPERCEDE);
1.606 }
1.607
1.608 int LoadPluginsFromFile(const char *file)
1.609 @@ -345,11 +468,7 @@
1.610 ext = "";
1.611 }
1.612 //Format the new path
1.613 -#if defined WIN32 || defined _WIN32
1.614 - snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), buffer, ext);
1.615 -#else
1.616 - snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), buffer, ext);
1.617 -#endif
1.618 + UTIL_PathFmt(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), buffer, ext);
1.619 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error)-1);
1.620 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
1.621 {
1.622 @@ -407,13 +526,12 @@
1.623 strcat(buffer, "\n");
1.624 va_end(ap);
1.625
1.626 - g_Engine.engine->LogPrint(buffer);
1.627 -}
1.628 -
1.629 -void CServerGameDLL::LevelShutdown(void)
1.630 -{
1.631 - LevelShutdown_handler();
1.632 - m_pOrig->LevelShutdown();
1.633 + if (!g_Engine.engine)
1.634 + {
1.635 + fprintf(stdout, "%s", buffer);
1.636 + } else {
1.637 + g_Engine.engine->LogPrint(buffer);
1.638 + }
1.639 }
1.640
1.641 void LevelShutdown_handler(void)
1.642 @@ -432,7 +550,7 @@
1.643 }
1.644 }
1.645
1.646 -bool CServerGameDLL::LevelInit( char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background )
1.647 +bool LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
1.648 {
1.649 if (!g_SmmAPI.CacheSuccessful())
1.650 {
1.651 @@ -440,19 +558,7 @@
1.652 LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
1.653 }
1.654
1.655 - return m_pOrig->LevelInit(pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background);
1.656 -}
1.657 -
1.658 -const char *CServerGameDLL::GetGameDescription()
1.659 -{
1.660 - if (m_pOrig)
1.661 - {
1.662 - const char *game = m_pOrig->GetGameDescription();
1.663 - if (game)
1.664 - strcpy(m_GameDescBuffer, game);
1.665 - }
1.666 -
1.667 - return m_GameDescBuffer;
1.668 + RETURN_META_VALUE(MRES_IGNORED, false);
1.669 }
1.670
1.671 #if defined __GNUC__ && (__GNUC__ == 3)