|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Xml; |
| 5 | +using System.Xml.Serialization; |
| 6 | +using YAMDCC.Common.Configs; |
| 7 | + |
| 8 | +namespace YAMDCC.HotkeyHandler.Config; |
| 9 | + |
| 10 | +public class HotkeyConf |
| 11 | +{ |
| 12 | + [XmlAttribute] |
| 13 | + public int Ver { get; set; } = 1; |
| 14 | + |
| 15 | + [XmlIgnore] |
| 16 | + private static readonly int ExpectedVer = 1; |
| 17 | + |
| 18 | + [XmlArray] |
| 19 | + public List<Hotkey> Hotkeys { get; set; } = []; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Parses a hotkey config XML and returns a |
| 23 | + /// <see cref="HotkeyConf"/> object. |
| 24 | + /// </summary> |
| 25 | + /// <param name="path"> |
| 26 | + /// The path to an XML config file. |
| 27 | + /// </param> |
| 28 | + /// <exception cref="InvalidConfigException"/> |
| 29 | + /// <exception cref="ArgumentNullException"/> |
| 30 | + /// <exception cref="FileNotFoundException"/> |
| 31 | + /// <exception cref="InvalidOperationException"/> |
| 32 | + public static HotkeyConf Load(string path) |
| 33 | + { |
| 34 | + XmlSerializer serialiser = new(typeof(HotkeyConf)); |
| 35 | + using (XmlReader reader = XmlReader.Create(path)) |
| 36 | + { |
| 37 | + HotkeyConf cfg = (HotkeyConf)serialiser.Deserialize(reader); |
| 38 | + return cfg.IsValid() ? cfg : throw new InvalidConfigException(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Saves a hotkey config to the specified location. |
| 44 | + /// </summary> |
| 45 | + /// <param name="path"> |
| 46 | + /// The XML file to write to. |
| 47 | + /// </param> |
| 48 | + /// <exception cref="ArgumentNullException"/> |
| 49 | + /// <exception cref="InvalidOperationException"/> |
| 50 | + public void Save(string path) |
| 51 | + { |
| 52 | + XmlSerializer serialiser = new(typeof(HotkeyConf)); |
| 53 | + XmlWriterSettings settings = new() |
| 54 | + { |
| 55 | + Indent = true, |
| 56 | + IndentChars = "\t", |
| 57 | + }; |
| 58 | + |
| 59 | + using (XmlWriter writer = XmlWriter.Create(path, settings)) |
| 60 | + { |
| 61 | + serialiser.Serialize(writer, this); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Performs some validation on the loaded config to make |
| 67 | + /// sure it is in the expected format. |
| 68 | + /// </summary> |
| 69 | + /// <remarks> |
| 70 | + /// This does NOT guarantee the loaded config is valid! |
| 71 | + /// (e.g. register values are not checked) |
| 72 | + /// </remarks> |
| 73 | + /// <returns> |
| 74 | + /// <see langword="true"/> if the config is valid, otherwise <see langword="false"/>. |
| 75 | + /// </returns> |
| 76 | + private bool IsValid() |
| 77 | + { |
| 78 | + // Check the config version. |
| 79 | + // Pretty self-explanatory, if the loaded config is older/newer |
| 80 | + // than the version expected by the config library, don't bother |
| 81 | + // checking anything else as some/all of it is probably invalid. |
| 82 | + if (Ver != ExpectedVer) |
| 83 | + { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + // All other values are considered to be valid; return true. |
| 88 | + return true; |
| 89 | + } |
| 90 | +} |
0 commit comments