|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using HUDMerger.Extensions; |
| 6 | +using VDF; |
| 7 | +using VDF.Models; |
| 8 | + |
| 9 | +namespace HUDMerger.Models.Scheme; |
| 10 | + |
| 11 | +public abstract class SchemeBase : IScheme |
| 12 | +{ |
| 13 | + public abstract string Type { get; } |
| 14 | + |
| 15 | + private readonly Dictionary<KeyValue, string?> Colours = new(KeyValueComparer.KeyComparer); |
| 16 | + private readonly Dictionary<KeyValue, dynamic> Borders = new(KeyValueComparer.KeyComparer); |
| 17 | + private readonly Dictionary<KeyValue, HashSet<KeyValue>?> Fonts = new(KeyValueComparer.KeyComparer); |
| 18 | + |
| 19 | + public List<KeyValue> CustomFontFiles { get; } = []; |
| 20 | + |
| 21 | + private record class SchemeFile |
| 22 | + { |
| 23 | + public readonly Dictionary<KeyValue, string?> Colours = new(KeyValueComparer.KeyComparer); |
| 24 | + public readonly Dictionary<KeyValue, dynamic> Borders = new(KeyValueComparer.KeyComparer); |
| 25 | + public readonly Dictionary<KeyValue, HashSet<KeyValue>?> Fonts = new(KeyValueComparer.KeyComparer); |
| 26 | + public readonly List<KeyValue> CustomFontFiles = []; |
| 27 | + } |
| 28 | + |
| 29 | + public SchemeBase() |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + public SchemeBase(string folderPath) |
| 34 | + { |
| 35 | + static SchemeFile? ReadBaseFile(FileInfo file) |
| 36 | + { |
| 37 | + if (!file.Exists) return null; |
| 38 | + |
| 39 | + KeyValues keyValues = VDFSerializer.Deserialize(File.ReadAllText(file.FullName)); |
| 40 | + KeyValues header = keyValues.Header(); |
| 41 | + |
| 42 | + SchemeFile scheme = new(); |
| 43 | + |
| 44 | + IEnumerable<KeyValue> colours = header |
| 45 | + .Where((kv) => StringComparer.OrdinalIgnoreCase.Equals(kv.Key, "Colors") && kv.Value is KeyValues) |
| 46 | + .SelectMany((kv) => (KeyValues)kv.Value); |
| 47 | + foreach (KeyValue colour in colours) |
| 48 | + { |
| 49 | + scheme.Colours.TryAdd(colour, colour.Value is string value ? value : null); |
| 50 | + } |
| 51 | + |
| 52 | + IEnumerable<KeyValue> borders = header |
| 53 | + .Where((kv) => StringComparer.OrdinalIgnoreCase.Equals(kv.Key, "Borders") && kv.Value is KeyValues) |
| 54 | + .SelectMany((kv) => (KeyValues)kv.Value); |
| 55 | + foreach (KeyValue border in borders) |
| 56 | + { |
| 57 | + if (scheme.Borders.TryGetValue(border, out dynamic? value)) |
| 58 | + { |
| 59 | + if (value is HashSet<KeyValue> existingBorder && border.Value is KeyValues borderValues) |
| 60 | + { |
| 61 | + existingBorder.UnionWithRecursive(borderValues); |
| 62 | + } |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + scheme.Borders[border] = border.Value switch |
| 67 | + { |
| 68 | + string borderReference => borderReference, |
| 69 | + KeyValues borderValues => borderValues.ToHashSet(), |
| 70 | + _ => throw new NotSupportedException() |
| 71 | + }; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + IEnumerable<KeyValue> fonts = header |
| 76 | + .Where((kv) => StringComparer.OrdinalIgnoreCase.Equals(kv.Key, "Fonts") && kv.Value is KeyValues) |
| 77 | + .SelectMany((kv) => (KeyValues)kv.Value); |
| 78 | + foreach (KeyValue font in fonts) |
| 79 | + { |
| 80 | + if (scheme.Fonts.TryGetValue(font, out HashSet<KeyValue>? value)) |
| 81 | + { |
| 82 | + if (value is HashSet<KeyValue> existingFont && font.Value is KeyValues fontValues) |
| 83 | + { |
| 84 | + existingFont.UnionWithRecursive(fontValues); |
| 85 | + } |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + scheme.Fonts[font] = font.Value switch |
| 90 | + { |
| 91 | + KeyValues values => values.ToHashSet(), |
| 92 | + string => null, |
| 93 | + _ => throw new NotSupportedException(), |
| 94 | + }; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + IEnumerable<KeyValue> customFontFiles = header |
| 99 | + .Where((kv) => StringComparer.OrdinalIgnoreCase.Equals(kv.Key, "CustomFontFiles") && kv.Value is KeyValues) |
| 100 | + .SelectMany((kv) => (KeyValues)kv.Value); |
| 101 | + foreach (KeyValue font in fonts) |
| 102 | + { |
| 103 | + scheme.CustomFontFiles.Add(font); |
| 104 | + } |
| 105 | + |
| 106 | + foreach (string baseFile in keyValues.BaseFiles()) |
| 107 | + { |
| 108 | + SchemeFile? baseScheme = ReadBaseFile(new FileInfo(Path.Join(file.DirectoryName, baseFile))); |
| 109 | + if (baseScheme == null) continue; |
| 110 | + |
| 111 | + foreach (KeyValuePair<KeyValue, string?> colour in baseScheme.Colours) |
| 112 | + { |
| 113 | + scheme.Colours.TryAdd(colour.Key, colour.Value); |
| 114 | + } |
| 115 | + |
| 116 | + foreach (KeyValuePair<KeyValue, dynamic> border in baseScheme.Borders) |
| 117 | + { |
| 118 | + if (scheme.Borders.TryGetValue(border.Key, out dynamic? value)) |
| 119 | + { |
| 120 | + if (value is HashSet<KeyValue> existingBorder && border.Value is IEnumerable<KeyValue> borderValues) |
| 121 | + { |
| 122 | + existingBorder.UnionWithRecursive(borderValues); |
| 123 | + } |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + scheme.Borders[border.Key] = border.Value; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + foreach (KeyValuePair<KeyValue, HashSet<KeyValue>?> font in baseScheme.Fonts) |
| 132 | + { |
| 133 | + if (scheme.Borders.TryGetValue(font.Key, out dynamic? value)) |
| 134 | + { |
| 135 | + if (value is HashSet<KeyValue> existingFont && font.Value is IEnumerable<KeyValue> fontValues) |
| 136 | + { |
| 137 | + existingFont.UnionWithRecursive(fontValues); |
| 138 | + } |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + scheme.Fonts[font.Key] = font.Value; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + foreach (KeyValue customFontFile in baseScheme.CustomFontFiles) |
| 147 | + { |
| 148 | + scheme.CustomFontFiles.Add(customFontFile); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return scheme; |
| 153 | + } |
| 154 | + |
| 155 | + static KeyValues GetValueOrDefault(KeyValues keyValues, string key) => |
| 156 | + keyValues.FirstOrDefault((kv) => StringComparer.OrdinalIgnoreCase.Equals(kv.Key, key)).Value is KeyValues v ? v : []; |
| 157 | + |
| 158 | + string schemePath = Path.Join(folderPath, $"resource/{Type}scheme.res"); |
| 159 | + |
| 160 | + KeyValues keyValues = VDFSerializer.Deserialize(File.ReadAllText(File.Exists(schemePath) ? schemePath : $"Resources\\HUD\\resource\\{Type}scheme.res")); |
| 161 | + |
| 162 | + KeyValues header = keyValues.Header(); |
| 163 | + |
| 164 | + foreach (KeyValue colour in GetValueOrDefault(header, "Colors")) |
| 165 | + { |
| 166 | + Colours.TryAdd(colour, colour.Value is string value ? value : null); |
| 167 | + } |
| 168 | + |
| 169 | + foreach (KeyValue border in GetValueOrDefault(header, "Borders")) |
| 170 | + { |
| 171 | + Borders.TryAdd( |
| 172 | + border, |
| 173 | + border.Value switch |
| 174 | + { |
| 175 | + string borderReference => borderReference, |
| 176 | + KeyValues borderValues => borderValues.ToHashSet(), |
| 177 | + _ => throw new NotSupportedException(), |
| 178 | + } |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + foreach (KeyValue font in GetValueOrDefault(header, "Fonts")) |
| 183 | + { |
| 184 | + Fonts.TryAdd(font, font.Value is KeyValues value ? value.ToHashSet() : null); |
| 185 | + } |
| 186 | + |
| 187 | + foreach (KeyValue customFontFile in GetValueOrDefault(header, "CustomFontFiles")) |
| 188 | + { |
| 189 | + CustomFontFiles.Add(customFontFile); |
| 190 | + } |
| 191 | + |
| 192 | + foreach (string baseFile in keyValues.BaseFiles()) |
| 193 | + { |
| 194 | + SchemeFile? baseScheme = ReadBaseFile(new FileInfo(Path.Join(Path.GetDirectoryName(schemePath), baseFile))); |
| 195 | + if (baseScheme == null) continue; |
| 196 | + |
| 197 | + foreach (KeyValuePair<KeyValue, string?> colour in baseScheme.Colours) |
| 198 | + { |
| 199 | + Colours.TryAdd(colour.Key, colour.Value); |
| 200 | + } |
| 201 | + |
| 202 | + foreach (KeyValuePair<KeyValue, dynamic> border in baseScheme.Borders) |
| 203 | + { |
| 204 | + if (Borders.TryGetValue(border.Key, out dynamic? value)) |
| 205 | + { |
| 206 | + if (value is HashSet<KeyValue> existingBorder && border.Value is IEnumerable<KeyValue> baseBorder) |
| 207 | + { |
| 208 | + existingBorder.UnionWithRecursive(baseBorder); |
| 209 | + } |
| 210 | + } |
| 211 | + else |
| 212 | + { |
| 213 | + Borders[border.Key] = border.Value; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + foreach (KeyValuePair<KeyValue, HashSet<KeyValue>?> font in baseScheme.Fonts) |
| 218 | + { |
| 219 | + if (Fonts.TryGetValue(font.Key, out HashSet<KeyValue>? value)) |
| 220 | + { |
| 221 | + if (value is HashSet<KeyValue> existingFont && font.Value is IEnumerable<KeyValue> baseFont) |
| 222 | + { |
| 223 | + existingFont.UnionWithRecursive(baseFont); |
| 224 | + } |
| 225 | + } |
| 226 | + else |
| 227 | + { |
| 228 | + Fonts[font.Key] = font.Value; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + foreach (KeyValue customFontFile in baseScheme.CustomFontFiles) |
| 233 | + { |
| 234 | + CustomFontFiles.Add(customFontFile); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + public IEnumerable<KeyValue> GetColour(string colourName) |
| 240 | + { |
| 241 | + return Colours |
| 242 | + .Where((colour) => StringComparer.OrdinalIgnoreCase.Equals(colour.Key.Key, colourName) && colour.Value != null) |
| 243 | + .Select((colour) => new KeyValue |
| 244 | + { |
| 245 | + Key = colour.Key.Key, |
| 246 | + Value = colour.Value!, |
| 247 | + Conditional = colour.Key.Conditional |
| 248 | + }); |
| 249 | + } |
| 250 | + |
| 251 | + public IEnumerable<KeyValue> GetBorder(string borderName) |
| 252 | + { |
| 253 | + return Borders |
| 254 | + .Where((border) => StringComparer.OrdinalIgnoreCase.Equals(border.Key.Key, borderName) && border.Value != null) |
| 255 | + .Select((border) => new KeyValue |
| 256 | + { |
| 257 | + Key = border.Key.Key, |
| 258 | + Value = border.Value!, |
| 259 | + Conditional = border.Key.Conditional |
| 260 | + }); |
| 261 | + } |
| 262 | + |
| 263 | + public IEnumerable<KeyValue> GetFont(string fontName) |
| 264 | + { |
| 265 | + return Fonts |
| 266 | + .Where((font) => StringComparer.OrdinalIgnoreCase.Equals(font.Key.Key, fontName) && font.Value != null) |
| 267 | + .Select((font) => new KeyValue |
| 268 | + { |
| 269 | + Key = font.Key.Key, |
| 270 | + Value = font.Value!, |
| 271 | + Conditional = font.Key.Conditional |
| 272 | + }); |
| 273 | + } |
| 274 | +} |
0 commit comments