core/smn_adt_stack.cpp
author Scott Ehlert <ds@alliedmods.net>
Sat Jul 12 09:05:19 2008 +0000 (2008-07-12)
changeset 2201 d2b8bc01ccd8
parent 1999 63aafc6ff212
child 2471 194be299b700
permissions -rw-r--r--
Builds against latest OB SDK now
     1 /**
     2 * vim: set ts=4 :
     3 * =============================================================================
     4 * SourceMod
     5 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6 * =============================================================================
     7 *
     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.
    11 * 
    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
    15 * details.
    16 *
    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/>.
    19 *
    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>.
    28 *
    29 * Version: $Id$
    30 */
    31 
    32 #include <malloc.h>
    33 #include "sm_globals.h"
    34 #include "sm_stringutil.h"
    35 #include "CellArray.h"
    36 #include "HandleSys.h"
    37 
    38 HandleType_t htCellStack;
    39 
    40 class CellStackHelpers : 
    41 	public SMGlobalClass,
    42 	public IHandleTypeDispatch
    43 {
    44 public: //SMGlobalClass
    45 	void OnSourceModAllInitialized()
    46 	{
    47 		htCellStack = g_HandleSys.CreateType("CellStack", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    48 	}
    49 	void OnSourceModShutdown()
    50 	{
    51 		g_HandleSys.RemoveType(htCellStack, g_pCoreIdent);
    52 	}
    53 public: //IHandleTypeDispatch
    54 	void OnHandleDestroy(HandleType_t type, void *object)
    55 	{
    56 		CellArray *array = (CellArray *)object;
    57 		delete array;
    58 	}
    59 	bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
    60 	{
    61 		CellArray *pArray = (CellArray *)object;
    62 		*pSize = sizeof(CellArray) + pArray->mem_usage();
    63 		return true;
    64 	}
    65 } s_CellStackHelpers;
    66 
    67 static cell_t CreateStack(IPluginContext *pContext, const cell_t *params)
    68 {
    69 	if (params[1] < 1)
    70 	{
    71 		return pContext->ThrowNativeError("Invalid block size (must be > 0)");
    72 	}
    73 
    74 	CellArray *array = new CellArray(params[1]);
    75 
    76 	Handle_t hndl = g_HandleSys.CreateHandle(htCellStack, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
    77 	if (!hndl)
    78 	{
    79 		delete array;
    80 	}
    81 
    82 	return hndl;
    83 }
    84 
    85 static cell_t PushStackCell(IPluginContext *pContext, const cell_t *params)
    86 {
    87 	CellArray *array;
    88 	HandleError err;
    89 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
    90 
    91 	if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array)) 
    92 		!= HandleError_None)
    93 	{
    94 		return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
    95 	}
    96 
    97 	cell_t *blk = array->push();
    98 	if (!blk)
    99 	{
   100 		return pContext->ThrowNativeError("Failed to grow stack");
   101 	}
   102 
   103 	*blk = params[2];
   104 
   105 	return 1;
   106 }
   107 
   108 static cell_t PushStackString(IPluginContext *pContext, const cell_t *params)
   109 {
   110 	CellArray *array;
   111 	HandleError err;
   112 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   113 
   114 	if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array)) 
   115 		!= HandleError_None)
   116 	{
   117 		return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
   118 	}
   119 
   120 	cell_t *blk = array->push();
   121 	if (!blk)
   122 	{
   123 		return pContext->ThrowNativeError("Failed to grow stack");
   124 	}
   125 
   126 	char *str;
   127 	pContext->LocalToString(params[2], &str);
   128 
   129 	strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
   130 
   131 	return 1;
   132 }
   133 
   134 static cell_t PushStackArray(IPluginContext *pContext, const cell_t *params)
   135 {
   136 	CellArray *array;
   137 	HandleError err;
   138 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   139 
   140 	if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array)) 
   141 		!= HandleError_None)
   142 	{
   143 		return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
   144 	}
   145 
   146 	cell_t *blk = array->push();
   147 	if (!blk)
   148 	{
   149 		return pContext->ThrowNativeError("Failed to grow array");
   150 	}
   151 
   152 	cell_t *addr;
   153 	pContext->LocalToPhysAddr(params[2], &addr);
   154 
   155 	size_t indexes = array->blocksize();
   156 	if (params[3] != -1 && (size_t)params[3] <= array->blocksize())
   157 	{
   158 		indexes = params[3];
   159 	}
   160 
   161 	memcpy(blk, addr, sizeof(cell_t) * indexes);
   162 
   163 	return 1;
   164 }
   165 
   166 static cell_t PopStackCell(IPluginContext *pContext, const cell_t *params)
   167 {
   168 	size_t idx;
   169 	HandleError err;
   170 	CellArray *array;
   171 	cell_t *blk, *buffer;
   172 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   173 
   174 	if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array)) 
   175 		!= HandleError_None)
   176 	{
   177 		return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
   178 	}
   179 
   180 	if (array->size() == 0)
   181 	{
   182 		return 0;
   183 	}
   184 
   185 	pContext->LocalToPhysAddr(params[2], &buffer);
   186 
   187 	blk = array->at(array->size() - 1);
   188 	idx = (size_t)params[3];
   189 
   190 	if (params[4] == 0)
   191 	{
   192 		if (idx >= array->blocksize())
   193 		{
   194 			return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
   195 		}
   196 		*buffer = blk[idx];
   197 	}
   198 	else
   199 	{
   200 		if (idx >= array->blocksize() * 4)
   201 		{
   202 			return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
   203 		}
   204 		*buffer = (cell_t)*((char *)blk + idx);
   205 	}
   206 
   207 	array->remove(array->size() - 1);
   208 
   209 	return 1;
   210 }
   211 
   212 static cell_t PopStackString(IPluginContext *pContext, const cell_t *params)
   213 {
   214 	HandleError err;
   215 	CellArray *array;
   216 	size_t idx, numWritten;
   217 	cell_t *blk, *pWritten;
   218 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   219 
   220 	if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array)) 
   221 		!= HandleError_None)
   222 	{
   223 		return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
   224 	}
   225 
   226 	if (array->size() == 0)
   227 	{
   228 		return 0;
   229 	}
   230 
   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;
   236 	array->remove(idx);
   237 
   238 	return 1;
   239 }
   240 
   241 static cell_t PopStackArray(IPluginContext *pContext, const cell_t *params)
   242 {
   243 	size_t idx;
   244 	cell_t *blk;
   245 	cell_t *addr;
   246 	size_t indexes;
   247 	HandleError err;
   248 	CellArray *array;
   249 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   250 
   251 	if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array)) 
   252 		!= HandleError_None)
   253 	{
   254 		return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
   255 	}
   256 
   257 	if (array->size() == 0)
   258 	{
   259 		return 0;
   260 	}
   261 
   262 	idx = array->size() - 1;
   263 	blk = array->at(idx);
   264 	indexes = array->blocksize();
   265 
   266 	if (params[3] != -1 && (size_t)params[3] <= array->blocksize())
   267 	{
   268 		indexes = params[3];
   269 	}
   270 
   271 	pContext->LocalToPhysAddr(params[2], &addr);
   272 	memcpy(addr, blk, sizeof(cell_t) * indexes);
   273 	array->remove(idx);
   274 
   275 	return indexes;
   276 }
   277 
   278 static cell_t IsStackEmpty(IPluginContext *pContext, const cell_t *params)
   279 {
   280 	HandleError err;
   281 	CellArray *array;
   282 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   283 
   284 	if ((err = g_HandleSys.ReadHandle(params[1], htCellStack, &sec, (void **)&array)) 
   285 		!= HandleError_None)
   286 	{
   287 		return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
   288 	}
   289 
   290 	if (array->size() == 0)
   291 	{
   292 		return 1;
   293 	}
   294 
   295 	return 0;
   296 }
   297 
   298 REGISTER_NATIVES(cellStackNatives)
   299 {
   300 	{"CreateStack",					CreateStack},
   301 	{"IsStackEmpty",				IsStackEmpty},
   302 	{"PopStackArray",				PopStackArray},
   303 	{"PopStackCell",				PopStackCell},
   304 	{"PopStackString",				PopStackString},
   305 	{"PushStackArray",				PushStackArray},
   306 	{"PushStackCell",				PushStackCell},
   307 	{"PushStackString",				PushStackString},
   308 	{NULL,							NULL},
   309 };