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