|
faluco@490
|
1 |
/** |
|
dvander@600
|
2 |
* vim: set ts=4 : |
|
dvander@600
|
3 |
* =============================================================== |
|
dvander@600
|
4 |
* SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. |
|
dvander@600
|
5 |
* =============================================================== |
|
dvander@600
|
6 |
* |
|
dvander@600
|
7 |
* This file is not open source and may not be copied without explicit |
|
dvander@600
|
8 |
* written permission of AlliedModders LLC. This file may not be redistributed |
|
dvander@600
|
9 |
* in whole or significant part. |
|
dvander@600
|
10 |
* For information, see LICENSE.txt or http://www.sourcemod.net/license.php |
|
dvander@600
|
11 |
* |
|
dvander@600
|
12 |
* Version: $Id$ |
|
dvander@600
|
13 |
*/ |
|
faluco@490
|
14 |
|
|
faluco@484
|
15 |
#include "sm_globals.h" |
|
faluco@484
|
16 |
#include "HandleSys.h" |
|
faluco@484
|
17 |
#include "CDataPack.h" |
|
faluco@484
|
18 |
|
|
faluco@484
|
19 |
HandleType_t g_DataPackType; |
|
faluco@484
|
20 |
|
|
faluco@484
|
21 |
class DataPackNatives : |
|
faluco@484
|
22 |
public SMGlobalClass, |
|
faluco@484
|
23 |
public IHandleTypeDispatch |
|
faluco@484
|
24 |
{ |
|
faluco@484
|
25 |
public: |
|
faluco@484
|
26 |
void OnSourceModAllInitialized() |
|
faluco@484
|
27 |
{ |
|
faluco@484
|
28 |
g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, NULL, NULL, g_pCoreIdent, NULL); |
|
faluco@484
|
29 |
} |
|
faluco@484
|
30 |
void OnSourceModShutdown() |
|
faluco@484
|
31 |
{ |
|
faluco@484
|
32 |
g_HandleSys.RemoveType(g_DataPackType, g_pCoreIdent); |
|
faluco@484
|
33 |
g_DataPackType = 0; |
|
faluco@484
|
34 |
} |
|
faluco@484
|
35 |
void OnHandleDestroy(HandleType_t type, void *object) |
|
faluco@484
|
36 |
{ |
|
faluco@484
|
37 |
g_SourceMod.FreeDataPack(reinterpret_cast<IDataPack *>(object)); |
|
faluco@484
|
38 |
} |
|
faluco@484
|
39 |
}; |
|
faluco@484
|
40 |
|
|
faluco@484
|
41 |
static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params) |
|
faluco@484
|
42 |
{ |
|
faluco@484
|
43 |
IDataPack *pDataPack = g_SourceMod.CreateDataPack(); |
|
faluco@484
|
44 |
|
|
faluco@484
|
45 |
if (!pDataPack) |
|
faluco@484
|
46 |
{ |
|
faluco@484
|
47 |
return 0; |
|
faluco@484
|
48 |
} |
|
faluco@484
|
49 |
|
|
faluco@484
|
50 |
return g_HandleSys.CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL); |
|
faluco@484
|
51 |
} |
|
faluco@484
|
52 |
|
|
faluco@484
|
53 |
static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params) |
|
faluco@484
|
54 |
{ |
|
faluco@484
|
55 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
faluco@484
|
56 |
HandleError herr; |
|
faluco@484
|
57 |
HandleSecurity sec; |
|
faluco@484
|
58 |
IDataPack *pDataPack; |
|
faluco@484
|
59 |
|
|
faluco@484
|
60 |
sec.pOwner = NULL; |
|
faluco@484
|
61 |
sec.pIdentity = g_pCoreIdent; |
|
faluco@484
|
62 |
|
|
faluco@484
|
63 |
if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) |
|
faluco@484
|
64 |
!= HandleError_None) |
|
faluco@484
|
65 |
{ |
|
faluco@484
|
66 |
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); |
|
faluco@484
|
67 |
} |
|
faluco@484
|
68 |
|
|
faluco@484
|
69 |
pDataPack->PackCell(params[2]); |
|
faluco@484
|
70 |
|
|
faluco@484
|
71 |
return 1; |
|
faluco@484
|
72 |
} |
|
faluco@484
|
73 |
|
|
faluco@484
|
74 |
static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params) |
|
faluco@484
|
75 |
{ |
|
faluco@484
|
76 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
faluco@484
|
77 |
HandleError herr; |
|
faluco@484
|
78 |
HandleSecurity sec; |
|
faluco@484
|
79 |
IDataPack *pDataPack; |
|
faluco@484
|
80 |
|
|
faluco@484
|
81 |
sec.pOwner = NULL; |
|
faluco@484
|
82 |
sec.pIdentity = g_pCoreIdent; |
|
faluco@484
|
83 |
|
|
faluco@484
|
84 |
if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) |
|
faluco@484
|
85 |
!= HandleError_None) |
|
faluco@484
|
86 |
{ |
|
faluco@484
|
87 |
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); |
|
faluco@484
|
88 |
} |
|
faluco@484
|
89 |
|
|
faluco@484
|
90 |
pDataPack->PackFloat(sp_ctof(params[2])); |
|
faluco@484
|
91 |
|
|
faluco@484
|
92 |
return 1; |
|
faluco@484
|
93 |
} |
|
faluco@484
|
94 |
|
|
faluco@484
|
95 |
static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params) |
|
faluco@484
|
96 |
{ |
|
faluco@484
|
97 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
faluco@484
|
98 |
HandleError herr; |
|
faluco@484
|
99 |
HandleSecurity sec; |
|
faluco@484
|
100 |
IDataPack *pDataPack; |
|
faluco@484
|
101 |
int err; |
|
faluco@484
|
102 |
|
|
faluco@484
|
103 |
sec.pOwner = NULL; |
|
faluco@484
|
104 |
sec.pIdentity = g_pCoreIdent; |
|
faluco@484
|
105 |
|
|
faluco@484
|
106 |
if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) |
|
faluco@484
|
107 |
!= HandleError_None) |
|
faluco@484
|
108 |
{ |
|
faluco@484
|
109 |
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); |
|
faluco@484
|
110 |
} |
|
faluco@484
|
111 |
|
|
faluco@484
|
112 |
char *str; |
|
faluco@484
|
113 |
if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE) |
|
faluco@484
|
114 |
{ |
|
faluco@484
|
115 |
pContext->ThrowNativeErrorEx(err, NULL); |
|
faluco@484
|
116 |
return 0; |
|
faluco@484
|
117 |
} |
|
faluco@484
|
118 |
|
|
faluco@484
|
119 |
pDataPack->PackString(str); |
|
faluco@484
|
120 |
|
|
faluco@484
|
121 |
return 1; |
|
faluco@484
|
122 |
} |
|
faluco@484
|
123 |
|
|
faluco@484
|
124 |
static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params) |
|
faluco@484
|
125 |
{ |
|
faluco@484
|
126 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
faluco@484
|
127 |
HandleError herr; |
|
faluco@484
|
128 |
HandleSecurity sec; |
|
faluco@484
|
129 |
IDataPack *pDataPack; |
|
faluco@484
|
130 |
|
|
faluco@484
|
131 |
sec.pOwner = NULL; |
|
faluco@484
|
132 |
sec.pIdentity = g_pCoreIdent; |
|
faluco@484
|
133 |
|
|
faluco@484
|
134 |
if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) |
|
faluco@484
|
135 |
!= HandleError_None) |
|
faluco@484
|
136 |
{ |
|
faluco@484
|
137 |
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); |
|
faluco@484
|
138 |
} |
|
faluco@484
|
139 |
|
|
faluco@488
|
140 |
if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(cell_t))) |
|
faluco@484
|
141 |
{ |
|
faluco@484
|
142 |
return pContext->ThrowNativeError("DataPack operation is out of bounds."); |
|
faluco@484
|
143 |
} |
|
faluco@484
|
144 |
|
|
faluco@484
|
145 |
return pDataPack->ReadCell(); |
|
faluco@484
|
146 |
} |
|
faluco@484
|
147 |
|
|
faluco@484
|
148 |
static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params) |
|
faluco@484
|
149 |
{ |
|
faluco@484
|
150 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
faluco@484
|
151 |
HandleError herr; |
|
faluco@484
|
152 |
HandleSecurity sec; |
|
faluco@484
|
153 |
IDataPack *pDataPack; |
|
faluco@484
|
154 |
|
|
faluco@484
|
155 |
sec.pOwner = NULL; |
|
faluco@484
|
156 |
sec.pIdentity = g_pCoreIdent; |
|
faluco@484
|
157 |
|
|
faluco@484
|
158 |
if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) |
|
faluco@484
|
159 |
!= HandleError_None) |
|
faluco@484
|
160 |
{ |
|
faluco@484
|
161 |
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); |
|
faluco@484
|
162 |
} |
|
faluco@484
|
163 |
|
|
faluco@488
|
164 |
if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(float))) |
|
faluco@484
|
165 |
{ |
|
faluco@484
|
166 |
return pContext->ThrowNativeError("DataPack operation is out of bounds."); |
|
faluco@484
|
167 |
} |
|
faluco@484
|
168 |
|
|
faluco@484
|
169 |
return sp_ftoc(pDataPack->ReadFloat()); |
|
faluco@484
|
170 |
} |
|
faluco@484
|
171 |
|
|
faluco@484
|
172 |
static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params) |
|
faluco@484
|
173 |
{ |
|
faluco@484
|
174 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
faluco@484
|
175 |
HandleError herr; |
|
faluco@484
|
176 |
HandleSecurity sec; |
|
faluco@484
|
177 |
IDataPack *pDataPack; |
|
faluco@484
|
178 |
|
|
faluco@484
|
179 |
sec.pOwner = NULL; |
|
faluco@484
|
180 |
sec.pIdentity = g_pCoreIdent; |
|
faluco@484
|
181 |
|
|
faluco@484
|
182 |
if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) |
|
faluco@484
|
183 |
!= HandleError_None) |
|
faluco@484
|
184 |
{ |
|
faluco@484
|
185 |
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); |
|
faluco@484
|
186 |
} |
|
faluco@484
|
187 |
|
|
faluco@484
|
188 |
const char *str; |
|
faluco@484
|
189 |
if (!(str=pDataPack->ReadString(NULL))) |
|
faluco@484
|
190 |
{ |
|
faluco@484
|
191 |
return pContext->ThrowNativeError("DataPack operation is out of bounds."); |
|
faluco@484
|
192 |
} |
|
faluco@484
|
193 |
|
|
faluco@484
|
194 |
pContext->StringToLocal(params[2], params[3], str); |
|
faluco@484
|
195 |
|
|
faluco@484
|
196 |
return 1; |
|
faluco@484
|
197 |
} |
|
faluco@484
|
198 |
|
|
faluco@484
|
199 |
static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params) |
|
faluco@484
|
200 |
{ |
|
faluco@484
|
201 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
faluco@484
|
202 |
HandleError herr; |
|
faluco@484
|
203 |
HandleSecurity sec; |
|
faluco@484
|
204 |
IDataPack *pDataPack; |
|
faluco@484
|
205 |
|
|
faluco@484
|
206 |
sec.pOwner = NULL; |
|
faluco@484
|
207 |
sec.pIdentity = g_pCoreIdent; |
|
faluco@484
|
208 |
|
|
faluco@484
|
209 |
if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) |
|
faluco@484
|
210 |
!= HandleError_None) |
|
faluco@484
|
211 |
{ |
|
faluco@484
|
212 |
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); |
|
faluco@484
|
213 |
} |
|
faluco@484
|
214 |
|
|
faluco@484
|
215 |
pDataPack->Reset(); |
|
faluco@484
|
216 |
if (params[2]) |
|
faluco@484
|
217 |
{ |
|
faluco@484
|
218 |
pDataPack->ResetSize(); |
|
faluco@484
|
219 |
} |
|
faluco@484
|
220 |
|
|
faluco@484
|
221 |
return 1; |
|
faluco@484
|
222 |
} |
|
faluco@484
|
223 |
|
|
faluco@484
|
224 |
static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params) |
|
faluco@484
|
225 |
{ |
|
faluco@484
|
226 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
faluco@484
|
227 |
HandleError herr; |
|
faluco@484
|
228 |
HandleSecurity sec; |
|
faluco@484
|
229 |
IDataPack *pDataPack; |
|
faluco@484
|
230 |
|
|
faluco@484
|
231 |
sec.pOwner = NULL; |
|
faluco@484
|
232 |
sec.pIdentity = g_pCoreIdent; |
|
faluco@484
|
233 |
|
|
faluco@484
|
234 |
if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) |
|
faluco@484
|
235 |
!= HandleError_None) |
|
faluco@484
|
236 |
{ |
|
faluco@484
|
237 |
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); |
|
faluco@484
|
238 |
} |
|
faluco@484
|
239 |
|
|
faluco@484
|
240 |
return static_cast<cell_t>(pDataPack->GetPosition()); |
|
faluco@484
|
241 |
} |
|
faluco@484
|
242 |
|
|
faluco@484
|
243 |
static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params) |
|
faluco@484
|
244 |
{ |
|
faluco@484
|
245 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
faluco@484
|
246 |
HandleError herr; |
|
faluco@484
|
247 |
HandleSecurity sec; |
|
faluco@484
|
248 |
IDataPack *pDataPack; |
|
faluco@484
|
249 |
|
|
faluco@484
|
250 |
sec.pOwner = NULL; |
|
faluco@484
|
251 |
sec.pIdentity = g_pCoreIdent; |
|
faluco@484
|
252 |
|
|
faluco@484
|
253 |
if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) |
|
faluco@484
|
254 |
!= HandleError_None) |
|
faluco@484
|
255 |
{ |
|
faluco@484
|
256 |
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); |
|
faluco@484
|
257 |
} |
|
faluco@484
|
258 |
|
|
faluco@484
|
259 |
if (!pDataPack->SetPosition(params[2])) |
|
faluco@484
|
260 |
{ |
|
faluco@484
|
261 |
return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]); |
|
faluco@484
|
262 |
} |
|
faluco@484
|
263 |
|
|
faluco@484
|
264 |
return 1; |
|
faluco@484
|
265 |
} |
|
faluco@484
|
266 |
|
|
faluco@484
|
267 |
static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params) |
|
faluco@484
|
268 |
{ |
|
faluco@484
|
269 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
faluco@484
|
270 |
HandleError herr; |
|
faluco@484
|
271 |
HandleSecurity sec; |
|
faluco@484
|
272 |
IDataPack *pDataPack; |
|
faluco@484
|
273 |
|
|
faluco@484
|
274 |
sec.pOwner = NULL; |
|
faluco@484
|
275 |
sec.pIdentity = g_pCoreIdent; |
|
faluco@484
|
276 |
|
|
faluco@484
|
277 |
if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) |
|
faluco@484
|
278 |
!= HandleError_None) |
|
faluco@484
|
279 |
{ |
|
faluco@484
|
280 |
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); |
|
faluco@484
|
281 |
} |
|
faluco@484
|
282 |
|
|
faluco@484
|
283 |
return pDataPack->IsReadable(params[2]) ? 1 : 0; |
|
faluco@484
|
284 |
} |
|
faluco@484
|
285 |
|
|
faluco@484
|
286 |
static DataPackNatives s_DataPackNatives; |
|
faluco@484
|
287 |
|
|
faluco@484
|
288 |
REGISTER_NATIVES(datapacknatives) |
|
faluco@484
|
289 |
{ |
|
faluco@484
|
290 |
{"CreateDataPack", smn_CreateDataPack}, |
|
faluco@484
|
291 |
{"WritePackCell", smn_WritePackCell}, |
|
faluco@484
|
292 |
{"WritePackFloat", smn_WritePackFloat}, |
|
faluco@484
|
293 |
{"WritePackString", smn_WritePackString}, |
|
faluco@484
|
294 |
{"ReadPackCell", smn_ReadPackCell}, |
|
faluco@484
|
295 |
{"ReadPackFloat", smn_ReadPackFloat}, |
|
faluco@484
|
296 |
{"ReadPackString", smn_ReadPackString}, |
|
faluco@484
|
297 |
{"ResetPack", smn_ResetPack}, |
|
faluco@484
|
298 |
{"GetPackPosition", smn_GetPackPosition}, |
|
faluco@484
|
299 |
{"SetPackPosition", smn_SetPackPosition}, |
|
faluco@484
|
300 |
{"IsPackReadable", smn_IsPackReadable}, |
|
faluco@484
|
301 |
{NULL, NULL} |
|
faluco@485
|
302 |
}; |