Updated some CS:GO gamedata.
2 * vim: set ts=4 sw=4 tw=99 noet :
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 "sp_vm_function.h"
33 #include "BaseRuntime.h"
39 void CFunction::Set(BaseRuntime *runtime, funcid_t fnid, uint32_t pub_id)
43 m_errorstate = SP_ERROR_NONE;
47 bool CFunction::IsRunnable()
49 return !m_pRuntime->IsPaused();
52 int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
54 return CallFunction2(m_pRuntime->GetDefaultContext(), params, num_params, result);
57 int CFunction::CallFunction2(IPluginContext *pContext, const cell_t *params, unsigned int num_params, cell_t *result)
59 return pContext->Execute2(this, params, num_params, result);
62 IPluginContext *CFunction::GetParentContext()
64 return m_pRuntime->GetDefaultContext();
67 CFunction::CFunction(BaseRuntime *runtime, funcid_t id, uint32_t pub_id) :
68 m_curparam(0), m_errorstate(SP_ERROR_NONE), m_FnId(id)
73 int CFunction::PushCell(cell_t cell)
75 if (m_curparam >= SP_MAX_EXEC_PARAMS)
77 return SetError(SP_ERROR_PARAMS_MAX);
80 m_info[m_curparam].marked = false;
81 m_params[m_curparam] = cell;
87 int CFunction::PushCellByRef(cell_t *cell, int flags)
89 return PushArray(cell, 1, flags);
92 int CFunction::PushFloat(float number)
94 cell_t val = *(cell_t *)&number;
99 int CFunction::PushFloatByRef(float *number, int flags)
101 return PushCellByRef((cell_t *)number, flags);
104 int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
106 if (m_curparam >= SP_MAX_EXEC_PARAMS)
108 return SetError(SP_ERROR_PARAMS_MAX);
111 ParamInfo *info = &m_info[m_curparam];
113 info->flags = inarray ? copyback : 0;
116 info->str.is_sz = false;
117 info->orig_addr = inarray;
121 return SP_ERROR_NONE;
124 int CFunction::PushString(const char *string)
126 return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
129 int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
131 return _PushString(buffer, sz_flags, cp_flags, length);
134 int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
136 if (m_curparam >= SP_MAX_EXEC_PARAMS)
138 return SetError(SP_ERROR_PARAMS_MAX);
141 ParamInfo *info = &m_info[m_curparam];
144 info->orig_addr = (cell_t *)string;
145 info->flags = cp_flags;
147 info->str.sz_flags = sz_flags;
148 info->str.is_sz = true;
152 return SP_ERROR_NONE;
155 void CFunction::Cancel()
162 m_errorstate = SP_ERROR_NONE;
166 int CFunction::Execute(cell_t *result)
168 return Execute2(m_pRuntime->GetDefaultContext(), result);
171 int CFunction::Execute2(IPluginContext *ctx, cell_t *result)
173 int err = SP_ERROR_NONE;
177 m_errorstate = SP_ERROR_NOT_RUNNABLE;
180 if (m_errorstate != SP_ERROR_NONE)
187 //This is for re-entrancy!
188 cell_t temp_params[SP_MAX_EXEC_PARAMS];
189 ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
190 unsigned int numparams = m_curparam;
192 bool docopies = true;
196 //Save the info locally, then reset it for re-entrant calls.
197 memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
201 /* Browse the parameters and build arrays */
202 for (i=0; i<numparams; i++)
204 /* Is this marked as an array? */
205 if (temp_info[i].marked)
207 if (!temp_info[i].str.is_sz)
209 /* Allocate a normal/generic array */
210 if ((err=ctx->HeapAlloc(temp_info[i].size,
211 &(temp_info[i].local_addr),
212 &(temp_info[i].phys_addr)))
217 if (temp_info[i].orig_addr)
219 memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
224 /* Calculate cells required for the string */
225 size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
227 /* Allocate the buffer */
228 if ((err=ctx->HeapAlloc(cells,
229 &(temp_info[i].local_addr),
230 &(temp_info[i].phys_addr)))
235 /* Copy original string if necessary */
236 if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
238 /* Cut off UTF-8 properly */
239 if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
241 if ((err=ctx->StringToLocalUTF8(temp_info[i].local_addr,
243 (const char *)temp_info[i].orig_addr,
250 /* Copy a binary blob */
251 else if (temp_info[i].str.sz_flags & SM_PARAM_STRING_BINARY)
253 memmove(temp_info[i].phys_addr, temp_info[i].orig_addr, temp_info[i].size);
255 /* Copy ASCII characters */
258 if ((err=ctx->StringToLocal(temp_info[i].local_addr,
260 (const char *)temp_info[i].orig_addr))
267 } /* End array/string calculation */
268 /* Update the pushed parameter with the byref local address */
269 temp_params[i] = temp_info[i].local_addr;
273 /* Just copy the value normally */
274 temp_params[i] = m_params[i];
278 /* Make the call if we can */
279 if (err == SP_ERROR_NONE)
281 if ((err = CallFunction2(ctx, temp_params, numparams, result)) != SP_ERROR_NONE)
291 /* i should be equal to the last valid parameter + 1 */
294 if (!temp_info[i].marked)
299 if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
301 if (temp_info[i].orig_addr)
303 if (temp_info[i].str.is_sz)
305 memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
310 if (temp_info[i].size == 1)
312 *temp_info[i].orig_addr = *(temp_info[i].phys_addr);
316 memcpy(temp_info[i].orig_addr,
317 temp_info[i].phys_addr,
318 temp_info[i].size * sizeof(cell_t));
324 if ((err=ctx->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
333 IPluginRuntime *CFunction::GetParentRuntime()
338 funcid_t CFunction::GetFunctionID()
343 int CFunction::SetError(int err)