1.1 --- a/core/systems/PluginSys.cpp Tue Jan 16 19:34:32 2007 +0000
1.2 +++ b/core/systems/PluginSys.cpp Tue Jan 16 19:41:21 2007 +0000
1.3 @@ -7,6 +7,7 @@
1.4 #include "sourcemod.h"
1.5 #include "CTextParsers.h"
1.6 #include "CLogger.h"
1.7 +#include "ExtensionSys.h"
1.8
1.9 CPluginManager g_PluginSys;
1.10 HandleType_t g_PluginType = 0;
1.11 @@ -31,6 +32,16 @@
1.12
1.13 CPlugin::~CPlugin()
1.14 {
1.15 + if (m_handle)
1.16 + {
1.17 + HandleSecurity sec;
1.18 + sec.pOwner = g_PluginSys.GetIdentity();
1.19 + sec.pIdentity = sec.pOwner;
1.20 +
1.21 + g_HandleSys.FreeHandle(m_handle, &sec);
1.22 + g_ShareSys.DestroyIdentity(m_ident);
1.23 + }
1.24 +
1.25 if (m_ctx.base)
1.26 {
1.27 delete m_ctx.base;
1.28 @@ -72,16 +83,6 @@
1.29 g_pSourcePawn->FreeFromMemory(m_plugin);
1.30 m_plugin = NULL;
1.31 }
1.32 -
1.33 - if (m_handle)
1.34 - {
1.35 - HandleSecurity sec;
1.36 - sec.pOwner = g_PluginSys.GetIdentity();
1.37 - sec.pIdentity = sec.pOwner;
1.38 -
1.39 - g_HandleSys.FreeHandle(m_handle, &sec);
1.40 - g_ShareSys.DestroyIdentity(m_ident);
1.41 - }
1.42 }
1.43
1.44 void CPlugin::InitIdentity()
1.45 @@ -100,18 +101,16 @@
1.46 g_LibSys.PathFormat(fullpath, sizeof(fullpath), "%s/plugins/%s", g_SourceMod.GetSMBaseDir(), file);
1.47 FILE *fp = fopen(fullpath, "rb");
1.48
1.49 + CPlugin *pPlugin = new CPlugin(file);
1.50 +
1.51 if (!fp)
1.52 {
1.53 if (error)
1.54 {
1.55 snprintf(error, maxlength, "Unable to open file");
1.56 - return NULL;
1.57 - } else {
1.58 - CPlugin *pPlugin = new CPlugin(file);
1.59 - snprintf(pPlugin->m_errormsg, sizeof(pPlugin->m_errormsg), "Unable to open file");
1.60 - pPlugin->m_status = Plugin_BadLoad;
1.61 - return pPlugin;
1.62 }
1.63 + pPlugin->m_status = Plugin_BadLoad;
1.64 + return pPlugin;
1.65 }
1.66
1.67 int err;
1.68 @@ -122,19 +121,15 @@
1.69 if (error)
1.70 {
1.71 snprintf(error, maxlength, "Error %d while parsing plugin", err);
1.72 - return NULL;
1.73 - } else {
1.74 - CPlugin *pPlugin = new CPlugin(file);
1.75 - snprintf(pPlugin->m_errormsg, sizeof(pPlugin->m_errormsg), "Error %d while parsing plugin", err);
1.76 - pPlugin->m_status = Plugin_BadLoad;
1.77 - return pPlugin;
1.78 }
1.79 + pPlugin->m_status = Plugin_BadLoad;
1.80 + return pPlugin;
1.81 }
1.82
1.83 fclose(fp);
1.84
1.85 - CPlugin *pPlugin = new CPlugin(file);
1.86 pPlugin->m_plugin = pl;
1.87 +
1.88 return pPlugin;
1.89 }
1.90
1.91 @@ -185,10 +180,8 @@
1.92 if (!m_ctx.ctx)
1.93 {
1.94 memset(&m_ctx, 0, sizeof(m_ctx));
1.95 - if (!error)
1.96 + if (error)
1.97 {
1.98 - SetErrorState(Plugin_Failed, "Failed to compile (error %d)", err);
1.99 - } else {
1.100 snprintf(error, maxlength, "Failed to compile (error %d)", err);
1.101 }
1.102 return false;
1.103 @@ -377,12 +370,6 @@
1.104 return false;
1.105 }
1.106
1.107 - if (!error)
1.108 - {
1.109 - error = m_errormsg;
1.110 - maxlength = sizeof(m_errormsg);
1.111 - }
1.112 -
1.113 m_status = Plugin_Loaded;
1.114
1.115 int err;
1.116 @@ -676,20 +663,20 @@
1.117
1.118 //well i have discovered that gabe newell is very fat, so i wrote this comment now
1.119 //:TODO: remove this function, create a better wrapper for LoadPlugin()/LoadAutoPlugin()
1.120 -void CPluginManager::LoadAutoPlugin(const char *file)
1.121 +bool CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool debug, PluginType type, char error[], size_t err_max)
1.122 {
1.123 /**
1.124 * Does this plugin already exist?
1.125 */
1.126 CPlugin *pPlugin;
1.127 - if (sm_trie_retrieve(m_LoadLookup, file, (void **)&pPlugin))
1.128 + if (sm_trie_retrieve(m_LoadLookup, path, (void **)&pPlugin))
1.129 {
1.130 /* First check the type */
1.131 PluginType type = pPlugin->GetType();
1.132 if (type == PluginType_Private
1.133 || type == PluginType_Global)
1.134 {
1.135 - return;
1.136 + return true;
1.137 }
1.138 /* Check to see if we should try reloading it */
1.139 if (pPlugin->GetStatus() == Plugin_BadLoad
1.140 @@ -700,7 +687,7 @@
1.141 }
1.142 }
1.143
1.144 - pPlugin = CPlugin::CreatePlugin(file, NULL, 0);
1.145 + pPlugin = CPlugin::CreatePlugin(path, error, err_max);
1.146
1.147 assert(pPlugin != NULL);
1.148
1.149 @@ -717,8 +704,7 @@
1.150 unsigned int setcount = m_PluginInfo.GetSettingsNum();
1.151 for (unsigned int i=0; i<setcount; i++)
1.152 {
1.153 - pset = m_PluginInfo.GetSettingsIfMatch(i, file);
1.154 - if (!pset)
1.155 + if ((pset=m_PluginInfo.GetSettingsIfMatch(i, path)) == NULL)
1.156 {
1.157 continue;
1.158 }
1.159 @@ -735,8 +721,12 @@
1.160 }
1.161 if (!g_pVM->SetCompilationOption(co, key, val))
1.162 {
1.163 - pPlugin->SetErrorState(Plugin_Failed, "Unable to set option (key \"%s\") (value \"%s\")", key, val);
1.164 + if (error)
1.165 + {
1.166 + snprintf(error, err_max, "Unable to set JIT option (key \"%s\") (value \"%s\")", key, val);
1.167 + }
1.168 pPlugin->CancelMyCompile();
1.169 + pPlugin->m_status = Plugin_Failed;
1.170 co = NULL;
1.171 break;
1.172 }
1.173 @@ -744,100 +734,71 @@
1.174 }
1.175 }
1.176
1.177 + /* Do the actual compiling */
1.178 if (co)
1.179 {
1.180 pPlugin->FinishMyCompile(NULL, 0);
1.181 co = NULL;
1.182 }
1.183
1.184 - /* We don't care about the return value */
1.185 + /* Get the status */
1.186 if (pPlugin->GetStatus() == Plugin_Created)
1.187 {
1.188 AddCoreNativesToPlugin(pPlugin);
1.189 pPlugin->InitIdentity();
1.190 - pPlugin->Call_AskPluginLoad(NULL, 0);
1.191 + pPlugin->Call_AskPluginLoad(error, err_max);
1.192 }
1.193
1.194 - AddPlugin(pPlugin);
1.195 + if (_plugin)
1.196 + {
1.197 + *_plugin = pPlugin;
1.198 + }
1.199 +
1.200 + return (pPlugin->GetStatus() == Plugin_Loaded);
1.201 }
1.202
1.203 IPlugin *CPluginManager::LoadPlugin(const char *path, bool debug, PluginType type, char error[], size_t err_max)
1.204 {
1.205 - /* See if this plugin is already loaded... reformat to get sep chars right */
1.206 - char checkpath[PLATFORM_MAX_PATH+1];
1.207 - g_LibSys.PathFormat(checkpath, sizeof(checkpath), "%s", path);
1.208 -
1.209 - /**
1.210 - * In manually loading a plugin, any sort of load error causes a deletion.
1.211 - * This is because it's assumed manually loaded plugins will not be managed.
1.212 - * For managed plugins, we need the UI to report them properly.
1.213 - */
1.214 + CPlugin *pl;
1.215
1.216 - CPlugin *pPlugin;
1.217 - if (sm_trie_retrieve(m_LoadLookup, checkpath, (void **)&pPlugin))
1.218 + if (!_LoadPlugin(&pl, path, debug, type, error, err_max))
1.219 {
1.220 - snprintf(error, err_max, "Plugin file is already loaded");
1.221 - return NULL;
1.222 - }
1.223 -
1.224 - pPlugin = CPlugin::CreatePlugin(path, error, err_max);
1.225 -
1.226 - if (!pPlugin)
1.227 - {
1.228 + delete pl;
1.229 return NULL;
1.230 }
1.231
1.232 - ICompilation *co = pPlugin->StartMyCompile(g_pVM);
1.233 - if (!co || (debug && !g_pVM->SetCompilationOption(co, "debug", "1")))
1.234 + AddPlugin(pl);
1.235 +
1.236 + return pl;
1.237 +}
1.238 +
1.239 +void CPluginManager::LoadAutoPlugin(const char *plugin)
1.240 +{
1.241 + CPlugin *pl;
1.242 + char error[255] = "Unknown error";
1.243 +
1.244 + if (!_LoadPlugin(&pl, plugin, false, PluginType_MapUpdated, error, sizeof(error)))
1.245 {
1.246 - snprintf(error, err_max, "Unable to start%s compilation", debug ? " debug" : "");
1.247 - pPlugin->CancelMyCompile();
1.248 - delete pPlugin;
1.249 - return NULL;
1.250 - }
1.251 -
1.252 - if (!pPlugin->FinishMyCompile(error, err_max))
1.253 - {
1.254 - delete pPlugin;
1.255 - return NULL;
1.256 + g_Logger.LogError("[SOURCEMOD] Failed to load plugin \"%s\": %s", plugin, error);
1.257 + pl->SetErrorState(Plugin_Failed, "%s", error);
1.258 }
1.259
1.260 - pPlugin->m_type = type;
1.261 -
1.262 - AddCoreNativesToPlugin(pPlugin);
1.263 -
1.264 - pPlugin->InitIdentity();
1.265 -
1.266 - /* Finally, ask the plugin if it wants to be loaded */
1.267 - if (!pPlugin->Call_AskPluginLoad(error, err_max))
1.268 - {
1.269 - delete pPlugin;
1.270 - return NULL;
1.271 - }
1.272 -
1.273 - AddPlugin(pPlugin);
1.274 -
1.275 - return pPlugin;
1.276 + AddPlugin(pl);
1.277 }
1.278
1.279 void CPluginManager::AddPlugin(CPlugin *pPlugin)
1.280 {
1.281 - m_plugins.push_back(pPlugin);
1.282 - sm_trie_insert(m_LoadLookup, pPlugin->m_filename, pPlugin);
1.283 -
1.284 List<IPluginsListener *>::iterator iter;
1.285 IPluginsListener *pListener;
1.286 +
1.287 for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++)
1.288 {
1.289 pListener = (*iter);
1.290 pListener->OnPluginCreated(pPlugin);
1.291 }
1.292
1.293 - /* If the second pass was already completed, we have to run the pass on this plugin */
1.294 - if (m_AllPluginsLoaded && pPlugin->GetStatus() == Plugin_Loaded)
1.295 - {
1.296 - RunSecondPass(pPlugin);
1.297 - }
1.298 + m_plugins.push_back(pPlugin);
1.299 + sm_trie_insert(m_LoadLookup, pPlugin->m_filename, pPlugin);
1.300 }
1.301
1.302 void CPluginManager::LoadAll_SecondPass()
1.303 @@ -850,16 +811,77 @@
1.304 pPlugin = (*iter);
1.305 if (pPlugin->GetStatus() == Plugin_Loaded)
1.306 {
1.307 - RunSecondPass(pPlugin);
1.308 + /* :TODO: fix */
1.309 + RunSecondPass(pPlugin, NULL, 0);
1.310 }
1.311 }
1.312 +
1.313 m_AllPluginsLoaded = true;
1.314 }
1.315
1.316 -void CPluginManager::RunSecondPass(CPlugin *pPlugin)
1.317 +bool CPluginManager::RunSecondPass(CPlugin *pPlugin, char *error, size_t maxlength)
1.318 {
1.319 - /* Tell this plugin to finish initializing itself */
1.320 - pPlugin->Call_OnPluginInit();
1.321 + /* Find any extensions this plugin needs */
1.322 + struct _ext
1.323 + {
1.324 + cell_t name;
1.325 + cell_t file;
1.326 + cell_t autoload;
1.327 + cell_t required;
1.328 + } *ext;
1.329 +
1.330 + IPluginContext *pBase = pPlugin->GetBaseContext();
1.331 + uint32_t num = pBase->GetPubVarsNum();
1.332 + sp_pubvar_t *pubvar;
1.333 + IExtension *pExt;
1.334 + char path[PLATFORM_MAX_PATH+1];
1.335 + char *file, *name;
1.336 + for (uint32_t i=0; i<num; i++)
1.337 + {
1.338 + if (pBase->GetPubvarByIndex(i, &pubvar) != SP_ERROR_NONE)
1.339 + {
1.340 + continue;
1.341 + }
1.342 + if (strncmp(pubvar->name, "__ext_", 6) == 0)
1.343 + {
1.344 + ext = (_ext *)pubvar->offs;
1.345 + if (!ext->required && !ext->autoload)
1.346 + {
1.347 + continue;
1.348 + }
1.349 + if (pBase->LocalToString(ext->file, &file) != SP_ERROR_NONE)
1.350 + {
1.351 + continue;
1.352 + }
1.353 + if (pBase->LocalToString(ext->name, &name) != SP_ERROR_NONE)
1.354 + {
1.355 + continue;
1.356 + }
1.357 + snprintf(path, PLATFORM_MAX_PATH, "%s.%s", file, PLATFORM_LIB_EXT);
1.358 + pExt = NULL;
1.359 + /* Attempt to auto-load if necessary */
1.360 + if (ext->autoload)
1.361 + {
1.362 + pExt = g_Extensions.LoadAutoExtension(path);
1.363 + }
1.364 + if (ext->required && !pExt)
1.365 + {
1.366 + if ((pExt = g_Extensions.FindExtensionByFile(path)) == NULL)
1.367 + {
1.368 + pExt = g_Extensions.FindExtensionByName(name);
1.369 + }
1.370 + }
1.371 + if (ext->required && (!pExt || !pExt->IsLoaded()))
1.372 + {
1.373 + if (error)
1.374 + {
1.375 + snprintf(error, maxlength, "Required extension \"%s\" (file \"%s\") not found", name, file);
1.376 + }
1.377 + pPlugin->m_status = Plugin_Failed;
1.378 + return false;
1.379 + }
1.380 + }
1.381 + }
1.382
1.383 /* Finish by telling all listeners */
1.384 List<IPluginsListener *>::iterator iter;
1.385 @@ -869,6 +891,11 @@
1.386 pListener = (*iter);
1.387 pListener->OnPluginLoaded(pPlugin);
1.388 }
1.389 +
1.390 + /* Tell this plugin to finish initializing itself */
1.391 + pPlugin->Call_OnPluginInit();
1.392 +
1.393 + return true;
1.394 }
1.395
1.396 void CPluginManager::AddCoreNativesToPlugin(CPlugin *pPlugin)
1.397 @@ -891,6 +918,13 @@
1.398 bool CPluginManager::UnloadPlugin(IPlugin *plugin)
1.399 {
1.400 CPlugin *pPlugin = (CPlugin *)plugin;
1.401 +
1.402 + /* This prevents removal during insertion or anything else weird */
1.403 + if (m_plugins.find(pPlugin) == m_plugins.end())
1.404 + {
1.405 + return false;
1.406 + }
1.407 +
1.408 List<IPluginsListener *>::iterator iter;
1.409 IPluginsListener *pListener;
1.410
1.411 @@ -1215,6 +1249,12 @@
1.412
1.413 void CPluginManager::OnSourceModShutdown()
1.414 {
1.415 + List<CPlugin *>::iterator iter;
1.416 + while ( (iter = m_plugins.begin()) != m_plugins.end() )
1.417 + {
1.418 + UnloadPlugin((*iter));
1.419 + }
1.420 +
1.421 g_HandleSys.RemoveType(g_PluginType, m_MyIdent);
1.422 g_ShareSys.DestroyIdentType(g_PluginIdent);
1.423 g_ShareSys.DestroyIdentity(m_MyIdent);
1.424 @@ -1270,4 +1310,4 @@
1.425 iter->Release();
1.426
1.427 return pl;
1.428 -}
1.429 \ No newline at end of file
1.430 +}