- add: DotNet runtime download

This commit is contained in:
2025-06-15 13:48:56 +03:00
parent 30a526a746
commit 22a6b3157d
9 changed files with 236 additions and 46 deletions

View File

@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Nebula.UpdateResolver.Configuration;
namespace Nebula.UpdateResolver;
public static class DotnetStandalone
{
private static readonly HttpClient HttpClient = new HttpClient();
private static readonly string FullPath = Path.Join(MainWindow.RootPath, "dotnet", DotnetUrlHelper.GetRuntimeIdentifier());
private static readonly string ExecutePath = Path.Join(FullPath, "dotnet" + DotnetUrlHelper.GetExtension());
public static async Task<Process?> Run(string dllPath)
{
await EnsureDotnet();
return Process.Start(new ProcessStartInfo
{
FileName = ExecutePath,
Arguments = dllPath,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.UTF8
});
}
private static async Task EnsureDotnet(){
if(!Directory.Exists(FullPath))
await Download();
}
private static async Task Download(){
LogStandalone.Log($"Downloading dotnet {DotnetUrlHelper.GetRuntimeIdentifier()}...");
var ridExt =
DotnetUrlHelper.GetCurrentPlatformDotnetUrl(ConfigurationStandalone.GetConfigValue(UpdateConVars.DotnetUrl)!);
using var response = await HttpClient.GetAsync(ridExt);
using var zipArchive = new ZipArchive(await response.Content.ReadAsStreamAsync());
Directory.CreateDirectory(FullPath);
zipArchive.ExtractToDirectory(FullPath);
LogStandalone.Log($"Downloading dotnet complete.");
}
}
public static class DotnetUrlHelper
{
public static string GetExtension()
{
if (OperatingSystem.IsWindows()) return ".exe";
return "";
}
public static string GetCurrentPlatformDotnetUrl(Dictionary<string, string> dotnetUrl)
{
string? rid = GetRuntimeIdentifier();
if (dotnetUrl.TryGetValue(rid, out var url))
{
return url;
}
throw new PlatformNotSupportedException($"No download URL available for the current platform: {rid}");
}
public static string GetRuntimeIdentifier()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Environment.Is64BitProcess ? "win-x64" : "win-x86";
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return "linux-x64";
}
throw new PlatformNotSupportedException("Unsupported operating system");
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Nebula.UpdateResolver;
public static class LogStandalone
{
public static Action<string, int>? OnLog;
public static void LogError(Exception e){
Log($"{e.GetType().Name}: "+ e.Message);
Log(e.StackTrace);
if(e.InnerException != null)
LogError(e.InnerException);
}
public static void Log(string? message, int percentage = 0)
{
if(message is null) return;
OnLog?.Invoke(message, percentage);
}
}

View File

