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<GoldbergGlobalConfiguration> GetGlobalSettings();
public Task SetGlobalSettings(GoldbergGlobalConfiguration configuration);
public bool GoldbergApplied(string path);
// public Task<bool> Download();
// public Task Extract(string archivePath);
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 _userSteamIdPath = Path.Combine(GlobalSettingsPath, "settings/user_steam_id.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
private readonly List<string> _interfaceNames = new List<string>
@ -90,6 +94,7 @@ namespace GoldbergGUI.Core.Services
{
await Extract(_goldbergZipPath).ConfigureAwait(false);
}
return await GetGlobalSettings().ConfigureAwait(false);
}
@ -110,6 +115,7 @@ namespace GoldbergGUI.Core.Services
{
_log.Error("Invalid User Steam ID!");
}
if (File.Exists(_languagePath)) language = File.ReadLines(_languagePath).First().Trim();
if (File.Exists(_customBroadcastIpsPath))
customBroadcastIps.AddRange(
@ -146,6 +152,7 @@ namespace GoldbergGUI.Core.Services
await File.Create(_accountNamePath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_accountNamePath, DefaultAccountName).ConfigureAwait(false);
}
// User SteamID
if (userSteamId >= 76561197960265729 && userSteamId <= 76561202255233023)
{
@ -161,6 +168,7 @@ namespace GoldbergGUI.Core.Services
await File.Create(_userSteamIdPath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_userSteamIdPath, DefaultSteamId.ToString()).ConfigureAwait(false);
}
// Language
if (!string.IsNullOrEmpty(language))
{
@ -176,6 +184,7 @@ namespace GoldbergGUI.Core.Services
await File.Create(_languagePath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_languagePath, DefaultLanguage).ConfigureAwait(false);
}
// Custom Broadcast IPs
if (customBroadcastIps != null && customBroadcastIps.Count > 0)
{
@ -274,7 +283,8 @@ namespace GoldbergGUI.Core.Services
}
// 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
if (c.DlcList.Count > 0)
@ -365,6 +375,8 @@ namespace GoldbergGUI.Core.Services
var jobIdPath = Path.Combine(_goldbergPath, "job_id");
var match = regex.Match(body);
if (File.Exists(jobIdPath))
{
try
{
_log.Info("Check if update is needed...");
var jobIdLocal = Convert.ToInt32(File.ReadLines(jobIdPath).First().Trim());
@ -376,6 +388,12 @@ namespace GoldbergGUI.Core.Services
return false;
}
}
catch (Exception)
{
_log.Error("An error occured, local Goldberg setup might be broken!");
}
}
_log.Info("Starting download...");
await StartDownload(match.Value).ConfigureAwait(false);
return true;
@ -408,23 +426,44 @@ namespace GoldbergGUI.Core.Services
// Extract all from archive to subfolder ./goldberg/
private async Task Extract(string archivePath)
{
var errorOccured = false;
_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(() =>
{
try
{
Directory.Delete(_goldbergPath, true);
ZipFile.ExtractToDirectory(archivePath, _goldbergPath);
var fullPath = Path.Combine(_goldbergPath, entry.FullName);
if (string.IsNullOrEmpty(entry.Name))
{
Directory.CreateDirectory(fullPath);
}
else
{
entry.ExtractToFile(fullPath, true);
}
}
catch (Exception e)
{
ShowErrorMessage();
errorOccured = true;
_log.Error($"Error while trying to extract {entry.FullName}");
_log.Error(e.ToString);
Environment.Exit(1);
// throw;
}
}).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()
@ -433,6 +472,7 @@ namespace GoldbergGUI.Core.Services
{
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!");