- tweak: exception representation

This commit is contained in:
2026-01-22 21:13:39 +03:00
parent ff31412719
commit c2ab550329
17 changed files with 195 additions and 73 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Nebula.Launcher.ProcessHelper;
using Nebula.Launcher.ViewModels;
using Nebula.Launcher.ViewModels.Popup;
using Nebula.Shared.Services;
@@ -8,18 +9,24 @@ namespace Nebula.Launcher.Models;
public sealed class ContentLogConsumer : IProcessLogConsumer
{
private readonly PopupMessageService _popupMessageService;
private readonly List<string> _outMessages = [];
private LogPopupModelView? _currentLogPopup;
public int MaxMessages { get; set; } = 100;
public void Popup(PopupMessageService popupMessageService)
public ContentLogConsumer(PopupMessageService popupMessageService)
{
_popupMessageService = popupMessageService;
}
public void Popup()
{
if(_currentLogPopup is not null)
return;
_currentLogPopup = new LogPopupModelView(popupMessageService);
_currentLogPopup = new LogPopupModelView(_popupMessageService);
_currentLogPopup.OnDisposing += OnLogPopupDisposing;
foreach (var message in _outMessages.ToArray())
@@ -27,7 +34,7 @@ public sealed class ContentLogConsumer : IProcessLogConsumer
_currentLogPopup.Append(message);
}
popupMessageService.Popup(_currentLogPopup);
_popupMessageService.Popup(_currentLogPopup);
}
private void OnLogPopupDisposing(PopupViewModelBase obj)
@@ -55,6 +62,6 @@ public sealed class ContentLogConsumer : IProcessLogConsumer
public void Fatal(string text)
{
throw new Exception("Error while running programm: " + text);
_popupMessageService.Popup(new ExceptionCompound("Error while running program", text));
}
}

View File

