Triggering build.
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>.
36 CookieManager g_CookieManager;
38 CookieManager::CookieManager()
40 for (int i=0; i<=MAXCLIENTS; i++)
43 statsLoaded[i] = false;
44 statsPending[i] = false;
47 cookieDataLoadedForward = NULL;
50 CookieManager::~CookieManager(){}
52 void CookieManager::Unload()
54 /* If clients are connected we should try save their data */
55 for (int i = playerhelpers->GetMaxClients()+1; --i > 0;)
59 OnClientDisconnecting(i);
63 /* Find all cookies and delete them */
66 for (SourceHook::List<Cookie *>::iterator _iter = cookieList.begin(); _iter != cookieList.end(); _iter++)
68 current = (Cookie *)*_iter;
75 g_ClientPrefs.cookieMutex->Lock();
76 if (current->usedInQuery)
78 current->shouldDelete = true;
79 g_ClientPrefs.cookieMutex->Unlock();
83 g_ClientPrefs.cookieMutex->Unlock();
91 Cookie *CookieManager::FindCookie(const char *name)
93 Cookie **pCookie = cookieTrie.retrieve(name);
103 Cookie *CookieManager::CreateCookie(const char *name, const char *description, CookieAccess access)
105 Cookie *pCookie = FindCookie(name);
107 /* Check if cookie already exists */
110 /* Update data fields to the provided values */
111 UTIL_strncpy(pCookie->description, description, MAX_DESC_LENGTH);
112 pCookie->access = access;
117 /* First time cookie - Create from scratch */
118 pCookie = new Cookie(name, description, access);
119 pCookie->usedInQuery++;
121 /* Attempt to insert cookie into the db and get its ID num */
122 TQueryOp *op = new TQueryOp(Query_InsertCookie, pCookie);
123 op->m_params.cookie = pCookie;
125 cookieTrie.insert(name, pCookie);
126 cookieList.push_back(pCookie);
128 g_ClientPrefs.AddQueryToQueue(op);
133 bool CookieManager::GetCookieValue(Cookie *pCookie, int client, char **value)
135 CookieData *data = pCookie->data[client];
137 /* Check if a value has been set before */
140 data = new CookieData("");
141 data->parent = pCookie;
142 clientData[client].push_back(data);
143 pCookie->data[client] = data;
144 data->changed = false;
148 *value = &data->value[0];
153 bool CookieManager::SetCookieValue(Cookie *pCookie, int client, const char *value)
155 CookieData *data = pCookie->data[client];
159 data = new CookieData(value);
160 data->parent = pCookie;
161 clientData[client].push_back(data);
162 pCookie->data[client] = data;
166 UTIL_strncpy(data->value, value, MAX_VALUE_LENGTH);
169 data->changed = true;
170 data->timestamp = time(NULL);
175 void CookieManager::OnClientAuthorized(int client, const char *authstring)
177 IGamePlayer *player = playerhelpers->GetGamePlayer(client);
184 connected[client] = true;
185 statsPending[client] = true;
187 g_ClientPrefs.AttemptReconnection();
189 TQueryOp *op = new TQueryOp(Query_SelectData, player->GetSerial());
190 UTIL_strncpy(op->m_params.steamId, authstring, MAX_NAME_LENGTH);
192 g_ClientPrefs.AddQueryToQueue(op);
195 void CookieManager::OnClientDisconnecting(int client)
197 connected[client] = false;
198 statsLoaded[client] = false;
199 statsPending[client] = false;
201 CookieData *current = NULL;
203 g_ClientPrefs.AttemptReconnection();
205 /* Save this cookie to the database */
206 IGamePlayer *player = playerhelpers->GetGamePlayer(client);
207 const char *pAuth = NULL;
212 pAuth = player->GetAuthString();
213 g_ClientPrefs.ClearQueryCache(player->GetSerial());
216 for (SourceHook::List<CookieData *>::iterator _iter = clientData[client].begin();\
217 _iter != clientData[client].end(); _iter++)
219 current = (CookieData *)*_iter;
220 dbId = current->parent->dbid;
222 if (player == NULL || pAuth == NULL || !current->changed || dbId == -1)
224 current->parent->data[client] = NULL;
229 TQueryOp *op = new TQueryOp(Query_InsertData, client);
231 UTIL_strncpy(op->m_params.steamId, pAuth, MAX_NAME_LENGTH);
232 op->m_params.cookieId = dbId;
233 op->m_params.data = current;
235 g_ClientPrefs.AddQueryToQueue(op);
237 current->parent->data[client] = NULL;
240 clientData[client].clear();
243 void CookieManager::ClientConnectCallback(int serial, IQuery *data)
247 /* Check validity of client */
248 if ((client = playerhelpers->GetClientFromSerial(serial)) == 0)
252 statsPending[client] = false;
255 /* Check validity of results */
256 if (data == NULL || (results = data->GetResultSet()) == NULL)
263 unsigned int timestamp;
266 while (results->MoreRows() && ((row = results->FetchRow()) != NULL))
268 const char *name = "";
269 row->GetString(0, &name, NULL);
271 const char *value = "";
272 row->GetString(1, &value, NULL);
274 pData = new CookieData(value);
275 pData->changed = false;
277 pData->timestamp = (row->GetInt(4, (int *)×tamp) == DBVal_Data) ? timestamp : 0;
279 Cookie *parent = FindCookie(name);
283 const char *desc = "";
284 row->GetString(2, &desc, NULL);
286 access = CookieAccess_Public;
287 row->GetInt(3, (int *)&access);
289 parent = CreateCookie(name, desc, access);
292 pData->parent = parent;
293 parent->data[client] = pData;
294 clientData[client].push_back(pData);
297 statsLoaded[client] = true;
299 cookieDataLoadedForward->PushCell(client);
300 cookieDataLoadedForward->Execute(NULL);
303 void CookieManager::InsertCookieCallback(Cookie *pCookie, int dbId)
307 pCookie->dbid = dbId;
311 TQueryOp *op = new TQueryOp(Query_SelectId, pCookie);
312 /* Put the cookie name into the steamId field to save space - Make sure we remember that it's there */
313 UTIL_strncpy(op->m_params.steamId, pCookie->name, MAX_NAME_LENGTH);
314 g_ClientPrefs.AddQueryToQueue(op);
317 void CookieManager::SelectIdCallback(Cookie *pCookie, IQuery *data)
321 if (data == NULL || (results = data->GetResultSet()) == NULL)
326 IResultRow *row = results->FetchRow();
333 row->GetInt(0, &pCookie->dbid);
336 bool CookieManager::AreClientCookiesCached(int client)
338 return statsLoaded[client];
341 bool CookieManager::AreClientCookiesPening(int client)
343 return statsPending[client];
346 void CookieManager::OnPluginDestroyed(IPlugin *plugin)
348 SourceHook::List<char *> *pList;
350 if (plugin->GetProperty("SettingsMenuItems", (void **)&pList, true))
358 for (SourceHook::List<char *>::iterator p_iter = pList->begin(); p_iter != pList->end(); p_iter++)
360 name = (char *)*p_iter;
361 itemcount = clientMenu->GetItemCount();
362 //remove from this plugins list
363 for (unsigned int i=0; i < itemcount; i++)
365 info = clientMenu->GetItemInfo(i, &draw);
372 if (strcmp(draw.display, name) == 0)
374 data = (AutoMenuData *)strtoul(info, NULL, 16);
376 if (data->handler->forward != NULL)
378 forwards->ReleaseForward(data->handler->forward);
380 delete data->handler;
383 clientMenu->RemoveItem(i);
395 bool CookieManager::GetCookieTime(Cookie *pCookie, int client, time_t *value)
397 CookieData *data = pCookie->data[client];
399 /* Check if a value has been set before */
405 *value = data->timestamp;