Show error message if Goldberg could not be downloaded/extracted, prompting the user to do so manually.

Try to skip getting DLC from SteamDB on error.
This commit is contained in:
Jeddunk 2021-04-06 16:55:53 +02:00
parent 19f460d12d
commit 8e62880e23
2 changed files with 96 additions and 54 deletions

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows;
using GoldbergGUI.Core.Models; using GoldbergGUI.Core.Models;
using GoldbergGUI.Core.Utils; using GoldbergGUI.Core.Utils;
using MvvmCross.Logging; using MvvmCross.Logging;
@ -85,7 +86,10 @@ namespace GoldbergGUI.Core.Services
_log = log; _log = log;
var download = await Download().ConfigureAwait(false); var download = await Download().ConfigureAwait(false);
if (download) await Extract(_goldbergZipPath).ConfigureAwait(false); if (download)
{
await Extract(_goldbergZipPath).ConfigureAwait(false);
}
return await GetGlobalSettings().ConfigureAwait(false); return await GetGlobalSettings().ConfigureAwait(false);
} }
@ -379,15 +383,24 @@ namespace GoldbergGUI.Core.Services
private async Task StartDownload(string downloadUrl) private async Task StartDownload(string downloadUrl)
{ {
var client = new HttpClient(); try
_log.Debug(downloadUrl);
await using var fileStream = File.OpenWrite(_goldbergZipPath);
//client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead)
var task = client.GetFileAsync(downloadUrl, fileStream).ConfigureAwait(false);
await task;
if (task.GetAwaiter().IsCompleted)
{ {
_log.Info("Download finished!"); var client = new HttpClient();
_log.Debug(downloadUrl);
await using var fileStream = File.OpenWrite(_goldbergZipPath);
//client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead)
var task = client.GetFileAsync(downloadUrl, fileStream).ConfigureAwait(false);
await task;
if (task.GetAwaiter().IsCompleted)
{
_log.Info("Download finished!");
}
}
catch (Exception e)
{
ShowErrorMessage();
_log.Error(e.ToString);
Environment.Exit(1);
} }
} }
@ -398,12 +411,33 @@ namespace GoldbergGUI.Core.Services
_log.Debug("Start extraction..."); _log.Debug("Start extraction...");
await Task.Run(() => await Task.Run(() =>
{ {
Directory.Delete(_goldbergPath, true); try
ZipFile.ExtractToDirectory(archivePath, _goldbergPath); {
Directory.Delete(_goldbergPath, true);
ZipFile.ExtractToDirectory(archivePath, _goldbergPath);
}
catch (Exception e)
{
ShowErrorMessage();
_log.Error(e.ToString);
Environment.Exit(1);
// throw;
}
}).ConfigureAwait(false); }).ConfigureAwait(false);
_log.Debug("Extraction done!"); _log.Debug("Extraction done!");
} }
private void ShowErrorMessage()
{
if (Directory.Exists(_goldbergPath))
{
Directory.Delete(_goldbergPath, true);
}
Directory.CreateDirectory(_goldbergPath);
MessageBox.Show("Could not setup Goldberg Emulator!\n" +
"Please download it manually and extract its content into the \"goldberg\" subfolder!");
}
// https://gitlab.com/Mr_Goldberg/goldberg_emulator/-/blob/master/generate_interfaces_file.cpp // https://gitlab.com/Mr_Goldberg/goldberg_emulator/-/blob/master/generate_interfaces_file.cpp
// (maybe) check DLL date first // (maybe) check DLL date first
public async Task GenerateInterfacesFile(string filePath) public async Task GenerateInterfacesFile(string filePath)

View File

@ -219,53 +219,61 @@ namespace GoldbergGUI.Core.Services
// ReSharper disable once InvertIf // ReSharper disable once InvertIf
if (useSteamDb) if (useSteamDb)
{ {
var steamDbUri = new Uri($"https://steamdb.info/app/{steamApp.AppId}/dlc/"); try
var client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
_log.Info("Get SteamDB App");
var httpCall = client.GetAsync(steamDbUri);
var response = await httpCall.ConfigureAwait(false);
_log.Debug(httpCall.Status.ToString());
_log.Debug(response.EnsureSuccessStatusCode().ToString());
var readAsStringAsync = response.Content.ReadAsStringAsync();
var responseBody = await readAsStringAsync.ConfigureAwait(false);
_log.Debug(readAsStringAsync.Status.ToString());
var parser = new HtmlParser();
var doc = parser.ParseDocument(responseBody);
var query1 = doc.QuerySelector("#dlc");
if (query1 != null)
{ {
var query2 = query1.QuerySelectorAll(".app"); var steamDbUri = new Uri($"https://steamdb.info/app/{steamApp.AppId}/dlc/");
foreach (var element in query2)
{
var dlcId = element.GetAttribute("data-appid");
var query3 = element.QuerySelectorAll("td");
var dlcName = query3 != null
? query3[1].Text().Replace("\n", "").Trim()
: $"Unknown DLC {dlcId}";
var dlcApp = new SteamApp {AppId = Convert.ToInt32(dlcId), Name = dlcName};
var i = dlcList.FindIndex(x => x.AppId.Equals(dlcApp.AppId));
if (i > -1)
{
if (dlcList[i].Name.Contains("Unknown DLC")) dlcList[i] = dlcApp;
}
else
{
dlcList.Add(dlcApp);
}
}
dlcList.ForEach(x => _log.Debug($"{x.AppId}={x.Name}")); var client = new HttpClient();
_log.Info("Got DLC from SteamDB successfully..."); client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
_log.Info("Get SteamDB App");
var httpCall = client.GetAsync(steamDbUri);
var response = await httpCall.ConfigureAwait(false);
_log.Debug(httpCall.Status.ToString());
_log.Debug(response.EnsureSuccessStatusCode().ToString());
var readAsStringAsync = response.Content.ReadAsStringAsync();
var responseBody = await readAsStringAsync.ConfigureAwait(false);
_log.Debug(readAsStringAsync.Status.ToString());
var parser = new HtmlParser();
var doc = parser.ParseDocument(responseBody);
var query1 = doc.QuerySelector("#dlc");
if (query1 != null)
{
var query2 = query1.QuerySelectorAll(".app");
foreach (var element in query2)
{
var dlcId = element.GetAttribute("data-appid");
var query3 = element.QuerySelectorAll("td");
var dlcName = query3 != null
? query3[1].Text().Replace("\n", "").Trim()
: $"Unknown DLC {dlcId}";
var dlcApp = new SteamApp {AppId = Convert.ToInt32(dlcId), Name = dlcName};
var i = dlcList.FindIndex(x => x.AppId.Equals(dlcApp.AppId));
if (i > -1)
{
if (dlcList[i].Name.Contains("Unknown DLC")) dlcList[i] = dlcApp;
}
else
{
dlcList.Add(dlcApp);
}
}
dlcList.ForEach(x => _log.Debug($"{x.AppId}={x.Name}"));
_log.Info("Got DLC from SteamDB successfully...");
}
else
{
_log.Error("Could not get DLC from SteamDB!");
}
} }
else catch (Exception e)
{ {
_log.Error("Could not get DLC from SteamDB!"); _log.Error("Could not get DLC from SteamDB!");
_log.Error(e.ToString);
} }
} }
} }