1 #include "sm_globals.h"
5 HandleType_t g_DataPackType;
7 class DataPackNatives :
9 public IHandleTypeDispatch
12 void OnSourceModAllInitialized()
14 g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, NULL, NULL, g_pCoreIdent, NULL);
16 void OnSourceModShutdown()
18 g_HandleSys.RemoveType(g_DataPackType, g_pCoreIdent);
21 void OnHandleDestroy(HandleType_t type, void *object)
23 g_SourceMod.FreeDataPack(reinterpret_cast<IDataPack *>(object));
27 static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
29 IDataPack *pDataPack = g_SourceMod.CreateDataPack();
36 return g_HandleSys.CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL);
39 static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params)
41 Handle_t hndl = static_cast<Handle_t>(params[1]);
47 sec.pIdentity = g_pCoreIdent;
49 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
52 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
55 pDataPack->PackCell(params[2]);
60 static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params)
62 Handle_t hndl = static_cast<Handle_t>(params[1]);
68 sec.pIdentity = g_pCoreIdent;
70 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
73 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
76 pDataPack->PackFloat(sp_ctof(params[2]));
81 static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params)
83 Handle_t hndl = static_cast<Handle_t>(params[1]);
90 sec.pIdentity = g_pCoreIdent;
92 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
95 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
99 if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE)
101 pContext->ThrowNativeErrorEx(err, NULL);
105 pDataPack->PackString(str);
110 static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params)
112 Handle_t hndl = static_cast<Handle_t>(params[1]);
115 IDataPack *pDataPack;
118 sec.pIdentity = g_pCoreIdent;
120 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
123 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
126 if (!pDataPack->IsReadable(sizeof(cell_t)))
128 return pContext->ThrowNativeError("DataPack operation is out of bounds.");
131 return pDataPack->ReadCell();
134 static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params)
136 Handle_t hndl = static_cast<Handle_t>(params[1]);
139 IDataPack *pDataPack;
142 sec.pIdentity = g_pCoreIdent;
144 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
147 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
150 if (!pDataPack->IsReadable(sizeof(float)))
152 return pContext->ThrowNativeError("DataPack operation is out of bounds.");
155 return sp_ftoc(pDataPack->ReadFloat());
158 static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params)
160 Handle_t hndl = static_cast<Handle_t>(params[1]);
163 IDataPack *pDataPack;
166 sec.pIdentity = g_pCoreIdent;
168 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
171 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
175 if (!(str=pDataPack->ReadString(NULL)))
177 return pContext->ThrowNativeError("DataPack operation is out of bounds.");
180 pContext->StringToLocal(params[2], params[3], str);
185 static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params)
187 Handle_t hndl = static_cast<Handle_t>(params[1]);
190 IDataPack *pDataPack;
193 sec.pIdentity = g_pCoreIdent;
195 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
198 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
204 pDataPack->ResetSize();
210 static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params)
212 Handle_t hndl = static_cast<Handle_t>(params[1]);
215 IDataPack *pDataPack;
218 sec.pIdentity = g_pCoreIdent;
220 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
223 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
226 return static_cast<cell_t>(pDataPack->GetPosition());
229 static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params)
231 Handle_t hndl = static_cast<Handle_t>(params[1]);
234 IDataPack *pDataPack;
237 sec.pIdentity = g_pCoreIdent;
239 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
242 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
245 if (!pDataPack->SetPosition(params[2]))
247 return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]);
253 static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params)
255 Handle_t hndl = static_cast<Handle_t>(params[1]);
258 IDataPack *pDataPack;
261 sec.pIdentity = g_pCoreIdent;
263 if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
266 return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
269 return pDataPack->IsReadable(params[2]) ? 1 : 0;
272 static DataPackNatives s_DataPackNatives;
274 REGISTER_NATIVES(datapacknatives)
276 {"CreateDataPack", smn_CreateDataPack},
277 {"WritePackCell", smn_WritePackCell},
278 {"WritePackFloat", smn_WritePackFloat},
279 {"WritePackString", smn_WritePackString},
280 {"ReadPackCell", smn_ReadPackCell},
281 {"ReadPackFloat", smn_ReadPackFloat},
282 {"ReadPackString", smn_ReadPackString},
283 {"ResetPack", smn_ResetPack},
284 {"GetPackPosition", smn_GetPackPosition},
285 {"SetPackPosition", smn_SetPackPosition},
286 {"IsPackReadable", smn_IsPackReadable},