1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/smn_fakenatives.cpp Sun Jul 06 04:49:55 2008 +0000
1.3 @@ -0,0 +1,455 @@
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.h"
1.38 +#include "sm_globals.h"
1.39 +#include "PluginSys.h"
1.40 +#include "sourcemod.h"
1.41 +#include "sm_stringutil.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 + sp_context_t *pNativeCtx = native->ctx->GetContext();
1.61 + if ((pNativeCtx->flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED)
1.62 + {
1.63 + return pContext->ThrowNativeError("Plugin owning this native is currently paused.");
1.64 + }
1.65 +
1.66 + CPlugin *pCaller = g_PluginSys.GetPluginByCtx(pContext->GetContext());
1.67 +
1.68 + /* Save any old data on the stack */
1.69 + FakeNative *pSaveNative = s_curnative;
1.70 + IPluginContext *pSaveCaller = s_curcaller;
1.71 + cell_t save_params[SP_MAX_EXEC_PARAMS+1];
1.72 + if (pSaveNative != NULL)
1.73 + {
1.74 + /* Copy all old parameters */
1.75 + for (cell_t i=0; i<=s_curparams[0]; i++)
1.76 + {
1.77 + save_params[i] = s_curparams[i];
1.78 + }
1.79 + }
1.80 +
1.81 + /* Save the current parameters */
1.82 + s_curnative = native;
1.83 + s_curcaller = pContext;
1.84 + for (cell_t i=0; i<=params[0]; i++)
1.85 + {
1.86 + s_curparams[i] = params[i];
1.87 + }
1.88 +
1.89 + /* Push info and execute. */
1.90 + cell_t result = 0;
1.91 + native->call->PushCell(pCaller->GetMyHandle());
1.92 + native->call->PushCell(params[0]);
1.93 + int error;
1.94 + if ((error=native->call->Execute(&result)) != SP_ERROR_NONE)
1.95 + {
1.96 + if (pContext->GetContext()->n_err == SP_ERROR_NONE)
1.97 + {
1.98 + pContext->ThrowNativeErrorEx(error, "Error encountered while processing a dynamic native");
1.99 + }
1.100 + }
1.101 +
1.102 + /* Restore everything from the stack if necessary */
1.103 + s_curnative = pSaveNative;
1.104 + s_curcaller = pSaveCaller;
1.105 + if (pSaveNative != NULL)
1.106 + {
1.107 + for (cell_t i=0; i<=save_params[0]; i++)
1.108 + {
1.109 + s_curparams[i] = save_params[i];
1.110 + }
1.111 + }
1.112 +
1.113 + return result;
1.114 +}
1.115 +
1.116 +static cell_t CreateNative(IPluginContext *pContext, const cell_t *params)
1.117 +{
1.118 + char *name;
1.119 + CPlugin *pPlugin;
1.120 +
1.121 + pContext->LocalToString(params[1], &name);
1.122 +
1.123 + IPluginFunction *pFunction = pContext->GetFunctionById(params[2]);
1.124 + if (!pFunction)
1.125 + {
1.126 + return pContext->ThrowNativeError("Function %x is not a valid function", params[2]);
1.127 + }
1.128 +
1.129 + pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
1.130 +
1.131 + if (!pPlugin->AddFakeNative(pFunction, name, FakeNativeRouter))
1.132 + {
1.133 + return pContext->ThrowNativeError("Fatal error creating dynamic native!");
1.134 + }
1.135 +
1.136 + return 1;
1.137 +}
1.138 +
1.139 +static cell_t ThrowNativeError(IPluginContext *pContext, const cell_t *params)
1.140 +{
1.141 + if (!s_curnative || (s_curnative->ctx != pContext))
1.142 + {
1.143 + return pContext->ThrowNativeError("Not called from inside a native function");
1.144 + }
1.145 +
1.146 + g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1.147 +
1.148 + char buffer[512];
1.149 +
1.150 + g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1.151 +
1.152 + if (pContext->GetContext()->n_err != SP_ERROR_NONE)
1.153 + {
1.154 + s_curcaller->ThrowNativeError("Error encountered while processing a dynamic native");
1.155 + } else {
1.156 + s_curcaller->ThrowNativeErrorEx(params[1], "%s", buffer);
1.157 + }
1.158 +
1.159 + return 0;
1.160 +}
1.161 +
1.162 +static cell_t GetNativeStringLength(IPluginContext *pContext, const cell_t *params)
1.163 +{
1.164 + if (!s_curnative || (s_curnative->ctx != pContext))
1.165 + {
1.166 + return pContext->ThrowNativeError("Not called from inside a native function");
1.167 + }
1.168 +
1.169 + cell_t param = params[1];
1.170 + if (param < 1 || param > s_curparams[0])
1.171 + {
1.172 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.173 + }
1.174 +
1.175 + int err;
1.176 + char *str;
1.177 + if ((err=s_curcaller->LocalToString(s_curparams[param], &str)) != SP_ERROR_NONE)
1.178 + {
1.179 + return err;
1.180 + }
1.181 +
1.182 + cell_t *addr;
1.183 + pContext->LocalToPhysAddr(params[2], &addr);
1.184 + *addr = (cell_t)strlen(str);
1.185 +
1.186 + return SP_ERROR_NONE;
1.187 +}
1.188 +
1.189 +static cell_t GetNativeString(IPluginContext *pContext, const cell_t *params)
1.190 +{
1.191 + if (!s_curnative || (s_curnative->ctx != pContext))
1.192 + {
1.193 + return pContext->ThrowNativeError("Not called from inside a native function");
1.194 + }
1.195 +
1.196 + cell_t param = params[1];
1.197 + if (param < 1 || param > s_curparams[0])
1.198 + {
1.199 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.200 + }
1.201 +
1.202 + int err;
1.203 + char *str;
1.204 + if ((err=s_curcaller->LocalToString(s_curparams[param], &str)) != SP_ERROR_NONE)
1.205 + {
1.206 + return err;
1.207 + }
1.208 +
1.209 + size_t bytes = 0;
1.210 + pContext->StringToLocalUTF8(params[2], params[3], str, &bytes);
1.211 +
1.212 + cell_t *addr;
1.213 + pContext->LocalToPhysAddr(params[4], &addr);
1.214 + *addr = (cell_t)bytes;
1.215 +
1.216 + return SP_ERROR_NONE;
1.217 +}
1.218 +
1.219 +static cell_t SetNativeString(IPluginContext *pContext, const cell_t *params)
1.220 +{
1.221 + if (!s_curnative || (s_curnative->ctx != pContext))
1.222 + {
1.223 + return pContext->ThrowNativeError("Not called from inside a native function");
1.224 + }
1.225 +
1.226 + cell_t param = params[1];
1.227 + if (param < 1 || param > s_curparams[0])
1.228 + {
1.229 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.230 + }
1.231 +
1.232 + char *str;
1.233 + pContext->LocalToString(params[2], &str);
1.234 +
1.235 + int err;
1.236 + size_t bytes = 0;
1.237 + if (params[4])
1.238 + {
1.239 + err = s_curcaller->StringToLocalUTF8(s_curparams[param], params[3], str, &bytes);
1.240 + } else {
1.241 + err = s_curcaller->StringToLocal(s_curparams[param], params[3], str);
1.242 + /* Eww */
1.243 + bytes = strlen(str);
1.244 + if (bytes >= (size_t)params[3])
1.245 + {
1.246 + bytes = params[3] - 1;
1.247 + }
1.248 + }
1.249 +
1.250 + if (err != SP_ERROR_NONE)
1.251 + {
1.252 + return err;
1.253 + }
1.254 +
1.255 + cell_t *addr;
1.256 + pContext->LocalToPhysAddr(params[5], &addr);
1.257 + *addr = (cell_t)bytes;
1.258 +
1.259 + return SP_ERROR_NONE;
1.260 +}
1.261 +
1.262 +static cell_t GetNativeCell(IPluginContext *pContext, const cell_t *params)
1.263 +{
1.264 + if (!s_curnative || (s_curnative->ctx != pContext))
1.265 + {
1.266 + return pContext->ThrowNativeError("Not called from inside a native function");
1.267 + }
1.268 +
1.269 + cell_t param = params[1];
1.270 + if (param < 1 || param > s_curparams[0])
1.271 + {
1.272 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.273 + }
1.274 +
1.275 + return s_curparams[param];
1.276 +}
1.277 +
1.278 +static cell_t GetNativeCellRef(IPluginContext *pContext, const cell_t *params)
1.279 +{
1.280 + if (!s_curnative || (s_curnative->ctx != pContext))
1.281 + {
1.282 + return pContext->ThrowNativeError("Not called from inside a native function");
1.283 + }
1.284 +
1.285 + cell_t param = params[1];
1.286 + if (param < 1 || param > s_curparams[0])
1.287 + {
1.288 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.289 + }
1.290 +
1.291 + cell_t *addr;
1.292 + if (s_curcaller->LocalToPhysAddr(s_curparams[param], &addr) != SP_ERROR_NONE)
1.293 + {
1.294 + return s_curcaller->ThrowNativeErrorEx(SP_ERROR_INVALID_ADDRESS, "Invalid address value");
1.295 + }
1.296 +
1.297 + return *addr;
1.298 +}
1.299 +
1.300 +static cell_t SetNativeCellRef(IPluginContext *pContext, const cell_t *params)
1.301 +{
1.302 + if (!s_curnative || (s_curnative->ctx != pContext))
1.303 + {
1.304 + return pContext->ThrowNativeError("Not called from inside a native function");
1.305 + }
1.306 +
1.307 + cell_t param = params[1];
1.308 + if (param < 1 || param > s_curparams[0])
1.309 + {
1.310 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.311 + }
1.312 +
1.313 + cell_t *addr;
1.314 + if (s_curcaller->LocalToPhysAddr(s_curparams[param], &addr) != SP_ERROR_NONE)
1.315 + {
1.316 + return s_curcaller->ThrowNativeErrorEx(SP_ERROR_INVALID_ADDRESS, "Invalid address value");
1.317 + }
1.318 +
1.319 + *addr = params[2];
1.320 +
1.321 + return 1;
1.322 +}
1.323 +
1.324 +static cell_t GetNativeArray(IPluginContext *pContext, const cell_t *params)
1.325 +{
1.326 + if (!s_curnative || (s_curnative->ctx != pContext))
1.327 + {
1.328 + return pContext->ThrowNativeError("Not called from inside a native function");
1.329 + }
1.330 +
1.331 + cell_t param = params[1];
1.332 + if (param < 1 || param > s_curparams[0])
1.333 + {
1.334 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.335 + }
1.336 +
1.337 + int err;
1.338 + cell_t *addr;
1.339 + if ((err=s_curcaller->LocalToPhysAddr(s_curparams[param], &addr)) != SP_ERROR_NONE)
1.340 + {
1.341 + return err;
1.342 + }
1.343 +
1.344 + cell_t *src;
1.345 + pContext->LocalToPhysAddr(params[2], &src);
1.346 +
1.347 + memcpy(src, addr, sizeof(cell_t) * params[3]);
1.348 +
1.349 + return SP_ERROR_NONE;
1.350 +}
1.351 +
1.352 +static cell_t SetNativeArray(IPluginContext *pContext, const cell_t *params)
1.353 +{
1.354 + if (!s_curnative || (s_curnative->ctx != pContext))
1.355 + {
1.356 + return pContext->ThrowNativeError("Not called from inside a native function");
1.357 + }
1.358 +
1.359 + cell_t param = params[1];
1.360 + if (param < 1 || param > s_curparams[0])
1.361 + {
1.362 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
1.363 + }
1.364 +
1.365 + int err;
1.366 + cell_t *addr;
1.367 + if ((err=s_curcaller->LocalToPhysAddr(s_curparams[param], &addr)) != SP_ERROR_NONE)
1.368 + {
1.369 + return err;
1.370 + }
1.371 +
1.372 + cell_t *src;
1.373 + pContext->LocalToPhysAddr(params[2], &src);
1.374 +
1.375 + memcpy(addr, src, sizeof(cell_t) * params[3]);
1.376 +
1.377 + return SP_ERROR_NONE;
1.378 +}
1.379 +
1.380 +static cell_t FormatNativeString(IPluginContext *pContext, const cell_t *params)
1.381 +{
1.382 + if (!s_curnative || (s_curnative->ctx != pContext))
1.383 + {
1.384 + return pContext->ThrowNativeError("Not called from inside a native function");
1.385 + }
1.386 +
1.387 + int out_param = params[1];
1.388 + int fmt_param = params[2];
1.389 + int var_param = params[3];
1.390 +
1.391 + /* Validate input */
1.392 + if (out_param && (out_param < 1 || out_param > s_curparams[0]))
1.393 + {
1.394 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", out_param);
1.395 + }
1.396 + if (fmt_param && (fmt_param < 1 || fmt_param > s_curparams[0]))
1.397 + {
1.398 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", fmt_param);
1.399 + }
1.400 + if (var_param && (var_param < 1 || var_param > s_curparams[0] + 1))
1.401 + {
1.402 + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", fmt_param);
1.403 + }
1.404 +
1.405 + /* Get buffer information */
1.406 + int err;
1.407 + char *output_buffer;
1.408 + char *format_buffer;
1.409 +
1.410 + if (out_param)
1.411 + {
1.412 + if ((err=s_curcaller->LocalToString(s_curparams[out_param], &output_buffer)) != SP_ERROR_NONE)
1.413 + {
1.414 + return err;
1.415 + }
1.416 + } else {
1.417 + pContext->LocalToString(params[6], &output_buffer);
1.418 + }
1.419 +
1.420 + if (fmt_param)
1.421 + {
1.422 + if ((err=s_curcaller->LocalToString(s_curparams[fmt_param], &format_buffer)) != SP_ERROR_NONE)
1.423 + {
1.424 + return err;
1.425 + }
1.426 + } else {
1.427 + pContext->LocalToString(params[7], &format_buffer);
1.428 + }
1.429 +
1.430 + /* Get maximum length */
1.431 + size_t maxlen = (size_t)params[4];
1.432 +
1.433 + /* Do the format */
1.434 + size_t written = atcprintf(output_buffer, maxlen, format_buffer, s_curcaller, s_curparams, &var_param);
1.435 +
1.436 + cell_t *addr;
1.437 + pContext->LocalToPhysAddr(params[5], &addr);
1.438 + *addr = (cell_t)written;
1.439 +
1.440 + return s_curcaller->GetContext()->n_err;
1.441 +}
1.442 +
1.443 +//tee hee
1.444 +REGISTER_NATIVES(nativeNatives)
1.445 +{
1.446 + {"CreateNative", CreateNative},
1.447 + {"GetNativeArray", GetNativeArray},
1.448 + {"GetNativeCell", GetNativeCell},
1.449 + {"GetNativeCellRef", GetNativeCellRef},
1.450 + {"GetNativeString", GetNativeString},
1.451 + {"GetNativeStringLength", GetNativeStringLength},
1.452 + {"FormatNativeString", FormatNativeString},
1.453 + {"ThrowNativeError", ThrowNativeError},
1.454 + {"SetNativeArray", SetNativeArray},
1.455 + {"SetNativeCellRef", SetNativeCellRef},
1.456 + {"SetNativeString", SetNativeString},
1.457 + {NULL, NULL},
1.458 +};