@@ -1,5 +1,6 @@
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Nebula.Shared.Services;
@@ -9,11 +10,11 @@ public abstract class DotnetProcessStartInfoProviderBase(DotnetResolverService r
{
protected abstract string GetDllPath();
public virtual async Task<ProcessStartInfo> GetProcessStartInfo()
public virtual async Task<ProcessStartInfo> GetProcessStartInfo(CancellationToken cancellationToken = default)
{
return new ProcessStartInfo
{
FileName = await resolverService.EnsureDotnet(),
FileName = await resolverService.EnsureDotnet(cancellationToken),
Arguments = GetDllPath(),
CreateNoWindow = true,
UseShellExecute = false,

View File

@@ -2,6 +2,7 @@ using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Nebula.Launcher.ViewModels.Pages;
using Nebula.Shared;
@@ -31,9 +32,9 @@ public sealed class GameProcessStartInfoProvider(DotnetResolverService resolverS
return this;
}
public override async Task<ProcessStartInfo> GetProcessStartInfo()
public override async Task<ProcessStartInfo> GetProcessStartInfo(CancellationToken cancellationToken = default)
{
var baseStart = await base.GetProcessStartInfo();
var baseStart = await base.GetProcessStartInfo(cancellationToken);
var authProv = accountInfoViewModel.Credentials.Value;
if(authProv is null)

View File

@@ -1,9 +1,10 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace Nebula.Launcher.ProcessHelper;
public interface IProcessStartInfoProvider
{
public Task<ProcessStartInfo> GetProcessStartInfo();
public Task<ProcessStartInfo> GetProcessStartInfo(CancellationToken cancellationToken = default);
}

View File

@@ -1,5 +1,7 @@
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Nebula.Shared.Services;
using Nebula.Shared.Services.Logging;
@@ -8,36 +10,23 @@ namespace Nebula.Launcher.ProcessHelper;
public class ProcessRunHandler : IDisposable
{
private ProcessStartInfo? _processInfo;
private Task<ProcessStartInfo>? _processInfoTask;
private Process? _process;
private readonly IProcessLogConsumer _logConsumer;
private string _lastError = string.Empty;
private readonly IProcessStartInfoProvider _currentProcessStartInfoProvider;
private StringBuilder _lastErrorBuilder = new StringBuilder();
public IProcessStartInfoProvider GetCurrentProcessStartInfo() => _currentProcessStartInfoProvider;
public bool IsRunning => _processInfo is not null;
public bool IsRunning => _process is not null;
public Action<ProcessRunHandler>? OnProcessExited;
public AsyncValueCache<ProcessStartInfo> ProcessStartInfoProvider { get; }
public bool Disposed { get; private set; }
public ProcessRunHandler(IProcessStartInfoProvider processStartInfoProvider, IProcessLogConsumer logConsumer)
{
_currentProcessStartInfoProvider = processStartInfoProvider;
_logConsumer = logConsumer;
_processInfoTask = _currentProcessStartInfoProvider.GetProcessStartInfo();
_processInfoTask.GetAwaiter().OnCompleted(OnInfoProvided);
}
private void OnInfoProvided()
{
if (_processInfoTask == null)
return;
_processInfo = _processInfoTask.GetAwaiter().GetResult();
_processInfoTask = null;
ProcessStartInfoProvider = new AsyncValueCache<ProcessStartInfo>(processStartInfoProvider.GetProcessStartInfo);
}
private void CheckIfDisposed()
@@ -51,13 +40,8 @@ public class ProcessRunHandler : IDisposable
CheckIfDisposed();
if(_process is not null)
throw new InvalidOperationException("Already running");
if (_processInfoTask != null)
{
_processInfoTask.Wait();
}
_process = Process.Start(_processInfo ?? throw new Exception("Process info is null, please try again."));
_process = Process.Start(ProcessStartInfoProvider.GetValue());
if (_process is null) return;
@@ -86,9 +70,8 @@ public class ProcessRunHandler : IDisposable
_process.ErrorDataReceived -= OnErrorDataReceived;
_process.Exited -= OnExited;
if (_process.ExitCode != 0)
_logConsumer.Fatal(_lastError);
_logConsumer.Fatal(_lastErrorBuilder.ToString());
_process.Dispose();
_process = null;
@@ -99,11 +82,13 @@ public class ProcessRunHandler : IDisposable
private void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
_lastError = e.Data;
_logConsumer.Error(e.Data);
}
if (e.Data == null) return;
if (!e.Data.StartsWith(" "))
_lastErrorBuilder.Clear();
_lastErrorBuilder.AppendLine(e.Data);
_logConsumer.Error(e.Data);
}
private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
@@ -122,9 +107,10 @@ public class ProcessRunHandler : IDisposable
return;
}
ProcessStartInfoProvider.Invalidate();
CheckIfDisposed();
_processInfoTask?.Dispose();
Disposed = true;
}
}
@@ -152,4 +138,76 @@ public sealed class DebugLoggerBridge : IProcessLogConsumer
{
_logger.Log(LoggerCategory.Error, text);
}
}
public class AsyncValueCache<T>
{
private readonly Func<CancellationToken, Task<T>> _valueFactory;
private readonly SemaphoreSlim _semaphore = new(1, 1);
private readonly CancellationTokenSource _cacheCts = new();
private Lazy<Task<T>> _lazyTask = null!;
private T _cachedValue = default!;
private bool _isCacheValid;
public AsyncValueCache(Func<CancellationToken, Task<T>> valueFactory)
{
_valueFactory = valueFactory ?? throw new ArgumentNullException(nameof(valueFactory));
ResetLazyTask();
}
public T GetValue()
{
if (_isCacheValid) return _cachedValue;
try
{
_semaphore.Wait();
if (_isCacheValid) return _cachedValue;
_cachedValue = _lazyTask.Value
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
_isCacheValid = true;
return _cachedValue;
}
finally
{
_semaphore.Release();
}
}
public void Invalidate()
{
using var cts = new CancellationTokenSource();
try
{
_semaphore.Wait();
_isCacheValid = false;
_cacheCts.Cancel();
_cacheCts.Dispose();
ResetLazyTask();
}
finally
{
_semaphore.Release();
}
}
private void ResetLazyTask()
{
_lazyTask = new Lazy<Task<T>>(() =>
_valueFactory(_cacheCts.Token)
.ContinueWith(t =>
{
if (t.IsCanceled || t.IsFaulted)
{
_isCacheValid = false;
throw t.Exception ?? new Exception();
}
return t.Result;
}, TaskContinuationOptions.ExecuteSynchronously));
}
}

View File

