diff --git a/SFR/Misc/Config.cs b/SFR/Misc/Config.cs new file mode 100644 index 0000000..f531536 --- /dev/null +++ b/SFR/Misc/Config.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.IO; +using System.Linq; +using HarmonyLib; +using SFD; +using Color = Microsoft.Xna.Framework.Color; + +namespace SFR.Misc; + +[HarmonyPatch()] +internal static class Config +{ + private static Dictionary _content; + public static Color MenuBorderColor = Constants.COLORS.MENU_BLUE; + + public static void Init() + { + if (_content is not null) return; + + string configPath = Path.Combine(Constants.Paths.UserDocumentsSFDUserDataPath, "sfr.ini"); + bool mustReset = !File.Exists(configPath); + if (mustReset) + { + using (_ = File.CreateText(configPath)) { }; + } + + _content = GetConfigData(configPath); + + if (!mustReset) + { + mustReset = !_content.ContainsKey("MenuColor"); + } + + if (mustReset) + { + DefaultSettings(configPath); + return; + } + + var menuBorderColor = ColorTranslator.FromHtml(_content["MenuColor"]); + MenuBorderColor = new Color(menuBorderColor.R, menuBorderColor.G, menuBorderColor.B, menuBorderColor.A); + + ApplySettings(); + } + + private static void ApplySettings() + { + Constants.COLORS.MENU_BLUE = MenuBorderColor; + } + + private static void DefaultSettings(string path) + { + _content = new Dictionary() + { + { + "MenuColor", MenuBorderColor.ToHex() + } + }; + + WriteConfigData(path, _content); + } + + [HarmonyPostfix] + [HarmonyPatch(typeof(SFDConfig), nameof(SFDConfig.LoadConfig))] + private static void LoadConfig() => Init(); + + private static Dictionary GetConfigData(string fileName) + { + Dictionary data = []; + foreach (string line in File.ReadLines(fileName)) + { + if (string.IsNullOrEmpty(line)) continue; + + string[] lineData = line.Split('='); + data.Add(lineData[0], lineData[1]); + } + + return data; + } + + private static void WriteConfigData(string fileName, Dictionary data) => File.WriteAllLines(fileName, data.Select(z => $"{z.Key}={z.Value}{Environment.NewLine}")); +} diff --git a/SFR/SFR.csproj b/SFR/SFR.csproj index 92c149c..d85e360 100644 --- a/SFR/SFR.csproj +++ b/SFR/SFR.csproj @@ -124,6 +124,7 @@ +