core/metamod.cpp
changeset 552 4b73ffd6e176
parent 539 db914129ae01
child 553 8fc4ad22c73d
     1.1 --- a/core/metamod.cpp	Sat Nov 15 21:54:46 2008 -0600
     1.2 +++ b/core/metamod.cpp	Sat Nov 15 23:46:27 2008 -0600
     1.3 @@ -50,14 +50,40 @@
     1.4  SH_DECL_MANUALHOOK0_void(SGD_LevelShutdown, 0, 0, 0);
     1.5  SH_DECL_MANUALHOOK0_void(SGD_DLLShutdown, 0, 0, 0);
     1.6  
     1.7 -bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
     1.8 -bool Handler_DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
     1.9 -void Handler_DLLShutdown();
    1.10 -void Handler_LevelShutdown();
    1.11 -bool Handler_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background);
    1.12 -bool Handler_GameInit();
    1.13 -void InitializeVSP();
    1.14 -void LookForVDFs(const char *dir);
    1.15 +static bool
    1.16 +Handler_DLLInit(CreateInterfaceFn engineFactory,
    1.17 +				CreateInterfaceFn physicsFactory,
    1.18 +				CreateInterfaceFn filesystemFactory,
    1.19 +				CGlobalVars *pGlobals);
    1.20 +
    1.21 +static bool
    1.22 +Handler_DLLInit_Post(CreateInterfaceFn engineFactory,
    1.23 +					 CreateInterfaceFn physicsFactory,
    1.24 +					 CreateInterfaceFn filesystemFactory,
    1.25 +					 CGlobalVars *pGlobals);
    1.26 +
    1.27 +static void
    1.28 +Handler_DLLShutdown();
    1.29 +
    1.30 +static void
    1.31 +Handler_LevelShutdown();
    1.32 +
    1.33 +static bool
    1.34 +Handler_LevelInit(char const *pMapName,
    1.35 +				  char const *pMapEntities,
    1.36 +				  char const *pOldLevel,
    1.37 +				  char const *pLandmarkName,
    1.38 +				  bool loadGame,
    1.39 +				  bool background);
    1.40 +
    1.41 +static bool
    1.42 +Handler_GameInit();
    1.43 +
    1.44 +static void
    1.45 +InitializeVSP();
    1.46 +
    1.47 +static void
    1.48 +LookForVDFs(const char *dir);
    1.49  
    1.50  struct game_dll_t
    1.51  {
    1.52 @@ -65,41 +91,42 @@
    1.53  	CreateInterfaceFn factory;
    1.54  };
    1.55  
    1.56 -String mod_path;
    1.57 -String metamod_path;
    1.58 -String full_bin_path;
    1.59 -bool parsed_game_info = false;
    1.60 -int vsp_version = 0;
    1.61 -int gamedll_version = 0;
    1.62 -int engine_build = SOURCE_ENGINE_UNKNOWN;
    1.63 -List<game_dll_t *> gamedll_list;
    1.64 -bool is_gamedll_loaded = false;
    1.65 -bool in_first_level = true;
    1.66 -bool is_game_init = false;
    1.67 -bool vsp_load_attempted = false;
    1.68 -bool vsp_load_requested = false;
    1.69 -bool vsp_loaded = false;
    1.70 -game_dll_t gamedll_info;
    1.71 -ConVar *metamod_version = NULL;
    1.72 -ConVar *mm_pluginsfile = NULL;
    1.73 -ConVar *mm_basedir = NULL;
    1.74 -IServerGameDLL *server = NULL;
    1.75 -CreateInterfaceFn engine_factory = NULL;
    1.76 -CreateInterfaceFn physics_factory = NULL;
    1.77 -CreateInterfaceFn filesystem_factory = NULL;
    1.78 -CGlobalVars *gpGlobals = NULL;
    1.79 -CSourceHookImpl g_SourceHook;
    1.80 -CHookManagerAutoGen g_SH_HookManagerAutoGen(&g_SourceHook);
    1.81 -ISourceHook *g_SHPtr = &g_SourceHook;
    1.82 -PluginId g_PLID = Pl_Console;
    1.83 -META_RES last_meta_res;
    1.84 -IServerPluginCallbacks *vsp_callbacks = NULL;
    1.85 -bool were_plugins_loaded = false;
    1.86 +static String mod_path;
    1.87 +static String metamod_path;
    1.88 +static String full_bin_path;
    1.89 +static bool parsed_game_info = false;
    1.90 +static int vsp_version = 0;
    1.91 +static int gamedll_version = 0;
    1.92 +static int engine_build = SOURCE_ENGINE_UNKNOWN;
    1.93 +static List<game_dll_t *> gamedll_list;
    1.94 +static bool is_gamedll_loaded = false;
    1.95 +static bool in_first_level = true;
    1.96 +static bool is_game_init = false;
    1.97 +static bool vsp_load_requested = false;
    1.98 +static bool vsp_loaded = false;
    1.99 +static game_dll_t gamedll_info;
   1.100 +static ConVar *metamod_version = NULL;
   1.101 +static ConVar *mm_pluginsfile = NULL;
   1.102 +static ConVar *mm_basedir = NULL;
   1.103 +static IServerGameDLL *server = NULL;
   1.104 +static CreateInterfaceFn engine_factory = NULL;
   1.105 +static CreateInterfaceFn physics_factory = NULL;
   1.106 +static CreateInterfaceFn filesystem_factory = NULL;
   1.107 +static CGlobalVars *gpGlobals = NULL;
   1.108 +static CHookManagerAutoGen g_SH_HookManagerAutoGen(&g_SourceHook);
   1.109 +static META_RES last_meta_res;
   1.110 +static IServerPluginCallbacks *vsp_callbacks = NULL;
   1.111 +static bool were_plugins_loaded = false;
   1.112 +static bool g_bIsVspBridged = false;
   1.113  
   1.114  MetamodSource g_Metamod;
   1.115 +PluginId g_PLID = Pl_Console;
   1.116 +CSourceHookImpl g_SourceHook;
   1.117 +ISourceHook *g_SHPtr = &g_SourceHook;
   1.118  SourceMM::ISmmAPI *g_pMetamod = &g_Metamod;
   1.119  
   1.120 -void ClearGamedllList();
   1.121 +static void
   1.122 +ClearGamedllList();
   1.123  
   1.124  /* Helper Macro */
   1.125  #define	IFACE_MACRO(orig,nam) \
   1.126 @@ -133,8 +160,15 @@
   1.127  		} \
   1.128  	}
   1.129  
   1.130 +void
   1.131 +mm_SetVspInfo(IServerPluginCallbacks *ispc, unsigned int version)
   1.132 +{
   1.133 +	vsp_version = version;
   1.134 +}
   1.135 +
   1.136  /* Initialize everything here */
   1.137 -void InitializeForLoad()
   1.138 +void
   1.139 +mm_InitializeForLoad()
   1.140  {
   1.141  	char full_path[PATH_SIZE] = {0};
   1.142  	GetFileOfAddress((void *)gamedll_info.factory, full_path, sizeof(full_path));
   1.143 @@ -185,13 +219,14 @@
   1.144  	SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLShutdown, server, Handler_DLLShutdown, false);
   1.145  }
   1.146  
   1.147 -bool DetectGameInformation()
   1.148 +bool
   1.149 +mm_DetectGameInformation()
   1.150  {
   1.151  	char mm_path[PATH_SIZE];
   1.152  	char game_path[PATH_SIZE];
   1.153  
   1.154  	/* Get path to SourceMM DLL */
   1.155 -	if (!GetFileOfAddress((void *)InitializeForLoad, mm_path, sizeof(mm_path)))
   1.156 +	if (!GetFileOfAddress((void *)mm_InitializeForLoad, mm_path, sizeof(mm_path)))
   1.157  	{
   1.158  		return false;
   1.159  	}
   1.160 @@ -211,7 +246,8 @@
   1.161  }
   1.162  
   1.163  /* This is where the magic happens */
   1.164 -SMM_API void *CreateInterface(const char *iface, int *ret)
   1.165 +SMM_API void *
   1.166 +CreateInterface(const char *iface, int *ret)
   1.167  {
   1.168  	/* Prevent loading of self as a SourceMM plugin or Valve server plugin :x */
   1.169  	if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
   1.170 @@ -226,14 +262,12 @@
   1.171  		return NULL;
   1.172  	}
   1.173  
   1.174 -	if (strncmp(iface, "ISERVERPLUGINCALLBACKS", 22) == 0)
   1.175 +	if (!g_bIsVspBridged && strncmp(iface, "ISERVERPLUGINCALLBACKS", 22) == 0)
   1.176  	{
   1.177  		if (vsp_callbacks != NULL && atoi(&iface[22]) != vsp_version)
   1.178  		{
   1.179  			if (ret != NULL)
   1.180 -			{
   1.181  				*ret = IFACE_FAILED;
   1.182 -			}
   1.183  			return NULL;
   1.184  		}
   1.185  
   1.186 @@ -241,19 +275,15 @@
   1.187  		vsp_callbacks = provider->GetVSPCallbacks(vsp_version);
   1.188  
   1.189  		if (ret)
   1.190 -		{
   1.191  			*ret = (vsp_callbacks != NULL) ? IFACE_OK : IFACE_FAILED;
   1.192 -		}
   1.193  
   1.194  		if (vsp_callbacks == NULL)
   1.195 -		{
   1.196  			vsp_version = 0;
   1.197 -		}
   1.198  
   1.199  		return vsp_callbacks;
   1.200  	}
   1.201  
   1.202 -	if (provider->IsAlternatelyLoaded())
   1.203 +	if (g_bIsVspBridged)
   1.204  	{
   1.205  		IFACE_MACRO(gamedll_info.factory, GameDLL);
   1.206  	}
   1.207 @@ -262,7 +292,7 @@
   1.208  	{
   1.209  		parsed_game_info = true;
   1.210  
   1.211 -		if (!DetectGameInformation())
   1.212 +		if (!mm_DetectGameInformation())
   1.213  		{
   1.214  			provider->DisplayError("GetFileOfAddress() failed! Metamod cannot load.\n");
   1.215  			return NULL;
   1.216 @@ -435,7 +465,7 @@
   1.217  			if (is_gamedll_loaded)
   1.218  			{
   1.219  				ClearGamedllList();
   1.220 -				InitializeForLoad();
   1.221 +				mm_InitializeForLoad();
   1.222  			}
   1.223  			else
   1.224  			{
   1.225 @@ -459,7 +489,8 @@
   1.226  	IFACE_MACRO(gamedll_info.factory, GameDLL);
   1.227  }
   1.228  
   1.229 -void ClearGamedllList()
   1.230 +static void
   1.231 +ClearGamedllList()
   1.232  {
   1.233  	List<game_dll_t *>::iterator iter;
   1.234  
   1.235 @@ -474,7 +505,8 @@
   1.236  	gamedll_list.clear();
   1.237  }
   1.238  
   1.239 -int LoadPluginsFromFile(const char *_file)
   1.240 +int
   1.241 +mm_LoadPluginsFromFile(const char *_file)
   1.242  {
   1.243  	FILE *fp;
   1.244  	int total = 0, skipped=0;
   1.245 @@ -625,10 +657,8 @@
   1.246  
   1.247  void InitializeVSP()
   1.248  {
   1.249 -	if (provider->IsAlternatelyLoaded())
   1.250 -	{
   1.251 +	if (g_bIsVspBridged)
   1.252  		return;
   1.253 -	}
   1.254  
   1.255  	size_t len;
   1.256  	char engine_file[PATH_SIZE];
   1.257 @@ -663,7 +693,8 @@
   1.258  /* Wrapper function.  This is called when the GameDLL thinks it's using
   1.259   * the engine's real engineFactory.
   1.260   */
   1.261 -void *EngineFactory(const char *iface, int *ret)
   1.262 +static void *
   1.263 +EngineFactory(const char *iface, int *ret)
   1.264  {
   1.265  	IFACE_MACRO(engine_factory, Engine);
   1.266  }
   1.267 @@ -671,7 +702,8 @@
   1.268  /* Wrapper function.  This is called when the GameDLL thinks it's using
   1.269   * the engine's real physicsFactory.
   1.270   */
   1.271 -void *PhysicsFactory(const char *iface, int *ret)
   1.272 +static void *
   1.273 +PhysicsFactory(const char *iface, int *ret)
   1.274  {
   1.275  	IFACE_MACRO(physics_factory, Physics);
   1.276  }
   1.277 @@ -679,12 +711,14 @@
   1.278  /* Wrapper function.  This is called when the GameDLL thinks it's using
   1.279   * the engine's real fileSystemFactory.
   1.280   */
   1.281 -void *FileSystemFactory(const char *iface, int *ret)
   1.282 +static void *
   1.283 +FileSystemFactory(const char *iface, int *ret)
   1.284  {
   1.285  	IFACE_MACRO(filesystem_factory, FileSystem);
   1.286  }
   1.287  
   1.288 -void LogMessage(const char *msg, ...)
   1.289 +void
   1.290 +mm_LogMessage(const char *msg, ...)
   1.291  {
   1.292  	va_list ap;
   1.293  	static char buffer[2048];
   1.294 @@ -702,7 +736,8 @@
   1.295  	}
   1.296  }
   1.297  
   1.298 -void DoInitialPluginLoads()
   1.299 +static void
   1.300 +DoInitialPluginLoads()
   1.301  {
   1.302  	const char *pluginFile = provider->GetCommandLineValue("mm_pluginsfile", NULL);
   1.303  	const char *mmBaseDir = provider->GetCommandLineValue("mm_basedir", NULL);
   1.304 @@ -726,13 +761,14 @@
   1.305  	char full_path[260];
   1.306  
   1.307  	g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), pluginFile);
   1.308 -	LoadPluginsFromFile(full_path);
   1.309 +	mm_LoadPluginsFromFile(full_path);
   1.310  
   1.311  	g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), mmBaseDir);
   1.312  	LookForVDFs(full_path);
   1.313  }
   1.314  
   1.315 -void StartupMetamod(bool is_vsp_load)
   1.316 +void
   1.317 +mm_StartupMetamod(bool is_vsp_load)
   1.318  {
   1.319  	char buffer[255];
   1.320  
   1.321 @@ -766,6 +802,8 @@
   1.322  #endif
   1.323  		"Metamod:Source Base Folder",
   1.324  		ConVarFlag_SpOnly);
   1.325 +	
   1.326 +	g_bIsVspBridged = true;
   1.327  
   1.328  	if (!is_vsp_load)
   1.329  	{
   1.330 @@ -774,10 +812,11 @@
   1.331  	}
   1.332  }
   1.333  
   1.334 -void InitializeGlobals(CreateInterfaceFn engineFactory, 
   1.335 -					   CreateInterfaceFn physicsFactory,
   1.336 -					   CreateInterfaceFn filesystemFactory,
   1.337 -					   CGlobalVars *pGlobals)
   1.338 +void
   1.339 +mm_InitializeGlobals(CreateInterfaceFn engineFactory, 
   1.340 +					 CreateInterfaceFn physicsFactory,
   1.341 +					 CreateInterfaceFn filesystemFactory,
   1.342 +					 CGlobalVars *pGlobals)
   1.343  {
   1.344  	engine_factory = engineFactory;
   1.345  	physics_factory = physicsFactory;
   1.346 @@ -786,27 +825,28 @@
   1.347  	provider->Notify_DLLInit_Pre(engineFactory, gamedll_info.factory);
   1.348  }
   1.349  
   1.350 -bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
   1.351 +static bool
   1.352 +Handler_DLLInit(CreateInterfaceFn engineFactory,
   1.353 +				CreateInterfaceFn physicsFactory,
   1.354 +				CreateInterfaceFn filesystemFactory,
   1.355 +				CGlobalVars *pGlobals)
   1.356  {
   1.357 -	InitializeGlobals(engineFactory, physicsFactory, filesystemFactory, pGlobals);
   1.358 -	StartupMetamod(false);
   1.359 +	mm_InitializeGlobals(engineFactory, physicsFactory, filesystemFactory, pGlobals);
   1.360 +	mm_StartupMetamod(false);
   1.361  
   1.362  	RETURN_META_VALUE(MRES_IGNORED, true);
   1.363  }
   1.364  
   1.365 -bool Handler_GameInit()
   1.366 +static bool
   1.367 +Handler_GameInit()
   1.368  {
   1.369  	if (is_game_init)
   1.370 -	{
   1.371  		return true;
   1.372 -	}
   1.373  
   1.374  	if (vsp_load_requested)
   1.375 -	{
   1.376  		InitializeVSP();
   1.377 -	}
   1.378  
   1.379 -	if (provider->IsAlternatelyLoaded() && !were_plugins_loaded)
   1.380 +	if (g_bIsVspBridged && !were_plugins_loaded)
   1.381  	{
   1.382  		DoInitialPluginLoads();
   1.383  		g_PluginMngr.SetAllLoaded();
   1.384 @@ -818,13 +858,18 @@
   1.385  	RETURN_META_VALUE(MRES_IGNORED, true);
   1.386  }
   1.387  
   1.388 -bool Handler_DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
   1.389 +static bool
   1.390 +Handler_DLLInit_Post(CreateInterfaceFn engineFactory,
   1.391 +					 CreateInterfaceFn physicsFactory,
   1.392 +					 CreateInterfaceFn filesystemFactory,
   1.393 +					 CGlobalVars *pGlobals)
   1.394  {
   1.395  	g_PluginMngr.SetAllLoaded();
   1.396  	RETURN_META_VALUE(MRES_IGNORED, true);
   1.397  }
   1.398  
   1.399 -void UnloadMetamod()
   1.400 +void
   1.401 +mm_UnloadMetamod()
   1.402  {
   1.403  	/* Unload plugins */
   1.404  	g_PluginMngr.UnloadAll();
   1.405 @@ -845,15 +890,17 @@
   1.406  	}
   1.407  }
   1.408  
   1.409 -void Handler_DLLShutdown()
   1.410 +static void
   1.411 +Handler_DLLShutdown()
   1.412  {
   1.413 -	UnloadMetamod();
   1.414 +	mm_UnloadMetamod();
   1.415  	RETURN_META(MRES_SUPERCEDE);
   1.416  }
   1.417  
   1.418 -void Handler_LevelShutdown(void)
   1.419 +static void
   1.420 +Handler_LevelShutdown(void)
   1.421  {
   1.422 -	if (provider->IsAlternatelyLoaded() && !were_plugins_loaded)
   1.423 +	if (g_bIsVspBridged && !were_plugins_loaded)
   1.424  	{
   1.425  		g_PluginMngr.SetAllLoaded();
   1.426  		DoInitialPluginLoads();
   1.427 @@ -870,7 +917,7 @@
   1.428  			"%s/%s", 
   1.429  			mod_path.c_str(),
   1.430  			provider->GetConVarString(mm_pluginsfile));
   1.431 -		LoadPluginsFromFile(full_path);
   1.432 +		mm_LoadPluginsFromFile(full_path);
   1.433  
   1.434  		g_Metamod.PathFormat(full_path,
   1.435  			sizeof(full_path),
   1.436 @@ -889,7 +936,13 @@
   1.437  	RETURN_META(MRES_IGNORED);
   1.438  }
   1.439  
   1.440 -bool Handler_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
   1.441 +static bool
   1.442 +Handler_LevelInit(char const *pMapName,
   1.443 +				  char const *pMapEntities,
   1.444 +				  char const *pOldLevel,
   1.445 +				  char const *pLandmarkName,
   1.446 +				  bool loadGame,
   1.447 +				  bool background)
   1.448  {
   1.449  	ITER_EVENT(OnLevelInit, (pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background));
   1.450  
   1.451 @@ -911,40 +964,28 @@
   1.452  CreateInterfaceFn MetamodSource::GetEngineFactory(bool syn/* =true */)
   1.453  {
   1.454  	if (syn)
   1.455 -	{
   1.456  		return EngineFactory;
   1.457 -	}
   1.458 -
   1.459  	return engine_factory;
   1.460  }
   1.461  
   1.462  CreateInterfaceFn MetamodSource::GetPhysicsFactory(bool syn/* =true */)
   1.463  {
   1.464  	if (syn)
   1.465 -	{
   1.466  		return PhysicsFactory;
   1.467 -	}
   1.468 -
   1.469  	return physics_factory;
   1.470  }
   1.471  
   1.472  CreateInterfaceFn MetamodSource::GetFileSystemFactory(bool syn/* =true */)
   1.473  {
   1.474  	if (syn)
   1.475 -	{
   1.476  		return FileSystemFactory;
   1.477 -	}
   1.478 -
   1.479  	return filesystem_factory;
   1.480  }
   1.481  
   1.482  CreateInterfaceFn MetamodSource::GetServerFactory(bool syn/* =true */)
   1.483  {
   1.484  	if (syn)
   1.485 -	{
   1.486  		return CreateInterface;
   1.487 -	}
   1.488 -
   1.489  	return gamedll_info.factory;
   1.490  }
   1.491  
   1.492 @@ -1145,9 +1186,7 @@
   1.493  void MetamodSource::EnableVSPListener()
   1.494  {
   1.495  	if (is_game_init && !vsp_load_requested && !vsp_loaded)
   1.496 -	{
   1.497  		InitializeVSP();
   1.498 -	}
   1.499  
   1.500  	vsp_load_requested = true;
   1.501  }
   1.502 @@ -1387,12 +1426,8 @@
   1.503  	gamedll_version = version;
   1.504  }
   1.505  
   1.506 -bool MetamodSource::IsAlternateLoadComplete()
   1.507 -{
   1.508 -	return were_plugins_loaded;
   1.509 -}
   1.510 -
   1.511 -void ProcessVDF(const char *path)
   1.512 +static void
   1.513 +ProcessVDF(const char *path)
   1.514  {
   1.515  	PluginId id;
   1.516  	bool already;
   1.517 @@ -1415,7 +1450,8 @@
   1.518  	}
   1.519  }
   1.520  
   1.521 -void LookForVDFs(const char *dir)
   1.522 +static void
   1.523 +LookForVDFs(const char *dir)
   1.524  {
   1.525  	char path[MAX_PATH];
   1.526  	int extidx;
   1.527 @@ -1486,3 +1522,16 @@
   1.528  	closedir(pDir);
   1.529  #endif
   1.530  }
   1.531 +
   1.532 +bool
   1.533 +mm_IsVspBridged()
   1.534 +{
   1.535 +	return g_bIsVspBridged;
   1.536 +}
   1.537 +
   1.538 +bool
   1.539 +mm_IsVspLoadComplete()
   1.540 +{
   1.541 +	return were_plugins_loaded;
   1.542 +}
   1.543 +