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