Added check to make sure player are alive to perform sm_slap, sm_burn, sm_slay.
3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
34 #include "sm_globals.h"
35 #include "sm_stringutil.h"
36 #include "CellArray.h"
37 #include "HandleSys.h"
39 HandleType_t htCellArray;
41 class CellArrayHelpers :
43 public IHandleTypeDispatch
45 public: //SMGlobalClass
46 void OnSourceModAllInitialized()
48 htCellArray = g_HandleSys.CreateType("CellArray", this, 0, NULL, NULL, g_pCoreIdent, NULL);
50 void OnSourceModShutdown()
52 g_HandleSys.RemoveType(htCellArray, g_pCoreIdent);
54 public: //IHandleTypeDispatch
55 void OnHandleDestroy(HandleType_t type, void *object)
57 CellArray *array = (CellArray *)object;
62 static cell_t CreateArray(IPluginContext *pContext, const cell_t *params)
66 return pContext->ThrowNativeError("Invalid block size (must be > 0)");
69 CellArray *array = new CellArray(params[1]);
73 array->resize(params[2]);
76 Handle_t hndl = g_HandleSys.CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
85 static cell_t ClearArray(IPluginContext *pContext, const cell_t *params)
89 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
91 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
94 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
102 static cell_t ResizeArray(IPluginContext *pContext, const cell_t *params)
106 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
108 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
111 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
114 if (!array->resize(params[2]))
116 return pContext->ThrowNativeError("Unable to resize array to \"%u\"", params[2]);
122 static cell_t GetArraySize(IPluginContext *pContext, const cell_t *params)
126 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
128 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
131 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
134 return (cell_t)array->size();
137 static cell_t PushArrayCell(IPluginContext *pContext, const cell_t *params)
141 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
143 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
146 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
149 cell_t *blk = array->push();
152 return pContext->ThrowNativeError("Failed to grow array");
157 return (cell_t)(array->size() - 1);
160 static cell_t PushArrayString(IPluginContext *pContext, const cell_t *params)
164 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
166 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
169 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
172 cell_t *blk = array->push();
175 return pContext->ThrowNativeError("Failed to grow array");
179 pContext->LocalToString(params[2], &str);
181 strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
183 return (cell_t)(array->size() - 1);
186 static cell_t PushArrayArray(IPluginContext *pContext, const cell_t *params)
190 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
192 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
195 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
198 cell_t *blk = array->push();
201 return pContext->ThrowNativeError("Failed to grow array");
205 pContext->LocalToPhysAddr(params[2], &addr);
207 size_t indexes = array->blocksize();
208 if (params[3] != -1 && (size_t)params[3] <= array->blocksize())
213 memcpy(blk, addr, sizeof(cell_t) * indexes);
215 return (cell_t)(array->size() - 1);
218 static cell_t GetArrayCell(IPluginContext *pContext, const cell_t *params)
222 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
224 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
227 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
230 size_t idx = (size_t)params[2];
231 if (idx >= array->size())
233 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
236 cell_t *blk = array->at(idx);
238 idx = (size_t)params[3];
241 if (idx >= array->blocksize())
243 return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
247 if (idx >= array->blocksize() * 4)
249 return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
251 return (cell_t)*((char *)blk + idx);
257 static cell_t GetArrayString(IPluginContext *pContext, const cell_t *params)
261 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
263 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
266 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
269 size_t idx = (size_t)params[2];
270 if (idx >= array->size())
272 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
275 cell_t *blk = array->at(idx);
276 size_t numWritten = 0;
278 pContext->StringToLocalUTF8(params[3], params[4], (char *)blk, &numWritten);
283 static cell_t GetArrayArray(IPluginContext *pContext, const cell_t *params)
287 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
289 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
292 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
295 size_t idx = (size_t)params[2];
296 if (idx >= array->size())
298 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
301 cell_t *blk = array->at(idx);
302 size_t indexes = array->blocksize();
303 if (params[4] != -1 && (size_t)params[4] <= array->blocksize())
309 pContext->LocalToPhysAddr(params[3], &addr);
311 memcpy(addr, blk, sizeof(cell_t) * indexes);
316 static cell_t SetArrayCell(IPluginContext *pContext, const cell_t *params)
320 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
322 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
325 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
328 size_t idx = (size_t)params[2];
329 if (idx >= array->size())
331 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
334 cell_t *blk = array->at(idx);
336 idx = (size_t)params[4];
339 if (idx >= array->blocksize())
341 return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
343 blk[idx] = params[3];
345 if (idx >= array->blocksize() * 4)
347 return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
349 *((char *)blk + idx) = (char)params[3];
355 static cell_t SetArrayString(IPluginContext *pContext, const cell_t *params)
359 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
361 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
364 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
367 size_t idx = (size_t)params[2];
368 if (idx >= array->size())
370 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
373 cell_t *blk = array->at(idx);
376 pContext->LocalToString(params[3], &str);
378 return strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
381 static cell_t SetArrayArray(IPluginContext *pContext, const cell_t *params)
385 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
387 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
390 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
393 size_t idx = (size_t)params[2];
394 if (idx >= array->size())
396 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
399 cell_t *blk = array->at(idx);
400 size_t indexes = array->blocksize();
401 if (params[4] != -1 && (size_t)params[4] <= array->blocksize())
407 pContext->LocalToPhysAddr(params[3], &addr);
409 memcpy(blk, addr, sizeof(cell_t) * indexes);
414 static cell_t ShiftArrayUp(IPluginContext *pContext, const cell_t *params)
418 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
420 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
423 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
426 size_t idx = (size_t)params[2];
427 if (idx >= array->size())
429 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
432 array->insert_at(idx);
437 static cell_t RemoveFromArray(IPluginContext *pContext, const cell_t *params)
441 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
443 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
446 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
449 size_t idx = (size_t)params[2];
450 if (idx >= array->size())
452 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
460 static cell_t SwapArrayItems(IPluginContext *pContext, const cell_t *params)
464 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
466 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
469 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
472 size_t idx1 = (size_t)params[2];
473 size_t idx2 = (size_t)params[3];
474 if (idx1 >= array->size())
476 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx1, array->size());
478 if (idx2 >= array->size())
480 return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx2, array->size());
483 array->swap(idx1, idx2);
488 REGISTER_NATIVES(cellArrayNatives)
490 {"ClearArray", ClearArray},
491 {"CreateArray", CreateArray},
492 {"GetArrayArray", GetArrayArray},
493 {"GetArrayCell", GetArrayCell},
494 {"GetArraySize", GetArraySize},
495 {"GetArrayString", GetArrayString},
496 {"ResizeArray", ResizeArray},
497 {"PushArrayArray", PushArrayArray},
498 {"PushArrayCell", PushArrayCell},
499 {"PushArrayString", PushArrayString},
500 {"RemoveFromArray", RemoveFromArray},
501 {"SetArrayCell", SetArrayCell},
502 {"SetArrayString", SetArrayString},
503 {"SetArrayArray", SetArrayArray},
504 {"ShiftArrayUp", ShiftArrayUp},
505 {"SwapArrayItems", SwapArrayItems},