fixed an overall deficiency where libraries did not include extensions. extensions can now mark themselves as libraries, and they thus become fully optional just as plugins do. additionally, a bug was fixed where plugins could be accidentally marked as required instead of optional. these changes resulted in a bcompat API bump to the extension/sharesys interface.

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401578
This commit is contained in:
David Anderson 2007-10-15 04:06:57 +00:00
parent 8eaf03c441
commit c803eb8328
13 changed files with 147 additions and 35 deletions

View File

@ -340,6 +340,11 @@ bool CExtension::IsRunning(char *error, size_t maxlength)
return m_pAPI->QueryRunning(error, maxlength); return m_pAPI->QueryRunning(error, maxlength);
} }
void CExtension::AddLibrary(const char *library)
{
m_Libraries.push_back(library);
}
/********************* /*********************
* EXTENSION MANAGER * * EXTENSION MANAGER *
*********************/ *********************/
@ -628,7 +633,9 @@ void CExtensionManager::BindAllNativesToPlugin(IPlugin *pPlugin)
{ {
WeakNative wkn = WeakNative((CPlugin *)pPlugin, i); WeakNative wkn = WeakNative((CPlugin *)pPlugin, i);
x_native->owner->m_WeakNatives.push_back(wkn); x_native->owner->m_WeakNatives.push_back(wkn);
} else if (exts.find(x_native->owner) == exts.end()) { }
else if (exts.find(x_native->owner) == exts.end())
{
exts.push_back(x_native->owner); exts.push_back(x_native->owner);
} }
} }
@ -677,6 +684,14 @@ bool CExtensionManager::UnloadExtension(IExtension *_pExt)
p_iter = pExt->m_Plugins.erase(p_iter); p_iter = pExt->m_Plugins.erase(p_iter);
} }
List<String>::iterator s_iter;
for (s_iter = pExt->m_Libraries.begin();
s_iter != pExt->m_Libraries.end();
s_iter++)
{
g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
}
/* Unbound weak natives */ /* Unbound weak natives */
List<WeakNative>::iterator wkn_iter; List<WeakNative>::iterator wkn_iter;
for (wkn_iter=pExt->m_WeakNatives.begin(); wkn_iter!=pExt->m_WeakNatives.end(); wkn_iter++) for (wkn_iter=pExt->m_WeakNatives.begin(); wkn_iter!=pExt->m_WeakNatives.end(); wkn_iter++)
@ -718,7 +733,9 @@ bool CExtensionManager::UnloadExtension(IExtension *_pExt)
} }
pAPI->NotifyInterfaceDrop((*i_iter).iface); pAPI->NotifyInterfaceDrop((*i_iter).iface);
i_iter = pDep->m_Deps.erase(i_iter); i_iter = pDep->m_Deps.erase(i_iter);
} else { }
else
{
i_iter++; i_iter++;
} }
} }
@ -729,7 +746,9 @@ bool CExtensionManager::UnloadExtension(IExtension *_pExt)
if ((*i_iter).owner == pExt) if ((*i_iter).owner == pExt)
{ {
i_iter = pDep->m_ChildDeps.erase(i_iter); i_iter = pDep->m_ChildDeps.erase(i_iter);
} else { }
else
{
i_iter++; i_iter++;
} }
} }
@ -1092,3 +1111,10 @@ CExtensionManager::CExtensionManager()
CExtensionManager::~CExtensionManager() CExtensionManager::~CExtensionManager()
{ {
} }
void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
{
CExtension *pExt = (CExtension *)pSource;
pExt->AddLibrary(library);
g_PluginSys.OnLibraryAction(library, false, false);
}

View File

@ -78,6 +78,7 @@ public:
void AddPlugin(IPlugin *pPlugin); void AddPlugin(IPlugin *pPlugin);
void RemovePlugin(IPlugin *pPlugin); void RemovePlugin(IPlugin *pPlugin);
void MarkAllLoaded(); void MarkAllLoaded();
void AddLibrary(const char *library);
private: private:
bool Load(char *error, size_t maxlength); bool Load(char *error, size_t maxlength);
private: private:
@ -93,6 +94,7 @@ private:
List<IPlugin *> m_Plugins; List<IPlugin *> m_Plugins;
List<const sp_nativeinfo_t *> m_Natives; List<const sp_nativeinfo_t *> m_Natives;
List<WeakNative> m_WeakNatives; List<WeakNative> m_WeakNatives;
List<String> m_Libraries;
PluginId m_PlId; PluginId m_PlId;
unsigned int unload_code; unsigned int unload_code;
bool m_FullyLoaded; bool m_FullyLoaded;
@ -132,6 +134,7 @@ public:
void MarkAllLoaded(); void MarkAllLoaded();
void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload); void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload);
void TryAutoload(); void TryAutoload();
void AddLibrary(IExtension *pSource, const char *library);
public: public:
CExtension *GetExtensionFromIdent(IdentityToken_t *ptr); CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
void Shutdown(); void Shutdown();

