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