3 * ================================================================
4 * SourcePawn (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ================================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
17 #include "PluginSys.h"
23 void CFunction::Set(uint32_t code_addr, IPluginContext *plugin)
25 m_codeaddr = code_addr;
28 m_errorstate = SP_ERROR_NONE;
30 m_pCtx = plugin ? plugin->GetContext() : NULL;
33 bool CFunction::IsRunnable()
35 return ((m_pCtx->flags & SPFLAG_PLUGIN_PAUSED) != SPFLAG_PLUGIN_PAUSED);
38 int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
42 return SP_ERROR_NOT_RUNNABLE;
47 m_pContext->PushCell(params[num_params]);
50 return m_pContext->Execute(m_codeaddr, result);
53 IPluginContext *CFunction::GetParentContext()
58 CFunction::CFunction(uint32_t code_addr, IPluginContext *plugin) :
59 m_codeaddr(code_addr), m_pContext(plugin), m_curparam(0),
60 m_errorstate(SP_ERROR_NONE)
65 m_pCtx = plugin->GetContext();
69 int CFunction::PushCell(cell_t cell)
71 if (m_curparam >= SP_MAX_EXEC_PARAMS)
73 return SetError(SP_ERROR_PARAMS_MAX);
76 m_info[m_curparam].marked = false;
77 m_params[m_curparam] = cell;
83 int CFunction::PushCellByRef(cell_t *cell, int flags)
85 return PushArray(cell, 1, flags);
88 int CFunction::PushFloat(float number)
90 cell_t val = *(cell_t *)&number;
95 int CFunction::PushFloatByRef(float *number, int flags)
97 return PushCellByRef((cell_t *)number, flags);
100 int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
102 if (m_curparam >= SP_MAX_EXEC_PARAMS)
104 return SetError(SP_ERROR_PARAMS_MAX);
107 ParamInfo *info = &m_info[m_curparam];
109 info->flags = inarray ? copyback : 0;
112 info->str.is_sz = false;
113 info->orig_addr = inarray;
117 return SP_ERROR_NONE;
120 int CFunction::PushString(const char *string)
122 return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
125 int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
127 return _PushString(buffer, sz_flags, cp_flags, length);
130 int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
132 if (m_curparam >= SP_MAX_EXEC_PARAMS)
134 return SetError(SP_ERROR_PARAMS_MAX);
137 ParamInfo *info = &m_info[m_curparam];
140 info->orig_addr = (cell_t *)string;
141 info->flags = cp_flags;
143 info->str.sz_flags = sz_flags;
144 info->str.is_sz = true;
148 return SP_ERROR_NONE;
151 void CFunction::Cancel()
158 m_errorstate = SP_ERROR_NONE;
162 int CFunction::Execute(cell_t *result)
164 int err = SP_ERROR_NONE;
167 m_errorstate = SP_ERROR_NOT_RUNNABLE;
169 if (m_errorstate != SP_ERROR_NONE)
176 //This is for re-entrancy!
177 cell_t temp_params[SP_MAX_EXEC_PARAMS];
178 ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
179 unsigned int numparams = m_curparam;
181 bool docopies = true;
185 //Save the info locally, then reset it for re-entrant calls.
186 memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
190 /* Browse the parameters and build arrays */
191 for (i=0; i<numparams; i++)
193 /* Is this marked as an array? */
194 if (temp_info[i].marked)
196 if (!temp_info[i].str.is_sz)
198 /* Allocate a normal/generic array */
199 if ((err=m_pContext->HeapAlloc(temp_info[i].size,
200 &(temp_info[i].local_addr),
201 &(temp_info[i].phys_addr)))
206 if (temp_info[i].orig_addr)
208 memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
211 /* Calculate cells required for the string */
212 size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
214 /* Allocate the buffer */
215 if ((err=m_pContext->HeapAlloc(cells,
216 &(temp_info[i].local_addr),
217 &(temp_info[i].phys_addr)))
222 /* Copy original string if necessary */
223 if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
225 if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
227 if ((err=m_pContext->StringToLocalUTF8(temp_info[i].local_addr,
229 (const char *)temp_info[i].orig_addr,
236 if ((err=m_pContext->StringToLocal(temp_info[i].local_addr,
238 (const char *)temp_info[i].orig_addr))
245 } /* End array/string calculation */
246 /* Update the pushed parameter with the byref local address */
247 temp_params[i] = temp_info[i].local_addr;
249 /* Just copy the value normally */
250 temp_params[i] = m_params[i];
254 /* Make the call if we can */
255 if (err == SP_ERROR_NONE)
257 if ((err = CallFunction(temp_params, numparams, result)) != SP_ERROR_NONE)
265 /* i should be equal to the last valid parameter + 1 */
268 if (!temp_info[i].marked)
272 if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
274 if (temp_info[i].orig_addr)
276 if (temp_info[i].str.is_sz)
278 memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
280 if (temp_info[i].size == 1)
282 *temp_info[i].orig_addr = *(temp_info[i].phys_addr);
284 memcpy(temp_info[i].orig_addr,
285 temp_info[i].phys_addr,
286 temp_info[i].size * sizeof(cell_t));
291 if ((err=m_pContext->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)