From b16b21e954b3751799c5aa73b1cd948f9d8791b5 Mon Sep 17 00:00:00 2001 From: Cinka Date: Tue, 7 Jan 2025 19:14:42 +0300 Subject: [PATCH] - add: Log --- .idea/.idea.Nebula/.idea/avalonia.xml | 1 + .../ViewModels/ServerEntryModelView.cs | 116 ++++++++++++++++-- Nebula.Launcher/Views/MainView.axaml | 3 +- .../Views/Pages/AccountInfoView.axaml | 9 +- .../Views/Pages/ServerListView.axaml | 8 +- .../Views/Popup/LogPopupView.axaml | 39 ++++++ .../Views/Popup/LogPopupView.axaml.cs | 19 +++ Nebula.Runner/Nebula.Runner.csproj | 2 +- 8 files changed, 177 insertions(+), 20 deletions(-) create mode 100644 Nebula.Launcher/Views/Popup/LogPopupView.axaml create mode 100644 Nebula.Launcher/Views/Popup/LogPopupView.axaml.cs diff --git a/.idea/.idea.Nebula/.idea/avalonia.xml b/.idea/.idea.Nebula/.idea/avalonia.xml index f25c2ce..21eb961 100644 --- a/.idea/.idea.Nebula/.idea/avalonia.xml +++ b/.idea/.idea.Nebula/.idea/avalonia.xml @@ -16,6 +16,7 @@ + diff --git a/Nebula.Launcher/ViewModels/ServerEntryModelView.cs b/Nebula.Launcher/ViewModels/ServerEntryModelView.cs index ecd0cb8..650d13a 100644 --- a/Nebula.Launcher/ViewModels/ServerEntryModelView.cs +++ b/Nebula.Launcher/ViewModels/ServerEntryModelView.cs @@ -1,7 +1,12 @@ using System; +using System.Collections.ObjectModel; using System.Diagnostics; +using System.Text; +using System.Text.RegularExpressions; +using Avalonia.Media; using CommunityToolkit.Mvvm.ComponentModel; using Nebula.Launcher.ViewHelper; +using Nebula.Launcher.Views.Popup; using Nebula.Shared.Models; using Nebula.Shared.Services; @@ -14,14 +19,21 @@ public partial class ServerEntryModelView : ViewModelBase private readonly ContentService _contentService = default!; private readonly CancellationService _cancellationService = default!; private readonly DebugService _debugService = default!; - private readonly RunnerService _runnerService; + private readonly RunnerService _runnerService = default!; + private readonly PopupMessageService _popupMessageService; [ObservableProperty] private bool _runVisible = true; public ServerHubInfo ServerHubInfo { get; set; } = default!; + + + private Process? _process; + + public LogPopupModelView CurrLog; public ServerEntryModelView() : base() { + CurrLog = GetViewModel(); } public ServerEntryModelView( @@ -30,7 +42,7 @@ public partial class ServerEntryModelView : ViewModelBase ContentService contentService, CancellationService cancellationService, DebugService debugService, - RunnerService runnerService + RunnerService runnerService, PopupMessageService popupMessageService ) : base(serviceProvider) { _authService = authService; @@ -38,9 +50,10 @@ public partial class ServerEntryModelView : ViewModelBase _cancellationService = cancellationService; _debugService = debugService; _runnerService = runnerService; - } + _popupMessageService = popupMessageService; - private Process? _process; + CurrLog = GetViewModel(); + } public async void RunInstance() { @@ -62,14 +75,23 @@ public partial class ServerEntryModelView : ViewModelBase { "GAME_URL", ServerHubInfo.Address } , { "AUTH_LOGIN", authProv?.AuthLoginPassword.Login } , }, - CreateNoWindow = true, UseShellExecute = false + CreateNoWindow = true, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, StandardOutputEncoding = Encoding.UTF8 }); + if (_process is null) { return; } + _process.BeginOutputReadLine(); + _process.BeginErrorReadLine(); + + RunVisible = false; + _process.OutputDataReceived += OnOutputDataReceived; _process.ErrorDataReceived += OnErrorDataReceived; @@ -91,22 +113,31 @@ public partial class ServerEntryModelView : ViewModelBase _process.Dispose(); _process = null; + RunVisible = true; } private void OnErrorDataReceived(object sender, DataReceivedEventArgs e) { - if (e.Data != null) _debugService.Error(e.Data); + if (e.Data != null) + { + _debugService.Error(e.Data); + CurrLog.Append(e.Data); + } } private void OnOutputDataReceived(object sender, DataReceivedEventArgs e) { - if (e.Data != null) _debugService.Log(e.Data); + if (e.Data != null) + { + _debugService.Log(e.Data); + CurrLog.Append(e.Data); + } } public void ReadLog() { - + _popupMessageService.Popup(CurrLog); } public void StopInstance() @@ -132,4 +163,73 @@ public partial class ServerEntryModelView : ViewModelBase throw new Exception("Dotnet not found!"); } +} + +public sealed class LogInfo +{ + public string Category { get; set; } = "LOG"; + public IBrush CategoryColor { get; set; } = Brush.Parse("#424242"); + public string Message { get; set; } = ""; + + public static LogInfo FromString(string input) + { + var matches = Regex.Matches(input, @"(\[(?.*)\] (?.*))|(?.*)"); + string category = "All"; + + if( matches[0].Groups.TryGetValue("c", out var c)) + { + category = c.Value; + } + + var color = Brush.Parse("#444444"); + + switch (category) + { + case "DEBG": + color = Brush.Parse("#2436d4"); + break; + case "ERRO": + color = Brush.Parse("#d42436"); + break; + case "INFO": + color = Brush.Parse("#0ab3c9"); + break; + } + + var message = matches[0].Groups["m"].Value; + return new LogInfo() + { + Category = category, Message = message, CategoryColor = color + }; + } +} + +[ViewModelRegister(typeof(LogPopupView), false)] +public sealed class LogPopupModelView : PopupViewModelBase +{ + public LogPopupModelView() : base() + { + Logs.Add(new LogInfo() + { + Category = "DEBG", Message = "MEOW MEOW TEST" + }); + + Logs.Add(new LogInfo() + { + Category = "ERRO", Message = "MEOW MEOW TEST 11\naaaaa" + }); + } + + public LogPopupModelView(IServiceProvider serviceProvider) : base(serviceProvider) + { + } + + public override string Title => "LOG"; + + public ObservableCollection Logs { get; } = new(); + + public void Append(string str) + { + Logs.Add(LogInfo.FromString(str)); + } } \ No newline at end of file diff --git a/Nebula.Launcher/Views/MainView.axaml b/Nebula.Launcher/Views/MainView.axaml index ceb414a..d9f9b67 100644 --- a/Nebula.Launcher/Views/MainView.axaml +++ b/Nebula.Launcher/Views/MainView.axaml @@ -98,8 +98,7 @@ + Margin="40"> Profiles: - - + - - + + diff --git a/Nebula.Launcher/Views/Pages/ServerListView.axaml b/Nebula.Launcher/Views/Pages/ServerListView.axaml index c4ad766..bce46df 100644 --- a/Nebula.Launcher/Views/Pages/ServerListView.axaml +++ b/Nebula.Launcher/Views/Pages/ServerListView.axaml @@ -17,11 +17,11 @@ - - + - - + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Nebula.Launcher/Views/Popup/LogPopupView.axaml.cs b/Nebula.Launcher/Views/Popup/LogPopupView.axaml.cs new file mode 100644 index 0000000..fd4ea37 --- /dev/null +++ b/Nebula.Launcher/Views/Popup/LogPopupView.axaml.cs @@ -0,0 +1,19 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; +using Nebula.Launcher.ViewModels; + +namespace Nebula.Launcher.Views.Popup; + +public partial class LogPopupView : UserControl +{ + public LogPopupView() + { + InitializeComponent(); + } + + public LogPopupView(LogPopupModelView viewModel) : this() + { + DataContext = viewModel; + } +} \ No newline at end of file diff --git a/Nebula.Runner/Nebula.Runner.csproj b/Nebula.Runner/Nebula.Runner.csproj index 60b4687..bd4b851 100644 --- a/Nebula.Runner/Nebula.Runner.csproj +++ b/Nebula.Runner/Nebula.Runner.csproj @@ -13,6 +13,6 @@ - +