@@ -23,6 +23,19 @@ public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
LogStandalone.OnLog += (message, percentage) =>
{
ProgressLabel.Content = message;
if (percentage == 0)
PercentLabel.Content = "";
else
PercentLabel.Content = percentage + "%";
var messageOut =
$"[{DateTime.Now.ToUniversalTime():yyyy-MM-dd HH:mm:ss}]: {message} {PercentLabel.Content}";
Console.WriteLine(messageOut);
LogStr += messageOut + "\n";
};
Start();
}
@@ -31,11 +44,11 @@ public partial class MainWindow : Window
try
{
var info = await EnsureFiles();
Log("Downloading files...");
LogStandalone.Log("Downloading files...");
foreach (var file in info.ToDelete)
{
Log("Deleting " + file.Path);
LogStandalone.Log("Deleting " + file.Path);
FileApi.Remove(file.Path);
}
@@ -55,30 +68,21 @@ public partial class MainWindow : Window
await using var stream = await response.Content.ReadAsStreamAsync();
FileApi.Save(file.Path, stream);
resolved++;
Log("Saving " + file.Path, (int)(resolved / (float)count * 100f));
LogStandalone.Log("Saving " + file.Path, (int)(resolved / (float)count * 100f));
loadedManifest.Add(file);
Save(loadedManifest);
}
Log("Download finished. Running launcher...");
LogStandalone.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
});
await DotnetStandalone.Run(Path.Join(FileApi.RootPath, "Nebula.Launcher.dll"));
}
catch(HttpRequestException e){
LogError(e);
Log("Проблемы с интернет-соединением...");
LogStandalone.LogError(e);
LogStandalone.Log("Network connection error...");
var logPath = Path.Join(RootPath,"updateResloverError.txt");
File.WriteAllText(logPath, LogStr);
await File.WriteAllTextAsync(logPath, LogStr);
Process.Start(new ProcessStartInfo(){
FileName = "notepad",
Arguments = logPath
@@ -86,9 +90,9 @@ public partial class MainWindow : Window
}
catch (Exception e)
{
LogError(e);
LogStandalone.LogError(e);
var logPath = Path.Join(RootPath,"updateResloverError.txt");
File.WriteAllText(logPath, LogStr);
await File.WriteAllTextAsync(logPath, LogStr);
Process.Start(new ProcessStartInfo(){
FileName = "notepad",
Arguments = logPath
@@ -102,7 +106,7 @@ public partial class MainWindow : Window
private async Task<ManifestEnsureInfo> EnsureFiles()
{
Log("Ensuring launcher manifest...");
LogStandalone.Log("Ensuring launcher manifest...");
var manifest = await RestStandalone.GetAsync<LauncherManifest>(
new Uri(ConfigurationStandalone.GetConfigValue(UpdateConVars.UpdateCacheUrl)! + "/manifest.json"), CancellationToken.None);
@@ -110,10 +114,10 @@ public partial class MainWindow : Window
var toDelete = new HashSet<LauncherManifestEntry>();
var filesExist = new HashSet<LauncherManifestEntry>();
Log("Manifest loaded!");
LogStandalone.Log("Manifest loaded!");
if (ConfigurationStandalone.TryGetConfigValue(UpdateConVars.CurrentLauncherManifest, out var currentManifest))
{
Log("Delta manifest loaded!");
LogStandalone.Log("Delta manifest loaded!");
foreach (var file in currentManifest.Entries)
{
if (!manifest.Entries.Contains(file))
@@ -133,31 +137,11 @@ public partial class MainWindow : Window
toDownload = manifest.Entries;
}
Log("Saving launcher manifest...");
LogStandalone.Log("Saving launcher manifest...");
return new ManifestEnsureInfo(toDownload, toDelete, filesExist);
}
private void LogError(Exception e){
Log($"{e.GetType().Name}: "+ e.Message);
Log(e.StackTrace);
if(e.InnerException != null)
LogError(e.InnerException);
}
private void Log(string? message, int percentage = 0)
{
if(message is null) return;
ProgressLabel.Content = message;
if (percentage == 0)
PercentLabel.Content = "";
else
PercentLabel.Content = percentage + "%";
var messageOut = $"[{DateTime.Now.ToUniversalTime():yyyy-MM-dd HH:mm:ss}]: {message} {PercentLabel.Content}";
Console.WriteLine(messageOut);
LogStr += messageOut + "\n";
}
private void Save(HashSet<LauncherManifestEntry> entries)
{

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using Nebula.UpdateResolver.Configuration;
namespace Nebula.UpdateResolver;
@@ -8,4 +9,11 @@ public static class UpdateConVars
ConVarBuilder.Build<string>("update.url","https://durenko.tatar/nebula/manifest/");
public static readonly ConVar<LauncherManifest> CurrentLauncherManifest =
ConVarBuilder.Build<LauncherManifest>("update.manifest");
public static readonly ConVar<Dictionary<string,string>> DotnetUrl = ConVarBuilder.Build<Dictionary<string,string>>("dotnet.url",
new(){
{"win-x64", "https://builds.dotnet.microsoft.com/dotnet/Runtime/9.0.6/dotnet-runtime-9.0.6-win-x64.zip"},
{"win-x86", "https://builds.dotnet.microsoft.com/dotnet/Runtime/9.0.6/dotnet-runtime-9.0.6-win-x86.zip"},
{"linux-x64", "https://builds.dotnet.microsoft.com/dotnet/Runtime/9.0.6/dotnet-runtime-9.0.6-linux-x64.tar.gz"}
});
}