ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #include "sm_globals.h" ds@1952: #include "HandleSys.h" ds@1952: #include "CDataPack.h" ds@1952: ds@1952: HandleType_t g_DataPackType; ds@1952: ds@1952: class DataPackNatives : ds@1952: public SMGlobalClass, ds@1952: public IHandleTypeDispatch ds@1952: { ds@1952: public: ds@1952: void OnSourceModAllInitialized() ds@1952: { ds@1952: HandleAccess hacc; ds@1952: TypeAccess tacc; ds@1952: ds@1952: g_HandleSys.InitAccessDefaults(&tacc, &hacc); ds@1952: tacc.access[HTypeAccess_Create] = true; ds@1952: tacc.access[HTypeAccess_Inherit] = true; ds@1952: tacc.ident = g_pCoreIdent; ds@1952: hacc.access[HandleAccess_Read] = HANDLE_RESTRICT_OWNER; ds@1952: ds@1952: g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, &tacc, &hacc, g_pCoreIdent, NULL); ds@1952: } ds@1952: void OnSourceModShutdown() ds@1952: { ds@1952: g_HandleSys.RemoveType(g_DataPackType, g_pCoreIdent); ds@1952: g_DataPackType = 0; ds@1952: } ds@1952: void OnHandleDestroy(HandleType_t type, void *object) ds@1952: { ds@1952: g_SourceMod.FreeDataPack(reinterpret_cast(object)); ds@1952: } ds@1952: }; ds@1952: ds@1952: static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: IDataPack *pDataPack = g_SourceMod.CreateDataPack(); ds@1952: ds@1952: if (!pDataPack) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: return g_HandleSys.CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL); ds@1952: } ds@1952: ds@1952: static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: IDataPack *pDataPack; ds@1952: ds@1952: sec.pOwner = pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: pDataPack->PackCell(params[2]); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: IDataPack *pDataPack; ds@1952: ds@1952: sec.pOwner = pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: pDataPack->PackFloat(sp_ctof(params[2])); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: IDataPack *pDataPack; ds@1952: int err; ds@1952: ds@1952: sec.pOwner = pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *str; ds@1952: if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: pDataPack->PackString(str); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: IDataPack *pDataPack; ds@1952: ds@1952: sec.pOwner = pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(cell_t))) ds@1952: { ds@1952: return pContext->ThrowNativeError("DataPack operation is out of bounds."); ds@1952: } ds@1952: ds@1952: return pDataPack->ReadCell(); ds@1952: } ds@1952: ds@1952: static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: IDataPack *pDataPack; ds@1952: ds@1952: sec.pOwner = pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(float))) ds@1952: { ds@1952: return pContext->ThrowNativeError("DataPack operation is out of bounds."); ds@1952: } ds@1952: ds@1952: return sp_ftoc(pDataPack->ReadFloat()); ds@1952: } ds@1952: ds@1952: static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: IDataPack *pDataPack; ds@1952: ds@1952: sec.pOwner = pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: const char *str; ds@1952: if (!(str=pDataPack->ReadString(NULL))) ds@1952: { ds@1952: return pContext->ThrowNativeError("DataPack operation is out of bounds."); ds@1952: } ds@1952: ds@1952: pContext->StringToLocal(params[2], params[3], str); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: IDataPack *pDataPack; ds@1952: ds@1952: sec.pOwner = pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: pDataPack->Reset(); ds@1952: if (params[2]) ds@1952: { ds@1952: pDataPack->ResetSize(); ds@1952: } ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: IDataPack *pDataPack; ds@1952: ds@1952: sec.pOwner = pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: return static_cast(pDataPack->GetPosition()); ds@1952: } ds@1952: ds@1952: static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: IDataPack *pDataPack; ds@1952: ds@1952: sec.pOwner = pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: if (!pDataPack->SetPosition(params[2])) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]); ds@1952: } ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: IDataPack *pDataPack; ds@1952: ds@1952: sec.pOwner = pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: return pDataPack->IsReadable(params[2]) ? 1 : 0; ds@1952: } ds@1952: ds@1952: static DataPackNatives s_DataPackNatives; ds@1952: ds@1952: REGISTER_NATIVES(datapacknatives) ds@1952: { ds@1952: {"CreateDataPack", smn_CreateDataPack}, ds@1952: {"WritePackCell", smn_WritePackCell}, ds@1952: {"WritePackFloat", smn_WritePackFloat}, ds@1952: {"WritePackString", smn_WritePackString}, ds@1952: {"ReadPackCell", smn_ReadPackCell}, ds@1952: {"ReadPackFloat", smn_ReadPackFloat}, ds@1952: {"ReadPackString", smn_ReadPackString}, ds@1952: {"ResetPack", smn_ResetPack}, ds@1952: {"GetPackPosition", smn_GetPackPosition}, ds@1952: {"SetPackPosition", smn_SetPackPosition}, ds@1952: {"IsPackReadable", smn_IsPackReadable}, ds@1952: {NULL, NULL} ds@1952: };