3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "sourcemod.h"
33 #include "HandleSys.h"
36 #include <HalfLife2.h>
38 static cell_t smn_BfWriteBool(IPluginContext *pCtx, const cell_t *params)
40 Handle_t hndl = static_cast<Handle_t>(params[1]);
46 sec.pIdentity = g_pCoreIdent;
48 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
51 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
54 pBitBuf->WriteOneBit(params[2]);
59 static cell_t smn_BfWriteByte(IPluginContext *pCtx, const cell_t *params)
61 Handle_t hndl = static_cast<Handle_t>(params[1]);
67 sec.pIdentity = g_pCoreIdent;
69 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
72 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
75 pBitBuf->WriteByte(params[2]);
80 static cell_t smn_BfWriteChar(IPluginContext *pCtx, const cell_t *params)
82 Handle_t hndl = static_cast<Handle_t>(params[1]);
88 sec.pIdentity = g_pCoreIdent;
90 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
93 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
96 pBitBuf->WriteChar(params[2]);
101 static cell_t smn_BfWriteShort(IPluginContext *pCtx, const cell_t *params)
103 Handle_t hndl = static_cast<Handle_t>(params[1]);
109 sec.pIdentity = g_pCoreIdent;
111 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
114 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
117 pBitBuf->WriteShort(params[2]);
122 static cell_t smn_BfWriteWord(IPluginContext *pCtx, const cell_t *params)
124 Handle_t hndl = static_cast<Handle_t>(params[1]);
130 sec.pIdentity = g_pCoreIdent;
132 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
135 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
138 pBitBuf->WriteWord(params[2]);
143 static cell_t smn_BfWriteNum(IPluginContext *pCtx, const cell_t *params)
145 Handle_t hndl = static_cast<Handle_t>(params[1]);
151 sec.pIdentity = g_pCoreIdent;
153 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
156 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
159 pBitBuf->WriteLong(static_cast<long>(params[2]));
164 static cell_t smn_BfWriteFloat(IPluginContext *pCtx, const cell_t *params)
166 Handle_t hndl = static_cast<Handle_t>(params[1]);
172 sec.pIdentity = g_pCoreIdent;
174 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
177 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
180 pBitBuf->WriteFloat(sp_ctof(params[2]));
185 static cell_t smn_BfWriteString(IPluginContext *pCtx, const cell_t *params)
187 Handle_t hndl = static_cast<Handle_t>(params[1]);
194 sec.pIdentity = g_pCoreIdent;
196 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
199 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
203 if ((err=pCtx->LocalToString(params[2], &str)) != SP_ERROR_NONE)
205 pCtx->ThrowNativeErrorEx(err, NULL);
209 pBitBuf->WriteString(str);
214 static cell_t smn_BfWriteEntity(IPluginContext *pCtx, const cell_t *params)
216 Handle_t hndl = static_cast<Handle_t>(params[1]);
222 sec.pIdentity = g_pCoreIdent;
224 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
227 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
230 int index = g_HL2.ReferenceToIndex(params[2]);
237 pBitBuf->WriteShort(index);
242 static cell_t smn_BfWriteAngle(IPluginContext *pCtx, const cell_t *params)
244 Handle_t hndl = static_cast<Handle_t>(params[1]);
250 sec.pIdentity = g_pCoreIdent;
252 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
255 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
258 pBitBuf->WriteBitAngle(sp_ctof(params[2]), params[3]);
263 static cell_t smn_BfWriteCoord(IPluginContext *pCtx, const cell_t *params)
265 Handle_t hndl = static_cast<Handle_t>(params[1]);
271 sec.pIdentity = g_pCoreIdent;
273 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
276 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
279 pBitBuf->WriteBitCoord(sp_ctof(params[2]));
284 static cell_t smn_BfWriteVecCoord(IPluginContext *pCtx, const cell_t *params)
286 Handle_t hndl = static_cast<Handle_t>(params[1]);
292 sec.pIdentity = g_pCoreIdent;
294 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
297 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
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);
308 static cell_t smn_BfWriteVecNormal(IPluginContext *pCtx, const cell_t *params)
310 Handle_t hndl = static_cast<Handle_t>(params[1]);
316 sec.pIdentity = g_pCoreIdent;
318 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
321 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
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);
332 static cell_t smn_BfWriteAngles(IPluginContext *pCtx, const cell_t *params)
334 Handle_t hndl = static_cast<Handle_t>(params[1]);
340 sec.pIdentity = g_pCoreIdent;
342 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
345 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
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);
356 static cell_t smn_BfReadBool(IPluginContext *pCtx, const cell_t *params)
358 Handle_t hndl = static_cast<Handle_t>(params[1]);
364 sec.pIdentity = g_pCoreIdent;
366 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
369 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
372 return pBitBuf->ReadOneBit() ? 1 : 0;
375 static cell_t smn_BfReadByte(IPluginContext *pCtx, const cell_t *params)
377 Handle_t hndl = static_cast<Handle_t>(params[1]);
383 sec.pIdentity = g_pCoreIdent;
385 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
388 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
391 return pBitBuf->ReadByte();
394 static cell_t smn_BfReadChar(IPluginContext *pCtx, const cell_t *params)
396 Handle_t hndl = static_cast<Handle_t>(params[1]);
402 sec.pIdentity = g_pCoreIdent;
404 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
407 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
410 return pBitBuf->ReadChar();
413 static cell_t smn_BfReadShort(IPluginContext *pCtx, const cell_t *params)
415 Handle_t hndl = static_cast<Handle_t>(params[1]);
421 sec.pIdentity = g_pCoreIdent;
423 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
426 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
429 return pBitBuf->ReadShort();
432 static cell_t smn_BfReadWord(IPluginContext *pCtx, const cell_t *params)
434 Handle_t hndl = static_cast<Handle_t>(params[1]);
440 sec.pIdentity = g_pCoreIdent;
442 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
445 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
448 return pBitBuf->ReadWord();
451 static cell_t smn_BfReadNum(IPluginContext *pCtx, const cell_t *params)
453 Handle_t hndl = static_cast<Handle_t>(params[1]);
459 sec.pIdentity = g_pCoreIdent;
461 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
464 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
467 return static_cast<cell_t>(pBitBuf->ReadLong());
470 static cell_t smn_BfReadFloat(IPluginContext *pCtx, const cell_t *params)
472 Handle_t hndl = static_cast<Handle_t>(params[1]);
478 sec.pIdentity = g_pCoreIdent;
480 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
483 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
486 return sp_ftoc(pBitBuf->ReadFloat());
489 static cell_t smn_BfReadString(IPluginContext *pCtx, const cell_t *params)
491 Handle_t hndl = static_cast<Handle_t>(params[1]);
499 sec.pIdentity = g_pCoreIdent;
501 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
504 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
507 pCtx->LocalToPhysAddr(params[2], (cell_t **)&buf);
508 pBitBuf->ReadString(buf, params[3], params[4] ? true : false, &numChars);
510 if (pBitBuf->IsOverflowed())
512 return -numChars - 1;
518 static cell_t smn_BfReadEntity(IPluginContext *pCtx, const cell_t *params)
520 Handle_t hndl = static_cast<Handle_t>(params[1]);
526 sec.pIdentity = g_pCoreIdent;
528 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
531 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
534 int ref = g_HL2.IndexToReference(pBitBuf->ReadShort());
536 return g_HL2.ReferenceToBCompatRef(ref);
539 static cell_t smn_BfReadAngle(IPluginContext *pCtx, const cell_t *params)
541 Handle_t hndl = static_cast<Handle_t>(params[1]);
547 sec.pIdentity = g_pCoreIdent;
549 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
552 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
555 return sp_ftoc(pBitBuf->ReadBitAngle(params[2]));
558 static cell_t smn_BfReadCoord(IPluginContext *pCtx, const cell_t *params)
560 Handle_t hndl = static_cast<Handle_t>(params[1]);
566 sec.pIdentity = g_pCoreIdent;
568 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
571 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
574 return sp_ftoc(pBitBuf->ReadBitCoord());
577 static cell_t smn_BfReadVecCoord(IPluginContext *pCtx, const cell_t *params)
579 Handle_t hndl = static_cast<Handle_t>(params[1]);
585 sec.pIdentity = g_pCoreIdent;
587 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
590 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
594 pCtx->LocalToPhysAddr(params[2], &pVec);
597 pBitBuf->ReadBitVec3Coord(vec);
599 pVec[0] = sp_ftoc(vec.x);
600 pVec[1] = sp_ftoc(vec.y);
601 pVec[2] = sp_ftoc(vec.z);
606 static cell_t smn_BfReadVecNormal(IPluginContext *pCtx, const cell_t *params)
608 Handle_t hndl = static_cast<Handle_t>(params[1]);
614 sec.pIdentity = g_pCoreIdent;
616 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
619 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
623 pCtx->LocalToPhysAddr(params[2], &pVec);
626 pBitBuf->ReadBitVec3Normal(vec);
628 pVec[0] = sp_ftoc(vec.x);
629 pVec[1] = sp_ftoc(vec.y);
630 pVec[2] = sp_ftoc(vec.z);
635 static cell_t smn_BfReadAngles(IPluginContext *pCtx, const cell_t *params)
637 Handle_t hndl = static_cast<Handle_t>(params[1]);
643 sec.pIdentity = g_pCoreIdent;
645 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
648 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
652 pCtx->LocalToPhysAddr(params[2], &pAng);
655 pBitBuf->ReadBitAngles(ang);
657 pAng[0] = sp_ftoc(ang.x);
658 pAng[1] = sp_ftoc(ang.y);
659 pAng[2] = sp_ftoc(ang.z);
664 static cell_t smn_BfGetNumBytesLeft(IPluginContext *pCtx, const cell_t *params)
666 Handle_t hndl = static_cast<Handle_t>(params[1]);
672 sec.pIdentity = g_pCoreIdent;
674 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
677 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
680 return pBitBuf->GetNumBitsLeft() >> 3;
683 REGISTER_NATIVES(bitbufnatives)
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},