extensions/sdktools/vstringtable.cpp
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/sdktools/vstringtable.cpp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,266 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod SDKTools Extension
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include "extension.h"
    1.36 +
    1.37 +static cell_t LockStringTables(IPluginContext *pContext, const cell_t *params)
    1.38 +{
    1.39 +	bool lock = params[1] ? true : false;
    1.40 +
    1.41 +	return engine->LockNetworkStringTables(lock) ? 1 : 0;
    1.42 +}
    1.43 +
    1.44 +static cell_t FindStringTable(IPluginContext *pContext, const cell_t *params)
    1.45 +{
    1.46 +	char *name;
    1.47 +	pContext->LocalToString(params[1], &name);
    1.48 +
    1.49 +	INetworkStringTable *pTable = netstringtables->FindTable(name);
    1.50 +
    1.51 +	if (!pTable)
    1.52 +	{
    1.53 +		return INVALID_STRING_TABLE;
    1.54 +	}
    1.55 +
    1.56 +	return pTable->GetTableId();
    1.57 +}
    1.58 +
    1.59 +static cell_t GetNumStringTables(IPluginContext *pContext, const cell_t *params)
    1.60 +{
    1.61 +	return netstringtables->GetNumTables();
    1.62 +}
    1.63 +
    1.64 +static cell_t GetStringTableNumStrings(IPluginContext *pContext, const cell_t *params)
    1.65 +{
    1.66 +	TABLEID idx = static_cast<TABLEID>(params[1]);
    1.67 +	INetworkStringTable *pTable = netstringtables->GetTable(idx);
    1.68 +
    1.69 +	if (!pTable)
    1.70 +	{
    1.71 +		return pContext->ThrowNativeError("Invalid string table index %d", idx);
    1.72 +	}
    1.73 +
    1.74 +	return pTable->GetNumStrings();
    1.75 +}
    1.76 +
    1.77 +static cell_t GetStringTableMaxStrings(IPluginContext *pContext, const cell_t *params)
    1.78 +{
    1.79 +	TABLEID idx = static_cast<TABLEID>(params[1]);
    1.80 +	INetworkStringTable *pTable = netstringtables->GetTable(idx);
    1.81 +
    1.82 +	if (!pTable)
    1.83 +	{
    1.84 +		return pContext->ThrowNativeError("Invalid string table index %d", idx);
    1.85 +	}
    1.86 +
    1.87 +	return pTable->GetMaxStrings();
    1.88 +}
    1.89 +
    1.90 +static cell_t GetStringTableName(IPluginContext *pContext, const cell_t *params)
    1.91 +{
    1.92 +	TABLEID idx = static_cast<TABLEID>(params[1]);
    1.93 +	INetworkStringTable *pTable = netstringtables->GetTable(idx);
    1.94 +	size_t numBytes;
    1.95 +
    1.96 +	if (!pTable)
    1.97 +	{
    1.98 +		return pContext->ThrowNativeError("Invalid string table index %d", idx);
    1.99 +	}
   1.100 +
   1.101 +	pContext->StringToLocalUTF8(params[2], params[3], pTable->GetTableName(), &numBytes);
   1.102 +
   1.103 +	return numBytes;
   1.104 +}
   1.105 +
   1.106 +static cell_t FindStringIndex(IPluginContext *pContext, const cell_t *params)
   1.107 +{
   1.108 +	TABLEID idx = static_cast<TABLEID>(params[1]);
   1.109 +	INetworkStringTable *pTable = netstringtables->GetTable(idx);
   1.110 +	char *str;
   1.111 +
   1.112 +	if (!pTable)
   1.113 +	{
   1.114 +		return pContext->ThrowNativeError("Invalid string table index %d", idx);
   1.115 +	}
   1.116 +
   1.117 +	pContext->LocalToString(params[2], &str);
   1.118 +
   1.119 +	return pTable->FindStringIndex(str);
   1.120 +}
   1.121 +
   1.122 +static cell_t ReadStringTable(IPluginContext *pContext, const cell_t *params)
   1.123 +{
   1.124 +	TABLEID idx = static_cast<TABLEID>(params[1]);
   1.125 +	INetworkStringTable *pTable = netstringtables->GetTable(idx);
   1.126 +	int stringidx;
   1.127 +	const char *value;
   1.128 +	size_t numBytes;
   1.129 +
   1.130 +	if (!pTable)
   1.131 +	{
   1.132 +		return pContext->ThrowNativeError("Invalid string table index %d", idx);
   1.133 +	}
   1.134 +	
   1.135 +	stringidx = params[2];
   1.136 +	value = pTable->GetString(stringidx);
   1.137 +
   1.138 +	if (!value)
   1.139 +	{
   1.140 +		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
   1.141 +	}
   1.142 +
   1.143 +	pContext->StringToLocalUTF8(params[3], params[4], value, &numBytes);
   1.144 +
   1.145 +	return numBytes;
   1.146 +}
   1.147 +
   1.148 +static cell_t GetStringTableDataLength(IPluginContext *pContext, const cell_t *params)
   1.149 +{
   1.150 +	TABLEID idx = static_cast<TABLEID>(params[1]);
   1.151 +	INetworkStringTable *pTable = netstringtables->GetTable(idx);
   1.152 +	int stringidx;
   1.153 +	const void *userdata;
   1.154 +	int datalen;
   1.155 +
   1.156 +	if (!pTable)
   1.157 +	{
   1.158 +		return pContext->ThrowNativeError("Invalid string table index %d", idx);
   1.159 +	}
   1.160 +
   1.161 +	stringidx = params[2];
   1.162 +	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
   1.163 +	{
   1.164 +		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
   1.165 +	}
   1.166 +
   1.167 +	userdata = pTable->GetStringUserData(stringidx, &datalen);
   1.168 +	if (!userdata)
   1.169 +	{
   1.170 +		datalen = 0;
   1.171 +	}
   1.172 +
   1.173 +	return datalen;
   1.174 +}
   1.175 +
   1.176 +static cell_t GetStringTableData(IPluginContext *pContext, const cell_t *params)
   1.177 +{
   1.178 +	TABLEID idx = static_cast<TABLEID>(params[1]);
   1.179 +	INetworkStringTable *pTable = netstringtables->GetTable(idx);
   1.180 +	int stringidx;
   1.181 +	const char *userdata;
   1.182 +	int datalen;
   1.183 +	size_t numBytes;
   1.184 +
   1.185 +	if (!pTable)
   1.186 +	{
   1.187 +		return pContext->ThrowNativeError("Invalid string table index %d", idx);
   1.188 +	}
   1.189 +
   1.190 +	stringidx = params[2];
   1.191 +	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
   1.192 +	{
   1.193 +		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
   1.194 +	}
   1.195 +
   1.196 +	userdata = (const char *)pTable->GetStringUserData(stringidx, &datalen);
   1.197 +	if (!userdata)
   1.198 +	{
   1.199 +		userdata = "";
   1.200 +	}
   1.201 +
   1.202 +	pContext->StringToLocalUTF8(params[3], params[4], userdata, &numBytes);
   1.203 +
   1.204 +	return numBytes;
   1.205 +}
   1.206 +
   1.207 +static cell_t SetStringTableData(IPluginContext *pContext, const cell_t *params)
   1.208 +{
   1.209 +	TABLEID idx = static_cast<TABLEID>(params[1]);
   1.210 +	INetworkStringTable *pTable = netstringtables->GetTable(idx);
   1.211 +	int stringidx;
   1.212 +	char *userdata;
   1.213 +
   1.214 +	if (!pTable)
   1.215 +	{
   1.216 +		return pContext->ThrowNativeError("Invalid string table index %d", idx);
   1.217 +	}
   1.218 +
   1.219 +	stringidx = params[2];
   1.220 +	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
   1.221 +	{
   1.222 +		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
   1.223 +	}
   1.224 +
   1.225 +	pContext->LocalToString(params[3], &userdata);
   1.226 +	pTable->SetStringUserData(stringidx, params[4], userdata);
   1.227 +
   1.228 +	return 1;
   1.229 +}
   1.230 +
   1.231 +static cell_t AddToStringTable(IPluginContext *pContext, const cell_t *params)
   1.232 +{
   1.233 +	TABLEID idx = static_cast<TABLEID>(params[1]);
   1.234 +	INetworkStringTable *pTable = netstringtables->GetTable(idx);
   1.235 +	char *str, *userdata;
   1.236 +
   1.237 +	if (!pTable)
   1.238 +	{
   1.239 +		return pContext->ThrowNativeError("Invalid string table index %d", idx);
   1.240 +	}
   1.241 +
   1.242 +	pContext->LocalToString(params[2], &str);
   1.243 +	pContext->LocalToString(params[3], &userdata);
   1.244 +
   1.245 +#if defined ORANGEBOX_BUILD
   1.246 +	pTable->AddString(true, str, params[4], userdata);
   1.247 +#else
   1.248 +	pTable->AddString(str, params[4], userdata);
   1.249 +#endif
   1.250 +
   1.251 +	return 1;
   1.252 +}
   1.253 +
   1.254 +sp_nativeinfo_t g_StringTableNatives[] = 
   1.255 +{
   1.256 +	{"LockStringTables",			LockStringTables},
   1.257 +	{"FindStringTable",				FindStringTable},
   1.258 +	{"GetNumStringTables",			GetNumStringTables},
   1.259 +	{"GetStringTableNumStrings",	GetStringTableNumStrings},
   1.260 +	{"GetStringTableMaxStrings",	GetStringTableMaxStrings},
   1.261 +	{"GetStringTableName",			GetStringTableName},
   1.262 +	{"FindStringIndex",				FindStringIndex},
   1.263 +	{"ReadStringTable",				ReadStringTable},
   1.264 +	{"GetStringTableDataLength",	GetStringTableDataLength},
   1.265 +	{"GetStringTableData",			GetStringTableData},
   1.266 +	{"SetStringTableData",			SetStringTableData},
   1.267 +	{"AddToStringTable",			AddToStringTable},
   1.268 +	{NULL,							NULL},
   1.269 +};