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,21 +152,23 @@ 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)
{ {
_log.Info("Setting user Steam ID..."); _log.Info("Setting user Steam ID...");
if (!File.Exists(_userSteamIdPath)) if (!File.Exists(_userSteamIdPath))
await File.Create(_userSteamIdPath).DisposeAsync().ConfigureAwait(false); await File.Create(_userSteamIdPath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_userSteamIdPath, userSteamId.ToString()).ConfigureAwait(false); await File.WriteAllTextAsync(_userSteamIdPath, userSteamId.ToString()).ConfigureAwait(false);
} }
else else
{ {
_log.Info("Invalid user Steam ID! Skipping..."); _log.Info("Invalid user Steam ID! Skipping...");
if (!File.Exists(_userSteamIdPath)) if (!File.Exists(_userSteamIdPath))
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,11 +184,12 @@ 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)
{ {
_log.Info("Setting custom broadcast IPs..."); _log.Info("Setting custom broadcast IPs...");
var result = var result =
customBroadcastIps.Aggregate("", (current, address) => $"{current}{address}\n"); customBroadcastIps.Aggregate("", (current, address) => $"{current}{address}\n");
if (!File.Exists(_customBroadcastIpsPath)) if (!File.Exists(_customBroadcastIpsPath))
await File.Create(_customBroadcastIpsPath).DisposeAsync().ConfigureAwait(false); await File.Create(_customBroadcastIpsPath).DisposeAsync().ConfigureAwait(false);
@ -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)
@ -366,16 +376,24 @@ namespace GoldbergGUI.Core.Services
var match = regex.Match(body); var match = regex.Match(body);
if (File.Exists(jobIdPath)) if (File.Exists(jobIdPath))
{ {
_log.Info("Check if update is needed..."); try
var jobIdLocal = Convert.ToInt32(File.ReadLines(jobIdPath).First().Trim());
var jobIdRemote = Convert.ToInt32(match.Groups["jobid"].Value);
_log.Debug($"job_id: local {jobIdLocal}; remote {jobIdRemote}");
if (jobIdLocal.Equals(jobIdRemote))
{ {
_log.Info("Latest Goldberg emulator is already available! Skipping..."); _log.Info("Check if update is needed...");
return false; var jobIdLocal = Convert.ToInt32(File.ReadLines(jobIdPath).First().Trim());
var jobIdRemote = Convert.ToInt32(match.Groups["jobid"].Value);
_log.Debug($"job_id: local {jobIdLocal}; remote {jobIdRemote}");
if (jobIdLocal.Equals(jobIdRemote))
{
_log.Info("Latest Goldberg emulator is already available! Skipping...");
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...");
await Task.Run(() => Directory.Delete(_goldbergPath, true);
Directory.CreateDirectory(_goldbergPath);
using (var archive = await Task.Run(() => ZipFile.OpenRead(archivePath)).ConfigureAwait(false))
{ {
try foreach (var entry in archive.Entries)
{ {
Directory.Delete(_goldbergPath, true); await Task.Run(() =>
ZipFile.ExtractToDirectory(archivePath, _goldbergPath); {
try
{
var fullPath = Path.Combine(_goldbergPath, entry.FullName);
if (string.IsNullOrEmpty(entry.Name))
{
Directory.CreateDirectory(fullPath);
}
else
{
entry.ExtractToFile(fullPath, true);
}
}
catch (Exception e)
{
errorOccured = true;
_log.Error($"Error while trying to extract {entry.FullName}");
_log.Error(e.ToString);
}
}).ConfigureAwait(false);
} }
catch (Exception e) }
{
ShowErrorMessage(); if (errorOccured)
_log.Error(e.ToString); {
Environment.Exit(1); ShowErrorMessage();
// throw; _log.Warn("Error occured while extraction! Please setup Goldberg manually");
} }
}).ConfigureAwait(false); _log.Info("Archive extracted successfully!");
_log.Debug("Extraction done!");
} }
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!");