-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcookie.cpp
More file actions
328 lines (258 loc) · 9.42 KB
/
cookie.cpp
File metadata and controls
328 lines (258 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* vim: set ts=4 sw=4 tw=99 noet:
* =============================================================================
* SourceMod Client Preferences Extension
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include "cookie.h"
#include "menus.h"
#include "query.h"
CookieManager g_CookieManager;
CookieManager::CookieManager()
{
for (int i = 0; i <= SM_MAXPLAYERS; i++) {
connected[i] = false;
statsLoaded[i] = false;
statsPending[i] = false;
}
cookieDataLoadedForward = NULL;
clientMenu = NULL;
}
CookieManager::~CookieManager() {}
void CookieManager::Unload()
{
/* If clients are connected we should try save their data */
for (int i = playerhelpers->GetMaxClients() + 1; --i > 0;) {
if (connected[i])
OnClientDisconnecting(i);
}
/* Find all cookies and delete them */
for (size_t iter = 0; iter < cookieList.length(); ++iter)
delete cookieList[iter];
cookieList.clear();
}
Cookie *CookieManager::FindCookie(const char *name)
{
Cookie *cookie;
if (!cookieFinder.retrieve(name, &cookie))
return NULL;
return cookie;
}
Cookie *CookieManager::CreateCookie(const char *name, const char *description, CookieAccess access)
{
Cookie *pCookie = FindCookie(name);
/* Check if cookie already exists */
if (pCookie != NULL) {
/* Update data fields to the provided values */
UTIL_strncpy(pCookie->description, description, MAX_DESC_LENGTH);
pCookie->access = access;
return pCookie;
}
/* First time cookie - Create from scratch */
pCookie = new Cookie(name, description, access);
/* Attempt to insert cookie into the db and get its ID num */
TQueryOp *op = new TQueryOp(Query_InsertCookie, pCookie);
op->m_params.cookie = pCookie;
cookieFinder.insert(name, pCookie);
cookieList.append(pCookie);
g_ClientPrefs.AddQueryToQueue(op);
return pCookie;
}
bool CookieManager::GetCookieValue(Cookie *pCookie, int client, char **value)
{
CookieData *data = pCookie->data[client];
/* Check if a value has been set before */
if (data == NULL) {
data = new CookieData("");
data->parent = pCookie;
clientData[client].append(data);
pCookie->data[client] = data;
data->changed = false;
data->timestamp = 0;
}
*value = &data->value[0];
return true;
}
bool CookieManager::SetCookieValue(Cookie *pCookie, int client, const char *value)
{
CookieData *data = pCookie->data[client];
if (data == NULL) {
data = new CookieData(value);
data->parent = pCookie;
clientData[client].append(data);
pCookie->data[client] = data;
} else {
UTIL_strncpy(data->value, value, MAX_VALUE_LENGTH);
}
data->changed = true;
data->timestamp = time(NULL);
return true;
}
void CookieManager::OnClientAuthorized(int client, const char *authstring)
{
IGamePlayer *player = playerhelpers->GetGamePlayer(client);
if (player == NULL || player->IsFakeClient()) {
return;
}
connected[client] = true;
statsPending[client] = true;
g_ClientPrefs.AttemptReconnection();
TQueryOp *op = new TQueryOp(Query_SelectData, player->GetSerial());
UTIL_strncpy(op->m_params.steamId, GetPlayerCompatAuthId(player), MAX_NAME_LENGTH);
g_ClientPrefs.AddQueryToQueue(op);
}
void CookieManager::OnClientDisconnecting(int client)
{
connected[client] = false;
statsLoaded[client] = false;
statsPending[client] = false;
CookieData *current = NULL;
g_ClientPrefs.AttemptReconnection();
/* Save this cookie to the database */
IGamePlayer *player = playerhelpers->GetGamePlayer(client);
const char *pAuth = NULL;
int dbId;
if (player && !player->IsFakeClient()) {
pAuth = GetPlayerCompatAuthId(player);
g_ClientPrefs.ClearQueryCache(player->GetSerial());
}
ke::Vector<CookieData *> &clientvec = clientData[client];
for (size_t iter = 0; iter < clientvec.length(); ++iter) {
current = clientvec[iter];
dbId = current->parent->dbid;
if (player == NULL || pAuth == NULL || !current->changed || dbId == -1) {
current->parent->data[client] = NULL;
delete current;
continue;
}
TQueryOp *op = new TQueryOp(Query_InsertData, client);
UTIL_strncpy(op->m_params.steamId, pAuth, MAX_NAME_LENGTH);
op->m_params.cookieId = dbId;
op->m_params.data = current;
g_ClientPrefs.AddQueryToQueue(op, PrioQueue_High);
current->parent->data[client] = NULL;
}
clientvec.clear();
}
void CookieManager::ClientConnectCallback(int serial, const std::vector<std::tuple<Cookie, std::string>> &data)
{
int client;
/* Check validity of client */
if ((client = playerhelpers->GetClientFromSerial(serial)) == 0) {
return;
}
statsPending[client] = false;
// IResultSet *results;
/* Check validity of results */
CookieData *pData;
// IResultRow *row;
// unsigned int timestamp;
// CookieAccess access;
for (const auto &[cookie, value] : data) {
pData = new CookieData(value.data());
pData->changed = false;
pData->timestamp = 0;
Cookie *parent = FindCookie(cookie.name);
if (parent == NULL) {
parent = CreateCookie(cookie.name, cookie.description, cookie.access);
}
pData->parent = parent;
parent->data[client] = pData;
clientData[client].append(pData);
}
statsLoaded[client] = true;
cookieDataLoadedForward->PushCell(client);
cookieDataLoadedForward->Execute(NULL);
}
void CookieManager::InsertCookieCallback(Cookie *pCookie, int dbId)
{
if (dbId > 0) {
pCookie->dbid = dbId;
return;
}
TQueryOp *op = new TQueryOp(Query_SelectId, pCookie);
/* Put the cookie name into the steamId field to save space - Make sure we remember that it's there */
UTIL_strncpy(op->m_params.steamId, pCookie->name, MAX_NAME_LENGTH);
g_ClientPrefs.AddQueryToQueue(op);
}
void CookieManager::SelectIdCallback(Cookie *pCookie, int dbId)
{
pCookie->dbid = dbId;
}
bool CookieManager::AreClientCookiesCached(int client)
{
return statsLoaded[client];
}
bool CookieManager::AreClientCookiesPending(int client)
{
return statsPending[client];
}
void CookieManager::OnPluginDestroyed(IPlugin *plugin)
{
ke::Vector<char *> *pList;
if (plugin->GetProperty("SettingsMenuItems", (void **)&pList, true)) {
ke::Vector<char *> &menuitems = (*pList);
char *name;
ItemDrawInfo draw;
const char *info;
AutoMenuData * data;
unsigned itemcount;
for (size_t p_iter = 0; p_iter < menuitems.length(); ++p_iter) {
name = menuitems[p_iter];
itemcount = clientMenu->GetItemCount();
//remove from this plugins list
for (unsigned int i = 0; i < itemcount; i++) {
info = clientMenu->GetItemInfo(i, &draw);
if (info == NULL) {
continue;
}
if (strcmp(draw.display, name) == 0) {
data = (AutoMenuData *)strtoul(info, NULL, 16);
if (data->handler->forward != NULL) {
forwards->ReleaseForward(data->handler->forward);
}
delete data->handler;
delete data;
clientMenu->RemoveItem(i);
break;
}
}
delete[] name;
}
menuitems.clear();
}
}
bool CookieManager::GetCookieTime(Cookie *pCookie, int client, time_t *value)
{
CookieData *data = pCookie->data[client];
/* Check if a value has been set before */
if (data == NULL) {
return false;
}
*value = data->timestamp;
return true;
}