- 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

@@ -15,7 +15,9 @@
<entry key="Nebula.Launcher/Views/Pages/AccountInfoView.axaml" value="Nebula.Launcher/Nebula.Launcher.csproj" />
<entry key="Nebula.Launcher/Views/Pages/ServerListPage.axaml" value="Nebula.Launcher/Nebula.Launcher.csproj" />
<entry key="Nebula.Launcher/Views/Pages/ServerListView.axaml" value="Nebula.Launcher/Nebula.Launcher.csproj" />
<entry key="Nebula.Launcher/Views/Popup/ExceptionView.axaml" value="Nebula.Launcher/Nebula.Launcher.csproj" />
<entry key="Nebula.Launcher/Views/Popup/InfoPopupView.axaml" value="Nebula.Launcher/Nebula.Launcher.csproj" />
<entry key="Nebula.Launcher/Views/Popup/LoadingContextView.axaml" value="Nebula.Launcher/Nebula.Launcher.csproj" />
<entry key="Nebula.Launcher/Views/Popup/LogPopupView.axaml" value="Nebula.Launcher/Nebula.Launcher.csproj" />
<entry key="Nebula.Launcher/Views/Popup/MessagePopupView.axaml" value="Nebula.Launcher/Nebula.Launcher.csproj" />
<entry key="Nebula.Launcher/Views/ServerContainer.axaml" value="Nebula.Launcher/Nebula.Launcher.csproj" />

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;
}
}

View File

@@ -47,11 +47,80 @@ public sealed class App(DebugService debugService, RunnerService runnerService,
args.Add("--ss14-address");
args.Add(url.ToString());
await runnerService.Run(args.ToArray(), buildInfo, this, cancelTokenSource.Token);
await runnerService.Run(args.ToArray(), buildInfo, this, new ConsoleLoadingHandler(), cancelTokenSource.Token);
}
public void Redial(Uri uri, string text = "")
{
}
}
public sealed class ConsoleLoadingHandler : ILoadingHandler
{
private int _currJobs;
private int _resolvedJobs;
private float _percent = 0f;
public void SetJobsCount(int count)
{
_currJobs = count;
UpdatePercent();
Draw();
}
public int GetJobsCount()
{
return _currJobs;
}
public void SetResolvedJobsCount(int count)
{
_resolvedJobs = count;
UpdatePercent();
Draw();
}
public int GetResolvedJobsCount()
{
return _resolvedJobs;
}
private void UpdatePercent()
{
if(_currJobs == 0)
{
_percent = 0;
return;
}
if(_resolvedJobs > _currJobs) return;
_percent = _resolvedJobs /(float) _currJobs;
}
private void Draw()
{
var barCount = 10;
var fullCount = (int)(barCount * _percent);
var emptyCount = barCount - fullCount;
Console.Write("\r");
for (var i = 0; i < fullCount; i++)
{
Console.Write("#");
}
for (var i = 0; i < emptyCount; i++)
{
Console.Write(" ");
}
Console.Write($"\t {_resolvedJobs}/{_currJobs}");
}
}

View File

@@ -2,7 +2,7 @@
namespace Nebula.Shared.FileApis;
public class FileApi : IReadWriteFileApi
public sealed class FileApi : IReadWriteFileApi
{
public string RootPath;
@@ -13,10 +13,19 @@ public class FileApi : IReadWriteFileApi
public bool TryOpen(string path, out Stream? stream)
{
if (File.Exists(Path.Join(RootPath, path)))
var fullPath = Path.Join(RootPath, path);
if (File.Exists(fullPath))
{
stream = File.OpenRead(Path.Join(RootPath, path));
return true;
try
{
stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
return true;
}
catch
{
stream = null;
return false;
}
}
stream = null;
@@ -27,27 +36,43 @@ public class FileApi : IReadWriteFileApi
{
var currPath = Path.Join(RootPath, path);
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(currPath));
if (!dirInfo.Exists) dirInfo.Create();
try
{
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(currPath) ?? throw new InvalidOperationException());
if (!dirInfo.Exists) dirInfo.Create();
using var stream = File.OpenWrite(currPath);
input.CopyTo(stream);
stream.Flush();
stream.Close();
return true;
using var stream = new FileStream(currPath, FileMode.Create, FileAccess.Write, FileShare.None);
input.CopyTo(stream);
return true;
}
catch
{
return false;
}
}
public bool Remove(string path)
{
if (!Has(path)) return false;
File.Delete(Path.Join(RootPath, path));
return true;
var fullPath = Path.Join(RootPath, path);
try
{
if (File.Exists(fullPath))
{
File.Delete(fullPath);
return true;
}
}
catch
{
// Log exception if necessary
}
return false;
}
public bool Has(string path)
{
var currPath = Path.Join(RootPath, path);
return File.Exists(currPath);
var fullPath = Path.Join(RootPath, path);
return File.Exists(fullPath);
}
public IEnumerable<string> AllFiles => Directory.EnumerateFiles(RootPath, "*.*", SearchOption.AllDirectories);

