extensions/sdktools/vstringtable.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Sep 14 02:58:58 2008 -0500 (2008-09-14)
changeset 2287 017a725c9d17
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Eliminated head for dead branch: sourcemod-1.1.0
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod SDKTools Extension
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 "extension.h"
ds@1952
    33
ds@1952
    34
static cell_t LockStringTables(IPluginContext *pContext, const cell_t *params)
ds@1952
    35
{
ds@1952
    36
	bool lock = params[1] ? true : false;
ds@1952
    37
ds@1952
    38
	return engine->LockNetworkStringTables(lock) ? 1 : 0;
ds@1952
    39
}
ds@1952
    40
ds@1952
    41
static cell_t FindStringTable(IPluginContext *pContext, const cell_t *params)
ds@1952
    42
{
ds@1952
    43
	char *name;
ds@1952
    44
	pContext->LocalToString(params[1], &name);
ds@1952
    45
ds@1952
    46
	INetworkStringTable *pTable = netstringtables->FindTable(name);
ds@1952
    47
ds@1952
    48
	if (!pTable)
ds@1952
    49
	{
ds@1952
    50
		return INVALID_STRING_TABLE;
ds@1952
    51
	}
ds@1952
    52
ds@1952
    53
	return pTable->GetTableId();
ds@1952
    54
}
ds@1952
    55
ds@1952
    56
static cell_t GetNumStringTables(IPluginContext *pContext, const cell_t *params)
ds@1952
    57
{
ds@1952
    58
	return netstringtables->GetNumTables();
ds@1952
    59
}
ds@1952
    60
ds@1952
    61
static cell_t GetStringTableNumStrings(IPluginContext *pContext, const cell_t *params)
ds@1952
    62
{
ds@1952
    63
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1952
    64
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1952
    65
ds@1952
    66
	if (!pTable)
ds@1952
    67
	{
ds@1952
    68
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1952
    69
	}
ds@1952
    70
ds@1952
    71
	return pTable->GetNumStrings();
ds@1952
    72
}
ds@1952
    73
ds@1952
    74
static cell_t GetStringTableMaxStrings(IPluginContext *pContext, const cell_t *params)
ds@1952
    75
{
ds@1952
    76
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1952
    77
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1952
    78
ds@1952
    79
	if (!pTable)
ds@1952
    80
	{
ds@1952
    81
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1952
    82
	}
ds@1952
    83
ds@1952
    84
	return pTable->GetMaxStrings();
ds@1952
    85
}
ds@1952
    86
ds@1952
    87
static cell_t GetStringTableName(IPluginContext *pContext, const cell_t *params)
ds@1952
    88
{
ds@1952
    89
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1952
    90
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1952
    91
	size_t numBytes;
ds@1952
    92
ds@1952
    93
	if (!pTable)
ds@1952
    94
	{
ds@1952
    95
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1952
    96
	}
ds@1952
    97
ds@1952
    98
	pContext->StringToLocalUTF8(params[2], params[3], pTable->GetTableName(), &numBytes);
ds@1952
    99
ds@1952
   100
	return numBytes;
ds@1952
   101
}
ds@1952
   102
ds@1952
   103
static cell_t FindStringIndex(IPluginContext *pContext, const cell_t *params)
ds@1952
   104
{
ds@1952
   105
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1952
   106
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1952
   107
	char *str;
ds@1952
   108
ds@1952
   109
	if (!pTable)
ds@1952
   110
	{
ds@1952
   111
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1952
   112
	}
ds@1952
   113
ds@1952
   114
	pContext->LocalToString(params[2], &str);
ds@1952
   115
ds@1952
   116
	return pTable->FindStringIndex(str);
ds@1952
   117
}
ds@1952
   118
ds@1952
   119
static cell_t ReadStringTable(IPluginContext *pContext, const cell_t *params)
ds@1952
   120
{
ds@1952
   121
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1952
   122
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1952
   123
	int stringidx;
ds@1952
   124
	const char *value;
ds@1952
   125
	size_t numBytes;
ds@1952
   126
ds@1952
   127
	if (!pTable)
ds@1952
   128
	{
ds@1952
   129
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1952
   130
	}
ds@1952
   131
	
ds@1952
   132
	stringidx = params[2];
ds@1952
   133
	value = pTable->GetString(stringidx);
ds@1952
   134
ds@1952
   135
	if (!value)
ds@1952
   136
	{
ds@1952
   137
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1952
   138
	}
ds@1952
   139
ds@1952
   140
	pContext->StringToLocalUTF8(params[3], params[4], value, &numBytes);
ds@1952
   141
ds@1952
   142
	return numBytes;
ds@1952
   143
}
ds@1952
   144
ds@1952
   145
