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