core/smn_datapacks.cpp
author Borja Ferrer <faluco@alliedmods.net>
Tue Feb 13 16:39:47 2007 +0000 (2007-02-13)
changeset 485 73bad6a148b9
parent 484 d254e257cac2
child 488 daf1bd692ef3
permissions -rw-r--r--
new lines for linux
     1 #include "sm_globals.h"
     2 #include "HandleSys.h"
     3 #include "CDataPack.h"
     4 
     5 HandleType_t g_DataPackType;
     6 
     7 class DataPackNatives : 
     8 	public SMGlobalClass,
     9 	public IHandleTypeDispatch
    10 {
    11 public:
    12 	void OnSourceModAllInitialized()
    13 	{
    14 		g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    15 	}
    16 	void OnSourceModShutdown()
    17 	{
    18 		g_HandleSys.RemoveType(g_DataPackType, g_pCoreIdent);
    19 		g_DataPackType = 0;
    20 	}
    21 	void OnHandleDestroy(HandleType_t type, void *object)
    22 	{
    23 		g_SourceMod.FreeDataPack(reinterpret_cast<IDataPack *>(object));
    24 	}
    25 };
    26 
    27 static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
    28 {
    29 	IDataPack *pDataPack = g_SourceMod.CreateDataPack();
    30 
    31 	if (!pDataPack)
    32 	{
    33 		return 0;
    34 	}
    35 
    36 	return g_HandleSys.CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL);
    37 }
    38 
    39 static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params)
    40 {
    41 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    42 	HandleError herr;
    43 	HandleSecurity sec;
    44 	IDataPack *pDataPack;
    45 
    46 	sec.pOwner = NULL;
    47 	sec.pIdentity = g_pCoreIdent;
    48 
    49 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
    50 		!= HandleError_None)
    51 	{
    52 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
    53 	}
    54 
    55 	pDataPack->PackCell(params[2]);
    56 
    57 	return 1;
    58 }
    59 
    60 static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params)
    61 {
    62 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    63 	HandleError herr;
    64 	HandleSecurity sec;
    65 	IDataPack *pDataPack;
    66 
    67 	sec.pOwner = NULL;
    68 	sec.pIdentity = g_pCoreIdent;
    69 
    70 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
    71 		!= HandleError_None)
    72 	{
    73 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
    74 	}
    75 
    76 	pDataPack->PackFloat(sp_ctof(params[2]));
    77 
    78 	return 1;
    79 }
    80 
    81 static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params)
    82 {
    83 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    84 	HandleError herr;
    85 	HandleSecurity sec;
    86 	IDataPack *pDataPack;
    87 	int err;
    88 
    89 	sec.pOwner = NULL;
    90 	sec.pIdentity = g_pCoreIdent;
    91 
    92 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
    93 		!= HandleError_None)
    94 	{
    95 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
    96 	}
    97 
    98 	char *str;
    99 	if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE)
   100 	{
   101 		pContext->ThrowNativeErrorEx(err, NULL);
   102 		return 0;
   103 	}
   104 
   105 	pDataPack->PackString(str);
   106 
   107 	return 1;
   108 }
   109 
   110 static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params)
   111 {
   112 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   113 	HandleError herr;
   114 	HandleSecurity sec;
   115 	IDataPack *pDataPack;
   116 
   117 	sec.pOwner = NULL;
   118 	sec.pIdentity = g_pCoreIdent;
   119 
   120 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   121 		!= HandleError_None)
   122 	{
   123 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   124 	}
   125 
   126 	if (!pDataPack->IsReadable(sizeof(cell_t)))
   127 	{
   128 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   129 	}
   130 
   131 	return pDataPack->ReadCell();
   132 }
   133 
   134 static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params)
   135 {
   136 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   137 	HandleError herr;
   138 	HandleSecurity sec;
   139 	IDataPack *pDataPack;
   140 
   141 	sec.pOwner = NULL;
   142 	sec.pIdentity = g_pCoreIdent;
   143 
   144 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   145 		!= HandleError_None)
   146 	{
   147 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   148 	}
   149 
   150 	if (!pDataPack->IsReadable(sizeof(float)))
   151 	{
   152 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   153 	}
   154 
   155 	return sp_ftoc(pDataPack->ReadFloat());
   156 }
   157 
   158 static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params)
   159 {
   160 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   161 	HandleError herr;
   162 	HandleSecurity sec;
   163 	IDataPack *pDataPack;
   164 
   165 	sec.pOwner = NULL;
   166 	sec.pIdentity = g_pCoreIdent;
   167 
   168 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   169 		!= HandleError_None)
   170 	{
   171 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   172 	}
   173 
   174 	const char *str;
   175 	if (!(str=pDataPack->ReadString(NULL)))
   176 	{
   177 		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
   178 	}
   179 
   180 	pContext->StringToLocal(params[2], params[3], str);
   181 
   182 	return 1;
   183 }
   184 
   185 static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params)
   186 {
   187 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   188 	HandleError herr;
   189 	HandleSecurity sec;
   190 	IDataPack *pDataPack;
   191 
   192 	sec.pOwner = NULL;
   193 	sec.pIdentity = g_pCoreIdent;
   194 
   195 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   196 		!= HandleError_None)
   197 	{
   198 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   199 	}
   200 
   201 	pDataPack->Reset();
   202 	if (params[2])
   203 	{
   204 		pDataPack->ResetSize();
   205 	}
   206 
   207 	return 1;
   208 }
   209 
   210 static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params)
   211 {
   212 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   213 	HandleError herr;
   214 	HandleSecurity sec;
   215 	IDataPack *pDataPack;
   216 
   217 	sec.pOwner = NULL;
   218 	sec.pIdentity = g_pCoreIdent;
   219 
   220 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   221 		!= HandleError_None)
   222 	{
   223 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   224 	}
   225 
   226 	return static_cast<cell_t>(pDataPack->GetPosition());
   227 }
   228 
   229 static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params)
   230 {
   231 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   232 	HandleError herr;
   233 	HandleSecurity sec;
   234 	IDataPack *pDataPack;
   235 
   236 	sec.pOwner = NULL;
   237 	sec.pIdentity = g_pCoreIdent;
   238 
   239 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   240 		!= HandleError_None)
   241 	{
   242 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   243 	}
   244 
   245 	if (!pDataPack->SetPosition(params[2]))
   246 	{
   247 		return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]);
   248 	}
   249 
   250 	return 1;
   251 }
   252 
   253 static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params)
   254 {
   255 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   256 	HandleError herr;
   257 	HandleSecurity sec;
   258 	IDataPack *pDataPack;
   259 
   260 	sec.pOwner = NULL;
   261 	sec.pIdentity = g_pCoreIdent;
   262 
   263 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
   264 		!= HandleError_None)
   265 	{
   266 		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
   267 	}
   268 
   269 	return pDataPack->IsReadable(params[2]) ? 1 : 0;
   270 }
   271 
   272 static DataPackNatives s_DataPackNatives;
   273 
   274 REGISTER_NATIVES(datapacknatives)
   275 {
   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},
   287 	{NULL,						NULL}
   288 };