Merge.
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 htCellStack;
40 class CellStackHelpers :
42 public IHandleTypeDispatch
44 public: //SMGlobalClass
45 void OnSourceModAllInitialized()
47 htCellStack = g_HandleSys.CreateType("CellStack", this, 0, NULL, NULL, g_pCoreIdent, NULL);
49 void OnSourceModShutdown()
51 g_HandleSys.RemoveType(htCellStack, 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 CreateStack(IPluginContext *pContext, const cell_t *params)
71 return pContext->ThrowNativeError("Invalid block size (must be > 0)");
74 CellArray *array = new CellArray(params[1]);
76 Handle_t hndl = g_HandleSys.CreateHandle(htCellStack, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
85 static cell_t PushStackCell(IPluginContext *pContext, const cell_t *params)
89 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
91 if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array))
94 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
97 cell_t *blk = array->push();
100 return pContext->ThrowNativeError("Failed to grow stack");
108 static cell_t PushStackString(IPluginContext *pContext, const cell_t *params)
112 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
114 if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array))
117 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
120 cell_t *blk = array->push();
123 return pContext->ThrowNativeError("Failed to grow stack");
127 pContext->LocalToString(params[2], &str);
129 strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
134 static cell_t PushStackArray(IPluginContext *pContext, const cell_t *params)
138 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
140 if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array))
143 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
146 cell_t *blk = array->push();
149 return pContext->ThrowNativeError("Failed to grow array");
153 pContext->LocalToPhysAddr(params[2], &addr);
155 size_t indexes = array->blocksize();
156 if (params[3] != -1 && (size_t)params[3] <= array->blocksize())
161 memcpy(blk, addr, sizeof(cell_t) * indexes);
166 static cell_t PopStackCell(IPluginContext *pContext, const cell_t *params)
171 cell_t *blk, *buffer;
172 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
174 if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array))
177 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
180 if (array->size() == 0)
185 pContext->LocalToPhysAddr(params[2], &buffer);
187 blk = array->at(array->size() - 1);
188 idx = (size_t)params[3];
192 if (idx >= array->blocksize())
194 return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
200 if (idx >= array->blocksize() * 4)
202 return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
204 *buffer = (cell_t)*((char *)blk + idx);
207 array->remove(array->size() - 1);
212 static cell_t PopStackString(IPluginContext *pContext, const cell_t *params)
216 size_t idx, numWritten;
217 cell_t *blk, *pWritten;
218 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
220 if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array))
223 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
226 if (array->size() == 0)
231 idx = array->size() - 1;
232 blk = array->at(idx);
233 pContext->StringToLocalUTF8(params[2], params[3], (char *)blk, &numWritten);
234 pContext->LocalToPhysAddr(params[4], &pWritten);
235 *pWritten = (cell_t)numWritten;
241 static cell_t PopStackArray(IPluginContext *pContext, const cell_t *params)
249 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
251 if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array))
254 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
257 if (array->size() == 0)
262 idx = array->size() - 1;
263 blk = array->at(idx);
264 indexes = array->blocksize();
266 if (params[3] != -1 && (size_t)params[3] <= array->blocksize())
271 pContext->LocalToPhysAddr(params[2], &addr);
272 memcpy(addr, blk, sizeof(cell_t) * indexes);
278 static cell_t IsStackEmpty(IPluginContext *pContext, const cell_t *params)
282 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
284 if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array))
287 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
290 if (array->size() == 0)
298 REGISTER_NATIVES(cellStackNatives)
300 {"CreateStack", CreateStack},
301 {"IsStackEmpty", IsStackEmpty},
302 {"PopStackArray", PopStackArray},
303 {"PopStackCell", PopStackCell},
304 {"PopStackString", PopStackString},
305 {"PushStackArray", PushStackArray},
306 {"PushStackCell", PushStackCell},
307 {"PushStackString", PushStackString},