core/smn_bitbuffer.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Fri Nov 04 10:01:33 2011 -0400 (2011-11-04)
changeset 3438 07a1b8a8a2ba
parent 2746 8d68569b9648
child 3743 a98686a77974
permissions -rw-r--r--
Updated TF2_OnIsHolidayActive ret behavior to match doc (bug 5155, r=fyren).
     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 #include <HalfLife2.h>
    37 
    38 static cell_t smn_BfWriteBool(IPluginContext *pCtx, const cell_t *params)
    39 {
    40 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    41 	HandleError herr;
    42 	HandleSecurity sec;
    43 	bf_write *pBitBuf;
    44 
    45 	sec.pOwner = NULL;
    46 	sec.pIdentity = g_pCoreIdent;
    47 
    48 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    49 		!= HandleError_None)
    50 	{
    51 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    52 	}
    53 
    54 	pBitBuf->WriteOneBit(params[2]);
    55 
    56 	return 1;
    57 }
    58 
    59 static cell_t smn_BfWriteByte(IPluginContext *pCtx, const cell_t *params)
    60 {
    61 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    62 	HandleError herr;
    63 	HandleSecurity sec;
    64 	bf_write *pBitBuf;
    65 
    66 	sec.pOwner = NULL;
    67 	sec.pIdentity = g_pCoreIdent;
    68 
    69 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    70 		!= HandleError_None)
    71 	{
    72 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    73 	}
    74 
    75 	pBitBuf->WriteByte(params[2]);
    76 
    77 	return 1;
    78 }
    79 
    80 static cell_t smn_BfWriteChar(IPluginContext *pCtx, const cell_t *params)
    81 {
    82 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    83 	HandleError herr;
    84 	HandleSecurity sec;
    85 	bf_write *pBitBuf;
    86 
    87 	sec.pOwner = NULL;
    88 	sec.pIdentity = g_pCoreIdent;
    89 
    90 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
    91 		!= HandleError_None)
    92 	{
    93 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    94 	}
    95 
    96 	pBitBuf->WriteChar(params[2]);
    97 
    98 	return 1;
    99 }
   100 
   101 static cell_t smn_BfWriteShort(IPluginContext *pCtx, const cell_t *params)
   102 {
   103 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   104 	HandleError herr;
   105 	HandleSecurity sec;
   106 	bf_write *pBitBuf;
   107 
   108 	sec.pOwner = NULL;
   109 	sec.pIdentity = g_pCoreIdent;
   110 
   111 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   112 		!= HandleError_None)
   113 	{
   114 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   115 	}
   116 
   117 	pBitBuf->WriteShort(params[2]);
   118 
   119 	return 1;
   120 }
   121 
   122 static cell_t smn_BfWriteWord(IPluginContext *pCtx, const cell_t *params)
   123 {
   124 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   125 	HandleError herr;
   126 	HandleSecurity sec;
   127 	bf_write *pBitBuf;
   128 
   129 	sec.pOwner = NULL;
   130 	sec.pIdentity = g_pCoreIdent;
   131 
   132 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   133 		!= HandleError_None)
   134 	{
   135 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   136 	}
   137 
   138 	pBitBuf->WriteWord(params[2]);
   139 
   140 	return 1;
   141 }
   142 
   143 static cell_t smn_BfWriteNum(IPluginContext *pCtx, const cell_t *params)
   144 {
   145 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   146 	HandleError herr;
   147 	HandleSecurity sec;
   148 	bf_write *pBitBuf;
   149 
   150 	sec.pOwner = NULL;
   151 	sec.pIdentity = g_pCoreIdent;
   152 
   153 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   154 		!= HandleError_None)
   155 	{
   156 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   157 	}
   158 
   159 	pBitBuf->WriteLong(static_cast<long>(params[2]));
   160 
   161 	return 1;
   162 }
   163 
   164 static cell_t smn_BfWriteFloat(IPluginContext *pCtx, const cell_t *params)
   165 {
   166 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   167 	HandleError herr;
   168 	HandleSecurity sec;
   169 	bf_write *pBitBuf;
   170 
   171 	sec.pOwner = NULL;
   172 	sec.pIdentity = g_pCoreIdent;
   173 
   174 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   175 		!= HandleError_None)
   176 	{
   177 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   178 	}
   179 
   180 	pBitBuf->WriteFloat(sp_ctof(params[2]));
   181 
   182 	return 1;
   183 }
   184 
   185 static cell_t smn_BfWriteString(IPluginContext *pCtx, const cell_t *params)
   186 {
   187 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   188 	HandleError herr;
   189 	HandleSecurity sec;
   190 	bf_write *pBitBuf;
   191 	int err;
   192 
   193 	sec.pOwner = NULL;
   194 	sec.pIdentity = g_pCoreIdent;
   195 
   196 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   197 		!= HandleError_None)
   198 	{
   199 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   200 	}
   201 
   202 	char *str;
   203 	if ((err=pCtx->LocalToString(params[2], &str)) != SP_ERROR_NONE)
   204 	{
   205 		pCtx->ThrowNativeErrorEx(err, NULL);
   206 		return 0;
   207 	}
   208 
   209 	pBitBuf->WriteString(str);
   210 
   211 	return 1;
   212 }
   213 
   214 static cell_t smn_BfWriteEntity(IPluginContext *pCtx, const cell_t *params)
   215 {
   216 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   217 	HandleError herr;
   218 	HandleSecurity sec;
   219 	bf_write *pBitBuf;
   220 
   221 	sec.pOwner = NULL;
   222 	sec.pIdentity = g_pCoreIdent;
   223 
   224 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   225 		!= HandleError_None)
   226 	{
   227 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   228 	}
   229 
   230 	int index = g_HL2.ReferenceToIndex(params[2]);
   231 
   232 	if (index == -1)
   233 	{
   234 		return 0;
   235 	}
   236 
   237 	pBitBuf->WriteShort(index);
   238 
   239 	return 1;
   240 }
   241 
   242 static cell_t smn_BfWriteAngle(IPluginContext *pCtx, const cell_t *params)
   243 {
   244 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   245 	HandleError herr;
   246 	HandleSecurity sec;
   247 	bf_write *pBitBuf;
   248 
   249 	sec.pOwner = NULL;
   250 	sec.pIdentity = g_pCoreIdent;
   251 
   252 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   253 		!= HandleError_None)
   254 	{
   255 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   256 	}
   257 
   258 	pBitBuf->WriteBitAngle(sp_ctof(params[2]), params[3]);
   259 
   260 	return 1;
   261 }
   262 
   263 static cell_t smn_BfWriteCoord(IPluginContext *pCtx, const cell_t *params)
   264 {
   265 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   266 	HandleError herr;
   267 	HandleSecurity sec;
   268 	bf_write *pBitBuf;
   269 
   270 	sec.pOwner = NULL;
   271 	sec.pIdentity = g_pCoreIdent;
   272 
   273 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   274 		!= HandleError_None)
   275 	{
   276 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   277 	}
   278 
   279 	pBitBuf->WriteBitCoord(sp_ctof(params[2]));
   280 
   281 	return 1;
   282 }
   283 
   284 static cell_t smn_BfWriteVecCoord(IPluginContext *pCtx, const cell_t *params)
   285 {
   286 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   287 	HandleError herr;
   288 	HandleSecurity sec;
   289 	bf_write *pBitBuf;
   290 
   291 	sec.pOwner = NULL;
   292 	sec.pIdentity = g_pCoreIdent;
   293 
   294 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   295 		!= HandleError_None)
   296 	{
   297 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   298 	}
   299 
   300 	cell_t *pVec;
   301 	pCtx->LocalToPhysAddr(params[2], &pVec);
   302 	Vector vec(sp_ctof(pVec[0]), sp_ctof(pVec[1]), sp_ctof(pVec[2]));
   303 	pBitBuf->WriteBitVec3Coord(vec);
   304 
   305 	return 1;
   306 }
   307 
   308 static cell_t smn_BfWriteVecNormal(IPluginContext *pCtx, const cell_t *params)
   309 {
   310 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   311 	HandleError herr;
   312 	HandleSecurity sec;
   313 	bf_write *pBitBuf;
   314 
   315 	sec.pOwner = NULL;
   316 	sec.pIdentity = g_pCoreIdent;
   317 
   318 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   319 		!= HandleError_None)
   320 	{
   321 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   322 	}
   323 
   324 	cell_t *pVec;
   325 	pCtx->LocalToPhysAddr(params[2], &pVec);
   326 	Vector vec(sp_ctof(pVec[0]), sp_ctof(pVec[1]), sp_ctof(pVec[2]));
   327 	pBitBuf->WriteBitVec3Normal(vec);
   328 
   329 	return 1;
   330 }
   331 
   332 static cell_t smn_BfWriteAngles(IPluginContext *pCtx, const cell_t *params)
   333 {
   334 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   335 	HandleError herr;
   336 	HandleSecurity sec;
   337 	bf_write *pBitBuf;
   338 
   339 	sec.pOwner = NULL;
   340 	sec.pIdentity = g_pCoreIdent;
   341 
   342 	if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
   343 		!= HandleError_None)
   344 	{
   345 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   346 	}
   347 
   348 	cell_t *pAng;
   349 	pCtx->LocalToPhysAddr(params[2], &pAng);
   350 	QAngle ang(sp_ctof(pAng[0]), sp_ctof(pAng[1]), sp_ctof(pAng[2]));
   351 	pBitBuf->WriteBitAngles(ang);
   352 
   353 	return 1;
   354 }
   355 
   356 static cell_t smn_BfReadBool(IPluginContext *pCtx, const cell_t *params)
   357 {
   358 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   359 	HandleError herr;
   360 	HandleSecurity sec;
   361 	bf_read *pBitBuf;
   362 
   363 	sec.pOwner = NULL;
   364 	sec.pIdentity = g_pCoreIdent;
   365 
   366 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   367 		!= HandleError_None)
   368 	{
   369 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   370 	}
   371 
   372 	return pBitBuf->ReadOneBit() ? 1 : 0;
   373 }
   374 
   375 static cell_t smn_BfReadByte(IPluginContext *pCtx, const cell_t *params)
   376 {
   377 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   378 	HandleError herr;
   379 	HandleSecurity sec;
   380 	bf_read *pBitBuf;
   381 
   382 	sec.pOwner = NULL;
   383 	sec.pIdentity = g_pCoreIdent;
   384 
   385 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   386 		!= HandleError_None)
   387 	{
   388 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   389 	}
   390 
   391 	return pBitBuf->ReadByte();
   392 }
   393 
   394 static cell_t smn_BfReadChar(IPluginContext *pCtx, const cell_t *params)
   395 {
   396 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   397 	HandleError herr;
   398 	HandleSecurity sec;
   399 	bf_read *pBitBuf;
   400 
   401 	sec.pOwner = NULL;
   402 	sec.pIdentity = g_pCoreIdent;
   403 
   404 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   405 		!= HandleError_None)
   406 	{
   407 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   408 	}
   409 
   410 	return pBitBuf->ReadChar();
   411 }
   412 
   413 static cell_t smn_BfReadShort(IPluginContext *pCtx, const cell_t *params)
   414 {
   415 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   416 	HandleError herr;
   417 	HandleSecurity sec;
   418 	bf_read *pBitBuf;
   419 
   420 	sec.pOwner = NULL;
   421 	sec.pIdentity = g_pCoreIdent;
   422 
   423 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   424 		!= HandleError_None)
   425 	{
   426 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   427 	}
   428 
   429 	return pBitBuf->ReadShort();
   430 }
   431 
   432 static cell_t smn_BfReadWord(IPluginContext *pCtx, const cell_t *params)
   433 {
   434 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   435 	HandleError herr;
   436 	HandleSecurity sec;
   437 	bf_read *pBitBuf;
   438 
   439 	sec.pOwner = NULL;
   440 	sec.pIdentity = g_pCoreIdent;
   441 
   442 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   443 		!= HandleError_None)
   444 	{
   445 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   446 	}
   447 
   448 	return pBitBuf->ReadWord();
   449 }
   450 
   451 static cell_t smn_BfReadNum(IPluginContext *pCtx, const cell_t *params)
   452 {
   453 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   454 	HandleError herr;
   455 	HandleSecurity sec;
   456 	bf_read *pBitBuf;
   457 
   458 	sec.pOwner = NULL;
   459 	sec.pIdentity = g_pCoreIdent;
   460 
   461 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   462 		!= HandleError_None)
   463 	{
   464 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   465 	}
   466 
   467 	return static_cast<cell_t>(pBitBuf->ReadLong());
   468 }
   469 
   470 static cell_t smn_BfReadFloat(IPluginContext *pCtx, const cell_t *params)
   471 {
   472 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   473 	HandleError herr;
   474 	HandleSecurity sec;
   475 	bf_read *pBitBuf;
   476 
   477 	sec.pOwner = NULL;
   478 	sec.pIdentity = g_pCoreIdent;
   479 
   480 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   481 		!= HandleError_None)
   482 	{
   483 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   484 	}
   485 
   486 	return sp_ftoc(pBitBuf->ReadFloat());
   487 }
   488 
   489 static cell_t smn_BfReadString(IPluginContext *pCtx, const cell_t *params)
   490 {
   491 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   492 	HandleError herr;
   493 	HandleSecurity sec;
   494 	bf_read *pBitBuf;
   495 	int numChars = 0;
   496 	char *buf;
   497 
   498 	sec.pOwner = NULL;
   499 	sec.pIdentity = g_pCoreIdent;
   500 
   501 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   502 		!= HandleError_None)
   503 	{
   504 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   505 	}
   506 
   507 	pCtx->LocalToPhysAddr(params[2], (cell_t **)&buf);
   508 	pBitBuf->ReadString(buf, params[3], params[4] ? true : false, &numChars);
   509 
   510 	if (pBitBuf->IsOverflowed())
   511 	{
   512 		return -numChars - 1;
   513 	}
   514 
   515 	return numChars;
   516 }
   517 
   518 static cell_t smn_BfReadEntity(IPluginContext *pCtx, const cell_t *params)
   519 {
   520 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   521 	HandleError herr;
   522 	HandleSecurity sec;
   523 	bf_read *pBitBuf;
   524 
   525 	sec.pOwner = NULL;
   526 	sec.pIdentity = g_pCoreIdent;
   527 
   528 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   529 		!= HandleError_None)
   530 	{
   531 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   532 	}
   533 
   534 	int ref = g_HL2.IndexToReference(pBitBuf->ReadShort());
   535 
   536 	return g_HL2.ReferenceToBCompatRef(ref);
   537 }
   538 
   539 static cell_t smn_BfReadAngle(IPluginContext *pCtx, const cell_t *params)
   540 {
   541 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   542 	HandleError herr;
   543 	HandleSecurity sec;
   544 	bf_read *pBitBuf;
   545 
   546 	sec.pOwner = NULL;
   547 	sec.pIdentity = g_pCoreIdent;
   548 
   549 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   550 		!= HandleError_None)
   551 	{
   552 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   553 	}
   554 
   555 	return sp_ftoc(pBitBuf->ReadBitAngle(params[2]));
   556 }
   557 
   558 static cell_t smn_BfReadCoord(IPluginContext *pCtx, const cell_t *params)
   559 {
   560 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   561 	HandleError herr;
   562 	HandleSecurity sec;
   563 	bf_read *pBitBuf;
   564 
   565 	sec.pOwner = NULL;
   566 	sec.pIdentity = g_pCoreIdent;
   567 
   568 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   569 		!= HandleError_None)
   570 	{
   571 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   572 	}
   573 
   574 	return sp_ftoc(pBitBuf->ReadBitCoord());
   575 }
   576 
   577 static cell_t smn_BfReadVecCoord(IPluginContext *pCtx, const cell_t *params)
   578 {
   579 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   580 	HandleError herr;
   581 	HandleSecurity sec;
   582 	bf_read *pBitBuf;
   583 
   584 	sec.pOwner = NULL;
   585 	sec.pIdentity = g_pCoreIdent;
   586 
   587 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   588 		!= HandleError_None)
   589 	{
   590 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   591 	}
   592 
   593 	cell_t *pVec;
   594 	pCtx->LocalToPhysAddr(params[2], &pVec);
   595 
   596 	Vector vec;
   597 	pBitBuf->ReadBitVec3Coord(vec);
   598 
   599 	pVec[0] = sp_ftoc(vec.x);
   600 	pVec[1] = sp_ftoc(vec.y);
   601 	pVec[2] = sp_ftoc(vec.z);
   602 
   603 	return 1;
   604 }
   605 
   606 static cell_t smn_BfReadVecNormal(IPluginContext *pCtx, const cell_t *params)
   607 {
   608 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   609 	HandleError herr;
   610 	HandleSecurity sec;
   611 	bf_read *pBitBuf;
   612 
   613 	sec.pOwner = NULL;
   614 	sec.pIdentity = g_pCoreIdent;
   615 
   616 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   617 		!= HandleError_None)
   618 	{
   619 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   620 	}
   621 
   622 	cell_t *pVec;
   623 	pCtx->LocalToPhysAddr(params[2], &pVec);
   624 
   625 	Vector vec;
   626 	pBitBuf->ReadBitVec3Normal(vec);
   627 
   628 	pVec[0] = sp_ftoc(vec.x);
   629 	pVec[1] = sp_ftoc(vec.y);
   630 	pVec[2] = sp_ftoc(vec.z);
   631 
   632 	return 1;
   633 }
   634 
   635 static cell_t smn_BfReadAngles(IPluginContext *pCtx, const cell_t *params)
   636 {
   637 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   638 	HandleError herr;
   639 	HandleSecurity sec;
   640 	bf_read *pBitBuf;
   641 
   642 	sec.pOwner = NULL;
   643 	sec.pIdentity = g_pCoreIdent;
   644 
   645 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   646 		!= HandleError_None)
   647 	{
   648 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   649 	}
   650 
   651 	cell_t *pAng;
   652 	pCtx->LocalToPhysAddr(params[2], &pAng);
   653 
   654 	QAngle ang;
   655 	pBitBuf->ReadBitAngles(ang);
   656 
   657 	pAng[0] = sp_ftoc(ang.x);
   658 	pAng[1] = sp_ftoc(ang.y);
   659 	pAng[2] = sp_ftoc(ang.z);
   660 
   661 	return 1;
   662 }
   663 
   664 static cell_t smn_BfGetNumBytesLeft(IPluginContext *pCtx, const cell_t *params)
   665 {
   666 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   667 	HandleError herr;
   668 	HandleSecurity sec;
   669 	bf_read *pBitBuf;
   670 
   671 	sec.pOwner = NULL;
   672 	sec.pIdentity = g_pCoreIdent;
   673 
   674 	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   675 		!= HandleError_None)
   676 	{
   677 		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   678 	}
   679 
   680 	return pBitBuf->GetNumBitsLeft() >> 3;
   681 }
   682 
   683 REGISTER_NATIVES(bitbufnatives)
   684 {
   685 	{"BfWriteBool",				smn_BfWriteBool},
   686 	{"BfWriteByte",				smn_BfWriteByte},
   687 	{"BfWriteChar",				smn_BfWriteChar},
   688 	{"BfWriteShort",			smn_BfWriteShort},
   689 	{"BfWriteWord",				smn_BfWriteWord},
   690 	{"BfWriteNum",				smn_BfWriteNum},
   691 	{"BfWriteFloat",			smn_BfWriteFloat},
   692 	{"BfWriteString",			smn_BfWriteString},
   693 	{"BfWriteEntity",			smn_BfWriteEntity},
   694 	{"BfWriteAngle",			smn_BfWriteAngle},
   695 	{"BfWriteCoord",			smn_BfWriteCoord},
   696 	{"BfWriteVecCoord",			smn_BfWriteVecCoord},
   697 	{"BfWriteVecNormal",		smn_BfWriteVecNormal},
   698 	{"BfWriteAngles",			smn_BfWriteAngles},
   699 	{"BfReadBool",				smn_BfReadBool},
   700 	{"BfReadByte",				smn_BfReadByte},
   701 	{"BfReadChar",				smn_BfReadChar},
   702 	{"BfReadShort",				smn_BfReadShort},
   703 	{"BfReadWord",				smn_BfReadWord},
   704 	{"BfReadNum",				smn_BfReadNum},
   705 	{"BfReadFloat",				smn_BfReadFloat},
   706 	{"BfReadString",			smn_BfReadString},
   707 	{"BfReadEntity",			smn_BfReadEntity},
   708 	{"BfReadAngle",				smn_BfReadAngle},
   709 	{"BfReadCoord",				smn_BfReadCoord},
   710 	{"BfReadVecCoord",			smn_BfReadVecCoord},
   711 	{"BfReadVecNormal",			smn_BfReadVecNormal},
   712 	{"BfReadAngles",			smn_BfReadAngles},
   713 	{"BfGetNumBytesLeft",		smn_BfGetNumBytesLeft},
   714 	{NULL,						NULL}
   715 };