Added native to clientprefs to alter data on a disconnected player (bug 3882, r=pred).
authorDon <datal30mb@users.sourceforge.net>
Fri Oct 14 11:28:53 2011 -0400 (2011-10-14)
changeset 34110a2e40a50757
parent 3410 b948e11cd67d
child 3412 dae51f7f4147
Added native to clientprefs to alter data on a disconnected player (bug 3882, r=pred).
extensions/clientprefs/extension.cpp
extensions/clientprefs/extension.h
extensions/clientprefs/natives.cpp
plugins/include/clientprefs.inc
     1.1 --- a/extensions/clientprefs/extension.cpp	Fri Oct 14 09:25:42 2011 -0400
     1.2 +++ b/extensions/clientprefs/extension.cpp	Fri Oct 14 11:28:53 2011 -0400
     1.3 @@ -365,6 +365,32 @@
     1.4  	queryMutex->Unlock();
     1.5  }
     1.6  
     1.7 +size_t IsAuthIdConnected(char *authID)
     1.8 +{
     1.9 +	IGamePlayer *player;
    1.10 +	int maxPlayers = playerhelpers->GetMaxClients();
    1.11 +
    1.12 +	for (int playerIndex = 1; playerIndex <= maxPlayers; playerIndex++)
    1.13 +	{
    1.14 +		player = playerhelpers->GetGamePlayer(playerIndex);
    1.15 +		if (!player || !player->IsConnected())
    1.16 +		{
    1.17 +			continue;
    1.18 +		}
    1.19 +		const char *authString = player->GetAuthString();
    1.20 +		if (!authString || authString[0] == '\0')
    1.21 +		{
    1.22 +			continue;
    1.23 +		}
    1.24 +
    1.25 +		if (strcmp(authString, authID) == 0)
    1.26 +		{
    1.27 +			return playerIndex;
    1.28 +		}
    1.29 +	}
    1.30 +	return 0;
    1.31 +}
    1.32 +
    1.33  size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
    1.34  {
    1.35  	va_list ap;
     2.1 --- a/extensions/clientprefs/extension.h	Fri Oct 14 09:25:42 2011 -0400
     2.2 +++ b/extensions/clientprefs/extension.h	Fri Oct 14 11:28:53 2011 -0400
     2.3 @@ -170,6 +170,8 @@
     2.4  	}
     2.5  };
     2.6  
     2.7 +size_t IsAuthIdConnected(char *authID);
     2.8 +
     2.9  size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
    2.10  
    2.11  extern sp_nativeinfo_t g_ClientPrefNatives[];
     3.1 --- a/extensions/clientprefs/natives.cpp	Fri Oct 14 09:25:42 2011 -0400
     3.2 +++ b/extensions/clientprefs/natives.cpp	Fri Oct 14 11:28:53 2011 -0400
     3.3 @@ -89,6 +89,63 @@
     3.4  		NULL);
     3.5  }
     3.6  
     3.7 +cell_t SetAuthIdCookie(IPluginContext *pContext, const cell_t *params)
     3.8 +{
     3.9 +	if (g_ClientPrefs.Database == NULL && !g_ClientPrefs.databaseLoading)
    3.10 +	{
    3.11 +		return pContext->ThrowNativeError("Clientprefs is disabled due to a failed database connection");
    3.12 +	}
    3.13 +
    3.14 +	char *steamID;
    3.15 +	pContext->LocalToString(params[1], &steamID);
    3.16 +
    3.17 +	// convert cookie handle to Cookie*
    3.18 +	Handle_t hndl = static_cast<Handle_t>(params[2]);
    3.19 +	HandleError err;
    3.20 +	HandleSecurity sec;
    3.21 + 
    3.22 +	sec.pOwner = NULL;
    3.23 +	sec.pIdentity = myself->GetIdentity();
    3.24 +
    3.25 +	Cookie *pCookie;
    3.26 +
    3.27 +	if ((err = handlesys->ReadHandle(hndl, g_CookieType, &sec, (void **)&pCookie))
    3.28 +	     != HandleError_None)
    3.29 +	{
    3.30 +		return pContext->ThrowNativeError("Invalid Cookie handle %x (error %d)", hndl, err);
    3.31 +	}
    3.32 +
    3.33 +	int i_dbId = pCookie->dbid;
    3.34 +	char *value;
    3.35 +	pContext->LocalToString(params[3], &value);
    3.36 +
    3.37 +	// make sure the steamID isn't currently connected
    3.38 +	if (int client = IsAuthIdConnected(steamID))
    3.39 +	{
    3.40 +		// use regular code for connected players
    3.41 +		return g_CookieManager.SetCookieValue(pCookie, client, value);
    3.42 +	}
    3.43 +
    3.44 +	// constructor calls strncpy for us
    3.45 +	CookieData *payload = new CookieData(value);
    3.46 +
    3.47 +	// set changed so players connecting later in during the same map will have the correct value
    3.48 +	payload->changed = true;
    3.49 +	payload->timestamp = time(NULL);
    3.50 +
    3.51 +	// edit database table
    3.52 +	TQueryOp *op = new TQueryOp(Query_InsertData, pCookie);
    3.53 +	// limit player auth length which doubles for cookie name length
    3.54 +	strncpy(op->m_params.steamId, steamID, MAX_NAME_LENGTH);
    3.55 +	op->m_params.steamId[MAX_NAME_LENGTH-1] = '\0';
    3.56 +	op->m_params.cookieId = i_dbId;
    3.57 +	op->m_params.data = payload;
    3.58 +
    3.59 +	g_ClientPrefs.AddQueryToQueue(op);
    3.60 +
    3.61 +	return 1;
    3.62 +}
    3.63 +
    3.64  cell_t SetClientPrefCookie(IPluginContext *pContext, const cell_t *params)
    3.65  {
    3.66  	if (g_ClientPrefs.Database == NULL && !g_ClientPrefs.databaseLoading)
    3.67 @@ -436,6 +493,7 @@
    3.68  	{"RegClientCookie",				RegClientPrefCookie},
    3.69  	{"FindClientCookie",			FindClientPrefCookie},
    3.70  	{"SetClientCookie",				SetClientPrefCookie},
    3.71 +	{"SetAuthIdCookie",				SetAuthIdCookie},
    3.72  	{"GetClientCookie",				GetClientPrefCookie},
    3.73  	{"AreClientCookiesCached",		AreClientCookiesCached},
    3.74  	{"GetCookieAccess",				GetCookieAccess},
     4.1 --- a/plugins/include/clientprefs.inc	Fri Oct 14 09:25:42 2011 -0400
     4.2 +++ b/plugins/include/clientprefs.inc	Fri Oct 14 11:28:53 2011 -0400
     4.3 @@ -132,6 +132,17 @@
     4.4  native GetClientCookie(client, Handle:cookie, String:buffer[], maxlen);
     4.5  
     4.6  /**
     4.7 + * Sets the value of a Client preference cookie based on an authID string.
     4.8 + *
     4.9 + * @param authID		String Auth/STEAM ID of player to set.
    4.10 + * @param cookie		Client preference cookie handle.
    4.11 + * @param value			String value to set.
    4.12 + * @noreturn
    4.13 + * @error			Invalid cookie handle.
    4.14 + */
    4.15 +native SetAuthIdCookie(const String:authID[], Handle:cookie, const String:value[]);
    4.16 +
    4.17 +/**
    4.18   * Checks if a clients cookies have been loaded from the database.
    4.19   *
    4.20   * @param client		Client index.