added natives for iterating plugins and retrieving plugin info
authorDavid Anderson <dvander@alliedmods.net>
Thu Mar 15 20:10:25 2007 +0000 (2007-03-15)
changeset 618290353a9f2a7
parent 617 dc7671b320e4
child 619 6934bf39171e
added natives for iterating plugins and retrieving plugin info
core/smn_core.cpp
plugins/include/core.inc
plugins/include/sourcemod.inc
     1.1 --- a/core/smn_core.cpp	Thu Mar 15 04:45:33 2007 +0000
     1.2 +++ b/core/smn_core.cpp	Thu Mar 15 20:10:25 2007 +0000
     1.3 @@ -14,6 +14,36 @@
     1.4  #include <time.h>
     1.5  #include "sm_globals.h"
     1.6  #include "sourcemod.h"
     1.7 +#include "PluginSys.h"
     1.8 +#include "HandleSys.h"
     1.9 +
    1.10 +HandleType_t g_PlIter;
    1.11 +
    1.12 +
    1.13 +class CoreNativeHelpers : 
    1.14 +	public SMGlobalClass,
    1.15 +	public IHandleTypeDispatch
    1.16 +{
    1.17 +public:
    1.18 +	void OnSourceModAllInitialized()
    1.19 +	{
    1.20 +		HandleAccess hacc;
    1.21 +		g_HandleSys.InitAccessDefaults(NULL,  &hacc);
    1.22 +		hacc.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
    1.23 +
    1.24 +		g_PlIter = g_HandleSys.CreateType("PluginIterator", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    1.25 +	}
    1.26 +	void OnHandleDestroy(HandleType_t type, void *object)
    1.27 +	{
    1.28 +		IPluginIterator *iter = (IPluginIterator *)object;
    1.29 +		iter->Release();
    1.30 +	}
    1.31 +	void OnSourceModShutdown()
    1.32 +	{
    1.33 +		g_HandleSys.RemoveType(g_PlIter, g_pCoreIdent);
    1.34 +	}
    1.35 +} g_CoreNativeHelpers;
    1.36 +
    1.37  
    1.38  static cell_t ThrowError(IPluginContext *pContext, const cell_t *params)
    1.39  {
    1.40 @@ -40,9 +70,191 @@
    1.41  	return static_cast<cell_t>(t);
    1.42  }
    1.43  
    1.44 +static cell_t GetPluginIterator(IPluginContext *pContext, const cell_t *params)
    1.45 +{
    1.46 +	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
    1.47 +
    1.48 +	Handle_t hndl = g_HandleSys.CreateHandle(g_PlIter, iter, pContext->GetIdentity(), g_pCoreIdent, NULL);
    1.49 +
    1.50 +	if (hndl == BAD_HANDLE)
    1.51 +	{
    1.52 +		iter->Release();
    1.53 +	}
    1.54 +
    1.55 +	return hndl;
    1.56 +}
    1.57 +
    1.58 +static cell_t MorePlugins(IPluginContext *pContext, const cell_t *params)
    1.59 +{
    1.60 +	Handle_t hndl = (Handle_t)params[1];
    1.61 +	HandleError err;
    1.62 +	IPluginIterator *pIter;
    1.63 +
    1.64 +	HandleSecurity sec;
    1.65 +	sec.pIdentity = g_pCoreIdent;
    1.66 +	sec.pOwner = pContext->GetIdentity();
    1.67 +
    1.68 +	if ((err=g_HandleSys.ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
    1.69 +	{
    1.70 +		return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
    1.71 +	}
    1.72 +
    1.73 +	return pIter->MorePlugins() ? 1 : 0;
    1.74 +}
    1.75 +
    1.76 +static cell_t ReadPlugin(IPluginContext *pContext, const cell_t *params)
    1.77 +{
    1.78 +	Handle_t hndl = (Handle_t)params[1];
    1.79 +	HandleError err;
    1.80 +	IPluginIterator *pIter;
    1.81 +
    1.82 +	HandleSecurity sec;
    1.83 +	sec.pIdentity = g_pCoreIdent;
    1.84 +	sec.pOwner = pContext->GetIdentity();
    1.85 +
    1.86 +	if ((err=g_HandleSys.ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
    1.87 +	{
    1.88 +		return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
    1.89 +	}
    1.90 +
    1.91 +	CPlugin *pPlugin = (CPlugin *)pIter->GetPlugin();
    1.92 +	if (!pPlugin)
    1.93 +	{
    1.94 +		return BAD_HANDLE;
    1.95 +	}
    1.96 +
    1.97 +	pIter->NextPlugin();
    1.98 +
    1.99 +	return pPlugin->GetMyHandle();
   1.100 +}
   1.101 +
   1.102 +CPlugin *GetPluginFromHandle(IPluginContext *pContext, Handle_t hndl)
   1.103 +{
   1.104 +	if (hndl == BAD_HANDLE)
   1.105 +	{
   1.106 +		return g_PluginSys.GetPluginByCtx(pContext->GetContext());
   1.107 +	} else {
   1.108 +		HandleError err;
   1.109 +		CPlugin *pPlugin = (CPlugin *)g_PluginSys.PluginFromHandle(hndl, &err);
   1.110 +		if (!pPlugin)
   1.111 +		{
   1.112 +			pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
   1.113 +		}
   1.114 +		return pPlugin;
   1.115 +	}
   1.116 +}
   1.117 +
   1.118 +static cell_t GetPluginStatus(IPluginContext *pContext, const cell_t *params)
   1.119 +{
   1.120 +	CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
   1.121 +	if (!pPlugin)
   1.122 +	{
   1.123 +		return 0;
   1.124 +	}
   1.125 +
   1.126 +	return pPlugin->GetStatus();
   1.127 +}
   1.128 +
   1.129 +static cell_t GetPluginFilename(IPluginContext *pContext, const cell_t *params)
   1.130 +{
   1.131 +	CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
   1.132 +	if (!pPlugin)
   1.133 +	{
   1.134 +		return 0;
   1.135 +	}
   1.136 +
   1.137 +	pContext->StringToLocalUTF8(params[2], params[3], pPlugin->GetFilename(), NULL);
   1.138 +
   1.139 +	return 1;
   1.140 +}
   1.141 +
   1.142 +static cell_t IsPluginDebugging(IPluginContext *pContext, const cell_t *params)
   1.143 +{
   1.144 +	CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
   1.145 +	if (!pPlugin)
   1.146 +	{
   1.147 +		return 0;
   1.148 +	}
   1.149 +
   1.150 +	return pPlugin->IsDebugging() ? 1 : 0;
   1.151 +}
   1.152 +
   1.153 +/* Local to plugins only */
   1.154 +enum PluginInfo
   1.155 +{
   1.156 +	PlInfo_Name,			/**< Plugin name */
   1.157 +	PlInfo_Author,			/**< Plugin author */
   1.158 +	PlInfo_Description,		/**< Plugin description */
   1.159 +	PlInfo_Version,			/**< Plugin verison */
   1.160 +	PlInfo_URL,				/**< Plugin URL */
   1.161 +};
   1.162 +
   1.163 +static cell_t GetPluginInfo(IPluginContext *pContext, const cell_t *params)
   1.164 +{
   1.165 +	CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
   1.166 +	if (!pPlugin)
   1.167 +	{
   1.168 +		return 0;
   1.169 +	}
   1.170 +
   1.171 +	const sm_plugininfo_t *info = pPlugin->GetPublicInfo();
   1.172 +
   1.173 +	if (!info)
   1.174 +	{
   1.175 +		return 0;
   1.176 +	}
   1.177 +
   1.178 +	const char *str = NULL;
   1.179 +
   1.180 +	switch ((PluginInfo)params[2])
   1.181 +	{
   1.182 +	case PlInfo_Name:
   1.183 +		{
   1.184 +			str = info->name;
   1.185 +			break;
   1.186 +		}
   1.187 +	case PlInfo_Author:
   1.188 +		{
   1.189 +			str = info->author;
   1.190 +			break;
   1.191 +		}
   1.192 +	case PlInfo_Description:
   1.193 +		{
   1.194 +			str = info->description;
   1.195 +			break;
   1.196 +		}
   1.197 +	case PlInfo_Version:
   1.198 +		{
   1.199 +			str = info->version;
   1.200 +			break;
   1.201 +		}
   1.202 +	case PlInfo_URL:
   1.203 +		{
   1.204 +			str = info->url;
   1.205 +			break;
   1.206 +		}
   1.207 +	}
   1.208 +
   1.209 +	if (!str || str[0] == '\0')
   1.210 +	{
   1.211 +		return 0;
   1.212 +	}
   1.213 +
   1.214 +	pContext->StringToLocalUTF8(params[3], params[4], str, NULL);
   1.215 +
   1.216 +	return 1;
   1.217 +}
   1.218 +
   1.219  REGISTER_NATIVES(coreNatives)
   1.220  {
   1.221 +	{"GetPluginFilename",	GetPluginFilename},
   1.222 +	{"GetPluginInfo",		GetPluginInfo},
   1.223 +	{"GetPluginIterator",	GetPluginIterator},
   1.224 +	{"GetPluginStatus",		GetPluginStatus},
   1.225  	{"GetTime",				GetTime},
   1.226 +	{"IsPluginDebugging",	IsPluginDebugging},
   1.227 +	{"MorePlugins",			MorePlugins},
   1.228 +	{"ReadPlugin",			ReadPlugin},
   1.229  	{"ThrowError",			ThrowError},
   1.230  	{NULL,					NULL},
   1.231  };
     2.1 --- a/plugins/include/core.inc	Thu Mar 15 04:45:33 2007 +0000
     2.2 +++ b/plugins/include/core.inc	Thu Mar 15 20:10:25 2007 +0000
     2.3 @@ -43,6 +43,35 @@
     2.4  	filevers = SOURCEMOD_VERSION
     2.5  };
     2.6  
     2.7 +/**
     2.8 + * Plugin status values.
     2.9 + */
    2.10 +enum PluginStatus
    2.11 +{
    2.12 +	Plugin_Running=0,		/**< Plugin is running */
    2.13 +	/* All states below are "temporarily" unexecutable */
    2.14 +	Plugin_Paused,			/**< Plugin is loaded but paused */
    2.15 +	Plugin_Error,			/**< Plugin is loaded but errored/locked */
    2.16 +	/* All states below do not have all natives */
    2.17 +	Plugin_Loaded,			/**< Plugin has passed loading and can be finalized */
    2.18 +	Plugin_Failed,			/**< Plugin has a fatal failure */
    2.19 +	Plugin_Created,			/**< Plugin is created but not initialized */
    2.20 +	Plugin_Uncompiled,		/**< Plugin is not yet compiled by the JIT */
    2.21 +	Plugin_BadLoad,			/**< Plugin failed to load */
    2.22 +};
    2.23 +
    2.24 +/**
    2.25 + * Plugin information properties.
    2.26 + */
    2.27 +enum PluginInfo
    2.28 +{
    2.29 +	PlInfo_Name,			/**< Plugin name */
    2.30 +	PlInfo_Author,			/**< Plugin author */
    2.31 +	PlInfo_Description,		/**< Plugin description */
    2.32 +	PlInfo_Version,			/**< Plugin verison */
    2.33 +	PlInfo_URL,				/**< Plugin URL */
    2.34 +};
    2.35 +
    2.36  struct Extension
    2.37  {
    2.38  	const String:name[],	/* Short name */
     3.1 --- a/plugins/include/sourcemod.inc	Thu Mar 15 04:45:33 2007 +0000
     3.2 +++ b/plugins/include/sourcemod.inc	Thu Mar 15 20:10:25 2007 +0000
     3.3 @@ -108,6 +108,74 @@
     3.4   */
     3.5  native Handle:GetMyHandle();
     3.6  
     3.7 +/**
     3.8 + * Returns an iterator that can be used to search through plugins.
     3.9 + *
    3.10 + * @return				Handle to iterate with.  Must be closed via 
    3.11 + * 						CloseHandle().
    3.12 + * @error				Invalid Handle.
    3.13 + */
    3.14 +native Handle:GetPluginIterator();
    3.15 +
    3.16 +/**
    3.17 + * Returns whether there are more plugins available in the iterator.
    3.18 + *
    3.19 + * @param iter			Handle to the plugin iterator.
    3.20 + * @return				True on more plugins, false otherwise.
    3.21 + * @error				Invalid Handle.
    3.22 + */
    3.23 +native bool:MorePlugins(Handle:iter);
    3.24 +
    3.25 +/**
    3.26 + * Returns the current plugin in the iterator and advances the iterator.
    3.27 + *
    3.28 + * @param iter			Handle to the plugin iterator.
    3.29 + * @return				Current plugin the iterator is at, before
    3.30 + *						the iterator is advanced.
    3.31 + * @error				Invalid Handle.
    3.32 + */
    3.33 +native Handle:ReadPlugin(Handle:iter);
    3.34 +
    3.35 +/**
    3.36 + * Returns a plugin's status.
    3.37 + *
    3.38 + * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
    3.39 + * @return				Status code for the plugin.
    3.40 + * @error				Invalid Handle.
    3.41 + */
    3.42 +native PluginStatus:GetPluginStatus(Handle:plugin);
    3.43 +
    3.44 +/**
    3.45 + * Retrieves a plugin's file name relative to the plugins folder.
    3.46 + *
    3.47 + * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
    3.48 + * @param buffer		Buffer to the store the file name.
    3.49 + * @param maxlength		Maximum length of the name buffer.
    3.50 + * @noreturn
    3.51 + * @error				Invalid Handle.
    3.52 + */
    3.53 +native GetPluginFilename(Handle:plugin, String:buffer[], maxlength);
    3.54 +
    3.55 +/**
    3.56 + * Retrieves whether or not a plugin is being debugged.
    3.57 + *
    3.58 + * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
    3.59 + * @return				True if being debugged, false otherwise.
    3.60 + * @error				Invalid Handle.
    3.61 + */
    3.62 +native bool:IsPluginDebugging(Handle:hndl);
    3.63 +
    3.64 +/**
    3.65 + * Retrieves a plugin's public info.
    3.66 + *
    3.67 + * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
    3.68 + * @param info			Plugin info property to retrieve.
    3.69 + * @param buffer		Buffer to store info in.
    3.70 + * @param maxlength		Maximum length of buffer.
    3.71 + * @return				True on success, false if property is not available.
    3.72 + * @error				Invalid Handle.
    3.73 + */
    3.74 +native bool:GetPluginInfo(Handle:plugin, PluginInfo:info, String:buffer[], maxlength);
    3.75  
    3.76  /**
    3.77   * Aborts the current callback and throws an error.  This function