| 1 | using CascadeIDE.Contracts; |
| 2 | namespace CascadeIDE.Features.Settings.DataAcquisition; |
| 3 | |
| 4 | /// <summary> |
| 5 | /// DAL: merged-словарь из штатного <c>Hotkeys/hotkeys.toml</c> и |
| 6 | /// <c>%LocalAppData%\CascadeIDE\hotkeys.toml</c> (оверлей поверх). |
| 7 | /// </summary> |
| 8 | [IoBoundary] |
| 9 | public static class HotkeyTomlLoader |
| 10 | { |
| 11 | public static Dictionary<string, string> LoadMergedDictionary() |
| 12 | { |
| 13 | var merged = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 14 | var basePath = UserSettingsPaths.GetBundledHotkeysFilePath(); |
| 15 | MergeFromFile(merged, basePath, "Hotkeys/hotkeys.toml"); |
| 16 | var userPath = UserSettingsPaths.GetHotkeysUserFilePath(); |
| 17 | MergeFromFile(merged, userPath, embeddedRelativeFallback: null); |
| 18 | return merged; |
| 19 | } |
| 20 | |
| 21 | internal static void MergeFromFile( |
| 22 | Dictionary<string, string> target, |
| 23 | string path, |
| 24 | string? embeddedRelativeFallback = null) |
| 25 | { |
| 26 | string? text = null; |
| 27 | if (File.Exists(path)) |
| 28 | text = File.ReadAllText(path); |
| 29 | else if (embeddedRelativeFallback is not null && BundledAppContent.TryReadEmbeddedText(embeddedRelativeFallback, out var emb)) |
| 30 | text = emb; |
| 31 | if (text is null) |
| 32 | return; |
| 33 | try |
| 34 | { |
| 35 | var parsed = CascadeTomlSerializer.Deserialize<Dictionary<string, string>>(text); |
| 36 | if (parsed is null) |
| 37 | return; |
| 38 | foreach (var kv in parsed) |
| 39 | target[kv.Key] = kv.Value.Trim(); |
| 40 | } |
| 41 | catch |
| 42 | { |
| 43 | // игнорируем битый файл — остаётся предыдущий мердж |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |