Improved logging

This commit is contained in:
Jeddunk 2021-01-13 21:26:35 +01:00
parent 3033cb6a73
commit fcf966f434
2 changed files with 75 additions and 30 deletions

View File

@ -87,6 +87,7 @@ namespace GoldbergGUI.Core.Services
public async Task<(string accountName, long steamId, string language)> GetGlobalSettings() public async Task<(string accountName, long steamId, string language)> GetGlobalSettings()
{ {
_log.Info("Getting global settings...");
var accountName = "Account name..."; var accountName = "Account name...";
long steamId = -1; long steamId = -1;
var language = DefaultLanguage; var language = DefaultLanguage;
@ -104,38 +105,67 @@ namespace GoldbergGUI.Core.Services
public async Task SetGlobalSettings(string accountName, long userSteamId, string language) public async Task SetGlobalSettings(string accountName, long userSteamId, string language)
{ {
_log.Info("Setting global settings...");
if (accountName != null && accountName != "Account name...") if (accountName != null && accountName != "Account name...")
{
_log.Info("Setting account name...");
await File.WriteAllTextAsync(_accountNamePath, accountName).ConfigureAwait(false); await File.WriteAllTextAsync(_accountNamePath, accountName).ConfigureAwait(false);
}
else else
{
_log.Info("Invalid account name! Skipping...");
await File.WriteAllTextAsync(_accountNamePath, "Goldberg").ConfigureAwait(false); await File.WriteAllTextAsync(_accountNamePath, "Goldberg").ConfigureAwait(false);
}
if (userSteamId >= 76561197960265729 && userSteamId <= 76561202255233023) if (userSteamId >= 76561197960265729 && userSteamId <= 76561202255233023)
{
_log.Info("Setting user Steam ID...");
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...");
await Task.Run(() => File.Delete(_userSteamIdPath)).ConfigureAwait(false); await Task.Run(() => File.Delete(_userSteamIdPath)).ConfigureAwait(false);
}
if (language != null) if (language != null)
{
_log.Info("Setting language...");
await File.WriteAllTextAsync(_languagePath, language).ConfigureAwait(false); await File.WriteAllTextAsync(_languagePath, language).ConfigureAwait(false);
}
else else
{
_log.Info("Invalid language! Skipping...");
await File.WriteAllTextAsync(_languagePath, DefaultLanguage).ConfigureAwait(false); await File.WriteAllTextAsync(_languagePath, DefaultLanguage).ConfigureAwait(false);
}
} }
// If first time, call GenerateInterfaces // If first time, call GenerateInterfaces
// else try to read config // else try to read config
public async Task<GoldbergConfiguration> Read(string path) public async Task<GoldbergConfiguration> Read(string path)
{ {
_log.Info("Reading configuration...");
var appId = -1; var appId = -1;
var dlcList = new List<SteamApp>(); var dlcList = new List<SteamApp>();
var steamAppidTxt = Path.Combine(path, "steam_appid.txt"); var steamAppidTxt = Path.Combine(path, "steam_appid.txt");
if (File.Exists(steamAppidTxt)) if (File.Exists(steamAppidTxt))
{ {
_log.Info("Getting AppID...");
await Task.Run(() => int.TryParse(File.ReadLines(steamAppidTxt).First().Trim(), out appId)) await Task.Run(() => int.TryParse(File.ReadLines(steamAppidTxt).First().Trim(), out appId))
.ConfigureAwait(false); .ConfigureAwait(false);
} }
else
{
_log.Info(@"""steam_appid.txt"" missing! Skipping...");
}
var dlcTxt = Path.Combine(path, "steam_settings", "DLC.txt"); var dlcTxt = Path.Combine(path, "steam_settings", "DLC.txt");
if (File.Exists(dlcTxt)) if (File.Exists(dlcTxt))
{ {
_log.Info("Getting DLCs...");
var readAllLinesAsync = await File.ReadAllLinesAsync(dlcTxt).ConfigureAwait(false); var readAllLinesAsync = await File.ReadAllLinesAsync(dlcTxt).ConfigureAwait(false);
var expression = new Regex(@"(?<id>.*) *= *(?<name>.*)"); var expression = new Regex(@"(?<id>.*) *= *(?<name>.*)");
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var line in readAllLinesAsync) foreach (var line in readAllLinesAsync)
{ {
var match = expression.Match(line); var match = expression.Match(line);
@ -147,6 +177,10 @@ namespace GoldbergGUI.Core.Services
}); });
} }
} }
else
{
_log.Info(@"""steam_settings/DLC.txt"" missing! Skipping...");
}
return new GoldbergConfiguration return new GoldbergConfiguration
{ {
@ -164,7 +198,9 @@ namespace GoldbergGUI.Core.Services
// Save configuration files // Save configuration files
public async Task Save(string path, GoldbergConfiguration c) public async Task Save(string path, GoldbergConfiguration c)
{ {
_log.Info("Saving configuration...");
// DLL setup // DLL setup
_log.Info("Running DLL setup...");
const string x86Name = "steam_api"; const string x86Name = "steam_api";
const string x64Name = "steam_api64"; const string x64Name = "steam_api64";
if (File.Exists(Path.Combine(path, $"{x86Name}.dll"))) if (File.Exists(Path.Combine(path, $"{x86Name}.dll")))
@ -178,6 +214,7 @@ namespace GoldbergGUI.Core.Services
} }
// Create steam_settings folder if missing // Create steam_settings folder if missing
_log.Info("Saving settings...");
if (!Directory.Exists(Path.Combine(path, "steam_settings"))) if (!Directory.Exists(Path.Combine(path, "steam_settings")))
{ {
Directory.CreateDirectory(Path.Combine(path, "steam_settings")); Directory.CreateDirectory(Path.Combine(path, "steam_settings"));
@ -256,6 +293,7 @@ namespace GoldbergGUI.Core.Services
{ {
var steamSettingsDirExists = Directory.Exists(Path.Combine(path, "steam_settings")); var steamSettingsDirExists = Directory.Exists(Path.Combine(path, "steam_settings"));
var steamAppIdTxtExists = File.Exists(Path.Combine(path, "steam_appid.txt")); var steamAppIdTxtExists = File.Exists(Path.Combine(path, "steam_appid.txt"));
_log.Debug($"Goldberg applied? {(steamSettingsDirExists && steamAppIdTxtExists).ToString()}");
return steamSettingsDirExists && steamAppIdTxtExists; return steamSettingsDirExists && steamAppIdTxtExists;
} }
@ -264,8 +302,7 @@ namespace GoldbergGUI.Core.Services
// Get latest archive if mismatch, call Extract // Get latest archive if mismatch, call Extract
public async Task<bool> Download() public async Task<bool> Download()
{ {
var value = false; _log.Info("Initializing download...");
_log.Debug("Download");
if (!Directory.Exists(_goldbergPath)) Directory.CreateDirectory(_goldbergPath); if (!Directory.Exists(_goldbergPath)) Directory.CreateDirectory(_goldbergPath);
var client = new HttpClient(); var client = new HttpClient();
var response = await client.GetAsync(GoldbergUrl).ConfigureAwait(false); var response = await client.GetAsync(GoldbergUrl).ConfigureAwait(false);
@ -274,25 +311,21 @@ namespace GoldbergGUI.Core.Services
@"https:\/\/gitlab\.com\/Mr_Goldberg\/goldberg_emulator\/-\/jobs\/(?<jobid>.*)\/artifacts\/download"); @"https:\/\/gitlab\.com\/Mr_Goldberg\/goldberg_emulator\/-\/jobs\/(?<jobid>.*)\/artifacts\/download");
var jobIdPath = Path.Combine(_goldbergPath, "job_id"); var jobIdPath = Path.Combine(_goldbergPath, "job_id");
var match = regex.Match(body); var match = regex.Match(body);
var downloadUrl = match.Value;
if (File.Exists(jobIdPath)) if (File.Exists(jobIdPath))
{ {
_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());
var jobIdRemote = Convert.ToInt32(match.Groups["jobid"].Value); var jobIdRemote = Convert.ToInt32(match.Groups["jobid"].Value);
_log.Debug($"job_id: local {jobIdLocal}; remote {jobIdRemote}"); _log.Debug($"job_id: local {jobIdLocal}; remote {jobIdRemote}");
if (!jobIdLocal.Equals(jobIdRemote)) if (jobIdLocal.Equals(jobIdRemote))
{ {
await StartDownload(client, downloadUrl).ConfigureAwait(false); _log.Info("Latest Goldberg emulator is already available! Skipping...");
value = true; return false;
} }
} }
else _log.Info("Starting download...");
{ await StartDownload(client, match.Value).ConfigureAwait(false);
await StartDownload(client, downloadUrl).ConfigureAwait(false); return true;
value = true;
}
return value;
} }
private async Task StartDownload(HttpClient client, string downloadUrl) private async Task StartDownload(HttpClient client, string downloadUrl)
@ -322,7 +355,7 @@ namespace GoldbergGUI.Core.Services
// Extract all from archive to subfolder ./goldberg/ // Extract all from archive to subfolder ./goldberg/
public async Task Extract(string archivePath) public async Task Extract(string archivePath)
{ {
_log.Debug("Extract"); _log.Debug("Start extraction...");
await Task.Run(() => await Task.Run(() =>
{ {
Directory.Delete(_goldbergPath, true); Directory.Delete(_goldbergPath, true);

View File

@ -36,6 +36,8 @@ namespace GoldbergGUI.Core.ViewModels
private bool _disableNetworking; private bool _disableNetworking;
private bool _disableOverlay; private bool _disableOverlay;
private string _statusText;
private readonly ISteamService _steam; private readonly ISteamService _steam;
private readonly IGoldbergService _goldberg; private readonly IGoldbergService _goldberg;
private readonly IMvxLog _log; private readonly IMvxLog _log;
@ -240,6 +242,16 @@ namespace GoldbergGUI.Core.ViewModels
public string AboutVersionText => public string AboutVersionText =>
FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion; FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
public string StatusText
{
get => _statusText;
set
{
_statusText = value;
RaisePropertyChanged(() => StatusText);
}
}
// COMMANDS // // COMMANDS //
public IMvxCommand OpenFileCommand => new MvxAsyncCommand(OpenFile); public IMvxCommand OpenFileCommand => new MvxAsyncCommand(OpenFile);
@ -267,6 +279,18 @@ namespace GoldbergGUI.Core.ViewModels
private async Task FindId() private async Task FindId()
{ {
async Task FindIdInList(SteamApp[] steamApps)
{
var navigateTask = _navigationService
.Navigate<SearchResultViewModel, IEnumerable<SteamApp>, SteamApp>(steamApps);
var navigateResult = await navigateTask.ConfigureAwait(false);
if (navigateResult != null)
{
GameName = navigateResult.Name;
AppId = navigateResult.AppId;
}
}
if (GameName.Contains("Game name...")) if (GameName.Contains("Game name..."))
{ {
_log.Error("No game name entered!"); _log.Error("No game name entered!");
@ -282,7 +306,6 @@ namespace GoldbergGUI.Core.ViewModels
} }
else else
{ {
var startSearch = false;
var list = _steam.GetListOfAppsByName(GameName); var list = _steam.GetListOfAppsByName(GameName);
var steamApps = list as SteamApp[] ?? list.ToArray(); var steamApps = list as SteamApp[] ?? list.ToArray();
if (steamApps.Length == 1) if (steamApps.Length == 1)
@ -295,24 +318,12 @@ namespace GoldbergGUI.Core.ViewModels
} }
else else
{ {
startSearch = true; await FindIdInList(steamApps).ConfigureAwait(false);
} }
} }
else else
{ {
startSearch = true; await FindIdInList(steamApps).ConfigureAwait(false);
}
if (startSearch)
{
var navigateTask = _navigationService
.Navigate<SearchResultViewModel, IEnumerable<SteamApp>, SteamApp>(steamApps);
var navigateResult = await navigateTask.ConfigureAwait(false);
if (navigateResult != null)
{
GameName = navigateResult.Name;
AppId = navigateResult.AppId;
}
} }
} }
@ -354,6 +365,7 @@ namespace GoldbergGUI.Core.ViewModels
private async Task SaveConfig() private async Task SaveConfig()
{ {
_log.Info("Saving global settings...");
await _goldberg.SetGlobalSettings(AccountName, SteamId, SelectedLanguage).ConfigureAwait(false); await _goldberg.SetGlobalSettings(AccountName, SteamId, SelectedLanguage).ConfigureAwait(false);
if (!DllSelected) if (!DllSelected)
{ {
@ -361,7 +373,7 @@ namespace GoldbergGUI.Core.ViewModels
return; return;
} }
_log.Info("Saving..."); _log.Info("Saving Goldberg settings...");
if (!GetDllPathDir(out var dirPath)) return; if (!GetDllPathDir(out var dirPath)) return;
MainWindowEnabled = false; MainWindowEnabled = false;
await _goldberg.Save(dirPath, new GoldbergConfiguration await _goldberg.Save(dirPath, new GoldbergConfiguration