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 "UserMessages.h"
34 #ifndef USE_PROTOBUF_USERMESSAGES
36 #include "sourcemod.h"
37 #include "HandleSys.h"
40 #include <HalfLife2.h>
41 #include "smn_usermsgs.h"
43 static cell_t smn_BfWriteBool(IPluginContext *pCtx, const cell_t *params)
45 Handle_t hndl = static_cast<Handle_t>(params[1]);
51 sec.pIdentity = g_pCoreIdent;
53 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
56 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
59 pBitBuf->WriteOneBit(params[2]);
64 static cell_t smn_BfWriteByte(IPluginContext *pCtx, const cell_t *params)
66 Handle_t hndl = static_cast<Handle_t>(params[1]);
72 sec.pIdentity = g_pCoreIdent;
74 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
77 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
80 pBitBuf->WriteByte(params[2]);
85 static cell_t smn_BfWriteChar(IPluginContext *pCtx, const cell_t *params)
87 Handle_t hndl = static_cast<Handle_t>(params[1]);
93 sec.pIdentity = g_pCoreIdent;
95 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
98 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
101 pBitBuf->WriteChar(params[2]);
106 static cell_t smn_BfWriteShort(IPluginContext *pCtx, const cell_t *params)
108 Handle_t hndl = static_cast<Handle_t>(params[1]);
114 sec.pIdentity = g_pCoreIdent;
116 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
119 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
122 pBitBuf->WriteShort(params[2]);
127 static cell_t smn_BfWriteWord(IPluginContext *pCtx, const cell_t *params)
129 Handle_t hndl = static_cast<Handle_t>(params[1]);
135 sec.pIdentity = g_pCoreIdent;
137 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
140 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
143 pBitBuf->WriteWord(params[2]);
148 static cell_t smn_BfWriteNum(IPluginContext *pCtx, const cell_t *params)
150 Handle_t hndl = static_cast<Handle_t>(params[1]);
156 sec.pIdentity = g_pCoreIdent;
158 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
161 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
164 pBitBuf->WriteLong(static_cast<long>(params[2]));
169 static cell_t smn_BfWriteFloat(IPluginContext *pCtx, const cell_t *params)
171 Handle_t hndl = static_cast<Handle_t>(params[1]);
177 sec.pIdentity = g_pCoreIdent;
179 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
182 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
185 pBitBuf->WriteFloat(sp_ctof(params[2]));
190 static cell_t smn_BfWriteString(IPluginContext *pCtx, const cell_t *params)
192 Handle_t hndl = static_cast<Handle_t>(params[1]);
199 sec.pIdentity = g_pCoreIdent;
201 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
204 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
208 if ((err=pCtx->LocalToString(params[2], &str)) != SP_ERROR_NONE)
210 pCtx->ThrowNativeErrorEx(err, NULL);
214 pBitBuf->WriteString(str);
219 static cell_t smn_BfWriteEntity(IPluginContext *pCtx, const cell_t *params)
221 Handle_t hndl = static_cast<Handle_t>(params[1]);
227 sec.pIdentity = g_pCoreIdent;
229 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
232 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
235 int index = g_HL2.ReferenceToIndex(params[2]);
242 pBitBuf->WriteShort(index);
247 static cell_t smn_BfWriteAngle(IPluginContext *pCtx, const cell_t *params)
249 Handle_t hndl = static_cast<Handle_t>(params[1]);
255 sec.pIdentity = g_pCoreIdent;
257 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
260 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
263 pBitBuf->WriteBitAngle(sp_ctof(params[2]), params[3]);
268 static cell_t smn_BfWriteCoord(IPluginContext *pCtx, const cell_t *params)
270 Handle_t hndl = static_cast<Handle_t>(params[1]);
276 sec.pIdentity = g_pCoreIdent;
278 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
281 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
284 pBitBuf->WriteBitCoord(sp_ctof(params[2]));
289 static cell_t smn_BfWriteVecCoord(IPluginContext *pCtx, const cell_t *params)
291 Handle_t hndl = static_cast<Handle_t>(params[1]);
297 sec.pIdentity = g_pCoreIdent;
299 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
302 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
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);
313 static cell_t smn_BfWriteVecNormal(IPluginContext *pCtx, const cell_t *params)
315 Handle_t hndl = static_cast<Handle_t>(params[1]);
321 sec.pIdentity = g_pCoreIdent;
323 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
326 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
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);
337 static cell_t smn_BfWriteAngles(IPluginContext *pCtx, const cell_t *params)
339 Handle_t hndl = static_cast<Handle_t>(params[1]);
345 sec.pIdentity = g_pCoreIdent;
347 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
350 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
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);
361 static cell_t smn_BfReadBool(IPluginContext *pCtx, const cell_t *params)
363 Handle_t hndl = static_cast<Handle_t>(params[1]);
369 sec.pIdentity = g_pCoreIdent;
371 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
374 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
377 return pBitBuf->ReadOneBit() ? 1 : 0;
380 static cell_t smn_BfReadByte(IPluginContext *pCtx, const cell_t *params)
382 Handle_t hndl = static_cast<Handle_t>(params[1]);
388 sec.pIdentity = g_pCoreIdent;
390 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
393 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
396 return pBitBuf->ReadByte();
399 static cell_t smn_BfReadChar(IPluginContext *pCtx, const cell_t *params)
401 Handle_t hndl = static_cast<Handle_t>(params[1]);
407 sec.pIdentity = g_pCoreIdent;
409 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
412 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
415 return pBitBuf->ReadChar();
418 static cell_t smn_BfReadShort(IPluginContext *pCtx, const cell_t *params)
420 Handle_t hndl = static_cast<Handle_t>(params[1]);
426 sec.pIdentity = g_pCoreIdent;
428 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
431 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
434 return pBitBuf->ReadShort();
437 static cell_t smn_BfReadWord(IPluginContext *pCtx, const cell_t *params)
439 Handle_t hndl = static_cast<Handle_t>(params[1]);
445 sec.pIdentity = g_pCoreIdent;
447 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
450 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
453 return pBitBuf->ReadWord();
456 static cell_t smn_BfReadNum(IPluginContext *pCtx, const cell_t *params)
458 Handle_t hndl = static_cast<Handle_t>(params[1]);
464 sec.pIdentity = g_pCoreIdent;
466 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
469 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
472 return static_cast<cell_t>(pBitBuf->ReadLong());
475 static cell_t smn_BfReadFloat(IPluginContext *pCtx, const cell_t *params)
477 Handle_t hndl = static_cast<Handle_t>(params[1]);
483 sec.pIdentity = g_pCoreIdent;
485 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
488 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
491 return sp_ftoc(pBitBuf->ReadFloat());
494 static cell_t smn_BfReadString(IPluginContext *pCtx, const cell_t *params)
496 Handle_t hndl = static_cast<Handle_t>(params[1]);
504 sec.pIdentity = g_pCoreIdent;
506 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
509 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
512 pCtx->LocalToPhysAddr(params[2], (cell_t **)&buf);
513 pBitBuf->ReadString(buf, params[3], params[4] ? true : false, &numChars);
515 if (pBitBuf->IsOverflowed())
517 return -numChars - 1;
523 static cell_t smn_BfReadEntity(IPluginContext *pCtx, const cell_t *params)
525 Handle_t hndl = static_cast<Handle_t>(params[1]);
531 sec.pIdentity = g_pCoreIdent;
533 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
536 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
539 int ref = g_HL2.IndexToReference(pBitBuf->ReadShort());
541 return g_HL2.ReferenceToBCompatRef(ref);
544 static cell_t smn_BfReadAngle(IPluginContext *pCtx, const cell_t *params)
546 Handle_t hndl = static_cast<Handle_t>(params[1]);
552 sec.pIdentity = g_pCoreIdent;
554 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
557 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
560 return sp_ftoc(pBitBuf->ReadBitAngle(params[2]));
563 static cell_t smn_BfReadCoord(IPluginContext *pCtx, const cell_t *params)
565 Handle_t hndl = static_cast<Handle_t>(params[1]);
571 sec.pIdentity = g_pCoreIdent;
573 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
576 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
579 return sp_ftoc(pBitBuf->ReadBitCoord());
582 static cell_t smn_BfReadVecCoord(IPluginContext *pCtx, const cell_t *params)
584 Handle_t hndl = static_cast<Handle_t>(params[1]);
590 sec.pIdentity = g_pCoreIdent;
592 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
595 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
599 pCtx->LocalToPhysAddr(params[2], &pVec);
602 pBitBuf->ReadBitVec3Coord(vec);
604 pVec[0] = sp_ftoc(vec.x);
605 pVec[1] = sp_ftoc(vec.y);
606 pVec[2] = sp_ftoc(vec.z);
611 static cell_t smn_BfReadVecNormal(IPluginContext *pCtx, const cell_t *params)
613 Handle_t hndl = static_cast<Handle_t>(params[1]);
619 sec.pIdentity = g_pCoreIdent;
621 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
624 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
628 pCtx->LocalToPhysAddr(params[2], &pVec);
631 pBitBuf->ReadBitVec3Normal(vec);
633 pVec[0] = sp_ftoc(vec.x);
634 pVec[1] = sp_ftoc(vec.y);
635 pVec[2] = sp_ftoc(vec.z);
640 static cell_t smn_BfReadAngles(IPluginContext *pCtx, const cell_t *params)
642 Handle_t hndl = static_cast<Handle_t>(params[1]);
648 sec.pIdentity = g_pCoreIdent;
650 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
653 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
657 pCtx->LocalToPhysAddr(params[2], &pAng);
660 pBitBuf->ReadBitAngles(ang);
662 pAng[0] = sp_ftoc(ang.x);
663 pAng[1] = sp_ftoc(ang.y);
664 pAng[2] = sp_ftoc(ang.z);
669 static cell_t smn_BfGetNumBytesLeft(IPluginContext *pCtx, const cell_t *params)
671 Handle_t hndl = static_cast<Handle_t>(params[1]);
677 sec.pIdentity = g_pCoreIdent;
679 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
682 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
685 return pBitBuf->GetNumBitsLeft() >> 3;
688 REGISTER_NATIVES(bitbufnatives)
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},