core/smn_bitbuffer.cpp
author Scott Ehlert <ds@alliedmods.net>
Sat Feb 17 05:33:31 2007 +0000 (2007-02-17)
changeset 511 c29f8796bcf4
parent 505 9fb8a1ccd4ec
child 598 991d7e247a2c
permissions -rw-r--r--
Initial fixes to ensure static strings passed to new convars
Also fixed minor warning in smn_bitbuffer.cpp
     1 /**
     2 * vim: set ts=4 :
     3 * ===============================================================
     4 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5 * ===============================================================
     6 *
     7 * This file is not open source and may not be copied without explicit
     8 * written permission of AlliedModders LLC.  This file may not be redistributed 
     9 * in whole or significant part.
    10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11 *
    12 * Version: $Id$
    13 */
    14 
    15 #include "sourcemod.h"
    16 #include "HandleSys.h"
    17 #include <bitbuf.h>
    18 #include <vector.h>
    19 
    20 static cell_t smn_BfWriteBool(IPluginContext *pCtx, const cell_t *params)
    21 {
    22 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    23 	HandleError herr;
    24 	HandleSecurity sec;
    25 	bf_write *pBitBuf;
    26 
    27 	sec.pOwner = NULL;
    28 	sec.pIdentity = g_pCoreIdent;
    29 
    30 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    31 		!= HandleError_None)
    32 	{
    33 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    34 	}
    35 
    36 	pBitBuf->WriteOneBit(params[2]);
    37 
    38 	return 1;
    39 }
    40 
    41 static cell_t smn_BfWriteByte(IPluginContext *pCtx, const cell_t *params)
    42 {
    43 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    44 	HandleError herr;
    45 	HandleSecurity sec;
    46 	bf_write *pBitBuf;
    47 
    48 	sec.pOwner = NULL;
    49 	sec.pIdentity = g_pCoreIdent;
    50 
    51 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    52 		!= HandleError_None)
    53 	{
    54 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    55 	}
    56 
    57 	pBitBuf->WriteByte(params[2]);
    58 
    59 	return 1;
    60 }
    61 
    62 static cell_t smn_BfWriteChar(IPluginContext *pCtx, const cell_t *params)
    63 {
    64 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    65 	HandleError herr;
    66 	HandleSecurity sec;
    67 	bf_write *pBitBuf;
    68 
    69 	sec.pOwner = NULL;
    70 	sec.pIdentity = g_pCoreIdent;
    71 
    72 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    73 		!= HandleError_None)
    74 	{
    75 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    76 	}
    77 
    78 	pBitBuf->WriteChar(params[2]);
    79 
    80 	return 1;
    81 }
    82 
    83 static cell_t smn_BfWriteShort(IPluginContext *pCtx, const cell_t *params)
    84 {
    85 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    86 	HandleError herr;
    87 	HandleSecurity sec;
    88 	bf_write *pBitBuf;
    89 
    90 	sec.pOwner = NULL;
    91 	sec.pIdentity = g_pCoreIdent;
    92 
    93 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    94 		!= HandleError_None)
    95 	{
    96 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    97 	}
    98 
    99 	pBitBuf->WriteShort(params[2]);
   100 
   101 	return 1;
   102 }
   103 
   104 static cell_t smn_BfWriteWord(IPluginContext *pCtx, const cell_t *params)
   105 {
   106 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   107 	HandleError herr;
   108 	HandleSecurity sec;
   109 	bf_write *pBitBuf;
   110 
   111 	sec.pOwner = NULL;
   112 	sec.pIdentity = g_pCoreIdent;
   113 
   114 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   115 		!= HandleError_None)
   116 	{
   117 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   118 	}
   119 
   120 	pBitBuf->WriteWord(params[2]);
   121 
   122 	return 1;
   123 }
   124 
   125 static cell_t smn_BfWriteNum(IPluginContext *pCtx, const cell_t *params)
   126 {
   127 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   128 	HandleError herr;
   129 	HandleSecurity sec;
   130 	bf_write *pBitBuf;
   131 
   132 	sec.pOwner = NULL;
   133 	sec.pIdentity = g_pCoreIdent;
   134 
   135 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   136 		!= HandleError_None)
   137 	{
   138 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   139 	}
   140 
   141 	pBitBuf->WriteLong(static_cast<long>(params[2]));
   142 
   143 	return 1;
   144 }
   145 
   146 static cell_t smn_BfWriteFloat(IPluginContext *pCtx, const cell_t *params)
   147 {
   148 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   149 	HandleError herr;
   150 	HandleSecurity sec;
   151 	bf_write *pBitBuf;
   152 
   153 	sec.pOwner = NULL;
   154 	sec.pIdentity = g_pCoreIdent;
   155 
   156 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   157 		!= HandleError_None)
   158 	{
   159 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   160 	}
   161 
   162 	pBitBuf->WriteFloat(sp_ctof(params[2]));
   163 
   164 	return 1;
   165 }
   166 
   167 static cell_t smn_BfWriteString(IPluginContext *pCtx, const cell_t *params)
   168 {
   169 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   170 	HandleError herr;
   171 	HandleSecurity sec;
   172 	bf_write *pBitBuf;
   173 	int err;
   174 
   175 	sec.pOwner = NULL;
   176 	sec.pIdentity = g_pCoreIdent;
   177 
   178 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   179 		!= HandleError_None)
   180 	{
   181 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   182 	}
   183 
   184 	char *str;
   185 	if ((err=pCtx->LocalToString(params[2], &str)) != SP_ERROR_NONE)
   186 	{
   187 		pCtx->ThrowNativeErrorEx(err, NULL);
   188 		return 0;
   189 	}
   190 
   191 	pBitBuf->WriteString(str);
   192 
   193 	return 1;
   194 }
   195 
   196 static cell_t smn_BfWriteEntity(IPluginContext *pCtx, const cell_t *params)
   197 {
   198 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   199 	HandleError herr;
   200 	HandleSecurity sec;
   201 	bf_write *pBitBuf;
   202 
   203 	sec.pOwner = NULL;
   204 	sec.pIdentity = g_pCoreIdent;
   205 
   206 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   207 		!= HandleError_None)
   208 	{
   209 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   210 	}
   211 
   212 	pBitBuf->WriteShort(params[2]);
   213 
   214 	return 1;
   215 }
   216 
   217 static cell_t smn_BfWriteAngle(IPluginContext *pCtx, const cell_t *params)
   218 {
   219 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   220 	HandleError herr;
   221 	HandleSecurity sec;
   222 	bf_write *pBitBuf;
   223 
   224 	sec.pOwner = NULL;
   225 	sec.pIdentity = g_pCoreIdent;
   226 
   227 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   228 		!= HandleError_None)
   229 	{
   230 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   231 	}
   232 
   233 	pBitBuf->WriteBitAngle(sp_ctof(params[2]), params[3]);
   234 
   235 	return 1;
   236 }
   237 
   238 static cell_t smn_BfWriteCoord(IPluginContext *pCtx, const cell_t *params)
   239 {
   240 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   241 	HandleError herr;
   242 	HandleSecurity sec;
   243 	bf_write *pBitBuf;
   244 
   245 	sec.pOwner = NULL;
   246 	sec.pIdentity = g_pCoreIdent;
   247 
   248 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   249 		!= HandleError_None)
   250 	{
   251 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   252 	}
   253 
   254 	pBitBuf->WriteBitCoord(sp_ctof(params[2]));
   255 
   256 	return 1;
   257 }
   258 
   259 static cell_t smn_BfWriteVecCoord(IPluginContext *pCtx, const cell_t *params)
   260 {
   261 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   262 	HandleError herr;
   263 	HandleSecurity sec;
   264 	bf_write *pBitBuf;
   265 
   266 	sec.pOwner = NULL;
   267 	sec.pIdentity = g_pCoreIdent;
   268 
   269 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   270 		!= HandleError_None)
   271 	{
   272 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   273 	}
   274 
   275 	cell_t *pVec;
   276 	pCtx->LocalToPhysAddr(params[2], &pVec);
   277 	Vector vec(sp_ctof(pVec[0]), sp_ctof(pVec[1]), sp_ctof(pVec[2]));
   278 	pBitBuf->WriteBitVec3Coord(vec);
   279 
   280 	return 1;
   281 }
   282 
   283 static cell_t smn_BfWriteVecNormal(IPluginContext *pCtx, const cell_t *params)
   284 {
   285 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   286 	HandleError herr;
   287 	HandleSecurity sec;
   288 	bf_write *pBitBuf;
   289 
   290 	sec.pOwner = NULL;
   291 	sec.pIdentity = g_pCoreIdent;
   292 
   293 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   294 		!= HandleError_None)
   295 	{
   296 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   297 	}
   298 
   299 	cell_t *pVec;
   300 	pCtx->LocalToPhysAddr(params[2], &pVec);
   301 	Vector vec(sp_ctof(pVec[0]), sp_ctof(pVec[1]), sp_ctof(pVec[2]));
   302 	pBitBuf->WriteBitVec3Normal(vec);
   303 
   304 	return 1;
   305 }
   306 
   307 static cell_t smn_BfWriteAngles(IPluginContext *pCtx, const cell_t *params)
   308 {
   309 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   310 	HandleError herr;
   311 	HandleSecurity sec;
   312 	bf_write *pBitBuf;
   313 
   314 	sec.pOwner = NULL;
   315 	sec.pIdentity = g_pCoreIdent;
   316 
   317 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   318 		!= HandleError_None)
   319 	{
   320 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   321 	}
   322 
   323 	cell_t *pAng;
   324 	pCtx->LocalToPhysAddr(params[2], &pAng);
   325 	QAngle ang(sp_ctof(pAng[0]), sp_ctof(pAng[1]), sp_ctof(pAng[2]));
   326 	pBitBuf->WriteBitAngles(ang);
   327 
   328 	return 1;
   329 }
   330 
   331 REGISTER_NATIVES(wrbitbufnatives)
   332 {
   333 	{"BfWriteBool",				smn_BfWriteBool},
   334 	{"BfWriteByte",				smn_BfWriteByte},
   335 	{"BfWriteChar",				smn_BfWriteChar},
   336 	{"BfWriteShort",			smn_BfWriteShort},
   337 	{"BfWriteWord",				smn_BfWriteWord},
   338 	{"BfWriteNum",				smn_BfWriteNum},
   339 	{"BfWriteFloat",			smn_BfWriteFloat},
   340 	{"BfWriteString",			smn_BfWriteString},
   341 	{"BfWriteEntity",			smn_BfWriteEntity},
   342 	{"BfWriteAngle",			smn_BfWriteAngle},
   343 	{"BfWriteCoord",			smn_BfWriteCoord},
   344 	{"BfWriteVecCoord",			smn_BfWriteVecCoord},
   345 	{"BfWriteVecNormal",		smn_BfWriteVecNormal},
   346 	{"BfWriteAngles",			smn_BfWriteAngles},
   347 	{NULL,						NULL}
   348 };