1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/smn_datapacks.cpp Tue Feb 13 16:11:04 2007 +0000
1.3 @@ -0,0 +1,288 @@
1.4 +#include "sm_globals.h"
1.5 +#include "HandleSys.h"
1.6 +#include "CDataPack.h"
1.7 +
1.8 +HandleType_t g_DataPackType;
1.9 +
1.10 +class DataPackNatives :
1.11 + public SMGlobalClass,
1.12 + public IHandleTypeDispatch
1.13 +{
1.14 +public:
1.15 + void OnSourceModAllInitialized()
1.16 + {
1.17 + g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, NULL, NULL, g_pCoreIdent, NULL);
1.18 + }
1.19 + void OnSourceModShutdown()
1.20 + {
1.21 + g_HandleSys.RemoveType(g_DataPackType, g_pCoreIdent);
1.22 + g_DataPackType = 0;
1.23 + }
1.24 + void OnHandleDestroy(HandleType_t type, void *object)
1.25 + {
1.26 + g_SourceMod.FreeDataPack(reinterpret_cast<IDataPack *>(object));
1.27 + }
1.28 +};
1.29 +
1.30 +static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
1.31 +{
1.32 + IDataPack *pDataPack = g_SourceMod.CreateDataPack();
1.33 +
1.34 + if (!pDataPack)
1.35 + {
1.36 + return 0;
1.37 + }
1.38 +
1.39 + return g_HandleSys.CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL);
1.40 +}
1.41 +
1.42 +static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params)
1.43 +{
1.44 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.45 + HandleError herr;
1.46 + HandleSecurity sec;
1.47 + IDataPack *pDataPack;
1.48 +
1.49 + sec.pOwner = NULL;
1.50 + sec.pIdentity = g_pCoreIdent;
1.51 +
1.52 + if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.53 + != HandleError_None)
1.54 + {
1.55 + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.56 + }
1.57 +
1.58 + pDataPack->PackCell(params[2]);
1.59 +
1.60 + return 1;
1.61 +}
1.62 +
1.63 +static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params)
1.64 +{
1.65 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.66 + HandleError herr;
1.67 + HandleSecurity sec;
1.68 + IDataPack *pDataPack;
1.69 +
1.70 + sec.pOwner = NULL;
1.71 + sec.pIdentity = g_pCoreIdent;
1.72 +
1.73 + if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.74 + != HandleError_None)
1.75 + {
1.76 + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.77 + }
1.78 +
1.79 + pDataPack->PackFloat(sp_ctof(params[2]));
1.80 +
1.81 + return 1;
1.82 +}
1.83 +
1.84 +static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params)
1.85 +{
1.86 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.87 + HandleError herr;
1.88 + HandleSecurity sec;
1.89 + IDataPack *pDataPack;
1.90 + int err;
1.91 +
1.92 + sec.pOwner = NULL;
1.93 + sec.pIdentity = g_pCoreIdent;
1.94 +
1.95 + if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.96 + != HandleError_None)
1.97 + {
1.98 + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.99 + }
1.100 +
1.101 + char *str;
1.102 + if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE)
1.103 + {
1.104 + pContext->ThrowNativeErrorEx(err, NULL);
1.105 + return 0;
1.106 + }
1.107 +
1.108 + pDataPack->PackString(str);
1.109 +
1.110 + return 1;
1.111 +}
1.112 +
1.113 +static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params)
1.114 +{
1.115 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.116 + HandleError herr;
1.117 + HandleSecurity sec;
1.118 + IDataPack *pDataPack;
1.119 +
1.120 + sec.pOwner = NULL;
1.121 + sec.pIdentity = g_pCoreIdent;
1.122 +
1.123 + if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.124 + != HandleError_None)
1.125 + {
1.126 + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.127 + }
1.128 +
1.129 + if (!pDataPack->IsReadable(sizeof(cell_t)))
1.130 + {
1.131 + return pContext->ThrowNativeError("DataPack operation is out of bounds.");
1.132 + }
1.133 +
1.134 + return pDataPack->ReadCell();
1.135 +}
1.136 +
1.137 +static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params)
1.138 +{
1.139 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.140 + HandleError herr;
1.141 + HandleSecurity sec;
1.142 + IDataPack *pDataPack;
1.143 +
1.144 + sec.pOwner = NULL;
1.145 + sec.pIdentity = g_pCoreIdent;
1.146 +
1.147 + if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.148 + != HandleError_None)
1.149 + {
1.150 + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.151 + }
1.152 +
1.153 + if (!pDataPack->IsReadable(sizeof(float)))
1.154 + {
1.155 + return pContext->ThrowNativeError("DataPack operation is out of bounds.");
1.156 + }
1.157 +
1.158 + return sp_ftoc(pDataPack->ReadFloat());
1.159 +}
1.160 +
1.161 +static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params)
1.162 +{
1.163 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.164 + HandleError herr;
1.165 + HandleSecurity sec;
1.166 + IDataPack *pDataPack;
1.167 +
1.168 + sec.pOwner = NULL;
1.169 + sec.pIdentity = g_pCoreIdent;
1.170 +
1.171 + if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.172 + != HandleError_None)
1.173 + {
1.174 + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.175 + }
1.176 +
1.177 + const char *str;
1.178 + if (!(str=pDataPack->ReadString(NULL)))
1.179 + {
1.180 + return pContext->ThrowNativeError("DataPack operation is out of bounds.");
1.181 + }
1.182 +
1.183 + pContext->StringToLocal(params[2], params[3], str);
1.184 +
1.185 + return 1;
1.186 +}
1.187 +
1.188 +static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params)
1.189 +{
1.190 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.191 + HandleError herr;
1.192 + HandleSecurity sec;
1.193 + IDataPack *pDataPack;
1.194 +
1.195 + sec.pOwner = NULL;
1.196 + sec.pIdentity = g_pCoreIdent;
1.197 +
1.198 + if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.199 + != HandleError_None)
1.200 + {
1.201 + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.202 + }
1.203 +
1.204 + pDataPack->Reset();
1.205 + if (params[2])
1.206 + {
1.207 + pDataPack->ResetSize();
1.208 + }
1.209 +
1.210 + return 1;
1.211 +}
1.212 +
1.213 +static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params)
1.214 +{
1.215 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.216 + HandleError herr;
1.217 + HandleSecurity sec;
1.218 + IDataPack *pDataPack;
1.219 +
1.220 + sec.pOwner = NULL;
1.221 + sec.pIdentity = g_pCoreIdent;
1.222 +
1.223 + if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.224 + != HandleError_None)
1.225 + {
1.226 + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.227 + }
1.228 +
1.229 + return static_cast<cell_t>(pDataPack->GetPosition());
1.230 +}
1.231 +
1.232 +static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params)
1.233 +{
1.234 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.235 + HandleError herr;
1.236 + HandleSecurity sec;
1.237 + IDataPack *pDataPack;
1.238 +
1.239 + sec.pOwner = NULL;
1.240 + sec.pIdentity = g_pCoreIdent;
1.241 +
1.242 + if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.243 + != HandleError_None)
1.244 + {
1.245 + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.246 + }
1.247 +
1.248 + if (!pDataPack->SetPosition(params[2]))
1.249 + {
1.250 + return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]);
1.251 + }
1.252 +
1.253 + return 1;
1.254 +}
1.255 +
1.256 +static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params)
1.257 +{
1.258 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.259 + HandleError herr;
1.260 + HandleSecurity sec;
1.261 + IDataPack *pDataPack;
1.262 +
1.263 + sec.pOwner = NULL;
1.264 + sec.pIdentity = g_pCoreIdent;
1.265 +
1.266 + if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
1.267 + != HandleError_None)
1.268 + {
1.269 + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
1.270 + }
1.271 +
1.272 + return pDataPack->IsReadable(params[2]) ? 1 : 0;
1.273 +}
1.274 +
1.275 +static DataPackNatives s_DataPackNatives;
1.276 +
1.277 +REGISTER_NATIVES(datapacknatives)
1.278 +{
1.279 + {"CreateDataPack", smn_CreateDataPack},
1.280 + {"WritePackCell", smn_WritePackCell},
1.281 + {"WritePackFloat", smn_WritePackFloat},
1.282 + {"WritePackString", smn_WritePackString},
1.283 + {"ReadPackCell", smn_ReadPackCell},
1.284 + {"ReadPackFloat", smn_ReadPackFloat},
1.285 + {"ReadPackString", smn_ReadPackString},
1.286 + {"ResetPack", smn_ResetPack},
1.287 + {"GetPackPosition", smn_GetPackPosition},
1.288 + {"SetPackPosition", smn_SetPackPosition},
1.289 + {"IsPackReadable", smn_IsPackReadable},
1.290 + {NULL, NULL}
1.291 +};
1.292 \ No newline at end of file