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
17 #include "sm_stringutil.h"
18 #include "sm_globals.h"
19 #include "sourcemod.h"
20 #include "PluginSys.h"
21 #include "HandleSys.h"
22 #include "LibrarySys.h"
24 #if defined PLATFORM_WINDOWS
26 #elif defined PLATFORM_LINUX
29 #include <sys/times.h>
32 HandleType_t g_PlIter;
34 class CoreNativeHelpers :
36 public IHandleTypeDispatch
39 void OnSourceModAllInitialized()
42 g_HandleSys.InitAccessDefaults(NULL, &hacc);
43 hacc.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
45 g_PlIter = g_HandleSys.CreateType("PluginIterator", this, 0, NULL, NULL, g_pCoreIdent, NULL);
47 void OnHandleDestroy(HandleType_t type, void *object)
49 IPluginIterator *iter = (IPluginIterator *)object;
52 void OnSourceModShutdown()
54 g_HandleSys.RemoveType(g_PlIter, g_pCoreIdent);
56 } g_CoreNativeHelpers;
59 static cell_t ThrowError(IPluginContext *pContext, const cell_t *params)
63 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
65 if (pContext->GetContext()->n_err == SP_ERROR_NONE)
67 pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", buffer);
73 static cell_t GetTime(IPluginContext *pContext, const cell_t *params)
75 time_t t = time(NULL);
77 pContext->LocalToPhysAddr(params[1], &addr);
81 return static_cast<cell_t>(t);
84 static cell_t GetPluginIterator(IPluginContext *pContext, const cell_t *params)
86 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
88 Handle_t hndl = g_HandleSys.CreateHandle(g_PlIter, iter, pContext->GetIdentity(), g_pCoreIdent, NULL);
90 if (hndl == BAD_HANDLE)
98 static cell_t MorePlugins(IPluginContext *pContext, const cell_t *params)
100 Handle_t hndl = (Handle_t)params[1];
102 IPluginIterator *pIter;
105 sec.pIdentity = g_pCoreIdent;
106 sec.pOwner = pContext->GetIdentity();
108 if ((err=g_HandleSys.ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
110 return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
113 return pIter->MorePlugins() ? 1 : 0;
116 static cell_t ReadPlugin(IPluginContext *pContext, const cell_t *params)
118 Handle_t hndl = (Handle_t)params[1];
120 IPluginIterator *pIter;
123 sec.pIdentity = g_pCoreIdent;
124 sec.pOwner = pContext->GetIdentity();
126 if ((err=g_HandleSys.ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
128 return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
131 CPlugin *pPlugin = (CPlugin *)pIter->GetPlugin();
139 return pPlugin->GetMyHandle();
142 CPlugin *GetPluginFromHandle(IPluginContext *pContext, Handle_t hndl)
144 if (hndl == BAD_HANDLE)
146 return g_PluginSys.GetPluginByCtx(pContext->GetContext());
149 CPlugin *pPlugin = (CPlugin *)g_PluginSys.PluginFromHandle(hndl, &err);
152 pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
158 static cell_t GetPluginStatus(IPluginContext *pContext, const cell_t *params)
160 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
166 return pPlugin->GetStatus();
169 static cell_t GetPluginFilename(IPluginContext *pContext, const cell_t *params)
171 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
177 pContext->StringToLocalUTF8(params[2], params[3], pPlugin->GetFilename(), NULL);
182 static cell_t IsPluginDebugging(IPluginContext *pContext, const cell_t *params)
184 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
190 return pPlugin->IsDebugging() ? 1 : 0;
193 /* Local to plugins only */
196 PlInfo_Name, /**< Plugin name */
197 PlInfo_Author, /**< Plugin author */
198 PlInfo_Description, /**< Plugin description */
199 PlInfo_Version, /**< Plugin verison */
200 PlInfo_URL, /**< Plugin URL */
203 static cell_t GetPluginInfo(IPluginContext *pContext, const cell_t *params)
205 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
211 const sm_plugininfo_t *info = pPlugin->GetPublicInfo();
218 const char *str = NULL;
220 switch ((PluginInfo)params[2])
232 case PlInfo_Description:
234 str = info->description;
249 if (!str || str[0] == '\0')
254 pContext->StringToLocalUTF8(params[3], params[4], str, NULL);
259 static cell_t SetFailState(IPluginContext *pContext, const cell_t *params)
262 pContext->LocalToString(params[1], &str);
264 CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
265 pPlugin->SetErrorState(Plugin_Error, "%s", str);
267 return pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", str);
270 static cell_t GetSysTickCount(IPluginContext *pContext, const cell_t *params)
272 #if defined PLATFORM_WINDOWS
273 return (cell_t)GetTickCount();
274 #elif defined PLATFORM_LINUX
276 clock_t ticks = times(&tm);
277 long ticks_per_sec = sysconf(_SC_CLK_TCK);
278 double fticks = (double)ticks / (double)ticks_per_sec;
280 if (fticks > INT_MAX)
282 double r = (int)(fticks / INT_MAX) * (double)INT_MAX;
285 return (cell_t)fticks;
289 static cell_t AutoExecConfig(IPluginContext *pContext, const cell_t *params)
291 CPlugin *plugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
294 pContext->LocalToString(params[2], &cfg);
295 pContext->LocalToString(params[3], &folder);
299 static char temp_str[255];
300 static char temp_file[PLATFORM_MAX_PATH];
303 g_LibSys.GetFileFromPath(temp_str, sizeof(temp_str), plugin->GetFilename());
304 if ((ptr = strstr(temp_str, ".smx")) != NULL)
309 /* We have the raw filename! */
310 UTIL_Format(temp_file, sizeof(temp_file), "plugin.%s", temp_str);
314 plugin->AddConfig(params[1] ? true : false, cfg, folder);
319 REGISTER_NATIVES(coreNatives)
321 {"AutoExecConfig", AutoExecConfig},
322 {"GetPluginFilename", GetPluginFilename},
323 {"GetPluginInfo", GetPluginInfo},
324 {"GetPluginIterator", GetPluginIterator},
325 {"GetPluginStatus", GetPluginStatus},
326 {"GetSysTickCount", GetSysTickCount},
327 {"GetTime", GetTime},
328 {"IsPluginDebugging", IsPluginDebugging},
329 {"MorePlugins", MorePlugins},
330 {"ReadPlugin", ReadPlugin},
331 {"ThrowError", ThrowError},
332 {"SetFailState", SetFailState},