extensions/sdktools/vstringtable.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 3960 d50b36dc6821
permissions -rw-r--r--
Update CS:GO team score gamedata.
     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 #if SOURCE_ENGINE == SE_DOTA
   134 	value = pTable->GetString(stringidx, false);
   135 #else
   136 	value = pTable->GetString(stringidx);
   137 #endif
   138 
   139 	if (!value)
   140 	{
   141 		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
   142 	}
   143 
   144 	pContext->StringToLocalUTF8(params[3], params[4], value, &numBytes);
   145 
   146 	return numBytes;
   147 }
   148 
   149 static cell_t GetStringTableDataLength(IPluginContext *pContext, const cell_t *params)
   150 {
   151 	TABLEID idx = static_cast<TABLEID>(params[1]);
   152 	INetworkStringTable *pTable = netstringtables->GetTable(idx);
   153 	int stringidx;
   154 	const void *userdata;
   155 	int datalen;
   156 
   157 	if (!pTable)
   158 	{
   159 		return pContext->ThrowNativeError("Invalid string table index %d", idx);
   160 	}
   161 
   162 	stringidx = params[2];
   163 	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
   164 	{
   165 		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
   166 	}
   167 
   168 	userdata = pTable->GetStringUserData(stringidx, &datalen);
   169 	if (!userdata)
   170 	{
   171 		datalen = 0;
   172 	}
   173 
   174 	return datalen;
   175 }
   176 
   177 static cell_t GetStringTableData(IPluginContext *pContext, const cell_t *params)
   178 {
   179 	TABLEID idx = static_cast<TABLEID>(params[1]);
   180 	INetworkStringTable *pTable = netstringtables->GetTable(idx);
   181 	int stringidx;
   182 	const char *userdata;
   183 	int datalen;
   184 	size_t numBytes;
   185 
   186 	if (!pTable)
   187 	{
   188 		return pContext->ThrowNativeError("Invalid string table index %d", idx);
   189 	}
   190 
   191 	stringidx = params[2];
   192 	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
   193 	{
   194 		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
   195 	}
   196 
   197 	userdata = (const char *)pTable->GetStringUserData(stringidx, &datalen);
   198 	if (!userdata)
   199 	{
   200 		userdata = "";
   201 	}
   202 
   203 	pContext->StringToLocalUTF8(params[3], params[4], userdata, &numBytes);
   204 
   205 	return numBytes;
   206 }
   207 
   208 static cell_t SetStringTableData(IPluginContext *pContext, const cell_t *params)
   209 {
   210 	TABLEID idx = static_cast<TABLEID>(params[1]);
   211 	INetworkStringTable *pTable = netstringtables->GetTable(idx);
   212 	int stringidx;
   213 	char *userdata;
   214 
   215 	if (!pTable)
   216 	{
   217 		return pContext->ThrowNativeError("Invalid string table index %d", idx);
   218 	}
   219 
   220 	stringidx = params[2];
   221 	if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
   222 	{
   223 		return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
   224 	}
   225 
   226 	pContext->LocalToString(params[3], &userdata);
   227 	pTable->SetStringUserData(stringidx, params[4], userdata);
   228 
   229 	return 1;
   230 }
   231 
   232 static cell_t AddToStringTable(IPluginContext *pContext, const cell_t *params)
   233 {
   234 	TABLEID idx = static_cast<TABLEID>(params[1]);
   235 	INetworkStringTable *pTable = netstringtables->GetTable(idx);
   236 	char *str, *userdata;
   237 
   238 	if (!pTable)
   239 	{
   240 		return pContext->ThrowNativeError("Invalid string table index %d", idx);
   241 	}
   242 
   243 	pContext->LocalToString(params[2], &str);
   244 	pContext->LocalToString(params[3], &userdata);
   245 
   246 #if SOURCE_ENGINE >= SE_ORANGEBOX
   247 	pTable->AddString(true, str, params[4], userdata);
   248 #else
   249 	pTable->AddString(str, params[4], userdata);
   250 #endif
   251 
   252 	return 1;
   253 }
   254 
   255 sp_nativeinfo_t g_StringTableNatives[] = 
   256 {
   257 	{"LockStringTables",			LockStringTables},
   258 	{"FindStringTable",				FindStringTable},
   259 	{"GetNumStringTables",			GetNumStringTables},
   260 	{"GetStringTableNumStrings",	GetStringTableNumStrings},
   261 	{"GetStringTableMaxStrings",	GetStringTableMaxStrings},
   262 	{"GetStringTableName",			GetStringTableName},
   263 	{"FindStringIndex",				FindStringIndex},
   264 	{"ReadStringTable",				ReadStringTable},
   265 	{"GetStringTableDataLength",	GetStringTableDataLength},
   266 	{"GetStringTableData",			GetStringTableData},
   267 	{"SetStringTableData",			SetStringTableData},
   268 	{"AddToStringTable",			AddToStringTable},
   269 	{NULL,							NULL},
   270 };