1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/clientprefs/extension.cpp Thu Apr 17 04:58:09 2008 +0000
1.3 @@ -0,0 +1,173 @@
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 +
1.50 +int driver = 0;
1.51 +
1.52 +bool ClientPrefs::SDK_OnLoad(char *error, size_t maxlength, bool late)
1.53 +{
1.54 + const DatabaseInfo *DBInfo = dbi->FindDatabaseConf("clientprefs");
1.55 +
1.56 + if (DBInfo == NULL)
1.57 + {
1.58 + DBInfo = dbi->FindDatabaseConf("default");
1.59 +
1.60 + if (DBInfo == NULL)
1.61 + {
1.62 + snprintf(error, maxlength, "Could not find \"clientprefs\" or \"default\" database configs");
1.63 + return false;
1.64 + }
1.65 +
1.66 + }
1.67 +
1.68 + if (DBInfo->driver[0] != '\0')
1.69 + {
1.70 + Driver = dbi->FindOrLoadDriver(DBInfo->driver);
1.71 + }
1.72 + else
1.73 + {
1.74 + Driver = dbi->GetDefaultDriver();
1.75 + }
1.76 +
1.77 + if (Driver == NULL)
1.78 + {
1.79 + snprintf(error, maxlength, "Could not load DB Driver \"%s\"", DBInfo->driver);
1.80 + return false;
1.81 + }
1.82 +
1.83 + Database = Driver->Connect(DBInfo, true, error, maxlength);
1.84 +
1.85 + if (Database == NULL)
1.86 + {
1.87 + return false;
1.88 + }
1.89 +
1.90 + const char *identifier = Driver->GetIdentifier();
1.91 +
1.92 + if (strcmp(identifier, "sqlite") == 0)
1.93 + {
1.94 + driver = DRIVER_SQLITE;
1.95 + }
1.96 + else if (strcmp(identifier, "mysql") == 0)
1.97 + {
1.98 + driver = DRIVER_MYSQL;
1.99 + }
1.100 + else
1.101 + {
1.102 + snprintf(error, maxlength, "Unsupported driver \"%s\"", identifier);
1.103 + return false;
1.104 + }
1.105 +
1.106 + if (driver == DRIVER_SQLITE)
1.107 + {
1.108 + Query_InsertCookie = Database->PrepareQuery("INSERT OR IGNORE INTO sm_cookies(name, description) VALUES(?, ?)", error, maxlength);
1.109 + }
1.110 + else
1.111 + {
1.112 + Query_InsertCookie = Database->PrepareQuery("INSERT IGNORE INTO sm_cookies(name, description) VALUES(?, ?)", error, maxlength);
1.113 + }
1.114 +
1.115 + if (Query_InsertCookie == NULL)
1.116 + {
1.117 + return false;
1.118 + }
1.119 +
1.120 + Query_SelectData = Database->PrepareQuery("SELECT sm_cookies.name, sm_cookie_cache.value, sm_cookies.description FROM sm_cookies JOIN sm_cookie_cache ON sm_cookies.id = sm_cookie_cache.cookie_id WHERE player = ?", error, maxlength);
1.121 +
1.122 + if (Query_SelectData == NULL)
1.123 + {
1.124 + return false;
1.125 + }
1.126 +
1.127 + if (driver == DRIVER_SQLITE)
1.128 + {
1.129 + Query_InsertData = Database->PrepareQuery("INSERT OR REPLACE INTO sm_cookie_cache(player,cookie_id, value, timestamp) VALUES(?, ?, ?, ?)", error, maxlength);
1.130 + }
1.131 + else
1.132 + {
1.133 + Query_InsertData = Database->PrepareQuery("INSERT INTO sm_cookie_cache(player,cookie_id, value, timestamp) VALUES(?, ?, ?, ?) ON DUPLICATE KEY UPDATE value = ?, timestamp = ?", error, maxlength);
1.134 + }
1.135 +
1.136 + if (Query_InsertData == NULL)
1.137 + {
1.138 + return false;
1.139 + }
1.140 +
1.141 + sharesys->AddNatives(myself, g_ClientPrefNatives);
1.142 + sharesys->RegisterLibrary(myself, "clientprefs");
1.143 + g_CookieManager.cookiesLoadedForward = forwards->CreateForward("OnClientCookiesLoaded", ET_Ignore, 1, NULL, Param_Cell);
1.144 +
1.145 + g_CookieType = handlesys->CreateType("Cookie",
1.146 + &g_CookieTypeHandler,
1.147 + 0,
1.148 + NULL,
1.149 + NULL,
1.150 + myself->GetIdentity(),
1.151 + NULL);
1.152 +
1.153 + return true;
1.154 +}
1.155 +
1.156 +void ClientPrefs::SDK_OnAllLoaded()
1.157 +{
1.158 + playerhelpers->AddClientListener(&g_CookieManager);
1.159 +}
1.160 +
1.161 +void ClientPrefs::SDK_OnUnload()
1.162 +{
1.163 + handlesys->RemoveType(g_CookieType, myself->GetIdentity());
1.164 +
1.165 + g_CookieManager.Unload();
1.166 +
1.167 + Query_InsertCookie->Destroy();
1.168 + Query_InsertData->Destroy();
1.169 + Query_SelectData->Destroy();
1.170 +
1.171 + Database->Close();
1.172 +
1.173 + forwards->ReleaseForward(g_CookieManager.cookiesLoadedForward);
1.174 +}
1.175 +
1.176 +