extensions/clientprefs/cookie.cpp
changeset 3804 90159516acd3
parent 3536 fddd9e8bdef5
child 3829 2d31287c982f
     1.1 --- a/extensions/clientprefs/cookie.cpp	Mon Feb 25 20:44:16 2013 -0600
     1.2 +++ b/extensions/clientprefs/cookie.cpp	Thu Feb 28 21:51:49 2013 +0000
     1.3 @@ -41,6 +41,7 @@
     1.4  	{
     1.5  		connected[i] = false;
     1.6  		statsLoaded[i] = false;
     1.7 +		statsPending[i] = false;
     1.8  	}
     1.9  
    1.10  	cookieDataLoadedForward = NULL;
    1.11 @@ -51,10 +52,7 @@
    1.12  void CookieManager::Unload()
    1.13  {
    1.14  	/* If clients are connected we should try save their data */
    1.15 -
    1.16 -	int maxclients = playerhelpers->GetMaxClients();
    1.17 -
    1.18 -	for (int i=1; i<=maxclients; i++)
    1.19 +	for (int i = playerhelpers->GetMaxClients()+1; --i > 0;)
    1.20  	{
    1.21  		if (connected[i])
    1.22  		{
    1.23 @@ -63,20 +61,14 @@
    1.24  	}
    1.25  
    1.26  	/* Find all cookies and delete them */
    1.27 -	
    1.28 -	SourceHook::List<Cookie *>::iterator _iter;
    1.29 -
    1.30  	Cookie *current;
    1.31  
    1.32 -	_iter = cookieList.begin();
    1.33 -
    1.34 -	while (_iter != cookieList.end())
    1.35 +	for (SourceHook::List<Cookie *>::iterator _iter = cookieList.begin(); _iter != cookieList.end(); _iter++)
    1.36  	{
    1.37  		current = (Cookie *)*_iter;
    1.38  
    1.39  		if (current == NULL)
    1.40  		{
    1.41 -			_iter++;
    1.42  			continue;
    1.43  		}
    1.44  
    1.45 @@ -91,9 +83,9 @@
    1.46  			g_ClientPrefs.cookieMutex->Unlock();
    1.47  			delete current;
    1.48  		}
    1.49 -
    1.50 -		_iter = cookieList.erase(_iter);
    1.51  	}
    1.52 +	
    1.53 +	cookieList.clear();
    1.54  }
    1.55  
    1.56  Cookie *CookieManager::FindCookie(const char *name)
    1.57 @@ -116,9 +108,7 @@
    1.58  	if (pCookie != NULL)
    1.59  	{
    1.60  		/* Update data fields to the provided values */
    1.61 -		strncpy(pCookie->description, description, MAX_DESC_LENGTH);
    1.62 -		pCookie->description[MAX_DESC_LENGTH-1] = '\0';
    1.63 -
    1.64 +		UTIL_strncpy(pCookie->description, description, MAX_DESC_LENGTH);
    1.65  		pCookie->access = access;
    1.66  
    1.67  		return pCookie;
    1.68 @@ -126,18 +116,15 @@
    1.69  
    1.70  	/* First time cookie - Create from scratch */
    1.71  	pCookie = new Cookie(name, description, access);
    1.72 +	pCookie->usedInQuery++;
    1.73 +	
    1.74 +	/* Attempt to insert cookie into the db and get its ID num */
    1.75 +	TQueryOp *op = new TQueryOp(Query_InsertCookie, pCookie);
    1.76 +	op->m_params.cookie = pCookie;
    1.77 +	
    1.78  	cookieTrie.insert(name, pCookie);
    1.79  	cookieList.push_back(pCookie);
    1.80  
    1.81 -	/* Attempt to insert cookie into the db and get its ID num */
    1.82 -
    1.83 -	TQueryOp *op = new TQueryOp(Query_InsertCookie, pCookie);
    1.84 -
    1.85 -	g_ClientPrefs.cookieMutex->Lock();
    1.86 -	op->m_params.cookie = pCookie;
    1.87 -	pCookie->usedInQuery++;
    1.88 -	g_ClientPrefs.cookieMutex->Unlock();
    1.89 -
    1.90  	g_ClientPrefs.AddQueryToQueue(op);
    1.91  
    1.92  	return pCookie;
    1.93 @@ -145,8 +132,6 @@
    1.94  
    1.95  bool CookieManager::GetCookieValue(Cookie *pCookie, int client, char **value)
    1.96  {
    1.97 -	assert(pCookie);
    1.98 -
    1.99  	CookieData *data = pCookie->data[client];
   1.100  
   1.101  	/* Check if a value has been set before */
   1.102 @@ -156,8 +141,8 @@
   1.103  		data->parent = pCookie;
   1.104  		clientData[client].push_back(data);
   1.105  		pCookie->data[client] = data;
   1.106 -		data->changed = true;
   1.107 -		data->timestamp = time(NULL);
   1.108 +		data->changed = false;
   1.109 +		data->timestamp = 0;
   1.110  	}
   1.111  
   1.112  	*value = &data->value[0];
   1.113 @@ -167,8 +152,6 @@
   1.114  
   1.115  bool CookieManager::SetCookieValue(Cookie *pCookie, int client, const char *value)
   1.116  {
   1.117 -	assert(pCookie);
   1.118 -
   1.119  	CookieData *data = pCookie->data[client];
   1.120  
   1.121  	if (data == NULL)
   1.122 @@ -180,8 +163,7 @@
   1.123  	}
   1.124  	else
   1.125  	{
   1.126 -		strncpy(data->value, value, MAX_VALUE_LENGTH);
   1.127 -		data->value[MAX_VALUE_LENGTH-1] = '\0';
   1.128 +		UTIL_strncpy(data->value, value, MAX_VALUE_LENGTH);
   1.129  	}
   1.130  
   1.131  	data->changed = true;
   1.132 @@ -200,9 +182,12 @@
   1.133  	}
   1.134  
   1.135  	connected[client] = true;
   1.136 +	statsPending[client] = true;
   1.137  
   1.138 +	g_ClientPrefs.AttemptReconnection();
   1.139 +	
   1.140  	TQueryOp *op = new TQueryOp(Query_SelectData, player->GetSerial());
   1.141 -	strcpy(op->m_params.steamId, authstring);
   1.142 +	UTIL_strncpy(op->m_params.steamId, authstring, MAX_NAME_LENGTH);
   1.143  
   1.144  	g_ClientPrefs.AddQueryToQueue(op);
   1.145  }
   1.146 @@ -211,105 +196,103 @@
   1.147  {
   1.148  	connected[client] = false;
   1.149  	statsLoaded[client] = false;
   1.150 -
   1.151 -	SourceHook::List<CookieData *>::iterator _iter;
   1.152 +	statsPending[client] = false;
   1.153  
   1.154  	CookieData *current;
   1.155  
   1.156 -	_iter = clientData[client].begin();
   1.157 -
   1.158 -	while (_iter != clientData[client].end())
   1.159 +	g_ClientPrefs.AttemptReconnection();
   1.160 +	
   1.161 +	/* Save this cookie to the database */
   1.162 +	IGamePlayer *player = playerhelpers->GetGamePlayer(client);
   1.163 +	const char *pAuth = NULL;
   1.164 +	int dbId;
   1.165 +	
   1.166 +	if (player)
   1.167  	{
   1.168 +		pAuth = player->GetAuthString();
   1.169 +	}
   1.170 +	
   1.171 +	g_ClientPrefs.ClearQueryCache(player->GetSerial());
   1.172 +	
   1.173 +	for (SourceHook::List<CookieData *>::iterator _iter = clientData[client].begin();\
   1.174 +	_iter != clientData[client].end(); _iter++)
   1.175 +	{
   1.176 +		if (player == NULL || pAuth == NULL)
   1.177 +		{
   1.178 +			/* panic! */
   1.179 +			current->parent->data[client] = NULL;
   1.180 +			delete current;
   1.181 +			continue;
   1.182 +		}
   1.183 +	
   1.184  		current = (CookieData *)*_iter;
   1.185 -
   1.186 -		if (!current->changed)
   1.187 +		dbId = current->parent->dbid;
   1.188 +		
   1.189 +		if (!current->changed || dbId == -1)
   1.190  		{
   1.191  			current->parent->data[client] = NULL;
   1.192  			delete current;
   1.193 -			_iter = clientData[client].erase(_iter);
   1.194  			continue;
   1.195  		}
   1.196  
   1.197 -		/* Save this cookie to the database */
   1.198 -		IGamePlayer *player = playerhelpers->GetGamePlayer(client);
   1.199 -
   1.200 -		if (player == NULL)
   1.201 -		{
   1.202 -			/* panic! */
   1.203 -			return;
   1.204 -		}
   1.205 -
   1.206 -		int dbId = current->parent->dbid;
   1.207 -
   1.208 -		if (dbId == -1)
   1.209 -		{
   1.210 -			/* Insert/Find Query must be still running or failed. */
   1.211 -			return;
   1.212 -		}
   1.213 -
   1.214  		TQueryOp *op = new TQueryOp(Query_InsertData, client);
   1.215  
   1.216 -		strcpy(op->m_params.steamId, player->GetAuthString());
   1.217 +		UTIL_strncpy(op->m_params.steamId, pAuth, MAX_NAME_LENGTH);
   1.218  		op->m_params.cookieId = dbId;
   1.219  		op->m_params.data = current;
   1.220  
   1.221  		g_ClientPrefs.AddQueryToQueue(op);
   1.222  
   1.223  		current->parent->data[client] = NULL;
   1.224 -		
   1.225 -
   1.226 -		/* We don't delete here, it will be removed when the query is completed */
   1.227 -
   1.228 -		_iter = clientData[client].erase(_iter);
   1.229  	}
   1.230 +	
   1.231 +	clientData[client].clear();
   1.232  }
   1.233  
   1.234  void CookieManager::ClientConnectCallback(int serial, IQuery *data)
   1.235  {
   1.236  	int client;
   1.237 -	IResultSet *results;
   1.238  
   1.239  	/* Check validity of client */
   1.240  	if ((client = playerhelpers->GetClientFromSerial(serial)) == 0)
   1.241  	{
   1.242  		return;
   1.243  	}
   1.244 -
   1.245 +	statsPending[client] = false;
   1.246 +	
   1.247 +	IResultSet *results;
   1.248  	/* Check validity of results */
   1.249  	if (data == NULL || (results = data->GetResultSet()) == NULL)
   1.250  	{
   1.251  		return;
   1.252  	}
   1.253  
   1.254 +	CookieData *pData;
   1.255  	IResultRow *row;
   1.256 -	do
   1.257 +	unsigned int timestamp;
   1.258 +	CookieAccess access;
   1.259 +	
   1.260 +	while (results->MoreRows() && ((row = results->FetchRow()) != NULL))
   1.261  	{
   1.262 -		if ((row = results->FetchRow()) == NULL)
   1.263 -		{
   1.264 -			break;
   1.265 -		}
   1.266 -
   1.267 -		const char *name;
   1.268 +		const char *name = "";
   1.269  		row->GetString(0, &name, NULL);
   1.270 -
   1.271 -		const char *value;
   1.272 +		
   1.273 +		const char *value = "";
   1.274  		row->GetString(1, &value, NULL);
   1.275  
   1.276 -		CookieData *pData = new CookieData(value);
   1.277 +		pData = new CookieData(value);
   1.278  		pData->changed = false;
   1.279  
   1.280 -		unsigned int timestamp = 0;
   1.281 -		row->GetInt(4, (int *)&timestamp);
   1.282 -		pData->timestamp = timestamp;
   1.283 +		pData->timestamp = (row->GetInt(4, (int *)&timestamp) == DBVal_Data) ? timestamp : 0;
   1.284  
   1.285  		Cookie *parent = FindCookie(name);
   1.286  
   1.287  		if (parent == NULL)
   1.288  		{
   1.289 -			const char *desc;
   1.290 +			const char *desc = "";
   1.291  			row->GetString(2, &desc, NULL);
   1.292  
   1.293 -			CookieAccess access = CookieAccess_Public;
   1.294 +			access = CookieAccess_Public;
   1.295  			row->GetInt(3, (int *)&access);
   1.296  
   1.297  			parent = CreateCookie(name, desc, access);
   1.298 @@ -317,10 +300,8 @@
   1.299  
   1.300  		pData->parent = parent;
   1.301  		parent->data[client] = pData;
   1.302 -
   1.303  		clientData[client].push_back(pData);
   1.304 -
   1.305 -	} while (results->MoreRows());
   1.306 +	}
   1.307  
   1.308  	statsLoaded[client] = true;
   1.309  
   1.310 @@ -338,7 +319,7 @@
   1.311  
   1.312  	TQueryOp *op = new TQueryOp(Query_SelectId, pCookie);
   1.313  	/* Put the cookie name into the steamId field to save space - Make sure we remember that it's there */
   1.314 -	strcpy(op->m_params.steamId, pCookie->name);
   1.315 +	UTIL_strncpy(op->m_params.steamId, pCookie->name, MAX_NAME_LENGTH);
   1.316  	g_ClientPrefs.AddQueryToQueue(op);
   1.317  }
   1.318  
   1.319 @@ -366,26 +347,31 @@
   1.320  	return statsLoaded[client];
   1.321  }
   1.322  
   1.323 +bool CookieManager::AreClientCookiesPening(int client)
   1.324 +{
   1.325 +	return statsPending[client];
   1.326 +}
   1.327 +
   1.328  void CookieManager::OnPluginDestroyed(IPlugin *plugin)
   1.329  {
   1.330  	SourceHook::List<char *> *pList;
   1.331  
   1.332  	if (plugin->GetProperty("SettingsMenuItems", (void **)&pList, true))
   1.333  	{
   1.334 -		SourceHook::List<char *>::iterator p_iter = pList->begin();
   1.335  		char *name;
   1.336 -
   1.337 -		while (p_iter != pList->end())
   1.338 +		ItemDrawInfo draw;
   1.339 +		const char *info;
   1.340 +		AutoMenuData * data;
   1.341 +		unsigned itemcount;
   1.342 +		
   1.343 +		for (SourceHook::List<char *>::iterator p_iter = pList->begin(); p_iter != pList->end(); p_iter++)
   1.344  		{
   1.345  			name = (char *)*p_iter;
   1.346 -		
   1.347 -			p_iter = pList->erase(p_iter); //remove from this plugins list
   1.348 -
   1.349 -			ItemDrawInfo draw;
   1.350 -			
   1.351 -			for (unsigned int i=0; i<clientMenu->GetItemCount(); i++)
   1.352 +			itemcount = clientMenu->GetItemCount();
   1.353 +			//remove from this plugins list
   1.354 +			for (unsigned int i=0; i < itemcount; i++)
   1.355  			{
   1.356 -				const char *info = clientMenu->GetItemInfo(i, &draw);
   1.357 +				info = clientMenu->GetItemInfo(i, &draw);
   1.358  
   1.359  				if (info == NULL)
   1.360  				{
   1.361 @@ -394,9 +380,7 @@
   1.362  
   1.363  				if (strcmp(draw.display, name) == 0)
   1.364  				{
   1.365 -					ItemDrawInfo draw;
   1.366 -					const char *info = clientMenu->GetItemInfo(i, &draw);
   1.367 -					AutoMenuData *data = (AutoMenuData *)strtoul(info, NULL, 16);
   1.368 +					data = (AutoMenuData *)strtoul(info, NULL, 16);
   1.369  
   1.370  					if (data->handler->forward != NULL)
   1.371  					{
   1.372 @@ -410,15 +394,15 @@
   1.373  				}
   1.374  			}
   1.375  
   1.376 -			delete name;
   1.377 -		}		
   1.378 +			delete [] name;
   1.379 +		}
   1.380 +		
   1.381 +		pList->clear();
   1.382  	}
   1.383  }
   1.384  
   1.385  bool CookieManager::GetCookieTime(Cookie *pCookie, int client, time_t *value)
   1.386  {
   1.387 -	assert(pCookie);
   1.388 -
   1.389  	CookieData *data = pCookie->data[client];
   1.390  
   1.391  	/* Check if a value has been set before */