ISource2ModCookies
Client preferences: per-player settings that outlive the session.
SourceMod's clientprefs. A cookie is a named string setting ("quake_sounds", "hud_color"); every player has their own value of it, kept in a database under their SteamID, and loaded into memory when the player is authorized. Until then -- AreCookiesCached() false -- reads return "" and writes are kept and saved once the player's values arrived.
The database is an SQLite file, addons/source2mod/data/clientprefs.sq3 unless core.json's "ClientPrefsDatabase" names another. The tables are SourceMod's (sm_cookies, sm_cookie_cache) and players are keyed by "STEAM_1:Y:Z", so a SourceMod clientprefs database can be carried over.
Values are saved when they change (a write goes to the database right away, asynchronously) -- nothing is lost on a crash or a map change.
Interface id ISource2ModCookies001 (SOURCE2MOD_COOKIES_INTERFACE) · Global g_pSource2ModCookies · Header source2mod/ISource2ModCookies.h
Types
Source2ModCookiesListenerId
using Source2ModCookiesListenerId = Source2ModHookId;A cookies-cached listener (AddCookiesCachedListener).
Source2ModCookieId
using Source2ModCookieId = int;A registered cookie; 0 is none.
CookiesCachedCallback
using CookiesCachedCallback = Source2ModCallback<void(CPlayerSlot slot)>;A player's cookies were loaded (SourceMod's OnClientCookiesCached).
Runs on the main thread, once per connection, after OnClientAuthorized.
Methods
IsConnected
virtual bool IsConnected() = 0;Whether the cookie database is connected. Everything else works without it, only nothing is loaded or saved.
RegisterCookie
virtual Source2ModCookieId RegisterCookie(const char* name, const char* description, Source2ModCookieAccess access) = 0;Registers a cookie, or finds it when it already is registered (by any plugin -- cookies are shared, by name).
| Parameter | Type |
|---|---|
name | const char* |
description | const char* |
access | Source2ModCookieAccess |
Returns Source2ModCookieId — The cookie, 0 for an empty name or one longer than SOURCE2MOD_COOKIE_MAX_NAME_LENGTH.
FindCookie
virtual Source2ModCookieId FindCookie(const char* name) = 0;A registered cookie by name; 0 when there is none.
| Parameter | Type |
|---|---|
name | const char* |
GetCookieName
virtual const char* GetCookieName(Source2ModCookieId cookie) = 0;Its name / description; nullptr for a bad id.
| Parameter | Type |
|---|---|
cookie | Source2ModCookieId |
GetCookieDescription
virtual const char* GetCookieDescription(Source2ModCookieId cookie) = 0;| Parameter | Type |
|---|---|
cookie | Source2ModCookieId |
GetCookieAccess
virtual Source2ModCookieAccess GetCookieAccess(Source2ModCookieId cookie) = 0;| Parameter | Type |
|---|---|
cookie | Source2ModCookieId |
GetCookieCount
virtual int GetCookieCount() = 0;How many cookies are registered; ids run from 1 to that.
AreCookiesCached
virtual bool AreCookiesCached(CPlayerSlot slot) = 0;Whether the player's values were loaded (or there is no database to load them from).
| Parameter | Type |
|---|---|
slot | CPlayerSlot |
GetCookie
virtual bool GetCookie(CPlayerSlot slot, Source2ModCookieId cookie, char* out, size_t maxlen) = 0;The player's value.
| Parameter | Type |
|---|---|
slot | CPlayerSlot |
cookie | Source2ModCookieId |
out | char* |
maxlen | size_t |
Returns bool — false (and "") when the player has never had one, or the values are not loaded yet.
SetCookie
virtual bool SetCookie(CPlayerSlot slot, Source2ModCookieId cookie, const char* value) = 0;Sets the player's value and saves it.
| Parameter | Type |
|---|---|
slot | CPlayerSlot |
cookie | Source2ModCookieId |
value | const char* |
Returns bool — false for an empty slot, a bot, a bad id or a value longer than SOURCE2MOD_COOKIE_MAX_VALUE_LENGTH.
GetCookieTime
virtual int64_t GetCookieTime(CPlayerSlot slot, Source2ModCookieId cookie) = 0;When the value was last set, Unix time; 0 when it never was.
| Parameter | Type |
|---|---|
slot | CPlayerSlot |
cookie | Source2ModCookieId |
SetCookieBySteamID
virtual bool SetCookieBySteamID(uint64 steamId, Source2ModCookieId cookie, const char* value) = 0;Sets the value of a player who need not be connected.
| Parameter | Type | Description |
|---|---|---|
steamId | uint64 | A SteamID64. |
cookie | Source2ModCookieId | |
value | const char* |
AddCookiesCachedListener
virtual Source2ModCookiesListenerId AddCookiesCachedListener(CookiesCachedCallback callback) = 0;Runs callback whenever a player's cookies were loaded. Goes with the plugin.
| Parameter | Type |
|---|---|
callback | CookiesCachedCallback |
RemoveCookiesCachedListener
virtual bool RemoveCookiesCachedListener(Source2ModCookiesListenerId id) = 0;| Parameter | Type |
|---|---|
id | Source2ModCookiesListenerId |
Enums
Source2ModCookieAccess
enum Source2ModCookieAccessWho may see and change a cookie from the cookie commands. SourceMod's CookieAccess.
| Name | Value | Description |
|---|---|---|
SOURCE2MOD_COOKIE_PUBLIC | 0 | Players can read and change it. |
SOURCE2MOD_COOKIE_PROTECTED | 1 | Players can read it, only plugins change it. |
SOURCE2MOD_COOKIE_PRIVATE | 2 | Hidden from players. |
Macros
SOURCE2MOD_COOKIE_MAX_NAME_LENGTH
#define SOURCE2MOD_COOKIE_MAX_NAME_LENGTH 30Longest cookie name and value, in bytes without the terminator (SourceMod's).