extensions/sdktools/vstringtable.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Jul 15 08:00:26 2007 +0000 (2007-07-15)
changeset 1116 40cc5e9f0f1b
parent 1114 4b4a61f32a96
child 1233 e9cfc03b9bb8
permissions -rw-r--r--
Removed strange debugging code
ds@1114
     1
/**
ds@1114
     2
 * vim: set ts=4 :
ds@1114
     3
 * ===============================================================
ds@1114
     4
 * SourceMod SDK Tools Extension
ds@1114
     5
 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
ds@1114
     6
 * ===============================================================
ds@1114
     7
 *
ds@1114
     8
 * This program is free software; you can redistribute it and/or
ds@1114
     9
 * modify it under the terms of the GNU General Public License
ds@1114
    10
 * as published by the Free Software Foundation; either version 2
ds@1114
    11
 * of the License, or (at your option) any later version.
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.
ds@1114
    16
 * 
ds@1114
    17
 * You should have received a copy of the GNU General Public License
ds@1114
    18
 * along with this program; if not, write to the Free Software
ds@1114
    19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
ds@1114
    20
 *
ds@1114
    21
 * Version: $Id$
ds@1114
    22
 */
ds@1114
    23
ds@1114
    24
#include "extension.h"
ds@1114
    25
ds@1114
    26
static cell_t LockStringTables(IPluginContext *pContext, const cell_t *params)
ds@1114
    27
{
ds@1114
    28
	bool lock = params[1] ? true : false;
ds@1114
    29
ds@1114
    30
	return engine->LockNetworkStringTables(lock) ? 1 : 0;
ds@1114
    31
}
ds@1114
    32
ds@1114
    33
static cell_t FindStringTable(IPluginContext *pContext, const cell_t *params)
ds@1114
    34
{
ds@1114
    35
	char *name;
ds@1114
    36
	pContext->LocalToString(params[1], &name);
ds@1114
    37
ds@1114
    38
	INetworkStringTable *pTable = netstringtables->FindTable(name);
ds@1114
    39
ds@1114
    40
	if (!pTable)
ds@1114
    41
	{
ds@1114
    42
		return INVALID_STRING_TABLE;
ds@1114
    43
	}
ds@1114
    44
ds@1114
    45
	return pTable->GetTableId();
ds@1114
    46
}
ds@1114
    47
ds@1114
    48
static cell_t GetNumStringTables(IPluginContext *pContext, const cell_t *params)
ds@1114
    49
{
ds@1114
    50
	return netstringtables->GetNumTables();
ds@1114
    51
}
ds@1114
    52
ds@1114
    53
static cell_t GetStringTableNumStrings(IPluginContext *pContext, const cell_t *params)
ds@1114
    54
{
ds@1114
    55
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
    56
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
    57
ds@1114
    58
	if (!pTable)
ds@1114
    59
	{
ds@1114
    60
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
    61
	}
ds@1114
    62
ds@1114
    63
	return pTable->GetNumStrings();
ds@1114
    64
}
ds@1114
    65
ds@1114
    66
static cell_t GetStringTableMaxStrings(IPluginContext *pContext, const cell_t *params)
ds@1114
    67
{
ds@1114
    68
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
    69
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
    70
ds@1114
    71
	if (!pTable)
ds@1114
    72
	{
ds@1114
    73
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
    74
	}
ds@1114
    75
ds@1114
    76
	return pTable->GetMaxStrings();
ds@1114
    77
}
ds@1114
    78
ds@1114
    79
static cell_t GetStringTableName(IPluginContext *pContext, const cell_t *params)
ds@1114
    80
{
ds@1114
    81
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
    82
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
    83
	size_t numBytes;
ds@1114
    84
ds@1114
    85
	if (!pTable)
ds@1114
    86
	{
ds@1114
    87
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
    88
	}
ds@1114
    89
ds@1114
    90
	pContext->StringToLocalUTF8(params[2], params[3], pTable->GetTableName(), &numBytes);
ds@1114
    91
ds@1114
    92
	return numBytes;
ds@1114
    93
}
ds@1114
    94
ds@1114
    95
static cell_t FindStringIndex(IPluginContext *pContext, const cell_t *params)
ds@1114
    96
{
ds@1114
    97
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
    98
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
    99
	char *str;
ds@1114
   100
ds@1114
   101
	if (!pTable)
ds@1114
   102
	{
ds@1114
   103
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   104
	}
ds@1114
   105
ds@1114
   106
	pContext->LocalToString(params[2], &str);
ds@1114
   107
ds@1114
   108
	return pTable->FindStringIndex(str);
ds@1114
   109
}
ds@1114
   110
ds@1114
   111
static cell_t ReadStringTable(IPluginContext *pContext, const cell_t *params)
ds@1114
   112
{
ds@1114
   113
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
   114
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
   115
	int stringidx;
ds@1114
   116
	const char *value;
ds@1114
   117
	size_t numBytes;
ds@1114
   118
ds@1114
   119
	if (!pTable)
ds@1114
   120
	{
ds@1114
   121
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   122
	}
ds@1114
   123
	
ds@1114
   124
	stringidx = params[2];
ds@1114
   125
	value = pTable->GetString(stringidx);
ds@1114
   126
ds@1114
   127
	if (!value)
ds@1114
   128
	{
ds@1114
   129
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1114
   130
	}
ds@1114
   131
ds@1114
   132
	pContext->StringToLocalUTF8(params[3], params[4], value, &numBytes);
ds@1114
   133
ds@1114
   134
	return numBytes;
ds@1114
   135
}
ds@1114
   136
