Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
NickFox007 authored May 11, 2024
0 parents commit 4777c7e
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 0 deletions.
Binary file added AnyBaseLib.dll
Binary file not shown.
31 changes: 31 additions & 0 deletions PlayerSettings.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34616.47
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlayerSettings", "PlayerSettings\PlayerSettings.csproj", "{A3C30B17-9E3D-4CA3-81E0-91B1EBFEEB6C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlayerSettingsApi", "PlayerSettingsApi\PlayerSettingsApi.csproj", "{CEFCA2C1-6552-4C3C-A348-3E3D004D5D44}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A3C30B17-9E3D-4CA3-81E0-91B1EBFEEB6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3C30B17-9E3D-4CA3-81E0-91B1EBFEEB6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3C30B17-9E3D-4CA3-81E0-91B1EBFEEB6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3C30B17-9E3D-4CA3-81E0-91B1EBFEEB6C}.Release|Any CPU.Build.0 = Release|Any CPU
{CEFCA2C1-6552-4C3C-A348-3E3D004D5D44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CEFCA2C1-6552-4C3C-A348-3E3D004D5D44}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEFCA2C1-6552-4C3C-A348-3E3D004D5D44}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEFCA2C1-6552-4C3C-A348-3E3D004D5D44}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7C957E67-975E-4328-B4BD-4DD82C04E59F}
EndGlobalSection
EndGlobal
46 changes: 46 additions & 0 deletions PlayerSettings/CPlayerSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using CounterStrikeSharp.API.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PlayerSettings
{
internal class CPlayerSettings
{
private int userid;
private static Dictionary<string, string> cached_values;

public CPlayerSettings(int _userid)
{
userid = _userid;
cached_values = new Dictionary<string, string>();
}

public string GetValue(string param, string default_value)
{
string value;
if(!cached_values.TryGetValue(param, out value))
{
value = Storage.GetUserSettingValue(userid, param, default_value);
cached_values[param] = value;
}

return value;
}

public void SetValue(string param, string value)
{
cached_values[param] = value;
Storage.SetUserSettingValue(userid, param, value);
}

public int UserId()
{
return userid;
}


}
}
25 changes: 25 additions & 0 deletions PlayerSettings/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Core.Capabilities;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Menu;


namespace PlayerSettings;
public class PlayerSettingsCore : BasePlugin
{
public override string ModuleName => "PlayerSettings [Core]";
public override string ModuleVersion => "0.1";
public override string ModuleAuthor => "Nick Fox";
public override string ModuleDescription => "One storage for player's settings (aka ClientCookies)";

private ISettingsApi? _api;
private readonly PluginCapability<ISettingsApi?> _pluginCapability = new("settings:nfcore");
public override void Load(bool hotReload)
{
_api = new SettingsApi();
Capabilities.RegisterPluginCapability(_pluginCapability, () => _api);
Storage.Init(this);
}
}
23 changes: 23 additions & 0 deletions PlayerSettings/PlayerSettings.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.233" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PlayerSettingsApi\PlayerSettingsApi.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="AnyBaseLib">
<HintPath>..\AnyBaseLib.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
44 changes: 44 additions & 0 deletions PlayerSettings/SettingsApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using CounterStrikeSharp.API.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace PlayerSettings
{

internal class SettingsApi : ISettingsApi
{
List<CPlayerSettings> settings;
public SettingsApi()
{
settings = new List<CPlayerSettings>();
}

private CPlayerSettings FindUser(int userid)
{
foreach (var item in this.settings)
{
if (item.UserId() == userid)
{
return item;
}
}
var newInst = new CPlayerSettings(userid);
settings.Add(newInst);
return newInst;
}

public string GetPlayerSettingsValue(CCSPlayerController player, string param, string default_value)
{
return FindUser(Storage.GetUserId(player)).GetValue(param, default_value);
}

public void SetPlayerSettingsValue(CCSPlayerController player, string param, string value)
{
FindUser(Storage.GetUserId(player)).SetValue(param, value);
}
}
}
60 changes: 60 additions & 0 deletions PlayerSettings/Storage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using AnyBaseLib;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace PlayerSettings
{
internal static class Storage
{
private static IAnyBase db;

public static void Init(BasePlugin plugin, string driver = "sqlite")
{
db = CAnyBase.Base(driver);
db.Set(AnyBaseLib.Bases.CommitMode.AutoCommit, Path.Combine(plugin.ModuleDirectory,"menusets"));

db.Init();
db.Query("create table if not exists \"users\" (\"id\" integer primary key AUTOINCREMENT, \"steam\" varchar(255) not null)", null, true);
db.Query("create table if not exists \"settings\" (\"user_id\" int, \"param\" varchar(255) not null, \"value\" varchar(255) not null)", null, true);
}

public static int GetUserId(CCSPlayerController player)
{
var steamid = player.SteamID;
var res = db.Query("select \"id\" from users where \"steam\" = \"{ARG}\"", new List<string>([steamid.ToString()]));
if(res.Count == 0)
{
db.Query("insert into \"users\" (\"steam\") values (\"{ARG}\")", new List<string>([steamid.ToString()]), true);
res = db.Query("select \"id\" from \"users\" where \"steam\" = \"{ARG}\"", new List<string>([steamid.ToString()]));
}
return int.Parse(res[0][0]);
}

public static string GetUserSettingValue(int userid, string param, string default_value)
{
var res = db.Query("select \"value\" from \"settings\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List<string>([userid.ToString(), param]));
if (res.Count == 0)
{
db.Query("insert into \"settings\" (\"user_id\", \"param\",\"value\") values ({ARG},\"{ARG}\", \"{ARG}\")", new List<string>([userid.ToString(), param, default_value]), true);
return default_value;
}
return res[0][0];
}

public static void SetUserSettingValue(int userid, string param, string value)
{
db.Query("update \"settings\" set \"value\" = \"{ARG}\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List<string>([value, userid.ToString(), param]), true);
}




}
}
15 changes: 15 additions & 0 deletions PlayerSettingsApi/ISettingsApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using CounterStrikeSharp.API.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PlayerSettings
{
public interface ISettingsApi
{
public string GetPlayerSettingsValue(CCSPlayerController player, string param, string default_value);
public void SetPlayerSettingsValue(CCSPlayerController player, string param, string value);
}
}
14 changes: 14 additions & 0 deletions PlayerSettingsApi/PlayerSettingsApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.233" />
</ItemGroup>

</Project>

0 comments on commit 4777c7e

Please sign in to comment.