core/smn_bitbuffer.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Mar 30 07:00:22 2008 +0000 (2008-03-30)
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Spring Cleaning, Part Ichi (1)
Various minor things done to project files
Updated sample extension project file and updated makefile to the new unified version (more changes likely on the way)
Updated regex project file and makefile
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "sourcemod.h"
    33 #include "HandleSys.h"
    34 #include <bitbuf.h>
    35 #include <vector.h>
    36 
    37 static cell_t smn_BfWriteBool(IPluginContext *pCtx, const cell_t *params)
    38 {
    39 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    40 	HandleError herr;
    41 	HandleSecurity sec;
    42 	bf_write *pBitBuf;
    43 
    44 	sec.pOwner = NULL;
    45 	sec.pIdentity = g_pCoreIdent;
    46 
    47 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    48 		!= HandleError_None)
    49 	{
    50 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    51 	}
    52 
    53 	pBitBuf->WriteOneBit(params[2]);
    54 
    55 	return 1;
    56 }
    57 
    58 static cell_t smn_BfWriteByte(IPluginContext *pCtx, const cell_t *params)
    59 {
    60 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    61 	HandleError herr;
    62 	HandleSecurity sec;
    63 	bf_write *pBitBuf;
    64 
    65 	sec.pOwner = NULL;
    66 	sec.pIdentity = g_pCoreIdent;
    67 
    68 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    69 		!= HandleError_None)
    70 	{
    71 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    72 	}
    73 
    74 	pBitBuf->WriteByte(params[2]);
    75 
    76 	return 1;
    77 }
    78 
    79 static cell_t smn_BfWriteChar(IPluginContext *pCtx, const cell_t *params)
    80 {
    81 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    82 	HandleError herr;
    83 	HandleSecurity sec;
    84 	bf_write *pBitBuf;
    85 
    86 	sec.pOwner = NULL;
    87 	sec.pIdentity = g_pCoreIdent;
    88 
    89 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    90 		!= HandleError_None)
    91 	{
    92 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    93 	}
    94 
    95 	pBitBuf->WriteChar(params[2]);
    96 
    97 	return 1;
    98 }
    99 
   100 static cell_t smn_BfWriteShort(IPluginContext *pCtx, const cell_t *params)
   101 {
   102 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   103 	HandleError herr;
   104 	HandleSecurity sec;
   105 	bf_write *pBitBuf;
   106 
   107 	sec.pOwner = NULL;
   108 	sec.pIdentity = g_pCoreIdent;
   109 
   110 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   111 		!= HandleError_None)
   112 	{
   113 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   114 	}
   115 
   116 	pBitBuf->WriteShort(params[2]);
   117 
   118 	return 1;
   119 }
   120 
   121 static cell_t smn_BfWriteWord(IPluginContext *pCtx, const cell_t *params)
   122 {
   123 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   124 	HandleError herr;
   125 	HandleSecurity sec;
   126 	bf_write *pBitBuf;
   127 
   128 	sec.pOwner = NULL;
   129 	sec.pIdentity = g_pCoreIdent;
   130 
   131 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   132 		!= HandleError_None)
   133 	{
   134 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   135 	}
   136 
   137 	pBitBuf->WriteWord(params[2]);
   138 
   139 	return 1;
   140 }
   141 
   142 static cell_t smn_BfWriteNum(IPluginContext *pCtx, const cell_t *params)
   143 {
   144 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   145 	HandleError herr;
   146 	HandleSecurity sec;
   147 	bf_write *pBitBuf;
   148 
   149 	sec.pOwner = NULL;
   150 	sec.pIdentity = g_pCoreIdent;
   151 
   152 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   153 		!= HandleError_None)
   154 	{
   155 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   156 	}
   157 
   158 	pBitBuf->WriteLong(static_cast<long>(params[2]));
   159 
   160 	return 1;
   161 }
   162 
   163 static cell_t smn_BfWriteFloat(IPluginContext *pCtx, const cell_t *params)
   164 {
   165 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   166 	HandleError herr;
   167 	HandleSecurity sec;
   168 	bf_write *pBitBuf;
   169 
   170 	sec.pOwner = NULL;
   171 	sec.pIdentity = g_pCoreIdent;
   172 
   173 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   174 		!= HandleError_None)
   175 	{
   176 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   177 	}
   178 
   179 	pBitBuf->WriteFloat(sp_ctof(params[2]));
   180 
   181 	return 1;
   182 }
   183 
   184 static cell_t smn_BfWriteString(IPluginContext *pCtx, const cell_t *params)
   185 {
   186 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   187 	HandleError herr;
   188 	HandleSecurity sec;
   189 	bf_write *pBitBuf;
   190 	int err;
   191 
   192 	sec.pOwner = NULL;
   193 	sec.pIdentity = g_pCoreIdent;
   194 
   195 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   196 		!= HandleError_None)
   197 	{
   198 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   199 	}
   200 
   201 	char *str;
   202 	if ((err=pCtx->LocalToString(params[2], &str)) != SP_ERROR_NONE)
   203 	{
   204 		pCtx->ThrowNativeErrorEx(err, NULL);
   205 		return 0;
   206 	}
   207 
   208 	pBitBuf->WriteString(str);
   209 
   210 	return 1;
   211 }
   212 
   213 static cell_t smn_BfWriteEntity(IPluginContext *pCtx, const cell_t *params)
   214 {
   215 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   216 	HandleError herr;
   217 	HandleSecurity sec;
   218 	bf_write *pBitBuf;
   219 
   220 	sec.pOwner = NULL;
   221 	sec.pIdentity = g_pCoreIdent;
   222 
   223 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   224 		!= HandleError_None)
   225 	{
   226 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   227 	}
   228 
   229 	pBitBuf->WriteShort(params[2]);
   230 
   231 	return 1;
   232 }
   233 
   234 static cell_t smn_BfWriteAngle(IPluginContext *pCtx, const cell_t *params)
   235 {
   236 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   237 	HandleError herr;
   238 	HandleSecurity sec;
   239 	bf_write *pBitBuf;
   240 
   241 	sec.pOwner = NULL;
   242 	sec.pIdentity = g_pCoreIdent;
   243 
   244 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   245 		!= HandleError_None)
   246 	{
   247 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   248 	}
   249 
   250 	pBitBuf->WriteBitAngle(sp_ctof(params[2]), params[3]);
   251 
   252 	return 1;
   253 }
   254 
   255 static cell_t smn_BfWriteCoord(IPluginContext *pCtx, const cell_t *params)
   256 {
   257 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   258 	HandleError herr;
   259 	HandleSecurity sec;
   260 	bf_write *pBitBuf;
   261 
   262 	sec.pOwner = NULL;
   263 	sec.pIdentity = g_pCoreIdent;
   264 
   265 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   266 		!= HandleError_None)
   267 	{
   268 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   269 	}
   270 
   271 	pBitBuf->WriteBitCoord(sp_ctof(params[2]));
   272 
   273 	return 1;
   274 }
   275 
   276 static cell_t smn_BfWriteVecCoord(IPluginContext *pCtx, const cell_t *params)
   277 {
   278 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   279 	HandleError herr;
   280 	HandleSecurity sec;
   281 	bf_write *pBitBuf;
   282 
   283 	sec.pOwner = NULL;
   284 	sec.pIdentity = g_pCoreIdent;
   285 
   286 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   287 		!= HandleError_None)
   288 	{
   289 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   290 	}
   291 
   292 	cell_t *pVec;
   293 	pCtx->LocalToPhysAddr(params[2], &pVec);
   294 	Vector vec(sp_ctof(pVec[0]), sp_ctof(pVec[1]), sp_ctof(pVec[2]));
   295 	pBitBuf->WriteBitVec3Coord(vec);
   296 
   297 	return 1;
   298 }
   299 
   300 static cell_t smn_BfWriteVecNormal(IPluginContext *pCtx, const cell_t *params)
   301 {
   302 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   303 	HandleError herr;
   304 	HandleSecurity sec;
   305 	bf_write *pBitBuf;
   306 
   307 	sec.pOwner = NULL;
   308 	sec.pIdentity = g_pCoreIdent;
   309 
   310 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   311 		!= HandleError_None)
   312 	{
   313 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   314 	}
   315 
   316 	cell_t *pVec;
   317 	pCtx->LocalToPhysAddr(params[2], &pVec);
   318 	Vector vec(sp_ctof(pVec[0]), sp_ctof(pVec[1]), sp_ctof(pVec[2]));
   319 	pBitBuf->WriteBitVec3Normal(vec);
   320 
   321 	return 1;
   322 }
   323 
   324 static cell_t smn_BfWriteAngles(IPluginContext *pCtx, const cell_t *params)
   325 {
   326 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   327 	HandleError herr;
   328 	HandleSecurity sec;
   329 	bf_write *pBitBuf;
   330 
   331 	sec.pOwner = NULL;
   332 	sec.pIdentity = g_pCoreIdent;
   333 
   334 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   335 		!= HandleError_None)
   336 	{
   337 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   338 	}
   339 
   340 	cell_t *pAng;
   341 	pCtx->LocalToPhysAddr(params[2], &pAng);
   342 	QAngle ang(sp_ctof(pAng[0]), sp_ctof(pAng[1]), sp_ctof(pAng[2]));
   343 	pBitBuf->WriteBitAngles(ang);
   344 
   345 	return 1;
   346 }
   347 
   348 static cell_t smn_BfReadBool(IPluginContext *pCtx, const cell_t *params)
   349 {
   350 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   351 	HandleError herr;
   352 	HandleSecurity sec;
   353 	bf_read *pBitBuf;
   354 
   355 	sec.pOwner = NULL;
   356 	sec.pIdentity = g_pCoreIdent;
   357 
   358 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   359 		!= HandleError_None)
   360 	{
   361 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   362 	}
   363 
   364 	return pBitBuf->ReadOneBit() ? 1 : 0;
   365 }
   366 
   367 static cell_t smn_BfReadByte(IPluginContext *pCtx, const cell_t *params)
   368 {
   369 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   370 	HandleError herr;
   371 	HandleSecurity sec;
   372 	bf_read *pBitBuf;
   373 
   374 	sec.pOwner = NULL;
   375 	sec.pIdentity = g_pCoreIdent;
   376 
   377 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   378 		!= HandleError_None)
   379 	{
   380 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   381 	}
   382 
   383 	return pBitBuf->ReadByte();
   384 }
   385 
   386 static cell_t smn_BfReadChar(IPluginContext *pCtx, const cell_t *params)
   387 {
   388 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   389 	HandleError herr;
   390 	HandleSecurity sec;
   391 	bf_read *pBitBuf;
   392 
   393 	sec.pOwner = NULL;
   394 	sec.pIdentity = g_pCoreIdent;
   395 
   396 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   397 		!= HandleError_None)
   398 	{
   399 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   400 	}
   401 
   402 	return pBitBuf->ReadChar();
   403 }
   404 
   405 static cell_t smn_BfReadShort(IPluginContext *pCtx, const cell_t *params)
   406 {
   407 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   408 	HandleError herr;
   409 	HandleSecurity sec;
   410 	bf_read *pBitBuf;
   411 
   412 	sec.pOwner = NULL;
   413 	sec.pIdentity = g_pCoreIdent;
   414 
   415 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   416 		!= HandleError_None)
   417 	{
   418 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   419 	}
   420 
   421 	return pBitBuf->ReadShort();
   422 }
   423 
   424 static cell_t smn_BfReadWord(IPluginContext *pCtx, const cell_t *params)
   425 {
   426 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   427 	HandleError herr;
   428 	HandleSecurity sec;
   429 	bf_read *pBitBuf;
   430 
   431 	sec.pOwner = NULL;
   432 	sec.pIdentity = g_pCoreIdent;
   433 
   434 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   435 		!= HandleError_None)
   436 	{
   437 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   438 	}
   439 
   440 	return pBitBuf->ReadWord();
   441 }
   442 
   443 static cell_t smn_BfReadNum(IPluginContext *pCtx, const cell_t *params)
   444 {
   445 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   446 	HandleError herr;
   447 	HandleSecurity sec;
   448 	bf_read *pBitBuf;
   449 
   450 	sec.pOwner = NULL;
   451 	sec.pIdentity = g_pCoreIdent;
   452 
   453 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   454 		!= HandleError_None)
   455 	{
   456 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   457 	}
   458 
   459 	return static_cast<cell_t>(pBitBuf->ReadLong());
   460 }
   461 
   462 static cell_t smn_BfReadFloat(IPluginContext *pCtx, const cell_t *params)
   463 {
   464 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   465 	HandleError herr;
   466 	HandleSecurity sec;
   467 	bf_read *pBitBuf;
   468 
   469 	sec.pOwner = NULL;
   470 	sec.pIdentity = g_pCoreIdent;
   471 
   472 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   473 		!= HandleError_None)
   474 	{
   475 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   476 	}
   477 
   478 	return sp_ftoc(pBitBuf->ReadFloat());
   479 }
   480 
   481 static cell_t smn_BfReadString(IPluginContext *pCtx, const cell_t *params)
   482 {
   483 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   484 	HandleError herr;
   485 	HandleSecurity sec;
   486 	bf_read *pBitBuf;
   487 	int numChars = 0;
   488 	char *buf;
   489 
   490 	sec.pOwner = NULL;
   491 	sec.pIdentity = g_pCoreIdent;
   492 
   493 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   494 		!= HandleError_None)
   495 	{
   496 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   497 	}
   498 
   499 	pCtx->LocalToPhysAddr(params[2], (cell_t **)&buf);
   500 	pBitBuf->ReadString(buf, params[3], params[4] ? true : false, &numChars);
   501 
   502 	if (pBitBuf->IsOverflowed())
   503 	{
   504 		return -numChars - 1;
   505 	}
   506 
   507 	return numChars;
   508 }
   509 
   510 static cell_t smn_BfReadEntity(IPluginContext *pCtx, const cell_t *params)
   511 {
   512 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   513 	HandleError herr;
   514 	HandleSecurity sec;
   515 	bf_read *pBitBuf;
   516 
   517 	sec.pOwner = NULL;
   518 	sec.pIdentity = g_pCoreIdent;
   519 
   520 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   521 		!= HandleError_None)
   522 	{
   523 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   524 	}
   525 
   526 	return pBitBuf->ReadShort();
   527 }
   528 
   529 static cell_t smn_BfReadAngle(IPluginContext *pCtx, const cell_t *params)
   530 {
   531 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   532 	HandleError herr;
   533 	HandleSecurity sec;
   534 	bf_read *pBitBuf;
   535 
   536 	sec.pOwner = NULL;
   537 	sec.pIdentity = g_pCoreIdent;
   538 
   539 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   540 		!= HandleError_None)
   541 	{
   542 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   543 	}
   544 
   545 	return sp_ftoc(pBitBuf->ReadBitAngle(params[2]));
   546 }
   547 
   548 static cell_t smn_BfReadCoord(IPluginContext *pCtx, const cell_t *params)
   549 {
   550 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   551 	HandleError herr;
   552 	HandleSecurity sec;
   553 	bf_read *pBitBuf;
   554 
   555 	sec.pOwner = NULL;
   556 	sec.pIdentity = g_pCoreIdent;
   557 
   558 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   559 		!= HandleError_None)
   560 	{
   561 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   562 	}
   563 
   564 	return sp_ftoc(pBitBuf->ReadBitCoord());
   565 }
   566 
   567 static cell_t smn_BfReadVecCoord(IPluginContext *pCtx, const cell_t *params)
   568 {
   569 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   570 	HandleError herr;
   571 	HandleSecurity sec;
   572 	bf_read *pBitBuf;
   573 
   574 	sec.pOwner = NULL;
   575 	sec.pIdentity = g_pCoreIdent;
   576 
   577 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   578 		!= HandleError_None)
   579 	{
   580 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   581 	}
   582 
   583 	cell_t *pVec;
   584 	pCtx->LocalToPhysAddr(params[2], &pVec);
   585 
   586 	Vector vec;
   587 	pBitBuf->ReadBitVec3Coord(vec);
   588 
   589 	pVec[0] = sp_ftoc(vec.x);
   590 	pVec[1] = sp_ftoc(vec.y);
   591 	pVec[2] = sp_ftoc(vec.z);
   592 
   593 	return 1;
   594 }
   595 
   596 static cell_t smn_BfReadVecNormal(IPluginContext *pCtx, const cell_t *params)
   597 {
   598 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   599 	HandleError herr;
   600 	HandleSecurity sec;
   601 	bf_read *pBitBuf;
   602 
   603 	sec.pOwner = NULL;
   604 	sec.pIdentity = g_pCoreIdent;
   605 
   606 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   607 		!= HandleError_None)
   608 	{
   609 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   610 	}
   611 
   612 	cell_t *pVec;
   613 	pCtx->LocalToPhysAddr(params[2], &pVec);
   614 
   615 	Vector vec;
   616 	pBitBuf->ReadBitVec3Normal(vec);
   617 
   618 	pVec[0] = sp_ftoc(vec.x);
   619 	pVec[1] = sp_ftoc(vec.y);
   620 	pVec[2] = sp_ftoc(vec.z);
   621 
   622 	return 1;
   623 }
   624 
   625 static cell_t smn_BfReadAngles(IPluginContext *pCtx, const cell_t *params)
   626 {
   627 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   628 	HandleError herr;
   629 	HandleSecurity sec;
   630 	bf_read *pBitBuf;
   631 
   632 	sec.pOwner = NULL;
   633 	sec.pIdentity = g_pCoreIdent;
   634 
   635 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   636 		!= HandleError_None)
   637 	{
   638 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   639 	}
   640 
   641 	cell_t *pAng;
   642 	pCtx->LocalToPhysAddr(params[2], &pAng);
   643 
   644 	QAngle ang;
   645 	pBitBuf->ReadBitAngles(ang);
   646 
   647 	pAng[0] = sp_ftoc(ang.x);
   648 	pAng[1] = sp_ftoc(ang.y);
   649 	pAng[2] = sp_ftoc(ang.z);
   650 
   651 	return 1;
   652 }
   653 
   654 static cell_t smn_BfGetNumBytesLeft(IPluginContext *pCtx, const cell_t *params)
   655 {
   656 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   657 	HandleError herr;
   658 	HandleSecurity sec;
   659 	bf_read *pBitBuf;
   660 
   661 	sec.pOwner = NULL;
   662 	sec.pIdentity = g_pCoreIdent;
   663 
   664 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   665 		!= HandleError_None)
   666 	{
   667 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   668 	}
   669 
   670 	return pBitBuf->GetNumBitsLeft() >> 3;
   671 }
   672 
   673 REGISTER_NATIVES(bitbufnatives)
   674 {
   675 	{"BfWriteBool",				smn_BfWriteBool},
   676 	{"BfWriteByte",				smn_BfWriteByte},
   677 	{"BfWriteChar",				smn_BfWriteChar},
   678 	{"BfWriteShort",			smn_BfWriteShort},
   679 	{"BfWriteWord",				smn_BfWriteWord},
   680 	{"BfWriteNum",				smn_BfWriteNum},
   681 	{"BfWriteFloat",			smn_BfWriteFloat},
   682 	{"BfWriteString",			smn_BfWriteString},
   683 	{"BfWriteEntity",			smn_BfWriteEntity},
   684 	{"BfWriteAngle",			smn_BfWriteAngle},
   685 	{"BfWriteCoord",			smn_BfWriteCoord},
   686 	{"BfWriteVecCoord",			smn_BfWriteVecCoord},
   687 	{"BfWriteVecNormal",		smn_BfWriteVecNormal},
   688 	{"BfWriteAngles",			smn_BfWriteAngles},
   689 	{"BfReadBool",				smn_BfReadBool},
   690 	{"BfReadByte",				smn_BfReadByte},
   691 	{"BfReadChar",				smn_BfReadChar},
   692 	{"BfReadShort",				smn_BfReadShort},
   693 	{"BfReadWord",				smn_BfReadWord},
   694 	{"BfReadNum",				smn_BfReadNum},
   695 	{"BfReadFloat",				smn_BfReadFloat},
   696 	{"BfReadString",			smn_BfReadString},
   697 	{"BfReadEntity",			smn_BfReadEntity},
   698 	{"BfReadAngle",				smn_BfReadAngle},
   699 	{"BfReadCoord",				smn_BfReadCoord},
   700 	{"BfReadVecCoord",			smn_BfReadVecCoord},
   701 	{"BfReadVecNormal",			smn_BfReadVecNormal},
   702 	{"BfReadAngles",			smn_BfReadAngles},
   703 	{"BfGetNumBytesLeft",		smn_BfGetNumBytesLeft},
   704 	{NULL,						NULL}
   705 };