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