Files
NebulaLauncher/Nebula.UpdateResolver/DotnetStandalone.cs

104 lines
3.2 KiB
C#
Raw Normal View History

2025-06-15 13:48:56 +03:00
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
{
2025-12-03 23:16:18 +03:00
private static readonly HttpClient HttpClient = new();
private static readonly string FullPath =
Path.Join(MainWindow.RootPath, "dotnet", DotnetUrlHelper.GetRuntimeIdentifier());
2025-06-15 13:48:56 +03:00
private static readonly string ExecutePath = Path.Join(FullPath, "dotnet" + DotnetUrlHelper.GetExtension());
2025-12-03 23:16:18 +03:00
2025-06-15 13:48:56 +03:00
public static async Task<Process?> Run(string dllPath)
{
await EnsureDotnet();
2025-12-03 23:16:18 +03:00
2025-06-15 13:48:56 +03:00
return Process.Start(new ProcessStartInfo
{
FileName = ExecutePath,
Arguments = dllPath,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.UTF8
});
}
2025-12-03 23:16:18 +03:00
private static async Task EnsureDotnet()
{
if (!Directory.Exists(FullPath))
2025-06-15 13:48:56 +03:00
await Download();
}
2025-12-03 23:16:18 +03:00
private static async Task Download()
{
2025-06-15 13:48:56 +03:00
LogStandalone.Log($"Downloading dotnet {DotnetUrlHelper.GetRuntimeIdentifier()}...");
2025-12-03 23:16:18 +03:00
var url = DotnetUrlHelper.GetCurrentPlatformDotnetUrl(
ConfigurationStandalone.GetConfigValue(UpdateConVars.DotnetUrl)!
);
using var response = await HttpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
await using var stream = await response.Content.ReadAsStreamAsync();
2025-06-15 13:48:56 +03:00
Directory.CreateDirectory(FullPath);
2025-12-03 23:16:18 +03:00
if (url.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
{
using var zipArchive = new ZipArchive(stream);
zipArchive.ExtractToDirectory(FullPath, true);
}
else if (url.EndsWith(".tar.gz", StringComparison.OrdinalIgnoreCase)
|| url.EndsWith(".tgz", StringComparison.OrdinalIgnoreCase))
{
TarUtils.ExtractTarGz(stream, FullPath);
}
else
{
throw new NotSupportedException("Unsupported archive format.");
}
LogStandalone.Log("Downloading dotnet complete.");
2025-06-15 13:48:56 +03:00
}
}
public static class DotnetUrlHelper
{
public static string GetExtension()
{
if (OperatingSystem.IsWindows()) return ".exe";
return "";
}
2025-12-03 23:16:18 +03:00
2025-06-15 13:48:56 +03:00
public static string GetCurrentPlatformDotnetUrl(Dictionary<string, string> dotnetUrl)
{
2025-12-03 23:16:18 +03:00
var rid = GetRuntimeIdentifier();
2025-06-15 13:48:56 +03:00
2025-12-03 23:16:18 +03:00
if (dotnetUrl.TryGetValue(rid, out var url)) return url;
2025-06-15 13:48:56 +03:00
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";
2025-12-03 23:16:18 +03:00
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return "linux-x64";
2025-06-15 13:48:56 +03:00
throw new PlatformNotSupportedException("Unsupported operating system");
}
}