3 * ================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
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.
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.
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/>.
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>.
33 #include "extension.h"
35 static cell_t LockStringTables(IPluginContext *pContext, const cell_t *params)
37 bool lock = params[1] ? true : false;
39 return engine->LockNetworkStringTables(lock) ? 1 : 0;
42 static cell_t FindStringTable(IPluginContext *pContext, const cell_t *params)
45 pContext->LocalToString(params[1], &name);
47 INetworkStringTable *pTable = netstringtables->FindTable(name);
51 return INVALID_STRING_TABLE;
54 return pTable->GetTableId();
57 static cell_t GetNumStringTables(IPluginContext *pContext, const cell_t *params)
59 return netstringtables->GetNumTables();
62 static cell_t GetStringTableNumStrings(IPluginContext *pContext, const cell_t *params)
64 TABLEID idx = static_cast<TABLEID>(params[1]);
65 INetworkStringTable *pTable = netstringtables->GetTable(idx);
69 return pContext->ThrowNativeError("Invalid string table index %d", idx);
72 return pTable->GetNumStrings();
75 static cell_t GetStringTableMaxStrings(IPluginContext *pContext, const cell_t *params)
77 TABLEID idx = static_cast<TABLEID>(params[1]);
78 INetworkStringTable *pTable = netstringtables->GetTable(idx);
82 return pContext->ThrowNativeError("Invalid string table index %d", idx);
85 return pTable->GetMaxStrings();
88 static cell_t GetStringTableName(IPluginContext *pContext, const cell_t *params)
90 TABLEID idx = static_cast<TABLEID>(params[1]);
91 INetworkStringTable *pTable = netstringtables->GetTable(idx);
96 return pContext->ThrowNativeError("Invalid string table index %d", idx);
99 pContext->StringToLocalUTF8(params[2], params[3], pTable->GetTableName(), &numBytes);
104 static cell_t FindStringIndex(IPluginContext *pContext, const cell_t *params)
106 TABLEID idx = static_cast<TABLEID>(params[1]);
107 INetworkStringTable *pTable = netstringtables->GetTable(idx);
112 return pContext->ThrowNativeError("Invalid string table index %d", idx);
115 pContext->LocalToString(params[2], &str);
117 return pTable->FindStringIndex(str);
120 static cell_t ReadStringTable(IPluginContext *pContext, const cell_t *params)
122 TABLEID idx = static_cast<TABLEID>(params[1]);
123 INetworkStringTable *pTable = netstringtables->GetTable(idx);
130 return pContext->ThrowNativeError("Invalid string table index %d", idx);
133 stringidx = params[2];
134 value = pTable->GetString(stringidx);
138 return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
141 pContext->StringToLocalUTF8(params[3], params[4], value, &numBytes);
146 static cell_t GetStringTableDataLength(IPluginContext *pContext, const cell_t *params)
148 TABLEID idx = static_cast<TABLEID>(params[1]);
149 INetworkStringTable *pTable = netstringtables->GetTable(idx);
151 const void *userdata;
156 return pContext->ThrowNativeError("Invalid string table index %d", idx);
159 stringidx = params[2];
160 if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
162 return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
165 userdata = pTable->GetStringUserData(stringidx, &datalen);
174 static cell_t GetStringTableData(IPluginContext *pContext, const cell_t *params)
176 TABLEID idx = static_cast<TABLEID>(params[1]);
177 INetworkStringTable *pTable = netstringtables->GetTable(idx);
179 const char *userdata;
185 return pContext->ThrowNativeError("Invalid string table index %d", idx);
188 stringidx = params[2];
189 if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
191 return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
194 userdata = (const char *)pTable->GetStringUserData(stringidx, &datalen);
200 pContext->StringToLocalUTF8(params[3], params[4], userdata, &numBytes);
205 static cell_t SetStringTableData(IPluginContext *pContext, const cell_t *params)
207 TABLEID idx = static_cast<TABLEID>(params[1]);
208 INetworkStringTable *pTable = netstringtables->GetTable(idx);
214 return pContext->ThrowNativeError("Invalid string table index %d", idx);
217 stringidx = params[2];
218 if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
220 return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
223 pContext->LocalToString(params[3], &userdata);
224 pTable->SetStringUserData(stringidx, params[4], userdata);
229 static cell_t AddToStringTable(IPluginContext *pContext, const cell_t *params)
231 TABLEID idx = static_cast<TABLEID>(params[1]);
232 INetworkStringTable *pTable = netstringtables->GetTable(idx);
233 char *str, *userdata;
237 return pContext->ThrowNativeError("Invalid string table index %d", idx);
240 pContext->LocalToString(params[2], &str);
241 pContext->LocalToString(params[3], &userdata);
243 pTable->AddString(str, params[4], userdata);
248 sp_nativeinfo_t g_StringTableNatives[] =
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},