extensions/sdktools/vstringtable.cpp
author David Anderson <dvander@alliedmods.net>
Wed Aug 01 02:48:11 2007 +0000 (2007-08-01)
changeset 1233 e9cfc03b9bb8
parent 1116 40cc5e9f0f1b
child 1330 ab94316bb52b
permissions -rw-r--r--
updated sdktools license headers
ds@1114
     1
/**
ds@1114
     2
 * vim: set ts=4 :
dvander@1233
     3
 * ================================================================
dvander@1233
     4
 * SourceMod SDKTools Extension
dvander@1233
     5
 * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
dvander@1233
     6
 * ================================================================
ds@1114
     7
 *
dvander@1233
     8
 * This program is free software; you can redistribute it and/or 
dvander@1233
     9
 * modify it under the terms of the GNU General Public License, 
dvander@1233
    10
 * version 3.0, as published by the Free Software Foundation.
dvander@1233
    11
 * 
ds@1114
    12
 * This program is distributed in the hope that it will be useful,
ds@1114
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ds@1114
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ds@1114
    15
 * GNU General Public License for more details.
dvander@1233
    16
 *
ds@1114
    17
 * You should have received a copy of the GNU General Public License
dvander@1233
    18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1233
    19
 *
dvander@1233
    20
 * As a special exception, AlliedModders LLC gives you permission to 
dvander@1233
    21
 * link the code of this program (as well as its derivative works) to 
dvander@1233
    22
 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
dvander@1233
    23
 * Game MODs that run on software by the Valve Corporation.  You must 
dvander@1233
    24
 * obey the GNU General Public License in all respects for all other 
dvander@1233
    25
 * code used. Additionally, AlliedModders LLC grants this exception 
dvander@1233
    26
 * to all derivative works. AlliedModders LLC defines further 
dvander@1233
    27
 * exceptions, found in LICENSE.txt (as of this writing, version 
dvander@1233
    28
 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
ds@1114
    29
 *
ds@1114
    30
 * Version: $Id$
ds@1114
    31
 */
ds@1114
    32
ds@1114
    33
#include "extension.h"
ds@1114
    34
ds@1114
    35
static cell_t LockStringTables(IPluginContext *pContext, const cell_t *params)
ds@1114
    36
{
ds@1114
    37
	bool lock = params[1] ? true : false;
ds@1114
    38
ds@1114
    39
	return engine->LockNetworkStringTables(lock) ? 1 : 0;
ds@1114
    40
}
ds@1114
    41
ds@1114
    42
static cell_t FindStringTable(IPluginContext *pContext, const cell_t *params)
ds@1114
    43
{
ds@1114
    44
	char *name;
ds@1114
    45
	pContext->LocalToString(params[1], &name);
ds@1114
    46
ds@1114
    47
	INetworkStringTable *pTable = netstringtables->FindTable(name);
ds@1114
    48
ds@1114
    49
	if (!pTable)
ds@1114
    50
	{
ds@1114
    51
		return INVALID_STRING_TABLE;
ds@1114
    52
	}
ds@1114
    53
ds@1114
    54
	return pTable->GetTableId();
ds@1114
    55
}
ds@1114
    56
ds@1114
    57
static cell_t GetNumStringTables(IPluginContext *pContext, const cell_t *params)
ds@1114
    58
{
ds@1114
    59
	return netstringtables->GetNumTables();
ds@1114
    60
}
ds@1114
    61
ds@1114
    62
static cell_t GetStringTableNumStrings(IPluginContext *pContext, const cell_t *params)
ds@1114
    63
{
ds@1114
    64
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
    65
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
    66
ds@1114
    67
	if (!pTable)
ds@1114
    68
	{
ds@1114
    69
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
    70
	}
ds@1114
    71
ds@1114
    72
	return pTable->GetNumStrings();
ds@1114
    73
}
ds@1114
    74
ds@1114
    75
static cell_t GetStringTableMaxStrings(IPluginContext *pContext, const cell_t *params)
ds@1114
    76
{
ds@1114
    77
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
    78
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
    79
ds@1114
    80
	if (!pTable)
ds@1114
    81
	{
ds@1114
    82
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
    83
	}
ds@1114
    84
ds@1114
    85
	return pTable->GetMaxStrings();
ds@1114
    86
}
ds@1114
    87
ds@1114
    88
static cell_t GetStringTableName(IPluginContext *pContext, const cell_t *params)
ds@1114
    89
{
ds@1114
    90
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
    91
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
    92
	size_t numBytes;
ds@1114
    93
ds@1114
    94
	if (!pTable)
ds@1114
    95
	{
ds@1114
    96
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
    97
	}
ds@1114
    98
ds@1114
    99
	pContext->StringToLocalUTF8(params[2], params[3], pTable->GetTableName(), &numBytes);
ds@1114
   100
ds@1114
   101
	return numBytes;
ds@1114
   102
}
ds@1114
   103
ds@1114
   104
static cell_t FindStringIndex(IPluginContext *pContext, const cell_t *params)
ds@1114
   105
{
ds@1114
   106
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
   107
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
   108
	char *str;
ds@1114
   109
ds@1114
   110
	if (!pTable)
ds@1114
   111
	{
ds@1114
   112
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   113
	}
ds@1114
   114
ds@1114
   115
	pContext->LocalToString(params[2], &str);
ds@1114
   116
ds@1114
   117
	return pTable->FindStringIndex(str);
ds@1114
   118
}
ds@1114
   119
ds@1114
   120
