3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
16 #include "sm_globals.h"
17 #include "sourcemod.h"
18 #include "PluginSys.h"
19 #include "HandleSys.h"
21 HandleType_t g_PlIter;
24 class CoreNativeHelpers :
26 public IHandleTypeDispatch
29 void OnSourceModAllInitialized()
32 g_HandleSys.InitAccessDefaults(NULL, &hacc);
33 hacc.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
35 g_PlIter = g_HandleSys.CreateType("PluginIterator", this, 0, NULL, NULL, g_pCoreIdent, NULL);
37 void OnHandleDestroy(HandleType_t type, void *object)
39 IPluginIterator *iter = (IPluginIterator *)object;
42 void OnSourceModShutdown()
44 g_HandleSys.RemoveType(g_PlIter, g_pCoreIdent);
46 } g_CoreNativeHelpers;
49 static cell_t ThrowError(IPluginContext *pContext, const cell_t *params)
53 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
55 if (pContext->GetContext()->n_err == SP_ERROR_NONE)
57 pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", buffer);
63 static cell_t GetTime(IPluginContext *pContext, const cell_t *params)
65 time_t t = time(NULL);
67 pContext->LocalToPhysAddr(params[1], &addr);
71 return static_cast<cell_t>(t);
74 static cell_t GetPluginIterator(IPluginContext *pContext, const cell_t *params)
76 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
78 Handle_t hndl = g_HandleSys.CreateHandle(g_PlIter, iter, pContext->GetIdentity(), g_pCoreIdent, NULL);
80 if (hndl == BAD_HANDLE)
88 static cell_t MorePlugins(IPluginContext *pContext, const cell_t *params)
90 Handle_t hndl = (Handle_t)params[1];
92 IPluginIterator *pIter;
95 sec.pIdentity = g_pCoreIdent;
96 sec.pOwner = pContext->GetIdentity();
98 if ((err=g_HandleSys.ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
100 return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
103 return pIter->MorePlugins() ? 1 : 0;
106 static cell_t ReadPlugin(IPluginContext *pContext, const cell_t *params)
108 Handle_t hndl = (Handle_t)params[1];
110 IPluginIterator *pIter;
113 sec.pIdentity = g_pCoreIdent;
114 sec.pOwner = pContext->GetIdentity();
116 if ((err=g_HandleSys.ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
118 return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
121 CPlugin *pPlugin = (CPlugin *)pIter->GetPlugin();
129 return pPlugin->GetMyHandle();
132 CPlugin *GetPluginFromHandle(IPluginContext *pContext, Handle_t hndl)
134 if (hndl == BAD_HANDLE)
136 return g_PluginSys.GetPluginByCtx(pContext->GetContext());
139 CPlugin *pPlugin = (CPlugin *)g_PluginSys.PluginFromHandle(hndl, &err);
142 pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
148 static cell_t GetPluginStatus(IPluginContext *pContext, const cell_t *params)
150 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
156 return pPlugin->GetStatus();
159 static cell_t GetPluginFilename(IPluginContext *pContext, const cell_t *params)
161 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
167 pContext->StringToLocalUTF8(params[2], params[3], pPlugin->GetFilename(), NULL);
172 static cell_t IsPluginDebugging(IPluginContext *pContext, const cell_t *params)
174 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
180 return pPlugin->IsDebugging() ? 1 : 0;
183 /* Local to plugins only */
186 PlInfo_Name, /**< Plugin name */
187 PlInfo_Author, /**< Plugin author */
188 PlInfo_Description, /**< Plugin description */
189 PlInfo_Version, /**< Plugin verison */
190 PlInfo_URL, /**< Plugin URL */
193 static cell_t GetPluginInfo(IPluginContext *pContext, const cell_t *params)
195 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
201 const sm_plugininfo_t *info = pPlugin->GetPublicInfo();
208 const char *str = NULL;
210 switch ((PluginInfo)params[2])
222 case PlInfo_Description:
224 str = info->description;
239 if (!str || str[0] == '\0')
244 pContext->StringToLocalUTF8(params[3], params[4], str, NULL);
249 static cell_t SetFailState(IPluginContext *pContext, const cell_t *params)
252 pContext->LocalToString(params[1], &str);
254 CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
255 pPlugin->SetErrorState(Plugin_Error, "%s", str);
257 return pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", str);
260 REGISTER_NATIVES(coreNatives)
262 {"GetPluginFilename", GetPluginFilename},
263 {"GetPluginInfo", GetPluginInfo},
264 {"GetPluginIterator", GetPluginIterator},
265 {"GetPluginStatus", GetPluginStatus},
266 {"GetTime", GetTime},
267 {"IsPluginDebugging", IsPluginDebugging},
268 {"MorePlugins", MorePlugins},
269 {"ReadPlugin", ReadPlugin},
270 {"ThrowError", ThrowError},
271 {"SetFailState", SetFailState},