static cell_t GetStringTableDataLength(IPluginContext *pContext, const cell_t *params)
ds@1952
   146
{
ds@1952
   147
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1952
   148
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1952
   149
	int stringidx;
ds@1952
   150
	const void *userdata;
ds@1952
   151
	int datalen;
ds@1952
   152
ds@1952
   153
	if (!pTable)
ds@1952
   154
	{
ds@1952
   155
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1952
   156
	}
ds@1952
   157
ds@1952
   158
	stringidx = params[2];
ds@1952
   159
	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
ds@1952
   160
	{
ds@1952
   161
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1952
   162
	}
ds@1952
   163
ds@1952
   164
	userdata = pTable->GetStringUserData(stringidx, &datalen);
ds@1952
   165
	if (!userdata)
ds@1952
   166
	{
ds@1952
   167
		datalen = 0;
ds@1952
   168
	}
ds@1952
   169
ds@1952
   170
	return datalen;
ds@1952
   171
}
ds@1952
   172
ds@1952
   173
static cell_t GetStringTableData(IPluginContext *pContext, const cell_t *params)
ds@1952
   174
{
ds@1952
   175
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1952
   176
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1952
   177
	int stringidx;
ds@1952
   178
	const char *userdata;
ds@1952
   179
	int datalen;
ds@1952
   180
	size_t numBytes;
ds@1952
   181
ds@1952
   182
	if (!pTable)
ds@1952
   183
	{
ds@1952
   184
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1952
   185
	}
ds@1952
   186
ds@1952
   187
	stringidx = params[2];
ds@1952
   188
	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
ds@1952
   189
	{
ds@1952
   190
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1952
   191
	}
ds@1952
   192
ds@1952
   193
	userdata = (const char *)pTable->GetStringUserData(stringidx, &datalen);
ds@1952
   194
	if (!userdata)
ds@1952
   195
	{
ds@1952
   196
		userdata = "";
ds@1952
   197
	}
ds@1952
   198
ds@1952
   199
	pContext->StringToLocalUTF8(params[3], params[4], userdata, &numBytes);
ds@1952
   200
ds@1952
   201
	return numBytes;
ds@1952
   202
}
ds@1952
   203
ds@1952
   204
static cell_t SetStringTableData(IPluginContext *pContext, const cell_t *params)
ds@1952
   205
{
ds@1952
   206
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1952
   207
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1952
   208
	int stringidx;
ds@1952
   209
	char *userdata;
ds@1952
   210
ds@1952
   211
	if (!pTable)
ds@1952
   212
	{
ds@1952
   213
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1952
   214
	}
ds@1952
   215
ds@1952
   216
	stringidx = params[2];
ds@1952
   217
	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
ds@1952
   218
	{
ds@1952
   219
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1952
   220
	}
ds@1952
   221
ds@1952
   222
	pContext->LocalToString(params[3], &userdata);
ds@1952
   223
	pTable->SetStringUserData(stringidx, params[4], userdata);
ds@1952
   224
ds@1952
   225
	return 1;
ds@1952
   226
}
ds@1952
   227
ds@1952
   228
static cell_t AddToStringTable(IPluginContext *pContext, const cell_t *params)
ds@1952
   229
{
ds@1952
   230
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1952
   231
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1952
   232
	char *str, *userdata;
ds@1952
   233
ds@1952
   234
	if (!pTable)
ds@1952
   235
	{
ds@1952
   236
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1952
   237
	}
ds@1952
   238
ds@1952
   239
	pContext->LocalToString(params[2], &str);
ds@1952
   240
	pContext->LocalToString(params[3], &userdata);
ds@1952
   241
ds@1952
   242
#if defined ORANGEBOX_BUILD
ds@1952
   243
	pTable->AddString(true, str, params[4], userdata);
ds@1952
   244
#else
ds@1952
   245
	pTable->AddString(str, params[4], userdata);
ds@1952
   246
#endif
ds@1952
   247
ds@1952
   248
	return 1;
ds@1952
   249
}
ds@1952
   250
ds@1952
   251
sp_nativeinfo_t g_StringTableNatives[] = 
ds@1952
   252
{
ds@1952
   253
	{"LockStringTables",			LockStringTables},
ds@1952
   254
	{"FindStringTable",				FindStringTable},
ds@1952
   255
	{"GetNumStringTables",			GetNumStringTables},
ds@1952
   256
	{"GetStringTableNumStrings",	GetStringTableNumStrings},
ds@1952
   257
	{"GetStringTableMaxStrings",	GetStringTableMaxStrings},
ds@1952
   258
	{"GetStringTableName",			GetStringTableName},
ds@1952
   259
	{"FindStringIndex",				FindStringIndex},
ds@1952
   260
	{"ReadStringTable",				ReadStringTable},
ds@1952
   261
	{"GetStringTableDataLength",	GetStringTableDataLength},
ds@1952
   262
	{"GetStringTableData",			GetStringTableData},
ds@1952
   263
	{"SetStringTableData",			SetStringTableData},
ds@1952
   264
	{"AddToStringTable",			AddToStringTable},
ds@1952
   265
	{NULL,							NULL},
ds@1952
   266
};