1.1 --- a/core/smn_adt_array.cpp Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,582 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod
1.8 - * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include <malloc.h>
1.36 -#include "sm_globals.h"
1.37 -#include "sm_stringutil.h"
1.38 -#include "CellArray.h"
1.39 -#include "HandleSys.h"
1.40 -
1.41 -HandleType_t htCellArray;
1.42 -
1.43 -class CellArrayHelpers :
1.44 - public SMGlobalClass,
1.45 - public IHandleTypeDispatch
1.46 -{
1.47 -public: //SMGlobalClass
1.48 - void OnSourceModAllInitialized()
1.49 - {
1.50 - htCellArray = g_HandleSys.CreateType("CellArray", this, 0, NULL, NULL, g_pCoreIdent, NULL);
1.51 - }
1.52 - void OnSourceModShutdown()
1.53 - {
1.54 - g_HandleSys.RemoveType(htCellArray, g_pCoreIdent);
1.55 - }
1.56 -public: //IHandleTypeDispatch
1.57 - void OnHandleDestroy(HandleType_t type, void *object)
1.58 - {
1.59 - CellArray *array = (CellArray *)object;
1.60 - delete array;
1.61 - }
1.62 -} s_CellArrayHelpers;
1.63 -
1.64 -static cell_t CreateArray(IPluginContext *pContext, const cell_t *params)
1.65 -{
1.66 - if (!params[1])
1.67 - {
1.68 - return pContext->ThrowNativeError("Invalid block size (must be > 0)");
1.69 - }
1.70 -
1.71 - CellArray *array = new CellArray(params[1]);
1.72 -
1.73 - if (params[2])
1.74 - {
1.75 - array->resize(params[2]);
1.76 - }
1.77 -
1.78 - Handle_t hndl = g_HandleSys.CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
1.79 - if (!hndl)
1.80 - {
1.81 - delete array;
1.82 - }
1.83 -
1.84 - return hndl;
1.85 -}
1.86 -
1.87 -static cell_t ClearArray(IPluginContext *pContext, const cell_t *params)
1.88 -{
1.89 - CellArray *array;
1.90 - HandleError err;
1.91 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.92 -
1.93 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.94 - != HandleError_None)
1.95 - {
1.96 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.97 - }
1.98 -
1.99 - array->clear();
1.100 -
1.101 - return 1;
1.102 -}
1.103 -
1.104 -static cell_t ResizeArray(IPluginContext *pContext, const cell_t *params)
1.105 -{
1.106 - CellArray *array;
1.107 - HandleError err;
1.108 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.109 -
1.110 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.111 - != HandleError_None)
1.112 - {
1.113 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.114 - }
1.115 -
1.116 - if (!array->resize(params[2]))
1.117 - {
1.118 - return pContext->ThrowNativeError("Unable to resize array to \"%u\"", params[2]);
1.119 - }
1.120 -
1.121 - return 1;
1.122 -}
1.123 -
1.124 -static cell_t GetArraySize(IPluginContext *pContext, const cell_t *params)
1.125 -{
1.126 - CellArray *array;
1.127 - HandleError err;
1.128 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.129 -
1.130 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.131 - != HandleError_None)
1.132 - {
1.133 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.134 - }
1.135 -
1.136 - return (cell_t)array->size();
1.137 -}
1.138 -
1.139 -static cell_t PushArrayCell(IPluginContext *pContext, const cell_t *params)
1.140 -{
1.141 - CellArray *array;
1.142 - HandleError err;
1.143 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.144 -
1.145 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.146 - != HandleError_None)
1.147 - {
1.148 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.149 - }
1.150 -
1.151 - cell_t *blk = array->push();
1.152 - if (!blk)
1.153 - {
1.154 - return pContext->ThrowNativeError("Failed to grow array");
1.155 - }
1.156 -
1.157 - *blk = params[2];
1.158 -
1.159 - return (cell_t)(array->size() - 1);
1.160 -}
1.161 -
1.162 -static cell_t PushArrayString(IPluginContext *pContext, const cell_t *params)
1.163 -{
1.164 - CellArray *array;
1.165 - HandleError err;
1.166 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.167 -
1.168 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.169 - != HandleError_None)
1.170 - {
1.171 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.172 - }
1.173 -
1.174 - cell_t *blk = array->push();
1.175 - if (!blk)
1.176 - {
1.177 - return pContext->ThrowNativeError("Failed to grow array");
1.178 - }
1.179 -
1.180 - char *str;
1.181 - pContext->LocalToString(params[2], &str);
1.182 -
1.183 - strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
1.184 -
1.185 - return (cell_t)(array->size() - 1);
1.186 -}
1.187 -
1.188 -static cell_t PushArrayArray(IPluginContext *pContext, const cell_t *params)
1.189 -{
1.190 - CellArray *array;
1.191 - HandleError err;
1.192 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.193 -
1.194 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.195 - != HandleError_None)
1.196 - {
1.197 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.198 - }
1.199 -
1.200 - cell_t *blk = array->push();
1.201 - if (!blk)
1.202 - {
1.203 - return pContext->ThrowNativeError("Failed to grow array");
1.204 - }
1.205 -
1.206 - cell_t *addr;
1.207 - pContext->LocalToPhysAddr(params[2], &addr);
1.208 -
1.209 - size_t indexes = array->blocksize();
1.210 - if (params[3] != -1 && (size_t)params[3] <= array->blocksize())
1.211 - {
1.212 - indexes = params[3];
1.213 - }
1.214 -
1.215 - memcpy(blk, addr, sizeof(cell_t) * indexes);
1.216 -
1.217 - return (cell_t)(array->size() - 1);
1.218 -}
1.219 -
1.220 -static cell_t GetArrayCell(IPluginContext *pContext, const cell_t *params)
1.221 -{
1.222 - CellArray *array;
1.223 - HandleError err;
1.224 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.225 -
1.226 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.227 - != HandleError_None)
1.228 - {
1.229 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.230 - }
1.231 -
1.232 - size_t idx = (size_t)params[2];
1.233 - if (idx >= array->size())
1.234 - {
1.235 - return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
1.236 - }
1.237 -
1.238 - cell_t *blk = array->at(idx);
1.239 -
1.240 - idx = (size_t)params[3];
1.241 - if (params[4] == 0)
1.242 - {
1.243 - if (idx >= array->blocksize())
1.244 - {
1.245 - return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
1.246 - }
1.247 - return blk[idx];
1.248 - } else {
1.249 - if (idx >= array->blocksize() * 4)
1.250 - {
1.251 - return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
1.252 - }
1.253 - return (cell_t)*((char *)blk + idx);
1.254 - }
1.255 -
1.256 - return 0;
1.257 -}
1.258 -
1.259 -static cell_t GetArrayString(IPluginContext *pContext, const cell_t *params)
1.260 -{
1.261 - CellArray *array;
1.262 - HandleError err;
1.263 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.264 -
1.265 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.266 - != HandleError_None)
1.267 - {
1.268 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.269 - }
1.270 -
1.271 - size_t idx = (size_t)params[2];
1.272 - if (idx >= array->size())
1.273 - {
1.274 - return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
1.275 - }
1.276 -
1.277 - cell_t *blk = array->at(idx);
1.278 - size_t numWritten = 0;
1.279 -
1.280 - pContext->StringToLocalUTF8(params[3], params[4], (char *)blk, &numWritten);
1.281 -
1.282 - return numWritten;
1.283 -}
1.284 -
1.285 -static cell_t GetArrayArray(IPluginContext *pContext, const cell_t *params)
1.286 -{
1.287 - CellArray *array;
1.288 - HandleError err;
1.289 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.290 -
1.291 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.292 - != HandleError_None)
1.293 - {
1.294 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.295 - }
1.296 -
1.297 - size_t idx = (size_t)params[2];
1.298 - if (idx >= array->size())
1.299 - {
1.300 - return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
1.301 - }
1.302 -
1.303 - cell_t *blk = array->at(idx);
1.304 - size_t indexes = array->blocksize();
1.305 - if (params[4] != -1 && (size_t)params[4] <= array->blocksize())
1.306 - {
1.307 - indexes = params[4];
1.308 - }
1.309 -
1.310 - cell_t *addr;
1.311 - pContext->LocalToPhysAddr(params[3], &addr);
1.312 -
1.313 - memcpy(addr, blk, sizeof(cell_t) * indexes);
1.314 -
1.315 - return indexes;
1.316 -}
1.317 -
1.318 -static cell_t SetArrayCell(IPluginContext *pContext, const cell_t *params)
1.319 -{
1.320 - CellArray *array;
1.321 - HandleError err;
1.322 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.323 -
1.324 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.325 - != HandleError_None)
1.326 - {
1.327 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.328 - }
1.329 -
1.330 - size_t idx = (size_t)params[2];
1.331 - if (idx >= array->size())
1.332 - {
1.333 - return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
1.334 - }
1.335 -
1.336 - cell_t *blk = array->at(idx);
1.337 -
1.338 - idx = (size_t)params[4];
1.339 - if (params[5] == 0)
1.340 - {
1.341 - if (idx >= array->blocksize())
1.342 - {
1.343 - return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
1.344 - }
1.345 - blk[idx] = params[3];
1.346 - } else {
1.347 - if (idx >= array->blocksize() * 4)
1.348 - {
1.349 - return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
1.350 - }
1.351 - *((char *)blk + idx) = (char)params[3];
1.352 - }
1.353 -
1.354 - return 1;
1.355 -}
1.356 -
1.357 -static cell_t SetArrayString(IPluginContext *pContext, const cell_t *params)
1.358 -{
1.359 - CellArray *array;
1.360 - HandleError err;
1.361 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.362 -
1.363 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.364 - != HandleError_None)
1.365 - {
1.366 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.367 - }
1.368 -
1.369 - size_t idx = (size_t)params[2];
1.370 - if (idx >= array->size())
1.371 - {
1.372 - return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
1.373 - }
1.374 -
1.375 - cell_t *blk = array->at(idx);
1.376 -
1.377 - char *str;
1.378 - pContext->LocalToString(params[3], &str);
1.379 -
1.380 - return strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
1.381 -}
1.382 -
1.383 -static cell_t SetArrayArray(IPluginContext *pContext, const cell_t *params)
1.384 -{
1.385 - CellArray *array;
1.386 - HandleError err;
1.387 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.388 -
1.389 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.390 - != HandleError_None)
1.391 - {
1.392 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.393 - }
1.394 -
1.395 - size_t idx = (size_t)params[2];
1.396 - if (idx >= array->size())
1.397 - {
1.398 - return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
1.399 - }
1.400 -
1.401 - cell_t *blk = array->at(idx);
1.402 - size_t indexes = array->blocksize();
1.403 - if (params[4] != -1 && (size_t)params[4] <= array->blocksize())
1.404 - {
1.405 - indexes = params[4];
1.406 - }
1.407 -
1.408 - cell_t *addr;
1.409 - pContext->LocalToPhysAddr(params[3], &addr);
1.410 -
1.411 - memcpy(blk, addr, sizeof(cell_t) * indexes);
1.412 -
1.413 - return indexes;
1.414 -}
1.415 -
1.416 -static cell_t ShiftArrayUp(IPluginContext *pContext, const cell_t *params)
1.417 -{
1.418 - CellArray *array;
1.419 - HandleError err;
1.420 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.421 -
1.422 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.423 - != HandleError_None)
1.424 - {
1.425 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.426 - }
1.427 -
1.428 - size_t idx = (size_t)params[2];
1.429 - if (idx >= array->size())
1.430 - {
1.431 - return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
1.432 - }
1.433 -
1.434 - array->insert_at(idx);
1.435 -
1.436 - return 1;
1.437 -}
1.438 -
1.439 -static cell_t RemoveFromArray(IPluginContext *pContext, const cell_t *params)
1.440 -{
1.441 - CellArray *array;
1.442 - HandleError err;
1.443 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.444 -
1.445 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.446 - != HandleError_None)
1.447 - {
1.448 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.449 - }
1.450 -
1.451 - size_t idx = (size_t)params[2];
1.452 - if (idx >= array->size())
1.453 - {
1.454 - return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
1.455 - }
1.456 -
1.457 - array->remove(idx);
1.458 -
1.459 - return 1;
1.460 -}
1.461 -
1.462 -static cell_t SwapArrayItems(IPluginContext *pContext, const cell_t *params)
1.463 -{
1.464 - CellArray *array;
1.465 - HandleError err;
1.466 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.467 -
1.468 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.469 - != HandleError_None)
1.470 - {
1.471 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.472 - }
1.473 -
1.474 - size_t idx1 = (size_t)params[2];
1.475 - size_t idx2 = (size_t)params[3];
1.476 - if (idx1 >= array->size())
1.477 - {
1.478 - return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx1, array->size());
1.479 - }
1.480 - if (idx2 >= array->size())
1.481 - {
1.482 - return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx2, array->size());
1.483 - }
1.484 -
1.485 - array->swap(idx1, idx2);
1.486 -
1.487 - return 1;
1.488 -}
1.489 -
1.490 -static cell_t CloneArray(IPluginContext *pContext, const cell_t *params)
1.491 -{
1.492 - CellArray *oldArray;
1.493 - HandleError err;
1.494 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.495 -
1.496 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&oldArray))
1.497 - != HandleError_None)
1.498 - {
1.499 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.500 - }
1.501 -
1.502 - CellArray *array = oldArray->clone();
1.503 -
1.504 - Handle_t hndl = g_HandleSys.CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
1.505 - if (!hndl)
1.506 - {
1.507 - delete array;
1.508 - }
1.509 -
1.510 - return hndl;
1.511 -}
1.512 -
1.513 -static cell_t FindStringInArray(IPluginContext *pContext, const cell_t *params)
1.514 -{
1.515 - CellArray *array;
1.516 - HandleError err;
1.517 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.518 -
1.519 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.520 - != HandleError_None)
1.521 - {
1.522 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.523 - }
1.524 -
1.525 - char *str;
1.526 - pContext->LocalToString(params[2], &str);
1.527 -
1.528 - for (unsigned int i = 0; i < array->size(); i++)
1.529 - {
1.530 - const char *array_str = (const char *)array->at(i);
1.531 - if (strcmp(str, array_str) == 0)
1.532 - {
1.533 - return (cell_t) i;
1.534 - }
1.535 - }
1.536 -
1.537 - return -1;
1.538 -}
1.539 -
1.540 -static cell_t FindValueInArray(IPluginContext *pContext, const cell_t *params)
1.541 -{
1.542 - CellArray *array;
1.543 - HandleError err;
1.544 - HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1.545 -
1.546 - if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
1.547 - != HandleError_None)
1.548 - {
1.549 - return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
1.550 - }
1.551 -
1.552 - for (unsigned int i = 0; i < array->size(); i++)
1.553 - {
1.554 - if (params[2] == *array->at(i))
1.555 - {
1.556 - return (cell_t) i;
1.557 - }
1.558 - }
1.559 -
1.560 - return -1;
1.561 -}
1.562 -
1.563 -REGISTER_NATIVES(cellArrayNatives)
1.564 -{
1.565 - {"ClearArray", ClearArray},
1.566 - {"CreateArray", CreateArray},
1.567 - {"GetArrayArray", GetArrayArray},
1.568 - {"GetArrayCell", GetArrayCell},
1.569 - {"GetArraySize", GetArraySize},
1.570 - {"GetArrayString", GetArrayString},
1.571 - {"ResizeArray", ResizeArray},
1.572 - {"PushArrayArray", PushArrayArray},
1.573 - {"PushArrayCell", PushArrayCell},
1.574 - {"PushArrayString", PushArrayString},
1.575 - {"RemoveFromArray", RemoveFromArray},
1.576 - {"SetArrayCell", SetArrayCell},
1.577 - {"SetArrayString", SetArrayString},
1.578 - {"SetArrayArray", SetArrayArray},
1.579 - {"ShiftArrayUp", ShiftArrayUp},
1.580 - {"SwapArrayItems", SwapArrayItems},
1.581 - {"CloneArray", CloneArray},
1.582 - {"FindStringInArray", FindStringInArray},
1.583 - {"FindValueInArray", FindValueInArray},
1.584 - {NULL, NULL},
1.585 -};