core/smn_core.cpp
author David Anderson <dvander@alliedmods.net>
Fri Aug 30 10:16:28 2013 -0700 (2013-08-30)
changeset 4109 10eb68d668af
parent 4073 56283c919b7b
child 4139 de7d1cd00703
permissions -rw-r--r--
Use Refcounted to manage to NativeEntry (bug 5852 part 4, r=ds).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <time.h>
    33 #include <string.h>
    34 #include <stdlib.h>
    35 #include "sm_stringutil.h"
    36 #include "sm_globals.h"
    37 #include "sourcemod.h"
    38 #include "LibrarySys.h"
    39 #include "TimerSys.h"
    40 #include "ForwardSys.h"
    41 #include "Logger.h"
    42 #include <sh_memory.h>
    43 #include "logic_bridge.h"
    44 
    45 #if defined PLATFORM_WINDOWS
    46 #include <windows.h>
    47 #elif defined PLATFORM_POSIX
    48 #include <limits.h>
    49 #include <unistd.h>
    50 #include <sys/times.h>
    51 #endif
    52 
    53 HandleType_t g_PlIter;
    54 ConVar sm_datetime_format("sm_datetime_format", "%m/%d/%Y - %H:%M:%S", 0, "Default formatting time rules");
    55 IForward *g_OnLogAction = NULL;
    56 
    57 class CoreNativeHelpers : 
    58 	public SMGlobalClass,
    59 	public IHandleTypeDispatch
    60 {
    61 public:
    62 	void OnSourceModAllInitialized()
    63 	{
    64 		HandleAccess hacc;
    65 		handlesys->InitAccessDefaults(NULL,  &hacc);
    66 		hacc.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
    67 
    68 		g_PlIter = handlesys->CreateType("PluginIterator", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    69 
    70 		g_OnLogAction = g_Forwards.CreateForward("OnLogAction", 
    71 			ET_Hook, 
    72 			5, 
    73 			NULL,
    74 			Param_Cell,
    75 			Param_Cell,
    76 			Param_Cell,
    77 			Param_Cell,
    78 			Param_String);
    79 	}
    80 	void OnHandleDestroy(HandleType_t type, void *object)
    81 	{
    82 		IPluginIterator *iter = (IPluginIterator *)object;
    83 		iter->Release();
    84 	}
    85 	void OnSourceModShutdown()
    86 	{
    87 		g_Forwards.ReleaseForward(g_OnLogAction);
    88 		handlesys->RemoveType(g_PlIter, g_pCoreIdent);
    89 	}
    90 } g_CoreNativeHelpers;
    91 
    92 void LogAction(Handle_t hndl, int type, int client, int target, const char *message)
    93 {
    94 	if (g_OnLogAction->GetFunctionCount())
    95 	{
    96 		cell_t result = 0;
    97 		g_OnLogAction->PushCell(hndl);
    98 		g_OnLogAction->PushCell(type);
    99 		g_OnLogAction->PushCell(client);
   100 		g_OnLogAction->PushCell(target);
   101 		g_OnLogAction->PushString(message);
   102 		g_OnLogAction->Execute(&result);
   103 
   104 		if (result >= (ResultType)Pl_Handled)
   105 		{
   106 			return;
   107 		}
   108 	}
   109 
   110 	const char *logtag = "SM";
   111 	if (type == 2)
   112 	{
   113 		HandleError err;
   114 		IPlugin *pPlugin = scripts->FindPluginByHandle(hndl, &err);
   115 		if (pPlugin)
   116 		{
   117 			logtag = pPlugin->GetFilename();
   118 		}
   119 	}
   120 
   121 	g_Logger.LogMessage("[%s] %s", logtag, message);
   122 }
   123 
   124 static cell_t ThrowError(IPluginContext *pContext, const cell_t *params)
   125 {
   126 	char buffer[512];
   127 
   128 	g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
   129 
   130 	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
   131 
   132 	if (pContext->GetLastNativeError() == SP_ERROR_NONE)
   133 	{
   134 		pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", buffer);
   135 	}
   136 
   137 	return 0;
   138 }
   139 
   140 static cell_t GetTime(IPluginContext *pContext, const cell_t *params)
   141 {
   142 	time_t t = GetAdjustedTime();
   143 	cell_t *addr;
   144 	pContext->LocalToPhysAddr(params[1], &addr);
   145 
   146 	*(time_t *)addr = t;
   147 
   148 	return static_cast<cell_t>(t);
   149 }
   150 
   151 #if defined SUBPLATFORM_SECURECRT
   152 void _ignore_invalid_parameter(
   153 						const wchar_t * expression,
   154 						const wchar_t * function, 
   155 						const wchar_t * file, 
   156 						unsigned int line,
   157 						uintptr_t pReserved
   158 						)
   159 {
   160 	/* Wow we don't care, thanks Microsoft. */
   161 }
   162 #endif
   163 
   164 static cell_t FormatTime(IPluginContext *pContext, const cell_t *params)
   165 {
   166 	char *format, *buffer;
   167 	pContext->LocalToString(params[1], &buffer);
   168 	pContext->LocalToStringNULL(params[3], &format);
   169 
   170 	if (format == NULL)
   171 	{
   172 		format = const_cast<char *>(sm_datetime_format.GetString());
   173 	}
   174 
   175 #if defined SUBPLATFORM_SECURECRT
   176 	_invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
   177 #endif
   178 
   179 	time_t t = (params[4] == -1) ? GetAdjustedTime() : (time_t)params[4];
   180 	size_t written = strftime(buffer, params[2], format, localtime(&t));
   181 
   182 #if defined SUBPLATFORM_SECURECRT
   183 	_set_invalid_parameter_handler(handler);
   184 #endif
   185 
   186 	if (params[2] && format[0] != '\0' && !written)
   187 	{
   188 		pContext->ThrowNativeError("Invalid time format or buffer too small");
   189 		return 0;
   190 	}
   191 
   192 	return 1;
   193 }
   194 
   195 static cell_t GetPluginIterator(IPluginContext *pContext, const cell_t *params)
   196 {
   197 	IPluginIterator *iter = scripts->GetPluginIterator();
   198 
   199 	Handle_t hndl = handlesys->CreateHandle(g_PlIter, iter, pContext->GetIdentity(), g_pCoreIdent, NULL);
   200 
   201 	if (hndl == BAD_HANDLE)
   202 	{
   203 		iter->Release();
   204 	}
   205 
   206 	return hndl;
   207 }
   208 
   209 static cell_t MorePlugins(IPluginContext *pContext, const cell_t *params)
   210 {
   211 	Handle_t hndl = (Handle_t)params[1];
   212 	HandleError err;
   213 	IPluginIterator *pIter;
   214 
   215 	HandleSecurity sec;
   216 	sec.pIdentity = g_pCoreIdent;
   217 	sec.pOwner = pContext->GetIdentity();
   218 
   219 	if ((err=handlesys->ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
   220 	{
   221 		return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
   222 	}
   223 
   224 	return pIter->MorePlugins() ? 1 : 0;
   225 }
   226 
   227 static cell_t ReadPlugin(IPluginContext *pContext, const cell_t *params)
   228 {
   229 	Handle_t hndl = (Handle_t)params[1];
   230 	HandleError err;
   231 	IPluginIterator *pIter;
   232 
   233 	HandleSecurity sec;
   234 	sec.pIdentity = g_pCoreIdent;
   235 	sec.pOwner = pContext->GetIdentity();
   236 
   237 	if ((err=handlesys->ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
   238 	{
   239 		return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
   240 	}
   241 
   242 	IPlugin *pPlugin = pIter->GetPlugin();
   243 	if (!pPlugin)
   244 	{
   245 		return BAD_HANDLE;
   246 	}
   247 
   248 	pIter->NextPlugin();
   249 
   250 	return pPlugin->GetMyHandle();
   251 }
   252 
   253 IPlugin *GetPluginFromHandle(IPluginContext *pContext, Handle_t hndl)
   254 {
   255 	if (hndl == BAD_HANDLE)
   256 	{
   257 		return scripts->FindPluginByContext(pContext->GetContext());
   258 	} else {
   259 		HandleError err;
   260 		IPlugin *pPlugin = scripts->FindPluginByHandle(hndl, &err);
   261 		if (!pPlugin)
   262 		{
   263 			pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
   264 		}
   265 		return pPlugin;
   266 	}
   267 }
   268 
   269 static cell_t GetPluginStatus(IPluginContext *pContext, const cell_t *params)
   270 {
   271 	IPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
   272 	if (!pPlugin)
   273 	{
   274 		return 0;
   275 	}
   276 
   277 	return pPlugin->GetStatus();
   278 }
   279 
   280 static cell_t GetPluginFilename(IPluginContext *pContext, const cell_t *params)
   281 {
   282 	IPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
   283 	if (!pPlugin)
   284 	{
   285 		return 0;
   286 	}
   287 
   288 	pContext->StringToLocalUTF8(params[2], params[3], pPlugin->GetFilename(), NULL);
   289 
   290 	return 1;
   291 }
   292 
   293 static cell_t IsPluginDebugging(IPluginContext *pContext, const cell_t *params)
   294 {
   295 	IPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
   296 	if (!pPlugin)
   297 	{
   298 		return 0;
   299 	}
   300 
   301 	return pPlugin->IsDebugging() ? 1 : 0;
   302 }
   303 
   304 /* Local to plugins only */
   305 enum PluginInfo
   306 {
   307 	PlInfo_Name,			/**< Plugin name */
   308 	PlInfo_Author,			/**< Plugin author */
   309 	PlInfo_Description,		/**< Plugin description */
   310 	PlInfo_Version,			/**< Plugin verison */
   311 	PlInfo_URL,				/**< Plugin URL */
   312 };
   313 
   314 static cell_t GetPluginInfo(IPluginContext *pContext, const cell_t *params)
   315 {
   316 	IPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
   317 	if (!pPlugin)
   318 	{
   319 		return 0;
   320 	}
   321 
   322 	const sm_plugininfo_t *info = pPlugin->GetPublicInfo();
   323 
   324 	if (!info)
   325 	{
   326 		return 0;
   327 	}
   328 
   329 	const char *str = NULL;
   330 
   331 	switch ((PluginInfo)params[2])
   332 	{
   333 	case PlInfo_Name:
   334 		{
   335 			str = info->name;
   336 			break;
   337 		}
   338 	case PlInfo_Author:
   339 		{
   340 			str = info->author;
   341 			break;
   342 		}
   343 	case PlInfo_Description:
   344 		{
   345 			str = info->description;
   346 			break;
   347 		}
   348 	case PlInfo_Version:
   349 		{
   350 			str = info->version;
   351 			break;
   352 		}
   353 	case PlInfo_URL:
   354 		{
   355 			str = info->url;
   356 			break;
   357 		}
   358 	}
   359 
   360 	if (!str || str[0] == '\0')
   361 	{
   362 		return 0;
   363 	}
   364 
   365 	pContext->StringToLocalUTF8(params[3], params[4], str, NULL);
   366 
   367 	return 1;
   368 }
   369 
   370 static cell_t SetFailState(IPluginContext *pContext, const cell_t *params)
   371 {
   372 	char *str;
   373 	SMPlugin *pPlugin;
   374 
   375 	pContext->LocalToString(params[1], &str);
   376 	pPlugin = scripts->FindPluginByContext(pContext->GetContext());
   377 
   378 	if (params[0] == 1)
   379 	{
   380 		pPlugin->SetErrorState(Plugin_Error, "%s", str);
   381 
   382 		return pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", str);
   383 	}
   384 	else
   385 	{
   386 		char buffer[2048];
   387 
   388 		g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
   389 		if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   390 		{
   391 			pPlugin->SetErrorState(Plugin_Error, "%s", str);
   392 			return pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "Formatting error (%s)", str);
   393 		}
   394 		else
   395 		{
   396 			pPlugin->SetErrorState(Plugin_Error, "%s", buffer);
   397 			return pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", buffer);
   398 		}
   399 	}
   400 
   401 	return 0;
   402 }
   403 
   404 static cell_t GetSysTickCount(IPluginContext *pContext, const cell_t *params)
   405 {
   406 #if defined PLATFORM_WINDOWS
   407 	return (cell_t)GetTickCount();
   408 #elif defined PLATFORM_POSIX
   409 	tms tm;
   410 	clock_t ticks = times(&tm);
   411 	long ticks_per_sec = sysconf(_SC_CLK_TCK);
   412 	double fticks = (double)ticks / (double)ticks_per_sec;
   413 	fticks *= 1000.0f;
   414 	if (fticks > INT_MAX)
   415 	{
   416 		double r = (int)(fticks / INT_MAX) * (double)INT_MAX;
   417 		fticks -= r;
   418 	}
   419 	return (cell_t)fticks;
   420 #endif
   421 }
   422 
   423 static cell_t AutoExecConfig(IPluginContext *pContext, const cell_t *params)
   424 {
   425 	SMPlugin *plugin = scripts->FindPluginByContext(pContext->GetContext());
   426 
   427 	char *cfg, *folder;
   428 	pContext->LocalToString(params[2], &cfg);
   429 	pContext->LocalToString(params[3], &folder);
   430 
   431 	if (cfg[0] == '\0')
   432 	{
   433 		static char temp_str[255];
   434 		static char temp_file[PLATFORM_MAX_PATH];
   435 		char *ptr;
   436 
   437 		g_LibSys.GetFileFromPath(temp_str, sizeof(temp_str), plugin->GetFilename());
   438 		if ((ptr = strstr(temp_str, ".smx")) != NULL)
   439 		{
   440 			*ptr = '\0';
   441 		}
   442 
   443 		/* We have the raw filename! */
   444 		UTIL_Format(temp_file, sizeof(temp_file), "plugin.%s", temp_str);
   445 		cfg = temp_file;
   446 	}
   447 
   448 	plugin->AddConfig(params[1] ? true : false, cfg, folder);
   449 
   450 	return 1;
   451 }
   452 
   453 static cell_t MarkNativeAsOptional(IPluginContext *pContext, const cell_t *params)
   454 {
   455 	char *name;
   456 	uint32_t idx;
   457 	sp_native_t *native;
   458 
   459 	pContext->LocalToString(params[1], &name);
   460 	if (pContext->FindNativeByName(name, &idx) != SP_ERROR_NONE)
   461 	{
   462 		/* Oops! This HAS to silently fail! */
   463 		return 0;
   464 	}
   465 
   466 	pContext->GetNativeByIndex(idx, &native);
   467 
   468 	native->flags |= SP_NTVFLAG_OPTIONAL;
   469 
   470 	return 1;
   471 }
   472 
   473 static cell_t RegPluginLibrary(IPluginContext *pContext, const cell_t *params)
   474 {
   475 	char *name;
   476 	SMPlugin *pl = scripts->FindPluginByContext(pContext->GetContext());
   477 
   478 	pContext->LocalToString(params[1], &name);
   479 
   480 	pl->AddLibrary(name);
   481 	return 1;
   482 }
   483 
   484 static cell_t LibraryExists(IPluginContext *pContext, const cell_t *params)
   485 {
   486 	char *str;
   487 	pContext->LocalToString(params[1], &str);
   488 
   489 	if (strcmp(str, "__CanTestFeatures__") == 0)
   490 	{
   491 		return 1;
   492 	}
   493 
   494 	if (scripts->LibraryExists(str))
   495 	{
   496 		return 1;
   497 	}
   498 
   499 	if (extsys->LibraryExists(str))
   500 	{
   501 		return 1;
   502 	}
   503 
   504 	return 0;
   505 }
   506 
   507 static cell_t sm_LogAction(IPluginContext *pContext, const cell_t *params)
   508 {
   509 	char buffer[2048];
   510 	g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
   511 	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 3);
   512 
   513 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   514 	{
   515 		return 0;
   516 	}
   517 
   518 	IPlugin *pPlugin = scripts->FindPluginByContext(pContext->GetContext());
   519 
   520 	LogAction(pPlugin->GetMyHandle(), 2, params[1], params[2], buffer);
   521 
   522 	return 1;
   523 }
   524 
   525 static cell_t LogToFile(IPluginContext *pContext, const cell_t *params)
   526 {
   527 	char *file;
   528 	pContext->LocalToString(params[1], &file);
   529 
   530 	char path[PLATFORM_MAX_PATH];
   531 	g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
   532 
   533 	FILE *fp = fopen(path, "at");
   534 	if (!fp)
   535 	{
   536 		return pContext->ThrowNativeError("Could not open file \"%s\"", path);
   537 	}
   538 
   539 	char buffer[2048];
   540 	g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
   541 	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
   542 
   543 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   544 	{
   545 		fclose(fp);
   546 		return 0;
   547 	}
   548 
   549 	IPlugin *pPlugin = scripts->FindPluginByContext(pContext->GetContext());
   550 
   551 	g_Logger.LogToOpenFile(fp, "[%s] %s", pPlugin->GetFilename(), buffer);
   552 
   553 	fclose(fp);
   554 
   555 	return 1;
   556 }
   557 
   558 static cell_t LogToFileEx(IPluginContext *pContext, const cell_t *params)
   559 {
   560 	char *file;
   561 	pContext->LocalToString(params[1], &file);
   562 
   563 	char path[PLATFORM_MAX_PATH];
   564 	g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
   565 
   566 	FILE *fp = fopen(path, "at");
   567 	if (!fp)
   568 	{
   569 		return pContext->ThrowNativeError("Could not open file \"%s\"", path);
   570 	}
   571 
   572 	char buffer[2048];
   573 	g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
   574 	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
   575 
   576 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   577 	{
   578 		fclose(fp);
   579 		return 0;
   580 	}
   581 
   582 	g_Logger.LogToOpenFile(fp, "%s", buffer);
   583 
   584 	fclose(fp);
   585 
   586 	return 1;
   587 }
   588 
   589 static cell_t GetExtensionFileStatus(IPluginContext *pContext, const cell_t *params)
   590 {
   591 	char *str;
   592 	pContext->LocalToString(params[1], &str);
   593 
   594 	IExtension *pExtension = extsys->FindExtensionByFile(str);
   595 	
   596 	if (!pExtension)
   597 	{
   598 		return -2;
   599 	}
   600 	
   601 	if (!pExtension->IsLoaded())
   602 	{
   603 		return -1;
   604 	}
   605 
   606 	char *error;
   607 	pContext->LocalToString(params[2], &error);
   608 	if (!pExtension->IsRunning(error, params[3]))
   609 	{
   610 		return 0;
   611 	}
   612 
   613 	return 1;
   614 }
   615 
   616 static cell_t FindPluginByNumber(IPluginContext *pContext, const cell_t *params)
   617 {
   618 	IPlugin *pPlugin = scripts->FindPluginByOrder(params[1]);
   619 	
   620 	if (pPlugin == NULL)
   621 	{
   622 		return BAD_HANDLE;
   623 	}
   624 
   625 	return pPlugin->GetMyHandle();
   626 }
   627 
   628 static cell_t VerifyCoreVersion(IPluginContext *pContext, const cell_t *params)
   629 {
   630 	return 4;
   631 }
   632 
   633 static cell_t GetFeatureStatus(IPluginContext *pContext, const cell_t *params)
   634 {
   635 	FeatureType type = (FeatureType)params[1];
   636 	char *name;
   637 
   638 	pContext->LocalToString(params[2], &name);
   639 
   640 	return sharesys->TestFeature(pContext->GetRuntime(), type, name);
   641 }
   642 
   643 static cell_t RequireFeature(IPluginContext *pContext, const cell_t *params)
   644 {
   645 	FeatureType type = (FeatureType)params[1];
   646 	char *name;
   647 
   648 	pContext->LocalToString(params[2], &name);
   649 
   650 	if (sharesys->TestFeature(pContext->GetRuntime(), type, name) != FeatureStatus_Available)
   651 	{
   652 		char buffer[255];
   653 		char *msg = buffer;
   654 		char default_message[255];
   655 		SMPlugin *pPlugin = scripts->FindPluginByContext(pContext->GetContext());
   656 
   657 		g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 3);
   658 		if (pContext->GetLastNativeError() != SP_ERROR_NONE || buffer[0] == '\0')
   659 		{
   660 			UTIL_Format(default_message, sizeof(default_message), "Feature \"%s\" not available", name);
   661 			msg = default_message;
   662 		}
   663 		pPlugin->SetErrorState(Plugin_Error, "%s", msg);
   664 		return pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", msg);
   665 	}
   666 
   667 	return 1;
   668 }
   669 
   670 enum NumberType
   671 {
   672 	NumberType_Int8,
   673 	NumberType_Int16,
   674 	NumberType_Int32
   675 };
   676 
   677 //memory addresses below 0x10000 are automatically considered invalid for dereferencing
   678 #define VALID_MINIMUM_MEMORY_ADDRESS 0x10000
   679 
   680 static cell_t LoadFromAddress(IPluginContext *pContext, const cell_t *params)
   681 {
   682 	void *addr = reinterpret_cast<void*>(params[1]);
   683 
   684 	if (addr == NULL)
   685 	{
   686 		pContext->ThrowNativeError("Address cannot be null");
   687 	}
   688 	else if (reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
   689 	{
   690 		pContext->ThrowNativeError("Invalid address 0x%x is pointing to reserved memory.", addr);
   691 	}
   692 	NumberType size = static_cast<NumberType>(params[2]);
   693 
   694 	switch(size)
   695 	{
   696 	case NumberType_Int8:
   697 		return *reinterpret_cast<uint8_t*>(addr);
   698 	case NumberType_Int16:
   699 		return *reinterpret_cast<uint16_t*>(addr);
   700 	case NumberType_Int32:
   701 		return *reinterpret_cast<uint32_t*>(addr);
   702 	default:
   703 		pContext->ThrowNativeError("Invalid number types %d", size);
   704 	}
   705 
   706 	return 1;
   707 }
   708 
   709 
   710 static cell_t StoreToAddress(IPluginContext *pContext, const cell_t *params)
   711 {
   712 	void *addr = reinterpret_cast<void*>(params[1]);
   713 
   714 	if (addr == NULL)
   715 	{
   716 		pContext->ThrowNativeError("Address cannot be null");
   717 	}
   718 	else if (reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
   719 	{
   720 		pContext->ThrowNativeError("Invalid address 0x%x is pointing to reserved memory.", addr);
   721 	}
   722 	cell_t data = params[2];
   723 
   724 	NumberType size = static_cast<NumberType>(params[3]);
   725 
   726 	switch(size)
   727 	{
   728 	case NumberType_Int8:
   729 		SourceHook::SetMemAccess(addr, sizeof(uint8_t), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   730 		*reinterpret_cast<uint8_t*>(addr) = data;
   731 		break;
   732 	case NumberType_Int16:
   733 		SourceHook::SetMemAccess(addr, sizeof(uint16_t), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   734 		*reinterpret_cast<uint16_t*>(addr) = data;
   735 		break;
   736 	case NumberType_Int32:
   737 		SourceHook::SetMemAccess(addr, sizeof(uint32_t), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   738 		*reinterpret_cast<uint32_t*>(addr) = data;
   739 		break;
   740 	default:
   741 		pContext->ThrowNativeError("Invalid number types %d", size);
   742 	}
   743 
   744 	return 1;
   745 }
   746 
   747 REGISTER_NATIVES(coreNatives)
   748 {
   749 	{"AutoExecConfig",			AutoExecConfig},
   750 	{"GetPluginFilename",		GetPluginFilename},
   751 	{"GetPluginInfo",			GetPluginInfo},
   752 	{"GetPluginIterator",		GetPluginIterator},
   753 	{"GetPluginStatus",			GetPluginStatus},
   754 	{"GetSysTickCount",			GetSysTickCount},
   755 	{"GetTime",					GetTime},
   756 	{"IsPluginDebugging",		IsPluginDebugging},
   757 	{"MorePlugins",				MorePlugins},
   758 	{"ReadPlugin",				ReadPlugin},
   759 	{"ThrowError",				ThrowError},
   760 	{"SetFailState",			SetFailState},
   761 	{"FormatTime",				FormatTime},
   762 	{"MarkNativeAsOptional",	MarkNativeAsOptional},
   763 	{"RegPluginLibrary",		RegPluginLibrary},
   764 	{"LibraryExists",			LibraryExists},
   765 	{"LogAction",				sm_LogAction},
   766 	{"LogToFile",				LogToFile},
   767 	{"LogToFileEx",				LogToFileEx},
   768 	{"GetExtensionFileStatus",	GetExtensionFileStatus},
   769 	{"FindPluginByNumber",		FindPluginByNumber},
   770 	{"VerifyCoreVersion",		VerifyCoreVersion},
   771 	{"GetFeatureStatus",        GetFeatureStatus},
   772 	{"RequireFeature",          RequireFeature},
   773 	{"LoadFromAddress",         LoadFromAddress},
   774 	{"StoreToAddress",          StoreToAddress},
   775 	{NULL,						NULL},
   776 };
   777