diff --git a/Data/Technic.cs b/Data/Technic.cs index d593773..ecf3ffe 100644 --- a/Data/Technic.cs +++ b/Data/Technic.cs @@ -9,21 +9,36 @@ namespace NewRGB.Data; public class Technic(string modpackName) { - private readonly HttpClient _httpClient = new(); + private static readonly HttpClientHandler ClientHandler = new(); + private readonly HttpClient _httpClient = new(ClientHandler); private const int BuildVersion = 69420; private string _version = ""; private string _downloadUrl = ""; private readonly string _modpackZipPath = Path.Combine(DataManager.Instance.DataPath, "modpack.zip"); - public async Task Init() + static Technic() { - var response = - await _httpClient.GetAsync($"https://api.technicpack.net/modpack/{modpackName}?build={BuildVersion}"); - response.EnsureSuccessStatusCode(); - var responseBody = await response.Content.ReadAsStringAsync(); - var json = JsonNode.Parse(responseBody) ?? throw new Exception("can't parse json"); - _version = json["version"]!.GetValue(); - _downloadUrl = json["url"]!.GetValue(); + ClientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true; + } + + public async Task Init() + { + try + { + var response = + await _httpClient.GetAsync($"https://api.technicpack.net/modpack/{modpackName}?build={BuildVersion}"); + response.EnsureSuccessStatusCode(); + var responseBody = await response.Content.ReadAsStringAsync(); + var json = JsonNode.Parse(responseBody) ?? throw new Exception("can't parse json"); + _version = json["version"]!.GetValue(); + _downloadUrl = json["url"]!.GetValue(); + return true; + } + catch (HttpRequestException e) + { + Console.Error.WriteLine(e); + return false; + } } public async Task CheckUpdate() @@ -42,7 +57,7 @@ public async Task SaveVersion() public Task DownloadUpdate() { - return DownloadProgress.Download(_downloadUrl, _modpackZipPath, _httpClient); + return DownloadProgress.Download(_downloadUrl, _modpackZipPath); } public async Task InstallUpdate() diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index 31c3268..814e851 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -619,7 +619,8 @@ private async Task OnLoaded() } UpdateProgress(0.0f, "Checking for updates", false); - await _technic.Init(); + if (!await _technic.Init()) UpdateProgress(1.0f, "Can't download info from technic"); + _needsUpdate = await _technic.CheckUpdate(); if (_needsUpdate) {