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); } private void RegisterCfgHighlighting() { static HighlightingBrush HB(string key) { var obj = Application.Current!.FindResource(key); if (obj is ISolidColorBrush scb) return new SimpleHighlightingBrush(scb.Color); // <-- Color, nicht IBrush throw new InvalidOperationException($"Resource '{key}' must be a SolidColorBrush."); } var section = new HighlightingColor { Foreground = HB("Cfg.SectionBrush"), FontWeight = FontWeight.Bold }; var comment = new HighlightingColor { Foreground = HB("Cfg.CommentBrush"), FontStyle = FontStyle.Italic }; var key = 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(); // 1) Kommentare: ^\s*#.* rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"^\s*#.*", RegexOptions.Compiled | RegexOptions.Multiline), Color = comment }); // 2) Sektionen: [Name] rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"^\s*\[[^\]\r\n]+\]\s*$", RegexOptions.Compiled | RegexOptions.Multiline), Color = section }); // 3a) Schlüssel (von Zeilenbeginn bis vor "=") rules.Spans.Add(new HighlightingSpan { StartExpression = new Regex(@"^\s*", RegexOptions.Compiled | RegexOptions.Multiline), EndExpression = new Regex(@"=", RegexOptions.Compiled), SpanColor = key }); // 3b) Gleichheitszeichen rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"=", RegexOptions.Compiled), Color = equal }); // 3c) Werte (von "=" bis Zeilenende; Kommentar nicht einschließen) rules.Spans.Add(new HighlightingSpan { StartExpression = new Regex(@"=(?!.*#)", RegexOptions.Compiled), // bis Zeilenende ohne # EndExpression = new Regex(@"$", RegexOptions.Compiled | RegexOptions.Multiline), SpanColor = value }); // 3d) Zahlen im Wert hervorheben rules.Rules.Add(new HighlightingRule { Regex = new Regex(@"\b-?\d+(\.\d+)?\b", RegexOptions.Compiled), Color = number }); var def = new CustomHighlightingDefinition("CfgDark", rules); HighlightingManager.Instance.RegisterHighlighting("CfgDark", new[] { ".cfg", ".cfg.txt", ".properties" }, def); IBrush? TryRes(string key) => (IBrush?)Current?.FindResource(key); } // kleine Helper-Definition sealed class CustomHighlightingDefinition : IHighlightingDefinition { public CustomHighlightingDefinition(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(); } }