View File

@ -466,7 +466,7 @@ void CPlugin::LibraryActions(bool dropping)
iter != m_Libraries.end(); iter != m_Libraries.end();
iter++) iter++)
{ {
g_PluginSys.OnLibraryAction((*iter).c_str(), dropping); g_PluginSys.OnLibraryAction((*iter).c_str(), true, dropping);
} }
} }
@ -988,7 +988,9 @@ LoadRes CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool de
{ {
/* Autoload any modules */ /* Autoload any modules */
LoadOrRequireExtensions(pPlugin, 1, error, maxlength); LoadOrRequireExtensions(pPlugin, 1, error, maxlength);
} else { }
else
{
pPlugin->SetErrorState(Plugin_Failed, "%s", error); pPlugin->SetErrorState(Plugin_Failed, "%s", error);
} }
} }
@ -1156,12 +1158,16 @@ bool CPluginManager::FindOrRequirePluginDeps(CPlugin *pPlugin, char *error, size
return false; return false;
} }
} }
} else { }
else
{
/* Check that we aren't registering the same library twice */ /* Check that we aren't registering the same library twice */
if (pPlugin->m_RequiredLibs.find(name) == pPlugin->m_RequiredLibs.end()) if (pPlugin->m_RequiredLibs.find(name) == pPlugin->m_RequiredLibs.end())
{ {
pPlugin->m_RequiredLibs.push_back(name); pPlugin->m_RequiredLibs.push_back(name);
} else { }
else
{
continue; continue;
} }
List<CPlugin *>::iterator iter; List<CPlugin *>::iterator iter;
@ -1237,7 +1243,9 @@ bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass
g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file); g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file);
g_Extensions.LoadAutoExtension(path); g_Extensions.LoadAutoExtension(path);
} }
} else if (pass == 2) { }
else if (pass == 2)
{
/* Is this required? */ /* Is this required? */
if (ext->required) if (ext->required)
{ {
@ -1256,10 +1264,32 @@ bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass
snprintf(error, maxlength, "Required extension \"%s\" file(\"%s\") not running", name, file); snprintf(error, maxlength, "Required extension \"%s\" file(\"%s\") not running", name, file);
} }
return false; return false;
} else { }
else
{
g_Extensions.BindChildPlugin(pExt, pPlugin); g_Extensions.BindChildPlugin(pExt, pPlugin);
} }
} }
else
{
IPluginFunction *pFunc;
char buffer[64];
UTIL_Format(buffer, sizeof(buffer), "__ext_%s_SetNTVOptional", &pubvar->name[6]);
if ((pFunc = pBase->GetFunctionByName(buffer)) != NULL)
{
cell_t res;
pFunc->Execute(&res);
if (pPlugin->GetContext()->n_err != SP_ERROR_NONE)
{
if (error)
{
UTIL_Format(error, maxlength, "Fatal error during plugin initialization (ext req)");
}
return false;
}
}
}
} }
} }
} }
@ -1277,13 +1307,14 @@ bool CPluginManager::RunSecondPass(CPlugin *pPlugin, char *error, size_t maxleng
/* Bind all extra natives */ /* Bind all extra natives */
g_Extensions.BindAllNativesToPlugin(pPlugin); g_Extensions.BindAllNativesToPlugin(pPlugin);
AddFakeNativesToPlugin(pPlugin);
if (!FindOrRequirePluginDeps(pPlugin, error, maxlength)) if (!FindOrRequirePluginDeps(pPlugin, error, maxlength))
{ {
return false; return false;
} }
AddFakeNativesToPlugin(pPlugin);
/* Find any unbound natives /* Find any unbound natives
* Right now, these are not allowed * Right now, these are not allowed
*/ */
@ -1345,7 +1376,7 @@ bool CPluginManager::RunSecondPass(CPlugin *pPlugin, char *error, size_t maxleng
s_iter != pPlugin->m_Libraries.end(); s_iter != pPlugin->m_Libraries.end();
s_iter++) s_iter++)
{ {
OnLibraryAction((*s_iter).c_str(), false); OnLibraryAction((*s_iter).c_str(), true, false);
} }
return true; return true;
@ -1505,7 +1536,7 @@ bool CPluginManager::UnloadPlugin(IPlugin *plugin)
s_iter != pPlugin->m_Libraries.end(); s_iter != pPlugin->m_Libraries.end();
s_iter++) s_iter++)
{ {
OnLibraryAction((*s_iter).c_str(), true); OnLibraryAction((*s_iter).c_str(), true, true);
} }
/* Go through all dependent plugins and tell them this plugin is now gone */ /* Go through all dependent plugins and tell them this plugin is now gone */
@ -2409,7 +2440,7 @@ CPlugin *CPluginManager::GetPluginFromIdentity(IdentityToken_t *pToken)
return (CPlugin *)(pToken->ptr); return (CPlugin *)(pToken->ptr);
} }
void CPluginManager::OnLibraryAction(const char *lib, bool drop) void CPluginManager::OnLibraryAction(const char *lib, bool is_a_plugin, bool drop)
{ {
List<CPlugin *>::iterator iter; List<CPlugin *>::iterator iter;
@ -2420,6 +2451,14 @@ void CPluginManager::OnLibraryAction(const char *lib, bool drop)
cell_t required; cell_t required;
} *plc; } *plc;
struct _ext
{
cell_t name;
cell_t file;
cell_t autoload;
cell_t required;
} *ext;
const char *name = drop ? "OnLibraryRemoved" : "OnLibraryAdded"; const char *name = drop ? "OnLibraryRemoved" : "OnLibraryAdded";
for (iter=m_plugins.begin(); for (iter=m_plugins.begin();
@ -2445,23 +2484,38 @@ void CPluginManager::OnLibraryAction(const char *lib, bool drop)
{ {
continue; continue;
} }
if (strncmp(pubvar->name, "__pl_", 5) != 0) if (is_a_plugin && strncmp(pubvar->name, "__pl_", 5) == 0)
{ {
continue; plc = (_pl *)pubvar->offs;
if (plc->required)
{
continue;
}
char *str;
pContext->LocalToString(plc->name, &str);
if (strcmp(str, lib) != 0)
{
continue;
}
pf->PushString(lib);
pf->Execute(NULL);
} }
plc = (_pl *)pubvar->offs; else if (!is_a_plugin && strncmp(pubvar->name, "__ext_", 6) == 0)
if (plc->required)
{ {
continue; ext = (_ext *)pubvar->offs;
if (ext->required)
{
continue;
}
char *str;
pContext->LocalToString(ext->name, &str);
if (strcmp(str, lib) != 0)
{
continue;
}
pf->PushString(lib);
pf->Execute(NULL);
} }
char *str;
pContext->LocalToString(plc->name, &str);
if (strcmp(str, lib) != 0)
{
continue;
}
pf->PushString(lib);
pf->Execute(NULL);
} }
} }
} }

View File

@ -431,7 +431,7 @@ public:
void Shutdown(); void Shutdown();
void OnLibraryAction(const char *lib, bool drop); void OnLibraryAction(const char *lib, bool is_a_plugin, bool drop);
bool LibraryExists(const char *lib); bool LibraryExists(const char *lib);

View File

@ -239,3 +239,7 @@ void ShareSystem::AddDependency(IExtension *myself, const char *filename, bool r
g_Extensions.AddDependency(myself, filename, require, autoload); g_Extensions.AddDependency(myself, filename, require, autoload);
} }
void ShareSystem::RegisterLibrary(IExtension *myself, const char *name)
{
g_Extensions.AddLibrary(myself, name);
}

View File

@ -80,6 +80,7 @@ public: //IShareSys
void DestroyIdentType(IdentityType_t type); void DestroyIdentType(IdentityType_t type);
void DestroyIdentity(IdentityToken_t *identity); void DestroyIdentity(IdentityToken_t *identity);
void AddDependency(IExtension *myself, const char *filename, bool require, bool autoload); void AddDependency(IExtension *myself, const char *filename, bool require, bool autoload);
void RegisterLibrary(IExtension *myself, const char *name);
public: //SMGlobalClass public: //SMGlobalClass
/* Pre-empt in case anything tries to register idents early */ /* Pre-empt in case anything tries to register idents early */
void OnSourceModStartup(bool late); void OnSourceModStartup(bool late);

View File

@ -52,6 +52,8 @@ bool TopMenuExtension::SDK_OnLoad(char *error, size_t maxlength, bool late)
Initialize_Natives(); Initialize_Natives();
sharesys->RegisterLibrary(myself, "TopMenus");
return true; return true;
} }

View File

@ -42,7 +42,7 @@
/* Basic information exposed publicly */ /* Basic information exposed publicly */
#define SMEXT_CONF_NAME "Top Menus" #define SMEXT_CONF_NAME "Top Menus"
#define SMEXT_CONF_DESCRIPTION "Creates sorted nested menus" #define SMEXT_CONF_DESCRIPTION "Creates sorted nested menus"
#define SMEXT_CONF_VERSION "1.0.0.0" #define SMEXT_CONF_VERSION SVN_FULL_VERSION
#define SMEXT_CONF_AUTHOR "AlliedModders" #define SMEXT_CONF_AUTHOR "AlliedModders"
#define SMEXT_CONF_URL "http://www.sourcemod.net/" #define SMEXT_CONF_URL "http://www.sourcemod.net/"
#define SMEXT_CONF_LOGTAG "MAINMENU" #define SMEXT_CONF_LOGTAG "MAINMENU"

View File

@ -126,9 +126,10 @@ public SharedPlugin:__pl_adminmenu =
#endif #endif
}; };
#if !defined REQUIRE_PLUGIN
public __pl_adminmenu_SetNTVOptional() public __pl_adminmenu_SetNTVOptional()
{ {
MarkNativeAsOptional("GetAdminTopMenu"); MarkNativeAsOptional("GetAdminTopMenu");
MarkNativeAsOptional("AddTargetsToMenu"); MarkNativeAsOptional("AddTargetsToMenu");
} }
#endif

View File

@ -467,16 +467,18 @@ native bool:LibraryExists(const String:name[]);
native GetExtensionFileStatus(const String:name[], String:error[]="", maxlength=0); native GetExtensionFileStatus(const String:name[], String:error[]="", maxlength=0);
/** /**
* Called after a library (plugin) is added that the * Called after a library is added that the current plugin references
* current plugin references optionally. * optionally. A library is either a plugin name or extension name, as
* exposed via its include file.
* *
* @param name Library name. * @param name Library name.
*/ */
forward OnLibraryAdded(const String:name[]); forward OnLibraryAdded(const String:name[]);
/** /**
* Called right before a library (plugin) is removed that the * Called right before a library is removed that the current plugin references
* current plugin references optionally. * optionally. A library is either a plugin name or extension name, as
* exposed via its include file.
* *
* @param name Library name. * @param name Library name.
*/ */

View File

@ -225,7 +225,7 @@ native TopMenuObject:FindTopMenuCategory(Handle:topmenu, const String:name[]);
/** /**
* Do not edit below this line! * Do not edit below this line!
*/ */
public Extension:__ext_cstrike = public Extension:__ext_topmenus =
{ {
name = "TopMenus", name = "TopMenus",
file = "topmenus.ext", file = "topmenus.ext",
@ -241,3 +241,14 @@ public Extension:__ext_cstrike =
#endif #endif
}; };
#if !defined REQUIRE_EXTENSIONS
public __ext_topmenus_SetNTVOptional()
{
MarkNativeAsOptional("CreateTopMenu");
MarkNativeAsOptional("LoadTopMenuConfig");
MarkNativeAsOptional("AddToTopMenu");
MarkNativeAsOptional("RemoveFromTopMenu");
MarkNativeAsOptional("DisplayTopMenu");
MarkNativeAsOptional("FindTopMenuCategory");
}
#endif

View File

@ -117,7 +117,7 @@ namespace SourceMod
/** /**
* @brief Version code of the IExtensionInterface API itself. * @brief Version code of the IExtensionInterface API itself.
*/ */
#define SMINTERFACE_EXTENSIONAPI_VERSION 1 #define SMINTERFACE_EXTENSIONAPI_VERSION 2
/** /**
* @brief The interface an extension must expose. * @brief The interface an extension must expose.

View File

@ -192,6 +192,14 @@ namespace SourceMod
* @param autoload Whether or not to autoload this extension. * @param autoload Whether or not to autoload this extension.
*/ */
virtual void AddDependency(IExtension *myself, const char *filename, bool require, bool autoload) =0; virtual void AddDependency(IExtension *myself, const char *filename, bool require, bool autoload) =0;
/**
* @brief Registers a library name to an extension.
*
* @param myself Extension to register library to.
* @param name Library name.
*/
virtual void RegisterLibrary(IExtension *myself, const char *name) =0;
}; };
} }