1.1 --- a/core-legacy/sourcemm.cpp Sun Nov 23 21:21:49 2008 -0600
1.2 +++ b/core-legacy/sourcemm.cpp Sun Nov 23 21:22:04 2008 -0600
1.3 @@ -32,66 +32,45 @@
1.4 #undef CommandLine
1.5 DLL_IMPORT ICommandLine *CommandLine();
1.6
1.7 -SH_DECL_HOOK4(IServerGameDLL, DLLInit, SH_NOATTRIB, false, bool, CreateInterfaceFn, CreateInterfaceFn, CreateInterfaceFn, CGlobalVars *);
1.8 SH_DECL_HOOK0_void(IServerGameDLL, DLLShutdown, SH_NOATTRIB, false);
1.9 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
1.10 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
1.11 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
1.12 SH_DECL_HOOK0(IServerGameDLL, GameInit, SH_NOATTRIB, false, bool);
1.13
1.14 -bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
1.15 -bool DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
1.16 void DLLShutdown_handler();
1.17 void LevelShutdown_handler();
1.18 -bool LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background);
1.19 +bool LevelInit_handler(char const *pMapName,
1.20 + char const *pMapEntities,
1.21 + char const *pOldLevel,
1.22 + char const *pLandmarkName,
1.23 + bool loadGame,
1.24 + bool background);
1.25 bool GameInit_handler();
1.26 void LookForVDFs(const char *dir);
1.27 -bool KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID = NULL);
1.28 +bool KVLoadFromFile(KeyValues *kv,
1.29 + IBaseFileSystem *filesystem,
1.30 + const char *resourceName,
1.31 + const char *pathID = NULL);
1.32
1.33 -GameDllInfo g_GameDll = {false, NULL, NULL, NULL, NULL};
1.34 +GameDllInfo g_GameDll = {false, NULL, NULL, NULL};
1.35 EngineInfo g_Engine;
1.36 SourceHook::CSourceHookImpl g_SourceHook;
1.37 SourceHook::ISourceHook *g_SHPtr = &g_SourceHook;
1.38 SourceHook::String g_ModPath;
1.39 -SourceHook::String g_BinPath;
1.40 -SourceHook::String g_SmmPath;
1.41 PluginId g_PLID = Pl_Console; /* Technically, SourceMM is the "Console" plugin... :p */
1.42 -bool bInFirstLevel = true;
1.43 -bool gParsedGameInfo = false;
1.44 -bool bGameInit = false;
1.45 -SourceHook::List<GameDllInfo *> gamedll_list;
1.46 +static bool bInFirstLevel = true;
1.47 +bool g_bGameInit = false;
1.48 SourceHook::CallClass<IServerGameDLL> *g_GameDllPatch;
1.49 SourceHook::CallClass<ICvar> *g_CvarPatch;
1.50 int g_GameDllVersion = 0;
1.51 -const char VSPIFACE_001[] = "ISERVERPLUGINCALLBACKS001";
1.52 -const char VSPIFACE_002[] = "ISERVERPLUGINCALLBACKS002";
1.53 -const char GAMEINFO_PATH[] = "|gameinfo_path|";
1.54 +static const char VSPIFACE_001[] = "ISERVERPLUGINCALLBACKS001";
1.55 +static const char VSPIFACE_002[] = "ISERVERPLUGINCALLBACKS002";
1.56 +static const char GAMEINFO_PATH[] = "|gameinfo_path|";
1.57 IFileSystem *baseFs = NULL;
1.58 bool g_bLevelChanged = false;
1.59 IServerPluginCallbacks *g_pRealVspCallbacks = &g_VspListener;
1.60
1.61 -void ClearGamedllList();
1.62 -
1.63 -/* Helper Macro */
1.64 -#define IFACE_MACRO(orig,nam) \
1.65 - CPluginManager::CPlugin *pl; \
1.66 - SourceHook::List<CPluginEventHandler>::iterator event; \
1.67 - IMetamodListener *api; \
1.68 - int mret = 0; \
1.69 - void *val = NULL; \
1.70 - for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
1.71 - pl = (*iter); \
1.72 - for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
1.73 - api = (*event).event; \
1.74 - mret = IFACE_FAILED; \
1.75 - if ( (val=api->On##nam##Query(iface, &mret)) != NULL ) { \
1.76 - if (ret) *ret = mret; \
1.77 - return val; \
1.78 - } \
1.79 - } \
1.80 - } \
1.81 - return (orig)(iface, ret);
1.82 -
1.83 #define ITER_EVENT(evn, args) \
1.84 CPluginManager::CPlugin *pl; \
1.85 SourceHook::List<CPluginEventHandler>::iterator event; \
1.86 @@ -111,18 +90,17 @@
1.87 /* Initialize everything here */
1.88 void InitMainStates()
1.89 {
1.90 - char full_path[PATH_SIZE] = {0};
1.91 - GetFileOfAddress((void *)g_GameDll.factory, full_path, sizeof(full_path));
1.92 - g_BinPath.assign(full_path);
1.93 -
1.94 /* Like Metamod, reload plugins at the end of the map.
1.95 * This is so plugins can hook everything on load, BUT, new plugins will be reloaded
1.96 * if the server is shut down (silly, but rare case).
1.97 */
1.98 bInFirstLevel = true;
1.99
1.100 - SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit, false);
1.101 - SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit_Post, true);
1.102 + char smm_path[PATH_SIZE];
1.103 + const char *game_dir = CommandLine()->ParmValue("-game", "hl2");
1.104 + abspath(smm_path, game_dir);
1.105 + g_ModPath.assign(smm_path);
1.106 +
1.107 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, g_GameDll.pGameDLL, DLLShutdown_handler, false);
1.108 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, g_GameDll.pGameDLL, LevelShutdown_handler, true);
1.109 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelInit, g_GameDll.pGameDLL, LevelInit_handler, true);
1.110 @@ -225,16 +203,20 @@
1.111 return true;
1.112 }
1.113
1.114 -bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
1.115 +void LoadAsGameDLL(const gamedll_bridge_info *info)
1.116 {
1.117 - g_Engine.engineFactory = engineFactory;
1.118 - g_Engine.fileSystemFactory = filesystemFactory;
1.119 - g_Engine.physicsFactory = physicsFactory;
1.120 - g_Engine.pGlobals = pGlobals;
1.121 + g_Engine.engineFactory = (CreateInterfaceFn)info->engineFactory;
1.122 + g_Engine.fileSystemFactory = (CreateInterfaceFn)info->fsFactory;
1.123 + g_Engine.physicsFactory = (CreateInterfaceFn)info->physicsFactory;
1.124 + g_Engine.pGlobals = (CGlobalVars*)info->pGlobals;
1.125 + g_GameDll.loaded = true;
1.126 + g_GameDll.factory = (CreateInterfaceFn)info->gsFactory;
1.127 + g_GameDll.pGameDLL = (IServerGameDLL*)info->isgd;
1.128 + g_GameDllVersion = info->dllVersion;
1.129
1.130 - StartupMetamod(engineFactory, false);
1.131 + InitMainStates();
1.132
1.133 - RETURN_META_VALUE(MRES_IGNORED, true);
1.134 + StartupMetamod(g_Engine.engineFactory, false);
1.135 }
1.136
1.137 bool AlternatelyLoadMetamod(CreateInterfaceFn ifaceFactory, CreateInterfaceFn serverFactory)
1.138 @@ -254,7 +236,6 @@
1.139
1.140 /* Now find the server */
1.141 g_GameDll.factory = serverFactory;
1.142 - g_GameDll.lib = NULL;
1.143
1.144 char gamedll_iface[] = "ServerGameDLL000";
1.145 for (unsigned int i = 3; i <= 50; i++)
1.146 @@ -285,15 +266,6 @@
1.147 }
1.148 }
1.149
1.150 - char smm_path[PATH_SIZE];
1.151 - const char *game_dir;
1.152 - GetFileOfAddress((void *)AlternatelyLoadMetamod, smm_path, sizeof(smm_path));
1.153 - g_SmmPath.assign(smm_path);
1.154 -
1.155 - game_dir = CommandLine()->ParmValue("-game", "hl2");
1.156 - abspath(smm_path, game_dir);
1.157 - g_ModPath.assign(smm_path);
1.158 -
1.159 InitMainStates();
1.160
1.161 if (!StartupMetamod(ifaceFactory, true))
1.162 @@ -308,7 +280,7 @@
1.163
1.164 bool GameInit_handler()
1.165 {
1.166 - if (bGameInit)
1.167 + if (g_bGameInit)
1.168 RETURN_META_VALUE(MRES_IGNORED, true);
1.169
1.170 if (g_SmmAPI.VSPEnabled() && !g_bIsBridgedAsVsp)
1.171 @@ -317,286 +289,11 @@
1.172 if (g_bIsBridgedAsVsp)
1.173 DoInitialPluginLoads();
1.174
1.175 - bGameInit = true;
1.176 + g_bGameInit = true;
1.177
1.178 RETURN_META_VALUE(MRES_IGNORED, true);
1.179 }
1.180
1.181 -bool DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
1.182 -{
1.183 - g_PluginMngr.SetAllLoaded();
1.184 - RETURN_META_VALUE(MRES_IGNORED, true);
1.185 -}
1.186 -
1.187 -/* This is where the magic happens */
1.188 -SMM_API void *CreateInterface(const char *iface, int *ret)
1.189 -{
1.190 - /* Prevent loading of self as a SourceMM plugin or Valve server plugin :x */
1.191 - if (strcmp(iface, PLAPI_NAME) == 0)
1.192 - {
1.193 - Warning("Do not try loading Metamod:Source as a Metamod:Source plugin");
1.194 -
1.195 - if (ret)
1.196 - {
1.197 - *ret = IFACE_FAILED;
1.198 - }
1.199 -
1.200 - return NULL;
1.201 - }
1.202 -
1.203 - /* We check these separately because we can't reply
1.204 - * unless our interface version really matches.
1.205 - */
1.206 - if (!g_bIsBridgedAsVsp &&
1.207 - (strcmp(iface, VSPIFACE_002) == 0 ||
1.208 - strcmp(iface, VSPIFACE_001) == 0))
1.209 - {
1.210 - if (ret)
1.211 - {
1.212 - *ret = IFACE_OK;
1.213 - }
1.214 - return &g_VspListener;
1.215 - }
1.216 -
1.217 - /* If we're a VSP, bypass this by default */
1.218 - if (g_bIsBridgedAsVsp)
1.219 - {
1.220 - IFACE_MACRO(g_GameDll.factory, GameDLL);
1.221 - }
1.222 -
1.223 - if (!gParsedGameInfo)
1.224 - {
1.225 - gParsedGameInfo = true;
1.226 - const char *gameDir = NULL;
1.227 - char gamePath[PATH_SIZE];
1.228 - char smmPath[PATH_SIZE];
1.229 -
1.230 - /* Get path to SourceMM DLL */
1.231 - if (!GetFileOfAddress((void *)CreateInterface, smmPath, sizeof(smmPath)))
1.232 - {
1.233 - Error("GetFileOfAddress() failed! Metamod cannot load.\n");
1.234 - return NULL;
1.235 - }
1.236 -
1.237 - g_SmmPath.assign(smmPath);
1.238 -
1.239 - /* Get value of -game from command line, defaulting to hl2 as engine seems to do */
1.240 - gameDir = CommandLine()->ParmValue("-game", "hl2");
1.241 -
1.242 - /* Get absolute path */
1.243 - abspath(gamePath, gameDir);
1.244 - g_ModPath.assign(gamePath);
1.245 -
1.246 - char tempPath[PATH_SIZE];
1.247 -
1.248 - /* Path to gameinfo.txt */
1.249 - g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s", g_ModPath.c_str(), "gameinfo.txt");
1.250 -
1.251 - FILE *fp = fopen(tempPath, "rt");
1.252 -
1.253 - if (!fp)
1.254 - {
1.255 - Error("Unable to open gameinfo.txt! Metamod cannot load.\n");
1.256 - return NULL;
1.257 - }
1.258 -
1.259 - char buffer[255];
1.260 - char key[128], val[128];
1.261 - size_t len = 0;
1.262 - bool search = false;
1.263 - bool gamebin = false;
1.264 - char *ptr;
1.265 - const char *lptr;
1.266 - char curPath[PATH_SIZE];
1.267 -
1.268 - getcwd(curPath, PATH_SIZE);
1.269 -
1.270 - while (!feof(fp))
1.271 - {
1.272 - buffer[0] = '\0';
1.273 - fgets(buffer, sizeof(buffer), fp);
1.274 - len = strlen(buffer);
1.275 - if (buffer[len-1] == '\n')
1.276 - buffer[--len] = '\0';
1.277 - UTIL_TrimComments(buffer);
1.278 - UTIL_TrimLeft(buffer);
1.279 - UTIL_TrimRight(buffer);
1.280 - if (stricmp(buffer, "SearchPaths") == 0)
1.281 - search = true;
1.282 - if (!search)
1.283 - continue;
1.284 - UTIL_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
1.285 - if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
1.286 - {
1.287 - if (stricmp(key, "Game") == 0)
1.288 - gamebin = false;
1.289 - else
1.290 - gamebin = true;
1.291 -
1.292 - if (strncmp(val, GAMEINFO_PATH, sizeof(GAMEINFO_PATH) - 1) == 0)
1.293 - {
1.294 - ptr = &(val[sizeof(GAMEINFO_PATH) - 1]);
1.295 - if (ptr[0] == '.')
1.296 - ptr++;
1.297 - lptr = g_ModPath.c_str();
1.298 - } else {
1.299 - ptr = val;
1.300 - lptr = curPath;
1.301 - }
1.302 -
1.303 - size_t ptr_len = strlen(ptr);
1.304 - if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
1.305 - ptr[--ptr_len] = '\0';
1.306 -
1.307 - /* No need to append "bin" if key is GameBin */
1.308 - if (gamebin)
1.309 - {
1.310 - g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s/%s", lptr, ptr, SERVER_DLL);
1.311 - } else if (!ptr[0]) {
1.312 - g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s/%s", lptr, "bin", SERVER_DLL);
1.313 - } else {
1.314 - g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
1.315 - }
1.316 -
1.317 - /* If not path to SourceMM... */
1.318 - if (!UTIL_PathCmp(smmPath, tempPath))
1.319 - {
1.320 - FILE *fp = fopen(tempPath, "rb");
1.321 - if (!fp)
1.322 - continue;
1.323 - //:TODO: Optimize this a bit!
1.324 - SourceHook::List<GameDllInfo *>::iterator iter;
1.325 - GameDllInfo *pCheck;
1.326 - bool found = false;
1.327 - for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
1.328 - {
1.329 - pCheck = (*iter);
1.330 - if (GetFileOfAddress((void *)pCheck->factory, buffer, sizeof(buffer)))
1.331 - {
1.332 - if (UTIL_PathCmp(tempPath, buffer))
1.333 - {
1.334 - found = true;
1.335 - break;
1.336 - }
1.337 - }
1.338 - }
1.339 - if (found)
1.340 - continue;
1.341 - fclose(fp);
1.342 - HINSTANCE gamedll = dlmount(tempPath);
1.343 - if (gamedll == NULL)
1.344 - continue;
1.345 - CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
1.346 - if (fn == NULL)
1.347 - {
1.348 - dlclose(gamedll);
1.349 - continue;
1.350 - }
1.351 - GameDllInfo *pInfo = new GameDllInfo;
1.352 - pInfo->factory = fn;
1.353 - pInfo->lib = gamedll;
1.354 - pInfo->loaded = true;
1.355 - pInfo->pGameDLL = NULL;
1.356 - gamedll_list.push_back(pInfo);
1.357 - break;
1.358 - }
1.359 - }
1.360 - }
1.361 - fclose(fp);
1.362 - }
1.363 -
1.364 - if (!g_GameDll.loaded)
1.365 - {
1.366 - const char *str = "ServerGameDLL";
1.367 - size_t len = strlen(str);
1.368 -
1.369 - if (strncmp(iface, str, len) == 0)
1.370 - {
1.371 - /* This is the interface we want! Right now we support versions 3 through 8 */
1.372 - g_GameDllVersion = atoi(&(iface[len]));
1.373 - int sizeTooBig = 0; //rename this to sizeWrong in the future!
1.374 - if (g_GameDllVersion < MIN_GAMEDLL_VERSION || g_GameDllVersion > MAX_GAMEDLL_VERSION)
1.375 - {
1.376 - /* Maybe this will get used in the future */
1.377 - sizeTooBig = g_GameDllVersion;
1.378 - if (ret)
1.379 - {
1.380 - *ret = IFACE_FAILED;
1.381 - }
1.382 - }
1.383 - SourceHook::List<GameDllInfo *>::iterator iter;
1.384 - GameDllInfo *pInfo = NULL;
1.385 - void *ptr;
1.386 - for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
1.387 - {
1.388 - pInfo = (*iter);
1.389 - ptr = (pInfo->factory)(iface, ret);
1.390 - if (ptr)
1.391 - {
1.392 - /* This is our GameDLL. Unload the others. */
1.393 - gamedll_list.erase(iter);
1.394 - ClearGamedllList();
1.395 - pInfo->pGameDLL = static_cast<IServerGameDLL *>(ptr);
1.396 - g_GameDll = *pInfo;
1.397 - delete pInfo;
1.398 - break;
1.399 - }
1.400 - }
1.401 - if (g_GameDll.loaded)
1.402 - {
1.403 - if (sizeTooBig)
1.404 - {
1.405 - Error("This mod version requires a SourceMM update (ServerGameDLL%03d)!\n", sizeTooBig);
1.406 - if (ret)
1.407 - {
1.408 - *ret = IFACE_FAILED;
1.409 - }
1.410 - return NULL;
1.411 - } else {
1.412 - InitMainStates();
1.413 - }
1.414 - } else {
1.415 - sizeTooBig = 0;
1.416 - if (ret)
1.417 - *ret = IFACE_FAILED;
1.418 - return NULL;
1.419 - }
1.420 - } else {
1.421 - /* wtf do we do... */
1.422 - /* :TODO: .. something a bit more intelligent? */
1.423 - Error("Engine requested unknown interface before GameDLL was known!\n");
1.424 - return NULL;
1.425 - }
1.426 - }
1.427 -
1.428 - /* We use this interface for responding to the meta client command */
1.429 - if (strncmp(iface, "ServerGameClients", 17) == 0)
1.430 - {
1.431 - void *ptr = (g_GameDll.factory)(iface, ret);
1.432 - g_GameDll.pGameClients = static_cast<IServerGameClients *>(ptr);
1.433 -
1.434 - return ptr;
1.435 - }
1.436 -
1.437 - /* If we got here, there's definitely a GameDLL */
1.438 - IFACE_MACRO(g_GameDll.factory, GameDLL);
1.439 -}
1.440 -
1.441 -void ClearGamedllList()
1.442 -{
1.443 - SourceHook::List<GameDllInfo *>::iterator iter;
1.444 -
1.445 - GameDllInfo *pInfo;
1.446 - for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
1.447 - {
1.448 - pInfo = (*iter);
1.449 - dlclose(pInfo->lib);
1.450 - delete pInfo;
1.451 - }
1.452 -
1.453 - gamedll_list.clear();
1.454 -}
1.455 -
1.456 void UnloadMetamod(bool shutting_down)
1.457 {
1.458 /* Unload plugins */
1.459 @@ -617,12 +314,6 @@
1.460 g_CvarPatch = NULL;
1.461
1.462 g_SourceHook.CompleteShutdown();
1.463 -
1.464 - if (g_GameDll.lib && g_GameDll.loaded)
1.465 - {
1.466 - dlclose(g_GameDll.lib);
1.467 - }
1.468 - memset(&g_GameDll, 0, sizeof(GameDllInfo));
1.469 }
1.470
1.471 void DLLShutdown_handler()
1.472 @@ -945,31 +636,6 @@
1.473 return total;
1.474 }
1.475
1.476 -
1.477 -/* Wrapper function. This is called when the GameDLL thinks it's using
1.478 - * the engine's real engineFactory.
1.479 - */
1.480 -void *EngineFactory(const char *iface, int *ret)
1.481 -{
1.482 - IFACE_MACRO(g_Engine.engineFactory, Engine);
1.483 -}
1.484 -
1.485 -/* Wrapper function. This is called when the GameDLL thinks it's using
1.486 - * the engine's real physicsFactory.
1.487 - */
1.488 -void *PhysicsFactory(const char *iface, int *ret)
1.489 -{
1.490 - IFACE_MACRO(g_Engine.physicsFactory, Physics);
1.491 -}
1.492 -
1.493 -/* Wrapper function. This is called when the GameDLL thinks it's using
1.494 - * the engine's real fileSystemFactory.
1.495 - */
1.496 -void *FileSystemFactory(const char *iface, int *ret)
1.497 -{
1.498 - IFACE_MACRO(g_Engine.fileSystemFactory, FileSystem);
1.499 -}
1.500 -
1.501 void LogMessage(const char *msg, ...)
1.502 {
1.503 va_list ap;