1.1 --- a/core/smn_datapacks.cpp Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,328 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod
1.8 - * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include "sm_globals.h"
1.36 -#include "HandleSys.h"
1.37 -#include "CDataPack.h"
1.38 -
1.39 -HandleType_t g_DataPackType;
1.40 -
1.41 -class DataPackNatives :
1.42 - public SMGlobalClass,
1.43 - public IHandleTypeDispatch
1.44 -{
1.45 -public:
1.46 - void OnSourceModAllInitialized()
1.47 - {
1.48 - HandleAccess hacc;
1.49 - TypeAccess tacc;
1.50 -
1.51 - g_HandleSys.InitAccessDefaults(&tacc, &hacc);
1.52 - tacc.access[HTypeAccess_Create] = true;
1.53 - tacc.access[HTypeAccess_Inherit] = true;
1.54 - tacc.ident = g_pCoreIdent;
1.55 - hacc.access[HandleAccess_Read] = HANDLE_RESTRICT_OWNER;
1.56 -
1.57 - g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, &tacc, &hacc, g_pCoreIdent, NULL);
1.58 - }
1.59 - void OnSourceModShutdown()
1.60 - {
1.61 - g_HandleSys.RemoveType(g_DataPackType, g_pCoreIdent);
1.62 - g_DataPackType = 0;
1.63 - }
1.64 - void OnHandleDestroy(HandleType_t type, void *object)
1.65 - {
1.66 - g_SourceMod.FreeDataPack(reinterpret_cast<IDataPack *>(object));
1.67 - }
1.68 -};
1.69 -
1.70 -static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
1.71 -{
1.72 - IDataPack *pDataPack = g_SourceMod.CreateDataPack();
1.73 -
1.74 - if (!pDataPack)
1.75 - {
1.76 - return 0;
1.77 - }
1.78 -
1.79 - return g_HandleSys.CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL);
1.80 -}
1.81 -
1.82 -static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params)
1.83 -{
1.84 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.85 - HandleError herr;
1.86 - HandleSecurity sec;
1.87 - IDataPack *pDataPack;
1.88 -
1.89 - sec.pOwner = pContext->GetIdentity();
1.90 - sec.pIdentity = g_pCoreIdent;
1.91 -
1.92 - if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.93 - != HandleError_None)
1.94 - {
1.95 - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.96 - }
1.97 -
1.98 - pDataPack->PackCell(params[2]);
1.99 -
1.100 - return 1;
1.101 -}
1.102 -
1.103 -static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params)
1.104 -{
1.105 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.106 - HandleError herr;
1.107 - HandleSecurity sec;
1.108 - IDataPack *pDataPack;
1.109 -
1.110 - sec.pOwner = pContext->GetIdentity();
1.111 - sec.pIdentity = g_pCoreIdent;
1.112 -
1.113 - if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.114 - != HandleError_None)
1.115 - {
1.116 - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.117 - }
1.118 -
1.119 - pDataPack->PackFloat(sp_ctof(params[2]));
1.120 -
1.121 - return 1;
1.122 -}
1.123 -
1.124 -static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params)
1.125 -{
1.126 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.127 - HandleError herr;
1.128 - HandleSecurity sec;
1.129 - IDataPack *pDataPack;
1.130 - int err;
1.131 -
1.132 - sec.pOwner = pContext->GetIdentity();
1.133 - sec.pIdentity = g_pCoreIdent;
1.134 -
1.135 - if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.136 - != HandleError_None)
1.137 - {
1.138 - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.139 - }
1.140 -
1.141 - char *str;
1.142 - if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE)
1.143 - {
1.144 - pContext->ThrowNativeErrorEx(err, NULL);
1.145 - return 0;
1.146 - }
1.147 -
1.148 - pDataPack->PackString(str);
1.149 -
1.150 - return 1;
1.151 -}
1.152 -
1.153 -static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params)
1.154 -{
1.155 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.156 - HandleError herr;
1.157 - HandleSecurity sec;
1.158 - IDataPack *pDataPack;
1.159 -
1.160 - sec.pOwner = pContext->GetIdentity();
1.161 - sec.pIdentity = g_pCoreIdent;
1.162 -
1.163 - if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.164 - != HandleError_None)
1.165 - {
1.166 - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.167 - }
1.168 -
1.169 - if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(cell_t)))
1.170 - {
1.171 - return pContext->ThrowNativeError("DataPack operation is out of bounds.");
1.172 - }
1.173 -
1.174 - return pDataPack->ReadCell();
1.175 -}
1.176 -
1.177 -static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params)
1.178 -{
1.179 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.180 - HandleError herr;
1.181 - HandleSecurity sec;
1.182 - IDataPack *pDataPack;
1.183 -
1.184 - sec.pOwner = pContext->GetIdentity();
1.185 - sec.pIdentity = g_pCoreIdent;
1.186 -
1.187 - if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.188 - != HandleError_None)
1.189 - {
1.190 - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.191 - }
1.192 -
1.193 - if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(float)))
1.194 - {
1.195 - return pContext->ThrowNativeError("DataPack operation is out of bounds.");
1.196 - }
1.197 -
1.198 - return sp_ftoc(pDataPack->ReadFloat());
1.199 -}
1.200 -
1.201 -static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params)
1.202 -{
1.203 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.204 - HandleError herr;
1.205 - HandleSecurity sec;
1.206 - IDataPack *pDataPack;
1.207 -
1.208 - sec.pOwner = pContext->GetIdentity();
1.209 - sec.pIdentity = g_pCoreIdent;
1.210 -
1.211 - if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.212 - != HandleError_None)
1.213 - {
1.214 - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.215 - }
1.216 -
1.217 - const char *str;
1.218 - if (!(str=pDataPack->ReadString(NULL)))
1.219 - {
1.220 - return pContext->ThrowNativeError("DataPack operation is out of bounds.");
1.221 - }
1.222 -
1.223 - pContext->StringToLocal(params[2], params[3], str);
1.224 -
1.225 - return 1;
1.226 -}
1.227 -
1.228 -static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params)
1.229 -{
1.230 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.231 - HandleError herr;
1.232 - HandleSecurity sec;
1.233 - IDataPack *pDataPack;
1.234 -
1.235 - sec.pOwner = pContext->GetIdentity();
1.236 - sec.pIdentity = g_pCoreIdent;
1.237 -
1.238 - if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.239 - != HandleError_None)
1.240 - {
1.241 - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.242 - }
1.243 -
1.244 - pDataPack->Reset();
1.245 - if (params[2])
1.246 - {
1.247 - pDataPack->ResetSize();
1.248 - }
1.249 -
1.250 - return 1;
1.251 -}
1.252 -
1.253 -static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params)
1.254 -{
1.255 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.256 - HandleError herr;
1.257 - HandleSecurity sec;
1.258 - IDataPack *pDataPack;
1.259 -
1.260 - sec.pOwner = pContext->GetIdentity();
1.261 - sec.pIdentity = g_pCoreIdent;
1.262 -
1.263 - if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.264 - != HandleError_None)
1.265 - {
1.266 - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.267 - }
1.268 -
1.269 - return static_cast<cell_t>(pDataPack->GetPosition());
1.270 -}
1.271 -
1.272 -static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params)
1.273 -{
1.274 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.275 - HandleError herr;
1.276 - HandleSecurity sec;
1.277 - IDataPack *pDataPack;
1.278 -
1.279 - sec.pOwner = pContext->GetIdentity();
1.280 - sec.pIdentity = g_pCoreIdent;
1.281 -
1.282 - if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.283 - != HandleError_None)
1.284 - {
1.285 - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.286 - }
1.287 -
1.288 - if (!pDataPack->SetPosition(params[2]))
1.289 - {
1.290 - return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]);
1.291 - }
1.292 -
1.293 - return 1;
1.294 -}
1.295 -
1.296 -static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params)
1.297 -{
1.298 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.299 - HandleError herr;
1.300 - HandleSecurity sec;
1.301 - IDataPack *pDataPack;
1.302 -
1.303 - sec.pOwner = pContext->GetIdentity();
1.304 - sec.pIdentity = g_pCoreIdent;
1.305 -
1.306 - if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.307 - != HandleError_None)
1.308 - {
1.309 - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.310 - }
1.311 -
1.312 - return pDataPack->IsReadable(params[2]) ? 1 : 0;
1.313 -}
1.314 -
1.315 -static DataPackNatives s_DataPackNatives;
1.316 -
1.317 -REGISTER_NATIVES(datapacknatives)
1.318 -{
1.319 - {"CreateDataPack", smn_CreateDataPack},
1.320 - {"WritePackCell", smn_WritePackCell},
1.321 - {"WritePackFloat", smn_WritePackFloat},
1.322 - {"WritePackString", smn_WritePackString},
1.323 - {"ReadPackCell", smn_ReadPackCell},
1.324 - {"ReadPackFloat", smn_ReadPackFloat},
1.325 - {"ReadPackString", smn_ReadPackString},
1.326 - {"ResetPack", smn_ResetPack},
1.327 - {"GetPackPosition", smn_GetPackPosition},
1.328 - {"SetPackPosition", smn_SetPackPosition},
1.329 - {"IsPackReadable", smn_IsPackReadable},
1.330 - {NULL, NULL}
1.331 -};