static cell_t ReadStringTable(IPluginContext *pContext, const cell_t *params)
ds@1114
   121
{
ds@1114
   122
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
   123
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
   124
	int stringidx;
ds@1114
   125
	const char *value;
ds@1114
   126
	size_t numBytes;
ds@1114
   127
ds@1114
   128
	if (!pTable)
ds@1114
   129
	{
ds@1114
   130
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   131
	}
ds@1114
   132
	
ds@1114
   133
	stringidx = params[2];
ds@1114
   134
	value = pTable->GetString(stringidx);
ds@1114
   135
ds@1114
   136
	if (!value)
ds@1114
   137
	{
ds@1114
   138
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1114
   139
	}
ds@1114
   140
ds@1114
   141
	pContext->StringToLocalUTF8(params[3], params[4], value, &numBytes);
ds@1114
   142
ds@1114
   143
	return numBytes;
ds@1114
   144
}
ds@1114
   145
ds@1114
   146
static cell_t GetStringTableDataLength(IPluginContext *pContext, const cell_t *params)
ds@1114
   147
{
ds@1114
   148
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
   149
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
   150
	int stringidx;
ds@1114
   151
	const void *userdata;
ds@1114
   152
	int datalen;
ds@1114
   153
ds@1114
   154
	if (!pTable)
ds@1114
   155
	{
ds@1114
   156
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   157
	}
ds@1114
   158
ds@1114
   159
	stringidx = params[2];
ds@1114
   160
	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
ds@1114
   161
	{
ds@1114
   162
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1114
   163
	}
ds@1114
   164
ds@1114
   165
	userdata = pTable->GetStringUserData(stringidx, &datalen);
ds@1114
   166
	if (!userdata)
ds@1114
   167
	{
ds@1114
   168
		datalen = 0;
ds@1114
   169
	}
ds@1114
   170
ds@1114
   171
	return datalen;
ds@1114
   172
}
ds@1114
   173
ds@1114
   174
static cell_t GetStringTableData(IPluginContext *pContext, const cell_t *params)
ds@1114
   175
{
ds@1114
   176
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
   177
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
   178
	int stringidx;
ds@1114
   179
	const char *userdata;
ds@1114
   180
	int datalen;
ds@1114
   181
	size_t numBytes;
ds@1114
   182
ds@1114
   183
	if (!pTable)
ds@1114
   184
	{
ds@1114
   185
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   186
	}
ds@1114
   187
ds@1114
   188
	stringidx = params[2];
ds@1114
   189
	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
ds@1114
   190
	{
ds@1114
   191
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1114
   192
	}
ds@1114
   193
ds@1114
   194
	userdata = (const char *)pTable->GetStringUserData(stringidx, &datalen);
ds@1114
   195
	if (!userdata)
ds@1114
   196
	{
ds@1114
   197
		userdata = "";
ds@1114
   198
	}
ds@1114
   199
ds@1114
   200
	pContext->StringToLocalUTF8(params[3], params[4], userdata, &numBytes);
ds@1114
   201
ds@1114
   202
	return numBytes;
ds@1114
   203
}
ds@1114
   204
ds@1114
   205
static cell_t SetStringTableData(IPluginContext *pContext, const cell_t *params)
ds@1114
   206
{
ds@1114
   207
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
   208
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
   209
	int stringidx;
ds@1114
   210
	char *userdata;
ds@1114
   211
ds@1114
   212
	if (!pTable)
ds@1114
   213
	{
ds@1114
   214
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   215
	}
ds@1114
   216
ds@1114
   217
	stringidx = params[2];
ds@1114
   218
	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
ds@1114
   219
	{
ds@1114
   220
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1114
   221
	}
ds@1114
   222
ds@1114
   223
	pContext->LocalToString(params[3], &userdata);
ds@1114
   224
	pTable->SetStringUserData(stringidx, params[4], userdata);
ds@1114
   225
ds@1114
   226
	return 1;
ds@1114
   227
}
ds@1114
   228
ds@1114
   229
static cell_t AddToStringTable(IPluginContext *pContext, const cell_t *params)
ds@1114
   230
{
ds@1114
   231
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
   232
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
   233
	char *str, *userdata;
ds@1114
   234
ds@1114
   235
	if (!pTable)
ds@1114
   236
	{
ds@1114
   237
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   238
	}
ds@1114
   239
ds@1114
   240
	pContext->LocalToString(params[2], &str);
ds@1114
   241
	pContext->LocalToString(params[3], &userdata);
ds@1114
   242
ds@1114
   243
	pTable->AddString(str, params[4], userdata);
ds@1114
   244
ds@1114
   245
	return 1;
ds@1114
   246
}
ds@1114
   247
ds@1114
   248
sp_nativeinfo_t g_StringTableNatives[] = 
ds@1114
   249
{
ds@1114
   250
	{"LockStringTables",			LockStringTables},
ds@1114
   251
	{"FindStringTable",				FindStringTable},
ds@1114
   252
	{"GetNumStringTables",			GetNumStringTables},
ds@1114
   253
	{"GetStringTableNumStrings",	GetStringTableNumStrings},
ds@1114
   254
	{"GetStringTableMaxStrings",	GetStringTableMaxStrings},
ds@1114
   255
	{"GetStringTableName",			GetStringTableName},
ds@1114
   256
	{"FindStringIndex",				FindStringIndex},
ds@1114
   257
	{"ReadStringTable",				ReadStringTable},
ds@1114
   258
	{"GetStringTableDataLength",	GetStringTableDataLength},
ds@1114
   259
	{"GetStringTableData",			GetStringTableData},
ds@1114
   260
	{"SetStringTableData",			SetStringTableData},
ds@1114
   261
	{"AddToStringTable",			AddToStringTable},
ds@1114
   262
	{NULL,							NULL},
ds@1114
   263
};