Fixed crashes related to global settings.

This commit is contained in:
Jeddunk 2021-03-21 12:52:22 +01:00
parent aac466802e
commit 19f460d12d

View File

@ -5,7 +5,6 @@ using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using GoldbergGUI.Core.Models; using GoldbergGUI.Core.Models;
using GoldbergGUI.Core.Utils; using GoldbergGUI.Core.Utils;
@ -24,8 +23,8 @@ namespace GoldbergGUI.Core.Services
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);
public List<string> Languages(); public List<string> Languages();
} }
@ -34,8 +33,10 @@ namespace GoldbergGUI.Core.Services
public class GoldbergService : IGoldbergService public class GoldbergService : IGoldbergService
{ {
private IMvxLog _log; private IMvxLog _log;
private const string GoldbergUrl = "https://mr_goldberg.gitlab.io/goldberg_emulator/"; private const string DefaultAccountName = "Mr_Goldberg";
private const long DefaultSteamId = 76561197960287930;
private const string DefaultLanguage = "english"; private const string DefaultLanguage = "english";
private const string GoldbergUrl = "https://mr_goldberg.gitlab.io/goldberg_emulator/";
private readonly string _goldbergZipPath = Path.Combine(Directory.GetCurrentDirectory(), "goldberg.zip"); private readonly string _goldbergZipPath = Path.Combine(Directory.GetCurrentDirectory(), "goldberg.zip");
private readonly string _goldbergPath = Path.Combine(Directory.GetCurrentDirectory(), "goldberg"); private readonly string _goldbergPath = Path.Combine(Directory.GetCurrentDirectory(), "goldberg");
@ -48,6 +49,7 @@ namespace GoldbergGUI.Core.Services
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
private readonly List<string> _interfaceNames = new List<string> private readonly List<string> _interfaceNames = new List<string>
{ {
"SteamClient", "SteamClient",
@ -90,10 +92,11 @@ namespace GoldbergGUI.Core.Services
public async Task<GoldbergGlobalConfiguration> GetGlobalSettings() public async Task<GoldbergGlobalConfiguration> GetGlobalSettings()
{ {
_log.Info("Getting global settings..."); _log.Info("Getting global settings...");
var accountName = "Account name..."; var accountName = DefaultAccountName;
long steamId = -1; var steamId = DefaultSteamId;
var language = DefaultLanguage; var language = DefaultLanguage;
var customBroadcastIps = new List<string>(); var customBroadcastIps = new List<string>();
if (!File.Exists(GlobalSettingsPath)) Directory.CreateDirectory(Path.Join(GlobalSettingsPath, "settings"));
await Task.Run(() => await Task.Run(() =>
{ {
if (File.Exists(_accountNamePath)) accountName = File.ReadLines(_accountNamePath).First().Trim(); if (File.Exists(_accountNamePath)) accountName = File.ReadLines(_accountNamePath).First().Trim();
@ -125,44 +128,58 @@ namespace GoldbergGUI.Core.Services
var customBroadcastIps = c.CustomBroadcastIps; var customBroadcastIps = c.CustomBroadcastIps;
_log.Info("Setting global settings..."); _log.Info("Setting global settings...");
// Account Name // Account Name
if (accountName != null && accountName != "Account name...") if (!string.IsNullOrEmpty(accountName))
{ {
_log.Info("Setting account name..."); _log.Info("Setting account name...");
if (!File.Exists(_accountNamePath))
await File.Create(_accountNamePath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_accountNamePath, accountName).ConfigureAwait(false); await File.WriteAllTextAsync(_accountNamePath, accountName).ConfigureAwait(false);
} }
else else
{ {
_log.Info("Invalid account name! Skipping..."); _log.Info("Invalid account name! Skipping...");
await File.WriteAllTextAsync(_accountNamePath, "Goldberg").ConfigureAwait(false); if (!File.Exists(_accountNamePath))
await File.Create(_accountNamePath).DisposeAsync().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))
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...");
await Task.Run(() => File.Delete(_userSteamIdPath)).ConfigureAwait(false); if (!File.Exists(_userSteamIdPath))
await File.Create(_userSteamIdPath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_userSteamIdPath, DefaultSteamId.ToString()).ConfigureAwait(false);
} }
// Language // Language
if (language != null) if (!string.IsNullOrEmpty(language))
{ {
_log.Info("Setting language..."); _log.Info("Setting language...");
if (!File.Exists(_languagePath))
await File.Create(_languagePath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_languagePath, language).ConfigureAwait(false); await File.WriteAllTextAsync(_languagePath, language).ConfigureAwait(false);
} }
else else
{ {
_log.Info("Invalid language! Skipping..."); _log.Info("Invalid language! Skipping...");
if (!File.Exists(_languagePath))
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.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))
await File.Create(_customBroadcastIpsPath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_customBroadcastIpsPath, result).ConfigureAwait(false); await File.WriteAllTextAsync(_customBroadcastIpsPath, result).ConfigureAwait(false);
} }
else else
@ -329,11 +346,11 @@ namespace GoldbergGUI.Core.Services
return steamSettingsDirExists && steamAppIdTxtExists; return steamSettingsDirExists && steamAppIdTxtExists;
} }
// Get webpage private async Task<bool> Download()
// Get job id, compare with local if exists, save it if false or missing
// Get latest archive if mismatch, call Extract
public async Task<bool> Download()
{ {
// Get webpage
// Get job id, compare with local if exists, save it if false or missing
// Get latest archive if mismatch, call Extract
_log.Info("Initializing download..."); _log.Info("Initializing download...");
if (!Directory.Exists(_goldbergPath)) Directory.CreateDirectory(_goldbergPath); if (!Directory.Exists(_goldbergPath)) Directory.CreateDirectory(_goldbergPath);
var client = new HttpClient(); var client = new HttpClient();
@ -376,7 +393,7 @@ namespace GoldbergGUI.Core.Services
// Empty subfolder ./goldberg/ // Empty subfolder ./goldberg/
// Extract all from archive to subfolder ./goldberg/ // Extract all from archive to subfolder ./goldberg/
public async Task Extract(string archivePath) private async Task Extract(string archivePath)
{ {
_log.Debug("Start extraction..."); _log.Debug("Start extraction...");
await Task.Run(() => await Task.Run(() =>