ds@1114
   137
static cell_t GetStringTableDataLength(IPluginContext *pContext, const cell_t *params)
ds@1114
   138
{
ds@1114
   139
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
   140
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
   141
	int stringidx;
ds@1114
   142
	const void *userdata;
ds@1114
   143
	int datalen;
ds@1114
   144
ds@1114
   145
	if (!pTable)
ds@1114
   146
	{
ds@1114
   147
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   148
	}
ds@1114
   149
ds@1114
   150
	stringidx = params[2];
ds@1114
   151
	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
ds@1114
   152
	{
ds@1114
   153
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1114
   154
	}
ds@1114
   155
ds@1114
   156
	userdata = pTable->GetStringUserData(stringidx, &datalen);
ds@1114
   157
	if (!userdata)
ds@1114
   158
	{
ds@1114
   159
		datalen = 0;
ds@1114
   160
	}
ds@1114
   161
ds@1114
   162
	return datalen;
ds@1114
   163
}
ds@1114
   164
ds@1114
   165
static cell_t GetStringTableData(IPluginContext *pContext, const cell_t *params)
ds@1114
   166
{
ds@1114
   167
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
   168
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
   169
	int stringidx;
ds@1114
   170
	const char *userdata;
ds@1114
   171
	int datalen;
ds@1114
   172
	size_t numBytes;
ds@1114
   173
ds@1114
   174
	if (!pTable)
ds@1114
   175
	{
ds@1114
   176
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   177
	}
ds@1114
   178
ds@1114
   179
	stringidx = params[2];
ds@1114
   180
	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
ds@1114
   181
	{
ds@1114
   182
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1114
   183
	}
ds@1114
   184
ds@1114
   185
	userdata = (const char *)pTable->GetStringUserData(stringidx, &datalen);
ds@1114
   186
	if (!userdata)
ds@1114
   187
	{
ds@1114
   188
		userdata = "";
ds@1114
   189
	}
ds@1114
   190
ds@1114
   191
	pContext->StringToLocalUTF8(params[3], params[4], userdata, &numBytes);
ds@1114
   192
ds@1114
   193
	return numBytes;
ds@1114
   194
}
ds@1114
   195
ds@1114
   196
static cell_t SetStringTableData(IPluginContext *pContext, const cell_t *params)
ds@1114
   197
{
ds@1114
   198
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
   199
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
   200
	int stringidx;
ds@1114
   201
	char *userdata;
ds@1114
   202
ds@1114
   203
	if (!pTable)
ds@1114
   204
	{
ds@1114
   205
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   206
	}
ds@1114
   207
ds@1114
   208
	stringidx = params[2];
ds@1114
   209
	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
ds@1114
   210
	{
ds@1114
   211
		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
ds@1114
   212
	}
ds@1114
   213
ds@1114
   214
	pContext->LocalToString(params[3], &userdata);
ds@1114
   215
	pTable->SetStringUserData(stringidx, params[4], userdata);
ds@1114
   216
ds@1114
   217
	return 1;
ds@1114
   218
}
ds@1114
   219
ds@1114
   220
static cell_t AddToStringTable(IPluginContext *pContext, const cell_t *params)
ds@1114
   221
{
ds@1114
   222
	TABLEID idx = static_cast<TABLEID>(params[1]);
ds@1114
   223
	INetworkStringTable *pTable = netstringtables->GetTable(idx);
ds@1114
   224
	char *str, *userdata;
ds@1114
   225
ds@1114
   226
	if (!pTable)
ds@1114
   227
	{
ds@1114
   228
		return pContext->ThrowNativeError("Invalid string table index %d", idx);
ds@1114
   229
	}
ds@1114
   230
ds@1114
   231
	pContext->LocalToString(params[2], &str);
ds@1114
   232
	pContext->LocalToString(params[3], &userdata);
ds@1114
   233
ds@1114
   234
	pTable->AddString(str, params[4], userdata);
ds@1114
   235
ds@1114
   236
	return 1;
ds@1114
   237
}
ds@1114
   238
ds@1114
   239
sp_nativeinfo_t g_StringTableNatives[] = 
ds@1114
   240
{
ds@1114
   241
	{"LockStringTables",			LockStringTables},
ds@1114
   242
	{"FindStringTable",				FindStringTable},
ds@1114
   243
	{"GetNumStringTables",			GetNumStringTables},
ds@1114
   244
	{"GetStringTableNumStrings",	GetStringTableNumStrings},
ds@1114
   245
	{"GetStringTableMaxStrings",	GetStringTableMaxStrings},
ds@1114
   246
	{"GetStringTableName",			GetStringTableName},
ds@1114
   247
	{"FindStringIndex",				FindStringIndex},
ds@1114
   248
	{"ReadStringTable",				ReadStringTable},
ds@1114
   249
	{"GetStringTableDataLength",	GetStringTableDataLength},
ds@1114
   250
	{"GetStringTableData",			GetStringTableData},
ds@1114
   251
	{"SetStringTableData",			SetStringTableData},
ds@1114
   252
	{"AddToStringTable",			AddToStringTable},
ds@1114
   253
	{NULL,							NULL},
ds@1114
   254
};