|
ds@1952
|
1 |
/** |
|
ds@1952
|
2 |
* vim: set ts=4 : |
|
ds@1952
|
3 |
* ============================================================================= |
|
ds@1952
|
4 |
* SourceMod |
|
ds@1952
|
5 |
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. |
|
ds@1952
|
6 |
* ============================================================================= |
|
ds@1952
|
7 |
* |
|
ds@1952
|
8 |
* This program is free software; you can redistribute it and/or modify it under |
|
ds@1952
|
9 |
* the terms of the GNU General Public License, version 3.0, as published by the |
|
ds@1952
|
10 |
* Free Software Foundation. |
|
ds@1952
|
11 |
* |
|
ds@1952
|
12 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
ds@1952
|
13 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
ds@1952
|
14 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
ds@1952
|
15 |
* details. |
|
ds@1952
|
16 |
* |
|
ds@1952
|
17 |
* You should have received a copy of the GNU General Public License along with |
|
ds@1952
|
18 |
* this program. If not, see <http://www.gnu.org/licenses/>. |
|
ds@1952
|
19 |
* |
|
ds@1952
|
20 |
* As a special exception, AlliedModders LLC gives you permission to link the |
|
ds@1952
|
21 |
* code of this program (as well as its derivative works) to "Half-Life 2," the |
|
ds@1952
|
22 |
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software |
|
ds@1952
|
23 |
* by the Valve Corporation. You must obey the GNU General Public License in |
|
ds@1952
|
24 |
* all respects for all other code used. Additionally, AlliedModders LLC grants |
|
ds@1952
|
25 |
* this exception to all derivative works. AlliedModders LLC defines further |
|
ds@1952
|
26 |
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), |
|
ds@1952
|
27 |
* or <http://www.sourcemod.net/license.php>. |
|
ds@1952
|
28 |
* |
|
ds@1952
|
29 |
* Version: $Id$ |
|
ds@1952
|
30 |
*/ |
|
ds@1952
|
31 |
|
|
ds@1952
|
32 |
#include <sh_list.h> |
|
ds@1952
|
33 |
#include <sh_string.h> |
|
ds@1952
|
34 |
#include "sm_trie.h" |
|
ds@1952
|
35 |
#include "sm_globals.h" |
|
ds@1952
|
36 |
#include "PluginSys.h" |
|
ds@1952
|
37 |
#include "sourcemod.h" |
|
ds@1952
|
38 |
#include "sm_stringutil.h" |
|
ds@1952
|
39 |
|
|
ds@1952
|
40 |
using namespace SourceHook; |
|
ds@1952
|
41 |
|
|
ds@1952
|
42 |
static cell_t s_curparams[SP_MAX_EXEC_PARAMS+1]; |
|
ds@1952
|
43 |
static FakeNative *s_curnative = NULL; |
|
ds@1952
|
44 |
static IPluginContext *s_curcaller = NULL; |
|
ds@1952
|
45 |
|
|
ds@1952
|
46 |
cell_t FakeNativeRouter(IPluginContext *pContext, const cell_t *params, void *pData) |
|
ds@1952
|
47 |
{ |
|
ds@1952
|
48 |
FakeNative *native = (FakeNative *)pData; |
|
ds@1952
|
49 |
|
|
ds@1952
|
50 |
/* Check if too many parameters were passed */ |
|
ds@1952
|
51 |
if (params[0] > SP_MAX_EXEC_PARAMS) |
|
ds@1952
|
52 |
{ |
|
ds@1952
|
53 |
return pContext->ThrowNativeError("Called native with too many parameters (%d>%d)", params[9], SP_MAX_EXEC_PARAMS); |
|
ds@1952
|
54 |
} |
|
ds@1952
|
55 |
|
|
ds@1952
|
56 |
/* Check if the native is paused */ |
|
ds@1952
|
57 |
sp_context_t *pNativeCtx = native->ctx->GetContext(); |
|
ds@1952
|
58 |
if ((pNativeCtx->flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED) |
|
ds@1952
|
59 |
{ |
|
ds@1952
|
60 |
return pContext->ThrowNativeError("Plugin owning this native is currently paused."); |
|
ds@1952
|
61 |
} |
|
ds@1952
|
62 |
|
|
ds@1952
|
63 |
CPlugin *pCaller = g_PluginSys.GetPluginByCtx(pContext->GetContext()); |
|
ds@1952
|
64 |
|
|
ds@1952
|
65 |
/* Save any old data on the stack */ |
|
ds@1952
|
66 |
FakeNative *pSaveNative = s_curnative; |
|
ds@1952
|
67 |
IPluginContext *pSaveCaller = s_curcaller; |
|
ds@1952
|
68 |
cell_t save_params[SP_MAX_EXEC_PARAMS+1]; |
|
ds@1952
|
69 |
if (pSaveNative != NULL) |
|
ds@1952
|
70 |
{ |
|
ds@1952
|
71 |
/* Copy all old parameters */ |
|
ds@1952
|
72 |
for (cell_t i=0; i<=s_curparams[0]; i++) |
|
ds@1952
|
73 |
{ |
|
ds@1952
|
74 |
save_params[i] = s_curparams[i]; |
|
ds@1952
|
75 |
} |
|
ds@1952
|
76 |
} |
|
ds@1952
|
77 |
|
|
ds@1952
|
78 |
/* Save the current parameters */ |
|
ds@1952
|
79 |
s_curnative = native; |
|
ds@1952
|
80 |
s_curcaller = pContext; |
|
ds@1952
|
81 |
for (cell_t i=0; i<=params[0]; i++) |
|
ds@1952
|
82 |
{ |
|
ds@1952
|
83 |
s_curparams[i] = params[i]; |
|
ds@1952
|
84 |
} |
|
ds@1952
|
85 |
|
|
ds@1952
|
86 |
/* Push info and execute. */ |
|
ds@1952
|
87 |
cell_t result = 0; |
|
ds@1952
|
88 |
native->call->PushCell(pCaller->GetMyHandle()); |
|
ds@1952
|
89 |
native->call->PushCell(params[0]); |
|
ds@1952
|
90 |
int error; |
|
ds@1952
|
91 |
if ((error=native->call->Execute(&result)) != SP_ERROR_NONE) |
|
ds@1952
|
92 |
{ |
|
ds@1952
|
93 |
if (pContext->GetContext()->n_err == SP_ERROR_NONE) |
|
ds@1952
|
94 |
{ |
|
ds@1952
|
95 |
pContext->ThrowNativeErrorEx(error, "Error encountered while processing a dynamic native"); |
|
ds@1952
|
96 |
} |
|
ds@1952
|
97 |
} |
|
ds@1952
|
98 |
|
|
ds@1952
|
99 |
/* Restore everything from the stack if necessary */ |
|
ds@1952
|
100 |
s_curnative = pSaveNative; |
|
ds@1952
|
101 |
s_curcaller = pSaveCaller; |
|
ds@1952
|
102 |
if (pSaveNative != NULL) |
|
ds@1952
|
103 |
{ |
|
ds@1952
|
104 |
for (cell_t i=0; i<=save_params[0]; i++) |
|
ds@1952
|
105 |
{ |
|
ds@1952
|
106 |
s_curparams[i] = save_params[i]; |
|
ds@1952
|
107 |
} |
|
ds@1952
|
108 |
} |
|
ds@1952
|
109 |
|
|
ds@1952
|
110 |
return result; |
|
ds@1952
|
111 |
} |
|
ds@1952
|
112 |
|
|
ds@1952
|
113 |
static cell_t CreateNative(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
114 |
{ |
|
ds@1952
|
115 |
char *name; |
|
dvander@2086
|
116 |
CPlugin *pPlugin; |
|
dvander@2086
|
117 |
|
|
ds@1952
|
118 |
pContext->LocalToString(params[1], &name); |
|
ds@1952
|
119 |
|
|
ds@1952
|
120 |
IPluginFunction *pFunction = pContext->GetFunctionById(params[2]); |
|
ds@1952
|
121 |
if (!pFunction) |
|
ds@1952
|
122 |
{ |
|
ds@1952
|
123 |
return pContext->ThrowNativeError("Function %x is not a valid function", params[2]); |
|
ds@1952
|
124 |
} |
|
ds@1952
|
125 |
|
|
dvander@2086
|
126 |
pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext()); |
|
dvander@2086
|
127 |
|
|
dvander@2086
|
128 |
if (!pPlugin->AddFakeNative(pFunction, name, FakeNativeRouter)) |
|
ds@1952
|
129 |
{ |
|
ds@1952
|
130 |
return pContext->ThrowNativeError("Fatal error creating dynamic native!"); |
|
ds@1952
|
131 |
} |
|
ds@1952
|
132 |
|
|
ds@1952
|
133 |
return 1; |
|
ds@1952
|
134 |
} |
|
ds@1952
|
135 |
|
|
ds@1952
|
136 |
static cell_t ThrowNativeError(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
137 |
{ |
|
ds@1952
|
138 |
if (!s_curnative || (s_curnative->ctx != pContext)) |
|
ds@1952
|
139 |
{ |
|
ds@1952
|
140 |
return pContext->ThrowNativeError("Not called from inside a native function"); |
|
ds@1952
|
141 |
} |
|
ds@1952
|
142 |
|
|
dvander@2042
|
143 |
g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); |
|
ds@1952
|
144 |
|
|
ds@1952
|
145 |
char buffer[512]; |
|
ds@1952
|
146 |
|
|
ds@1952
|
147 |
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2); |
|
ds@1952
|
148 |
|
|
ds@1952
|
149 |
if (pContext->GetContext()->n_err != SP_ERROR_NONE) |
|
ds@1952
|
150 |
{ |
|
ds@1952
|
151 |
s_curcaller->ThrowNativeError("Error encountered while processing a dynamic native"); |
|
ds@1952
|
152 |
} else { |
|
ds@1952
|
153 |
s_curcaller->ThrowNativeErrorEx(params[1], "%s", buffer); |
|
ds@1952
|
154 |
} |
|
ds@1952
|
155 |
|
|
ds@1952
|
156 |
return 0; |
|
ds@1952
|
157 |
} |
|
ds@1952
|
158 |
|
|
ds@1952
|
159 |
static cell_t GetNativeStringLength(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
160 |
{ |
|
ds@1952
|
161 |
if (!s_curnative || (s_curnative->ctx != pContext)) |
|
ds@1952
|
162 |
{ |
|
ds@1952
|
163 |
return pContext->ThrowNativeError("Not called from inside a native function"); |
|
ds@1952
|
164 |
} |
|
ds@1952
|
165 |
|
|
ds@1952
|
166 |
cell_t param = params[1]; |
|
ds@1952
|
167 |
if (param < 1 || param > s_curparams[0]) |
|
ds@1952
|
168 |
{ |
|
ds@1952
|
169 |
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param); |
|
ds@1952
|
170 |
} |
|
ds@1952
|
171 |
|
|
ds@1952
|
172 |
int err; |
|
ds@1952
|
173 |
char *str; |
|
ds@1952
|
174 |
if ((err=s_curcaller->LocalToString(s_curparams[param], &str)) != SP_ERROR_NONE) |
|
ds@1952
|
175 |
{ |
|
ds@1952
|
176 |
return err; |
|
ds@1952
|
177 |
} |
|
ds@1952
|
178 |
|
|
ds@1952
|
179 |
cell_t *addr; |
|
ds@1952
|
180 |
pContext->LocalToPhysAddr(params[2], &addr); |
|
ds@1952
|
181 |
*addr = (cell_t)strlen(str); |
|
ds@1952
|
182 |
|
|
ds@1952
|
183 |
return SP_ERROR_NONE; |
|
ds@1952
|
184 |
} |
|
ds@1952
|
185 |
|
|
ds@1952
|
186 |
static cell_t GetNativeString(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
187 |
{ |
|
ds@1952
|
188 |
if (!s_curnative || (s_curnative->ctx != pContext)) |
|
ds@1952
|
189 |
{ |
|
ds@1952
|
190 |
return pContext->ThrowNativeError("Not called from inside a native function"); |
|
ds@1952
|
191 |
} |
|
ds@1952
|
192 |
|
|
ds@1952
|
193 |
cell_t param = params[1]; |
|
ds@1952
|
194 |
if (param < 1 || param > s_curparams[0]) |
|
ds@1952
|
195 |
{ |
|
ds@1952
|
196 |
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param); |
|
ds@1952
|
197 |
} |
|
ds@1952
|
198 |
|
|
ds@1952
|
199 |
int err; |
|
ds@1952
|
200 |
char *str; |
|
ds@1952
|
201 |
if ((err=s_curcaller->LocalToString(s_curparams[param], &str)) != SP_ERROR_NONE) |
|
ds@1952
|
202 |
{ |
|
ds@1952
|
203 |
return err; |
|
ds@1952
|
204 |
} |
|
ds@1952
|
205 |
|
|
ds@1952
|
206 |
size_t bytes = 0; |
|
ds@1952
|
207 |
pContext->StringToLocalUTF8(params[2], params[3], str, &bytes); |
|
ds@1952
|
208 |
|
|
ds@1952
|
209 |
cell_t *addr; |
|
ds@1952
|
210 |
pContext->LocalToPhysAddr(params[4], &addr); |
|
ds@1952
|
211 |
*addr = (cell_t)bytes; |
|
ds@1952
|
212 |
|
|
ds@1952
|
213 |
return SP_ERROR_NONE; |
|
ds@1952
|
214 |
} |
|
ds@1952
|
215 |
|
|
ds@1952
|
216 |
static cell_t SetNativeString(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
217 |
{ |
|
ds@1952
|
218 |
if (!s_curnative || (s_curnative->ctx != pContext)) |
|
ds@1952
|
219 |
{ |
|
ds@1952
|
220 |
return pContext->ThrowNativeError("Not called from inside a native function"); |
|
ds@1952
|
221 |
} |
|
ds@1952
|
222 |
|
|
ds@1952
|
223 |
cell_t param = params[1]; |
|
ds@1952
|
224 |
if (param < 1 || param > s_curparams[0]) |
|
ds@1952
|
225 |
{ |
|
ds@1952
|
226 |
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param); |
|
ds@1952
|
227 |
} |
|
ds@1952
|
228 |
|
|
ds@1952
|
229 |
char *str; |
|
ds@1952
|
230 |
pContext->LocalToString(params[2], &str); |
|
ds@1952
|
231 |
|
|
ds@1952
|
232 |
int err; |
|
ds@1952
|
233 |
size_t bytes = 0; |
|
ds@1952
|
234 |
if (params[4]) |
|
ds@1952
|
235 |
{ |
|
ds@1952
|
236 |
err = s_curcaller->StringToLocalUTF8(s_curparams[param], params[3], str, &bytes); |
|
ds@1952
|
237 |
} else { |
|
ds@1952
|
238 |
err = s_curcaller->StringToLocal(s_curparams[param], params[3], str); |
|
ds@1952
|
239 |
/* Eww */ |
|
ds@1952
|
240 |
bytes = strlen(str); |
|
ds@1952
|
241 |
if (bytes >= (size_t)params[3]) |
|
ds@1952
|
242 |
{ |
|
ds@1952
|
243 |
bytes = params[3] - 1; |
|
ds@1952
|
244 |
} |
|
ds@1952
|
245 |
} |
|
ds@1952
|
246 |
|
|
ds@1952
|
247 |
if (err != SP_ERROR_NONE) |
|
ds@1952
|
248 |
{ |
|
ds@1952
|
249 |
return err; |
|
ds@1952
|
250 |
} |
|
ds@1952
|
251 |
|
|
ds@1952
|
252 |
cell_t *addr; |
|
ds@1952
|
253 |
pContext->LocalToPhysAddr(params[5], &addr); |
|
ds@1952
|
254 |
*addr = (cell_t)bytes; |
|
ds@1952
|
255 |
|
|
ds@1952
|
256 |
return SP_ERROR_NONE; |
|
ds@1952
|
257 |
} |
|
ds@1952
|
258 |
|
|
ds@1952
|
259 |
static cell_t GetNativeCell(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
260 |
{ |
|
ds@1952
|
261 |
if (!s_curnative || (s_curnative->ctx != pContext)) |
|
ds@1952
|
262 |
{ |
|
ds@1952
|
263 |
return pContext->ThrowNativeError("Not called from inside a native function"); |
|
ds@1952
|
264 |
} |
|
ds@1952
|
265 |
|
|
ds@1952
|
266 |
cell_t param = params[1]; |
|
ds@1952
|
267 |
if (param < 1 || param > s_curparams[0]) |
|
ds@1952
|
268 |
{ |
|
ds@1952
|
269 |
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param); |
|
ds@1952
|
270 |
} |
|
ds@1952
|
271 |
|
|
ds@1952
|
272 |
return s_curparams[param]; |
|
ds@1952
|
273 |
} |
|
ds@1952
|
274 |
|
|
ds@1952
|
275 |
static cell_t GetNativeCellRef(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
276 |
{ |
|
ds@1952
|
277 |
if (!s_curnative || (s_curnative->ctx != pContext)) |
|
ds@1952
|
278 |
{ |
|
ds@1952
|
279 |
return pContext->ThrowNativeError("Not called from inside a native function"); |
|
ds@1952
|
280 |
} |
|
ds@1952
|
281 |
|
|
ds@1952
|
282 |
cell_t param = params[1]; |
|
ds@1952
|
283 |
if (param < 1 || param > s_curparams[0]) |
|
ds@1952
|
284 |
{ |
|
ds@1952
|
285 |
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param); |
|
ds@1952
|
286 |
} |
|
ds@1952
|
287 |
|
|
ds@1952
|
288 |
cell_t *addr; |
|
ds@1952
|
289 |
if (s_curcaller->LocalToPhysAddr(s_curparams[param], &addr) != SP_ERROR_NONE) |
|
ds@1952
|
290 |
{ |
|
ds@1952
|
291 |
return s_curcaller->ThrowNativeErrorEx(SP_ERROR_INVALID_ADDRESS, "Invalid address value"); |
|
ds@1952
|
292 |
} |
|
ds@1952
|
293 |
|
|
ds@1952
|
294 |
return *addr; |
|
ds@1952
|
295 |
} |
|
ds@1952
|
296 |
|
|
ds@1952
|
297 |
static cell_t SetNativeCellRef(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
298 |
{ |
|
ds@1952
|
299 |
if (!s_curnative || (s_curnative->ctx != pContext)) |
|
ds@1952
|
300 |
{ |
|
ds@1952
|
301 |
return pContext->ThrowNativeError("Not called from inside a native function"); |
|
ds@1952
|
302 |
} |
|
ds@1952
|
303 |
|
|
ds@1952
|
304 |
cell_t param = params[1]; |
|
ds@1952
|
305 |
if (param < 1 || param > s_curparams[0]) |
|
ds@1952
|
306 |
{ |
|
ds@1952
|
307 |
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param); |
|
ds@1952
|
308 |
} |
|
ds@1952
|
309 |
|
|
ds@1952
|
310 |
cell_t *addr; |
|
ds@1952
|
311 |
if (s_curcaller->LocalToPhysAddr(s_curparams[param], &addr) != SP_ERROR_NONE) |
|
ds@1952
|
312 |
{ |
|
ds@1952
|
313 |
return s_curcaller->ThrowNativeErrorEx(SP_ERROR_INVALID_ADDRESS, "Invalid address value"); |
|
ds@1952
|
314 |
} |
|
ds@1952
|
315 |
|
|
ds@1952
|
316 |
*addr = params[2]; |
|
ds@1952
|
317 |
|
|
ds@1952
|
318 |
return 1; |
|
ds@1952
|
319 |
} |
|
ds@1952
|
320 |
|
|
ds@1952
|
321 |
static cell_t GetNativeArray(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
322 |
{ |
|
ds@1952
|
323 |
if (!s_curnative || (s_curnative->ctx != pContext)) |
|
ds@1952
|
324 |
{ |
|
ds@1952
|
325 |
return pContext->ThrowNativeError("Not called from inside a native function"); |
|
ds@1952
|
326 |
} |
|
ds@1952
|
327 |
|
|
ds@1952
|
328 |
cell_t param = params[1]; |
|
ds@1952
|
329 |
if (param < 1 || param > s_curparams[0]) |
|
ds@1952
|
330 |
{ |
|
ds@1952
|
331 |
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param); |
|
ds@1952
|
332 |
} |
|
ds@1952
|
333 |
|
|
ds@1952
|
334 |
int err; |
|
ds@1952
|
335 |
cell_t *addr; |
|
ds@1952
|
336 |
if ((err=s_curcaller->LocalToPhysAddr(s_curparams[param], &addr)) != SP_ERROR_NONE) |
|
ds@1952
|
337 |
{ |
|
ds@1952
|
338 |
return err; |
|
ds@1952
|
339 |
} |
|
ds@1952
|
340 |
|
|
ds@1952
|
341 |
cell_t *src; |
|
ds@1952
|
342 |
pContext->LocalToPhysAddr(params[2], &src); |
|
ds@1952
|
343 |
|
|
ds@1952
|
344 |
memcpy(src, addr, sizeof(cell_t) * params[3]); |
|
ds@1952
|
345 |
|
|
ds@1952
|
346 |
return SP_ERROR_NONE; |
|
ds@1952
|
347 |
} |
|
ds@1952
|
348 |
|
|
ds@1952
|
349 |
static cell_t SetNativeArray(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
350 |
{ |
|
ds@1952
|
351 |
if (!s_curnative || (s_curnative->ctx != pContext)) |
|
ds@1952
|
352 |
{ |
|
ds@1952
|
353 |
return pContext->ThrowNativeError("Not called from inside a native function"); |
|
ds@1952
|
354 |
} |
|
ds@1952
|
355 |
|
|
ds@1952
|
356 |
cell_t param = params[1]; |
|
ds@1952
|
357 |
if (param < 1 || param > s_curparams[0]) |
|
ds@1952
|
358 |
{ |
|
ds@1952
|
359 |
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param); |
|
ds@1952
|
360 |
} |
|
ds@1952
|
361 |
|
|
ds@1952
|
362 |
int err; |
|
ds@1952
|
363 |
cell_t *addr; |
|
ds@1952
|
364 |
if ((err=s_curcaller->LocalToPhysAddr(s_curparams[param], &addr)) != SP_ERROR_NONE) |
|
ds@1952
|
365 |
{ |
|
ds@1952
|
366 |
return err; |
|
ds@1952
|
367 |
} |
|
ds@1952
|
368 |
|
|
ds@1952
|
369 |
cell_t *src; |
|
ds@1952
|
370 |
pContext->LocalToPhysAddr(params[2], &src); |
|
ds@1952
|
371 |
|
|
ds@1952
|
372 |
memcpy(addr, src, sizeof(cell_t) * params[3]); |
|
ds@1952
|
373 |
|
|
ds@1952
|
374 |
return SP_ERROR_NONE; |
|
ds@1952
|
375 |
} |
|
ds@1952
|
376 |
|
|
ds@1952
|
377 |
static cell_t FormatNativeString(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
378 |
{ |
|
ds@1952
|
379 |
if (!s_curnative || (s_curnative->ctx != pContext)) |
|
ds@1952
|
380 |
{ |
|
ds@1952
|
381 |
return pContext->ThrowNativeError("Not called from inside a native function"); |
|
ds@1952
|
382 |
} |
|
ds@1952
|
383 |
|
|
ds@1952
|
384 |
int out_param = params[1]; |
|
ds@1952
|
385 |
int fmt_param = params[2]; |
|
ds@1952
|
386 |
int var_param = params[3]; |
|
ds@1952
|
387 |
|
|
ds@1952
|
388 |
/* Validate input */ |
|
ds@1952
|
389 |
if (out_param && (out_param < 1 || out_param > s_curparams[0])) |
|
ds@1952
|
390 |
{ |
|
ds@1952
|
391 |
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", out_param); |
|
ds@1952
|
392 |
} |
|
ds@1952
|
393 |
if (fmt_param && (fmt_param < 1 || fmt_param > s_curparams[0])) |
|
ds@1952
|
394 |
{ |
|
ds@1952
|
395 |
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", fmt_param); |
|
ds@1952
|
396 |
} |
|
ds@1952
|
397 |
if (var_param && (var_param < 1 || var_param > s_curparams[0] + 1)) |
|
ds@1952
|
398 |
{ |
|
ds@1952
|
399 |
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", fmt_param); |
|
ds@1952
|
400 |
} |
|
ds@1952
|
401 |
|
|
ds@1952
|
402 |
/* Get buffer information */ |
|
ds@1952
|
403 |
int err; |
|
ds@1952
|
404 |
char *output_buffer; |
|
ds@1952
|
405 |
char *format_buffer; |
|
ds@1952
|
406 |
|
|
ds@1952
|
407 |
if (out_param) |
|
ds@1952
|
408 |
{ |
|
ds@1952
|
409 |
if ((err=s_curcaller->LocalToString(s_curparams[out_param], &output_buffer)) != SP_ERROR_NONE) |
|
ds@1952
|
410 |
{ |
|
ds@1952
|
411 |
return err; |
|
ds@1952
|
412 |
} |
|
ds@1952
|
413 |
} else { |
|
ds@1952
|
414 |
pContext->LocalToString(params[6], &output_buffer); |
|
ds@1952
|
415 |
} |
|
ds@1952
|
416 |
|
|
ds@1952
|
417 |
if (fmt_param) |
|
ds@1952
|
418 |
{ |
|
ds@1952
|
419 |
if ((err=s_curcaller->LocalToString(s_curparams[fmt_param], &format_buffer)) != SP_ERROR_NONE) |
|
ds@1952
|
420 |
{ |
|
ds@1952
|
421 |
return err; |
|
ds@1952
|
422 |
} |
|
ds@1952
|
423 |
} else { |
|
ds@1952
|
424 |
pContext->LocalToString(params[7], &format_buffer); |
|
ds@1952
|
425 |
} |
|
ds@1952
|
426 |
|
|
ds@1952
|
427 |
/* Get maximum length */ |
|
ds@1952
|
428 |
size_t maxlen = (size_t)params[4]; |
|
ds@1952
|
429 |
|
|
ds@1952
|
430 |
/* Do the format */ |
|
ds@1952
|
431 |
size_t written = atcprintf(output_buffer, maxlen, format_buffer, s_curcaller, s_curparams, &var_param); |
|
ds@1952
|
432 |
|
|
ds@1952
|
433 |
cell_t *addr; |
|
ds@1952
|
434 |
pContext->LocalToPhysAddr(params[5], &addr); |
|
ds@1952
|
435 |
*addr = (cell_t)written; |
|
ds@1952
|
436 |
|
|
ds@1952
|
437 |
return s_curcaller->GetContext()->n_err; |
|
ds@1952
|
438 |
} |
|
ds@1952
|
439 |
|
|
ds@1952
|
440 |
//tee hee |
|
ds@1952
|
441 |
REGISTER_NATIVES(nativeNatives) |
|
ds@1952
|
442 |
{ |
|
ds@1952
|
443 |
{"CreateNative", CreateNative}, |
|
ds@1952
|
444 |
{"GetNativeArray", GetNativeArray}, |
|
ds@1952
|
445 |
{"GetNativeCell", GetNativeCell}, |
|
ds@1952
|
446 |
{"GetNativeCellRef", GetNativeCellRef}, |
|
ds@1952
|
447 |
{"GetNativeString", GetNativeString}, |
|
ds@1952
|
448 |
{"GetNativeStringLength", GetNativeStringLength}, |
|
ds@1952
|
449 |
{"FormatNativeString", FormatNativeString}, |
|
ds@1952
|
450 |
{"ThrowNativeError", ThrowNativeError}, |
|
ds@1952
|
451 |
{"SetNativeArray", SetNativeArray}, |
|
ds@1952
|
452 |
{"SetNativeCellRef", SetNativeCellRef}, |
|
ds@1952
|
453 |
{"SetNativeString", SetNativeString}, |
|
ds@1952
|
454 |
{NULL, NULL}, |
|
ds@1952
|
455 |
}; |