core/PluginSys.cpp
changeset 3703 391951cdde71
parent 3700 d2194fab1dd5
child 3790 98b099a406d2
     1.1 --- a/core/PluginSys.cpp	Fri Dec 14 15:12:22 2012 -0500
     1.2 +++ b/core/PluginSys.cpp	Fri Dec 14 15:15:12 2012 -0500
     1.3 @@ -501,14 +501,14 @@
     1.4  	m_bSilentlyFailed = sf;
     1.5  }
     1.6  
     1.7 -void CPlugin::LibraryActions(bool dropping)
     1.8 +void CPlugin::LibraryActions(LibraryAction action)
     1.9  {
    1.10  	List<String>::iterator iter;
    1.11  	for (iter = m_Libraries.begin();
    1.12  		iter != m_Libraries.end();
    1.13  		iter++)
    1.14  	{
    1.15 -		g_PluginSys.OnLibraryAction((*iter).c_str(), true, dropping);
    1.16 +		g_PluginSys.OnLibraryAction((*iter).c_str(), action);
    1.17  	}
    1.18  }
    1.19  
    1.20 @@ -523,7 +523,7 @@
    1.21  
    1.22  	if (paused)
    1.23  	{
    1.24 -		LibraryActions(true);
    1.25 +		LibraryActions(LibraryAction_Removed);
    1.26  	}
    1.27  
    1.28  	IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("OnPluginPauseChange");
    1.29 @@ -547,7 +547,7 @@
    1.30  
    1.31  	if (!paused)
    1.32  	{
    1.33 -		LibraryActions(false);
    1.34 +		LibraryActions(LibraryAction_Added);
    1.35  	}
    1.36  
    1.37  	return true;
    1.38 @@ -1495,7 +1495,7 @@
    1.39  		s_iter != pPlugin->m_Libraries.end();
    1.40  		s_iter++)
    1.41  	{
    1.42 -		OnLibraryAction((*s_iter).c_str(), true, false);
    1.43 +		OnLibraryAction((*s_iter).c_str(), LibraryAction_Added);
    1.44  	}
    1.45  
    1.46  	/* :TODO: optimize? does this even matter? */
    1.47 @@ -1597,7 +1597,7 @@
    1.48  		 s_iter != pPlugin->m_Libraries.end();
    1.49  		 s_iter++)
    1.50  	{
    1.51 -		OnLibraryAction((*s_iter).c_str(), true, true);
    1.52 +		OnLibraryAction((*s_iter).c_str(), LibraryAction_Removed);
    1.53  	}
    1.54  
    1.55  	List<IPluginsListener *>::iterator iter;
    1.56 @@ -1914,6 +1914,9 @@
    1.57  	g_RootMenu.AddRootConsoleCommand("plugins", "Manage Plugins", this);
    1.58  
    1.59  	g_ShareSys.AddInterface(NULL, this);
    1.60 +	
    1.61 +	m_pOnLibraryAdded = g_Forwards.CreateForward("OnLibraryAdded", ET_Ignore, 1, NULL, Param_String);
    1.62 +	m_pOnLibraryRemoved = g_Forwards.CreateForward("OnLibraryRemoved", ET_Ignore, 1, NULL, Param_String);
    1.63  }
    1.64  
    1.65  void CPluginManager::OnSourceModShutdown()
    1.66 @@ -1925,6 +1928,9 @@
    1.67  	g_HandleSys.RemoveType(g_PluginType, m_MyIdent);
    1.68  	g_ShareSys.DestroyIdentType(g_PluginIdent);
    1.69  	g_ShareSys.DestroyIdentity(m_MyIdent);
    1.70 +	
    1.71 +	g_Forwards.ReleaseForward(m_pOnLibraryAdded);
    1.72 +	g_Forwards.ReleaseForward(m_pOnLibraryRemoved);
    1.73  }
    1.74  
    1.75  ConfigResult CPluginManager::OnSourceModConfigChanged(const char *key, 
    1.76 @@ -2551,83 +2557,18 @@
    1.77  	return (CPlugin *)(pToken->ptr);
    1.78  }
    1.79  
    1.80 -void CPluginManager::OnLibraryAction(const char *lib, bool is_a_plugin, bool drop)
    1.81 +void CPluginManager::OnLibraryAction(const char *lib, LibraryAction action)
    1.82  {
    1.83 -	List<CPlugin *>::iterator iter;
    1.84 -
    1.85 -	struct _pl
    1.86 -	{
    1.87 -		cell_t name;
    1.88 -		cell_t file;
    1.89 -		cell_t required;
    1.90 -	} *plc;
    1.91 -
    1.92 -	struct _ext
    1.93 -	{
    1.94 -		cell_t name;
    1.95 -		cell_t file;
    1.96 -		cell_t autoload;
    1.97 -		cell_t required;
    1.98 -	} *ext;
    1.99 -
   1.100 -	const char *name = drop ? "OnLibraryRemoved" : "OnLibraryAdded";
   1.101 -
   1.102 -	for (iter=m_plugins.begin();
   1.103 -		 iter!=m_plugins.end();
   1.104 -		 iter++)
   1.105 +	switch (action)
   1.106  	{
   1.107 -		CPlugin *pl = (*iter);
   1.108 -		if (pl->GetStatus() != Plugin_Running)
   1.109 -		{
   1.110 -			continue;
   1.111 -		}
   1.112 -		IPluginContext *pContext = pl->GetBaseContext();
   1.113 -		IPluginFunction *pf = pContext->GetFunctionByName(name);
   1.114 -		if (!pf)
   1.115 -		{
   1.116 -			continue;
   1.117 -		}
   1.118 -		uint32_t num_vars = pContext->GetPubVarsNum();
   1.119 -		for (uint32_t i=0; i<num_vars; i++)
   1.120 -		{
   1.121 -			sp_pubvar_t *pubvar;
   1.122 -			if (pContext->GetPubvarByIndex(i, &pubvar) != SP_ERROR_NONE)
   1.123 -			{
   1.124 -				continue;
   1.125 -			}
   1.126 -			if (is_a_plugin && strncmp(pubvar->name, "__pl_", 5) == 0)
   1.127 -			{
   1.128 -				plc = (_pl *)pubvar->offs;
   1.129 -				if (plc->required)
   1.130 -				{
   1.131 -					continue;
   1.132 -				}
   1.133 -				char *str;
   1.134 -				pContext->LocalToString(plc->name, &str);
   1.135 -				if (strcmp(str, lib) != 0)
   1.136 -				{
   1.137 -					continue;
   1.138 -				}
   1.139 -				pf->PushString(lib);
   1.140 -				pf->Execute(NULL);
   1.141 -			}
   1.142 -			else if (!is_a_plugin && strncmp(pubvar->name, "__ext_", 6) == 0)
   1.143 -			{
   1.144 -				ext = (_ext *)pubvar->offs;
   1.145 -				if (ext->required)
   1.146 -				{
   1.147 -					continue;
   1.148 -				}
   1.149 -				char *str;
   1.150 -				pContext->LocalToString(ext->name, &str);
   1.151 -				if (strcmp(str, lib) != 0)
   1.152 -				{
   1.153 -					continue;
   1.154 -				}
   1.155 -				pf->PushString(lib);
   1.156 -				pf->Execute(NULL);
   1.157 -			}
   1.158 -		}
   1.159 +	case LibraryAction_Removed:
   1.160 +		m_pOnLibraryRemoved->PushString(lib);
   1.161 +		m_pOnLibraryRemoved->Execute(NULL);
   1.162 +		break;
   1.163 +	case LibraryAction_Added:
   1.164 +		m_pOnLibraryAdded->PushString(lib);
   1.165 +		m_pOnLibraryAdded->Execute(NULL);
   1.166 +		break;
   1.167  	}
   1.168  }
   1.169