Files
NebulaLauncher/Nebula.UpdateResolver/MainWindow.axaml.cs

140 lines
4.5 KiB
C#
Raw Normal View History

2025-05-01 19:01:59 +03:00
using System;
using System.Collections.Generic;
2025-05-01 21:03:55 +03:00
using System.Diagnostics;
using System.IO;
2025-05-01 19:01:59 +03:00
using System.Net.Http;
2025-05-01 21:03:55 +03:00
using System.Text;
2025-05-01 19:01:59 +03:00
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Controls;
2025-05-01 21:03:55 +03:00
using Nebula.Shared.FileApis;
2025-05-01 19:01:59 +03:00
using Nebula.Shared.FileApis.Interfaces;
using Nebula.Shared.Models;
using Nebula.Shared.Services;
2025-05-01 21:03:55 +03:00
using Tmds.DBus.Protocol;
2025-05-01 19:01:59 +03:00
namespace Nebula.UpdateResolver;
public partial class MainWindow : Window
{
private readonly ConfigurationService _configurationService;
private readonly RestService _restService;
private readonly HttpClient _httpClient = new HttpClient();
2025-05-01 21:03:55 +03:00
public FileApi FileApi { get; set; }
2025-05-01 19:01:59 +03:00
public MainWindow(FileService fileService, ConfigurationService configurationService, RestService restService)
{
_configurationService = configurationService;
_restService = restService;
InitializeComponent();
2025-05-01 21:03:55 +03:00
FileApi = (FileApi)fileService.CreateFileApi("app");
Start();
2025-05-01 19:01:59 +03:00
}
2025-05-01 21:03:55 +03:00
private async Task Start()
2025-05-01 19:01:59 +03:00
{
var info = await EnsureFiles();
2025-05-01 21:03:55 +03:00
Log("Downloading files...");
2025-05-01 19:01:59 +03:00
foreach (var file in info.ToDelete)
{
2025-05-01 21:03:55 +03:00
Log("Deleting " + file.Path);
2025-05-01 19:01:59 +03:00
FileApi.Remove(file.Path);
}
2025-05-01 21:03:55 +03:00
var loadedManifest = info.FilesExist;
Save(loadedManifest);
var count = info.ToDownload.Count;
var resolved = 0;
2025-05-01 19:01:59 +03:00
foreach (var file in info.ToDownload)
{
2025-05-01 21:03:55 +03:00
using var response = await _httpClient.GetAsync(
2025-05-01 19:01:59 +03:00
_configurationService.GetConfigValue(UpdateConVars.UpdateCacheUrl)
2025-05-01 21:03:55 +03:00
+ "/" + file.Hash);
response.EnsureSuccessStatusCode();
await using var stream = await response.Content.ReadAsStreamAsync();
2025-05-01 19:01:59 +03:00
FileApi.Save(file.Path, stream);
2025-05-01 21:03:55 +03:00
resolved++;
Log("Saving " + file.Path, (int)(resolved/(float)count*100f));
loadedManifest.Add(file);
Save(loadedManifest);
2025-05-01 19:01:59 +03:00
}
2025-05-01 21:03:55 +03:00
Log("Download finished. Running launcher...");
var process = Process.Start(new ProcessStartInfo
{
FileName = "dotnet.exe",
Arguments = Path.Join(FileApi.RootPath,"Nebula.Launcher.dll"),
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.UTF8
});
Thread.Sleep(2000);
Environment.Exit(0);
2025-05-01 19:01:59 +03:00
}
private async Task<ManifestEnsureInfo> EnsureFiles()
{
2025-05-01 21:03:55 +03:00
Log("Ensuring launcher manifest...");
2025-05-01 19:01:59 +03:00
var manifest = await _restService.GetAsync<LauncherManifest>(
2025-05-01 21:03:55 +03:00
new Uri(_configurationService.GetConfigValue(UpdateConVars.UpdateCacheUrl)! + "/manifest.json"), CancellationToken.None);
2025-05-01 19:01:59 +03:00
var toDownload = new HashSet<LauncherManifestEntry>();
var toDelete = new HashSet<LauncherManifestEntry>();
2025-05-01 21:03:55 +03:00
var filesExist = new HashSet<LauncherManifestEntry>();
2025-05-01 19:01:59 +03:00
2025-05-01 21:03:55 +03:00
Log("Manifest loaded!");
2025-05-01 19:01:59 +03:00
if (_configurationService.TryGetConfigValue(UpdateConVars.CurrentLauncherManifest, out var currentManifest))
{
2025-05-01 21:03:55 +03:00
Log("Delta manifest loaded!");
2025-05-01 19:01:59 +03:00
foreach (var file in currentManifest.Entries)
{
2025-05-01 21:03:55 +03:00
if (!manifest.Entries.Contains(file))
2025-05-01 19:01:59 +03:00
toDelete.Add(file);
2025-05-01 21:03:55 +03:00
else
filesExist.Add(file);
2025-05-01 19:01:59 +03:00
}
foreach (var file in manifest.Entries)
{
if(!currentManifest.Entries.Contains(file))
toDownload.Add(file);
}
}
else
{
toDownload = manifest.Entries;
}
2025-05-01 21:03:55 +03:00
Log("Saving launcher manifest...");
return new ManifestEnsureInfo(toDownload, toDelete, filesExist);
}
2025-05-01 19:01:59 +03:00
2025-05-01 21:03:55 +03:00
private void Log(string message, int percentage = 0)
{
ProgressLabel.Content = message;
if (percentage == 0)
PercentLabel.Content = "";
else
PercentLabel.Content = percentage + "%";
Console.WriteLine(message);
}
private void Save(HashSet<LauncherManifestEntry> entries)
{
_configurationService.SetConfigValue(UpdateConVars.CurrentLauncherManifest, new LauncherManifest(entries));
2025-05-01 19:01:59 +03:00
}
}
2025-05-01 21:03:55 +03:00
public record struct ManifestEnsureInfo(HashSet<LauncherManifestEntry> ToDownload, HashSet<LauncherManifestEntry> ToDelete, HashSet<LauncherManifestEntry> FilesExist);