View File

@@ -0,0 +1,47 @@
namespace Nebula.Shared.Models;
public interface ILoadingHandler
{
public void SetJobsCount(int count);
public int GetJobsCount();
public void SetResolvedJobsCount(int count);
public int GetResolvedJobsCount();
public void AppendJob(int count = 1)
{
SetJobsCount(GetJobsCount() + count);
}
public void AppendResolvedJob(int count = 1)
{
SetResolvedJobsCount(GetResolvedJobsCount() + count);
}
public void Clear()
{
SetResolvedJobsCount(0);
SetJobsCount(0);
}
public QueryJob GetQueryJob()
{
return new QueryJob(this);
}
}
public sealed class QueryJob: IDisposable
{
private readonly ILoadingHandler _handler;
public QueryJob(ILoadingHandler handler)
{
_handler = handler;
handler.AppendJob();
}
public void Dispose()
{
_handler.AppendResolvedJob();
}
}

View File

@@ -15,7 +15,7 @@ public partial class ContentService
return fileService.ContentFileApi.Has(item.Hash);
}
public async Task<List<RobustManifestItem>> EnsureItems(ManifestReader manifestReader, Uri downloadUri,
public async Task<List<RobustManifestItem>> EnsureItems(ManifestReader manifestReader, Uri downloadUri, ILoadingHandler loadingHandler,
CancellationToken cancellationToken)
{
List<RobustManifestItem> allItems = [];
@@ -36,14 +36,14 @@ public partial class ContentService
debugService.Log("Download Count:" + items.Count);
await Download(downloadUri, items, cancellationToken);
await Download(downloadUri, items, loadingHandler, cancellationToken);
fileService.ManifestItems = allItems;
return allItems;
}
public async Task<List<RobustManifestItem>> EnsureItems(RobustManifestInfo info,
public async Task<List<RobustManifestItem>> EnsureItems(RobustManifestInfo info, ILoadingHandler loadingHandler,
CancellationToken cancellationToken)
{
debugService.Log("Getting manifest: " + info.Hash);
@@ -51,7 +51,7 @@ public partial class ContentService
if (fileService.ManifestFileApi.TryOpen(info.Hash, out var stream))
{
debugService.Log("Loading manifest from: " + info.Hash);
return await EnsureItems(new ManifestReader(stream), info.DownloadUri, cancellationToken);
return await EnsureItems(new ManifestReader(stream), info.DownloadUri, loadingHandler, cancellationToken);
}
debugService.Log("Fetching manifest from: " + info.ManifestUri);
@@ -63,14 +63,16 @@ public partial class ContentService
fileService.ManifestFileApi.Save(info.Hash, streamContent);
streamContent.Seek(0, SeekOrigin.Begin);
using var manifestReader = new ManifestReader(streamContent);
return await EnsureItems(manifestReader, info.DownloadUri, cancellationToken);
return await EnsureItems(manifestReader, info.DownloadUri, loadingHandler, cancellationToken);
}
public async Task Unpack(RobustManifestInfo info, IWriteFileApi otherApi, CancellationToken cancellationToken)
public async Task Unpack(RobustManifestInfo info, IWriteFileApi otherApi, ILoadingHandler loadingHandler, CancellationToken cancellationToken)
{
debugService.Log("Unpack manifest files");
var items = await EnsureItems(info, cancellationToken);
var items = await EnsureItems(info, loadingHandler, cancellationToken);
loadingHandler.AppendJob(items.Count);
foreach (var item in items)
{
if (fileService.ContentFileApi.TryOpen(item.Hash, out var stream))
{
debugService.Log($"Unpack {item.Hash} to: {item.Path}");
@@ -81,9 +83,11 @@ public partial class ContentService
{
debugService.Error("OH FUCK!! " + item.Path);
}
loadingHandler.AppendResolvedJob();
}
}
public async Task Download(Uri contentCdn, List<RobustManifestItem> toDownload, CancellationToken cancellationToken)
public async Task Download(Uri contentCdn, List<RobustManifestItem> toDownload, ILoadingHandler loadingHandler, CancellationToken cancellationToken)
{
if (toDownload.Count == 0 || cancellationToken.IsCancellationRequested)
{
@@ -91,6 +95,8 @@ public partial class ContentService
return;
}
var downloadJobWatch = loadingHandler.GetQueryJob();
debugService.Log("Downloading from: " + contentCdn);
var requestBody = new byte[toDownload.Count * 4];
@@ -117,6 +123,8 @@ public partial class ContentService
debugService.Log("Downloading is cancelled!");
return;
}
downloadJobWatch.Dispose();
response.EnsureSuccessStatusCode();
@@ -155,6 +163,9 @@ public partial class ContentService
var readBuffer = new byte[1024];
var i = 0;
loadingHandler.AppendJob(toDownload.Count);
foreach (var item in toDownload)
{
if (cancellationToken.IsCancellationRequested)
@@ -230,6 +241,7 @@ public partial class ContentService
fileService.ContentFileApi.Save(item.Hash, fileStream);
debugService.Log("file saved:" + item.Path);
loadingHandler.AppendResolvedJob();
i += 1;
}
}

View File

@@ -81,13 +81,16 @@ public sealed class EngineService
try
{
return _assemblyService.Mount(_fileService.OpenZip(version, _fileService.EngineFileApi));
var api = _fileService.OpenZip(version, _fileService.EngineFileApi);
if (api != null) return _assemblyService.Mount(api);
}
catch (Exception e)
{
_fileService.EngineFileApi.Remove(version);
throw;
}
return null;
}
public async Task DownloadEngine(string version)

View File

@@ -51,15 +51,24 @@ public class FileService
return new FileApi(Path.Join(RootPath, path));
}
public ZipFileApi OpenZip(string path, IFileApi fileApi)
public ZipFileApi? OpenZip(string path, IFileApi fileApi)
{
if (!fileApi.TryOpen(path, out var zipStream))
return null;
Stream? zipStream = null;
try
{
if (!fileApi.TryOpen(path, out zipStream))
return null;
var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Read);
var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Read);
var prefix = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) prefix = "Space Station 14.app/Contents/Resources/";
return new ZipFileApi(zipArchive, prefix);
var prefix = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) prefix = "Space Station 14.app/Contents/Resources/";
return new ZipFileApi(zipArchive, prefix);
}
catch (Exception e)
{
zipStream?.Dispose();
throw;
}
}
}

