core/sourcemod.cpp
changeset 4029 cf8bdfcf6503
parent 4013 6201c1180e0e
child 4116 3e58428d988f
     1.1 --- a/core/sourcemod.cpp	Wed Aug 21 10:55:12 2013 -0400
     1.2 +++ b/core/sourcemod.cpp	Thu Aug 22 14:05:44 2013 -0700
     1.3 @@ -129,6 +129,8 @@
     1.4  	return ConfigResult_Ignore;
     1.5  }
     1.6  
     1.7 +static bool sSourceModInitialized = false;
     1.8 +
     1.9  bool SourceModBase::InitializeSourceMod(char *error, size_t maxlength, bool late)
    1.10  {
    1.11  	const char *gamepath = g_SMAPI->GetBaseDir();
    1.12 @@ -240,6 +242,8 @@
    1.13  
    1.14  	g_pSourcePawn2->SetDebugListener(logicore.debugger);
    1.15  
    1.16 +	sSourceModInitialized = true;
    1.17 +
    1.18  	/* Hook this now so we can detect startup without calling StartSourceMod() */
    1.19  	SH_ADD_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(this, &SourceModBase::LevelInit), false);
    1.20  
    1.21 @@ -475,69 +479,74 @@
    1.22  
    1.23  void SourceModBase::CloseSourceMod()
    1.24  {
    1.25 -	/* Force a level end */
    1.26 -	LevelShutdown();
    1.27 +	if (!sSourceModInitialized)
    1.28 +		return;
    1.29 +
    1.30 +	SH_REMOVE_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(this, &SourceModBase::LevelInit), false);
    1.31  
    1.32 +	if (g_Loaded)
    1.33 +	{
    1.34 +		/* Force a level end */
    1.35 +		LevelShutdown();
    1.36 +		ShutdownServices();
    1.37 +	}
    1.38 +
    1.39 +	/* Rest In Peace */
    1.40 +	ShutdownLogicBridge();
    1.41 +	ShutdownJIT();
    1.42 +}
    1.43 +
    1.44 +void SourceModBase::ShutdownServices()
    1.45 +{
    1.46  	/* Unload plugins */
    1.47  	scripts->Shutdown();
    1.48  
    1.49  	/* Unload extensions */
    1.50  	extsys->Shutdown();
    1.51  
    1.52 -	SH_REMOVE_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(this, &SourceModBase::LevelInit), false);
    1.53 +	if (g_pOnMapEnd)
    1.54 +		g_Forwards.ReleaseForward(g_pOnMapEnd);
    1.55  
    1.56 -	if (g_Loaded)
    1.57 +	/* Notify! */
    1.58 +	SMGlobalClass *pBase = SMGlobalClass::head;
    1.59 +	while (pBase)
    1.60  	{
    1.61 -		if (g_pOnMapEnd)
    1.62 -		{
    1.63 -			g_Forwards.ReleaseForward(g_pOnMapEnd);
    1.64 -		}
    1.65 -	
    1.66 -		/* Notify! */
    1.67 -		SMGlobalClass *pBase = SMGlobalClass::head;
    1.68 -		while (pBase)
    1.69 -		{
    1.70 -			pBase->OnSourceModShutdown();
    1.71 -			pBase = pBase->m_pGlobalClassNext;
    1.72 -		}
    1.73 -	
    1.74 -		/* Delete all data packs */
    1.75 -		CStack<CDataPack *>::iterator iter;
    1.76 -		CDataPack *pd;
    1.77 -		for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
    1.78 -		{
    1.79 -			pd = (*iter);
    1.80 -			delete pd;
    1.81 -		}
    1.82 -		m_freepacks.popall();
    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 -		{
    1.94 -			SH_RELEASE_CALLCLASS(enginePatch);
    1.95 -			enginePatch = NULL;
    1.96 -		}
    1.97 -	
    1.98 -		if (gamedllPatch)
    1.99 -		{
   1.100 -			SH_RELEASE_CALLCLASS(gamedllPatch);
   1.101 -			gamedllPatch = NULL;
   1.102 -		}
   1.103 -
   1.104 -		SH_REMOVE_HOOK(IServerGameDLL, LevelShutdown, gamedll, SH_MEMBER(this, &SourceModBase::LevelShutdown), false);
   1.105 -		SH_REMOVE_HOOK(IServerGameDLL, GameFrame, gamedll, SH_MEMBER(&g_Timers, &TimerSystem::GameFrame), false);
   1.106 +		pBase->OnSourceModShutdown();
   1.107 +		pBase = pBase->m_pGlobalClassNext;
   1.108  	}
   1.109  
   1.110 -	/* Rest In Peace */
   1.111 -	ShutdownLogicBridge();
   1.112 -	ShutdownJIT();
   1.113 +	/* Delete all data packs */
   1.114 +	CStack<CDataPack *>::iterator iter;
   1.115 +	CDataPack *pd;
   1.116 +	for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
   1.117 +	{
   1.118 +		pd = (*iter);
   1.119 +		delete pd;
   1.120 +	}
   1.121 +	m_freepacks.popall();
   1.122 +
   1.123 +	/* Notify! */
   1.124 +	pBase = SMGlobalClass::head;
   1.125 +	while (pBase)
   1.126 +	{
   1.127 +		pBase->OnSourceModAllShutdown();
   1.128 +		pBase = pBase->m_pGlobalClassNext;
   1.129 +	}
   1.130 +
   1.131 +	if (enginePatch)
   1.132 +	{
   1.133 +		SH_RELEASE_CALLCLASS(enginePatch);
   1.134 +		enginePatch = NULL;
   1.135 +	}
   1.136 +
   1.137 +	if (gamedllPatch)
   1.138 +	{
   1.139 +		SH_RELEASE_CALLCLASS(gamedllPatch);
   1.140 +		gamedllPatch = NULL;
   1.141 +	}
   1.142 +
   1.143 +	SH_REMOVE_HOOK(IServerGameDLL, LevelShutdown, gamedll, SH_MEMBER(this, &SourceModBase::LevelShutdown), false);
   1.144 +	SH_REMOVE_HOOK(IServerGameDLL, GameFrame, gamedll, SH_MEMBER(&g_Timers, &TimerSystem::GameFrame), false);
   1.145  }
   1.146  
   1.147  void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)