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