|
| 1 | +/* |
| 2 | + This file is part of Module Manager Watch Dog |
| 3 | + ©2020-2023 Lisias T : http://lisias.net <support@lisias.net> |
| 4 | + |
| 5 | + Module Manager Watch Dog is licensed as follows: |
| 6 | + * SKL 1.0 : https://ksp.lisias.net/SKL-1_0.txt |
| 7 | + |
| 8 | + Module Manager Watchdog is distributed in the hope that it will be useful, |
| 9 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | + |
| 12 | + You should have received a copy of the SKL Standard License 1.0 |
| 13 | + along with Module Manager Watch Dog. If not, see |
| 14 | + <https://ksp.lisias.net/SKL-1_0.txt>. |
| 15 | + |
| 16 | +*/ |
| 17 | +using System; |
| 18 | +using SIO = System.IO; |
| 19 | + |
| 20 | +namespace ModuleManagerWatchDog |
| 21 | +{ |
| 22 | + public class Globals |
| 23 | + { |
| 24 | + private static Globals INSTANCE = null; |
| 25 | + public static Globals Instance => INSTANCE ?? (INSTANCE = new Globals()); |
| 26 | + |
| 27 | + private const string NODE = "ModuleManagerWatchDog"; |
| 28 | + private const string DATAFILE = NODE + ".cfg"; |
| 29 | + private readonly string ROOT = SIO.Path.Combine(KSPUtil.ApplicationRootPath, "PluginData"); |
| 30 | + |
| 31 | + internal bool IsValid; |
| 32 | + private bool dirty = false; |
| 33 | + |
| 34 | + private bool prefersMyFork = false; |
| 35 | + public bool PrefersMyFork { |
| 36 | + get => this.prefersMyFork; |
| 37 | + internal set |
| 38 | + { |
| 39 | + this.dirty = true; |
| 40 | + this.prefersMyFork = value; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private Globals() |
| 45 | + { |
| 46 | + string path = SIO.Path.Combine(ROOT, DATAFILE); |
| 47 | + this.dirty = false; |
| 48 | + bool r = true; |
| 49 | + try |
| 50 | + { |
| 51 | + ConfigNode cn = ConfigNode.Load(path).GetNode(NODE); |
| 52 | + r &= this.prefersMyFork = cn.TryGetValue("PrefersMyFork", ref this.prefersMyFork); |
| 53 | + } |
| 54 | + catch (Exception) |
| 55 | + { |
| 56 | + this.prefersMyFork = false; |
| 57 | + r = false; |
| 58 | + } |
| 59 | + this.IsValid = r; |
| 60 | + } |
| 61 | + |
| 62 | + internal void Save() |
| 63 | + { |
| 64 | + if (!this.dirty) return; |
| 65 | + string path = SIO.Path.Combine(ROOT, DATAFILE); |
| 66 | + Log.force("Writing {0}", path); |
| 67 | + try |
| 68 | + { |
| 69 | + ConfigNode cn = new ConfigNode(NODE); |
| 70 | + ConfigNode ccn = cn.AddNode(NODE); |
| 71 | + ccn.SetValue("PrefersMyFork", this.prefersMyFork, true); |
| 72 | + cn.Save(path); |
| 73 | + } |
| 74 | + catch (Exception e) |
| 75 | + { |
| 76 | + Log.error("Could not write {0} due {1}!", path, e.Message); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments