core/smn_datapacks.cpp
author Phong Dang <teame06@gmail.com>
Wed Aug 01 07:29:40 2007 +0000 (2007-08-01)
changeset 1241 2e8046793bc9
parent 1231 17badaf66ce4
child 1330 ab94316bb52b
permissions -rw-r--r--
Added check to make sure player are alive to perform sm_slap, sm_burn, sm_slay.
faluco@490
     1
/**
dvander@600
     2
 * vim: set ts=4 :
dvander@1231
     3
 * ================================================================
dvander@1231
     4
 * SourceMod
dvander@1231
     5
 * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
dvander@1231
     6
 * ================================================================
dvander@600
     7
 *
dvander@1231
     8
 * This program is free software; you can redistribute it and/or 
dvander@1231
     9
 * modify it under the terms of the GNU General Public License, 
dvander@1231
    10
 * version 3.0, as published by the Free Software Foundation.
dvander@1231
    11
 * 
dvander@1231
    12
 * This program is distributed in the hope that it will be useful,
dvander@1231
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
dvander@1231
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dvander@1231
    15
 * GNU General Public License for more details.
dvander@1231
    16
 *
dvander@1231
    17
 * You should have received a copy of the GNU General Public License
dvander@1231
    18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
dvander@1231
    20
 * As a special exception, AlliedModders LLC gives you permission to 
dvander@1231
    21
 * link the code of this program (as well as its derivative works) to 
dvander@1231
    22
 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
dvander@1231
    23
 * Game MODs that run on software by the Valve Corporation.  You must 
dvander@1231
    24
 * obey the GNU General Public License in all respects for all other 
dvander@1231
    25
 * code used. Additionally, AlliedModders LLC grants this exception 
dvander@1231
    26
 * to all derivative works. AlliedModders LLC defines further 
dvander@1231
    27
 * exceptions, found in LICENSE.txt (as of this writing, version 
dvander@1231
    28
 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
dvander@600
    29
 *
dvander@600
    30
 * Version: $Id$
dvander@600
    31
 */
faluco@490
    32
faluco@484
    33
#include "sm_globals.h"
faluco@484
    34
#include "HandleSys.h"
faluco@484
    35
#include "CDataPack.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@1191
    49
		g_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@1191
    55
		g_DataPackType = g_HandleSys.CreateType("DataPack", this, 0, &tacc, &hacc, g_pCoreIdent, NULL);
faluco@484
    56
	}
faluco@484
    57
	void OnSourceModShutdown()
faluco@484
    58
	{
faluco@484
    59
		g_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
	{
faluco@484
    64
		g_SourceMod.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
{
faluco@484
    70
	IDataPack *pDataPack = g_SourceMod.CreateDataPack();
faluco@484
    71
faluco@484
    72
	if (!pDataPack)
faluco@484
    73
	{
faluco@484
    74
		return 0;
faluco@484
    75
	}
faluco@484
    76
faluco@484
    77
	return g_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
faluco@484
    90
	if ((herr=g_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
faluco@484
   111
	if ((herr=g_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
faluco@484
   133
	if ((herr=g_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
faluco@484
   161
	if ((herr=g_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
faluco@484
   185
	if ((herr=g_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
faluco@484
   209
	if ((herr=g_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
faluco@484
   236
	if ((herr=g_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
faluco@484
   261
	if ((herr=g_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
faluco@484
   280
	if ((herr=g_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
faluco@484
   304
	if ((herr=g_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
};