From 685a3f4f41d838dc2e528a7eadea40f6b054f753 Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 14 Jan 2024 00:07:55 +0100 Subject: [PATCH] Settings File --- OpenCS2hock/Installer.cs | 10 ++++++ OpenCS2hock/OpenCS2hock.cs | 4 ++- OpenCS2hock/Settings.cs | 68 ++++++++++++++++++++++++++++++++++++++ OpenCS2hock/Shocker.cs | 2 +- 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 OpenCS2hock/Settings.cs diff --git a/OpenCS2hock/Installer.cs b/OpenCS2hock/Installer.cs index a91a622..da9a6e3 100644 --- a/OpenCS2hock/Installer.cs +++ b/OpenCS2hock/Installer.cs @@ -1,9 +1,19 @@ using Microsoft.Win32; +using Newtonsoft.Json; namespace OpenCS2hock; public static class Installer { + public static Settings GetSettings(string? path = null) + { + string settingsFilePath = path ?? "config.json"; + if (!File.Exists(settingsFilePath)) + File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(new Settings(), Formatting.Indented)); + + return JsonConvert.DeserializeObject(File.ReadAllText(settingsFilePath)); + } + public static void InstallGsi() { string installLocation = Path.Combine(GetInstallDirectory(), "game\\csgo\\cfg\\gamestate_integration_opencs2hock.cfg"); diff --git a/OpenCS2hock/OpenCS2hock.cs b/OpenCS2hock/OpenCS2hock.cs index 37dce8a..394d43d 100644 --- a/OpenCS2hock/OpenCS2hock.cs +++ b/OpenCS2hock/OpenCS2hock.cs @@ -4,9 +4,11 @@ public class OpenCS2hock { private GSIServer GSIServer { get; init; } private List _shockers = new(); + private readonly Settings _settings; - public OpenCS2hock() + public OpenCS2hock(string? settingsPath = null) { + _settings = Installer.GetSettings(settingsPath); Installer.InstallGsi(); this.GSIServer = new GSIServer(3000); this.GSIServer.OnMessage += OnGSIMessage; diff --git a/OpenCS2hock/Settings.cs b/OpenCS2hock/Settings.cs new file mode 100644 index 0000000..d8f8994 --- /dev/null +++ b/OpenCS2hock/Settings.cs @@ -0,0 +1,68 @@ +namespace OpenCS2hock; + +public struct Settings +{ + public OpenShockSettings OpenShockSettings = new() + { + Endpoint = "https://api.shocklink.net", + ApiKey = "", + Shockers = Array.Empty() + }; + + public short FixedIntensity = 30; + public short FixedDuration = 1000; + + public Range IntensityRange = new () + { + Min = 0, + Max = 100 + }; + + public Range DurationRange = new() + { + Min = 1000, + Max = 2000 + }; + + public Actions Actions = new() + { + OnKill = "Nothing", + OnDeath = "Shock", + OnRoundStart = "Vibrate", + OnRoundEnd = "Nothing", + OnRoundWin = "Beep", + OnRoundLoss = "Nothing" + }; + + public Settings() + { + + } + + internal Shocker.ControlAction StringToAction(string str) + { + return str.ToLower() switch + { + "shock" => Shocker.ControlAction.Shock, + "vibrate" => Shocker.ControlAction.Vibrate, + "beep" => Shocker.ControlAction.Beep, + _ => Shocker.ControlAction.Nothing + }; + } +} + +public struct OpenShockSettings +{ + public string Endpoint, ApiKey; + public string[] Shockers; +} + +public struct Range +{ + public short Min, Max; +} + +public struct Actions +{ + public string OnKill, OnDeath, OnRoundStart, OnRoundEnd, OnRoundWin, OnRoundLoss; +} \ No newline at end of file diff --git a/OpenCS2hock/Shocker.cs b/OpenCS2hock/Shocker.cs index e40177b..7884f7f 100644 --- a/OpenCS2hock/Shocker.cs +++ b/OpenCS2hock/Shocker.cs @@ -7,7 +7,7 @@ public abstract class Shocker protected readonly string Endpoint; protected string[] ShockerIds; - public enum ControlAction { Beep, Vibrate, Shock } + public enum ControlAction { Beep, Vibrate, Shock, Nothing } public abstract void Control(ControlAction action, byte intensity, short duration, string? shockerId = null);