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