1.1 --- a/extensions/clientprefs/extension.cpp Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,315 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod Client Preferences Extension
1.8 - * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include "extension.h"
1.36 -
1.37 -/**
1.38 - * @file extension.cpp
1.39 - * @brief Implement extension code here.
1.40 - */
1.41 -
1.42 -ClientPrefs g_ClientPrefs; /**< Global singleton for extension's main interface */
1.43 -
1.44 -SMEXT_LINK(&g_ClientPrefs);
1.45 -
1.46 -HandleType_t g_CookieType = 0;
1.47 -CookieTypeHandler g_CookieTypeHandler;
1.48 -
1.49 -HandleType_t g_CookieIterator = 0;
1.50 -CookieIteratorHandler g_CookieIteratorHandler;
1.51 -
1.52 -
1.53 -int driver = 0;
1.54 -
1.55 -bool ClientPrefs::SDK_OnLoad(char *error, size_t maxlength, bool late)
1.56 -{
1.57 - const DatabaseInfo *DBInfo = dbi->FindDatabaseConf("clientprefs");
1.58 -
1.59 - if (DBInfo == NULL)
1.60 - {
1.61 - DBInfo = dbi->FindDatabaseConf("default");
1.62 -
1.63 - if (DBInfo == NULL)
1.64 - {
1.65 - snprintf(error, maxlength, "Could not find \"clientprefs\" or \"default\" database configs");
1.66 - return false;
1.67 - }
1.68 -
1.69 - }
1.70 -
1.71 - if (DBInfo->driver[0] != '\0')
1.72 - {
1.73 - Driver = dbi->FindOrLoadDriver(DBInfo->driver);
1.74 - }
1.75 - else
1.76 - {
1.77 - Driver = dbi->GetDefaultDriver();
1.78 - }
1.79 -
1.80 - if (Driver == NULL)
1.81 - {
1.82 - snprintf(error, maxlength, "Could not load DB Driver \"%s\"", DBInfo->driver);
1.83 - return false;
1.84 - }
1.85 -
1.86 - Database = Driver->Connect(DBInfo, true, error, maxlength);
1.87 -
1.88 - if (Database == NULL)
1.89 - {
1.90 - return false;
1.91 - }
1.92 -
1.93 - dbi->AddDependency(myself, Driver);
1.94 -
1.95 - const char *identifier = Driver->GetIdentifier();
1.96 -
1.97 - if (strcmp(identifier, "sqlite") == 0)
1.98 - {
1.99 - driver = DRIVER_SQLITE;
1.100 -
1.101 - TQueryOp *op = new TQueryOp(
1.102 - Database,
1.103 - "CREATE TABLE IF NOT EXISTS sm_cookies \
1.104 - ( \
1.105 - id INTEGER PRIMARY KEY AUTOINCREMENT, \
1.106 - name varchar(30) NOT NULL UNIQUE, \
1.107 - description varchar(255), \
1.108 - access INTEGER \
1.109 - )",
1.110 - Query_CreateTable,
1.111 - 0);
1.112 -
1.113 - dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.114 -
1.115 - op = new TQueryOp(
1.116 - Database,
1.117 - "CREATE TABLE IF NOT EXISTS sm_cookie_cache \
1.118 - ( \
1.119 - player varchar(65) NOT NULL, \
1.120 - cookie_id int(10) NOT NULL, \
1.121 - value varchar(100), \
1.122 - timestamp int, \
1.123 - PRIMARY KEY (player, cookie_id) \
1.124 - )",
1.125 - Query_CreateTable,
1.126 - 0);
1.127 -
1.128 - dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.129 - }
1.130 - else if (strcmp(identifier, "mysql") == 0)
1.131 - {
1.132 - driver = DRIVER_MYSQL;
1.133 -
1.134 - TQueryOp *op = new TQueryOp(
1.135 - Database,
1.136 - "CREATE TABLE IF NOT EXISTS sm_cookies \
1.137 - ( \
1.138 - id INTEGER unsigned NOT NULL auto_increment, \
1.139 - name varchar(30) NOT NULL UNIQUE, \
1.140 - description varchar(255), \
1.141 - access INTEGER, \
1.142 - PRIMARY KEY (id) \
1.143 - )",
1.144 - Query_CreateTable,
1.145 - 0);
1.146 -
1.147 - dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.148 -
1.149 - op = new TQueryOp(
1.150 - Database,
1.151 - "CREATE TABLE IF NOT EXISTS sm_cookie_cache \
1.152 - ( \
1.153 - player varchar(65) NOT NULL, \
1.154 - cookie_id int(10) NOT NULL, \
1.155 - value varchar(100), \
1.156 - timestamp int NOT NULL, \
1.157 - PRIMARY KEY (player, cookie_id) \
1.158 - )",
1.159 - Query_CreateTable,
1.160 - 0);
1.161 -
1.162 - dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.163 - }
1.164 - else
1.165 - {
1.166 - snprintf(error, maxlength, "Unsupported driver \"%s\"", identifier);
1.167 - return false;
1.168 - }
1.169 -
1.170 -
1.171 -
1.172 - sharesys->AddNatives(myself, g_ClientPrefNatives);
1.173 - sharesys->RegisterLibrary(myself, "clientprefs");
1.174 - g_CookieManager.cookieDataLoadedForward = forwards->CreateForward("OnClientCookiesCached", ET_Ignore, 1, NULL, Param_Cell);
1.175 -
1.176 - g_CookieType = handlesys->CreateType("Cookie",
1.177 - &g_CookieTypeHandler,
1.178 - 0,
1.179 - NULL,
1.180 - NULL,
1.181 - myself->GetIdentity(),
1.182 - NULL);
1.183 -
1.184 - g_CookieIterator = handlesys->CreateType("CookieIterator",
1.185 - &g_CookieIteratorHandler,
1.186 - 0,
1.187 - NULL,
1.188 - NULL,
1.189 - myself->GetIdentity(),
1.190 - NULL);
1.191 -
1.192 - IMenuStyle *style = menus->GetDefaultStyle();
1.193 - g_CookieManager.clientMenu = style->CreateMenu(&g_Handler, NULL);
1.194 - g_CookieManager.clientMenu->SetDefaultTitle("Client Settings:");
1.195 -
1.196 - plsys->AddPluginsListener(&g_CookieManager);
1.197 -
1.198 - phrases = translator->CreatePhraseCollection();
1.199 - phrases->AddPhraseFile("clientprefs.phrases");
1.200 - phrases->AddPhraseFile("common.phrases");
1.201 -
1.202 - return true;
1.203 -}
1.204 -
1.205 -void ClientPrefs::SDK_OnAllLoaded()
1.206 -{
1.207 - playerhelpers->AddClientListener(&g_CookieManager);
1.208 -}
1.209 -
1.210 -bool ClientPrefs::QueryInterfaceDrop(SMInterface *pInterface)
1.211 -{
1.212 - if ((void *)pInterface == (void *)(Database->GetDriver()))
1.213 - {
1.214 - return false;
1.215 - }
1.216 -
1.217 - return true;
1.218 -}
1.219 -
1.220 -void ClientPrefs::NotifyInterfaceDrop(SMInterface *pInterface)
1.221 -{
1.222 - if (Database != NULL && (void *)pInterface == (void *)(Database->GetDriver()))
1.223 - {
1.224 - Database->Close();
1.225 - Database = NULL;
1.226 - }
1.227 -}
1.228 -
1.229 -void ClientPrefs::SDK_OnUnload()
1.230 -{
1.231 - handlesys->RemoveType(g_CookieType, myself->GetIdentity());
1.232 - handlesys->RemoveType(g_CookieIterator, myself->GetIdentity());
1.233 -
1.234 - g_CookieManager.Unload();
1.235 -
1.236 - if (Database != NULL)
1.237 - {
1.238 - Database->Close();
1.239 - }
1.240 -
1.241 - forwards->ReleaseForward(g_CookieManager.cookieDataLoadedForward);
1.242 -
1.243 - g_CookieManager.clientMenu->Destroy();
1.244 -
1.245 - phrases->Destroy();
1.246 -
1.247 - plsys->RemovePluginsListener(&g_CookieManager);
1.248 - playerhelpers->RemoveClientListener(&g_CookieManager);
1.249 -}
1.250 -
1.251 -size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
1.252 -{
1.253 - va_list ap;
1.254 - va_start(ap, fmt);
1.255 - size_t len = vsnprintf(buffer, maxlength, fmt, ap);
1.256 - va_end(ap);
1.257 -
1.258 - if (len >= maxlength)
1.259 - {
1.260 - buffer[maxlength - 1] = '\0';
1.261 - return (maxlength - 1);
1.262 - }
1.263 - else
1.264 - {
1.265 - return len;
1.266 - }
1.267 -}
1.268 -
1.269 -bool Translate(char *buffer,
1.270 - size_t maxlength,
1.271 - const char *format,
1.272 - unsigned int numparams,
1.273 - size_t *pOutLength,
1.274 - ...)
1.275 -{
1.276 - va_list ap;
1.277 - unsigned int i;
1.278 - const char *fail_phrase;
1.279 - void *params[MAX_TRANSLATE_PARAMS];
1.280 -
1.281 - if (numparams > MAX_TRANSLATE_PARAMS)
1.282 - {
1.283 - assert(false);
1.284 - return false;
1.285 - }
1.286 -
1.287 - va_start(ap, pOutLength);
1.288 - for (i = 0; i < numparams; i++)
1.289 - {
1.290 - params[i] = va_arg(ap, void *);
1.291 - }
1.292 - va_end(ap);
1.293 -
1.294 - if (!g_ClientPrefs.phrases->FormatString(buffer,
1.295 - maxlength,
1.296 - format,
1.297 - params,
1.298 - numparams,
1.299 - pOutLength,
1.300 - &fail_phrase))
1.301 - {
1.302 - if (fail_phrase != NULL)
1.303 - {
1.304 - g_pSM->LogError(myself, "[SM] Could not find core phrase: %s", fail_phrase);
1.305 - }
1.306 - else
1.307 - {
1.308 - g_pSM->LogError(myself, "[SM] Unknown fatal error while translating a core phrase.");
1.309 - }
1.310 -
1.311 - return false;
1.312 - }
1.313 -
1.314 - return true;
1.315 -}
1.316 -
1.317 -
1.318 -