@@ -22,7 +22,7 @@ public sealed class InstanceRunningContainer(PopupMessageService popupMessageSer
{
var id = _keyPool.Take();
var currentContentLogConsumer = new ContentLogConsumer();
var currentContentLogConsumer = new ContentLogConsumer(popupMessageService);
var logBridge = new DebugLoggerBridge(debugService.GetLogger("PROCESS_"+id.Id));
var logContainer = new ProcessLogConsumerCollection();
logContainer.RegisterLogger(currentContentLogConsumer);
@@ -43,7 +43,7 @@ public sealed class InstanceRunningContainer(PopupMessageService popupMessageSer
if(!_contentLoggerCache.TryGetValue(instanceKey, out var handler))
return;
handler.Popup(popupMessageService);
handler.Popup();
}
public void Run(InstanceKey instanceKey)

View File

@@ -0,0 +1,39 @@
using System;
using Nebula.Launcher.Views;
using Nebula.Shared.ViewHelper;
namespace Nebula.Launcher.ViewModels;
[ViewModelRegister(typeof(ExceptionView), false)]
public class ExceptionCompound : ViewModelBase
{
public ExceptionCompound()
{
Message = "Test exception";
StackTrace = "Stack trace";
}
public ExceptionCompound(string message, string stackTrace)
{
Message = message;
StackTrace = stackTrace;
}
public ExceptionCompound(Exception ex)
{
Message = ex.Message;
StackTrace = ex.StackTrace;
}
public string Message { get; set; }
public string? StackTrace { get; set; }
protected override void InitialiseInDesignMode()
{
}
protected override void Initialise()
{
}
}

View File

@@ -3,9 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Avalonia.Logging;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Nebula.Launcher.Models;
using Nebula.Launcher.Services;
using Nebula.Launcher.Utils;
@@ -231,6 +229,11 @@ public partial class MainViewModel : ViewModelBase
case PopupViewModelBase @base:
PopupMessage(@base);
break;
case ExceptionCompound error:
var errViewModel = ViewHelperService.GetViewModel<ExceptionListViewModel>();
errViewModel.AppendError(error);
PopupMessage(errViewModel);
break;
case Exception error:
var err = ViewHelperService.GetViewModel<ExceptionListViewModel>();
_logger.Error(error);

View File

@@ -15,7 +15,7 @@ public sealed partial class ExceptionListViewModel : PopupViewModelBase
public override string Title => LocalizationService.GetString("popup-exception");
public override bool IsClosable => true;
public ObservableCollection<Exception> Errors { get; } = new();
public ObservableCollection<ExceptionCompound> Errors { get; } = new();
protected override void Initialise()
{
@@ -23,13 +23,18 @@ public sealed partial class ExceptionListViewModel : PopupViewModelBase
protected override void InitialiseInDesignMode()
{
var e = new Exception("TEST");
var e = new ExceptionCompound("TEST", "thrown in design mode");
AppendError(e);
}
public void AppendError(ExceptionCompound exception)
{
Errors.Add(exception);
}
public void AppendError(Exception exception)
{
Errors.Add(exception);
AppendError(new ExceptionCompound(exception));
if (exception.InnerException != null)
AppendError(exception.InnerException);
}

View File

@@ -18,7 +18,6 @@ namespace Nebula.Launcher.ViewModels;
public sealed partial class ServerCompoundEntryViewModel :
ViewModelBase, IFavoriteEntryModelView, IFilterConsumer, IListEntryModelView, IEntryNameHolder
{
private ServerEntryModelView? _currentEntry;
[ObservableProperty] private string _message = "Loading server entry...";
[ObservableProperty] private bool _isFavorite;
[ObservableProperty] private bool _loading = true;
@@ -29,22 +28,22 @@ public sealed partial class ServerCompoundEntryViewModel :
public ServerEntryModelView? CurrentEntry
{
get => _currentEntry;
get;
set
{
if (value == _currentEntry) return;
_currentEntry = value;
if (value == field) return;
if (_currentEntry != null)
field = value;
if (field != null)
{
_currentEntry.IsFavorite = IsFavorite;
_currentEntry.Name = Name;
_currentEntry.ProcessFilter(_currentFilter);
field.IsFavorite = IsFavorite;
field.Name = Name;
field.ProcessFilter(_currentFilter);
}
Loading = _currentEntry == null;
Loading = field == null;
OnPropertyChanged();
}
}

View File

@@ -230,6 +230,7 @@ public sealed partial class ServerEntryModelView : ViewModelBase, IFilterConsume
public void Dispose()
{
_logger.Dispose();
InstanceRunningContainer.IsRunningChanged -= IsRunningChanged;
}
}

View File

@@ -3,14 +3,14 @@
d:DesignWidth="800"
mc:Ignorable="d"
x:Class="Nebula.Launcher.Views.ExceptionView"
x:DataType="system:Exception"
x:DataType="viewModels:ExceptionCompound"
xmlns="https://github.com/avaloniaui"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModels="clr-namespace:Nebula.Launcher.ViewModels">
<Design.DataContext>
<system:Exception />
<viewModels:ExceptionCompound />
</Design.DataContext>
<Border
BoxShadow="{StaticResource DefaultShadow}"

View File

@@ -2,6 +2,8 @@ using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Nebula.Launcher.Models;
using Nebula.Launcher.ViewModels;
namespace Nebula.Launcher.Views;
@@ -14,6 +16,6 @@ public partial class ExceptionView : UserControl
public ExceptionView(Exception exception): this()
{
DataContext = exception;
DataContext = new ExceptionCompound(exception);
}
}

View File

@@ -9,6 +9,7 @@
<EmbeddedResource Include="Utils\runtime.json">
<LogicalName>Utility.runtime.json</LogicalName>
</EmbeddedResource>
<PackageReference Include="JetBrains.Annotations" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions"/>
<PackageReference Include="Robust.Natives"/>
<PackageReference Include="SharpZstd.Interop"/>

View File

@@ -1,4 +1,5 @@
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
namespace Nebula.Shared;
@@ -42,6 +43,7 @@ public static class ServiceExt
}
}
[MeansImplicitUse]
public sealed class ServiceRegisterAttribute : Attribute
{
public ServiceRegisterAttribute(Type? inference = null, bool isSingleton = true)

View File

@@ -14,15 +14,15 @@ public class DotnetResolverService(DebugService debugService, ConfigurationServi
private string ExecutePath => Path.Join(FullPath, "dotnet" + DotnetUrlHelper.GetExtension());
private readonly HttpClient _httpClient = new();
public async Task<string> EnsureDotnet()
public async Task<string> EnsureDotnet(CancellationToken cancellationToken = default)
{
if (!Directory.Exists(FullPath))
await Download();
await Download(cancellationToken);
return ExecutePath;
}
private async Task Download()
private async Task Download(CancellationToken cancellationToken = default)
{
var debugLogger = debugService.GetLogger(this);
debugLogger.Log($"Downloading dotnet {DotnetUrlHelper.GetRuntimeIdentifier()}...");
@@ -31,16 +31,16 @@ public class DotnetResolverService(DebugService debugService, ConfigurationServi
configurationService.GetConfigValue(CurrentConVar.DotnetUrl)!
);
using var response = await _httpClient.GetAsync(url);
using var response = await _httpClient.GetAsync(url, cancellationToken);
response.EnsureSuccessStatusCode();
await using var stream = await response.Content.ReadAsStreamAsync();
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
Directory.CreateDirectory(FullPath);
if (url.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
{
using var zipArchive = new ZipArchive(stream);
zipArchive.ExtractToDirectory(FullPath, true);
await using var zipArchive = new ZipArchive(stream);
await zipArchive.ExtractToDirectoryAsync(FullPath, true, cancellationToken);
}
else if (url.EndsWith(".tar.gz", StringComparison.OrdinalIgnoreCase)
|| url.EndsWith(".tgz", StringComparison.OrdinalIgnoreCase))

View File

@@ -77,7 +77,9 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AStreamGeometryContext_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003FCinka_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F5245275d55a2287c120f7503c3e453ddeee7c693d2f85f8cde43f7c8f01ee6_003FStreamGeometryContext_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AStreamReader_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003FCinka_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F6a802af2346a87e430fb66291f320aa22871259d47c2bc928a59f14b42aa34_003FStreamReader_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AStream_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003FCinka_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fd1287462d4ec4078c61b8e92a0952fb7de3e7e877d279e390a4c136a6365126_003FStream_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AStringBuilder_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003FCinka_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fd98be6eaa7445b7eb5caec7916b10e37af115adb1635b6336772135513ae6_003FStringBuilder_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AString_002EManipulation_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003FCinka_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fe75a5575ba872c8ea754c015cb363850e6c661f39569712d5b74aaca67263c_003FString_002EManipulation_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AStyledElement_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003FCinka_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fe49d9521ff091d353928d1c44539ba0a4c93a9ebb2e65190880b4fe5eb8_003FStyledElement_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AStyle_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003FCinka_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fcfbd5689fdab68d1c02f6a9b3c5921abcc409b8743dcc958da77cc1cfcb8e_003FStyle_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ATextBox_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003FCinka_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F43273dba3ac6a4e11aefe78fbbccf5d36f07542ca37ecebffb25c95d1a1c16b_003FTextBox_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AType_002ECoreCLR_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003FCinka_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F5cde391207de75962d7bacb899ca2bd3985c86911b152d185b58999a422bf0_003FType_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>