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).

ParameterType
nameconst char*
descriptionconst char*
accessSource2ModCookieAccess

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.

ParameterType
nameconst char*

GetCookieName

virtual const char* GetCookieName(Source2ModCookieId cookie) = 0;

Its name / description; nullptr for a bad id.

ParameterType
cookieSource2ModCookieId

GetCookieDescription

virtual const char* GetCookieDescription(Source2ModCookieId cookie) = 0;
ParameterType
cookieSource2ModCookieId

GetCookieAccess

virtual Source2ModCookieAccess GetCookieAccess(Source2ModCookieId cookie) = 0;
ParameterType
cookieSource2ModCookieId

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).

ParameterType
slotCPlayerSlot

GetCookie

virtual bool GetCookie(CPlayerSlot slot, Source2ModCookieId cookie, char* out, size_t maxlen) = 0;

The player's value.

ParameterType
slotCPlayerSlot
cookieSource2ModCookieId
outchar*
maxlensize_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.

ParameterType
slotCPlayerSlot
cookieSource2ModCookieId
valueconst 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.

ParameterType
slotCPlayerSlot
cookieSource2ModCookieId

SetCookieBySteamID

virtual bool SetCookieBySteamID(uint64 steamId, Source2ModCookieId cookie, const char* value) = 0;

Sets the value of a player who need not be connected.

ParameterTypeDescription
steamIduint64A SteamID64.
cookieSource2ModCookieId
valueconst char*

AddCookiesCachedListener

virtual Source2ModCookiesListenerId AddCookiesCachedListener(CookiesCachedCallback callback) = 0;

Runs callback whenever a player's cookies were loaded. Goes with the plugin.

ParameterType
callbackCookiesCachedCallback

RemoveCookiesCachedListener

virtual bool RemoveCookiesCachedListener(Source2ModCookiesListenerId id) = 0;

Enums

Source2ModCookieAccess

enum Source2ModCookieAccess

Who may see and change a cookie from the cookie commands. SourceMod's CookieAccess.

NameValueDescription
SOURCE2MOD_COOKIE_PUBLIC0Players can read and change it.
SOURCE2MOD_COOKIE_PROTECTED1Players can read it, only plugins change it.
SOURCE2MOD_COOKIE_PRIVATE2Hidden from players.

Macros

#define SOURCE2MOD_COOKIE_MAX_NAME_LENGTH 30

Longest cookie name and value, in bytes without the terminator (SourceMod's).

On this page