using System; using System.Xml; using BetterPatchFileCreator.XAML; namespace BetterPatchFileCreator.Code { public static class DataProvider { private const string ConfigsPath = "Settings.xml"; public static string BasePath = "C:\\Patches"; public static MainWindow MainWindowInstance; public static PatchConfigCollection PatchConfigs { get; private set; } public static DeployConfigCollection DeployConfigs { get; private set; } public static void Save() { XmlDocument doc = new XmlDocument(); XmlNode root = doc.CreateElement("Settings"); doc.AppendChild(root); AddConfigNodes(root, doc); AddSettingsNodes(root, doc); doc.Save(ConfigsPath); } private static void AddSettingsNodes(XmlNode root, XmlDocument doc) { XmlNode node = doc.CreateElement("RootDir"); node.InnerText = BasePath; root.AppendChild(node); } private static void AddConfigNodes(XmlNode root, XmlDocument source) { root.AppendChild(PatchConfigs.ToXmlNode(source)); root.AppendChild(DeployConfigs.ToXmlNode(source)); } public static void Intialize() { PatchConfigs = new PatchConfigCollection(); DeployConfigs = new DeployConfigCollection(); ParseSettings(); } private static void ParseSettings() { XmlDocument doc = new XmlDocument(); doc.Load(ConfigsPath); XmlNode root = doc; foreach (XmlNode node in root.ChildNodes) { if (node.Name == "Settings") { root = node; break; } } if(root.Name != "Settings") throw new InvalidOperationException(); ParseXmlNodes(root); } private static void ParseXmlNodes(XmlNode node) { switch (node.Name) { case "PatchConfigs": PatchConfigs.ParseFromXmlNode(node); break; case "DeployConfigs": DeployConfigs.LoadFromXmlNode(node); break; case "RootDir": BasePath = node.InnerText; break; default: foreach (XmlNode n in node.ChildNodes) { ParseXmlNodes(n); } break; } } } }