3 * =============================================================================
5 * Copyright (C) 2004-2008 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;
59 bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
61 CellArray *pArray = (CellArray *)object;
62 *pSize = sizeof(CellArray) + pArray->mem_usage();
67 static cell_t CreateArray(IPluginContext *pContext, const cell_t *params)
71 return pContext->ThrowNativeError("Invalid block size (must be > 0)");
74 CellArray *array = new CellArray(params[1]);
78 array->resize(params[2]);
81 Handle_t hndl = g_HandleSys.CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
90 static cell_t ClearArray(IPluginContext *pContext, const cell_t *params)
94 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
96 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
99 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
107 static cell_t ResizeArray(IPluginContext *pContext, const cell_t *params)
111 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
113 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
116 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
119 if (!array->resize(params[2]))
121 return pContext->ThrowNativeError("Unable to resize array to \"%u\"", params[2]);
127 static cell_t GetArraySize(IPluginContext *pContext, const cell_t *params)
131 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
133 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
136 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
139 return (cell_t)array->size();
142 static cell_t PushArrayCell(IPluginContext *pContext, const cell_t *params)
146 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
148 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
151 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
154 cell_t *blk = array->push();
157 return pContext->ThrowNativeError("Failed to grow array");
162 return (cell_t)(array->size() - 1);
165 static cell_t PushArrayString(IPluginContext *pContext, const cell_t *params)
169 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
171 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
174 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
177 cell_t *blk = array->push();
180 return pContext->ThrowNativeError("Failed to grow array");
184 pContext->LocalToString(params[2], &str);
186 strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
188 return (cell_t)(array->size() - 1);
191 static cell_t PushArrayArray(IPluginContext *pContext, const cell_t *params)
195 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
197 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
200 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
203 cell_t *blk = array->push();
206 return pContext->ThrowNativeError("Failed to grow array");
210 pContext->LocalToPhysAddr(params[2], &addr);
212 size_t indexes = array->blocksize();
213 if (params[3] != -1 && (size_t)params[3] <= array->blocksize())
218 memcpy(blk, addr, sizeof(cell_t) * indexes);
220 return (cell_t)(array->size() - 1);
223 static cell_t GetArrayCell(IPluginContext *pContext, const cell_t *params)
227 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
229 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
232 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
235 size_t idx = (size_t)params[2];
236 if (idx >= array->size())
238 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
241 cell_t *blk = array->at(idx);
243 idx = (size_t)params[3];
246 if (idx >= array->blocksize())
248 return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
252 if (idx >= array->blocksize() * 4)
254 return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
256 return (cell_t)*((char *)blk + idx);
262 static cell_t GetArrayString(IPluginContext *pContext, const cell_t *params)
266 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
268 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
271 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
274 size_t idx = (size_t)params[2];
275 if (idx >= array->size())
277 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
280 cell_t *blk = array->at(idx);
281 size_t numWritten = 0;
283 pContext->StringToLocalUTF8(params[3], params[4], (char *)blk, &numWritten);
288 static cell_t GetArrayArray(IPluginContext *pContext, const cell_t *params)
292 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
294 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
297 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
300 size_t idx = (size_t)params[2];
301 if (idx >= array->size())
303 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
306 cell_t *blk = array->at(idx);
307 size_t indexes = array->blocksize();
308 if (params[4] != -1 && (size_t)params[4] <= array->blocksize())
314 pContext->LocalToPhysAddr(params[3], &addr);
316 memcpy(addr, blk, sizeof(cell_t) * indexes);
321 static cell_t SetArrayCell(IPluginContext *pContext, const cell_t *params)
325 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
327 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
330 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
333 size_t idx = (size_t)params[2];
334 if (idx >= array->size())
336 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
339 cell_t *blk = array->at(idx);
341 idx = (size_t)params[4];
344 if (idx >= array->blocksize())
346 return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
348 blk[idx] = params[3];
350 if (idx >= array->blocksize() * 4)
352 return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
354 *((char *)blk + idx) = (char)params[3];
360 static cell_t SetArrayString(IPluginContext *pContext, const cell_t *params)
364 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
366 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
369 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
372 size_t idx = (size_t)params[2];
373 if (idx >= array->size())
375 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
378 cell_t *blk = array->at(idx);
381 pContext->LocalToString(params[3], &str);
383 return strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
386 static cell_t SetArrayArray(IPluginContext *pContext, const cell_t *params)
390 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
392 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
395 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
398 size_t idx = (size_t)params[2];
399 if (idx >= array->size())
401 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
404 cell_t *blk = array->at(idx);
405 size_t indexes = array->blocksize();
406 if (params[4] != -1 && (size_t)params[4] <= array->blocksize())
412 pContext->LocalToPhysAddr(params[3], &addr);
414 memcpy(blk, addr, sizeof(cell_t) * indexes);
419 static cell_t ShiftArrayUp(IPluginContext *pContext, const cell_t *params)
423 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
425 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
428 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
431 size_t idx = (size_t)params[2];
432 if (idx >= array->size())
434 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
437 array->insert_at(idx);
442 static cell_t RemoveFromArray(IPluginContext *pContext, const cell_t *params)
446 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
448 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
451 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
454 size_t idx = (size_t)params[2];
455 if (idx >= array->size())
457 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
465 static cell_t SwapArrayItems(IPluginContext *pContext, const cell_t *params)
469 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
471 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
474 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
477 size_t idx1 = (size_t)params[2];
478 size_t idx2 = (size_t)params[3];
479 if (idx1 >= array->size())
481 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx1, array->size());
483 if (idx2 >= array->size())
485 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx2, array->size());
488 array->swap(idx1, idx2);
493 static cell_t CloneArray(IPluginContext *pContext, const cell_t *params)
497 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
499 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&oldArray))
502 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
505 CellArray *array = oldArray->clone();
507 Handle_t hndl = g_HandleSys.CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
516 static cell_t FindStringInArray(IPluginContext *pContext, const cell_t *params)
520 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
522 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
525 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
529 pContext->LocalToString(params[2], &str);
531 for (unsigned int i = 0; i < array->size(); i++)
533 const char *array_str = (const char *)array->at(i);
534 if (strcmp(str, array_str) == 0)
543 static cell_t FindValueInArray(IPluginContext *pContext, const cell_t *params)
547 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
549 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
552 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
555 for (unsigned int i = 0; i < array->size(); i++)
557 if (params[2] == *array->at(i))
566 REGISTER_NATIVES(cellArrayNatives)
568 {"ClearArray", ClearArray},
569 {"CreateArray", CreateArray},
570 {"GetArrayArray", GetArrayArray},
571 {"GetArrayCell", GetArrayCell},
572 {"GetArraySize", GetArraySize},
573 {"GetArrayString", GetArrayString},
574 {"ResizeArray", ResizeArray},
575 {"PushArrayArray", PushArrayArray},
576 {"PushArrayCell", PushArrayCell},
577 {"PushArrayString", PushArrayString},
578 {"RemoveFromArray", RemoveFromArray},
579 {"SetArrayCell", SetArrayCell},
580 {"SetArrayString", SetArrayString},
581 {"SetArrayArray", SetArrayArray},
582 {"ShiftArrayUp", ShiftArrayUp},
583 {"SwapArrayItems", SwapArrayItems},
584 {"CloneArray", CloneArray},
585 {"FindStringInArray", FindStringInArray},
586 {"FindValueInArray", FindValueInArray},