From b467a0e8e31aaa8d26c3f6617d032746f734abc6 Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 20 Dec 2017 00:07:51 -0800 Subject: [PATCH] add randsettings utility for saving settings across sessions --- pk3DS/Main.cs | 10 +++ pk3DS/Tools/RandSettings.cs | 135 ++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 pk3DS/Tools/RandSettings.cs diff --git a/pk3DS/Main.cs b/pk3DS/Main.cs index b522b7a..6d46bd6 100644 --- a/pk3DS/Main.cs +++ b/pk3DS/Main.cs @@ -20,6 +20,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using System.Threading; using System.Windows.Forms; @@ -54,6 +55,9 @@ public Main() string[] args = Environment.GetCommandLineArgs(); string filename = args.Length > 0 ? Path.GetFileNameWithoutExtension(args[0])?.ToLower() : ""; skipBoth = filename.IndexOf("3DSkip", StringComparison.Ordinal) >= 0; + + if (File.Exists(RandSettings.FileName)) + RandSettings.Load(File.ReadAllLines(RandSettings.FileName)); } internal static GameConfig Config; public static string RomFSPath; @@ -128,6 +132,12 @@ private void formClosing(object sender, FormClosingEventArgs e) string[][] files = Config.GameTextStrings; g.Files = files.Select(x => TextFile.getBytes(Config, x)).ToArray(); g.Save(); + + try + { + File.WriteAllLines(RandSettings.FileName, RandSettings.Save(), Encoding.Unicode); + } + catch { } } private void openQuick(string path) diff --git a/pk3DS/Tools/RandSettings.cs b/pk3DS/Tools/RandSettings.cs new file mode 100644 index 0000000..dd5c6fc --- /dev/null +++ b/pk3DS/Tools/RandSettings.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Windows.Forms; + +namespace pk3DS +{ + public static class RandSettings + { + public const string FileName = "randsettings.txt"; + private static readonly Dictionary> Settings = new Dictionary>(); + public static void Load(string[] lines) + { + Settings.Clear(); + int ctr = 0; + while (ctr < lines.Length) + { + string formname = lines[0]; + int end = Array.FindIndex(lines, ctr, string.IsNullOrWhiteSpace); + var list = GetList(lines, ctr + 1, end - 1); + Settings.Add(formname, list); + ctr = end + 1; + } + } + public static string[] Save() + { + var result = new List(); + foreach (var list in Settings) + { + result.Add(list.Key); + result.AddRange(list.Value.Select(val => val.Write())); + result.Add(string.Empty); + } + return result.ToArray(); + } + + public static void LoadFormSettings(Form form, Control.ControlCollection controls) + { + if (!Settings.TryGetValue(form.Name, out var list)) + return; + + foreach (Control ctrl in controls) + { + LoadFormSettings(form, ctrl.Controls); + var pair = list.FirstOrDefault(z => ctrl.Name == z.Name); + if (pair == null) + continue; + + TryGetValue(ctrl, pair.Value); + } + } + public static void SaveFormSettings(Form form, Control.ControlCollection controls) + { + if (!Settings.TryGetValue(form.Name, out var list)) + return; + + foreach (Control ctrl in controls) + { + SaveFormSettings(form, ctrl.Controls); + var pair = list.FirstOrDefault(z => ctrl.Name == z.Name) ?? new NameValue(ctrl.Name); + TrySetValue(ctrl, pair); + } + } + private static void TryGetValue(Control ctrl, string s) + { + switch (ctrl) + { + case NumericUpDown nud: + if (decimal.TryParse(s, out var n)) + nud.Value = n; + break; + case ComboBox cb: + if (int.TryParse(s, out var c)) + cb.SelectedIndex = c; + break; + case CheckBox ck: + if (bool.TryParse(s, out var b)) + ck.Checked = b; + break; + default: + System.Diagnostics.Debug.WriteLine($"{ctrl.Name}: unknown control type."); + break; + } + } + private static void TrySetValue(Control ctrl, NameValue v) + { + switch (ctrl) + { + case NumericUpDown nud: + v.Value = nud.Value.ToString(CultureInfo.InvariantCulture); + break; + case ComboBox cb: + v.Value = cb.SelectedIndex.ToString(); + break; + case CheckBox ck: + v.Value = ck.Checked.ToString(); + break; + default: + System.Diagnostics.Debug.WriteLine($"{ctrl.Name}: unknown control type."); + break; + } + } + + private static List GetList(IList lines, int start, int end) + { + var list = new List(); + for (int i = start; i <= end; i++) + { + var val = new NameValue(lines[i]); + if (val.Name != null) + list.Add(val); + } + return list; + } + + private class NameValue + { + public readonly string Name; + public string Value; + + private const char Separator = '\t'; + public string Write() => Name + Separator + Value; + + public NameValue(string s) + { + var split = s.Split(Separator); + Name = split[0]; + if (split.Length < 2) + return; + Value = split[1]; + } + } + } +}