- tweak: file managment

This commit is contained in:
2025-01-08 18:00:06 +03:00
parent b16b21e954
commit e5ed27f72d
20 changed files with 539 additions and 161 deletions

View File

@@ -80,18 +80,19 @@ public partial class AccountInfoViewModel : ViewModelBase
public async void DoAuth()
{
_popupMessageService.Popup("Auth think, please wait...");
var message = GetViewModel<InfoPopupViewModel>();
message.InfoText = "Auth think, please wait...";
_popupMessageService.Popup(message);
if(await _authService.Auth(CurrentAlp))
{
_popupMessageService.ClosePopup();
_popupMessageService.Popup("Hello, " + _authService.SelectedAuth!.AuthLoginPassword.Login);
message.Dispose();
IsLogged = true;
_configurationService.SetConfigValue(CurrentConVar.AuthCurrent, CurrentAlp);
}
else
{
_popupMessageService.ClosePopup();
message.Dispose();
Logout();
_popupMessageService.Popup("Well, shit is happened: " + _authService.Reason);
}
@@ -100,7 +101,7 @@ public partial class AccountInfoViewModel : ViewModelBase
public void Logout()
{
IsLogged = false;
CurrentAlp = new AuthLoginPassword("", "", "");
//CurrentAlp = new AuthLoginPassword("", "", "");
_authService.ClearAuth();
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.ObjectModel;
using Nebula.Launcher.ViewHelper;
using Nebula.Launcher.Views.Popup;
namespace Nebula.Launcher.ViewModels;
[ViewModelRegister(typeof(ExceptionView), false)]
public class ExceptionViewModel : PopupViewModelBase
{
public ExceptionViewModel() : base()
{
var e = new Exception("TEST");
AppendError(e);
}
public ExceptionViewModel(IServiceProvider serviceProvider) : base(serviceProvider){}
public override string Title => "Oopsie! Some shit is happened now!";
public ObservableCollection<Exception> Errors { get; } = new();
public void AppendError(Exception exception)
{
Errors.Add(exception);
if(exception.InnerException != null)
AppendError(exception.InnerException);
}
}

View File

@@ -0,0 +1,44 @@
using System;
using CommunityToolkit.Mvvm.ComponentModel;
using Nebula.Launcher.ViewHelper;
using Nebula.Launcher.Views.Popup;
using Nebula.Shared.Models;
namespace Nebula.Launcher.ViewModels;
[ViewModelRegister(typeof(LoadingContextView), false)]
public sealed partial class LoadingContextViewModel : PopupViewModelBase, ILoadingHandler
{
public LoadingContextViewModel() :base(){}
public LoadingContextViewModel(IServiceProvider provider) : base(provider){}
public string LoadingName { get; set; } = "Loading...";
public override string Title => LoadingName;
[ObservableProperty]
private int _currJobs;
[ObservableProperty]
private int _resolvedJobs;
public void SetJobsCount(int count)
{
CurrJobs = count;
}
public int GetJobsCount()
{
return CurrJobs;
}
public void SetResolvedJobsCount(int count)
{
ResolvedJobs = count;
}
public int GetResolvedJobsCount()
{
return ResolvedJobs;
}
}

View File

@@ -27,19 +27,20 @@ public partial class MainViewModel : ViewModelBase
}
[UsedImplicitly]
public MainViewModel(AccountInfoViewModel accountInfoViewModel, PopupMessageService popupMessageService,
public MainViewModel(AccountInfoViewModel accountInfoViewModel,DebugService debugService, PopupMessageService popupMessageService,
IServiceProvider serviceProvider): base(serviceProvider)
{
_currentPage = accountInfoViewModel;
_popupMessageService = popupMessageService;
_debugService = debugService;
Items = new ObservableCollection<ListItemTemplate>(_templates);
_popupMessageService.OnPopupRequired += OnPopupRequired;
popupMessageService.OnPopupRequired += OnPopupRequired;
popupMessageService.OnCloseRequired += OnPopupCloseRequired;
SelectedListItem = Items.First(vm => vm.ModelType == typeof(AccountInfoViewModel));
}
private readonly Queue<PopupViewModelBase> _viewQueue = new();
private readonly List<PopupViewModelBase> _viewQueue = new();
private readonly List<ListItemTemplate> _templates =
[
@@ -53,7 +54,7 @@ public partial class MainViewModel : ViewModelBase
[ObservableProperty]
private ViewModelBase _currentPage;
private readonly PopupMessageService _popupMessageService;
private readonly DebugService _debugService;
[ObservableProperty] private bool _isEnabled = true;
[ObservableProperty] private bool _popup;
@@ -90,7 +91,7 @@ public partial class MainViewModel : ViewModelBase
}
else
{
_viewQueue.Enqueue(viewModelBase);
_viewQueue.Add(viewModelBase);
}
}
@@ -111,13 +112,10 @@ public partial class MainViewModel : ViewModelBase
Helper.OpenBrowser("https://cinka.ru/nebula-launcher/");
}
private void OnPopupRequired(object? viewModelBase)
private void OnPopupRequired(object viewModelBase)
{
switch (viewModelBase)
{
case null:
ClosePopup();
break;
case string str:
{
var view = GetViewModel<InfoPopupViewModel>();
@@ -128,8 +126,28 @@ public partial class MainViewModel : ViewModelBase
case PopupViewModelBase @base:
PopupMessage(@base);
break;
case Exception error:
var err = GetViewModel<ExceptionViewModel>();
_debugService.Error(error);
err.AppendError(error);
PopupMessage(err);
break;
}
}
private void OnPopupCloseRequired(object obj)
{
if(obj is not PopupViewModelBase viewModelBase)
{
return;
}
if (obj == CurrentPopup)
ClosePopup();
else
_viewQueue.Remove(viewModelBase);
}
[RelayCommand]
private void TriggerPane()
@@ -140,10 +158,14 @@ public partial class MainViewModel : ViewModelBase
[RelayCommand]
public void ClosePopup()
{
if (!_viewQueue.TryDequeue(out var viewModelBase))
var viewModelBase = _viewQueue.FirstOrDefault();
if (viewModelBase is null)
OnCloseRequired();
else
{
CurrentTitle = viewModelBase.Title;
_viewQueue.RemoveAt(0);
}
CurrentPopup = viewModelBase;
}

View File

@@ -1,16 +1,25 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Nebula.Shared.Services;
namespace Nebula.Launcher.ViewModels;
public abstract class PopupViewModelBase : ViewModelBase
public abstract class PopupViewModelBase : ViewModelBase, IDisposable
{
private readonly IServiceProvider _serviceProvider;
public PopupViewModelBase()
{
}
public PopupViewModelBase(IServiceProvider serviceProvider) : base(serviceProvider)
{
_serviceProvider = serviceProvider;
}
public abstract string Title { get; }
public abstract string Title { get; }
public void Dispose()
{
_serviceProvider.GetService<PopupMessageService>()?.ClosePopup(this);
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using Nebula.Launcher.ViewHelper;
@@ -22,12 +23,23 @@ public partial class ServerEntryModelView : ViewModelBase
private readonly RunnerService _runnerService = default!;
private readonly PopupMessageService _popupMessageService;
[ObservableProperty] private bool _runVisible = true;
public bool RunVisible => Process == null;
public ServerHubInfo ServerHubInfo { get; set; } = default!;
private Process? _process;
private Process? _p;
private Process? Process
{
get { return _p; }
set
{
_p = value;
OnPropertyChanged(nameof(RunVisible));
}
}
public LogPopupModelView CurrLog;
@@ -55,65 +67,93 @@ public partial class ServerEntryModelView : ViewModelBase
CurrLog = GetViewModel<LogPopupModelView>();
}
public async void RunInstance()
public void RunInstance()
{
var authProv = _authService.SelectedAuth;
var buildInfo = await _contentService.GetBuildInfo(new RobustUrl(ServerHubInfo.Address), _cancellationService.Token);
Task.Run(RunAsync);
}
await _runnerService.PrepareRun(buildInfo, _cancellationService.Token);
_process = Process.Start(new ProcessStartInfo()
{
FileName = "dotnet.exe",
Arguments = "./Nebula.Runner.dll",
Environment = {
{ "ROBUST_AUTH_USERID", authProv?.UserId.ToString() } ,
{ "ROBUST_AUTH_TOKEN", authProv?.Token.Token } ,
{ "ROBUST_AUTH_SERVER", authProv?.AuthLoginPassword.AuthServer } ,
{ "ROBUST_AUTH_PUBKEY", buildInfo.BuildInfo.Auth.PublicKey } ,
{ "GAME_URL", ServerHubInfo.Address } ,
{ "AUTH_LOGIN", authProv?.AuthLoginPassword.Login } ,
},
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;
_process.Exited += OnExited;
public async Task RunAsync()
{
try
{
var authProv = _authService.SelectedAuth;
var buildInfo =
await _contentService.GetBuildInfo(new RobustUrl(ServerHubInfo.Address), _cancellationService.Token);
using (var loadingContext = GetViewModel<LoadingContextViewModel>())
{
loadingContext.LoadingName = "Loading instance...";
((ILoadingHandler)loadingContext).AppendJob();
_popupMessageService.Popup(loadingContext);
await _runnerService.PrepareRun(buildInfo, loadingContext, _cancellationService.Token);
Process = Process.Start(new ProcessStartInfo()
{
FileName = "dotnet.exe",
Arguments = "./Nebula.Runner.dll",
Environment =
{
{ "ROBUST_AUTH_USERID", authProv?.UserId.ToString() },
{ "ROBUST_AUTH_TOKEN", authProv?.Token.Token },
{ "ROBUST_AUTH_SERVER", authProv?.AuthLoginPassword.AuthServer },
{ "ROBUST_AUTH_PUBKEY", buildInfo.BuildInfo.Auth.PublicKey },
{ "GAME_URL", ServerHubInfo.Address },
{ "AUTH_LOGIN", authProv?.AuthLoginPassword.Login },
},
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.UTF8
});
((ILoadingHandler)loadingContext).AppendResolvedJob();
}
if (Process is null)
{
return;
}
Process.EnableRaisingEvents = true;
Process.BeginOutputReadLine();
Process.BeginErrorReadLine();
Process.OutputDataReceived += OnOutputDataReceived;
Process.ErrorDataReceived += OnErrorDataReceived;
Process.Exited += OnExited;
}
catch (TaskCanceledException e)
{
_popupMessageService.Popup("Task canceled");
}
catch (Exception e)
{
_popupMessageService.Popup(e);
}
}
private void OnExited(object? sender, EventArgs e)
{
if (_process is null)
if (Process is null)
{
return;
}
_process.OutputDataReceived -= OnOutputDataReceived;
_process.ErrorDataReceived -= OnErrorDataReceived;
_process.Exited -= OnExited;
Process.OutputDataReceived -= OnOutputDataReceived;
Process.ErrorDataReceived -= OnErrorDataReceived;
Process.Exited -= OnExited;
_debugService.Log("PROCESS EXIT WITH CODE " + _process.ExitCode);
_debugService.Log("PROCESS EXIT WITH CODE " + Process.ExitCode);
_process.Dispose();
_process = null;
RunVisible = true;
Process.Dispose();
Process = null;
}
private void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
@@ -142,7 +182,7 @@ public partial class ServerEntryModelView : ViewModelBase
public void StopInstance()
{
_process?.Close();
Process?.CloseMainWindow();
}
static string FindDotnetPath()

View File

@@ -0,0 +1,32 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:Nebula.Launcher.ViewModels"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Nebula.Launcher.Views.Popup.ExceptionView"
x:DataType="viewModels:ExceptionViewModel">
<Design.DataContext>
<viewModels:ExceptionViewModel />
</Design.DataContext>
<ScrollViewer Margin="10" Padding="0,0,8,0">
<ItemsControl
Background="#00000000"
ItemsSource="{Binding Errors}"
Padding="0">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type system:Exception}">
<Border Background="#333333" CornerRadius="5" Margin="0,0,0,5">
<StackPanel>
<Border Background="#aa2222" CornerRadius="5,5,0,0">
<Label Margin="4"><TextBlock Text="{Binding Message}"/></Label>
</Border>
<Label Margin="4"><TextBlock Text="{Binding StackTrace}"/></Label>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</UserControl>

View File

@@ -0,0 +1,20 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Nebula.Launcher.ViewModels;
namespace Nebula.Launcher.Views.Popup;
public partial class ExceptionView : UserControl
{
public ExceptionView()
{
InitializeComponent();
}
public ExceptionView(ExceptionViewModel viewModel) : this()
{
DataContext = viewModel;
}
}

View File

@@ -0,0 +1,32 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:Nebula.Launcher.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Nebula.Launcher.Views.Popup.LoadingContextView"
x:DataType="viewModels:LoadingContextViewModel">
<Design.DataContext>
<viewModels:LoadingContextViewModel />
</Design.DataContext>
<StackPanel Margin="25" Spacing="15">
<ProgressBar Height="40" Maximum="{Binding CurrJobs}" Value="{Binding ResolvedJobs}"/>
<Panel>
<StackPanel Orientation="Horizontal" Spacing="5" HorizontalAlignment="Left" VerticalAlignment="Center">
<Label>
<TextBlock Text="{Binding ResolvedJobs}"/>
</Label>
<Label>
/
</Label>
<Label>
<TextBlock Text="{Binding CurrJobs}"/>
</Label>
</StackPanel>
<Button HorizontalAlignment="Right" VerticalAlignment="Center">
<Label>Cancel</Label>
</Button>
</Panel>
</StackPanel>
</UserControl>

View File

@@ -0,0 +1,17 @@
using Avalonia.Controls;
using Nebula.Launcher.ViewModels;
namespace Nebula.Launcher.Views.Popup;
public partial class LoadingContextView : UserControl
{
public LoadingContextView()
{
InitializeComponent();
}
public LoadingContextView(LoadingContextViewModel viewModel): this()
{
DataContext = viewModel;
}
}