Triggering build against new mac libprotobuf.
3 * =============================================================================
5 * Copyright (C) 2004-2009 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "NativeInvoker.h"
35 NativeInterface g_NInvoke;
37 NativeInvoker::NativeInvoker()
41 NativeInvoker::~NativeInvoker()
45 const char *NativeInterface::GetInterfaceName()
47 return SMINTERFACE_NINVOKE_NAME;
50 unsigned int NativeInterface::GetInterfaceVersion()
52 return SMINTERFACE_NINVOKE_VERSION;
55 void NativeInterface::OnSourceModAllInitialized()
57 g_ShareSys.AddInterface(NULL, &g_NInvoke);
60 IPluginRuntime *NativeInterface::CreateRuntime(const char *name, size_t bytes)
62 return g_pSourcePawn2->CreateEmptyRuntime(name, bytes);
65 INativeInvoker *NativeInterface::CreateInvoker()
67 return new NativeInvoker();
70 bool NativeInvoker::Start(IPluginContext *pContext, const char *name)
74 entry = g_ShareSys.FindNative(name);
81 if (entry->replacement.owner != NULL)
83 native = entry->replacement.func;
85 else if (entry->owner != NULL)
95 this->pContext = pContext;
98 m_errorstate = SP_ERROR_NONE;
103 cell_t NativeInvoker::PushCell(cell_t cell)
105 if (m_curparam >= SP_MAX_EXEC_PARAMS)
107 return SetError(SP_ERROR_PARAMS_MAX);
110 m_info[m_curparam].marked = false;
111 m_params[m_curparam] = cell;
114 return SP_ERROR_NONE;
117 int NativeInvoker::PushCellByRef(cell_t *cell, int flags)
119 return PushArray(cell, 1, flags);
122 int NativeInvoker::PushFloat(float number)
124 cell_t val = *(cell_t *)&number;
126 return PushCell(val);
129 int NativeInvoker::PushFloatByRef(float *number, int flags)
131 return PushCellByRef((cell_t *)number, flags);
134 int NativeInvoker::PushArray(cell_t *inarray, unsigned int cells, int copyback)
136 if (m_curparam >= SP_MAX_EXEC_PARAMS)
138 return SetError(SP_ERROR_PARAMS_MAX);
141 ParamInfo *info = &m_info[m_curparam];
143 info->flags = inarray ? copyback : 0;
146 info->str.is_sz = false;
147 info->orig_addr = inarray;
151 return SP_ERROR_NONE;
154 int NativeInvoker::PushString(const char *string)
156 return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
159 int NativeInvoker::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
161 return _PushString(buffer, sz_flags, cp_flags, length);
164 int NativeInvoker::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
166 if (m_curparam >= SP_MAX_EXEC_PARAMS)
168 return SetError(SP_ERROR_PARAMS_MAX);
171 ParamInfo *info = &m_info[m_curparam];
174 info->orig_addr = (cell_t *)string;
175 info->flags = cp_flags;
177 info->str.sz_flags = sz_flags;
178 info->str.is_sz = true;
182 return SP_ERROR_NONE;
185 void NativeInvoker::Cancel()
187 if (pContext == NULL)
192 m_errorstate = SP_ERROR_NONE;
198 int NativeInvoker::SetError(int err)
204 int NativeInvoker::Invoke(cell_t *result)
206 int err = SP_ERROR_NONE;
208 if (pContext == NULL)
210 return SP_ERROR_INVALID_NATIVE;
213 if (m_errorstate != SP_ERROR_NONE)
227 //This is for re-entrancy!
228 IPluginContext *ctx = pContext;
229 cell_t _temp_params[SP_MAX_EXEC_PARAMS + 1];
230 cell_t *temp_params = &_temp_params[1];
231 ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
232 unsigned int numparams = m_curparam;
234 bool docopies = true;
238 //Save the info locally, then reset it for re-entrant calls.
239 memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
244 /* Initialize 0th parameter */
245 _temp_params[0] = numparams;
247 /* Browse the parameters and build arrays */
248 for (i = 0; i < numparams; i++)
250 /* Is this marked as an array? */
251 if (temp_info[i].marked)
253 if (!temp_info[i].str.is_sz)
255 /* Allocate a normal/generic array */
256 if ((err = ctx->HeapAlloc(temp_info[i].size,
257 &temp_info[i].local_addr,
258 &temp_info[i].phys_addr))
263 if (temp_info[i].orig_addr)
265 memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
270 /* Calculate cells required for the string */
271 size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
273 /* Allocate the buffer */
274 if ((err = ctx->HeapAlloc(cells,
275 &temp_info[i].local_addr,
276 &temp_info[i].phys_addr))
281 /* Copy original string if necessary */
282 if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
284 /* Cut off UTF-8 properly */
285 if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
287 if ((err = ctx->StringToLocalUTF8(temp_info[i].local_addr,
289 (const char *)temp_info[i].orig_addr,
296 /* Copy a binary blob */
297 else if (temp_info[i].str.sz_flags & SM_PARAM_STRING_BINARY)
299 memmove(temp_info[i].phys_addr, temp_info[i].orig_addr, temp_info[i].size);
301 /* Copy ASCII characters */
304 if ((err = ctx->StringToLocal(temp_info[i].local_addr,
306 (const char *)temp_info[i].orig_addr))
313 } /* End array/string calculation */
314 /* Update the pushed parameter with the byref local address */
315 temp_params[i] = temp_info[i].local_addr;
319 /* Just copy the value normally */
320 temp_params[i] = m_params[i];
324 /* Make the call if we can */
325 if (err == SP_ERROR_NONE)
327 *result = native(ctx, _temp_params);
328 if (ctx->GetLastNativeError() != SP_ERROR_NONE)
331 ctx->ClearLastNativeError();
339 /* i should be equal to the last valid parameter + 1 */
342 if (!temp_info[i].marked)
347 if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
349 if (temp_info[i].orig_addr)
351 if (temp_info[i].str.is_sz)
353 memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
358 if (temp_info[i].size == 1)
360 *temp_info[i].orig_addr = *(temp_info[i].phys_addr);
364 memcpy(temp_info[i].orig_addr,
365 temp_info[i].phys_addr,
366 temp_info[i].size * sizeof(cell_t));
372 if ((err = ctx->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)