core/metamod_plugins.cpp
changeset 768 e6394d70ced0
parent 747 9563de0165f7
child 850 3d9258ef9a30
     1.1 --- a/core/metamod_plugins.cpp	Sat Oct 15 22:16:50 2011 -0700
     1.2 +++ b/core/metamod_plugins.cpp	Mon Dec 12 15:53:56 2011 -0600
     1.3 @@ -382,6 +382,36 @@
     1.4  	}
     1.5  }
     1.6  
     1.7 +struct Unloader : public SourceHook::Impl::UnloadListener
     1.8 +{
     1.9 +	CPluginManager::CPlugin *plugin_;
    1.10 +	bool destroy_;
    1.11 +
    1.12 +	Unloader(CPluginManager::CPlugin *plugin, bool destroy)
    1.13 +	  : plugin_(plugin), destroy_(destroy)
    1.14 +	{ }
    1.15 +
    1.16 +	void ReadyToUnload(SourceHook::Plugin plug)
    1.17 +	{
    1.18 +		if (plugin_->m_UnloadFn != NULL)
    1.19 +			plugin_->m_UnloadFn();
    1.20 +
    1.21 +		dlclose(plugin_->m_Lib);
    1.22 +
    1.23 +		if (destroy_)
    1.24 +		{
    1.25 +			delete plugin_;
    1.26 +		}
    1.27 +		else
    1.28 +		{
    1.29 +			plugin_->m_Lib = NULL;
    1.30 +			plugin_->m_API = NULL;
    1.31 +		}
    1.32 +
    1.33 +		delete this;
    1.34 +	}
    1.35 +};
    1.36 +
    1.37  CPluginManager::CPlugin *CPluginManager::_Load(const char *file, PluginId source, char *error, size_t maxlen)
    1.38  {
    1.39  	FILE *fp;
    1.40 @@ -552,17 +582,8 @@
    1.41  	if (pl->m_Lib && (pl->m_Status < Pl_Paused))
    1.42  	{
    1.43  		pl->m_Events.clear();
    1.44 -		g_SourceHook.UnloadPlugin(pl->m_Id);
    1.45  		UnregAllConCmds(pl);
    1.46 -
    1.47 -		if (pl->m_UnloadFn != NULL)
    1.48 -		{
    1.49 -			pl->m_UnloadFn();
    1.50 -		}
    1.51 -
    1.52 -		dlclose(pl->m_Lib);
    1.53 -		pl->m_Lib = NULL;
    1.54 -		pl->m_API = NULL;
    1.55 +		g_SourceHook.UnloadPlugin(pl->m_Id, new Unloader(pl, false));
    1.56  	}
    1.57  
    1.58  	return pl;
    1.59 @@ -580,23 +601,9 @@
    1.60  		//Note, we'll always tell the plugin it will be unloading...
    1.61  		if (pl->m_API->Unload(error, maxlen) || force)
    1.62  		{
    1.63 -			//Make sure to detach it from sourcehook!
    1.64 -			g_SourceHook.UnloadPlugin(pl->m_Id);
    1.65 -
    1.66  			pl->m_Events.clear();
    1.67 -
    1.68  			UnregAllConCmds(pl);
    1.69  
    1.70 -			if (pl->m_UnloadFn != NULL)
    1.71 -			{
    1.72 -				pl->m_UnloadFn();
    1.73 -			}
    1.74 -
    1.75 -			//Clean up the DLL
    1.76 -			dlclose(pl->m_Lib);
    1.77 -			pl->m_Lib = NULL;
    1.78 -			pl->m_API = NULL;
    1.79 -
    1.80  			//Remove the plugin from the list
    1.81  			PluginIter i;
    1.82  			for (i=m_Plugins.begin(); i!=m_Plugins.end(); i++)
    1.83 @@ -607,9 +614,9 @@
    1.84  					break;
    1.85  				}
    1.86  			}
    1.87 -			//Free its memory
    1.88 -			delete pl;
    1.89  
    1.90 +			//Make sure to detach it from sourcehook!
    1.91 +			g_SourceHook.UnloadPlugin(pl->m_Id, new Unloader(pl, true));
    1.92  			return true;
    1.93  		}
    1.94  	} else {