using Avalonia; using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; using Avalonia.Media; using System.Collections.Generic; using System.Text.RegularExpressions; using Valheim_Dashboard.ViewModels; using Valheim_Dashboard.Views; using AvaloniaEdit.Highlighting; using AvaloniaEdit.Highlighting.Xshd; using System; namespace Valheim_Dashboard; public partial class App : Application { public override void Initialize() { AvaloniaXamlLoader.Load(this); } static HighlightingBrush HB(string key) { var obj = Application.Current!.FindResource(key); if (obj is ISolidColorBrush scb) return new SimpleHighlightingBrush(scb.Color); return new SimpleHighlightingBrush(Colors.LightGray); } private void RegisterCfgHighlighting() { var section = new HighlightingColor { Foreground = HB("Cfg.SectionBrush"), FontWeight = FontWeight.Bold }; var comment = new HighlightingColor { Foreground = HB("Cfg.CommentBrush"), FontStyle = FontStyle.Italic }; var keyCol = new HighlightingColor { Foreground = HB("Cfg.KeyBrush") }; var equal = new HighlightingColor { Foreground = HB("Cfg.EqualBrush") }; var value = new HighlightingColor { Foreground = HB("Cfg.ValueBrush") }; var number = new HighlightingColor { Foreground = HB("Cfg.NumberBrush") }; var rules = new HighlightingRuleSet(); // 0) Volle Kommentarzeilen rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"^[ \t]*[;#].*$", RegexOptions.Multiline), Color = comment }); // 1) Sektionen: [Section] rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"^\s*\[[^\]\r\n]+\]\s*$", RegexOptions.Multiline), Color = section }); // 2) Key (nur der Text vor '=' auf derselben Zeile) // - ignoriert führende Spaces (Lookbehind) // - endet direkt vor '=' (Lookahead) rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"(?<=^[ \t]*)[^=\r\n\[\];#][^=\r\n]*?(?=\s*=)", RegexOptions.Multiline), Color = keyCol }); // 3) '=' rot rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"=", RegexOptions.None), Color = equal }); // 4a) Zahlen im Value rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"(?<==\s*)(-?\d+(\.\d+)?)", RegexOptions.Multiline), Color = number }); // 4b) Bool-Werte im Value var boolVal = new HighlightingColor { Foreground = HB("Cfg.BoolBrush") }; rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"(?<==\s*)(On|Off|True|False)\b", RegexOptions.IgnoreCase | RegexOptions.Multiline), Color = boolVal }); // 4c) Allgemeine Werte (kein Zahl, kein Bool) rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"(?<==\s*)(?!On\b|Off\b|True\b|False\b)([A-Za-z_][^\r\n#;]*)", RegexOptions.IgnoreCase | RegexOptions.Multiline), Color = value }); // 6) Inline-Kommentare rechts vom Wert rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"[ \t][;#].*$", RegexOptions.Multiline), Color = comment }); var def = new InlineHighlightingDefinition("CfgDark", rules); HighlightingManager.Instance.RegisterHighlighting("CfgDark", new[] { ".cfg", ".properties" }, def); } sealed class InlineHighlightingDefinition : IHighlightingDefinition { public InlineHighlightingDefinition(string name, HighlightingRuleSet rules) { Name = name; MainRuleSet = rules; } public string Name { get; } public HighlightingRuleSet MainRuleSet { get; } public IEnumerable NamedHighlightingColors => Array.Empty(); public IDictionary Properties => new Dictionary(); public HighlightingRuleSet? GetNamedRuleSet(string name) => null; public HighlightingColor? GetNamedColor(string name) => null; } public override void OnFrameworkInitializationCompleted() { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { // ViewModelLocator im Ressourcenverzeichnis registrieren Resources.Add("Locator", new ViewModelLocator()); desktop.MainWindow = new MainWindow { DataContext = (Resources["Locator"] as ViewModelLocator)?.MainWindowViewModel, }; } RegisterCfgHighlighting(); base.OnFrameworkInitializationCompleted(); } }