| 1 | namespace CascadeIDE.Cockpit.DataBus; |
| 2 | |
| 3 | /// <summary>Загрузка <see cref="DataBusEventPolicy"/> из embedded TOML в манифесте сборки (без дискового оверлея).</summary> |
| 4 | public static class DataBusEventPolicyLoader |
| 5 | { |
| 6 | /// <summary>Путь внутри манифеста <c>CascadeIDE</c> (см. <c>EmbeddedResource</c> в csproj).</summary> |
| 7 | public const string BundledRelativePath = "Cockpit/DataBus/databus-event-policy.toml"; |
| 8 | |
| 9 | private sealed class EventPolicyTomlRoot |
| 10 | { |
| 11 | public Dictionary<string, string>? Events { get; set; } |
| 12 | } |
| 13 | |
| 14 | /// <summary> |
| 15 | /// Читает манифест встроенного TOML (байты поставляются вместе со сборкой). Исключение — только баг: неверный |
| 16 | /// <c>EmbeddedResource</c> в csproj либо неверное содержимое исходного файла, см. <see cref="BundledRelativePath"/>. |
| 17 | /// </summary> |
| 18 | public static DataBusEventPolicy Load() |
| 19 | { |
| 20 | if (!BundledAppContent.TryReadEmbeddedText(BundledRelativePath, out var text) || string.IsNullOrWhiteSpace(text)) |
| 21 | throw new InvalidOperationException( |
| 22 | $"Embedded resource not in assembly manifest: {BundledRelativePath} (check EmbeddedResource in CascadeIDE.csproj)."); |
| 23 | |
| 24 | if (!TryParse(text, out var policy)) |
| 25 | throw new InvalidOperationException( |
| 26 | $"Fix {BundledRelativePath} in the repo: expected [events] with values burst or reliable."); |
| 27 | |
| 28 | return policy; |
| 29 | } |
| 30 | |
| 31 | /// <summary>Для тестов: разбор TOML без I/O.</summary> |
| 32 | internal static bool TryParse(string toml, out DataBusEventPolicy policy) |
| 33 | { |
| 34 | policy = default; |
| 35 | try |
| 36 | { |
| 37 | var root = CascadeTomlSerializer.Deserialize<EventPolicyTomlRoot>(toml.Trim()); |
| 38 | if (root?.Events is not { Count: > 0 }) |
| 39 | return false; |
| 40 | |
| 41 | var map = new Dictionary<string, bool>(StringComparer.Ordinal); |
| 42 | foreach (var kv in root.Events) |
| 43 | { |
| 44 | var name = kv.Key?.Trim(); |
| 45 | if (string.IsNullOrEmpty(name)) |
| 46 | continue; |
| 47 | var mode = kv.Value?.Trim(); |
| 48 | if (string.IsNullOrEmpty(mode)) |
| 49 | continue; |
| 50 | if (string.Equals(mode, "burst", StringComparison.OrdinalIgnoreCase)) |
| 51 | map[name] = true; |
| 52 | else if (string.Equals(mode, "reliable", StringComparison.OrdinalIgnoreCase)) |
| 53 | map[name] = false; |
| 54 | } |
| 55 | |
| 56 | if (map.Count == 0) |
| 57 | return false; |
| 58 | policy = new DataBusEventPolicy(map); |
| 59 | return true; |
| 60 | } |
| 61 | catch |
| 62 | { |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |