1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/NativeInvoker.cpp Sun Feb 01 02:03:03 2009 -0500
1.3 @@ -0,0 +1,379 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourcePawn
1.8 + * Copyright (C) 2004-2009 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 "NativeInvoker.h"
1.36 +#include "ShareSys.h"
1.37 +
1.38 +NativeInterface g_NInvoke;
1.39 +
1.40 +NativeInvoker::NativeInvoker()
1.41 +{
1.42 +}
1.43 +
1.44 +NativeInvoker::~NativeInvoker()
1.45 +{
1.46 +}
1.47 +
1.48 +const char *NativeInterface::GetInterfaceName()
1.49 +{
1.50 + return SMINTERFACE_NINVOKE_NAME;
1.51 +}
1.52 +
1.53 +unsigned int NativeInterface::GetInterfaceVersion()
1.54 +{
1.55 + return SMINTERFACE_NINVOKE_VERSION;
1.56 +}
1.57 +
1.58 +void NativeInterface::OnSourceModAllInitialized()
1.59 +{
1.60 + g_ShareSys.AddInterface(NULL, &g_NInvoke);
1.61 +}
1.62 +
1.63 +IPluginRuntime *NativeInterface::CreateRuntime(const char *name, size_t bytes)
1.64 +{
1.65 + return g_pSourcePawn2->CreateEmptyRuntime(name, bytes);
1.66 +}
1.67 +
1.68 +INativeInvoker *NativeInterface::CreateInvoker()
1.69 +{
1.70 + return new NativeInvoker();
1.71 +}
1.72 +
1.73 +bool NativeInvoker::Start(IPluginContext *pContext, const char *name)
1.74 +{
1.75 + NativeEntry *entry;
1.76 +
1.77 + entry = g_ShareSys.FindNative(name);
1.78 + if (entry == NULL)
1.79 + {
1.80 + return false;
1.81 + }
1.82 +
1.83 + native = NULL;
1.84 + if (entry->replacement.owner != NULL)
1.85 + {
1.86 + native = entry->replacement.func;
1.87 + }
1.88 + else if (entry->owner != NULL)
1.89 + {
1.90 + native = entry->func;
1.91 + }
1.92 +
1.93 + if (native == NULL)
1.94 + {
1.95 + return false;
1.96 + }
1.97 +
1.98 + this->pContext = pContext;
1.99 +
1.100 + m_curparam = 0;
1.101 + m_errorstate = SP_ERROR_NONE;
1.102 +
1.103 + return true;
1.104 +}
1.105 +
1.106 +cell_t NativeInvoker::PushCell(cell_t cell)
1.107 +{
1.108 + if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.109 + {
1.110 + return SetError(SP_ERROR_PARAMS_MAX);
1.111 + }
1.112 +
1.113 + m_info[m_curparam].marked = false;
1.114 + m_params[m_curparam] = cell;
1.115 + m_curparam++;
1.116 +
1.117 + return SP_ERROR_NONE;
1.118 +}
1.119 +
1.120 +int NativeInvoker::PushCellByRef(cell_t *cell, int flags)
1.121 +{
1.122 + return PushArray(cell, 1, flags);
1.123 +}
1.124 +
1.125 +int NativeInvoker::PushFloat(float number)
1.126 +{
1.127 + cell_t val = *(cell_t *)&number;
1.128 +
1.129 + return PushCell(val);
1.130 +}
1.131 +
1.132 +int NativeInvoker::PushFloatByRef(float *number, int flags)
1.133 +{
1.134 + return PushCellByRef((cell_t *)number, flags);
1.135 +}
1.136 +
1.137 +int NativeInvoker::PushArray(cell_t *inarray, unsigned int cells, int copyback)
1.138 +{
1.139 + if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.140 + {
1.141 + return SetError(SP_ERROR_PARAMS_MAX);
1.142 + }
1.143 +
1.144 + ParamInfo *info = &m_info[m_curparam];
1.145 +
1.146 + info->flags = inarray ? copyback : 0;
1.147 + info->marked = true;
1.148 + info->size = cells;
1.149 + info->str.is_sz = false;
1.150 + info->orig_addr = inarray;
1.151 +
1.152 + m_curparam++;
1.153 +
1.154 + return SP_ERROR_NONE;
1.155 +}
1.156 +
1.157 +int NativeInvoker::PushString(const char *string)
1.158 +{
1.159 + return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
1.160 +}
1.161 +
1.162 +int NativeInvoker::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
1.163 +{
1.164 + return _PushString(buffer, sz_flags, cp_flags, length);
1.165 +}
1.166 +
1.167 +int NativeInvoker::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
1.168 +{
1.169 + if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.170 + {
1.171 + return SetError(SP_ERROR_PARAMS_MAX);
1.172 + }
1.173 +
1.174 + ParamInfo *info = &m_info[m_curparam];
1.175 +
1.176 + info->marked = true;
1.177 + info->orig_addr = (cell_t *)string;
1.178 + info->flags = cp_flags;
1.179 + info->size = len;
1.180 + info->str.sz_flags = sz_flags;
1.181 + info->str.is_sz = true;
1.182 +
1.183 + m_curparam++;
1.184 +
1.185 + return SP_ERROR_NONE;
1.186 +}
1.187 +
1.188 +void NativeInvoker::Cancel()
1.189 +{
1.190 + if (pContext == NULL)
1.191 + {
1.192 + return;
1.193 + }
1.194 +
1.195 + m_errorstate = SP_ERROR_NONE;
1.196 + m_curparam = 0;
1.197 + pContext = NULL;
1.198 + native = NULL;
1.199 +}
1.200 +
1.201 +int NativeInvoker::SetError(int err)
1.202 +{
1.203 + m_errorstate = err;
1.204 + return err;
1.205 +}
1.206 +
1.207 +int NativeInvoker::Invoke(cell_t *result)
1.208 +{
1.209 + int err = SP_ERROR_NONE;
1.210 +
1.211 + if (pContext == NULL)
1.212 + {
1.213 + return SP_ERROR_INVALID_NATIVE;
1.214 + }
1.215 +
1.216 + if (m_errorstate != SP_ERROR_NONE)
1.217 + {
1.218 + err = m_errorstate;
1.219 + Cancel();
1.220 + return err;
1.221 + }
1.222 +
1.223 + cell_t tresult;
1.224 +
1.225 + if (result == NULL)
1.226 + {
1.227 + result = &tresult;
1.228 + }
1.229 +
1.230 + //This is for re-entrancy!
1.231 + IPluginContext *ctx = pContext;
1.232 + cell_t _temp_params[SP_MAX_EXEC_PARAMS + 1];
1.233 + cell_t *temp_params = &_temp_params[1];
1.234 + ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
1.235 + unsigned int numparams = m_curparam;
1.236 + unsigned int i;
1.237 + bool docopies = true;
1.238 +
1.239 + if (numparams)
1.240 + {
1.241 + //Save the info locally, then reset it for re-entrant calls.
1.242 + memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
1.243 + }
1.244 + m_curparam = 0;
1.245 + pContext = NULL;
1.246 +
1.247 + /* Initialize 0th parameter */
1.248 + _temp_params[0] = numparams;
1.249 +
1.250 + /* Browse the parameters and build arrays */
1.251 + for (i = 0; i < numparams; i++)
1.252 + {
1.253 + /* Is this marked as an array? */
1.254 + if (temp_info[i].marked)
1.255 + {
1.256 + if (!temp_info[i].str.is_sz)
1.257 + {
1.258 + /* Allocate a normal/generic array */
1.259 + if ((err = ctx->HeapAlloc(temp_info[i].size,
1.260 + &temp_info[i].local_addr,
1.261 + &temp_info[i].phys_addr))
1.262 + != SP_ERROR_NONE)
1.263 + {
1.264 + break;
1.265 + }
1.266 + if (temp_info[i].orig_addr)
1.267 + {
1.268 + memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
1.269 + }
1.270 + }
1.271 + else
1.272 + {
1.273 + /* Calculate cells required for the string */
1.274 + size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
1.275 +
1.276 + /* Allocate the buffer */
1.277 + if ((err = ctx->HeapAlloc(cells,
1.278 + &temp_info[i].local_addr,
1.279 + &temp_info[i].phys_addr))
1.280 + != SP_ERROR_NONE)
1.281 + {
1.282 + break;
1.283 + }
1.284 + /* Copy original string if necessary */
1.285 + if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
1.286 + {
1.287 + /* Cut off UTF-8 properly */
1.288 + if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
1.289 + {
1.290 + if ((err = ctx->StringToLocalUTF8(temp_info[i].local_addr,
1.291 + temp_info[i].size,
1.292 + (const char *)temp_info[i].orig_addr,
1.293 + NULL))
1.294 + != SP_ERROR_NONE)
1.295 + {
1.296 + break;
1.297 + }
1.298 + }
1.299 + /* Copy a binary blob */
1.300 + else if (temp_info[i].str.sz_flags & SM_PARAM_STRING_BINARY)
1.301 + {
1.302 + memmove(temp_info[i].phys_addr, temp_info[i].orig_addr, temp_info[i].size);
1.303 + }
1.304 + /* Copy ASCII characters */
1.305 + else
1.306 + {
1.307 + if ((err = ctx->StringToLocal(temp_info[i].local_addr,
1.308 + temp_info[i].size,
1.309 + (const char *)temp_info[i].orig_addr))
1.310 + != SP_ERROR_NONE)
1.311 + {
1.312 + break;
1.313 + }
1.314 + }
1.315 + }
1.316 + } /* End array/string calculation */
1.317 + /* Update the pushed parameter with the byref local address */
1.318 + temp_params[i] = temp_info[i].local_addr;
1.319 + }
1.320 + else
1.321 + {
1.322 + /* Just copy the value normally */
1.323 + temp_params[i] = m_params[i];
1.324 + }
1.325 + }
1.326 +
1.327 + /* Make the call if we can */
1.328 + if (err == SP_ERROR_NONE)
1.329 + {
1.330 + *result = native(ctx, _temp_params);
1.331 + if (ctx->GetLastNativeError() != SP_ERROR_NONE)
1.332 + {
1.333 + docopies = false;
1.334 + ctx->ClearLastNativeError();
1.335 + }
1.336 + }
1.337 + else
1.338 + {
1.339 + docopies = false;
1.340 + }
1.341 +
1.342 + /* i should be equal to the last valid parameter + 1 */
1.343 + while (i--)
1.344 + {
1.345 + if (!temp_info[i].marked)
1.346 + {
1.347 + continue;
1.348 + }
1.349 +
1.350 + if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
1.351 + {
1.352 + if (temp_info[i].orig_addr)
1.353 + {
1.354 + if (temp_info[i].str.is_sz)
1.355 + {
1.356 + memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
1.357 +
1.358 + }
1.359 + else
1.360 + {
1.361 + if (temp_info[i].size == 1)
1.362 + {
1.363 + *temp_info[i].orig_addr = *(temp_info[i].phys_addr);
1.364 + }
1.365 + else
1.366 + {
1.367 + memcpy(temp_info[i].orig_addr,
1.368 + temp_info[i].phys_addr,
1.369 + temp_info[i].size * sizeof(cell_t));
1.370 + }
1.371 + }
1.372 + }
1.373 + }
1.374 +
1.375 + if ((err = ctx->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
1.376 + {
1.377 + return err;
1.378 + }
1.379 + }
1.380 +
1.381 + return err;
1.382 +}