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