core/smn_datapacks.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 "sm_globals.h"
ds@1952
    33
#include "HandleSys.h"
ds@1952
    34
#include "CDataPack.h"
ds@1952
    35
ds@1952
    36
HandleType_t g_DataPackType;
ds@1952
    37
ds@1952
    38
class DataPackNatives : 
ds@1952
    39
	public SMGlobalClass,
ds@1952
    40
	public IHandleTypeDispatch
ds@1952
    41
{
ds@1952
    42
public:
ds@1952
    43
	void OnSourceModAllInitialized()
ds@1952
    44
	{
ds@1952
    45
		HandleAccess hacc;
ds@1952
    46
		TypeAccess tacc;
ds@1952
    47
ds@1952
    48
		g_HandleSys.InitAccessDefaults(&tacc, &hacc);
ds@1952
    49
		tacc.access[HTypeAccess_Create] = true;
ds@1952
    50
		tacc.access[HTypeAccess_Inherit] = true;
ds@1952
    51
		tacc.ident = g_pCoreIdent;
ds@1952
    52
		hacc.access[HandleAccess_Read] = HANDLE_RESTRICT_OWNER;
ds@1952
    53
ds@1952
    54
		g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, &tacc, &hacc, g_pCoreIdent, NULL);
ds@1952
    55
	}
ds@1952
    56
	void OnSourceModShutdown()
ds@1952
    57
	{
ds@1952
    58
		g_HandleSys.RemoveType(g_DataPackType, g_pCoreIdent);
ds@1952
    59
		g_DataPackType = 0;
ds@1952
    60
	}
ds@1952
    61
	void OnHandleDestroy(HandleType_t type, void *object)
ds@1952
    62
	{
ds@1952
    63
		g_SourceMod.FreeDataPack(reinterpret_cast<IDataPack *>(object));
ds@1952
    64
	}
ds@1952
    65
};
ds@1952
    66
ds@1952
    67
static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
ds@1952
    68
{
ds@1952
    69
	IDataPack *pDataPack = g_SourceMod.CreateDataPack();
ds@1952
    70
ds@1952
    71
	if (!pDataPack)
ds@1952
    72
	{
ds@1952
    73
		return 0;
ds@1952
    74
	}
ds@1952
    75
ds@1952
    76
	return g_HandleSys.CreateHandle(g_DataPackType, pDataPack, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@1952
    77
}
ds@1952
    78
ds@1952
    79
static cell_t smn_WritePackCell(IPluginContext *pContext, 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
	IDataPack *pDataPack;
ds@1952
    85
ds@1952
    86
	sec.pOwner = pContext->GetIdentity();
ds@1952
    87
	sec.pIdentity = g_pCoreIdent;
ds@1952
    88
ds@1952
    89
	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
ds@1952
    90
		!= HandleError_None)
ds@1952
    91
	{
ds@1952
    92
		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
ds@1952
    93
	}
ds@1952
    94
ds@1952
    95
	pDataPack->PackCell(params[2]);
ds@1952
    96
ds@1952
    97
	return 1;
ds@1952
    98
}
ds@1952
    99
ds@1952
   100
static cell_t smn_WritePackFloat(IPluginContext *pContext, 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
	IDataPack *pDataPack;
ds@1952
   106
ds@1952
   107
	sec.pOwner = pContext->GetIdentity();
ds@1952
   108
	sec.pIdentity = g_pCoreIdent;
ds@1952
   109
ds@1952
   110
	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
ds@1952
   111
		!= HandleError_None)
ds@1952
   112
	{
ds@1952
   113
		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
ds@1952
   114
	}
ds@1952
   115
ds@1952
   116
	pDataPack->PackFloat(sp_ctof(params[2]));
ds@1952
   117
ds@1952
   118
	return 1;
ds@1952
   119
}
ds@1952
   120
ds@1952
   121
static cell_t smn_WritePackString(IPluginContext *pContext, 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
	IDataPack *pDataPack;
ds@1952
   127
	int err;
ds@1952
   128
ds@1952
   129
	sec.pOwner = pContext->GetIdentity();
ds@1952
   130
	sec.pIdentity = g_pCoreIdent;
ds@1952
   131
ds@1952
   132
	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
ds@1952
   133
		!= HandleError_None)
ds@1952
   134
	{
ds@1952
   135
		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
ds@1952
   136
	}
ds@1952
   137
ds@1952
   138
	char *str;
ds@1952
   139
	if ((err=pContext->LocalToString(params[2], &str)) != SP_ERROR_NONE)
ds@1952
   140
	{
ds@1952
   141
		pContext->ThrowNativeErrorEx(err, NULL);
ds@1952
   142
		return 0;
ds@1952
   143
	}
ds@1952
   144
ds@1952
   145
	pDataPack->PackString(str);
ds@1952
   146
ds@1952
   147
	return 1;
ds@1952
   148
}
ds@1952
   149
ds@1952
   150
static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params)
ds@1952
   151
{
ds@1952
   152
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   153
	HandleError herr;
ds@1952
   154
	HandleSecurity sec;
ds@1952
   155
	IDataPack *pDataPack;
ds@1952
   156
ds@1952
   157
	sec.pOwner = pContext->GetIdentity();
ds@1952
   158
	sec.pIdentity = g_pCoreIdent;
ds@1952
   159
ds@1952
   160
	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
ds@1952
   161
		!= HandleError_None)
ds@1952
   162
	{
ds@1952
   163
		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
ds@1952
   164
	}
ds@1952
   165
ds@1952
   166
	if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(cell_t)))
ds@1952
   167
	{
ds@1952
   168
		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
ds@1952
   169
	}
ds@1952
   170
ds@1952
   171
	return pDataPack->ReadCell();
ds@1952
   172
}
ds@1952
   173
ds@1952
   174
static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params)
ds@1952
   175
{
ds@1952
   176
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   177
	HandleError herr;
ds@1952
   178
	HandleSecurity sec;
ds@1952
   179
	IDataPack *pDataPack;
ds@1952
   180
ds@1952
   181
	sec.pOwner = pContext->GetIdentity();
ds@1952
   182
	sec.pIdentity = g_pCoreIdent;
ds@1952
   183
ds@1952
   184
	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
ds@1952
   185
		!= HandleError_None)
ds@1952
   186
	{
ds@1952
   187
		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
ds@1952
   188
	}
ds@1952
   189
ds@1952
   190
	if (!pDataPack->IsReadable(sizeof(size_t) + sizeof(float)))
ds@1952
   191
	{
ds@1952
   192
		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
ds@1952
   193
	}
ds@1952
   194
ds@1952
   195
	return sp_ftoc(pDataPack->ReadFloat());
ds@1952
   196
}
ds@1952
   197
ds@1952
   198
static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params)
ds@1952
   199
{
ds@1952
   200
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   201
	HandleError herr;
ds@1952
   202
	HandleSecurity sec;
ds@1952
   203
	IDataPack *pDataPack;
ds@1952
   204
ds@1952
   205
	sec.pOwner = pContext->GetIdentity();
ds@1952
   206
	sec.pIdentity = g_pCoreIdent;
ds@1952
   207
ds@1952
   208
	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
ds@1952
   209
		!= HandleError_None)
ds@1952
   210
	{
ds@1952
   211
		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
ds@1952
   212
	}
ds@1952
   213
ds@1952
   214
	const char *str;
ds@1952
   215
	if (!(str=pDataPack->ReadString(NULL)))
ds@1952
   216
	{
ds@1952
   217
		return pContext->ThrowNativeError("DataPack operation is out of bounds.");
ds@1952
   218
	}
ds@1952
   219
ds@1952
   220
	pContext->StringToLocal(params[2], params[3], str);
ds@1952
   221
ds@1952
   222
	return 1;
ds@1952
   223
}
ds@1952
   224
ds@1952
   225
static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params)
ds@1952
   226
{
ds@1952
   227
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   228
	HandleError herr;
ds@1952
   229
	HandleSecurity sec;
ds@1952
   230
	IDataPack *pDataPack;
ds@1952
   231
ds@1952
   232
	sec.pOwner = pContext->GetIdentity();
ds@1952
   233
	sec.pIdentity = g_pCoreIdent;
ds@1952
   234
ds@1952
   235
	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
ds@1952
   236
		!= HandleError_None)
