Added amb<536> requested for using sm_kick without putting quotes around reason.
3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
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
15 #include "sm_globals.h"
16 #include "HandleSys.h"
17 #include "CDataPack.h"
19 HandleType_t g_DataPackType;
21 class DataPackNatives :
23 public IHandleTypeDispatch
26 void OnSourceModAllInitialized()
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;
37 g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, &tacc, &hacc, g_pCoreIdent, NULL);
39 void OnSourceModShutdown()
41 g_HandleSys.RemoveType(g_DataPackType, g_pCoreIdent);
44 void OnHandleDestroy(HandleType_t type, void *object)
46 g_SourceMod.FreeDataPack(reinterpret_cast<IDataPack *>(object));
50 static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
52 IDataPack *pDataPack = g_SourceMod.CreateDataPack();
59 return g_HandleSys.CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL);
62 static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params)
64 Handle_t hndl = static_cast<Handle_t>(params[1]);
69 sec.pOwner = pContext->GetIdentity();
70 sec.pIdentity = g_pCoreIdent;
72 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
75 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
78 pDataPack->PackCell(params[2]);
83 static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params)
85 Handle_t hndl = static_cast<Handle_t>(params[1]);
90 sec.pOwner = pContext->GetIdentity();
91 sec.pIdentity = g_pCoreIdent;
93 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
96 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
99 pDataPack->PackFloat(sp_ctof(params[2]));
104 static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params)
106 Handle_t hndl = static_cast<Handle_t>(params[1]);
109 IDataPack *pDataPack;
112 sec.pOwner = pContext->GetIdentity();
113 sec.pIdentity = g_pCoreIdent;
115 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
118 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
122 if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE)
124 pContext->ThrowNativeErrorEx(err, NULL);
128 pDataPack->PackString(str);
133 static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params)
135 Handle_t hndl = static_cast<Handle_t>(params[1]);
138 IDataPack *pDataPack;
140 sec.pOwner = pContext->GetIdentity();
141 sec.pIdentity = g_pCoreIdent;
143 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
146 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
149 if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(cell_t)))
151 return pContext->ThrowNativeError("DataPack operation is out of bounds.");
154 return pDataPack->ReadCell();
157 static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params)
159 Handle_t hndl = static_cast<Handle_t>(params[1]);
162 IDataPack *pDataPack;
164 sec.pOwner = pContext->GetIdentity();
165 sec.pIdentity = g_pCoreIdent;
167 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
170 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
173 if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(float)))
175 return pContext->ThrowNativeError("DataPack operation is out of bounds.");
178 return sp_ftoc(pDataPack->ReadFloat());
181 static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params)
183 Handle_t hndl = static_cast<Handle_t>(params[1]);
186 IDataPack *pDataPack;
188 sec.pOwner = pContext->GetIdentity();
189 sec.pIdentity = g_pCoreIdent;
191 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
194 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
198 if (!(str=pDataPack->ReadString(NULL)))
200 return pContext->ThrowNativeError("DataPack operation is out of bounds.");
203 pContext->StringToLocal(params[2], params[3], str);
208 static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params)
210 Handle_t hndl = static_cast<Handle_t>(params[1]);
213 IDataPack *pDataPack;
215 sec.pOwner = pContext->GetIdentity();
216 sec.pIdentity = g_pCoreIdent;
218 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
221 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
227 pDataPack->ResetSize();
233 static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params)
235 Handle_t hndl = static_cast<Handle_t>(params[1]);
238 IDataPack *pDataPack;
240 sec.pOwner = pContext->GetIdentity();
241 sec.pIdentity = g_pCoreIdent;
243 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
246 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
249 return static_cast<cell_t>(pDataPack->GetPosition());
252 static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params)
254 Handle_t hndl = static_cast<Handle_t>(params[1]);
257 IDataPack *pDataPack;
259 sec.pOwner = pContext->GetIdentity();
260 sec.pIdentity = g_pCoreIdent;
262 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
265 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
268 if (!pDataPack->SetPosition(params[2]))
270 return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]);
276 static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params)
278 Handle_t hndl = static_cast<Handle_t>(params[1]);
281 IDataPack *pDataPack;
283 sec.pOwner = pContext->GetIdentity();
284 sec.pIdentity = g_pCoreIdent;
286 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
289 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
292 return pDataPack->IsReadable(params[2]) ? 1 : 0;
295 static DataPackNatives s_DataPackNatives;
297 REGISTER_NATIVES(datapacknatives)
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},