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