1.1 --- a/core/metamod.cpp Sun Nov 23 22:36:06 2008 -0600
1.2 +++ b/core/metamod.cpp Sun Nov 23 23:22:33 2008 -0600
1.3 @@ -45,26 +45,9 @@
1.4 * @file sourcemm.cpp
1.5 */
1.6
1.7 -SH_DECL_MANUALHOOK4(SGD_DLLInit, 0, 0, 0, bool, CreateInterfaceFn, CreateInterfaceFn, CreateInterfaceFn, CGlobalVars *);
1.8 SH_DECL_MANUALHOOK0(SGD_GameInit, 0, 0, 0, bool);
1.9 SH_DECL_MANUALHOOK6(SGD_LevelInit, 0, 0, 0, bool, const char *, const char *, const char *, const char *, bool, bool);
1.10 SH_DECL_MANUALHOOK0_void(SGD_LevelShutdown, 0, 0, 0);
1.11 -SH_DECL_MANUALHOOK0_void(SGD_DLLShutdown, 0, 0, 0);
1.12 -
1.13 -static bool
1.14 -Handler_DLLInit(CreateInterfaceFn engineFactory,
1.15 - CreateInterfaceFn physicsFactory,
1.16 - CreateInterfaceFn filesystemFactory,
1.17 - CGlobalVars *pGlobals);
1.18 -
1.19 -static bool
1.20 -Handler_DLLInit_Post(CreateInterfaceFn engineFactory,
1.21 - CreateInterfaceFn physicsFactory,
1.22 - CreateInterfaceFn filesystemFactory,
1.23 - CGlobalVars *pGlobals);
1.24 -
1.25 -static void
1.26 -Handler_DLLShutdown();
1.27
1.28 static void
1.29 Handler_LevelShutdown();
1.30 @@ -88,14 +71,12 @@
1.31
1.32 struct game_dll_t
1.33 {
1.34 - HINSTANCE lib;
1.35 CreateInterfaceFn factory;
1.36 };
1.37
1.38 static String mod_path;
1.39 static String metamod_path;
1.40 static String full_bin_path;
1.41 -static bool parsed_game_info = false;
1.42 static int vsp_version = 0;
1.43 static int gamedll_version = 0;
1.44 static int engine_build = SOURCE_ENGINE_UNKNOWN;
1.45 @@ -125,9 +106,6 @@
1.46 ISourceHook *g_SHPtr = &g_SourceHook;
1.47 SourceMM::ISmmAPI *g_pMetamod = &g_Metamod;
1.48
1.49 -static void
1.50 -ClearGamedllList();
1.51 -
1.52 /* Helper Macro */
1.53 #define IFACE_MACRO(orig,nam) \
1.54 CPluginManager::CPlugin *pl; \
1.55 @@ -182,14 +160,6 @@
1.56
1.57 SourceHook::MemFuncInfo info;
1.58
1.59 - if (!provider->GetHookInfo(ProvidedHook_DLLInit, &info))
1.60 - {
1.61 - provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::DLLInit");
1.62 - }
1.63 - SH_MANUALHOOK_RECONFIGURE(SGD_DLLInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
1.64 - SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLInit, server, Handler_DLLInit, false);
1.65 - SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLInit, server, Handler_DLLInit_Post, true);
1.66 -
1.67 if (!provider->GetHookInfo(ProvidedHook_GameInit, &info))
1.68 {
1.69 provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::GameInit");
1.70 @@ -210,13 +180,6 @@
1.71 }
1.72 SH_MANUALHOOK_RECONFIGURE(SGD_LevelShutdown, info.vtblindex, info.vtbloffs, info.thisptroffs);
1.73 SH_ADD_MANUALHOOK_STATICFUNC(SGD_LevelShutdown, server, Handler_LevelShutdown, true);
1.74 -
1.75 - if (!provider->GetHookInfo(ProvidedHook_DLLShutdown, &info))
1.76 - {
1.77 - provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::DLLShutdown");
1.78 - }
1.79 - SH_MANUALHOOK_RECONFIGURE(SGD_DLLShutdown, info.vtblindex, info.vtbloffs, info.thisptroffs);
1.80 - SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLShutdown, server, Handler_DLLShutdown, false);
1.81 }
1.82
1.83 bool
1.84 @@ -245,266 +208,12 @@
1.85 return true;
1.86 }
1.87
1.88 -/* This is where the magic happens */
1.89 -SMM_API void *
1.90 -CreateInterface(const char *iface, int *ret)
1.91 +void *
1.92 +ServerFactory(const char *iface, int *ret)
1.93 {
1.94 - /* Prevent loading of self as a SourceMM plugin or Valve server plugin :x */
1.95 - if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
1.96 - {
1.97 - provider->DisplayWarning("Do not try loading Metamod:Source as a plugin.\n");
1.98 -
1.99 - if (ret)
1.100 - {
1.101 - *ret = IFACE_FAILED;
1.102 - }
1.103 -
1.104 - return NULL;
1.105 - }
1.106 -
1.107 - if (!g_bIsVspBridged && strncmp(iface, "ISERVERPLUGINCALLBACKS", 22) == 0)
1.108 - {
1.109 - if (vsp_callbacks != NULL && atoi(&iface[22]) != vsp_version)
1.110 - {
1.111 - if (ret != NULL)
1.112 - *ret = IFACE_FAILED;
1.113 - return NULL;
1.114 - }
1.115 -
1.116 - vsp_version = atoi(&iface[22]);
1.117 - vsp_callbacks = provider->GetVSPCallbacks(vsp_version);
1.118 -
1.119 - if (ret)
1.120 - *ret = (vsp_callbacks != NULL) ? IFACE_OK : IFACE_FAILED;
1.121 -
1.122 - if (vsp_callbacks == NULL)
1.123 - vsp_version = 0;
1.124 -
1.125 - return vsp_callbacks;
1.126 - }
1.127 -
1.128 - if (g_bIsVspBridged)
1.129 - {
1.130 - IFACE_MACRO(gamedll_info.factory, GameDLL);
1.131 - }
1.132 -
1.133 - if (!parsed_game_info)
1.134 - {
1.135 - parsed_game_info = true;
1.136 -
1.137 - if (!mm_DetectGameInformation())
1.138 - {
1.139 - provider->DisplayError("GetFileOfAddress() failed! Metamod cannot load.\n");
1.140 - return NULL;
1.141 - }
1.142 -
1.143 - char temp_path[PATH_SIZE];
1.144 -
1.145 - /* Path to gameinfo.txt */
1.146 - g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s", mod_path.c_str(), "gameinfo.txt");
1.147 -
1.148 - FILE *fp = fopen(temp_path, "rt");
1.149 -
1.150 - if (!fp)
1.151 - {
1.152 - provider->DisplayError("Unable to open gameinfo.txt! Metamod cannot load.\n");
1.153 - return NULL;
1.154 - }
1.155 -
1.156 - char buffer[255];
1.157 - char key[128], val[128];
1.158 - bool search = false;
1.159 - bool gamebin = false;
1.160 - char *ptr;
1.161 - const char *lptr;
1.162 - char cur_path[PATH_SIZE];
1.163 -
1.164 - getcwd(cur_path, PATH_SIZE);
1.165 -
1.166 - while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
1.167 - {
1.168 - UTIL_TrimComments(buffer);
1.169 - UTIL_TrimLeft(buffer);
1.170 - UTIL_TrimRight(buffer);
1.171 -
1.172 - if (stricmp(buffer, "SearchPaths") == 0)
1.173 - {
1.174 - search = true;
1.175 - }
1.176 -
1.177 - if (!search)
1.178 - {
1.179 - continue;
1.180 - }
1.181 -
1.182 - UTIL_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
1.183 - if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
1.184 - {
1.185 - if (stricmp(key, "Game") == 0)
1.186 - {
1.187 - gamebin = false;
1.188 - }
1.189 - else
1.190 - {
1.191 - gamebin = true;
1.192 - }
1.193 -
1.194 - if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
1.195 - {
1.196 - ptr = &(val[sizeof("|gameinfo_path|") - 1]);
1.197 - if (ptr[0] == '.')
1.198 - {
1.199 - ptr++;
1.200 - }
1.201 - lptr = mod_path.c_str();
1.202 - } else {
1.203 - ptr = val;
1.204 - lptr = cur_path;
1.205 - }
1.206 -
1.207 - size_t ptr_len = strlen(ptr);
1.208 - if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
1.209 - {
1.210 - ptr[--ptr_len] = '\0';
1.211 - }
1.212 -
1.213 - /* No need to append "bin" if key is GameBin */
1.214 - if (gamebin)
1.215 - {
1.216 - g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, ptr, SERVER_DLL);
1.217 - }
1.218 - else if (!ptr[0])
1.219 - {
1.220 - g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, "bin", SERVER_DLL);
1.221 - }
1.222 - else
1.223 - {
1.224 - g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
1.225 - }
1.226 -
1.227 - /* If not path to SourceMM... */
1.228 - if (!UTIL_PathCmp(metamod_path.c_str(), temp_path))
1.229 - {
1.230 - FILE *temp_fp = fopen(temp_path, "rb");
1.231 - if (!temp_fp)
1.232 - {
1.233 - continue;
1.234 - }
1.235 - fclose(temp_fp);
1.236 -
1.237 - /* Search for a matching game dll */
1.238 - List<game_dll_t *>::iterator iter;
1.239 - game_dll_t *pCheck;
1.240 - bool found = false;
1.241 - for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
1.242 - {
1.243 - pCheck = (*iter);
1.244 - if (GetFileOfAddress((void *)pCheck->factory, buffer, sizeof(buffer)))
1.245 - {
1.246 - if (UTIL_PathCmp(temp_path, buffer))
1.247 - {
1.248 - found = true;
1.249 - break;
1.250 - }
1.251 - }
1.252 - }
1.253 -
1.254 - if (found)
1.255 - {
1.256 - continue;
1.257 - }
1.258 -
1.259 - HINSTANCE gamedll = dlmount(temp_path);
1.260 - if (gamedll == NULL)
1.261 - {
1.262 - continue;
1.263 - }
1.264 -
1.265 - CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
1.266 - if (fn == NULL)
1.267 - {
1.268 - dlclose(gamedll);
1.269 - continue;
1.270 - }
1.271 -
1.272 - game_dll_t *pInfo = new game_dll_t;
1.273 - pInfo->factory = fn;
1.274 - pInfo->lib = gamedll;
1.275 - gamedll_list.push_back(pInfo);
1.276 - break;
1.277 - }
1.278 - }
1.279 - }
1.280 - fclose(fp);
1.281 - }
1.282 -
1.283 - if (!is_gamedll_loaded)
1.284 - {
1.285 - if (strncmp(iface, "ServerGameDLL", 13) == 0)
1.286 - {
1.287 - List<game_dll_t *>::iterator iter;
1.288 - game_dll_t *pInfo;
1.289 - for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
1.290 - {
1.291 - pInfo = (*iter);
1.292 - if ((server = (IServerGameDLL *)((pInfo->factory)(iface, ret))) != NULL)
1.293 - {
1.294 - if ((gamedll_version = provider->TryServerGameDLL(iface)) != 0)
1.295 - {
1.296 - /* We have a match. Erase us from the list and save our info. */
1.297 - gamedll_list.erase(iter);
1.298 - gamedll_info = *pInfo;
1.299 - delete pInfo;
1.300 - is_gamedll_loaded = true;
1.301 - break;
1.302 - }
1.303 - /* :TODO: error otherwise? */
1.304 - }
1.305 - }
1.306 -
1.307 - if (is_gamedll_loaded)
1.308 - {
1.309 - ClearGamedllList();
1.310 - mm_InitializeForLoad();
1.311 - }
1.312 - else
1.313 - {
1.314 - if (ret)
1.315 - {
1.316 - *ret = IFACE_FAILED;
1.317 - }
1.318 - return NULL;
1.319 - }
1.320 - }
1.321 - else
1.322 - {
1.323 - /* wtf do we do... */
1.324 - /* :TODO: .. something a bit more intelligent? */
1.325 - provider->DisplayError("Engine requested unknown interface before GameDLL was known!\n");
1.326 - return NULL;
1.327 - }
1.328 - }
1.329 -
1.330 - /* If we got here, there's definitely a GameDLL */
1.331 IFACE_MACRO(gamedll_info.factory, GameDLL);
1.332 }
1.333
1.334 -static void
1.335 -ClearGamedllList()
1.336 -{
1.337 - List<game_dll_t *>::iterator iter;
1.338 -
1.339 - game_dll_t *pInfo;
1.340 - for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
1.341 - {
1.342 - pInfo = (*iter);
1.343 - dlclose(pInfo->lib);
1.344 - delete pInfo;
1.345 - }
1.346 -
1.347 - gamedll_list.clear();
1.348 -}
1.349 -
1.350 int
1.351 mm_LoadPluginsFromFile(const char *_file)
1.352 {
1.353 @@ -814,18 +523,6 @@
1.354 }
1.355
1.356 static bool
1.357 -Handler_DLLInit(CreateInterfaceFn engineFactory,
1.358 - CreateInterfaceFn physicsFactory,
1.359 - CreateInterfaceFn filesystemFactory,
1.360 - CGlobalVars *pGlobals)
1.361 -{
1.362 - mm_InitializeGlobals(engineFactory, physicsFactory, filesystemFactory, pGlobals);
1.363 - mm_StartupMetamod(false);
1.364 -
1.365 - RETURN_META_VALUE(MRES_IGNORED, true);
1.366 -}
1.367 -
1.368 -static bool
1.369 Handler_GameInit()
1.370 {
1.371 if (is_game_init)
1.372 @@ -846,16 +543,6 @@
1.373 RETURN_META_VALUE(MRES_IGNORED, true);
1.374 }
1.375
1.376 -static bool
1.377 -Handler_DLLInit_Post(CreateInterfaceFn engineFactory,
1.378 - CreateInterfaceFn physicsFactory,
1.379 - CreateInterfaceFn filesystemFactory,
1.380 - CGlobalVars *pGlobals)
1.381 -{
1.382 - g_PluginMngr.SetAllLoaded();
1.383 - RETURN_META_VALUE(MRES_IGNORED, true);
1.384 -}
1.385 -
1.386 void
1.387 mm_UnloadMetamod()
1.388 {
1.389 @@ -864,25 +551,7 @@
1.390
1.391 provider->Notify_DLLShutdown_Pre();
1.392
1.393 - if (is_gamedll_loaded)
1.394 - {
1.395 - SH_CALL(server, &IServerGameDLL::DLLShutdown)();
1.396 - }
1.397 -
1.398 g_SourceHook.CompleteShutdown();
1.399 -
1.400 - if (is_gamedll_loaded && gamedll_info.lib)
1.401 - {
1.402 - dlclose(gamedll_info.lib);
1.403 - is_gamedll_loaded = false;
1.404 - }
1.405 -}
1.406 -
1.407 -static void
1.408 -Handler_DLLShutdown()
1.409 -{
1.410 - mm_UnloadMetamod();
1.411 - RETURN_META(MRES_SUPERCEDE);
1.412 }
1.413
1.414 static void
1.415 @@ -973,7 +642,7 @@
1.416 CreateInterfaceFn MetamodSource::GetServerFactory(bool syn/* =true */)
1.417 {
1.418 if (syn)
1.419 - return CreateInterface;
1.420 + return ServerFactory;
1.421 return gamedll_info.factory;
1.422 }
1.423
1.424 @@ -1408,10 +1077,11 @@
1.425 return is_gamedll_loaded;
1.426 }
1.427
1.428 -void MetamodSource::SetGameDLLInfo(CreateInterfaceFn serverFactory, int version)
1.429 +void MetamodSource::SetGameDLLInfo(CreateInterfaceFn serverFactory, int version, bool loaded)
1.430 {
1.431 gamedll_info.factory = serverFactory;
1.432 gamedll_version = version;
1.433 + is_gamedll_loaded = loaded;
1.434 }
1.435
1.436 static void