core/smn_fakenatives.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jul 06 04:49:55 2008 +0000 (2008-07-06)
branchrefac-jit
changeset 2168 c24a6fa957db
child 2175 54a8411faaac
permissions -rw-r--r--
jit refactoring branch
     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 <sh_list.h>
    33 #include <sh_string.h>
    34 #include "sm_trie.h"
    35 #include "sm_globals.h"
    36 #include "PluginSys.h"
    37 #include "sourcemod.h"
    38 #include "sm_stringutil.h"
    39 
    40 using namespace SourceHook;
    41 
    42 static cell_t s_curparams[SP_MAX_EXEC_PARAMS+1];
    43 static FakeNative *s_curnative = NULL;
    44 static IPluginContext *s_curcaller = NULL;
    45 
    46 cell_t FakeNativeRouter(IPluginContext *pContext, const cell_t *params, void *pData)
    47 {
    48 	FakeNative *native = (FakeNative *)pData;
    49 
    50 	/* Check if too many parameters were passed */
    51 	if (params[0] > SP_MAX_EXEC_PARAMS)
    52 	{
    53 		return pContext->ThrowNativeError("Called native with too many parameters (%d>%d)", params[9], SP_MAX_EXEC_PARAMS);
    54 	}
    55 
    56 	/* Check if the native is paused */
    57 	sp_context_t *pNativeCtx = native->ctx->GetContext();
    58 	if ((pNativeCtx->flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED)
    59 	{
    60 		return pContext->ThrowNativeError("Plugin owning this native is currently paused.");
    61 	}
    62 
    63 	CPlugin *pCaller = g_PluginSys.GetPluginByCtx(pContext->GetContext());
    64 
    65 	/* Save any old data on the stack */
    66 	FakeNative *pSaveNative = s_curnative;
    67 	IPluginContext *pSaveCaller = s_curcaller;
    68 	cell_t save_params[SP_MAX_EXEC_PARAMS+1];
    69 	if (pSaveNative != NULL)
    70 	{
    71 		/* Copy all old parameters */
    72 		for (cell_t i=0; i<=s_curparams[0]; i++)
    73 		{
    74 			save_params[i] = s_curparams[i];
    75 		}
    76 	}
    77 
    78 	/* Save the current parameters */
    79 	s_curnative = native;
    80 	s_curcaller = pContext;
    81 	for (cell_t i=0; i<=params[0]; i++)
    82 	{
    83 		s_curparams[i] = params[i];
    84 	}
    85 
    86 	/* Push info and execute. */
    87 	cell_t result = 0;
    88 	native->call->PushCell(pCaller->GetMyHandle());
    89 	native->call->PushCell(params[0]);
    90 	int error;
    91 	if ((error=native->call->Execute(&result)) != SP_ERROR_NONE)
    92 	{
    93 		if (pContext->GetContext()->n_err == SP_ERROR_NONE)
    94 		{
    95 			pContext->ThrowNativeErrorEx(error, "Error encountered while processing a dynamic native");
    96 		}
    97 	}
    98 
    99 	/* Restore everything from the stack if necessary */
   100 	s_curnative = pSaveNative;
   101 	s_curcaller = pSaveCaller;
   102 	if (pSaveNative != NULL)
   103 	{
   104 		for (cell_t i=0; i<=save_params[0]; i++)
   105 		{
   106 			s_curparams[i] = save_params[i];
   107 		}
   108 	}
   109 
   110 	return result;
   111 }
   112 
   113 static cell_t CreateNative(IPluginContext *pContext, const cell_t *params)
   114 {
   115 	char *name;
   116 	CPlugin *pPlugin;
   117 
   118 	pContext->LocalToString(params[1], &name);
   119 
   120 	IPluginFunction *pFunction = pContext->GetFunctionById(params[2]);
   121 	if (!pFunction)
   122 	{
   123 		return pContext->ThrowNativeError("Function %x is not a valid function", params[2]);
   124 	}
   125 
   126 	pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
   127 
   128 	if (!pPlugin->AddFakeNative(pFunction, name, FakeNativeRouter))
   129 	{
   130 		return pContext->ThrowNativeError("Fatal error creating dynamic native!");
   131 	}
   132 
   133 	return 1;
   134 }
   135 
   136 static cell_t ThrowNativeError(IPluginContext *pContext, const cell_t *params)
   137 {
   138 	if (!s_curnative || (s_curnative->ctx != pContext))
   139 	{
   140 		return pContext->ThrowNativeError("Not called from inside a native function");
   141 	}
   142 
   143 	g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
   144 
   145 	char buffer[512];
   146 
   147 	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
   148 
   149 	if (pContext->GetContext()->n_err != SP_ERROR_NONE)
   150 	{
   151 		s_curcaller->ThrowNativeError("Error encountered while processing a dynamic native");
   152 	} else {
   153 		s_curcaller->ThrowNativeErrorEx(params[1], "%s", buffer);
   154 	}
   155 
   156 	return 0;
   157 }
   158 
   159 static cell_t GetNativeStringLength(IPluginContext *pContext, const cell_t *params)
   160 {
   161 	if (!s_curnative || (s_curnative->ctx != pContext))
   162 	{
   163 		return pContext->ThrowNativeError("Not called from inside a native function");
   164 	}
   165 
   166 	cell_t param = params[1];
   167 	if (param < 1 || param > s_curparams[0])
   168 	{
   169 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
   170 	}
   171 
   172 	int err;
   173 	char *str;
   174 	if ((err=s_curcaller->LocalToString(s_curparams[param], &str)) != SP_ERROR_NONE)
   175 	{
   176 		return err;
   177 	}
   178 
   179 	cell_t *addr;
   180 	pContext->LocalToPhysAddr(params[2], &addr);
   181 	*addr = (cell_t)strlen(str);
   182 
   183 	return SP_ERROR_NONE;
   184 }
   185 
   186 static cell_t GetNativeString(IPluginContext *pContext, const cell_t *params)
   187 {
   188 	if (!s_curnative || (s_curnative->ctx != pContext))
   189 	{
   190 		return pContext->ThrowNativeError("Not called from inside a native function");
   191 	}
   192 
   193 	cell_t param = params[1];
   194 	if (param < 1 || param > s_curparams[0])
   195 	{
   196 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
   197 	}
   198 
   199 	int err;
   200 	char *str;
   201 	if ((err=s_curcaller->LocalToString(s_curparams[param], &str)) != SP_ERROR_NONE)
   202 	{
   203 		return err;
   204 	}
   205 
   206 	size_t bytes = 0;
   207 	pContext->StringToLocalUTF8(params[2], params[3], str, &bytes);
   208 
   209 	cell_t *addr;
   210 	pContext->LocalToPhysAddr(params[4], &addr);
   211 	*addr = (cell_t)bytes;
   212 
   213 	return SP_ERROR_NONE;
   214 }
   215 
   216 static cell_t SetNativeString(IPluginContext *pContext, const cell_t *params)
   217 {
   218 	if (!s_curnative || (s_curnative->ctx != pContext))
   219 	{
   220 		return pContext->ThrowNativeError("Not called from inside a native function");
   221 	}
   222 
   223 	cell_t param = params[1];
   224 	if (param < 1 || param > s_curparams[0])
   225 	{
   226 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
   227 	}
   228 
   229 	char *str;
   230 	pContext->LocalToString(params[2], &str);
   231 
   232 	int err;
   233 	size_t bytes = 0;
   234 	if (params[4])
   235 	{
   236 		err = s_curcaller->StringToLocalUTF8(s_curparams[param], params[3], str, &bytes);
   237 	} else {
   238 		err = s_curcaller->StringToLocal(s_curparams[param], params[3], str);
   239 		/* Eww */
   240 		bytes = strlen(str);
   241 		if (bytes >= (size_t)params[3])
   242 		{
   243 			bytes = params[3] - 1;
   244 		}
   245 	}
   246 
   247 	if (err != SP_ERROR_NONE)
   248 	{
   249 		return err;
   250 	}
   251 
   252 	cell_t *addr;
   253 	pContext->LocalToPhysAddr(params[5], &addr);
   254 	*addr = (cell_t)bytes;
   255 
   256 	return SP_ERROR_NONE;
   257 }
   258 
   259 static cell_t GetNativeCell(IPluginContext *pContext, const cell_t *params)
   260 {
   261 	if (!s_curnative || (s_curnative->ctx != pContext))
   262 	{
   263 		return pContext->ThrowNativeError("Not called from inside a native function");
   264 	}
   265 
   266 	cell_t param = params[1];
   267 	if (param < 1 || param > s_curparams[0])
   268 	{
   269 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
   270 	}
   271 
   272 	return s_curparams[param];
   273 }
   274 
   275 static cell_t GetNativeCellRef(IPluginContext *pContext, const cell_t *params)
   276 {
   277 	if (!s_curnative || (s_curnative->ctx != pContext))
   278 	{
   279 		return pContext->ThrowNativeError("Not called from inside a native function");
   280 	}
   281 
   282 	cell_t param = params[1];
   283 	if (param < 1 || param > s_curparams[0])
   284 	{
   285 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
   286 	}
   287 
   288 	cell_t *addr;
   289 	if (s_curcaller->LocalToPhysAddr(s_curparams[param], &addr) != SP_ERROR_NONE)
   290 	{
   291 		return s_curcaller->ThrowNativeErrorEx(SP_ERROR_INVALID_ADDRESS, "Invalid address value");
   292 	}
   293 
   294 	return *addr;
   295 }
   296 
   297 static cell_t SetNativeCellRef(IPluginContext *pContext, const cell_t *params)
   298 {
   299 	if (!s_curnative || (s_curnative->ctx != pContext))
   300 	{
   301 		return pContext->ThrowNativeError("Not called from inside a native function");
   302 	}
   303 
   304 	cell_t param = params[1];
   305 	if (param < 1 || param > s_curparams[0])
   306 	{
   307 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
   308 	}
   309 
   310 	cell_t *addr;
   311 	if (s_curcaller->LocalToPhysAddr(s_curparams[param], &addr) != SP_ERROR_NONE)
   312 	{
   313 		return s_curcaller->ThrowNativeErrorEx(SP_ERROR_INVALID_ADDRESS, "Invalid address value");
   314 	}
   315 
   316 	*addr = params[2];
   317 
   318 	return 1;
   319 }
   320 
   321 static cell_t GetNativeArray(IPluginContext *pContext, const cell_t *params)
   322 {
   323 	if (!s_curnative || (s_curnative->ctx != pContext))
   324 	{
   325 		return pContext->ThrowNativeError("Not called from inside a native function");
   326 	}
   327 
   328 	cell_t param = params[1];
   329 	if (param < 1 || param > s_curparams[0])
   330 	{
   331 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
   332 	}
   333 
   334 	int err;
   335 	cell_t *addr;
   336 	if ((err=s_curcaller->LocalToPhysAddr(s_curparams[param], &addr)) != SP_ERROR_NONE)
   337 	{
   338 		return err;
   339 	}
   340 
   341 	cell_t *src;
   342 	pContext->LocalToPhysAddr(params[2], &src);
   343 
   344 	memcpy(src, addr, sizeof(cell_t) * params[3]);
   345 
   346 	return SP_ERROR_NONE;
   347 }
   348 
   349 static cell_t SetNativeArray(IPluginContext *pContext, const cell_t *params)
   350 {
   351 	if (!s_curnative || (s_curnative->ctx != pContext))
   352 	{
   353 		return pContext->ThrowNativeError("Not called from inside a native function");
   354 	}
   355 
   356 	cell_t param = params[1];
   357 	if (param < 1 || param > s_curparams[0])
   358 	{
   359 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
   360 	}
   361 
   362 	int err;
   363 	cell_t *addr;
   364 	if ((err=s_curcaller->LocalToPhysAddr(s_curparams[param], &addr)) != SP_ERROR_NONE)
   365 	{
   366 		return err;
   367 	}
   368 
   369 	cell_t *src;
   370 	pContext->LocalToPhysAddr(params[2], &src);
   371 
   372 	memcpy(addr, src, sizeof(cell_t) * params[3]);
   373 
   374 	return SP_ERROR_NONE;
   375 }
   376 
   377 static cell_t FormatNativeString(IPluginContext *pContext, const cell_t *params)
   378 {
   379 	if (!s_curnative || (s_curnative->ctx != pContext))
   380 	{
   381 		return pContext->ThrowNativeError("Not called from inside a native function");
   382 	}
   383 
   384 	int out_param = params[1];
   385 	int fmt_param = params[2];
   386 	int var_param = params[3];
   387 
   388 	/* Validate input */
   389 	if (out_param && (out_param < 1 || out_param > s_curparams[0]))
   390 	{
   391 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", out_param);
   392 	}
   393 	if (fmt_param && (fmt_param < 1 || fmt_param > s_curparams[0]))
   394 	{
   395 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", fmt_param);
   396 	}
   397 	if (var_param && (var_param < 1 || var_param > s_curparams[0] + 1))
   398 	{
   399 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", fmt_param);
   400 	}
   401 
   402 	/* Get buffer information */
   403 	int err;
   404 	char *output_buffer;
   405 	char *format_buffer;
   406 
   407 	if (out_param)
   408 	{
   409 		if ((err=s_curcaller->LocalToString(s_curparams[out_param], &output_buffer)) != SP_ERROR_NONE)
   410 		{
   411 			return err;
   412 		}
   413 	} else {
   414 		pContext->LocalToString(params[6], &output_buffer);
   415 	}
   416 
   417 	if (fmt_param)
   418 	{
   419 		if ((err=s_curcaller->LocalToString(s_curparams[fmt_param], &format_buffer)) != SP_ERROR_NONE)
   420 		{
   421 			return err;
   422 		}
   423 	} else {
   424 		pContext->LocalToString(params[7], &format_buffer);
   425 	}
   426 
   427 	/* Get maximum length */
   428 	size_t maxlen = (size_t)params[4];
   429 
   430 	/* Do the format */
   431 	size_t written = atcprintf(output_buffer, maxlen, format_buffer, s_curcaller, s_curparams, &var_param);
   432 
   433 	cell_t *addr;
   434 	pContext->LocalToPhysAddr(params[5], &addr);
   435 	*addr = (cell_t)written;
   436 
   437 	return s_curcaller->GetContext()->n_err;
   438 }
   439 
   440 //tee hee
   441 REGISTER_NATIVES(nativeNatives)
   442 {
   443 	{"CreateNative",			CreateNative},
   444 	{"GetNativeArray",			GetNativeArray},
   445 	{"GetNativeCell",			GetNativeCell},
   446 	{"GetNativeCellRef",		GetNativeCellRef},
   447 	{"GetNativeString",			GetNativeString},
   448 	{"GetNativeStringLength",	GetNativeStringLength},
   449 	{"FormatNativeString",		FormatNativeString},
   450 	{"ThrowNativeError",		ThrowNativeError},
   451 	{"SetNativeArray",			SetNativeArray},
   452 	{"SetNativeCellRef",		SetNativeCellRef},
   453 	{"SetNativeString",			SetNativeString},
   454 	{NULL,						NULL},
   455 };