core/smn_datapacks.cpp
author Scott Ehlert <ds@alliedmods.net>
Mon May 14 21:29:17 2007 +0000 (2007-05-14)
changeset 794 5947b0ee60bb
parent 600 f6d811158ad3
child 1191 054147e50960
permissions -rw-r--r--
Implemented amb286 - .smx extension no longer required with "sm plugins load"
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "sm_globals.h"
    16 #include "HandleSys.h"
    17 #include "CDataPack.h"
    18 
    19 HandleType_t g_DataPackType;
    20 
    21 class DataPackNatives : 
    22 	public SMGlobalClass,
    23 	public IHandleTypeDispatch
    24 {
    25 public:
    26 	void OnSourceModAllInitialized()
    27 	{
    28 		g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    29 	}
    30 	void OnSourceModShutdown()
    31 	{
    32 		g_HandleSys.RemoveType(g_DataPackType, g_pCoreIdent);
    33 		g_DataPackType = 0;
    34 	}
    35 	void OnHandleDestroy(HandleType_t type, void *object)
    36 	{
    37 		g_SourceMod.FreeDataPack(reinterpret_cast<IDataPack *>(object));
    38 	}
    39 };
    40 
    41 static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
    42 {
    43 	IDataPack *pDataPack = g_SourceMod.CreateDataPack();
    44 
    45 	if (!pDataPack)
    46 	{
    47 		return 0;
    48 	}
    49 
    50 	return g_HandleSys.CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL);
    51 }
    52 
    53 static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params)
    54 {
    55 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    56 	HandleError herr;
    57 	HandleSecurity sec;
    58 	IDataPack *pDataPack;
    59 
    60 	sec.pOwner = NULL;
    61 	sec.pIdentity = g_pCoreIdent;
    62 
    63 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
    64 		!= HandleError_None)
    65 	{
    66 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
    67 	}
    68 
    69 	pDataPack->PackCell(params[2]);
    70 
    71 	return 1;
    72 }
    73 
    74 static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params)
    75 {
    76 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    77 	HandleError herr;
    78 	HandleSecurity sec;
    79 	IDataPack *pDataPack;
    80 
    81 	sec.pOwner = NULL;
    82 	sec.pIdentity = g_pCoreIdent;
    83 
    84 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
    85 		!= HandleError_None)
    86 	{
    87 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
    88 	}
    89 
    90 	pDataPack->PackFloat(sp_ctof(params[2]));
    91 
    92 	return 1;
    93 }
    94 
    95 static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params)
    96 {
    97 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    98 	HandleError herr;
    99 	HandleSecurity sec;
   100 	IDataPack *pDataPack;
   101 	int err;
   102 
   103 	sec.pOwner = NULL;
   104 	sec.pIdentity = g_pCoreIdent;
   105 
   106 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   107 		!= HandleError_None)
   108 	{
   109 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   110 	}
   111 
   112 	char *str;
   113 	if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE)
   114 	{
   115 		pContext->ThrowNativeErrorEx(err, NULL);
   116 		return 0;
   117 	}
   118 
   119 	pDataPack->PackString(str);
   120 
   121 	return 1;
   122 }
   123 
   124 static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params)
   125 {
   126 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   127 	HandleError herr;
   128 	HandleSecurity sec;
   129 	IDataPack *pDataPack;
   130 
   131 	sec.pOwner = NULL;
   132 	sec.pIdentity = g_pCoreIdent;
   133 
   134 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   135 		!= HandleError_None)
   136 	{
   137 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   138 	}
   139 
   140 	if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(cell_t)))
   141 	{
   142 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   143 	}
   144 
   145 	return pDataPack->ReadCell();
   146 }
   147 
   148 static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params)
   149 {
   150 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   151 	HandleError herr;
   152 	HandleSecurity sec;
   153 	IDataPack *pDataPack;
   154 
   155 	sec.pOwner = NULL;
   156 	sec.pIdentity = g_pCoreIdent;
   157 
   158 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   159 		!= HandleError_None)
   160 	{
   161 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   162 	}
   163 
   164 	if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(float)))
   165 	{
   166 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   167 	}
   168 
   169 	return sp_ftoc(pDataPack->ReadFloat());
   170 }
   171 
   172 static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params)
   173 {
   174 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   175 	HandleError herr;
   176 	HandleSecurity sec;
   177 	IDataPack *pDataPack;
   178 
   179 	sec.pOwner = NULL;
   180 	sec.pIdentity = g_pCoreIdent;
   181 
   182 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   183 		!= HandleError_None)
   184 	{
   185 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   186 	}
   187 
   188 	const char *str;
   189 	if (!(str=pDataPack->ReadString(NULL)))
   190 	{
   191 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   192 	}
   193 
   194 	pContext->StringToLocal(params[2], params[3], str);
   195 
   196 	return 1;
   197 }
   198 
   199 static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params)
   200 {
   201 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   202 	HandleError herr;
   203 	HandleSecurity sec;
   204 	IDataPack *pDataPack;
   205 
   206 	sec.pOwner = NULL;
   207 	sec.pIdentity = g_pCoreIdent;
   208 
   209 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   210 		!= HandleError_None)
   211 	{
   212 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   213 	}
   214 
   215 	pDataPack->Reset();
   216 	if (params[2])
   217 	{
   218 		pDataPack->ResetSize();
   219 	}
   220 
   221 	return 1;
   222 }
   223 
   224 static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params)
   225 {
   226 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   227 	HandleError herr;
   228 	HandleSecurity sec;
   229 	IDataPack *pDataPack;
   230 
   231 	sec.pOwner = NULL;
   232 	sec.pIdentity = g_pCoreIdent;
   233 
   234 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   235 		!= HandleError_None)
   236 	{
   237 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   238 	}
   239 
   240 	return static_cast<cell_t>(pDataPack->GetPosition());
   241 }
   242 
   243 static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params)
   244 {
   245 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   246 	HandleError herr;
   247 	HandleSecurity sec;
   248 	IDataPack *pDataPack;
   249 
   250 	sec.pOwner = NULL;
   251 	sec.pIdentity = g_pCoreIdent;
   252 
   253 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   254 		!= HandleError_None)
   255 	{
   256 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   257 	}
   258 
   259 	if (!pDataPack->SetPosition(params[2]))
   260 	{
   261 		return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]);
   262 	}
   263 
   264 	return 1;
   265 }
   266 
   267 static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params)
   268 {
   269 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   270 	HandleError herr;
   271 	HandleSecurity sec;
   272 	IDataPack *pDataPack;
   273 
   274 	sec.pOwner = NULL;
   275 	sec.pIdentity = g_pCoreIdent;
   276 
   277 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   278 		!= HandleError_None)
   279 	{
   280 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   281 	}
   282 
   283 	return pDataPack->IsReadable(params[2]) ? 1 : 0;
   284 }
   285 
   286 static DataPackNatives s_DataPackNatives;
   287 
   288 REGISTER_NATIVES(datapacknatives)
   289 {
   290 	{"CreateDataPack",			smn_CreateDataPack},
   291 	{"WritePackCell",			smn_WritePackCell},
   292 	{"WritePackFloat",			smn_WritePackFloat},
   293 	{"WritePackString",			smn_WritePackString},
   294 	{"ReadPackCell",			smn_ReadPackCell},
   295 	{"ReadPackFloat",			smn_ReadPackFloat},
   296 	{"ReadPackString",			smn_ReadPackString},
   297 	{"ResetPack",				smn_ResetPack},
   298 	{"GetPackPosition",			smn_GetPackPosition},
   299 	{"SetPackPosition",			smn_SetPackPosition},
   300 	{"IsPackReadable",			smn_IsPackReadable},
   301 	{NULL,						NULL}
   302 };