Initial Import of beta structs extension.
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"
37 static cell_t smn_BfWriteBool(IPluginContext *pCtx, const cell_t *params)
39 Handle_t hndl = static_cast<Handle_t>(params[1]);
45 sec.pIdentity = g_pCoreIdent;
47 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
50 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
53 pBitBuf->WriteOneBit(params[2]);
58 static cell_t smn_BfWriteByte(IPluginContext *pCtx, const cell_t *params)
60 Handle_t hndl = static_cast<Handle_t>(params[1]);
66 sec.pIdentity = g_pCoreIdent;
68 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
71 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
74 pBitBuf->WriteByte(params[2]);
79 static cell_t smn_BfWriteChar(IPluginContext *pCtx, const cell_t *params)
81 Handle_t hndl = static_cast<Handle_t>(params[1]);
87 sec.pIdentity = g_pCoreIdent;
89 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
92 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
95 pBitBuf->WriteChar(params[2]);
100 static cell_t smn_BfWriteShort(IPluginContext *pCtx, const cell_t *params)
102 Handle_t hndl = static_cast<Handle_t>(params[1]);
108 sec.pIdentity = g_pCoreIdent;
110 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
113 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
116 pBitBuf->WriteShort(params[2]);
121 static cell_t smn_BfWriteWord(IPluginContext *pCtx, const cell_t *params)
123 Handle_t hndl = static_cast<Handle_t>(params[1]);
129 sec.pIdentity = g_pCoreIdent;
131 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
134 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
137 pBitBuf->WriteWord(params[2]);
142 static cell_t smn_BfWriteNum(IPluginContext *pCtx, const cell_t *params)
144 Handle_t hndl = static_cast<Handle_t>(params[1]);
150 sec.pIdentity = g_pCoreIdent;
152 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
155 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
158 pBitBuf->WriteLong(static_cast<long>(params[2]));
163 static cell_t smn_BfWriteFloat(IPluginContext *pCtx, const cell_t *params)
165 Handle_t hndl = static_cast<Handle_t>(params[1]);
171 sec.pIdentity = g_pCoreIdent;
173 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
176 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
179 pBitBuf->WriteFloat(sp_ctof(params[2]));
184 static cell_t smn_BfWriteString(IPluginContext *pCtx, const cell_t *params)
186 Handle_t hndl = static_cast<Handle_t>(params[1]);
193 sec.pIdentity = g_pCoreIdent;
195 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
198 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
202 if ((err=pCtx->LocalToString(params[2], &str)) != SP_ERROR_NONE)
204 pCtx->ThrowNativeErrorEx(err, NULL);
208 pBitBuf->WriteString(str);
213 static cell_t smn_BfWriteEntity(IPluginContext *pCtx, const cell_t *params)
215 Handle_t hndl = static_cast<Handle_t>(params[1]);
221 sec.pIdentity = g_pCoreIdent;
223 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
226 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
229 pBitBuf->WriteShort(params[2]);
234 static cell_t smn_BfWriteAngle(IPluginContext *pCtx, const cell_t *params)
236 Handle_t hndl = static_cast<Handle_t>(params[1]);
242 sec.pIdentity = g_pCoreIdent;
244 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
247 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
250 pBitBuf->WriteBitAngle(sp_ctof(params[2]), params[3]);
255 static cell_t smn_BfWriteCoord(IPluginContext *pCtx, const cell_t *params)
257 Handle_t hndl = static_cast<Handle_t>(params[1]);
263 sec.pIdentity = g_pCoreIdent;
265 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
268 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
271 pBitBuf->WriteBitCoord(sp_ctof(params[2]));
276 static cell_t smn_BfWriteVecCoord(IPluginContext *pCtx, const cell_t *params)
278 Handle_t hndl = static_cast<Handle_t>(params[1]);
284 sec.pIdentity = g_pCoreIdent;
286 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
289 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
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);
300 static cell_t smn_BfWriteVecNormal(IPluginContext *pCtx, const cell_t *params)
302 Handle_t hndl = static_cast<Handle_t>(params[1]);
308 sec.pIdentity = g_pCoreIdent;
310 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
313 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
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);
324 static cell_t smn_BfWriteAngles(IPluginContext *pCtx, const cell_t *params)
326 Handle_t hndl = static_cast<Handle_t>(params[1]);
332 sec.pIdentity = g_pCoreIdent;
334 if ((herr=g_HandleSys.ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
337 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
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);
348 static cell_t smn_BfReadBool(IPluginContext *pCtx, const cell_t *params)
350 Handle_t hndl = static_cast<Handle_t>(params[1]);
356 sec.pIdentity = g_pCoreIdent;
358 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
361 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
364 return pBitBuf->ReadOneBit() ? 1 : 0;
367 static cell_t smn_BfReadByte(IPluginContext *pCtx, const cell_t *params)
369 Handle_t hndl = static_cast<Handle_t>(params[1]);
375 sec.pIdentity = g_pCoreIdent;
377 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
380 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
383 return pBitBuf->ReadByte();
386 static cell_t smn_BfReadChar(IPluginContext *pCtx, const cell_t *params)
388 Handle_t hndl = static_cast<Handle_t>(params[1]);
394 sec.pIdentity = g_pCoreIdent;
396 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
399 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
402 return pBitBuf->ReadChar();
405 static cell_t smn_BfReadShort(IPluginContext *pCtx, const cell_t *params)
407 Handle_t hndl = static_cast<Handle_t>(params[1]);
413 sec.pIdentity = g_pCoreIdent;
415 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
418 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
421 return pBitBuf->ReadShort();
424 static cell_t smn_BfReadWord(IPluginContext *pCtx, const cell_t *params)
426 Handle_t hndl = static_cast<Handle_t>(params[1]);
432 sec.pIdentity = g_pCoreIdent;
434 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
437 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
440 return pBitBuf->ReadWord();
443 static cell_t smn_BfReadNum(IPluginContext *pCtx, const cell_t *params)
445 Handle_t hndl = static_cast<Handle_t>(params[1]);
451 sec.pIdentity = g_pCoreIdent;
453 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
456 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
459 return static_cast<cell_t>(pBitBuf->ReadLong());
462 static cell_t smn_BfReadFloat(IPluginContext *pCtx, const cell_t *params)
464 Handle_t hndl = static_cast<Handle_t>(params[1]);
470 sec.pIdentity = g_pCoreIdent;
472 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
475 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
478 return sp_ftoc(pBitBuf->ReadFloat());
481 static cell_t smn_BfReadString(IPluginContext *pCtx, const cell_t *params)
483 Handle_t hndl = static_cast<Handle_t>(params[1]);
491 sec.pIdentity = g_pCoreIdent;
493 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
496 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
499 pCtx->LocalToPhysAddr(params[2], (cell_t **)&buf);
500 pBitBuf->ReadString(buf, params[3], params[4] ? true : false, &numChars);
502 if (pBitBuf->IsOverflowed())
504 return -numChars - 1;
510 static cell_t smn_BfReadEntity(IPluginContext *pCtx, const cell_t *params)
512 Handle_t hndl = static_cast<Handle_t>(params[1]);
518 sec.pIdentity = g_pCoreIdent;
520 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
523 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
526 return pBitBuf->ReadShort();
529 static cell_t smn_BfReadAngle(IPluginContext *pCtx, const cell_t *params)
531 Handle_t hndl = static_cast<Handle_t>(params[1]);
537 sec.pIdentity = g_pCoreIdent;
539 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
542 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
545 return sp_ftoc(pBitBuf->ReadBitAngle(params[2]));
548 static cell_t smn_BfReadCoord(IPluginContext *pCtx, const cell_t *params)
550 Handle_t hndl = static_cast<Handle_t>(params[1]);
556 sec.pIdentity = g_pCoreIdent;
558 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
561 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
564 return sp_ftoc(pBitBuf->ReadBitCoord());
567 static cell_t smn_BfReadVecCoord(IPluginContext *pCtx, const cell_t *params)
569 Handle_t hndl = static_cast<Handle_t>(params[1]);
575 sec.pIdentity = g_pCoreIdent;
577 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
580 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
584 pCtx->LocalToPhysAddr(params[2], &pVec);
587 pBitBuf->ReadBitVec3Coord(vec);
589 pVec[0] = sp_ftoc(vec.x);
590 pVec[1] = sp_ftoc(vec.y);
591 pVec[2] = sp_ftoc(vec.z);
596 static cell_t smn_BfReadVecNormal(IPluginContext *pCtx, const cell_t *params)
598 Handle_t hndl = static_cast<Handle_t>(params[1]);
604 sec.pIdentity = g_pCoreIdent;
606 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
609 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
613 pCtx->LocalToPhysAddr(params[2], &pVec);
616 pBitBuf->ReadBitVec3Normal(vec);
618 pVec[0] = sp_ftoc(vec.x);
619 pVec[1] = sp_ftoc(vec.y);
620 pVec[2] = sp_ftoc(vec.z);
625 static cell_t smn_BfReadAngles(IPluginContext *pCtx, const cell_t *params)
627 Handle_t hndl = static_cast<Handle_t>(params[1]);
633 sec.pIdentity = g_pCoreIdent;
635 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
638 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
642 pCtx->LocalToPhysAddr(params[2], &pAng);
645 pBitBuf->ReadBitAngles(ang);
647 pAng[0] = sp_ftoc(ang.x);
648 pAng[1] = sp_ftoc(ang.y);
649 pAng[2] = sp_ftoc(ang.z);
654 static cell_t smn_BfGetNumBytesLeft(IPluginContext *pCtx, const cell_t *params)
656 Handle_t hndl = static_cast<Handle_t>(params[1]);
662 sec.pIdentity = g_pCoreIdent;
664 if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
667 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
670 return pBitBuf->GetNumBitsLeft() >> 3;
673 REGISTER_NATIVES(bitbufnatives)
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},