View File

@@ -3,13 +3,14 @@ namespace Nebula.Shared.Services;
[ServiceRegister]
public class PopupMessageService
{
public Action<object?>? OnPopupRequired;
public Action<object>? OnPopupRequired;
public Action<object>? OnCloseRequired;
public void Popup(object obj)
{
OnPopupRequired?.Invoke(obj);
}
public void ClosePopup()
public void ClosePopup(object obj)
{
OnPopupRequired?.Invoke(null);
OnCloseRequired?.Invoke(obj);
}
}

View File

@@ -25,17 +25,8 @@ public class RestService
public async Task<RestResult<T>> GetAsync<T>(Uri uri, CancellationToken cancellationToken)
{
_debug.Debug("GET " + uri);
try
{
var response = await _client.GetAsync(uri, cancellationToken);
return await ReadResult<T>(response, cancellationToken);
}
catch (Exception ex)
{
_debug.Error("ERROR WHILE CONNECTION " + uri + ": " + ex.Message);
return new RestResult<T>(default, ex.Message, HttpStatusCode.RequestTimeout);
}
var response = await _client.GetAsync(uri, cancellationToken);
return await ReadResult<T>(response, cancellationToken);
}
public async Task<T> GetAsyncDefault<T>(Uri uri, T defaultValue, CancellationToken cancellationToken)
@@ -46,53 +37,25 @@ public class RestService
public async Task<RestResult<K>> PostAsync<K, T>(T information, Uri uri, CancellationToken cancellationToken)
{
_debug.Debug("POST " + uri);
try
{
var json = JsonSerializer.Serialize(information, _serializerOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _client.PostAsync(uri, content, cancellationToken);
return await ReadResult<K>(response, cancellationToken);
}
catch (Exception ex)
{
_debug.Debug("ERROR " + ex.Message);
return new RestResult<K>(default, ex.Message, HttpStatusCode.RequestTimeout);
}
var json = JsonSerializer.Serialize(information, _serializerOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _client.PostAsync(uri, content, cancellationToken);
return await ReadResult<K>(response, cancellationToken);
}
public async Task<RestResult<T>> PostAsync<T>(Stream stream, Uri uri, CancellationToken cancellationToken)
{
_debug.Debug("POST " + uri);
try
{
using var multipartFormContent =
new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
multipartFormContent.Add(new StreamContent(stream), "formFile", "image.png");
var response = await _client.PostAsync(uri, multipartFormContent, cancellationToken);
return await ReadResult<T>(response, cancellationToken);
}
catch (Exception ex)
{
_debug.Error("ERROR " + ex.Message);
if (ex.StackTrace != null) _debug.Error(ex.StackTrace);
return new RestResult<T>(default, ex.Message, HttpStatusCode.RequestTimeout);
}
using var multipartFormContent =
new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
multipartFormContent.Add(new StreamContent(stream), "formFile", "image.png");
var response = await _client.PostAsync(uri, multipartFormContent, cancellationToken);
return await ReadResult<T>(response, cancellationToken);
}
public async Task<RestResult<T>> DeleteAsync<T>(Uri uri, CancellationToken cancellationToken)
{
_debug.Debug("DELETE " + uri);
try
{
var response = await _client.DeleteAsync(uri, cancellationToken);
return await ReadResult<T>(response, cancellationToken);
}
catch (Exception ex)
{
_debug.Debug("ERROR " + ex.Message);
return new RestResult<T>(default, ex.Message, HttpStatusCode.RequestTimeout);
}
var response = await _client.DeleteAsync(uri, cancellationToken);
return await ReadResult<T>(response, cancellationToken);
}
private async Task<RestResult<T>> ReadResult<T>(HttpResponseMessage response, CancellationToken cancellationToken)

View File

@@ -12,7 +12,7 @@ public sealed class RunnerService(
EngineService engineService,
AssemblyService assemblyService)
{
public async Task PrepareRun(RobustBuildInfo buildInfo, CancellationToken cancellationToken)
public async Task PrepareRun(RobustBuildInfo buildInfo, ILoadingHandler loadingHandler, CancellationToken cancellationToken)
{
debugService.Log("Prepare Content!");
@@ -21,11 +21,11 @@ public sealed class RunnerService(
if (engine is null)
throw new Exception("Engine version is not usable: " + buildInfo.BuildInfo.Build.EngineVersion);
await contentService.EnsureItems(buildInfo.RobustManifestInfo, cancellationToken);
await contentService.EnsureItems(buildInfo.RobustManifestInfo, loadingHandler, cancellationToken);
await engineService.EnsureEngineModules("Robust.Client.WebView", buildInfo.BuildInfo.Build.EngineVersion);
}
public async Task Run(string[] runArgs, RobustBuildInfo buildInfo, IRedialApi redialApi,
public async Task Run(string[] runArgs, RobustBuildInfo buildInfo, IRedialApi redialApi, ILoadingHandler loadingHandler,
CancellationToken cancellationToken)
{
debugService.Log("Start Content!");
@@ -35,7 +35,7 @@ public sealed class RunnerService(
if (engine is null)
throw new Exception("Engine version is not usable: " + buildInfo.BuildInfo.Build.EngineVersion);
await contentService.EnsureItems(buildInfo.RobustManifestInfo, cancellationToken);
await contentService.EnsureItems(buildInfo.RobustManifestInfo, loadingHandler, cancellationToken);
var extraMounts = new List<ApiMount>
{