3 * =============================================================================
4 * SourceMod Client Preferences 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"
36 * @brief Implement extension code here.
39 ClientPrefs g_ClientPrefs; /**< Global singleton for extension's main interface */
41 SMEXT_LINK(&g_ClientPrefs);
43 HandleType_t g_CookieType = 0;
44 CookieTypeHandler g_CookieTypeHandler;
46 HandleType_t g_CookieIterator = 0;
47 CookieIteratorHandler g_CookieIteratorHandler;
48 DbDriver g_DriverType;
49 static const DatabaseInfo *storage_local = NULL;
51 bool ClientPrefs::SDK_OnLoad(char *error, size_t maxlength, bool late)
53 queryMutex = threader->MakeMutex();
54 cookieMutex = threader->MakeMutex();
56 DBInfo = dbi->FindDatabaseConf("clientprefs");
60 DBInfo = dbi->FindDatabaseConf("default");
63 (strcmp(DBInfo->host, "localhost") == 0 &&
64 strcmp(DBInfo->database, "sourcemod") == 0 &&
65 strcmp(DBInfo->user, "root") == 0 &&
66 strcmp(DBInfo->pass, "") == 0 &&
67 strcmp(DBInfo->driver, "") == 0))
69 storage_local = dbi->FindDatabaseConf("storage-local");
72 DBInfo = storage_local;
78 snprintf(error, maxlength, "Could not find \"clientprefs\" or \"default\" database configs");
83 if (DBInfo->driver[0] != '\0')
85 Driver = dbi->FindOrLoadDriver(DBInfo->driver);
89 Driver = dbi->GetDefaultDriver();
94 snprintf(error, maxlength, "Could not load DB Driver \"%s\"", DBInfo->driver);
99 databaseLoading = true;
100 TQueryOp *op = new TQueryOp(Query_Connect, 0);
101 dbi->AddToThreadQueue(op, PrioQueue_High);
103 dbi->AddDependency(myself, Driver);
105 sharesys->AddNatives(myself, g_ClientPrefNatives);
106 sharesys->RegisterLibrary(myself, "clientprefs");
107 g_CookieManager.cookieDataLoadedForward = forwards->CreateForward("OnClientCookiesCached", ET_Ignore, 1, NULL, Param_Cell);
109 g_CookieType = handlesys->CreateType("Cookie",
110 &g_CookieTypeHandler,
114 myself->GetIdentity(),
117 g_CookieIterator = handlesys->CreateType("CookieIterator",
118 &g_CookieIteratorHandler,
122 myself->GetIdentity(),
125 IMenuStyle *style = menus->GetDefaultStyle();
126 g_CookieManager.clientMenu = style->CreateMenu(&g_Handler, NULL);
127 g_CookieManager.clientMenu->SetDefaultTitle("Client Settings:");
129 plsys->AddPluginsListener(&g_CookieManager);
131 phrases = translator->CreatePhraseCollection();
132 phrases->AddPhraseFile("clientprefs.phrases");
133 phrases->AddPhraseFile("common.phrases");
137 int maxclients = playerhelpers->GetMaxClients();
139 for (int i = 1; i <= maxclients; i++)
141 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(i);
143 if (!pPlayer || !pPlayer->IsAuthorized())
148 g_CookieManager.OnClientAuthorized(i, pPlayer->GetAuthString());
155 void ClientPrefs::SDK_OnAllLoaded()
157 playerhelpers->AddClientListener(&g_CookieManager);
160 bool ClientPrefs::QueryInterfaceDrop(SMInterface *pInterface)
162 if ((void *)pInterface == (void *)(Database->GetDriver()))
170 void ClientPrefs::NotifyInterfaceDrop(SMInterface *pInterface)
172 if (Database != NULL && (void *)pInterface == (void *)(Database->GetDriver()))
179 void ClientPrefs::SDK_OnUnload()
181 handlesys->RemoveType(g_CookieType, myself->GetIdentity());
182 handlesys->RemoveType(g_CookieIterator, myself->GetIdentity());
184 g_CookieManager.Unload();
186 if (Database != NULL)
191 forwards->ReleaseForward(g_CookieManager.cookieDataLoadedForward);
193 g_CookieManager.clientMenu->Destroy();
197 plsys->RemovePluginsListener(&g_CookieManager);
198 playerhelpers->RemoveClientListener(&g_CookieManager);
200 queryMutex->DestroyThis();
201 cookieMutex->DestroyThis();
204 void ClientPrefs::DatabaseConnect()
209 Database = Driver->Connect(DBInfo, true, error, sizeof(error));
211 if (Database == NULL &&
212 DBInfo != storage_local &&
213 storage_local != NULL)
215 DBInfo = storage_local;
216 Database = Driver->Connect(DBInfo, true, error, sizeof(error));
219 if (Database == NULL)
221 g_pSM->LogError(myself, error);
222 databaseLoading = false;
227 const char *identifier = Driver->GetIdentifier();
229 if (strcmp(identifier, "sqlite") == 0)
231 g_DriverType = Driver_SQLite;
233 if (!Database->DoSimpleQuery(
234 "CREATE TABLE IF NOT EXISTS sm_cookies \
236 id INTEGER PRIMARY KEY AUTOINCREMENT, \
237 name varchar(30) NOT NULL UNIQUE, \
238 description varchar(255), \
242 g_pSM->LogMessage(myself, "Failed to CreateTable sm_cookies: %s", Database->GetError());
246 if (!Database->DoSimpleQuery(
247 "CREATE TABLE IF NOT EXISTS sm_cookie_cache \
249 player varchar(65) NOT NULL, \
250 cookie_id int(10) NOT NULL, \
251 value varchar(100), \
253 PRIMARY KEY (player, cookie_id) \
256 g_pSM->LogMessage(myself, "Failed to CreateTable sm_cookie_cache: %s", Database->GetError());
260 else if (strcmp(identifier, "mysql") == 0)
262 g_DriverType = Driver_MySQL;
264 if (!Database->DoSimpleQuery(
265 "CREATE TABLE IF NOT EXISTS sm_cookies \
267 id INTEGER unsigned NOT NULL auto_increment, \
268 name varchar(30) NOT NULL UNIQUE, \
269 description varchar(255), \
274 g_pSM->LogMessage(myself, "Failed to CreateTable sm_cookies: %s", Database->GetError());
278 if (!Database->DoSimpleQuery(
279 "CREATE TABLE IF NOT EXISTS sm_cookie_cache \
281 player varchar(65) NOT NULL, \
282 cookie_id int(10) NOT NULL, \
283 value varchar(100), \
284 timestamp int NOT NULL, \
285 PRIMARY KEY (player, cookie_id) \
288 g_pSM->LogMessage(myself, "Failed to CreateTable sm_cookie_cache: %s", Database->GetError());
294 g_pSM->LogError(myself, "Unsupported driver \"%s\"", identifier);
298 databaseLoading = false;
307 databaseLoading = false;
311 bool ClientPrefs::AddQueryToQueue( TQueryOp *query )
315 if (Database == NULL && databaseLoading)
317 cachedQueries.push_back(query);
318 queryMutex->Unlock();
322 queryMutex->Unlock();
326 query->SetDatabase(Database);
327 dbi->AddToThreadQueue(query, PrioQueue_Normal);
333 /* If Database is NULL and we're not in the loading phase it must have failed - Can't do much */
337 void ClientPrefs::ProcessQueryCache()
339 SourceHook::List<TQueryOp *>::iterator iter;
343 iter = cachedQueries.begin();
345 while (iter != cachedQueries.end())
347 TQueryOp *op = *iter;
349 if (Database != NULL)
351 op->SetDatabase(Database);
352 dbi->AddToThreadQueue(op, PrioQueue_Normal);
362 cachedQueries.clear();
364 queryMutex->Unlock();
367 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
371 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
374 if (len >= maxlength)
376 buffer[maxlength - 1] = '\0';
377 return (maxlength - 1);
385 bool Translate(char *buffer,
388 unsigned int numparams,
394 const char *fail_phrase;
395 void *params[MAX_TRANSLATE_PARAMS];
397 if (numparams > MAX_TRANSLATE_PARAMS)
403 va_start(ap, pOutLength);
404 for (i = 0; i < numparams; i++)
406 params[i] = va_arg(ap, void *);
410 if (!g_ClientPrefs.phrases->FormatString(buffer,
418 if (fail_phrase != NULL)
420 g_pSM->LogError(myself, "[SM] Could not find core phrase: %s", fail_phrase);
424 g_pSM->LogError(myself, "[SM] Unknown fatal error while translating a core phrase.");