From 9fc36ee19948dee87f509a8cd66bdfa26d520ea0 Mon Sep 17 00:00:00 2001 From: "April P." Date: Fri, 13 Dec 2024 12:53:06 -0800 Subject: [PATCH] Create base configuration builder --- MistWX-i2Me/Config.cs | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 MistWX-i2Me/Config.cs diff --git a/MistWX-i2Me/Config.cs b/MistWX-i2Me/Config.cs new file mode 100644 index 0000000..2f515b1 --- /dev/null +++ b/MistWX-i2Me/Config.cs @@ -0,0 +1,54 @@ +using System.Xml.Serialization; + +namespace MistWX_i2Me; + +/// +/// Class for building the config.xml file used to configure the software +/// +[XmlRoot("Config")] +public class Config +{ + [XmlElement] public string TwcApiKey { get; set; } = "REPLACE_ME"; + + // Used to process what locations to generate + [XmlElement] + public string MachineProductConfig { get; set; } = + "C:\\Program Files (x86)\\TWC\\i2\\managed\\MachineProductConfig.xml"; + + + public static Config config = new Config(); + + + /// + /// Loads values from the configuration file into the software + /// + /// Config object + public static Config Load() + { + string path = Path.Combine(AppContext.BaseDirectory, "config.xml"); + XmlSerializer serializer = new XmlSerializer(typeof(Config)); + + // Create a base config if none exists + if (!File.Exists(path)) + { + config = new Config(); + serializer.Serialize(File.Create(path), config); + + return config; + } + + using (FileStream stream = new FileStream(path, FileMode.Open)) + { + var deserializedConfig = serializer.Deserialize(stream); + + if (deserializedConfig != null && deserializedConfig is Config cfg) + { + config = cfg; + return config; + } + + return new Config(); + } + + } +} \ No newline at end of file