diff --git a/GoldbergGUI.Core/Services/GoldbergService.cs b/GoldbergGUI.Core/Services/GoldbergService.cs index 4de2f33..a6f5617 100644 --- a/GoldbergGUI.Core/Services/GoldbergService.cs +++ b/GoldbergGUI.Core/Services/GoldbergService.cs @@ -23,7 +23,9 @@ namespace GoldbergGUI.Core.Services public Task Save(string path, GoldbergConfiguration configuration); public Task GetGlobalSettings(); public Task SetGlobalSettings(GoldbergGlobalConfiguration configuration); + public bool GoldbergApplied(string path); + // public Task 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 _interfaceNames = new List @@ -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,21 +152,23 @@ 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) { _log.Info("Setting user Steam ID..."); - if (!File.Exists(_userSteamIdPath)) + if (!File.Exists(_userSteamIdPath)) await File.Create(_userSteamIdPath).DisposeAsync().ConfigureAwait(false); await File.WriteAllTextAsync(_userSteamIdPath, userSteamId.ToString()).ConfigureAwait(false); } else { _log.Info("Invalid user Steam ID! Skipping..."); - if (!File.Exists(_userSteamIdPath)) + if (!File.Exists(_userSteamIdPath)) await File.Create(_userSteamIdPath).DisposeAsync().ConfigureAwait(false); await File.WriteAllTextAsync(_userSteamIdPath, DefaultSteamId.ToString()).ConfigureAwait(false); } + // Language if (!string.IsNullOrEmpty(language)) { @@ -176,11 +184,12 @@ 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) { _log.Info("Setting custom broadcast IPs..."); - var result = + var result = customBroadcastIps.Aggregate("", (current, address) => $"{current}{address}\n"); if (!File.Exists(_customBroadcastIpsPath)) await File.Create(_customBroadcastIpsPath).DisposeAsync().ConfigureAwait(false); @@ -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) @@ -366,16 +376,24 @@ namespace GoldbergGUI.Core.Services var match = regex.Match(body); if (File.Exists(jobIdPath)) { - _log.Info("Check if update is needed..."); - 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)) + try { - _log.Info("Latest Goldberg emulator is already available! Skipping..."); - return false; + _log.Info("Check if update is needed..."); + 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..."); 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..."); - 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); - ZipFile.ExtractToDirectory(archivePath, _goldbergPath); + await Task.Run(() => + { + 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(); - _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!");