sourcemm/metamod.cpp
changeset 481 5a2e34651450
parent 480 d096210677d4
child 483 58a289949f4e
     1.1 --- a/sourcemm/metamod.cpp	Mon Nov 26 14:03:25 2007 +0000
     1.2 +++ b/sourcemm/metamod.cpp	Fri Dec 14 04:32:47 2007 +0000
     1.3 @@ -25,6 +25,7 @@
     1.4   * Version: $Id$
     1.5   */
     1.6  
     1.7 +#include "metamod_oslink.h"
     1.8  #if defined _DEBUG
     1.9  #define DEBUG2
    1.10  #undef _DEBUG
    1.11 @@ -36,7 +37,6 @@
    1.12  #include "metamod_plugins.h"
    1.13  #include "metamod_util.h"
    1.14  #include "metamod_console.h"
    1.15 -#include "metamod_oslink.h"
    1.16  #if defined DEBUG2
    1.17  #undef DEBUG2
    1.18  #define _DEBUG
    1.19 @@ -99,6 +99,7 @@
    1.20  PluginId g_PLID = Pl_Console;
    1.21  META_RES last_meta_res;
    1.22  IServerPluginCallbacks *vsp_callbacks = NULL;
    1.23 +bool were_plugins_loaded = false;
    1.24  
    1.25  MetamodSource g_Metamod;
    1.26  
    1.27 @@ -137,7 +138,7 @@
    1.28  	}
    1.29  
    1.30  /* Initialize everything here */
    1.31 -void InitMainStates()
    1.32 +void InitializeForLoad()
    1.33  {
    1.34  	char full_path[PATH_SIZE] = {0};
    1.35  	GetFileOfAddress((void *)gamedll_info.factory, full_path, sizeof(full_path));
    1.36 @@ -188,6 +189,31 @@
    1.37  	SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLShutdown, server, Handler_DLLShutdown, false);
    1.38  }
    1.39  
    1.40 +bool DetectGameInformation()
    1.41 +{
    1.42 +	char mm_path[PATH_SIZE];
    1.43 +	char game_path[PATH_SIZE];
    1.44 +
    1.45 +	/* Get path to SourceMM DLL */
    1.46 +	if (!GetFileOfAddress((void *)InitializeForLoad, mm_path, sizeof(mm_path)))
    1.47 +	{
    1.48 +		return false;
    1.49 +	}
    1.50 +
    1.51 +	metamod_path.assign(mm_path);
    1.52 +
    1.53 +	/* Get value of -game from command line, defaulting to hl2 as engine seems to do */
    1.54 +	const char *game_dir = provider->GetCommandLineValue("-game", "hl2");
    1.55 +
    1.56 +	/* Get absolute path */
    1.57 +	abspath(game_path, game_dir);
    1.58 +	mod_path.assign(game_path);
    1.59 +
    1.60 +	engine_build = provider->DetermineSourceEngine(game_dir);;
    1.61 +
    1.62 +	return true;
    1.63 +}
    1.64 +
    1.65  /* This is where the magic happens */
    1.66  SMM_API void *CreateInterface(const char *iface, int *ret)
    1.67  {
    1.68 @@ -221,31 +247,22 @@
    1.69  		return vsp_callbacks;
    1.70  	}
    1.71  
    1.72 +	if (provider->IsAlternatelyLoaded())
    1.73 +	{
    1.74 +		IFACE_MACRO(gamedll_info.factory, GameDLL);
    1.75 +	}
    1.76 +
    1.77  	if (!parsed_game_info)
    1.78  	{
    1.79  		parsed_game_info = true;
    1.80  		const char *game_dir = NULL;
    1.81 -		char game_path[PATH_SIZE];
    1.82 -		char mm_path[PATH_SIZE];
    1.83  
    1.84 -		/* Get path to SourceMM DLL */
    1.85 -		if (!GetFileOfAddress((void *)CreateInterface, mm_path, sizeof(mm_path)))
    1.86 +		if (!DetectGameInformation())
    1.87  		{
    1.88  			provider->DisplayError("GetFileOfAddress() failed! Metamod cannot load.\n");
    1.89  			return NULL;
    1.90  		}
    1.91  
    1.92 -		metamod_path.assign(mm_path);
    1.93 -
    1.94 -		/* Get value of -game from command line, defaulting to hl2 as engine seems to do */
    1.95 -		game_dir = provider->GetCommandLineValue("-game", "hl2");
    1.96 -
    1.97 -		engine_build = provider->DetermineSourceEngine(game_dir);;
    1.98 -
    1.99 -		/* Get absolute path */
   1.100 -		abspath(game_path, game_dir);
   1.101 -		mod_path.assign(game_path);
   1.102 -
   1.103  		char temp_path[PATH_SIZE];
   1.104  
   1.105  		/* Path to gameinfo.txt */
   1.106 @@ -331,7 +348,7 @@
   1.107  				}
   1.108  
   1.109  				/* If not path to SourceMM... */
   1.110 -				if (!UTIL_PathCmp(mm_path, temp_path))
   1.111 +				if (!UTIL_PathCmp(metamod_path.c_str(), temp_path))
   1.112  				{
   1.113  					FILE *temp_fp = fopen(temp_path, "rb");
   1.114  					if (!temp_fp)
   1.115 @@ -413,7 +430,7 @@
   1.116  			if (is_gamedll_loaded)
   1.117  			{
   1.118  				ClearGamedllList();
   1.119 -				InitMainStates();
   1.120 +				InitializeForLoad();
   1.121  			}
   1.122  			else
   1.123  			{
   1.124 @@ -603,6 +620,11 @@
   1.125  
   1.126  void InitializeVSP()
   1.127  {
   1.128 +	if (provider->IsAlternatelyLoaded())
   1.129 +	{
   1.130 +		return;
   1.131 +	}
   1.132 +
   1.133  	size_t len;
   1.134  	char engine_file[PATH_SIZE];
   1.135  	char engine_path[PATH_SIZE];
   1.136 @@ -675,29 +697,8 @@
   1.137  	}
   1.138  }
   1.139  
   1.140 -bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
   1.141 +void DoInitialPluginLoads()
   1.142  {
   1.143 -	engine_factory = engineFactory;
   1.144 -	filesystem_factory = filesystemFactory;
   1.145 -	physics_factory = physicsFactory;
   1.146 -	gpGlobals = pGlobals;
   1.147 -
   1.148 -	provider->Notify_DLLInit_Pre(server, engineFactory, gamedll_info.factory);
   1.149 -
   1.150 -	metamod_version = provider->CreateConVar("metamod_version", 
   1.151 -		SOURCEMM_VERSION, 
   1.152 -		"Metamod:Source Version",
   1.153 -		ConVarFlag_Notify|ConVarFlag_Replicated|ConVarFlag_SpOnly);
   1.154 -	
   1.155 -	mm_pluginsfile = provider->CreateConVar("mm_pluginsfile", 
   1.156 -#if defined WIN32 || defined _WIN32
   1.157 -		"addons\\metamod\\metaplugins.ini", 
   1.158 -#else
   1.159 -		"addons/metamod/metaplugins.ini",
   1.160 -#endif
   1.161 -		"Metamod:Source Plugins File",
   1.162 -		ConVarFlag_SpOnly);
   1.163 -
   1.164  	const char *pluginFile = provider->GetCommandLineValue("mm_pluginsfile", NULL);
   1.165  	if (!pluginFile) 
   1.166  	{
   1.167 @@ -712,8 +713,47 @@
   1.168  	g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), pluginFile);
   1.169  
   1.170  	LoadPluginsFromFile(full_path);
   1.171 +}
   1.172  
   1.173 -	in_first_level = true;
   1.174 +void StartupMetamod(bool bWaitForGameInit)
   1.175 +{
   1.176 +	metamod_version = provider->CreateConVar("metamod_version", 
   1.177 +		SOURCEMM_VERSION, 
   1.178 +		"Metamod:Source Version",
   1.179 +		ConVarFlag_Notify|ConVarFlag_Replicated|ConVarFlag_SpOnly);
   1.180 +
   1.181 +	mm_pluginsfile = provider->CreateConVar("mm_pluginsfile", 
   1.182 +#if defined WIN32 || defined _WIN32
   1.183 +		"addons\\metamod\\metaplugins.ini", 
   1.184 +#else
   1.185 +		"addons/metamod/metaplugins.ini",
   1.186 +#endif
   1.187 +		"Metamod:Source Plugins File",
   1.188 +		ConVarFlag_SpOnly);
   1.189 +
   1.190 +	if (!bWaitForGameInit)
   1.191 +	{
   1.192 +		DoInitialPluginLoads();
   1.193 +		in_first_level = true;
   1.194 +	}
   1.195 +}
   1.196 +
   1.197 +void InitializeGlobals(CreateInterfaceFn engineFactory, 
   1.198 +					   CreateInterfaceFn physicsFactory,
   1.199 +					   CreateInterfaceFn filesystemFactory,
   1.200 +					   CGlobalVars *pGlobals)
   1.201 +{
   1.202 +	engine_factory = engineFactory;
   1.203 +	physics_factory = physicsFactory;
   1.204 +	filesystem_factory = filesystemFactory;
   1.205 +	gpGlobals = pGlobals;
   1.206 +	provider->Notify_DLLInit_Pre(engineFactory, gamedll_info.factory);
   1.207 +}
   1.208 +
   1.209 +bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
   1.210 +{
   1.211 +	InitializeGlobals(engineFactory, physicsFactory, filesystemFactory, pGlobals);
   1.212 +	StartupMetamod(false);
   1.213  
   1.214  	RETURN_META_VALUE(MRES_IGNORED, true);
   1.215  }
   1.216 @@ -730,6 +770,13 @@
   1.217  		InitializeVSP();
   1.218  	}
   1.219  
   1.220 +	if (provider->IsAlternatelyLoaded() && !were_plugins_loaded)
   1.221 +	{
   1.222 +		DoInitialPluginLoads();
   1.223 +		g_PluginMngr.SetAllLoaded();
   1.224 +		were_plugins_loaded = true;
   1.225 +	}
   1.226 +
   1.227  	is_game_init = true;
   1.228  
   1.229  	RETURN_META_VALUE(MRES_IGNORED, true);
   1.230 @@ -741,14 +788,17 @@
   1.231  	RETURN_META_VALUE(MRES_IGNORED, true);
   1.232  }
   1.233  
   1.234 -void Handler_DLLShutdown()
   1.235 +void UnloadMetamod()
   1.236  {
   1.237  	/* Unload plugins */
   1.238  	g_PluginMngr.UnloadAll();
   1.239  
   1.240  	provider->Notify_DLLShutdown_Pre();
   1.241  
   1.242 -	SH_CALL(server, &IServerGameDLL::DLLShutdown)();
   1.243 +	if (is_gamedll_loaded)
   1.244 +	{
   1.245 +		SH_CALL(server, &IServerGameDLL::DLLShutdown)();
   1.246 +	}
   1.247  
   1.248  	g_SourceHook.CompleteShutdown();
   1.249  
   1.250 @@ -757,12 +807,24 @@
   1.251  		dlclose(gamedll_info.lib);
   1.252  		is_gamedll_loaded = false;
   1.253  	}
   1.254 +}
   1.255  
   1.256 +void Handler_DLLShutdown()
   1.257 +{
   1.258 +	UnloadMetamod();
   1.259  	RETURN_META(MRES_SUPERCEDE);
   1.260  }
   1.261  
   1.262  void Handler_LevelShutdown(void)
   1.263  {
   1.264 +	if (provider->IsAlternatelyLoaded() && !were_plugins_loaded)
   1.265 +	{
   1.266 +		g_PluginMngr.SetAllLoaded();
   1.267 +		DoInitialPluginLoads();
   1.268 +		were_plugins_loaded = true;
   1.269 +		in_first_level = true;
   1.270 +	}
   1.271 +
   1.272  	if (!in_first_level)
   1.273  	{
   1.274  		char full_path[255];
   1.275 @@ -1270,3 +1332,19 @@
   1.276  {
   1.277  	return UTIL_FormatArgs(buffer, maxlength, format, ap);
   1.278  }
   1.279 +
   1.280 +bool MetamodSource::IsLoadedAsGameDLL()
   1.281 +{
   1.282 +	return is_gamedll_loaded;
   1.283 +}
   1.284 +
   1.285 +void MetamodSource::SetGameDLLInfo(CreateInterfaceFn serverFactory, int version)
   1.286 +{
   1.287 +	gamedll_info.factory = serverFactory;
   1.288 +	gamedll_version = version;
   1.289 +}
   1.290 +
   1.291 +bool MetamodSource::IsAlternateLoadComplete()
   1.292 +{
   1.293 +	return were_plugins_loaded;
   1.294 +}