core/smn_core.cpp
changeset 618 290353a9f2a7
parent 595 1d8e56a119f2
child 653 ef60870067ff
     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  };