Removed pointless "systems" folder from core.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "sm_globals.h"
33 #include "HandleSys.h"
34 #include "CDataPack.h"
36 HandleType_t g_DataPackType;
38 class DataPackNatives :
40 public IHandleTypeDispatch
43 void OnSourceModAllInitialized()
48 g_HandleSys.InitAccessDefaults(&tacc, &hacc);
49 tacc.access[HTypeAccess_Create] = true;
50 tacc.access[HTypeAccess_Inherit] = true;
51 tacc.ident = g_pCoreIdent;
52 hacc.access[HandleAccess_Read] = HANDLE_RESTRICT_OWNER;
54 g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, &tacc, &hacc, g_pCoreIdent, NULL);
56 void OnSourceModShutdown()
58 g_HandleSys.RemoveType(g_DataPackType, g_pCoreIdent);
61 void OnHandleDestroy(HandleType_t type, void *object)
63 g_SourceMod.FreeDataPack(reinterpret_cast<IDataPack *>(object));
67 static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
69 IDataPack *pDataPack = g_SourceMod.CreateDataPack();
76 return g_HandleSys.CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL);
79 static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params)
81 Handle_t hndl = static_cast<Handle_t>(params[1]);
86 sec.pOwner = pContext->GetIdentity();
87 sec.pIdentity = g_pCoreIdent;
89 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
92 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
95 pDataPack->PackCell(params[2]);
100 static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params)
102 Handle_t hndl = static_cast<Handle_t>(params[1]);
105 IDataPack *pDataPack;
107 sec.pOwner = pContext->GetIdentity();
108 sec.pIdentity = g_pCoreIdent;
110 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
113 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
116 pDataPack->PackFloat(sp_ctof(params[2]));
121 static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params)
123 Handle_t hndl = static_cast<Handle_t>(params[1]);
126 IDataPack *pDataPack;
129 sec.pOwner = pContext->GetIdentity();
130 sec.pIdentity = g_pCoreIdent;
132 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
135 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
139 if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE)
141 pContext->ThrowNativeErrorEx(err, NULL);
145 pDataPack->PackString(str);
150 static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params)
152 Handle_t hndl = static_cast<Handle_t>(params[1]);
155 IDataPack *pDataPack;
157 sec.pOwner = pContext->GetIdentity();
158 sec.pIdentity = g_pCoreIdent;
160 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
163 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
166 if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(cell_t)))
168 return pContext->ThrowNativeError("DataPack operation is out of bounds.");
171 return pDataPack->ReadCell();
174 static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params)
176 Handle_t hndl = static_cast<Handle_t>(params[1]);
179 IDataPack *pDataPack;
181 sec.pOwner = pContext->GetIdentity();
182 sec.pIdentity = g_pCoreIdent;
184 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
187 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
190 if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(float)))
192 return pContext->ThrowNativeError("DataPack operation is out of bounds.");
195 return sp_ftoc(pDataPack->ReadFloat());
198 static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params)
200 Handle_t hndl = static_cast<Handle_t>(params[1]);
203 IDataPack *pDataPack;
205 sec.pOwner = pContext->GetIdentity();
206 sec.pIdentity = g_pCoreIdent;
208 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
211 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
215 if (!(str=pDataPack->ReadString(NULL)))
217 return pContext->ThrowNativeError("DataPack operation is out of bounds.");
220 pContext->StringToLocal(params[2], params[3], str);
225 static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params)
227 Handle_t hndl = static_cast<Handle_t>(params[1]);
230 IDataPack *pDataPack;
232 sec.pOwner = pContext->GetIdentity();
233 sec.pIdentity = g_pCoreIdent;
235 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
238 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
244 pDataPack->ResetSize();
250 static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params)
252 Handle_t hndl = static_cast<Handle_t>(params[1]);
255 IDataPack *pDataPack;
257 sec.pOwner = pContext->GetIdentity();
258 sec.pIdentity = g_pCoreIdent;
260 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
263 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
266 return static_cast<cell_t>(pDataPack->GetPosition());
269 static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params)
271 Handle_t hndl = static_cast<Handle_t>(params[1]);
274 IDataPack *pDataPack;
276 sec.pOwner = pContext->GetIdentity();
277 sec.pIdentity = g_pCoreIdent;
279 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
282 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
285 if (!pDataPack->SetPosition(params[2]))
287 return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]);
293 static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params)
295 Handle_t hndl = static_cast<Handle_t>(params[1]);
298 IDataPack *pDataPack;
300 sec.pOwner = pContext->GetIdentity();
301 sec.pIdentity = g_pCoreIdent;
303 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
306 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
309 return pDataPack->IsReadable(params[2]) ? 1 : 0;
312 static DataPackNatives s_DataPackNatives;
314 REGISTER_NATIVES(datapacknatives)
316 {"CreateDataPack", smn_CreateDataPack},
317 {"WritePackCell", smn_WritePackCell},
318 {"WritePackFloat", smn_WritePackFloat},
319 {"WritePackString", smn_WritePackString},
320 {"ReadPackCell", smn_ReadPackCell},
321 {"ReadPackFloat", smn_ReadPackFloat},
322 {"ReadPackString", smn_ReadPackString},
323 {"ResetPack", smn_ResetPack},
324 {"GetPackPosition", smn_GetPackPosition},
325 {"SetPackPosition", smn_SetPackPosition},
326 {"IsPackReadable", smn_IsPackReadable},