ds@1952
   237
	{
ds@1952
   238
		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
ds@1952
   239
	}
ds@1952
   240
ds@1952
   241
	pDataPack->Reset();
ds@1952
   242
	if (params[2])
ds@1952
   243
	{
ds@1952
   244
		pDataPack->ResetSize();
ds@1952
   245
	}
ds@1952
   246
ds@1952
   247
	return 1;
ds@1952
   248
}
ds@1952
   249
ds@1952
   250
static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params)
ds@1952
   251
{
ds@1952
   252
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   253
	HandleError herr;
ds@1952
   254
	HandleSecurity sec;
ds@1952
   255
	IDataPack *pDataPack;
ds@1952
   256
ds@1952
   257
	sec.pOwner = pContext->GetIdentity();
ds@1952
   258
	sec.pIdentity = g_pCoreIdent;
ds@1952
   259
ds@1952
   260
	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
ds@1952
   261
		!= HandleError_None)
ds@1952
   262
	{
ds@1952
   263
		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
ds@1952
   264
	}
ds@1952
   265
ds@1952
   266
	return static_cast<cell_t>(pDataPack->GetPosition());
ds@1952
   267
}
ds@1952
   268
ds@1952
   269
static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params)
ds@1952
   270
{
ds@1952
   271
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   272
	HandleError herr;
ds@1952
   273
	HandleSecurity sec;
ds@1952
   274
	IDataPack *pDataPack;
ds@1952
   275
ds@1952
   276
	sec.pOwner = pContext->GetIdentity();
ds@1952
   277
	sec.pIdentity = g_pCoreIdent;
ds@1952
   278
ds@1952
   279
	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
ds@1952
   280
		!= HandleError_None)
ds@1952
   281
	{
ds@1952
   282
		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
ds@1952
   283
	}
ds@1952
   284
ds@1952
   285
	if (!pDataPack->SetPosition(params[2]))
ds@1952
   286
	{
ds@1952
   287
		return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]);
ds@1952
   288
	}
ds@1952
   289
ds@1952
   290
	return 1;
ds@1952
   291
}
ds@1952
   292
ds@1952
   293
static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params)
ds@1952
   294
{
ds@1952
   295
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   296
	HandleError herr;
ds@1952
   297
	HandleSecurity sec;
ds@1952
   298
	IDataPack *pDataPack;
ds@1952
   299
ds@1952
   300
	sec.pOwner = pContext->GetIdentity();
ds@1952
   301
	sec.pIdentity = g_pCoreIdent;
ds@1952
   302
ds@1952
   303
	if ((herr=g_HandleSys.ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack))
ds@1952
   304
		!= HandleError_None)
ds@1952
   305
	{
ds@1952
   306
		return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr);
ds@1952
   307
	}
ds@1952
   308
ds@1952
   309
	return pDataPack->IsReadable(params[2]) ? 1 : 0;
ds@1952
   310
}
ds@1952
   311
ds@1952
   312
static DataPackNatives s_DataPackNatives;
ds@1952
   313
ds@1952
   314
REGISTER_NATIVES(datapacknatives)
ds@1952
   315
{
ds@1952
   316
	{"CreateDataPack",			smn_CreateDataPack},
ds@1952
   317
	{"WritePackCell",			smn_WritePackCell},
ds@1952
   318
	{"WritePackFloat",			smn_WritePackFloat},
ds@1952
   319
	{"WritePackString",			smn_WritePackString},
ds@1952
   320
	{"ReadPackCell",			smn_ReadPackCell},
ds@1952
   321
	{"ReadPackFloat",			smn_ReadPackFloat},
ds@1952
   322
	{"ReadPackString",			smn_ReadPackString},
ds@1952
   323
	{"ResetPack",				smn_ResetPack},
ds@1952
   324
	{"GetPackPosition",			smn_GetPackPosition},
ds@1952
   325
	{"SetPackPosition",			smn_SetPackPosition},
ds@1952
   326
	{"IsPackReadable",			smn_IsPackReadable},
ds@1952
   327
	{NULL,						NULL}
ds@1952
   328
};