3 * =============================================================================
5 * Copyright (C) 2004-2007 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>.
34 #include "sp_vm_function.h"
35 #include "sm_stringutil.h"
41 void CFunction::Set(uint32_t code_addr, IPluginContext *plugin, funcid_t id, uint32_t pub_id)
43 m_codeaddr = code_addr;
46 m_errorstate = SP_ERROR_NONE;
48 m_pCtx = plugin ? plugin->GetContext() : NULL;
51 m_pContext->GetPublicByIndex(pub_id, &m_pPublic);
54 bool CFunction::IsRunnable()
56 return ((m_pCtx->flags & SPFLAG_PLUGIN_PAUSED) != SPFLAG_PLUGIN_PAUSED);
59 int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
65 return SP_ERROR_NOT_RUNNABLE;
68 if ((m_pCtx->prof_flags & SP_PROF_CALLBACKS) == SP_PROF_CALLBACKS
71 serial = m_pCtx->profiler->OnCallbackBegin(m_pContext, m_pPublic);
76 m_pContext->PushCell(params[num_params]);
79 ir = m_pContext->Execute(m_codeaddr, result);
81 if ((m_pCtx->prof_flags & SP_PROF_CALLBACKS) == SP_PROF_CALLBACKS
84 m_pCtx->profiler->OnCallbackEnd(serial);
90 IPluginContext *CFunction::GetParentContext()
95 CFunction::CFunction(uint32_t code_addr, IPluginContext *plugin, funcid_t id, uint32_t pub_id) :
96 m_codeaddr(code_addr), m_pContext(plugin), m_curparam(0),
97 m_errorstate(SP_ERROR_NONE), m_FnId(id)
102 m_pCtx = plugin->GetContext();
104 m_pContext->GetPublicByIndex(pub_id, &m_pPublic);
107 int CFunction::PushCell(cell_t cell)
109 if (m_curparam >= SP_MAX_EXEC_PARAMS)
111 return SetError(SP_ERROR_PARAMS_MAX);
114 m_info[m_curparam].marked = false;
115 m_params[m_curparam] = cell;
118 return SP_ERROR_NONE;
121 int CFunction::PushCellByRef(cell_t *cell, int flags)
123 return PushArray(cell, 1, flags);
126 int CFunction::PushFloat(float number)
128 cell_t val = *(cell_t *)&number;
130 return PushCell(val);
133 int CFunction::PushFloatByRef(float *number, int flags)
135 return PushCellByRef((cell_t *)number, flags);
138 int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
140 if (m_curparam >= SP_MAX_EXEC_PARAMS)
142 return SetError(SP_ERROR_PARAMS_MAX);
145 ParamInfo *info = &m_info[m_curparam];
147 info->flags = inarray ? copyback : 0;
150 info->str.is_sz = false;
151 info->orig_addr = inarray;
155 return SP_ERROR_NONE;
158 int CFunction::PushString(const char *string)
160 return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
163 int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
165 return _PushString(buffer, sz_flags, cp_flags, length);
168 int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
170 if (m_curparam >= SP_MAX_EXEC_PARAMS)
172 return SetError(SP_ERROR_PARAMS_MAX);
175 ParamInfo *info = &m_info[m_curparam];
178 info->orig_addr = (cell_t *)string;
179 info->flags = cp_flags;
181 info->str.sz_flags = sz_flags;
182 info->str.is_sz = true;
186 return SP_ERROR_NONE;
189 void CFunction::Cancel()
196 m_errorstate = SP_ERROR_NONE;
200 int CFunction::Execute(cell_t *result)
202 int err = SP_ERROR_NONE;
205 m_errorstate = SP_ERROR_NOT_RUNNABLE;
207 if (m_errorstate != SP_ERROR_NONE)
214 //This is for re-entrancy!
215 cell_t temp_params[SP_MAX_EXEC_PARAMS];
216 ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
217 unsigned int numparams = m_curparam;
219 bool docopies = true;
223 //Save the info locally, then reset it for re-entrant calls.
224 memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
228 /* Browse the parameters and build arrays */
229 for (i=0; i<numparams; i++)
231 /* Is this marked as an array? */
232 if (temp_info[i].marked)
234 if (!temp_info[i].str.is_sz)
236 /* Allocate a normal/generic array */
237 if ((err=m_pContext->HeapAlloc(temp_info[i].size,
238 &(temp_info[i].local_addr),
239 &(temp_info[i].phys_addr)))
244 if (temp_info[i].orig_addr)
246 memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
249 /* Calculate cells required for the string */
250 size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
252 /* Allocate the buffer */
253 if ((err=m_pContext->HeapAlloc(cells,
254 &(temp_info[i].local_addr),
255 &(temp_info[i].phys_addr)))
260 /* Copy original string if necessary */
261 if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
263 if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
265 if ((err=m_pContext->StringToLocalUTF8(temp_info[i].local_addr,
267 (const char *)temp_info[i].orig_addr,
274 if ((err=m_pContext->StringToLocal(temp_info[i].local_addr,
276 (const char *)temp_info[i].orig_addr))
283 } /* End array/string calculation */
284 /* Update the pushed parameter with the byref local address */
285 temp_params[i] = temp_info[i].local_addr;
287 /* Just copy the value normally */
288 temp_params[i] = m_params[i];
292 /* Make the call if we can */
293 if (err == SP_ERROR_NONE)
295 if ((err = CallFunction(temp_params, numparams, result)) != SP_ERROR_NONE)
303 /* i should be equal to the last valid parameter + 1 */
306 if (!temp_info[i].marked)
310 if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
312 if (temp_info[i].orig_addr)
314 if (temp_info[i].str.is_sz)
316 memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
318 if (temp_info[i].size == 1)
320 *temp_info[i].orig_addr = *(temp_info[i].phys_addr);
322 memcpy(temp_info[i].orig_addr,
323 temp_info[i].phys_addr,
324 temp_info[i].size * sizeof(cell_t));
329 if ((err=m_pContext->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
338 funcid_t CFunction::GetFunctionID()