Skip to content

Repository files navigation

Clientprefs for CSSharp

This plugin exposes some natives for developers to save player data to SQLite / MySQL without actually adding sql code to your plugin giving developers easy access to save player cookie to database.

Example

usingClientprefs.API;privatereadonlyPluginCapability<IClientprefsApi>PluginCapability=new("Clientprefs");privateIClientprefsApi?ClientprefsApi;privateintCookieID=0;privateDictionary<CCSPlayerController,string>PlayerCookie=new();publicoverridevoidUnload(boolhotReload){base.Unload(hotReload);if(ClientprefsApi==null)return;ClientprefsApi.OnDatabaseLoaded-=OnClientprefDatabaseReady;ClientprefsApi.OnPlayerCookiesCached-=OnPlayerCookiesCached;}publicoverridevoidOnAllPluginsLoaded(boolhotReload){ClientprefsApi=PluginCapability.Get();if(ClientprefsApi==null)return;ClientprefsApi.OnDatabaseLoaded+=OnClientprefDatabaseReady;ClientprefsApi.OnPlayerCookiesCached+=OnPlayerCookiesCached;if(hotReload){if(ClientprefsApi==null||CookieID==-1)return;foreach(varplayerinUtilities.GetPlayers().Where(p =>!p.IsBot)){if(!ClientprefsApi.ArePlayerCookiesCached(player))continue;PlayerCookie[player]=ClientprefsApi.GetPlayerCookie(player,CookieID);}}}publicvoidOnClientprefDatabaseReady(){if(ClientprefsApi==null)return;Task.Run(async()=>{CookieID=ClientprefsApi.RegPlayerCookieAsync("example_cookie","Example cookie description",CookieAccess.CookieAccess_Public);if(CookieID==-1){Logger.LogError("[Clientprefs-Example] Failed to register/load cookie 1");return;}Logger.LogInformation($"[Clientprefs-Example] Registered/Loaded cookie with ID: {CookieID}");// ID: 1});}publicvoidOnPlayerCookiesCached(CCSPlayerControllerplayer){if(ClientprefsApi==null||CookieID==-1)return;PlayerCookie[player]=ClientprefsApi.GetPlayerCookie(player,CookieID);Logger.LogInformation($"[Clientprefs-Example] Cookie value: {cookieValue}");}[ConsoleCommand("css_clientprefs_example","Saves example clientprefs cookie value")]publicvoidOnExampleCommand(CCSPlayerController?caller,CommandInfo_){if(caller==null||!caller.IsValid||ClientprefsApi==null||CookieID==-1){return;}ClientprefsApi.SetPlayerCookie(caller,CookieID,PlayerCookie[player]);}

Config

{
"TableName": "css_cookies",
"TableNamePlayerData": "css_cookies_playerdata",
"DatabaseType": "sqlite",
"DatabaseHost": "",
"DatabaseName": "",
"DatabaseUsername": "",
"DatabasePassword": "",
"DatabasePort": 3306,
"DatabaseSslmode": "none",
"Debug": false,
"ConfigVersion": 1
}

Things left to implement

[ ] css_settings

Changelog

  • v1.0.7

    • refactor: whole codebase.
    • fix: cases where player cookies were not getting saved.
  • v1.0.6

    • fix: Exceptions on player cached & disconnect.
  • v1.0.4

    • fix: CreatePlayerCookie native using wrong parameter name.
    • fix: SavePlayerCookie missing not operator.
    • fix: Player preferences not getting saved at map change.
  • v1.0.3

    • fix: Plugin will no longer throw "Native d01e4eb5 was invoked on a non-main thread" on playerConnect.
    • Devs can now save clientpref for player at PlayerDisconnect(HookMode.Post) too as the plugin now saves in database after 0.5 seconds.
    • Added more error logging and added variable in config to enable debug logs.
  • v1.0.2

    • Fixed dictionary errors for players after map change.
  • v1.0.1

    • Changed hook method of OnClientprefDatabaseReady & OnPlayerCookiesCached. Please re-refer ClientprefsExample.

About

Clientprefs plugin for CounterStrikeSharp

Topics

Resources

Stars

22 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages