extensions/clientprefs/extension.cpp
author Matt Woodrow <pred@alliedmods.net>
Sun Jul 06 03:30:06 2008 +0000 (2008-07-06)
changeset 2165 ca3757e676f6
parent 2135 2c56e1550aaa
child 2207 3a97a56ded62
permissions -rw-r--r--
Fixed requiring dependencies at the wrong time.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Client Preferences Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "extension.h"
    33 
    34 /**
    35  * @file extension.cpp
    36  * @brief Implement extension code here.
    37  */
    38 
    39 ClientPrefs g_ClientPrefs;		/**< Global singleton for extension's main interface */
    40 
    41 SMEXT_LINK(&g_ClientPrefs);
    42 
    43 HandleType_t g_CookieType = 0;
    44 CookieTypeHandler g_CookieTypeHandler;
    45 
    46 HandleType_t g_CookieIterator = 0;
    47 CookieIteratorHandler g_CookieIteratorHandler;
    48 
    49 
    50 int driver = 0;
    51 
    52 bool ClientPrefs::SDK_OnLoad(char *error, size_t maxlength, bool late)
    53 {	
    54 	const DatabaseInfo *DBInfo = dbi->FindDatabaseConf("clientprefs");
    55 
    56 	if (DBInfo == NULL)
    57 	{
    58 		DBInfo = dbi->FindDatabaseConf("default");
    59 
    60 		if (DBInfo == NULL)
    61 		{
    62 			snprintf(error, maxlength, "Could not find \"clientprefs\" or \"default\" database configs");
    63 			return false;
    64 		}
    65 
    66 	}
    67 
    68 	if (DBInfo->driver[0] != '\0')
    69 	{
    70 		Driver = dbi->FindOrLoadDriver(DBInfo->driver);
    71 	}
    72 	else
    73 	{
    74 		Driver = dbi->GetDefaultDriver();
    75 	}
    76 
    77 	if (Driver == NULL)
    78 	{
    79 		snprintf(error, maxlength, "Could not load DB Driver \"%s\"", DBInfo->driver);
    80 		return false;
    81 	}
    82 
    83 	Database = Driver->Connect(DBInfo, true, error, maxlength);
    84 
    85 	if (Database == NULL)
    86 	{
    87 		return false;
    88 	}
    89 
    90 	dbi->AddDependency(myself, Driver);
    91 
    92 	const char *identifier = Driver->GetIdentifier();
    93 
    94 	if (strcmp(identifier, "sqlite") == 0)
    95 	{
    96 		driver = DRIVER_SQLITE;
    97 
    98 		TQueryOp *op = new TQueryOp(
    99 						Database, 
   100 						"CREATE TABLE IF NOT EXISTS sm_cookies  \
   101 						( \
   102 							id INTEGER PRIMARY KEY AUTOINCREMENT, \
   103 							name varchar(30) NOT NULL UNIQUE, \
   104 							description varchar(255), \
   105 							access INTEGER \
   106 						)", 
   107 						Query_CreateTable, 
   108 						0);
   109 
   110 		dbi->AddToThreadQueue(op, PrioQueue_Normal);
   111 
   112 		op = new TQueryOp(
   113 						Database, 
   114 						"CREATE TABLE IF NOT EXISTS sm_cookie_cache \
   115 						( \
   116 							player varchar(65) NOT NULL, \
   117 							cookie_id int(10) NOT NULL, \
   118 							value varchar(100), \
   119 							timestamp int, \
   120 							PRIMARY KEY (player, cookie_id) \
   121 						)", 
   122 						Query_CreateTable, 
   123 						0);
   124 
   125 		dbi->AddToThreadQueue(op, PrioQueue_Normal);
   126 	}
   127 	else if (strcmp(identifier, "mysql") == 0)
   128 	{
   129 		driver = DRIVER_MYSQL;
   130 
   131 		TQueryOp *op = new TQueryOp(
   132 						Database, 
   133 						"CREATE TABLE IF NOT EXISTS sm_cookies \
   134 						( \
   135 							id INTEGER unsigned NOT NULL auto_increment, \
   136 							name varchar(30) NOT NULL UNIQUE, \
   137 							description varchar(255), \
   138 							access INTEGER, \
   139 							PRIMARY KEY (id) \
   140 						)", 
   141 						Query_CreateTable, 
   142 						0);
   143 
   144 		dbi->AddToThreadQueue(op, PrioQueue_Normal);
   145 
   146 		op = new TQueryOp(
   147 						Database, 
   148 						"CREATE TABLE IF NOT EXISTS sm_cookie_cache \
   149 						( \
   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) \
   155 						)", 
   156 						Query_CreateTable, 
   157 						0);
   158 
   159 		dbi->AddToThreadQueue(op, PrioQueue_Normal);
   160 	}
   161 	else
   162 	{
   163 		snprintf(error, maxlength, "Unsupported driver \"%s\"", identifier);
   164 		return false;
   165 	}
   166 
   167 
   168 
   169 	sharesys->AddNatives(myself, g_ClientPrefNatives);
   170 	sharesys->RegisterLibrary(myself, "clientprefs");
   171 	g_CookieManager.cookieDataLoadedForward = forwards->CreateForward("OnClientCookiesCached", ET_Ignore, 1, NULL, Param_Cell);
   172 
   173 	g_CookieType = handlesys->CreateType("Cookie", 
   174 		&g_CookieTypeHandler, 
   175 		0, 
   176 		NULL, 
   177 		NULL, 
   178 		myself->GetIdentity(), 
   179 		NULL);
   180 
   181 	g_CookieIterator = handlesys->CreateType("CookieIterator", 
   182 		&g_CookieIteratorHandler, 
   183 		0, 
   184 		NULL, 
   185 		NULL, 
   186 		myself->GetIdentity(), 
   187 		NULL);
   188 
   189 	IMenuStyle *style = menus->GetDefaultStyle();
   190 	g_CookieManager.clientMenu = style->CreateMenu(&g_Handler, NULL);
   191 	g_CookieManager.clientMenu->SetDefaultTitle("Client Settings:");
   192 
   193 	plsys->AddPluginsListener(&g_CookieManager);
   194 
   195 	phrases = translator->CreatePhraseCollection();
   196 	phrases->AddPhraseFile("clientprefs.phrases");
   197 	phrases->AddPhraseFile("common.phrases");
   198 
   199 	return true;
   200 }
   201 
   202 void ClientPrefs::SDK_OnAllLoaded()
   203 {
   204 	playerhelpers->AddClientListener(&g_CookieManager);
   205 }
   206 
   207 bool ClientPrefs::QueryInterfaceDrop(SMInterface *pInterface)
   208 {
   209 	if ((void *)pInterface == (void *)(Database->GetDriver()))
   210 	{
   211 		return false;
   212 	}
   213 
   214 	return true;
   215 }
   216 
   217 void ClientPrefs::NotifyInterfaceDrop(SMInterface *pInterface)
   218 {
   219 	if (Database != NULL && (void *)pInterface == (void *)(Database->GetDriver()))
   220 	{
   221 		Database->Close();
   222 		Database = NULL;
   223 	}
   224 }
   225 
   226 void ClientPrefs::SDK_OnUnload()
   227 {
   228 	handlesys->RemoveType(g_CookieType, myself->GetIdentity());
   229 	handlesys->RemoveType(g_CookieIterator, myself->GetIdentity());
   230 
   231 	g_CookieManager.Unload();
   232 
   233 	if (Database != NULL)
   234 	{
   235 		Database->Close();
   236 	}
   237 
   238 	forwards->ReleaseForward(g_CookieManager.cookieDataLoadedForward);
   239 
   240 	g_CookieManager.clientMenu->Destroy();
   241 
   242 	phrases->Destroy();
   243 
   244 	plsys->RemovePluginsListener(&g_CookieManager);
   245 	playerhelpers->RemoveClientListener(&g_CookieManager);
   246 }
   247 
   248 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   249 {
   250 	va_list ap;
   251 	va_start(ap, fmt);
   252 	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   253 	va_end(ap);
   254 
   255 	if (len >= maxlength)
   256 	{
   257 		buffer[maxlength - 1] = '\0';
   258 		return (maxlength - 1);
   259 	}
   260 	else
   261 	{
   262 		return len;
   263 	}
   264 }
   265 
   266 bool Translate(char *buffer, 
   267 				   size_t maxlength,
   268 				   const char *format,
   269 				   unsigned int numparams,
   270 				   size_t *pOutLength,
   271 				   ...)
   272 {
   273 	va_list ap;
   274 	unsigned int i;
   275 	const char *fail_phrase;
   276 	void *params[MAX_TRANSLATE_PARAMS];
   277 
   278 	if (numparams > MAX_TRANSLATE_PARAMS)
   279 	{
   280 		assert(false);
   281 		return false;
   282 	}
   283 
   284 	va_start(ap, pOutLength);
   285 	for (i = 0; i < numparams; i++)
   286 	{
   287 		params[i] = va_arg(ap, void *);
   288 	}
   289 	va_end(ap);
   290 
   291 	if (!g_ClientPrefs.phrases->FormatString(buffer,
   292 		maxlength, 
   293 		format, 
   294 		params,
   295 		numparams,
   296 		pOutLength,
   297 		&fail_phrase))
   298 	{
   299 		if (fail_phrase != NULL)
   300 		{
   301 			g_pSM->LogError(myself, "[SM] Could not find core phrase: %s", fail_phrase);
   302 		}
   303 		else
   304 		{
   305 			g_pSM->LogError(myself, "[SM] Unknown fatal error while translating a core phrase.");
   306 		}
   307 
   308 		return false;
   309 	}
   310 
   311 	return true;
   312 }
   313 
   314 
   315