More Windows build fix.
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 "common_logic.h"
34 #include "CellArray.h"
36 HandleType_t htCellStack;
38 class CellStackHelpers :
40 public IHandleTypeDispatch
42 public: //SMGlobalClass
43 void OnSourceModAllInitialized()
45 htCellStack = handlesys->CreateType("CellStack", this, 0, NULL, NULL, g_pCoreIdent, NULL);
47 void OnSourceModShutdown()
49 handlesys->RemoveType(htCellStack, g_pCoreIdent);
51 public: //IHandleTypeDispatch
52 void OnHandleDestroy(HandleType_t type, void *object)
54 CellArray *array = (CellArray *)object;
57 bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
59 CellArray *pArray = (CellArray *)object;
60 *pSize = sizeof(CellArray) + pArray->mem_usage();
65 static cell_t CreateStack(IPluginContext *pContext, const cell_t *params)
69 return pContext->ThrowNativeError("Invalid block size (must be > 0)");
72 CellArray *array = new CellArray(params[1]);
74 Handle_t hndl = handlesys->CreateHandle(htCellStack, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
83 static cell_t PushStackCell(IPluginContext *pContext, const cell_t *params)
87 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
89 if ((err = handlesys->ReadHandle(params[1], htCellStack, &sec, (void **)&array))
92 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
95 cell_t *blk = array->push();
98 return pContext->ThrowNativeError("Failed to grow stack");
106 static cell_t PushStackString(IPluginContext *pContext, const cell_t *params)
110 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
112 if ((err = handlesys->ReadHandle(params[1], htCellStack, &sec, (void **)&array))
115 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
118 cell_t *blk = array->push();
121 return pContext->ThrowNativeError("Failed to grow stack");
125 pContext->LocalToString(params[2], &str);
127 smcore.strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
132 static cell_t PushStackArray(IPluginContext *pContext, const cell_t *params)
136 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
138 if ((err = handlesys->ReadHandle(params[1], htCellStack, &sec, (void **)&array))
141 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
144 cell_t *blk = array->push();
147 return pContext->ThrowNativeError("Failed to grow array");
151 pContext->LocalToPhysAddr(params[2], &addr);
153 size_t indexes = array->blocksize();
154 if (params[3] != -1 && (size_t)params[3] <= array->blocksize())
159 memcpy(blk, addr, sizeof(cell_t) * indexes);
164 static cell_t PopStackCell(IPluginContext *pContext, const cell_t *params)
169 cell_t *blk, *buffer;
170 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
172 if ((err = handlesys->ReadHandle(params[1], htCellStack, &sec, (void **)&array))
175 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
178 if (array->size() == 0)
183 pContext->LocalToPhysAddr(params[2], &buffer);
185 blk = array->at(array->size() - 1);
186 idx = (size_t)params[3];
190 if (idx >= array->blocksize())
192 return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
198 if (idx >= array->blocksize() * 4)
200 return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
202 *buffer = (cell_t)*((char *)blk + idx);
205 array->remove(array->size() - 1);
210 static cell_t PopStackString(IPluginContext *pContext, const cell_t *params)
214 size_t idx, numWritten;
215 cell_t *blk, *pWritten;
216 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
218 if ((err = handlesys->ReadHandle(params[1], htCellStack, &sec, (void **)&array))
221 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
224 if (array->size() == 0)
229 idx = array->size() - 1;
230 blk = array->at(idx);
231 pContext->StringToLocalUTF8(params[2], params[3], (char *)blk, &numWritten);
232 pContext->LocalToPhysAddr(params[4], &pWritten);
233 *pWritten = (cell_t)numWritten;
239 static cell_t PopStackArray(IPluginContext *pContext, const cell_t *params)
247 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
249 if ((err = handlesys->ReadHandle(params[1], htCellStack, &sec, (void **)&array))
252 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
255 if (array->size() == 0)
260 idx = array->size() - 1;
261 blk = array->at(idx);
262 indexes = array->blocksize();
264 if (params[3] != -1 && (size_t)params[3] <= array->blocksize())
269 pContext->LocalToPhysAddr(params[2], &addr);
270 memcpy(addr, blk, sizeof(cell_t) * indexes);
276 static cell_t IsStackEmpty(IPluginContext *pContext, const cell_t *params)
280 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
282 if ((err = handlesys->ReadHandle(params[1], htCellStack, &sec, (void **)&array))
285 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
288 if (array->size() == 0)
296 REGISTER_NATIVES(cellStackNatives)
298 {"CreateStack", CreateStack},
299 {"IsStackEmpty", IsStackEmpty},
300 {"PopStackArray", PopStackArray},
301 {"PopStackCell", PopStackCell},
302 {"PopStackString", PopStackString},
303 {"PushStackArray", PushStackArray},
304 {"PushStackCell", PushStackCell},
305 {"PushStackString", PushStackString},