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