core/smn_bitbuffer.cpp
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/smn_bitbuffer.cpp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,705 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include "sourcemod.h"
    1.36 +#include "HandleSys.h"
    1.37 +#include <bitbuf.h>
    1.38 +#include <vector.h>
    1.39 +
    1.40 +static cell_t smn_BfWriteBool(IPluginContext *pCtx, const cell_t *params)
    1.41 +{
    1.42 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.43 +	HandleError herr;
    1.44 +	HandleSecurity sec;
    1.45 +	bf_write *pBitBuf;
    1.46 +
    1.47 +	sec.pOwner = NULL;
    1.48 +	sec.pIdentity = g_pCoreIdent;
    1.49 +
    1.50 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    1.51 +		!= HandleError_None)
    1.52 +	{
    1.53 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    1.54 +	}
    1.55 +
    1.56 +	pBitBuf->WriteOneBit(params[2]);
    1.57 +
    1.58 +	return 1;
    1.59 +}
    1.60 +
    1.61 +static cell_t smn_BfWriteByte(IPluginContext *pCtx, const cell_t *params)
    1.62 +{
    1.63 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.64 +	HandleError herr;
    1.65 +	HandleSecurity sec;
    1.66 +	bf_write *pBitBuf;
    1.67 +
    1.68 +	sec.pOwner = NULL;
    1.69 +	sec.pIdentity = g_pCoreIdent;
    1.70 +
    1.71 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    1.72 +		!= HandleError_None)
    1.73 +	{
    1.74 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    1.75 +	}
    1.76 +
    1.77 +	pBitBuf->WriteByte(params[2]);
    1.78 +
    1.79 +	return 1;
    1.80 +}
    1.81 +
    1.82 +static cell_t smn_BfWriteChar(IPluginContext *pCtx, const cell_t *params)
    1.83 +{
    1.84 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.85 +	HandleError herr;
    1.86 +	HandleSecurity sec;
    1.87 +	bf_write *pBitBuf;
    1.88 +
    1.89 +	sec.pOwner = NULL;
    1.90 +	sec.pIdentity = g_pCoreIdent;
    1.91 +
    1.92 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    1.93 +		!= HandleError_None)
    1.94 +	{
    1.95 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    1.96 +	}
    1.97 +
    1.98 +	pBitBuf->WriteChar(params[2]);
    1.99 +
   1.100 +	return 1;
   1.101 +}
   1.102 +
   1.103 +static cell_t smn_BfWriteShort(IPluginContext *pCtx, const cell_t *params)
   1.104 +{
   1.105 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.106 +	HandleError herr;
   1.107 +	HandleSecurity sec;
   1.108 +	bf_write *pBitBuf;
   1.109 +
   1.110 +	sec.pOwner = NULL;
   1.111 +	sec.pIdentity = g_pCoreIdent;
   1.112 +
   1.113 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   1.114 +		!= HandleError_None)
   1.115 +	{
   1.116 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.117 +	}
   1.118 +
   1.119 +	pBitBuf->WriteShort(params[2]);
   1.120 +
   1.121 +	return 1;
   1.122 +}
   1.123 +
   1.124 +static cell_t smn_BfWriteWord(IPluginContext *pCtx, const cell_t *params)
   1.125 +{
   1.126 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.127 +	HandleError herr;
   1.128 +	HandleSecurity sec;
   1.129 +	bf_write *pBitBuf;
   1.130 +
   1.131 +	sec.pOwner = NULL;
   1.132 +	sec.pIdentity = g_pCoreIdent;
   1.133 +
   1.134 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   1.135 +		!= HandleError_None)
   1.136 +	{
   1.137 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.138 +	}
   1.139 +
   1.140 +	pBitBuf->WriteWord(params[2]);
   1.141 +
   1.142 +	return 1;
   1.143 +}
   1.144 +
   1.145 +static cell_t smn_BfWriteNum(IPluginContext *pCtx, const cell_t *params)
   1.146 +{
   1.147 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.148 +	HandleError herr;
   1.149 +	HandleSecurity sec;
   1.150 +	bf_write *pBitBuf;
   1.151 +
   1.152 +	sec.pOwner = NULL;
   1.153 +	sec.pIdentity = g_pCoreIdent;
   1.154 +
   1.155 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   1.156 +		!= HandleError_None)
   1.157 +	{
   1.158 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.159 +	}
   1.160 +
   1.161 +	pBitBuf->WriteLong(static_cast<long>(params[2]));
   1.162 +
   1.163 +	return 1;
   1.164 +}
   1.165 +
   1.166 +static cell_t smn_BfWriteFloat(IPluginContext *pCtx, const cell_t *params)
   1.167 +{
   1.168 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.169 +	HandleError herr;
   1.170 +	HandleSecurity sec;
   1.171 +	bf_write *pBitBuf;
   1.172 +
   1.173 +	sec.pOwner = NULL;
   1.174 +	sec.pIdentity = g_pCoreIdent;
   1.175 +
   1.176 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   1.177 +		!= HandleError_None)
   1.178 +	{
   1.179 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.180 +	}
   1.181 +
   1.182 +	pBitBuf->WriteFloat(sp_ctof(params[2]));
   1.183 +
   1.184 +	return 1;
   1.185 +}
   1.186 +
   1.187 +static cell_t smn_BfWriteString(IPluginContext *pCtx, const cell_t *params)
   1.188 +{
   1.189 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.190 +	HandleError herr;
   1.191 +	HandleSecurity sec;
   1.192 +	bf_write *pBitBuf;
   1.193 +	int err;
   1.194 +
   1.195 +	sec.pOwner = NULL;
   1.196 +	sec.pIdentity = g_pCoreIdent;
   1.197 +
   1.198 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   1.199 +		!= HandleError_None)
   1.200 +	{
   1.201 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.202 +	}
   1.203 +
   1.204 +	char *str;
   1.205 +	if ((err=pCtx->LocalToString(params[2], &str)) != SP_ERROR_NONE)
   1.206 +	{
   1.207 +		pCtx->ThrowNativeErrorEx(err, NULL);
   1.208 +		return 0;
   1.209 +	}
   1.210 +
   1.211 +	pBitBuf->WriteString(str);
   1.212 +
   1.213 +	return 1;
   1.214 +}
   1.215 +
   1.216 +static cell_t smn_BfWriteEntity(IPluginContext *pCtx, const cell_t *params)
   1.217 +{
   1.218 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.219 +	HandleError herr;
   1.220 +	HandleSecurity sec;
   1.221 +	bf_write *pBitBuf;
   1.222 +
   1.223 +	sec.pOwner = NULL;
   1.224 +	sec.pIdentity = g_pCoreIdent;
   1.225 +
   1.226 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   1.227 +		!= HandleError_None)
   1.228 +	{
   1.229 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.230 +	}
   1.231 +
   1.232 +	pBitBuf->WriteShort(params[2]);
   1.233 +
   1.234 +	return 1;
   1.235 +}
   1.236 +
   1.237 +static cell_t smn_BfWriteAngle(IPluginContext *pCtx, const cell_t *params)
   1.238 +{
   1.239 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.240 +	HandleError herr;
   1.241 +	HandleSecurity sec;
   1.242 +	bf_write *pBitBuf;
   1.243 +
   1.244 +	sec.pOwner = NULL;
   1.245 +	sec.pIdentity = g_pCoreIdent;
   1.246 +
   1.247 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   1.248 +		!= HandleError_None)
   1.249 +	{
   1.250 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.251 +	}
   1.252 +
   1.253 +	pBitBuf->WriteBitAngle(sp_ctof(params[2]), params[3]);
   1.254 +
   1.255 +	return 1;
   1.256 +}
   1.257 +
   1.258 +static cell_t smn_BfWriteCoord(IPluginContext *pCtx, const cell_t *params)
   1.259 +{
   1.260 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.261 +	HandleError herr;
   1.262 +	HandleSecurity sec;
   1.263 +	bf_write *pBitBuf;
   1.264 +
   1.265 +	sec.pOwner = NULL;
   1.266 +	sec.pIdentity = g_pCoreIdent;
   1.267 +
   1.268 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   1.269 +		!= HandleError_None)
   1.270 +	{
   1.271 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.272 +	}
   1.273 +
   1.274 +	pBitBuf->WriteBitCoord(sp_ctof(params[2]));
   1.275 +
   1.276 +	return 1;
   1.277 +}
   1.278 +
   1.279 +static cell_t smn_BfWriteVecCoord(IPluginContext *pCtx, const cell_t *params)
   1.280 +{
   1.281 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.282 +	HandleError herr;
   1.283 +	HandleSecurity sec;
   1.284 +	bf_write *pBitBuf;
   1.285 +
   1.286 +	sec.pOwner = NULL;
   1.287 +	sec.pIdentity = g_pCoreIdent;
   1.288 +
   1.289 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   1.290 +		!= HandleError_None)
   1.291 +	{
   1.292 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.293 +	}
   1.294 +
   1.295 +	cell_t *pVec;
   1.296 +	pCtx->LocalToPhysAddr(params[2], &pVec);
   1.297 +	Vector vec(sp_ctof(pVec[0]), sp_ctof(pVec[1]), sp_ctof(pVec[2]));
   1.298 +	pBitBuf->WriteBitVec3Coord(vec);
   1.299 +
   1.300 +	return 1;
   1.301 +}
   1.302 +
   1.303 +static cell_t smn_BfWriteVecNormal(IPluginContext *pCtx, const cell_t *params)
   1.304 +{
   1.305 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.306 +	HandleError herr;
   1.307 +	HandleSecurity sec;
   1.308 +	bf_write *pBitBuf;
   1.309 +
   1.310 +	sec.pOwner = NULL;
   1.311 +	sec.pIdentity = g_pCoreIdent;
   1.312 +
   1.313 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   1.314 +		!= HandleError_None)
   1.315 +	{
   1.316 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.317 +	}
   1.318 +
   1.319 +	cell_t *pVec;
   1.320 +	pCtx->LocalToPhysAddr(params[2], &pVec);
   1.321 +	Vector vec(sp_ctof(pVec[0]), sp_ctof(pVec[1]), sp_ctof(pVec[2]));
   1.322 +	pBitBuf->WriteBitVec3Normal(vec);
   1.323 +
   1.324 +	return 1;
   1.325 +}
   1.326 +
   1.327 +static cell_t smn_BfWriteAngles(IPluginContext *pCtx, const cell_t *params)
   1.328 +{
   1.329 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.330 +	HandleError herr;
   1.331 +	HandleSecurity sec;
   1.332 +	bf_write *pBitBuf;
   1.333 +
   1.334 +	sec.pOwner = NULL;
   1.335 +	sec.pIdentity = g_pCoreIdent;
   1.336 +
   1.337 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   1.338 +		!= HandleError_None)
   1.339 +	{
   1.340 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.341 +	}
   1.342 +
   1.343 +	cell_t *pAng;
   1.344 +	pCtx->LocalToPhysAddr(params[2], &pAng);
   1.345 +	QAngle ang(sp_ctof(pAng[0]), sp_ctof(pAng[1]), sp_ctof(pAng[2]));
   1.346 +	pBitBuf->WriteBitAngles(ang);
   1.347 +
   1.348 +	return 1;
   1.349 +}
   1.350 +
   1.351 +static cell_t smn_BfReadBool(IPluginContext *pCtx, const cell_t *params)
   1.352 +{
   1.353 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.354 +	HandleError herr;
   1.355 +	HandleSecurity sec;
   1.356 +	bf_read *pBitBuf;
   1.357 +
   1.358 +	sec.pOwner = NULL;
   1.359 +	sec.pIdentity = g_pCoreIdent;
   1.360 +
   1.361 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.362 +		!= HandleError_None)
   1.363 +	{
   1.364 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.365 +	}
   1.366 +
   1.367 +	return pBitBuf->ReadOneBit() ? 1 : 0;
   1.368 +}
   1.369 +
   1.370 +static cell_t smn_BfReadByte(IPluginContext *pCtx, const cell_t *params)
   1.371 +{
   1.372 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.373 +	HandleError herr;
   1.374 +	HandleSecurity sec;
   1.375 +	bf_read *pBitBuf;
   1.376 +
   1.377 +	sec.pOwner = NULL;
   1.378 +	sec.pIdentity = g_pCoreIdent;
   1.379 +
   1.380 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.381 +		!= HandleError_None)
   1.382 +	{
   1.383 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.384 +	}
   1.385 +
   1.386 +	return pBitBuf->ReadByte();
   1.387 +}
   1.388 +
   1.389 +static cell_t smn_BfReadChar(IPluginContext *pCtx, const cell_t *params)
   1.390 +{
   1.391 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.392 +	HandleError herr;
   1.393 +	HandleSecurity sec;
   1.394 +	bf_read *pBitBuf;
   1.395 +
   1.396 +	sec.pOwner = NULL;
   1.397 +	sec.pIdentity = g_pCoreIdent;
   1.398 +
   1.399 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.400 +		!= HandleError_None)
   1.401 +	{
   1.402 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.403 +	}
   1.404 +
   1.405 +	return pBitBuf->ReadChar();
   1.406 +}
   1.407 +
   1.408 +static cell_t smn_BfReadShort(IPluginContext *pCtx, const cell_t *params)
   1.409 +{
   1.410 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.411 +	HandleError herr;
   1.412 +	HandleSecurity sec;
   1.413 +	bf_read *pBitBuf;
   1.414 +
   1.415 +	sec.pOwner = NULL;
   1.416 +	sec.pIdentity = g_pCoreIdent;
   1.417 +
   1.418 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.419 +		!= HandleError_None)
   1.420 +	{
   1.421 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.422 +	}
   1.423 +
   1.424 +	return pBitBuf->ReadShort();
   1.425 +}
   1.426 +
   1.427 +static cell_t smn_BfReadWord(IPluginContext *pCtx, const cell_t *params)
   1.428 +{
   1.429 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.430 +	HandleError herr;
   1.431 +	HandleSecurity sec;
   1.432 +	bf_read *pBitBuf;
   1.433 +
   1.434 +	sec.pOwner = NULL;
   1.435 +	sec.pIdentity = g_pCoreIdent;
   1.436 +
   1.437 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.438 +		!= HandleError_None)
   1.439 +	{
   1.440 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.441 +	}
   1.442 +
   1.443 +	return pBitBuf->ReadWord();
   1.444 +}
   1.445 +
   1.446 +static cell_t smn_BfReadNum(IPluginContext *pCtx, const cell_t *params)
   1.447 +{
   1.448 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.449 +	HandleError herr;
   1.450 +	HandleSecurity sec;
   1.451 +	bf_read *pBitBuf;
   1.452 +
   1.453 +	sec.pOwner = NULL;
   1.454 +	sec.pIdentity = g_pCoreIdent;
   1.455 +
   1.456 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.457 +		!= HandleError_None)
   1.458 +	{
   1.459 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.460 +	}
   1.461 +
   1.462 +	return static_cast<cell_t>(pBitBuf->ReadLong());
   1.463 +}
   1.464 +
   1.465 +static cell_t smn_BfReadFloat(IPluginContext *pCtx, const cell_t *params)
   1.466 +{
   1.467 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.468 +	HandleError herr;
   1.469 +	HandleSecurity sec;
   1.470 +	bf_read *pBitBuf;
   1.471 +
   1.472 +	sec.pOwner = NULL;
   1.473 +	sec.pIdentity = g_pCoreIdent;
   1.474 +
   1.475 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.476 +		!= HandleError_None)
   1.477 +	{
   1.478 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.479 +	}
   1.480 +
   1.481 +	return sp_ftoc(pBitBuf->ReadFloat());
   1.482 +}
   1.483 +
   1.484 +static cell_t smn_BfReadString(IPluginContext *pCtx, const cell_t *params)
   1.485 +{
   1.486 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.487 +	HandleError herr;
   1.488 +	HandleSecurity sec;
   1.489 +	bf_read *pBitBuf;
   1.490 +	int numChars = 0;
   1.491 +	char *buf;
   1.492 +
   1.493 +	sec.pOwner = NULL;
   1.494 +	sec.pIdentity = g_pCoreIdent;
   1.495 +
   1.496 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.497 +		!= HandleError_None)
   1.498 +	{
   1.499 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.500 +	}
   1.501 +
   1.502 +	pCtx->LocalToPhysAddr(params[2], (cell_t **)&buf);
   1.503 +	pBitBuf->ReadString(buf, params[3], params[4] ? true : false, &numChars);
   1.504 +
   1.505 +	if (pBitBuf->IsOverflowed())
   1.506 +	{
   1.507 +		return -numChars - 1;
   1.508 +	}
   1.509 +
   1.510 +	return numChars;
   1.511 +}
   1.512 +
   1.513 +static cell_t smn_BfReadEntity(IPluginContext *pCtx, const cell_t *params)
   1.514 +{
   1.515 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.516 +	HandleError herr;
   1.517 +	HandleSecurity sec;
   1.518 +	bf_read *pBitBuf;
   1.519 +
   1.520 +	sec.pOwner = NULL;
   1.521 +	sec.pIdentity = g_pCoreIdent;
   1.522 +
   1.523 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.524 +		!= HandleError_None)
   1.525 +	{
   1.526 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.527 +	}
   1.528 +
   1.529 +	return pBitBuf->ReadShort();
   1.530 +}
   1.531 +
   1.532 +static cell_t smn_BfReadAngle(IPluginContext *pCtx, const cell_t *params)
   1.533 +{
   1.534 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.535 +	HandleError herr;
   1.536 +	HandleSecurity sec;
   1.537 +	bf_read *pBitBuf;
   1.538 +
   1.539 +	sec.pOwner = NULL;
   1.540 +	sec.pIdentity = g_pCoreIdent;
   1.541 +
   1.542 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.543 +		!= HandleError_None)
   1.544 +	{
   1.545 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.546 +	}
   1.547 +
   1.548 +	return sp_ftoc(pBitBuf->ReadBitAngle(params[2]));
   1.549 +}
   1.550 +
   1.551 +static cell_t smn_BfReadCoord(IPluginContext *pCtx, const cell_t *params)
   1.552 +{
   1.553 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.554 +	HandleError herr;
   1.555 +	HandleSecurity sec;
   1.556 +	bf_read *pBitBuf;
   1.557 +
   1.558 +	sec.pOwner = NULL;
   1.559 +	sec.pIdentity = g_pCoreIdent;
   1.560 +
   1.561 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.562 +		!= HandleError_None)
   1.563 +	{
   1.564 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.565 +	}
   1.566 +
   1.567 +	return sp_ftoc(pBitBuf->ReadBitCoord());
   1.568 +}
   1.569 +
   1.570 +static cell_t smn_BfReadVecCoord(IPluginContext *pCtx, const cell_t *params)
   1.571 +{
   1.572 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.573 +	HandleError herr;
   1.574 +	HandleSecurity sec;
   1.575 +	bf_read *pBitBuf;
   1.576 +
   1.577 +	sec.pOwner = NULL;
   1.578 +	sec.pIdentity = g_pCoreIdent;
   1.579 +
   1.580 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.581 +		!= HandleError_None)
   1.582 +	{
   1.583 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.584 +	}
   1.585 +
   1.586 +	cell_t *pVec;
   1.587 +	pCtx->LocalToPhysAddr(params[2], &pVec);
   1.588 +
   1.589 +	Vector vec;
   1.590 +	pBitBuf->ReadBitVec3Coord(vec);
   1.591 +
   1.592 +	pVec[0] = sp_ftoc(vec.x);
   1.593 +	pVec[1] = sp_ftoc(vec.y);
   1.594 +	pVec[2] = sp_ftoc(vec.z);
   1.595 +
   1.596 +	return 1;
   1.597 +}
   1.598 +
   1.599 +static cell_t smn_BfReadVecNormal(IPluginContext *pCtx, const cell_t *params)
   1.600 +{
   1.601 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.602 +	HandleError herr;
   1.603 +	HandleSecurity sec;
   1.604 +	bf_read *pBitBuf;
   1.605 +
   1.606 +	sec.pOwner = NULL;
   1.607 +	sec.pIdentity = g_pCoreIdent;
   1.608 +
   1.609 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.610 +		!= HandleError_None)
   1.611 +	{
   1.612 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.613 +	}
   1.614 +
   1.615 +	cell_t *pVec;
   1.616 +	pCtx->LocalToPhysAddr(params[2], &pVec);
   1.617 +
   1.618 +	Vector vec;
   1.619 +	pBitBuf->ReadBitVec3Normal(vec);
   1.620 +
   1.621 +	pVec[0] = sp_ftoc(vec.x);
   1.622 +	pVec[1] = sp_ftoc(vec.y);
   1.623 +	pVec[2] = sp_ftoc(vec.z);
   1.624 +
   1.625 +	return 1;
   1.626 +}
   1.627 +
   1.628 +static cell_t smn_BfReadAngles(IPluginContext *pCtx, const cell_t *params)
   1.629 +{
   1.630 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.631 +	HandleError herr;
   1.632 +	HandleSecurity sec;
   1.633 +	bf_read *pBitBuf;
   1.634 +
   1.635 +	sec.pOwner = NULL;
   1.636 +	sec.pIdentity = g_pCoreIdent;
   1.637 +
   1.638 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.639 +		!= HandleError_None)
   1.640 +	{
   1.641 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.642 +	}
   1.643 +
   1.644 +	cell_t *pAng;
   1.645 +	pCtx->LocalToPhysAddr(params[2], &pAng);
   1.646 +
   1.647 +	QAngle ang;
   1.648 +	pBitBuf->ReadBitAngles(ang);
   1.649 +
   1.650 +	pAng[0] = sp_ftoc(ang.x);
   1.651 +	pAng[1] = sp_ftoc(ang.y);
   1.652 +	pAng[2] = sp_ftoc(ang.z);
   1.653 +
   1.654 +	return 1;
   1.655 +}
   1.656 +
   1.657 +static cell_t smn_BfGetNumBytesLeft(IPluginContext *pCtx, const cell_t *params)
   1.658 +{
   1.659 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.660 +	HandleError herr;
   1.661 +	HandleSecurity sec;
   1.662 +	bf_read *pBitBuf;
   1.663 +
   1.664 +	sec.pOwner = NULL;
   1.665 +	sec.pIdentity = g_pCoreIdent;
   1.666 +
   1.667 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.668 +		!= HandleError_None)
   1.669 +	{
   1.670 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.671 +	}
   1.672 +
   1.673 +	return pBitBuf->GetNumBitsLeft() >> 3;
   1.674 +}
   1.675 +
   1.676 +REGISTER_NATIVES(bitbufnatives)
   1.677 +{
   1.678 +	{"BfWriteBool",				smn_BfWriteBool},
   1.679 +	{"BfWriteByte",				smn_BfWriteByte},
   1.680 +	{"BfWriteChar",				smn_BfWriteChar},
   1.681 +	{"BfWriteShort",			smn_BfWriteShort},
   1.682 +	{"BfWriteWord",				smn_BfWriteWord},
   1.683 +	{"BfWriteNum",				smn_BfWriteNum},
   1.684 +	{"BfWriteFloat",			smn_BfWriteFloat},
   1.685 +	{"BfWriteString",			smn_BfWriteString},
   1.686 +	{"BfWriteEntity",			smn_BfWriteEntity},
   1.687 +	{"BfWriteAngle",			smn_BfWriteAngle},
   1.688 +	{"BfWriteCoord",			smn_BfWriteCoord},
   1.689 +	{"BfWriteVecCoord",			smn_BfWriteVecCoord},
   1.690 +	{"BfWriteVecNormal",		smn_BfWriteVecNormal},
   1.691 +	{"BfWriteAngles",			smn_BfWriteAngles},
   1.692 +	{"BfReadBool",				smn_BfReadBool},
   1.693 +	{"BfReadByte",				smn_BfReadByte},
   1.694 +	{"BfReadChar",				smn_BfReadChar},
   1.695 +	{"BfReadShort",				smn_BfReadShort},
   1.696 +	{"BfReadWord",				smn_BfReadWord},
   1.697 +	{"BfReadNum",				smn_BfReadNum},
   1.698 +	{"BfReadFloat",				smn_BfReadFloat},
   1.699 +	{"BfReadString",			smn_BfReadString},
   1.700 +	{"BfReadEntity",			smn_BfReadEntity},
   1.701 +	{"BfReadAngle",				smn_BfReadAngle},
   1.702 +	{"BfReadCoord",				smn_BfReadCoord},
   1.703 +	{"BfReadVecCoord",			smn_BfReadVecCoord},
   1.704 +	{"BfReadVecNormal",			smn_BfReadVecNormal},
   1.705 +	{"BfReadAngles",			smn_BfReadAngles},
   1.706 +	{"BfGetNumBytesLeft",		smn_BfGetNumBytesLeft},
   1.707 +	{NULL,						NULL}
   1.708 +};