Fixed requiring dependencies at the wrong time.
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;
52 bool ClientPrefs::SDK_OnLoad(char *error, size_t maxlength, bool late)
54 const DatabaseInfo *DBInfo = dbi->FindDatabaseConf("clientprefs");
58 DBInfo = dbi->FindDatabaseConf("default");
62 snprintf(error, maxlength, "Could not find \"clientprefs\" or \"default\" database configs");
68 if (DBInfo->driver[0] != '\0')
70 Driver = dbi->FindOrLoadDriver(DBInfo->driver);
74 Driver = dbi->GetDefaultDriver();
79 snprintf(error, maxlength, "Could not load DB Driver \"%s\"", DBInfo->driver);
83 Database = Driver->Connect(DBInfo, true, error, maxlength);
90 dbi->AddDependency(myself, Driver);
92 const char *identifier = Driver->GetIdentifier();
94 if (strcmp(identifier, "sqlite") == 0)
96 driver = DRIVER_SQLITE;
98 TQueryOp *op = new TQueryOp(
100 "CREATE TABLE IF NOT EXISTS sm_cookies \
102 id INTEGER PRIMARY KEY AUTOINCREMENT, \
103 name varchar(30) NOT NULL UNIQUE, \
104 description varchar(255), \
110 dbi->AddToThreadQueue(op, PrioQueue_Normal);
114 "CREATE TABLE IF NOT EXISTS sm_cookie_cache \
116 player varchar(65) NOT NULL, \
117 cookie_id int(10) NOT NULL, \
118 value varchar(100), \
120 PRIMARY KEY (player, cookie_id) \
125 dbi->AddToThreadQueue(op, PrioQueue_Normal);
127 else if (strcmp(identifier, "mysql") == 0)
129 driver = DRIVER_MYSQL;
131 TQueryOp *op = new TQueryOp(
133 "CREATE TABLE IF NOT EXISTS sm_cookies \
135 id INTEGER unsigned NOT NULL auto_increment, \
136 name varchar(30) NOT NULL UNIQUE, \
137 description varchar(255), \
144 dbi->AddToThreadQueue(op, PrioQueue_Normal);
148 "CREATE TABLE IF NOT EXISTS sm_cookie_cache \
150 player varchar(65) NOT NULL, \
151 cookie_id int(10) NOT NULL, \
152 value varchar(100), \
153 timestamp int NOT NULL, \
154 PRIMARY KEY (player, cookie_id) \
159 dbi->AddToThreadQueue(op, PrioQueue_Normal);
163 snprintf(error, maxlength, "Unsupported driver \"%s\"", identifier);
169 sharesys->AddNatives(myself, g_ClientPrefNatives);
170 sharesys->RegisterLibrary(myself, "clientprefs");
171 g_CookieManager.cookieDataLoadedForward = forwards->CreateForward("OnClientCookiesCached", ET_Ignore, 1, NULL, Param_Cell);
173 g_CookieType = handlesys->CreateType("Cookie",
174 &g_CookieTypeHandler,
178 myself->GetIdentity(),
181 g_CookieIterator = handlesys->CreateType("CookieIterator",
182 &g_CookieIteratorHandler,
186 myself->GetIdentity(),
189 IMenuStyle *style = menus->GetDefaultStyle();
190 g_CookieManager.clientMenu = style->CreateMenu(&g_Handler, NULL);
191 g_CookieManager.clientMenu->SetDefaultTitle("Client Settings:");
193 plsys->AddPluginsListener(&g_CookieManager);
195 phrases = translator->CreatePhraseCollection();
196 phrases->AddPhraseFile("clientprefs.phrases");
197 phrases->AddPhraseFile("common.phrases");
202 void ClientPrefs::SDK_OnAllLoaded()
204 playerhelpers->AddClientListener(&g_CookieManager);
207 bool ClientPrefs::QueryInterfaceDrop(SMInterface *pInterface)
209 if ((void *)pInterface == (void *)(Database->GetDriver()))
217 void ClientPrefs::NotifyInterfaceDrop(SMInterface *pInterface)
219 if (Database != NULL && (void *)pInterface == (void *)(Database->GetDriver()))
226 void ClientPrefs::SDK_OnUnload()
228 handlesys->RemoveType(g_CookieType, myself->GetIdentity());
229 handlesys->RemoveType(g_CookieIterator, myself->GetIdentity());
231 g_CookieManager.Unload();
233 if (Database != NULL)
238 forwards->ReleaseForward(g_CookieManager.cookieDataLoadedForward);
240 g_CookieManager.clientMenu->Destroy();
244 plsys->RemovePluginsListener(&g_CookieManager);
245 playerhelpers->RemoveClientListener(&g_CookieManager);
248 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
252 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
255 if (len >= maxlength)
257 buffer[maxlength - 1] = '\0';
258 return (maxlength - 1);
266 bool Translate(char *buffer,
269 unsigned int numparams,
275 const char *fail_phrase;
276 void *params[MAX_TRANSLATE_PARAMS];
278 if (numparams > MAX_TRANSLATE_PARAMS)
284 va_start(ap, pOutLength);
285 for (i = 0; i < numparams; i++)
287 params[i] = va_arg(ap, void *);
291 if (!g_ClientPrefs.phrases->FormatString(buffer,
299 if (fail_phrase != NULL)
301 g_pSM->LogError(myself, "[SM] Could not find core phrase: %s", fail_phrase);
305 g_pSM->LogError(myself, "[SM] Unknown fatal error while translating a core phrase.");