|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.Xml.Linq; |
| 5 | + |
| 6 | +namespace ReClassNET.Util |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// A class which stores custom config items from plugins. |
| 10 | + /// The id of an item should consist of "a-zA-z0-9.,;_-+". |
| 11 | + /// The naming convention is "Plugin.[ConfigGroup.]ConfigItem". |
| 12 | + /// </summary> |
| 13 | + public class CustomConfig |
| 14 | + { |
| 15 | + private readonly Dictionary<string, string> data = new Dictionary<string, string>(); |
| 16 | + |
| 17 | + internal XElement Serialize(string name) |
| 18 | + { |
| 19 | + return XElementSerializer.ToXml(name, data); |
| 20 | + } |
| 21 | + |
| 22 | + internal void Deserialize(XElement element) |
| 23 | + { |
| 24 | + foreach (var kv in XElementSerializer.ToDictionary(element)) |
| 25 | + { |
| 26 | + data[kv.Key] = kv.Value; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Sets a configuration item. |
| 32 | + /// </summary> |
| 33 | + /// <param name="id">The id of the item.</param> |
| 34 | + /// <param name="value"> |
| 35 | + /// The value of the item. |
| 36 | + /// If the value is null the item gets removed. |
| 37 | + /// </param> |
| 38 | + public void SetString(string id, string value) |
| 39 | + { |
| 40 | + if (id == null) |
| 41 | + { |
| 42 | + throw new ArgumentNullException(nameof(id)); |
| 43 | + } |
| 44 | + if (id.Length == 0) |
| 45 | + { |
| 46 | + throw new ArgumentException(); |
| 47 | + } |
| 48 | + |
| 49 | + if (value == null) |
| 50 | + { |
| 51 | + data.Remove(id); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + data[id] = value; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Sets a configuration item. |
| 61 | + /// </summary> |
| 62 | + /// <param name="id">The id of the item.</param> |
| 63 | + /// <param name="value">The value of the item.</param> |
| 64 | + public void SetBool(string id, bool value) |
| 65 | + { |
| 66 | + SetString(id, Convert.ToString(value)); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Sets a configuration item. |
| 71 | + /// </summary> |
| 72 | + /// <param name="id">The id of the item.</param> |
| 73 | + /// <param name="value">The value of the item.</param> |
| 74 | + public void SetLong(string id, long value) |
| 75 | + { |
| 76 | + SetString(id, value.ToString(NumberFormatInfo.InvariantInfo)); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Sets a configuration item. |
| 81 | + /// </summary> |
| 82 | + /// <param name="id">The id of the item.</param> |
| 83 | + /// <param name="value">The value of the item.</param> |
| 84 | + public void SetULong(string id, ulong value) |
| 85 | + { |
| 86 | + SetString(id, value.ToString(NumberFormatInfo.InvariantInfo)); |
| 87 | + } |
| 88 | + |
| 89 | + public void SetXElement(string id, XElement value) |
| 90 | + { |
| 91 | + SetString(id, value.ToString()); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Gets the value of the config item. |
| 96 | + /// </summary> |
| 97 | + /// <param name="id">The id of the item.</param> |
| 98 | + /// <returns>The value of the config item or null if the id does not exists.</returns> |
| 99 | + public string GetString(string id) |
| 100 | + { |
| 101 | + return GetString(id, null); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Gets the value of the config item. |
| 106 | + /// </summary> |
| 107 | + /// <param name="id">The id of the item.</param> |
| 108 | + /// <param name="def">The default value if the id does not exists.</param> |
| 109 | + /// <returns>The value of the config item or <paramref name="def"/> if the id does not exists.</returns> |
| 110 | + public string GetString(string id, string def) |
| 111 | + { |
| 112 | + if (id == null) |
| 113 | + { |
| 114 | + throw new ArgumentNullException(nameof(id)); |
| 115 | + } |
| 116 | + if (id.Length == 0) |
| 117 | + { |
| 118 | + throw new ArgumentException(); |
| 119 | + } |
| 120 | + |
| 121 | + if (data.TryGetValue(id, out var value)) |
| 122 | + { |
| 123 | + return value; |
| 124 | + } |
| 125 | + |
| 126 | + return def; |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Gets the value of the config item. |
| 131 | + /// </summary> |
| 132 | + /// <param name="id">The id of the item.</param> |
| 133 | + /// <param name="def">The default value if the id does not exists.</param> |
| 134 | + /// <returns>The value of the config item or <paramref name="def"/> if the id does not exists.</returns> |
| 135 | + public bool GetBool(string id, bool def) |
| 136 | + { |
| 137 | + var value = GetString(id, null); |
| 138 | + if (string.IsNullOrEmpty(value)) |
| 139 | + { |
| 140 | + return def; |
| 141 | + } |
| 142 | + |
| 143 | + return Convert.ToBoolean(value); |
| 144 | + } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// Gets the value of the config item. |
| 148 | + /// </summary> |
| 149 | + /// <param name="id">The id of the item.</param> |
| 150 | + /// <param name="def">The default value if the id does not exists.</param> |
| 151 | + /// <returns>The value of the config item or <paramref name="def"/> if the id does not exists.</returns> |
| 152 | + public long GetLong(string id, long def) |
| 153 | + { |
| 154 | + var str = GetString(id, null); |
| 155 | + if (string.IsNullOrEmpty(str)) |
| 156 | + { |
| 157 | + return def; |
| 158 | + } |
| 159 | + |
| 160 | + if (long.TryParse(str, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var value)) |
| 161 | + { |
| 162 | + return value; |
| 163 | + } |
| 164 | + |
| 165 | + return def; |
| 166 | + } |
| 167 | + |
| 168 | + /// <summary> |
| 169 | + /// Gets the value of the config item. |
| 170 | + /// </summary> |
| 171 | + /// <param name="id">The id of the item.</param> |
| 172 | + /// <param name="def">The default value if the id does not exists.</param> |
| 173 | + /// <returns>The value of the config item or <paramref name="def"/> if the id does not exists.</returns> |
| 174 | + public ulong GetULong(string id, ulong def) |
| 175 | + { |
| 176 | + var str = GetString(id, null); |
| 177 | + if (string.IsNullOrEmpty(str)) |
| 178 | + { |
| 179 | + return def; |
| 180 | + } |
| 181 | + |
| 182 | + if (ulong.TryParse(str, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var value)) |
| 183 | + { |
| 184 | + return value; |
| 185 | + } |
| 186 | + |
| 187 | + return def; |
| 188 | + } |
| 189 | + |
| 190 | + /// <summary> |
| 191 | + /// Gets the value of the config item. |
| 192 | + /// </summary> |
| 193 | + /// <param name="id">The id of the item.</param> |
| 194 | + /// <param name="def">The default value if the id does not exists.</param> |
| 195 | + /// <returns>The value of the config item or <paramref name="def"/> if the id does not exists.</returns> |
| 196 | + public XElement GetXElement(string id, XElement def) |
| 197 | + { |
| 198 | + var str = GetString(id, null); |
| 199 | + if (string.IsNullOrEmpty(str)) |
| 200 | + { |
| 201 | + return def; |
| 202 | + } |
| 203 | + |
| 204 | + return XElement.Parse(str); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments