core/logic/smn_datapacks.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed Aug 14 14:20:57 2013 -0400 (2013-08-14)
changeset 4008 490b01aa0683
parent 3009 a1f52cb232d3
permissions -rw-r--r--
Updated some CS:GO gamedata.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2010 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 "common_logic.h"
    33 #include <IHandleSys.h>
    34 #include <IDataPack.h>
    35 #include <ISourceMod.h>
    36 
    37 HandleType_t g_DataPackType;
    38 
    39 class DataPackNatives : 
    40 	public SMGlobalClass,
    41 	public IHandleTypeDispatch
    42 {
    43 public:
    44 	void OnSourceModAllInitialized()
    45 	{
    46 		HandleAccess hacc;
    47 		TypeAccess tacc;
    48 
    49 		handlesys->InitAccessDefaults(&tacc, &hacc);
    50 		tacc.access[HTypeAccess_Create] = true;
    51 		tacc.access[HTypeAccess_Inherit] = true;
    52 		tacc.ident = g_pCoreIdent;
    53 		hacc.access[HandleAccess_Read] = HANDLE_RESTRICT_OWNER;
    54 
    55 		g_DataPackType = handlesys->CreateType("DataPack", this, 0, &tacc, &hacc, g_pCoreIdent, NULL);
    56 	}
    57 	void OnSourceModShutdown()
    58 	{
    59 		handlesys->RemoveType(g_DataPackType, g_pCoreIdent);
    60 		g_DataPackType = 0;
    61 	}
    62 	void OnHandleDestroy(HandleType_t type, void *object)
    63 	{
    64 		g_pSM->FreeDataPack(reinterpret_cast<IDataPack *>(object));
    65 	}
    66 };
    67 
    68 static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
    69 {
    70 	IDataPack *pDataPack = g_pSM->CreateDataPack();
    71 
    72 	if (!pDataPack)
    73 	{
    74 		return 0;
    75 	}
    76 
    77 	return handlesys->CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL);
    78 }
    79 
    80 static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params)
    81 {
    82 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    83 	HandleError herr;
    84 	HandleSecurity sec;
    85 	IDataPack *pDataPack;
    86 
    87 	sec.pOwner = pContext->GetIdentity();
    88 	sec.pIdentity = g_pCoreIdent;
    89 
    90 	if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
    91 		!= HandleError_None)
    92 	{
    93 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
    94 	}
    95 
    96 	pDataPack->PackCell(params[2]);
    97 
    98 	return 1;
    99 }
   100 
   101 static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params)
   102 {
   103 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   104 	HandleError herr;
   105 	HandleSecurity sec;
   106 	IDataPack *pDataPack;
   107 
   108 	sec.pOwner = pContext->GetIdentity();
   109 	sec.pIdentity = g_pCoreIdent;
   110 
   111 	if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   112 		!= HandleError_None)
   113 	{
   114 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   115 	}
   116 
   117 	pDataPack->PackFloat(sp_ctof(params[2]));
   118 
   119 	return 1;
   120 }
   121 
   122 static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params)
   123 {
   124 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   125 	HandleError herr;
   126 	HandleSecurity sec;
   127 	IDataPack *pDataPack;
   128 	int err;
   129 
   130 	sec.pOwner = pContext->GetIdentity();
   131 	sec.pIdentity = g_pCoreIdent;
   132 
   133 	if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   134 		!= HandleError_None)
   135 	{
   136 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   137 	}
   138 
   139 	char *str;
   140 	if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE)
   141 	{
   142 		pContext->ThrowNativeErrorEx(err, NULL);
   143 		return 0;
   144 	}
   145 
   146 	pDataPack->PackString(str);
   147 
   148 	return 1;
   149 }
   150 
   151 static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params)
   152 {
   153 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   154 	HandleError herr;
   155 	HandleSecurity sec;
   156 	IDataPack *pDataPack;
   157 
   158 	sec.pOwner = pContext->GetIdentity();
   159 	sec.pIdentity = g_pCoreIdent;
   160 
   161 	if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   162 		!= HandleError_None)
   163 	{
   164 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   165 	}
   166 
   167 	if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(cell_t)))
   168 	{
   169 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   170 	}
   171 
   172 	return pDataPack->ReadCell();
   173 }
   174 
   175 static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params)
   176 {
   177 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   178 	HandleError herr;
   179 	HandleSecurity sec;
   180 	IDataPack *pDataPack;
   181 
   182 	sec.pOwner = pContext->GetIdentity();
   183 	sec.pIdentity = g_pCoreIdent;
   184 
   185 	if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   186 		!= HandleError_None)
   187 	{
   188 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   189 	}
   190 
   191 	if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(float)))
   192 	{
   193 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   194 	}
   195 
   196 	return sp_ftoc(pDataPack->ReadFloat());
   197 }
   198 
   199 static cell_t smn_ReadPackString(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 = pContext->GetIdentity();
   207 	sec.pIdentity = g_pCoreIdent;
   208 
   209 	if ((herr=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 	const char *str;
   216 	if (!(str=pDataPack->ReadString(NULL)))
   217 	{
   218 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   219 	}
   220 
   221 	pContext->StringToLocal(params[2], params[3], str);
   222 
   223 	return 1;
   224 }
   225 
   226 static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params)
   227 {
   228 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   229 	HandleError herr;
   230 	HandleSecurity sec;
   231 	IDataPack *pDataPack;
   232 
   233 	sec.pOwner = pContext->GetIdentity();
   234 	sec.pIdentity = g_pCoreIdent;
   235 
   236 	if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   237 		!= HandleError_None)
   238 	{
   239 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   240 	}
   241 
   242 	pDataPack->Reset();
   243 	if (params[2])
   244 	{
   245 		pDataPack->ResetSize();
   246 	}
   247 
   248 	return 1;
   249 }
   250 
   251 static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params)
   252 {
   253 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   254 	HandleError herr;
   255 	HandleSecurity sec;
   256 	IDataPack *pDataPack;
   257 
   258 	sec.pOwner = pContext->GetIdentity();
   259 	sec.pIdentity = g_pCoreIdent;
   260 
   261 	if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   262 		!= HandleError_None)
   263 	{
   264 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   265 	}
   266 
   267 	return static_cast<cell_t>(pDataPack->GetPosition());
   268 }
   269 
   270 static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params)
   271 {
   272 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   273 	HandleError herr;
   274 	HandleSecurity sec;
   275 	IDataPack *pDataPack;
   276 
   277 	sec.pOwner = pContext->GetIdentity();
   278 	sec.pIdentity = g_pCoreIdent;
   279 
   280 	if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   281 		!= HandleError_None)
   282 	{
   283 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   284 	}
   285 
   286 	if (!pDataPack->SetPosition(params[2]))
   287 	{
   288 		return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]);
   289 	}
   290 
   291 	return 1;
   292 }
   293 
   294 static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params)
   295 {
   296 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   297 	HandleError herr;
   298 	HandleSecurity sec;
   299 	IDataPack *pDataPack;
   300 
   301 	sec.pOwner = pContext->GetIdentity();
   302 	sec.pIdentity = g_pCoreIdent;
   303 
   304 	if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   305 		!= HandleError_None)
   306 	{
   307 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   308 	}
   309 
   310 	return pDataPack->IsReadable(params[2]) ? 1 : 0;
   311 }
   312 
   313 static DataPackNatives s_DataPackNatives;
   314 
   315 REGISTER_NATIVES(datapacknatives)
   316 {
   317 	{"CreateDataPack",			smn_CreateDataPack},
   318 	{"WritePackCell",			smn_WritePackCell},
   319 	{"WritePackFloat",			smn_WritePackFloat},
   320 	{"WritePackString",			smn_WritePackString},
   321 	{"ReadPackCell",			smn_ReadPackCell},
   322 	{"ReadPackFloat",			smn_ReadPackFloat},
   323 	{"ReadPackString",			smn_ReadPackString},
   324 	{"ResetPack",				smn_ResetPack},
   325 	{"GetPackPosition",			smn_GetPackPosition},
   326 	{"SetPackPosition",			smn_SetPackPosition},
   327 	{"IsPackReadable",			smn_IsPackReadable},
   328 	{NULL,						NULL}
   329 };