Improved extraction.

This commit is contained in:
Jeddunk 2021-04-07 14:22:45 +02:00
parent 653e97beab
commit 62abe0e212

View File

@ -23,7 +23,9 @@ namespace GoldbergGUI.Core.Services
public Task Save(string path, GoldbergConfiguration configuration); public Task Save(string path, GoldbergConfiguration configuration);
public Task<GoldbergGlobalConfiguration> GetGlobalSettings(); public Task<GoldbergGlobalConfiguration> GetGlobalSettings();
public Task SetGlobalSettings(GoldbergGlobalConfiguration configuration); public Task SetGlobalSettings(GoldbergGlobalConfiguration configuration);
public bool GoldbergApplied(string path); public bool GoldbergApplied(string path);
// public Task<bool> Download(); // public Task<bool> Download();
// public Task Extract(string archivePath); // public Task Extract(string archivePath);
public Task GenerateInterfacesFile(string filePath); public Task GenerateInterfacesFile(string filePath);
@ -48,7 +50,9 @@ namespace GoldbergGUI.Core.Services
private readonly string _accountNamePath = Path.Combine(GlobalSettingsPath, "settings/account_name.txt"); private readonly string _accountNamePath = Path.Combine(GlobalSettingsPath, "settings/account_name.txt");
private readonly string _userSteamIdPath = Path.Combine(GlobalSettingsPath, "settings/user_steam_id.txt"); private readonly string _userSteamIdPath = Path.Combine(GlobalSettingsPath, "settings/user_steam_id.txt");
private readonly string _languagePath = Path.Combine(GlobalSettingsPath, "settings/language.txt"); private readonly string _languagePath = Path.Combine(GlobalSettingsPath, "settings/language.txt");
private readonly string _customBroadcastIpsPath = Path.Combine(GlobalSettingsPath, "settings/custom_broadcasts.txt");
private readonly string _customBroadcastIpsPath =
Path.Combine(GlobalSettingsPath, "settings/custom_broadcasts.txt");
// ReSharper disable StringLiteralTypo // ReSharper disable StringLiteralTypo
private readonly List<string> _interfaceNames = new List<string> private readonly List<string> _interfaceNames = new List<string>
@ -90,6 +94,7 @@ namespace GoldbergGUI.Core.Services
{ {
await Extract(_goldbergZipPath).ConfigureAwait(false); await Extract(_goldbergZipPath).ConfigureAwait(false);
} }
return await GetGlobalSettings().ConfigureAwait(false); return await GetGlobalSettings().ConfigureAwait(false);
} }
@ -110,6 +115,7 @@ namespace GoldbergGUI.Core.Services
{ {
_log.Error("Invalid User Steam ID!"); _log.Error("Invalid User Steam ID!");
} }
if (File.Exists(_languagePath)) language = File.ReadLines(_languagePath).First().Trim(); if (File.Exists(_languagePath)) language = File.ReadLines(_languagePath).First().Trim();
if (File.Exists(_customBroadcastIpsPath)) if (File.Exists(_customBroadcastIpsPath))
customBroadcastIps.AddRange( customBroadcastIps.AddRange(
@ -146,6 +152,7 @@ namespace GoldbergGUI.Core.Services
await File.Create(_accountNamePath).DisposeAsync().ConfigureAwait(false); await File.Create(_accountNamePath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_accountNamePath, DefaultAccountName).ConfigureAwait(false); await File.WriteAllTextAsync(_accountNamePath, DefaultAccountName).ConfigureAwait(false);
} }
// User SteamID // User SteamID
if (userSteamId >= 76561197960265729 && userSteamId <= 76561202255233023) if (userSteamId >= 76561197960265729 && userSteamId <= 76561202255233023)
{ {
@ -161,6 +168,7 @@ namespace GoldbergGUI.Core.Services
await File.Create(_userSteamIdPath).DisposeAsync().ConfigureAwait(false); await File.Create(_userSteamIdPath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_userSteamIdPath, DefaultSteamId.ToString()).ConfigureAwait(false); await File.WriteAllTextAsync(_userSteamIdPath, DefaultSteamId.ToString()).ConfigureAwait(false);
} }
// Language // Language
if (!string.IsNullOrEmpty(language)) if (!string.IsNullOrEmpty(language))
{ {
@ -176,6 +184,7 @@ namespace GoldbergGUI.Core.Services
await File.Create(_languagePath).DisposeAsync().ConfigureAwait(false); await File.Create(_languagePath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_languagePath, DefaultLanguage).ConfigureAwait(false); await File.WriteAllTextAsync(_languagePath, DefaultLanguage).ConfigureAwait(false);
} }
// Custom Broadcast IPs // Custom Broadcast IPs
if (customBroadcastIps != null && customBroadcastIps.Count > 0) if (customBroadcastIps != null && customBroadcastIps.Count > 0)
{ {
@ -274,7 +283,8 @@ namespace GoldbergGUI.Core.Services
} }
// create steam_appid.txt // create steam_appid.txt
await File.WriteAllTextAsync(Path.Combine(path, "steam_appid.txt"), c.AppId.ToString()).ConfigureAwait(false); await File.WriteAllTextAsync(Path.Combine(path, "steam_appid.txt"), c.AppId.ToString())
.ConfigureAwait(false);
// DLC // DLC
if (c.DlcList.Count > 0) if (c.DlcList.Count > 0)
@ -365,6 +375,8 @@ namespace GoldbergGUI.Core.Services
var jobIdPath = Path.Combine(_goldbergPath, "job_id"); var jobIdPath = Path.Combine(_goldbergPath, "job_id");
var match = regex.Match(body); var match = regex.Match(body);
if (File.Exists(jobIdPath)) if (File.Exists(jobIdPath))
{
try
{ {
_log.Info("Check if update is needed..."); _log.Info("Check if update is needed...");
var jobIdLocal = Convert.ToInt32(File.ReadLines(jobIdPath).First().Trim()); var jobIdLocal = Convert.ToInt32(File.ReadLines(jobIdPath).First().Trim());
@ -376,6 +388,12 @@ namespace GoldbergGUI.Core.Services
return false; return false;
} }
} }
catch (Exception)
{
_log.Error("An error occured, local Goldberg setup might be broken!");
}
}
_log.Info("Starting download..."); _log.Info("Starting download...");
await StartDownload(match.Value).ConfigureAwait(false); await StartDownload(match.Value).ConfigureAwait(false);
return true; return true;
@ -408,23 +426,44 @@ namespace GoldbergGUI.Core.Services
// Extract all from archive to subfolder ./goldberg/ // Extract all from archive to subfolder ./goldberg/
private async Task Extract(string archivePath) private async Task Extract(string archivePath)
{ {
var errorOccured = false;
_log.Debug("Start extraction..."); _log.Debug("Start extraction...");
Directory.Delete(_goldbergPath, true);
Directory.CreateDirectory(_goldbergPath);
using (var archive = await Task.Run(() => ZipFile.OpenRead(archivePath)).ConfigureAwait(false))
{
foreach (var entry in archive.Entries)
{
await Task.Run(() => await Task.Run(() =>
{ {
try try
{ {
Directory.Delete(_goldbergPath, true); var fullPath = Path.Combine(_goldbergPath, entry.FullName);
ZipFile.ExtractToDirectory(archivePath, _goldbergPath); if (string.IsNullOrEmpty(entry.Name))
{
Directory.CreateDirectory(fullPath);
}
else
{
entry.ExtractToFile(fullPath, true);
}
} }
catch (Exception e) catch (Exception e)
{ {
ShowErrorMessage(); errorOccured = true;
_log.Error($"Error while trying to extract {entry.FullName}");
_log.Error(e.ToString); _log.Error(e.ToString);
Environment.Exit(1);
// throw;
} }
}).ConfigureAwait(false); }).ConfigureAwait(false);
_log.Debug("Extraction done!"); }
}
if (errorOccured)
{
ShowErrorMessage();
_log.Warn("Error occured while extraction! Please setup Goldberg manually");
}
_log.Info("Archive extracted successfully!");
} }
private void ShowErrorMessage() private void ShowErrorMessage()
@ -433,6 +472,7 @@ namespace GoldbergGUI.Core.Services
{ {
Directory.Delete(_goldbergPath, true); Directory.Delete(_goldbergPath, true);
} }
Directory.CreateDirectory(_goldbergPath); Directory.CreateDirectory(_goldbergPath);
MessageBox.Show("Could not setup Goldberg Emulator!\n" + MessageBox.Show("Could not setup Goldberg Emulator!\n" +
"Please download it manually and extract its content into the \"goldberg\" subfolder!"); "Please download it manually and extract its content into the \"goldberg\" subfolder!");