3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "extension.h"
34 static cell_t LockStringTables(IPluginContext *pContext, const cell_t *params)
36 bool lock = params[1] ? true : false;
38 return engine->LockNetworkStringTables(lock) ? 1 : 0;
41 static cell_t FindStringTable(IPluginContext *pContext, const cell_t *params)
44 pContext->LocalToString(params[1], &name);
46 INetworkStringTable *pTable = netstringtables->FindTable(name);
50 return INVALID_STRING_TABLE;
53 return pTable->GetTableId();
56 static cell_t GetNumStringTables(IPluginContext *pContext, const cell_t *params)
58 return netstringtables->GetNumTables();
61 static cell_t GetStringTableNumStrings(IPluginContext *pContext, const cell_t *params)
63 TABLEID idx = static_cast<TABLEID>(params[1]);
64 INetworkStringTable *pTable = netstringtables->GetTable(idx);
68 return pContext->ThrowNativeError("Invalid string table index %d", idx);
71 return pTable->GetNumStrings();
74 static cell_t GetStringTableMaxStrings(IPluginContext *pContext, const cell_t *params)
76 TABLEID idx = static_cast<TABLEID>(params[1]);
77 INetworkStringTable *pTable = netstringtables->GetTable(idx);
81 return pContext->ThrowNativeError("Invalid string table index %d", idx);
84 return pTable->GetMaxStrings();
87 static cell_t GetStringTableName(IPluginContext *pContext, const cell_t *params)
89 TABLEID idx = static_cast<TABLEID>(params[1]);
90 INetworkStringTable *pTable = netstringtables->GetTable(idx);
95 return pContext->ThrowNativeError("Invalid string table index %d", idx);
98 pContext->StringToLocalUTF8(params[2], params[3], pTable->GetTableName(), &numBytes);
103 static cell_t FindStringIndex(IPluginContext *pContext, const cell_t *params)
105 TABLEID idx = static_cast<TABLEID>(params[1]);
106 INetworkStringTable *pTable = netstringtables->GetTable(idx);
111 return pContext->ThrowNativeError("Invalid string table index %d", idx);
114 pContext->LocalToString(params[2], &str);
116 return pTable->FindStringIndex(str);
119 static cell_t ReadStringTable(IPluginContext *pContext, const cell_t *params)
121 TABLEID idx = static_cast<TABLEID>(params[1]);
122 INetworkStringTable *pTable = netstringtables->GetTable(idx);
129 return pContext->ThrowNativeError("Invalid string table index %d", idx);
132 stringidx = params[2];
133 value = pTable->GetString(stringidx);
137 return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
140 pContext->StringToLocalUTF8(params[3], params[4], value, &numBytes);
145 static cell_t GetStringTableDataLength(IPluginContext *pContext, const cell_t *params)
147 TABLEID idx = static_cast<TABLEID>(params[1]);
148 INetworkStringTable *pTable = netstringtables->GetTable(idx);
150 const void *userdata;
155 return pContext->ThrowNativeError("Invalid string table index %d", idx);
158 stringidx = params[2];
159 if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
161 return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
164 userdata = pTable->GetStringUserData(stringidx, &datalen);
173 static cell_t GetStringTableData(IPluginContext *pContext, const cell_t *params)
175 TABLEID idx = static_cast<TABLEID>(params[1]);
176 INetworkStringTable *pTable = netstringtables->GetTable(idx);
178 const char *userdata;
184 return pContext->ThrowNativeError("Invalid string table index %d", idx);
187 stringidx = params[2];
188 if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
190 return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
193 userdata = (const char *)pTable->GetStringUserData(stringidx, &datalen);
199 pContext->StringToLocalUTF8(params[3], params[4], userdata, &numBytes);
204 static cell_t SetStringTableData(IPluginContext *pContext, const cell_t *params)
206 TABLEID idx = static_cast<TABLEID>(params[1]);
207 INetworkStringTable *pTable = netstringtables->GetTable(idx);
213 return pContext->ThrowNativeError("Invalid string table index %d", idx);
216 stringidx = params[2];
217 if (stringidx < 0 || stringidx >= pTable->GetNumStrings())
219 return pContext->ThrowNativeError("Invalid string index specified for table (index %d) (table \"%s\")", stringidx, pTable->GetTableName());
222 pContext->LocalToString(params[3], &userdata);
223 pTable->SetStringUserData(stringidx, params[4], userdata);
228 static cell_t AddToStringTable(IPluginContext *pContext, const cell_t *params)
230 TABLEID idx = static_cast<TABLEID>(params[1]);
231 INetworkStringTable *pTable = netstringtables->GetTable(idx);
232 char *str, *userdata;
236 return pContext->ThrowNativeError("Invalid string table index %d", idx);
239 pContext->LocalToString(params[2], &str);
240 pContext->LocalToString(params[3], &userdata);
242 #if defined ORANGEBOX_BUILD
243 pTable->AddString(true, str, params[4], userdata);
245 pTable->AddString(str, params[4], userdata);
251 sp_nativeinfo_t g_StringTableNatives[] =
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},