core/smn_datapacks.cpp
author Phong Dang <teame06@gmail.com>
Sun Jul 29 19:20:42 2007 +0000 (2007-07-29)
changeset 1211 990912ee1e1e
parent 1193 6860e7bd800c
child 1231 17badaf66ce4
permissions -rw-r--r--
Added amb<536> requested for using sm_kick without putting quotes around reason.
     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 		HandleAccess hacc;
    29 		TypeAccess tacc;
    30 
    31 		g_HandleSys.InitAccessDefaults(&tacc, &hacc);
    32 		tacc.access[HTypeAccess_Create] = true;
    33 		tacc.access[HTypeAccess_Inherit] = true;
    34 		tacc.ident = g_pCoreIdent;
    35 		hacc.access[HandleAccess_Read] = HANDLE_RESTRICT_OWNER;
    36 
    37 		g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, &tacc, &hacc, g_pCoreIdent, NULL);
    38 	}
    39 	void OnSourceModShutdown()
    40 	{
    41 		g_HandleSys.RemoveType(g_DataPackType, g_pCoreIdent);
    42 		g_DataPackType = 0;
    43 	}
    44 	void OnHandleDestroy(HandleType_t type, void *object)
    45 	{
    46 		g_SourceMod.FreeDataPack(reinterpret_cast<IDataPack *>(object));
    47 	}
    48 };
    49 
    50 static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
    51 {
    52 	IDataPack *pDataPack = g_SourceMod.CreateDataPack();
    53 
    54 	if (!pDataPack)
    55 	{
    56 		return 0;
    57 	}
    58 
    59 	return g_HandleSys.CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL);
    60 }
    61 
    62 static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params)
    63 {
    64 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    65 	HandleError herr;
    66 	HandleSecurity sec;
    67 	IDataPack *pDataPack;
    68 
    69 	sec.pOwner = pContext->GetIdentity();
    70 	sec.pIdentity = g_pCoreIdent;
    71 
    72 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
    73 		!= HandleError_None)
    74 	{
    75 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
    76 	}
    77 
    78 	pDataPack->PackCell(params[2]);
    79 
    80 	return 1;
    81 }
    82 
    83 static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params)
    84 {
    85 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    86 	HandleError herr;
    87 	HandleSecurity sec;
    88 	IDataPack *pDataPack;
    89 
    90 	sec.pOwner = pContext->GetIdentity();
    91 	sec.pIdentity = g_pCoreIdent;
    92 
    93 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
    94 		!= HandleError_None)
    95 	{
    96 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
    97 	}
    98 
    99 	pDataPack->PackFloat(sp_ctof(params[2]));
   100 
   101 	return 1;
   102 }
   103 
   104 static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params)
   105 {
   106 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   107 	HandleError herr;
   108 	HandleSecurity sec;
   109 	IDataPack *pDataPack;
   110 	int err;
   111 
   112 	sec.pOwner = pContext->GetIdentity();
   113 	sec.pIdentity = g_pCoreIdent;
   114 
   115 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   116 		!= HandleError_None)
   117 	{
   118 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   119 	}
   120 
   121 	char *str;
   122 	if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE)
   123 	{
   124 		pContext->ThrowNativeErrorEx(err, NULL);
   125 		return 0;
   126 	}
   127 
   128 	pDataPack->PackString(str);
   129 
   130 	return 1;
   131 }
   132 
   133 static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params)
   134 {
   135 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   136 	HandleError herr;
   137 	HandleSecurity sec;
   138 	IDataPack *pDataPack;
   139 
   140 	sec.pOwner = pContext->GetIdentity();
   141 	sec.pIdentity = g_pCoreIdent;
   142 
   143 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   144 		!= HandleError_None)
   145 	{
   146 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   147 	}
   148 
   149 	if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(cell_t)))
   150 	{
   151 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   152 	}
   153 
   154 	return pDataPack->ReadCell();
   155 }
   156 
   157 static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params)
   158 {
   159 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   160 	HandleError herr;
   161 	HandleSecurity sec;
   162 	IDataPack *pDataPack;
   163 
   164 	sec.pOwner = pContext->GetIdentity();
   165 	sec.pIdentity = g_pCoreIdent;
   166 
   167 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   168 		!= HandleError_None)
   169 	{
   170 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   171 	}
   172 
   173 	if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(float)))
   174 	{
   175 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   176 	}
   177 
   178 	return sp_ftoc(pDataPack->ReadFloat());
   179 }
   180 
   181 static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params)
   182 {
   183 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   184 	HandleError herr;
   185 	HandleSecurity sec;
   186 	IDataPack *pDataPack;
   187 
   188 	sec.pOwner = pContext->GetIdentity();
   189 	sec.pIdentity = g_pCoreIdent;
   190 
   191 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   192 		!= HandleError_None)
   193 	{
   194 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   195 	}
   196 
   197 	const char *str;
   198 	if (!(str=pDataPack->ReadString(NULL)))
   199 	{
   200 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   201 	}
   202 
   203 	pContext->StringToLocal(params[2], params[3], str);
   204 
   205 	return 1;
   206 }
   207 
   208 static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params)
   209 {
   210 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   211 	HandleError herr;
   212 	HandleSecurity sec;
   213 	IDataPack *pDataPack;
   214 
   215 	sec.pOwner = pContext->GetIdentity();
   216 	sec.pIdentity = g_pCoreIdent;
   217 
   218 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   219 		!= HandleError_None)
   220 	{
   221 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   222 	}
   223 
   224 	pDataPack->Reset();
   225 	if (params[2])
   226 	{
   227 		pDataPack->ResetSize();
   228 	}
   229 
   230 	return 1;
   231 }
   232 
   233 static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params)
   234 {
   235 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   236 	HandleError herr;
   237 	HandleSecurity sec;
   238 	IDataPack *pDataPack;
   239 
   240 	sec.pOwner = pContext->GetIdentity();
   241 	sec.pIdentity = g_pCoreIdent;
   242 
   243 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   244 		!= HandleError_None)
   245 	{
   246 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   247 	}
   248 
   249 	return static_cast<cell_t>(pDataPack->GetPosition());
   250 }
   251 
   252 static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params)
   253 {
   254 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   255 	HandleError herr;
   256 	HandleSecurity sec;
   257 	IDataPack *pDataPack;
   258 
   259 	sec.pOwner = pContext->GetIdentity();
   260 	sec.pIdentity = g_pCoreIdent;
   261 
   262 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   263 		!= HandleError_None)
   264 	{
   265 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   266 	}
   267 
   268 	if (!pDataPack->SetPosition(params[2]))
   269 	{
   270 		return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]);
   271 	}
   272 
   273 	return 1;
   274 }
   275 
   276 static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params)
   277 {
   278 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   279 	HandleError herr;
   280 	HandleSecurity sec;
   281 	IDataPack *pDataPack;
   282 
   283 	sec.pOwner = pContext->GetIdentity();
   284 	sec.pIdentity = g_pCoreIdent;
   285 
   286 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   287 		!= HandleError_None)
   288 	{
   289 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   290 	}
   291 
   292 	return pDataPack->IsReadable(params[2]) ? 1 : 0;
   293 }
   294 
   295 static DataPackNatives s_DataPackNatives;
   296 
   297 REGISTER_NATIVES(datapacknatives)
   298 {
   299 	{"CreateDataPack",			smn_CreateDataPack},
   300 	{"WritePackCell",			smn_WritePackCell},
   301 	{"WritePackFloat",			smn_WritePackFloat},
   302 	{"WritePackString",			smn_WritePackString},
   303 	{"ReadPackCell",			smn_ReadPackCell},
   304 	{"ReadPackFloat",			smn_ReadPackFloat},
   305 	{"ReadPackString",			smn_ReadPackString},
   306 	{"ResetPack",				smn_ResetPack},
   307 	{"GetPackPosition",			smn_GetPackPosition},
   308 	{"SetPackPosition",			smn_SetPackPosition},
   309 	{"IsPackReadable",			smn_IsPackReadable},
   310 	{NULL,						NULL}
   311 };