core/sourcemod.cpp
changeset 688 0b99367c7a5a
parent 673 fc1d50579d06
child 696 d5cc8d0fa899
     1.1 --- a/core/sourcemod.cpp	Thu Apr 12 19:49:49 2007 +0000
     1.2 +++ b/core/sourcemod.cpp	Thu Apr 12 20:18:24 2007 +0000
     1.3 @@ -45,6 +45,7 @@
     1.4  float g_LastTime = 0.0f;
     1.5  float g_LastAuthCheck = 0.0f;
     1.6  IForward *g_pOnGameFrame = NULL;
     1.7 +bool g_Loaded = false;
     1.8  
     1.9  typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
    1.10  typedef unsigned int (*GETEXPORTCOUNT)();
    1.11 @@ -188,15 +189,20 @@
    1.12  		return false;
    1.13  	}
    1.14  
    1.15 -	StartSourceMod(late);
    1.16 +	/* Hook this now so we can detect startup without calling StartSourceMod() */
    1.17 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
    1.18 +
    1.19 +	/* Only load if we're not late */
    1.20 +	if (!late)
    1.21 +	{
    1.22 +		StartSourceMod(false);
    1.23 +	}
    1.24  
    1.25  	return true;
    1.26  }
    1.27  
    1.28  void SourceModBase::StartSourceMod(bool late)
    1.29  {
    1.30 -	/* First initialize the global hooks we need */
    1.31 -	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
    1.32  	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
    1.33  	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
    1.34  
    1.35 @@ -224,10 +230,20 @@
    1.36  
    1.37  	/* Add us now... */
    1.38  	g_ShareSys.AddInterface(NULL, this);
    1.39 +
    1.40 +	/* We're loaded! */
    1.41 +	g_Loaded = true;
    1.42  }
    1.43  
    1.44  bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
    1.45  {
    1.46 +	/* If we're not loaded... */
    1.47 +	if (!g_Loaded)
    1.48 +	{
    1.49 +		/* Do all global initialization now */
    1.50 +		StartSourceMod(true);
    1.51 +	}
    1.52 +
    1.53  	m_IsMapLoading = true;
    1.54  	m_ExecPluginReload = true;
    1.55  	g_LastTime = 0.0f;
    1.56 @@ -358,53 +374,57 @@
    1.57  
    1.58  void SourceModBase::CloseSourceMod()
    1.59  {
    1.60 -	if (g_pOnGameFrame)
    1.61 -	{
    1.62 -		g_Forwards.ReleaseForward(g_pOnGameFrame);
    1.63 -	}
    1.64 -
    1.65 -	/* Notify! */
    1.66 -	SMGlobalClass *pBase = SMGlobalClass::head;
    1.67 -	while (pBase)
    1.68 -	{
    1.69 -		pBase->OnSourceModShutdown();
    1.70 -		pBase = pBase->m_pGlobalClassNext;
    1.71 -	}
    1.72 -
    1.73 -	/* Delete all data packs */
    1.74 -	CStack<CDataPack *>::iterator iter;
    1.75 -	CDataPack *pd;
    1.76 -	for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
    1.77 -	{
    1.78 -		pd = (*iter);
    1.79 -		delete pd;
    1.80 -	}
    1.81 -	m_freepacks.popall();
    1.82 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
    1.83  
    1.84 -	/* Notify! */
    1.85 -	pBase = SMGlobalClass::head;
    1.86 -	while (pBase)
    1.87 -	{
    1.88 -		pBase->OnSourceModAllShutdown();
    1.89 -		pBase = pBase->m_pGlobalClassNext;
    1.90 -	}
    1.91 -
    1.92 -	if (enginePatch)
    1.93 +	if (g_Loaded)
    1.94  	{
    1.95 -		SH_RELEASE_CALLCLASS(enginePatch);
    1.96 -		enginePatch = NULL;
    1.97 -	}
    1.98 +		if (g_pOnGameFrame)
    1.99 +		{
   1.100 +			g_Forwards.ReleaseForward(g_pOnGameFrame);
   1.101 +		}
   1.102 +	
   1.103 +		/* Notify! */
   1.104 +		SMGlobalClass *pBase = SMGlobalClass::head;
   1.105 +		while (pBase)
   1.106 +		{
   1.107 +			pBase->OnSourceModShutdown();
   1.108 +			pBase = pBase->m_pGlobalClassNext;
   1.109 +		}
   1.110 +	
   1.111 +		/* Delete all data packs */
   1.112 +		CStack<CDataPack *>::iterator iter;
   1.113 +		CDataPack *pd;
   1.114 +		for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
   1.115 +		{
   1.116 +			pd = (*iter);
   1.117 +			delete pd;
   1.118 +		}
   1.119 +		m_freepacks.popall();
   1.120 +	
   1.121 +		/* Notify! */
   1.122 +		pBase = SMGlobalClass::head;
   1.123 +		while (pBase)
   1.124 +		{
   1.125 +			pBase->OnSourceModAllShutdown();
   1.126 +			pBase = pBase->m_pGlobalClassNext;
   1.127 +		}
   1.128 +	
   1.129 +		if (enginePatch)
   1.130 +		{
   1.131 +			SH_RELEASE_CALLCLASS(enginePatch);
   1.132 +			enginePatch = NULL;
   1.133 +		}
   1.134 +	
   1.135 +		if (gamedllPatch)
   1.136 +		{
   1.137 +			SH_RELEASE_CALLCLASS(gamedllPatch);
   1.138 +			gamedllPatch = NULL;
   1.139 +		}
   1.140  
   1.141 -	if (gamedllPatch)
   1.142 -	{
   1.143 -		SH_RELEASE_CALLCLASS(gamedllPatch);
   1.144 -		gamedllPatch = NULL;
   1.145 +		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   1.146 +		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
   1.147  	}
   1.148  
   1.149 -	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   1.150 -	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   1.151 -	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
   1.152 -
   1.153  	/* Rest In Peace */
   1.154  	ShutdownJIT();
   1.155  }