3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
35 #include "sm_stringutil.h"
36 #include "sm_globals.h"
37 #include "sourcemod.h"
38 #include "PluginSys.h"
39 #include "HandleSys.h"
40 #include "LibrarySys.h"
42 #if defined PLATFORM_WINDOWS
44 #elif defined PLATFORM_LINUX
47 #include <sys/times.h>
50 HandleType_t g_PlIter;
51 ConVar sm_datetime_format("sm_datetime_format", "%m/%d/%Y - %H:%M:%S", 0, "Default formatting time rules");
53 class CoreNativeHelpers :
55 public IHandleTypeDispatch
58 void OnSourceModAllInitialized()
61 g_HandleSys.InitAccessDefaults(NULL, &hacc);
62 hacc.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
64 g_PlIter = g_HandleSys.CreateType("PluginIterator", this, 0, NULL, NULL, g_pCoreIdent, NULL);
66 void OnHandleDestroy(HandleType_t type, void *object)
68 IPluginIterator *iter = (IPluginIterator *)object;
71 void OnSourceModShutdown()
73 g_HandleSys.RemoveType(g_PlIter, g_pCoreIdent);
75 } g_CoreNativeHelpers;
78 static cell_t ThrowError(IPluginContext *pContext, const cell_t *params)
82 g_SourceMod.SetGlobalTarget(LANG_SERVER);
84 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
86 if (pContext->GetContext()->n_err == SP_ERROR_NONE)
88 pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", buffer);
94 static cell_t GetTime(IPluginContext *pContext, const cell_t *params)
96 time_t t = time(NULL);
98 pContext->LocalToPhysAddr(params[1], &addr);
102 return static_cast<cell_t>(t);
105 #if defined SUBPLATFORM_SECURECRT
106 void _ignore_invalid_parameter(
107 const wchar_t * expression,
108 const wchar_t * function,
109 const wchar_t * file,
114 /* Wow we don't care, thanks Microsoft. */
118 static cell_t FormatTime(IPluginContext *pContext, const cell_t *params)
120 char *format, *buffer;
121 pContext->LocalToString(params[1], &buffer);
122 pContext->LocalToStringNULL(params[3], &format);
126 format = const_cast<char *>(sm_datetime_format.GetString());
129 #if defined SUBPLATFORM_SECURECRT
130 _invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
133 time_t t = (params[4] == -1) ? time(NULL) : (time_t)params[4];
134 size_t written = strftime(buffer, params[2], format, localtime(&t));
136 #if defined SUBPLATFORM_SECURECRT
137 _set_invalid_parameter_handler(handler);
140 if (params[2] && format[0] != '\0' && !written)
142 pContext->ThrowNativeError("Invalid time format or buffer too small");
149 static cell_t GetPluginIterator(IPluginContext *pContext, const cell_t *params)
151 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
153 Handle_t hndl = g_HandleSys.CreateHandle(g_PlIter, iter, pContext->GetIdentity(), g_pCoreIdent, NULL);
155 if (hndl == BAD_HANDLE)
163 static cell_t MorePlugins(IPluginContext *pContext, const cell_t *params)
165 Handle_t hndl = (Handle_t)params[1];
167 IPluginIterator *pIter;
170 sec.pIdentity = g_pCoreIdent;
171 sec.pOwner = pContext->GetIdentity();
173 if ((err=g_HandleSys.ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
175 return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
178 return pIter->MorePlugins() ? 1 : 0;
181 static cell_t ReadPlugin(IPluginContext *pContext, const cell_t *params)
183 Handle_t hndl = (Handle_t)params[1];
185 IPluginIterator *pIter;
188 sec.pIdentity = g_pCoreIdent;
189 sec.pOwner = pContext->GetIdentity();
191 if ((err=g_HandleSys.ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
193 return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
196 CPlugin *pPlugin = (CPlugin *)pIter->GetPlugin();
204 return pPlugin->GetMyHandle();
207 CPlugin *GetPluginFromHandle(IPluginContext *pContext, Handle_t hndl)
209 if (hndl == BAD_HANDLE)
211 return g_PluginSys.GetPluginByCtx(pContext->GetContext());
214 CPlugin *pPlugin = (CPlugin *)g_PluginSys.PluginFromHandle(hndl, &err);
217 pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
223 static cell_t GetPluginStatus(IPluginContext *pContext, const cell_t *params)
225 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
231 return pPlugin->GetStatus();
234 static cell_t GetPluginFilename(IPluginContext *pContext, const cell_t *params)
236 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
242 pContext->StringToLocalUTF8(params[2], params[3], pPlugin->GetFilename(), NULL);
247 static cell_t IsPluginDebugging(IPluginContext *pContext, const cell_t *params)
249 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
255 return pPlugin->IsDebugging() ? 1 : 0;
258 /* Local to plugins only */
261 PlInfo_Name, /**< Plugin name */
262 PlInfo_Author, /**< Plugin author */
263 PlInfo_Description, /**< Plugin description */
264 PlInfo_Version, /**< Plugin verison */
265 PlInfo_URL, /**< Plugin URL */
268 static cell_t GetPluginInfo(IPluginContext *pContext, const cell_t *params)
270 CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
276 const sm_plugininfo_t *info = pPlugin->GetPublicInfo();
283 const char *str = NULL;
285 switch ((PluginInfo)params[2])
297 case PlInfo_Description:
299 str = info->description;
314 if (!str || str[0] == '\0')
319 pContext->StringToLocalUTF8(params[3], params[4], str, NULL);
324 static cell_t SetFailState(IPluginContext *pContext, const cell_t *params)
327 pContext->LocalToString(params[1], &str);
329 CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
330 pPlugin->SetErrorState(Plugin_Error, "%s", str);
332 return pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", str);
335 static cell_t GetSysTickCount(IPluginContext *pContext, const cell_t *params)
337 #if defined PLATFORM_WINDOWS
338 return (cell_t)GetTickCount();
339 #elif defined PLATFORM_LINUX
341 clock_t ticks = times(&tm);
342 long ticks_per_sec = sysconf(_SC_CLK_TCK);
343 double fticks = (double)ticks / (double)ticks_per_sec;
345 if (fticks > INT_MAX)
347 double r = (int)(fticks / INT_MAX) * (double)INT_MAX;
350 return (cell_t)fticks;
354 static cell_t AutoExecConfig(IPluginContext *pContext, const cell_t *params)
356 CPlugin *plugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
359 pContext->LocalToString(params[2], &cfg);
360 pContext->LocalToString(params[3], &folder);
364 static char temp_str[255];
365 static char temp_file[PLATFORM_MAX_PATH];
368 g_LibSys.GetFileFromPath(temp_str, sizeof(temp_str), plugin->GetFilename());
369 if ((ptr = strstr(temp_str, ".smx")) != NULL)
374 /* We have the raw filename! */
375 UTIL_Format(temp_file, sizeof(temp_file), "plugin.%s", temp_str);
379 plugin->AddConfig(params[1] ? true : false, cfg, folder);
384 static cell_t MarkNativeAsOptional(IPluginContext *pContext, const cell_t *params)
388 sp_context_t *ctx = pContext->GetContext();
390 pContext->LocalToString(params[1], &name);
391 if (pContext->FindNativeByName(name, &idx) != SP_ERROR_NONE)
393 /* Oops! This HAS to silently fail! */
397 ctx->natives[idx].flags |= SP_NTVFLAG_OPTIONAL;
402 static cell_t RegPluginLibrary(IPluginContext *pContext, const cell_t *params)
405 CPlugin *pl = g_PluginSys.GetPluginByCtx(pContext->GetContext());
407 pContext->LocalToString(params[1], &name);
409 pl->AddLibrary(name);
414 static cell_t LibraryExists(IPluginContext *pContext, const cell_t *params)
417 pContext->LocalToString(params[1], &str);
419 return g_PluginSys.LibraryExists(str) ? 1 : 0;
422 REGISTER_NATIVES(coreNatives)
424 {"AutoExecConfig", AutoExecConfig},
425 {"GetPluginFilename", GetPluginFilename},
426 {"GetPluginInfo", GetPluginInfo},
427 {"GetPluginIterator", GetPluginIterator},
428 {"GetPluginStatus", GetPluginStatus},
429 {"GetSysTickCount", GetSysTickCount},
430 {"GetTime", GetTime},
431 {"IsPluginDebugging", IsPluginDebugging},
432 {"MorePlugins", MorePlugins},
433 {"ReadPlugin", ReadPlugin},
434 {"ThrowError", ThrowError},
435 {"SetFailState", SetFailState},
436 {"FormatTime", FormatTime},
437 {"MarkNativeAsOptional", MarkNativeAsOptional},
438 {"RegPluginLibrary", RegPluginLibrary},
439 {"LibraryExists", LibraryExists},