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