Files
NebulaLauncher/Nebula.Shared/Services/DotnetResolverService.cs

90 lines
2.9 KiB
C#
Raw Permalink Normal View History

2025-06-15 13:48:56 +03:00
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Text;
2025-12-03 23:16:18 +03:00
using Nebula.Shared.Utils;
2025-06-15 13:48:56 +03:00
namespace Nebula.Shared.Services;
[ServiceRegister]
public class DotnetResolverService(DebugService debugService, ConfigurationService configurationService)
{
2026-01-16 18:32:37 +03:00
private string FullPath =>
Path.Join(FileService.RootPath, $"dotnet.{configurationService.GetConfigValue(CurrentConVar.DotnetVersion)}", DotnetUrlHelper.GetRuntimeIdentifier());
2025-12-03 23:16:18 +03:00
2026-01-16 18:32:37 +03:00
private string ExecutePath => Path.Join(FullPath, "dotnet" + DotnetUrlHelper.GetExtension());
2025-12-03 23:16:18 +03:00
private readonly HttpClient _httpClient = new();
public async Task<string> EnsureDotnet()
{
if (!Directory.Exists(FullPath))
2025-06-15 13:48:56 +03:00
await Download();
2025-12-03 23:16:18 +03:00
2025-06-15 13:48:56 +03:00
return ExecutePath;
}
2025-12-03 23:16:18 +03:00
private async Task Download()
{
var debugLogger = debugService.GetLogger(this);
debugLogger.Log($"Downloading dotnet {DotnetUrlHelper.GetRuntimeIdentifier()}...");
var url = DotnetUrlHelper.GetCurrentPlatformDotnetUrl(
configurationService.GetConfigValue(CurrentConVar.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.");
}
debugLogger.Log("Downloading dotnet complete.");
2025-06-15 13:48:56 +03:00
}
}
public static class DotnetUrlHelper
{
2025-12-03 23:16:18 +03:00
[Obsolete("FOR TEST USING ONLY!")]
public static string? RidOverrideTest = null; // FOR TEST PURPOSES ONLY!!!
2025-06-15 13:48:56 +03:00
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()
{
2025-12-03 23:16:18 +03:00
if(RidOverrideTest != null) return RidOverrideTest;
2025-06-15 13:48:56 +03:00
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");
}
}