using System.Xml.Serialization; using AtomicLib.XML; namespace AtomicLib; /// /// Class that handles saving and loading the modpacker configuration. /// [Serializable] [XmlRoot("config")] public class Config { public static readonly string ConfigFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create), "Atomic", "config.xml"); /// /// Language used for the modpacker. /// [XmlAttribute("Language")] public string Language { get; set; } /// /// Determines if the modpacker should remember information entered into the fields between usages. /// [XmlAttribute("FillInContents")] public bool FillInContents { get; set; } /// /// Used to save information from the fields. Only used when is /// [XmlElement("Fields")] public FieldContents Fields { get; set; } public Config() { } public Config(string language, bool fillIn) { Language = language; FillInContents = fillIn; Fields = new FieldContents(); } /// /// Reads the configuration from disk and returns a object. If the configuration does not exist, a default configuration will be created. /// /// Returns a object containing either the configuration read from disk or the default one. public static Config LoadAndReturnConfig() { if (!File.Exists(ConfigFilePath)) return CreateAndReturnDefaultConfig(); return Serializer.Deserialize(File.ReadAllText(ConfigFilePath)); } /// /// Saves a default configuration to disk and returns it, creating the config folders if necessary. /// /// Returns the object. public static Config CreateAndReturnDefaultConfig() { Directory.CreateDirectory(Path.GetDirectoryName(ConfigFilePath)); Config defaultConfig = new Config("SystemLanguage", true); SaveConfig(defaultConfig); return defaultConfig; } /// /// Writes the to disk. /// /// The that should be saved to the disk. public static void SaveConfig(Config config) { string xmlOutput = Serializer.Serialize(config); File.WriteAllText(ConfigFilePath, xmlOutput); } }