atcprintf() is now fully re-entrant, meaning translations can be used from inside translations
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>.
33 #include "sm_globals.h"
34 #include "sm_stringutil.h"
35 #include "CellArray.h"
36 #include "HandleSys.h"
38 HandleType_t htCellArray;
40 class CellArrayHelpers :
42 public IHandleTypeDispatch
44 public: //SMGlobalClass
45 void OnSourceModAllInitialized()
47 htCellArray = g_HandleSys.CreateType("CellArray", this, 0, NULL, NULL, g_pCoreIdent, NULL);
49 void OnSourceModShutdown()
51 g_HandleSys.RemoveType(htCellArray, g_pCoreIdent);
53 public: //IHandleTypeDispatch
54 void OnHandleDestroy(HandleType_t type, void *object)
56 CellArray *array = (CellArray *)object;
61 static cell_t CreateArray(IPluginContext *pContext, const cell_t *params)
65 return pContext->ThrowNativeError("Invalid block size (must be > 0)");
68 CellArray *array = new CellArray(params[1]);
72 array->resize(params[2]);
75 Handle_t hndl = g_HandleSys.CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
84 static cell_t ClearArray(IPluginContext *pContext, const cell_t *params)
88 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
90 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
93 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
101 static cell_t ResizeArray(IPluginContext *pContext, const cell_t *params)
105 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
107 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
110 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
113 if (!array->resize(params[2]))
115 return pContext->ThrowNativeError("Unable to resize array to \"%u\"", params[2]);
121 static cell_t GetArraySize(IPluginContext *pContext, const cell_t *params)
125 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
127 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
130 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
133 return (cell_t)array->size();
136 static cell_t PushArrayCell(IPluginContext *pContext, const cell_t *params)
140 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
142 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
145 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
148 cell_t *blk = array->push();
151 return pContext->ThrowNativeError("Failed to grow array");
156 return (cell_t)(array->size() - 1);
159 static cell_t PushArrayString(IPluginContext *pContext, const cell_t *params)
163 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
165 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
168 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
171 cell_t *blk = array->push();
174 return pContext->ThrowNativeError("Failed to grow array");
178 pContext->LocalToString(params[2], &str);
180 strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
182 return (cell_t)(array->size() - 1);
185 static cell_t PushArrayArray(IPluginContext *pContext, const cell_t *params)
189 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
191 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
194 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
197 cell_t *blk = array->push();
200 return pContext->ThrowNativeError("Failed to grow array");
204 pContext->LocalToPhysAddr(params[2], &addr);
206 size_t indexes = array->blocksize();
207 if (params[3] != -1 && (size_t)params[3] <= array->blocksize())
212 memcpy(blk, addr, sizeof(cell_t) * indexes);
214 return (cell_t)(array->size() - 1);
217 static cell_t GetArrayCell(IPluginContext *pContext, const cell_t *params)
221 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
223 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
226 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
229 size_t idx = (size_t)params[2];
230 if (idx >= array->size())
232 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
235 cell_t *blk = array->at(idx);
237 idx = (size_t)params[3];
240 if (idx >= array->blocksize())
242 return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
246 if (idx >= array->blocksize() * 4)
248 return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
250 return (cell_t)*((char *)blk + idx);
256 static cell_t GetArrayString(IPluginContext *pContext, const cell_t *params)
260 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
262 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
265 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
268 size_t idx = (size_t)params[2];
269 if (idx >= array->size())
271 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
274 cell_t *blk = array->at(idx);
275 size_t numWritten = 0;
277 pContext->StringToLocalUTF8(params[3], params[4], (char *)blk, &numWritten);
282 static cell_t GetArrayArray(IPluginContext *pContext, const cell_t *params)
286 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
288 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
291 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
294 size_t idx = (size_t)params[2];
295 if (idx >= array->size())
297 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
300 cell_t *blk = array->at(idx);
301 size_t indexes = array->blocksize();
302 if (params[4] != -1 && (size_t)params[4] <= array->blocksize())
308 pContext->LocalToPhysAddr(params[3], &addr);
310 memcpy(addr, blk, sizeof(cell_t) * indexes);
315 static cell_t SetArrayCell(IPluginContext *pContext, const cell_t *params)
319 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
321 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
324 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
327 size_t idx = (size_t)params[2];
328 if (idx >= array->size())
330 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
333 cell_t *blk = array->at(idx);
335 idx = (size_t)params[4];
338 if (idx >= array->blocksize())
340 return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
342 blk[idx] = params[3];
344 if (idx >= array->blocksize() * 4)
346 return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
348 *((char *)blk + idx) = (char)params[3];
354 static cell_t SetArrayString(IPluginContext *pContext, const cell_t *params)
358 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
360 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
363 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
366 size_t idx = (size_t)params[2];
367 if (idx >= array->size())
369 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
372 cell_t *blk = array->at(idx);
375 pContext->LocalToString(params[3], &str);
377 return strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
380 static cell_t SetArrayArray(IPluginContext *pContext, const cell_t *params)
384 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
386 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
389 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
392 size_t idx = (size_t)params[2];
393 if (idx >= array->size())
395 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
398 cell_t *blk = array->at(idx);
399 size_t indexes = array->blocksize();
400 if (params[4] != -1 && (size_t)params[4] <= array->blocksize())
406 pContext->LocalToPhysAddr(params[3], &addr);
408 memcpy(blk, addr, sizeof(cell_t) * indexes);
413 static cell_t ShiftArrayUp(IPluginContext *pContext, const cell_t *params)
417 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
419 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
422 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
425 size_t idx = (size_t)params[2];
426 if (idx >= array->size())
428 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
431 array->insert_at(idx);
436 static cell_t RemoveFromArray(IPluginContext *pContext, const cell_t *params)
440 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
442 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
445 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
448 size_t idx = (size_t)params[2];
449 if (idx >= array->size())
451 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
459 static cell_t SwapArrayItems(IPluginContext *pContext, const cell_t *params)
463 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
465 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
468 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
471 size_t idx1 = (size_t)params[2];
472 size_t idx2 = (size_t)params[3];
473 if (idx1 >= array->size())
475 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx1, array->size());
477 if (idx2 >= array->size())
479 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx2, array->size());
482 array->swap(idx1, idx2);
487 REGISTER_NATIVES(cellArrayNatives)
489 {"ClearArray", ClearArray},
490 {"CreateArray", CreateArray},
491 {"GetArrayArray", GetArrayArray},
492 {"GetArrayCell", GetArrayCell},
493 {"GetArraySize", GetArraySize},
494 {"GetArrayString", GetArrayString},
495 {"ResizeArray", ResizeArray},
496 {"PushArrayArray", PushArrayArray},
497 {"PushArrayCell", PushArrayCell},
498 {"PushArrayString", PushArrayString},
499 {"RemoveFromArray", RemoveFromArray},
500 {"SetArrayCell", SetArrayCell},
501 {"SetArrayString", SetArrayString},
502 {"SetArrayArray", SetArrayArray},
503 {"ShiftArrayUp", ShiftArrayUp},
504 {"SwapArrayItems", SwapArrayItems},