1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/logic/smn_fakenatives.cpp Fri Mar 29 11:37:29 2013 -0700
1.3 @@ -0,0 +1,453 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include <sh_list.h>
1.36 +#include <sh_string.h>
1.37 +#include <sm_trie_tpl.h>
1.38 +#include <ISourceMod.h>
1.39 +#include "common_logic.h"
1.40 +#include "ShareSys.h"
1.41 +#include "PluginSys.h"
1.42 +
1.43 +using namespace SourceHook;
1.44 +
1.45 +static cell_t s_curparams[SP_MAX_EXEC_PARAMS+1];
1.46 +static FakeNative *s_curnative = NULL;
1.47 +static IPluginContext *s_curcaller = NULL;
1.48 +
1.49 +cell_t FakeNativeRouter(IPluginContext *pContext, const cell_t *params, void *pData)
1.50 +{
1.51 + FakeNative *native = (FakeNative *)pData;
1.52 +
1.53 + /* Check if too many parameters were passed */
1.54 + if (params[0] > SP_MAX_EXEC_PARAMS)
1.55 + {
1.56 + return pContext->ThrowNativeError("Called native with too many parameters (%d>%d)", params[9], SP_MAX_EXEC_PARAMS);
1.57 + }
1.58 +
1.59 + /* Check if the native is paused */
1.60 + if (native->ctx->GetRuntime()->IsPaused())
1.61 + {
1.62 + return pContext->ThrowNativeError("Plugin owning this native is currently paused.");
1.63 + }
1.64 +
1.65 + CPlugin *pCaller = g_PluginSys.GetPluginByCtx(pContext->GetContext());
1.66 +
1.67 + /* Save any old data on the stack */
1.68 + FakeNative *pSaveNative = s_curnative;
1.69 + IPluginContext *pSaveCaller = s_curcaller;
1.70 + cell_t save_params[SP_MAX_EXEC_PARAMS+1];
1.71 + if (pSaveNative != NULL)
1.72 + {
1.73 + /* Copy all old parameters */
1.74 + for (cell_t i=0; i<=s_curparams[0]; i++)
1.75 + {
1.76 + save_params[i] = s_curparams[i];
1.77 + }
1.78 + }
1.79 +
1.80 + /* Save the current parameters */
1.81 + s_curnative = native;
1.82 + s_curcaller = pContext;
1.83 + for (cell_t i=0; i<=params[0]; i++)
1.84 + {
1.85 + s_curparams[i] = params[i];
1.86 + }
1.87 +
1.88 + /* Push info and execute. */
1.89 + cell_t result = 0;
1.90 + native->call->PushCell(pCaller->GetMyHandle());
1.91 + native->call->PushCell(params[0]);
1.92 + int error;
1.93 + if ((error=native->call->Execute(&result)) != SP_ERROR_NONE)
1.94 + {
1.95 + if (pContext->GetLastNativeError() == SP_ERROR_NONE)
1.96 + {
1.97 + pContext->ThrowNativeErrorEx(error, "Error encountered while processing a dynamic native");
1.98 + }
1.99 + }
1.100 +
1.101 + /* Restore everything from the stack if necessary */
1.102 + s_curnative = pSaveNative;
1.103 + s_curcaller = pSaveCaller;
1.104 + if (pSaveNative != NULL)
1.105 + {
1.106 + for (cell_t i=0; i<=save_params[0]; i++)
1.107 + {
1.108 + s_curparams[i] = save_params[i];
1.109 + }
1.110 + }
1.111 +
1.112 + return result;
1.113 +}
1.114 +
1.115 +static cell_t CreateNative(IPluginContext *pContext, const cell_t *params)
1.116 +{
1.117 + char *name;
1.118 + CPlugin *pPlugin;
1.119 +
1.120 + pContext->LocalToString(params[1], &name);
1.121 +
1.122 + IPluginFunction *pFunction = pContext->GetFunctionById(params[2]);
1.123 + if (!pFunction)
1.124 + {
1.125 + return pContext->ThrowNativeError("Function %x is not a valid function", params[2]);
1.126 + }
1.127 +
1.128 + pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
1.129 +
1.130 + if (!pPlugin->AddFakeNative(pFunction, name, FakeNativeRouter))
1.131 + {
1.132 + return pContext->ThrowNativeError("Fatal error creating dynamic native!");
1.133 + }
1.134 +
1.135 + return 1;
1.136 +}
1.137 +
1.138 +static cell_t ThrowNativeError(IPluginContext *pContext, const cell_t *params)
1.139 +{
1.140 + if (!s_curnative || (s_curnative->ctx != pContext))
1.141 + {
1.142 + return pContext->ThrowNativeError("Not called from inside a native function");
1.143 + }
1.144 +
1.145 + g_pSM->SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1.146 +
1.147 + char buffer[512];
1.148 + g_pSM->FormatString(buffer, sizeof(buffer), pContext, params, 2);
1.149 +
1.150 + if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1.151 + {
1.152 + s_curcaller->ThrowNativeError("Error encountered while processing a dynamic native");
1.153 + } else {
1.154 + s_curcaller->ThrowNativeErrorEx(params[1], "%s", buffer);
1.155 + }
1.156 +
1.157 + return 0;
1.158 +}
1.159 +
1.160 +static cell_t GetNativeStringLength(IPluginContext *pContext, const cell_t *params)
1.161 +{
1.162 + if (!s_curnative || (s_curnative->ctx != pContext))
1.163 + {
1.164 + return pContext->ThrowNativeError("Not called from inside a native function");
1.165 + }
1.166 +
1.167 + cell_t param = params[1];
1.168 + if (param < 1 || param > s_curparams[0])
1.169 + {
1.170 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.171 + }
1.172 +
1.173 + int err;
1.174 + char *str;
1.175 + if ((err=s_curcaller->LocalToString(s_curparams[param], &str)) != SP_ERROR_NONE)
1.176 + {
1.177 + return err;
1.178 + }
1.179 +
1.180 + cell_t *addr;
1.181 + pContext->LocalToPhysAddr(params[2], &addr);
1.182 + *addr = (cell_t)strlen(str);
1.183 +
1.184 + return SP_ERROR_NONE;
1.185 +}
1.186 +
1.187 +static cell_t GetNativeString(IPluginContext *pContext, const cell_t *params)
1.188 +{
1.189 + if (!s_curnative || (s_curnative->ctx != pContext))
1.190 + {
1.191 + return pContext->ThrowNativeError("Not called from inside a native function");
1.192 + }
1.193 +
1.194 + cell_t param = params[1];
1.195 + if (param < 1 || param > s_curparams[0])
1.196 + {
1.197 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.198 + }
1.199 +
1.200 + int err;
1.201 + char *str;
1.202 + if ((err=s_curcaller->LocalToString(s_curparams[param], &str)) != SP_ERROR_NONE)
1.203 + {
1.204 + return err;
1.205 + }
1.206 +
1.207 + size_t bytes = 0;
1.208 + pContext->StringToLocalUTF8(params[2], params[3], str, &bytes);
1.209 +
1.210 + cell_t *addr;
1.211 + pContext->LocalToPhysAddr(params[4], &addr);
1.212 + *addr = (cell_t)bytes;
1.213 +
1.214 + return SP_ERROR_NONE;
1.215 +}
1.216 +
1.217 +static cell_t SetNativeString(IPluginContext *pContext, const cell_t *params)
1.218 +{
1.219 + if (!s_curnative || (s_curnative->ctx != pContext))
1.220 + {
1.221 + return pContext->ThrowNativeError("Not called from inside a native function");
1.222 + }
1.223 +
1.224 + cell_t param = params[1];
1.225 + if (param < 1 || param > s_curparams[0])
1.226 + {
1.227 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.228 + }
1.229 +
1.230 + char *str;
1.231 + pContext->LocalToString(params[2], &str);
1.232 +
1.233 + int err;
1.234 + size_t bytes = 0;
1.235 + if (params[4])
1.236 + {
1.237 + err = s_curcaller->StringToLocalUTF8(s_curparams[param], params[3], str, &bytes);
1.238 + } else {
1.239 + err = s_curcaller->StringToLocal(s_curparams[param], params[3], str);
1.240 + /* Eww */
1.241 + bytes = strlen(str);
1.242 + if (bytes >= (size_t)params[3])
1.243 + {
1.244 + bytes = params[3] - 1;
1.245 + }
1.246 + }
1.247 +
1.248 + if (err != SP_ERROR_NONE)
1.249 + {
1.250 + return err;
1.251 + }
1.252 +
1.253 + cell_t *addr;
1.254 + pContext->LocalToPhysAddr(params[5], &addr);
1.255 + *addr = (cell_t)bytes;
1.256 +
1.257 + return SP_ERROR_NONE;
1.258 +}
1.259 +
1.260 +static cell_t GetNativeCell(IPluginContext *pContext, const cell_t *params)
1.261 +{
1.262 + if (!s_curnative || (s_curnative->ctx != pContext))
1.263 + {
1.264 + return pContext->ThrowNativeError("Not called from inside a native function");
1.265 + }
1.266 +
1.267 + cell_t param = params[1];
1.268 + if (param < 1 || param > s_curparams[0])
1.269 + {
1.270 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.271 + }
1.272 +
1.273 + return s_curparams[param];
1.274 +}
1.275 +
1.276 +static cell_t GetNativeCellRef(IPluginContext *pContext, const cell_t *params)
1.277 +{
1.278 + if (!s_curnative || (s_curnative->ctx != pContext))
1.279 + {
1.280 + return pContext->ThrowNativeError("Not called from inside a native function");
1.281 + }
1.282 +
1.283 + cell_t param = params[1];
1.284 + if (param < 1 || param > s_curparams[0])
1.285 + {
1.286 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.287 + }
1.288 +
1.289 + cell_t *addr;
1.290 + if (s_curcaller->LocalToPhysAddr(s_curparams[param], &addr) != SP_ERROR_NONE)
1.291 + {
1.292 + return s_curcaller->ThrowNativeErrorEx(SP_ERROR_INVALID_ADDRESS, "Invalid address value");
1.293 + }
1.294 +
1.295 + return *addr;
1.296 +}
1.297 +
1.298 +static cell_t SetNativeCellRef(IPluginContext *pContext, const cell_t *params)
1.299 +{
1.300 + if (!s_curnative || (s_curnative->ctx != pContext))
1.301 + {
1.302 + return pContext->ThrowNativeError("Not called from inside a native function");
1.303 + }
1.304 +
1.305 + cell_t param = params[1];
1.306 + if (param < 1 || param > s_curparams[0])
1.307 + {
1.308 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.309 + }
1.310 +
1.311 + cell_t *addr;
1.312 + if (s_curcaller->LocalToPhysAddr(s_curparams[param], &addr) != SP_ERROR_NONE)
1.313 + {
1.314 + return s_curcaller->ThrowNativeErrorEx(SP_ERROR_INVALID_ADDRESS, "Invalid address value");
1.315 + }
1.316 +
1.317 + *addr = params[2];
1.318 +
1.319 + return 1;
1.320 +}
1.321 +
1.322 +static cell_t GetNativeArray(IPluginContext *pContext, const cell_t *params)
1.323 +{
1.324 + if (!s_curnative || (s_curnative->ctx != pContext))
1.325 + {
1.326 + return pContext->ThrowNativeError("Not called from inside a native function");
1.327 + }
1.328 +
1.329 + cell_t param = params[1];
1.330 + if (param < 1 || param > s_curparams[0])
1.331 + {
1.332 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.333 + }
1.334 +
1.335 + int err;
1.336 + cell_t *addr;
1.337 + if ((err=s_curcaller->LocalToPhysAddr(s_curparams[param], &addr)) != SP_ERROR_NONE)
1.338 + {
1.339 + return err;
1.340 + }
1.341 +
1.342 + cell_t *src;
1.343 + pContext->LocalToPhysAddr(params[2], &src);
1.344 +
1.345 + memcpy(src, addr, sizeof(cell_t) * params[3]);
1.346 +
1.347 + return SP_ERROR_NONE;
1.348 +}
1.349 +
1.350 +static cell_t SetNativeArray(IPluginContext *pContext, const cell_t *params)
1.351 +{
1.352 + if (!s_curnative || (s_curnative->ctx != pContext))
1.353 + {
1.354 + return pContext->ThrowNativeError("Not called from inside a native function");
1.355 + }
1.356 +
1.357 + cell_t param = params[1];
1.358 + if (param < 1 || param > s_curparams[0])
1.359 + {
1.360 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.361 + }
1.362 +
1.363 + int err;
1.364 + cell_t *addr;
1.365 + if ((err=s_curcaller->LocalToPhysAddr(s_curparams[param], &addr)) != SP_ERROR_NONE)
1.366 + {
1.367 + return err;
1.368 + }
1.369 +
1.370 + cell_t *src;
1.371 + pContext->LocalToPhysAddr(params[2], &src);
1.372 +
1.373 + memcpy(addr, src, sizeof(cell_t) * params[3]);
1.374 +
1.375 + return SP_ERROR_NONE;
1.376 +}
1.377 +
1.378 +static cell_t FormatNativeString(IPluginContext *pContext, const cell_t *params)
1.379 +{
1.380 + if (!s_curnative || (s_curnative->ctx != pContext))
1.381 + {
1.382 + return pContext->ThrowNativeError("Not called from inside a native function");
1.383 + }
1.384 +
1.385 + int out_param = params[1];
1.386 + int fmt_param = params[2];
1.387 + int var_param = params[3];
1.388 +
1.389 + /* Validate input */
1.390 + if (out_param && (out_param < 1 || out_param > s_curparams[0]))
1.391 + {
1.392 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", out_param);
1.393 + }
1.394 + if (fmt_param && (fmt_param < 1 || fmt_param > s_curparams[0]))
1.395 + {
1.396 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", fmt_param);
1.397 + }
1.398 + if (var_param && (var_param < 1 || var_param > s_curparams[0] + 1))
1.399 + {
1.400 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", fmt_param);
1.401 + }
1.402 +
1.403 + /* Get buffer information */
1.404 + int err;
1.405 + char *output_buffer;
1.406 + char *format_buffer;
1.407 +
1.408 + if (out_param)
1.409 + {
1.410 + if ((err=s_curcaller->LocalToString(s_curparams[out_param], &output_buffer)) != SP_ERROR_NONE)
1.411 + {
1.412 + return err;
1.413 + }
1.414 + } else {
1.415 + pContext->LocalToString(params[6], &output_buffer);
1.416 + }
1.417 +
1.418 + if (fmt_param)
1.419 + {
1.420 + if ((err=s_curcaller->LocalToString(s_curparams[fmt_param], &format_buffer)) != SP_ERROR_NONE)
1.421 + {
1.422 + return err;
1.423 + }
1.424 + } else {
1.425 + pContext->LocalToString(params[7], &format_buffer);
1.426 + }
1.427 +
1.428 + /* Get maximum length */
1.429 + size_t maxlen = (size_t)params[4];
1.430 +
1.431 + /* Do the format */
1.432 + size_t written = smcore.atcprintf(output_buffer, maxlen, format_buffer, s_curcaller, s_curparams, &var_param);
1.433 +
1.434 + cell_t *addr;
1.435 + pContext->LocalToPhysAddr(params[5], &addr);
1.436 + *addr = (cell_t)written;
1.437 +
1.438 + return s_curcaller->GetLastNativeError();
1.439 +}
1.440 +
1.441 +//tee hee
1.442 +REGISTER_NATIVES(nativeNatives)
1.443 +{
1.444 + {"CreateNative", CreateNative},
1.445 + {"GetNativeArray", GetNativeArray},
1.446 + {"GetNativeCell", GetNativeCell},
1.447 + {"GetNativeCellRef", GetNativeCellRef},
1.448 + {"GetNativeString", GetNativeString},
1.449 + {"GetNativeStringLength", GetNativeStringLength},
1.450 + {"FormatNativeString", FormatNativeString},
1.451 + {"ThrowNativeError", ThrowNativeError},
1.452 + {"SetNativeArray", SetNativeArray},
1.453 + {"SetNativeCellRef", SetNativeCellRef},
1.454 + {"SetNativeString", SetNativeString},
1.455 + {NULL, NULL},
1.456 +};