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");
138 void ClientPrefs::SDK_OnAllLoaded()
140 playerhelpers->AddClientListener(&g_CookieManager);
143 bool ClientPrefs::QueryInterfaceDrop(SMInterface *pInterface)
145 if ((void *)pInterface == (void *)(Database->GetDriver()))
153 void ClientPrefs::NotifyInterfaceDrop(SMInterface *pInterface)
155 if (Database != NULL && (void *)pInterface == (void *)(Database->GetDriver()))
162 void ClientPrefs::SDK_OnUnload()
164 handlesys->RemoveType(g_CookieType, myself->GetIdentity());
165 handlesys->RemoveType(g_CookieIterator, myself->GetIdentity());
167 g_CookieManager.Unload();
169 if (Database != NULL)
174 forwards->ReleaseForward(g_CookieManager.cookieDataLoadedForward);
176 g_CookieManager.clientMenu->Destroy();
180 plsys->RemovePluginsListener(&g_CookieManager);
181 playerhelpers->RemoveClientListener(&g_CookieManager);
183 queryMutex->DestroyThis();
184 cookieMutex->DestroyThis();
187 void ClientPrefs::DatabaseConnect()
192 Database = Driver->Connect(DBInfo, true, error, sizeof(error));
194 if (Database == NULL &&
195 DBInfo != storage_local &&
196 storage_local != NULL)
198 DBInfo = storage_local;
199 Database = Driver->Connect(DBInfo, true, error, sizeof(error));
202 if (Database == NULL)
204 g_pSM->LogError(myself, error);
205 databaseLoading = false;
210 const char *identifier = Driver->GetIdentifier();
212 if (strcmp(identifier, "sqlite") == 0)
214 g_DriverType = Driver_SQLite;
216 if (!Database->DoSimpleQuery(
217 "CREATE TABLE IF NOT EXISTS sm_cookies \
219 id INTEGER PRIMARY KEY AUTOINCREMENT, \
220 name varchar(30) NOT NULL UNIQUE, \
221 description varchar(255), \
225 g_pSM->LogMessage(myself, "Failed to CreateTable sm_cookies: %s", Database->GetError());
229 if (!Database->DoSimpleQuery(
230 "CREATE TABLE IF NOT EXISTS sm_cookie_cache \
232 player varchar(65) NOT NULL, \
233 cookie_id int(10) NOT NULL, \
234 value varchar(100), \
236 PRIMARY KEY (player, cookie_id) \
239 g_pSM->LogMessage(myself, "Failed to CreateTable sm_cookie_cache: %s", Database->GetError());
243 else if (strcmp(identifier, "mysql") == 0)
245 g_DriverType = Driver_MySQL;
247 if (!Database->DoSimpleQuery(
248 "CREATE TABLE IF NOT EXISTS sm_cookies \
250 id INTEGER unsigned NOT NULL auto_increment, \
251 name varchar(30) NOT NULL UNIQUE, \
252 description varchar(255), \
257 g_pSM->LogMessage(myself, "Failed to CreateTable sm_cookies: %s", Database->GetError());
261 if (!Database->DoSimpleQuery(
262 "CREATE TABLE IF NOT EXISTS sm_cookie_cache \
264 player varchar(65) NOT NULL, \
265 cookie_id int(10) NOT NULL, \
266 value varchar(100), \
267 timestamp int NOT NULL, \
268 PRIMARY KEY (player, cookie_id) \
271 g_pSM->LogMessage(myself, "Failed to CreateTable sm_cookie_cache: %s", Database->GetError());
277 g_pSM->LogError(myself, "Unsupported driver \"%s\"", identifier);
281 databaseLoading = false;
290 databaseLoading = false;
294 bool ClientPrefs::AddQueryToQueue( TQueryOp *query )
298 if (Database == NULL && databaseLoading)
300 cachedQueries.push_back(query);
301 queryMutex->Unlock();
305 queryMutex->Unlock();
309 query->SetDatabase(Database);
310 dbi->AddToThreadQueue(query, PrioQueue_Normal);
316 /* If Database is NULL and we're not in the loading phase it must have failed - Can't do much */
320 void ClientPrefs::ProcessQueryCache()
322 SourceHook::List<TQueryOp *>::iterator iter;
326 iter = cachedQueries.begin();
328 while (iter != cachedQueries.end())
330 TQueryOp *op = *iter;
332 if (Database != NULL)
334 op->SetDatabase(Database);
335 dbi->AddToThreadQueue(op, PrioQueue_Normal);
345 cachedQueries.clear();
347 queryMutex->Unlock();
350 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
354 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
357 if (len >= maxlength)
359 buffer[maxlength - 1] = '\0';
360 return (maxlength - 1);
368 bool Translate(char *buffer,
371 unsigned int numparams,
377 const char *fail_phrase;
378 void *params[MAX_TRANSLATE_PARAMS];
380 if (numparams > MAX_TRANSLATE_PARAMS)
386 va_start(ap, pOutLength);
387 for (i = 0; i < numparams; i++)
389 params[i] = va_arg(ap, void *);
393 if (!g_ClientPrefs.phrases->FormatString(buffer,
401 if (fail_phrase != NULL)
403 g_pSM->LogError(myself, "[SM] Could not find core phrase: %s", fail_phrase);
407 g_pSM->LogError(myself, "[SM] Unknown fatal error while translating a core phrase.");