Added Extensions.cs.

Added GoldbergGlobalConfiguration.
Code cleanup.
This commit is contained in:
Jeddunk 2021-01-15 17:46:13 +01:00
parent a34ed8881d
commit 508d23da5f
5 changed files with 74 additions and 44 deletions

1
.gitignore vendored
View File

@ -542,3 +542,4 @@ MigrationBackup/
# End of https://www.toptal.com/developers/gitignore/api/intellij,rider,visualstudio,dotnetcore,windows # End of https://www.toptal.com/developers/gitignore/api/intellij,rider,visualstudio,dotnetcore,windows
/GoldbergGUI.Core/Utils/Secrets.cs /GoldbergGUI.Core/Utils/Secrets.cs
/README.bbcode

View File

@ -2,6 +2,12 @@ using System.Collections.Generic;
namespace GoldbergGUI.Core.Models namespace GoldbergGUI.Core.Models
{ {
public class GoldbergGlobalConfiguration
{
public string AccountName { get; set; }
public long UserSteamId { get; set; }
public string Language { get; set; }
}
public class GoldbergConfiguration public class GoldbergConfiguration
{ {
public int AppId { get; set; } public int AppId { get; set; }

View File

@ -8,6 +8,7 @@ using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using GoldbergGUI.Core.Models; using GoldbergGUI.Core.Models;
using GoldbergGUI.Core.Utils;
using MvvmCross.Logging; using MvvmCross.Logging;
namespace GoldbergGUI.Core.Services namespace GoldbergGUI.Core.Services
@ -17,11 +18,11 @@ namespace GoldbergGUI.Core.Services
// does file copy stuff // does file copy stuff
public interface IGoldbergService public interface IGoldbergService
{ {
public Task<(string accountName, long userSteamId, string language)> Initialize(IMvxLog log); public Task<GoldbergGlobalConfiguration> Initialize(IMvxLog log);
public Task<GoldbergConfiguration> Read(string path); public Task<GoldbergConfiguration> Read(string path);
public Task Save(string path, GoldbergConfiguration configuration); public Task Save(string path, GoldbergConfiguration configuration);
public Task<(string accountName, long steamId, string language)> GetGlobalSettings(); public Task<GoldbergGlobalConfiguration> GetGlobalSettings();
public Task SetGlobalSettings(string accountName, long userSteamId, string language); 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);
@ -76,7 +77,7 @@ namespace GoldbergGUI.Core.Services
// Call Download // Call Download
// Get global settings // Get global settings
public async Task<(string accountName, long userSteamId, string language)> Initialize(IMvxLog log) public async Task<GoldbergGlobalConfiguration> Initialize(IMvxLog log)
{ {
_log = log; _log = log;
@ -85,7 +86,7 @@ namespace GoldbergGUI.Core.Services
return await GetGlobalSettings().ConfigureAwait(false); return await GetGlobalSettings().ConfigureAwait(false);
} }
public async Task<(string accountName, long steamId, string language)> GetGlobalSettings() public async Task<GoldbergGlobalConfiguration> GetGlobalSettings()
{ {
_log.Info("Getting global settings..."); _log.Info("Getting global settings...");
var accountName = "Account name..."; var accountName = "Account name...";
@ -100,11 +101,19 @@ 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();
}).ConfigureAwait(false); }).ConfigureAwait(false);
return (accountName, steamId, language); return new GoldbergGlobalConfiguration
{
AccountName = accountName,
UserSteamId = steamId,
Language = language
};
} }
public async Task SetGlobalSettings(string accountName, long userSteamId, string language) public async Task SetGlobalSettings(GoldbergGlobalConfiguration c)
{ {
var accountName = c.AccountName;
var userSteamId = c.UserSteamId;
var language = c.Language;
_log.Info("Setting global settings..."); _log.Info("Setting global settings...");
if (accountName != null && accountName != "Account name...") if (accountName != null && accountName != "Account name...")
{ {
@ -324,16 +333,17 @@ namespace GoldbergGUI.Core.Services
} }
} }
_log.Info("Starting download..."); _log.Info("Starting download...");
await StartDownload(client, match.Value).ConfigureAwait(false); await StartDownload(match.Value).ConfigureAwait(false);
return true; return true;
} }
private async Task StartDownload(HttpClient client, string downloadUrl) private async Task StartDownload(string downloadUrl)
{ {
var client = new HttpClient();
_log.Debug(downloadUrl); _log.Debug(downloadUrl);
await using var fileStream = File.OpenWrite(_goldbergZipPath); await using var fileStream = File.OpenWrite(_goldbergZipPath);
//client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead) //client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead)
var task = GetFileAsync(client, downloadUrl, fileStream).ConfigureAwait(false); var task = client.GetFileAsync(downloadUrl, fileStream).ConfigureAwait(false);
await task; await task;
if (task.GetAwaiter().IsCompleted) if (task.GetAwaiter().IsCompleted)
{ {
@ -341,16 +351,6 @@ namespace GoldbergGUI.Core.Services
} }
} }
private static async Task GetFileAsync(HttpClient client, string requestUri, Stream destination,
CancellationToken cancelToken = default)
{
var response = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancelToken)
.ConfigureAwait(false);
await using var download = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
await download.CopyToAsync(destination, cancelToken).ConfigureAwait(false);
if (destination.CanSeek) destination.Position = 0;
}
// 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) public async Task Extract(string archivePath)
@ -361,7 +361,7 @@ namespace GoldbergGUI.Core.Services
Directory.Delete(_goldbergPath, true); Directory.Delete(_goldbergPath, true);
ZipFile.ExtractToDirectory(archivePath, _goldbergPath); ZipFile.ExtractToDirectory(archivePath, _goldbergPath);
}).ConfigureAwait(false); }).ConfigureAwait(false);
_log.Debug("Extract done!"); _log.Debug("Extraction done!");
} }
// https://gitlab.com/Mr_Goldberg/goldberg_emulator/-/blob/master/generate_interfaces_file.cpp // https://gitlab.com/Mr_Goldberg/goldberg_emulator/-/blob/master/generate_interfaces_file.cpp

View File

@ -0,0 +1,20 @@
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace GoldbergGUI.Core.Utils
{
public static class Extensions
{
public static async Task GetFileAsync(this HttpClient client, string requestUri, Stream destination,
CancellationToken cancelToken = default)
{
var response = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancelToken)
.ConfigureAwait(false);
await using var download = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
await download.CopyToAsync(destination, cancelToken).ConfigureAwait(false);
if (destination.CanSeek) destination.Position = 0;
}
}
}

View File

@ -12,7 +12,6 @@ using System.Windows;
using GoldbergGUI.Core.Models; using GoldbergGUI.Core.Models;
using GoldbergGUI.Core.Services; using GoldbergGUI.Core.Services;
using Microsoft.Win32; using Microsoft.Win32;
using MvvmCross;
using MvvmCross.Commands; using MvvmCross.Commands;
using MvvmCross.Logging; using MvvmCross.Logging;
using MvvmCross.Navigation; using MvvmCross.Navigation;
@ -71,11 +70,11 @@ namespace GoldbergGUI.Core.ViewModels
SteamLanguages = new ObservableCollection<string>(_goldberg.Languages()); SteamLanguages = new ObservableCollection<string>(_goldberg.Languages());
ResetForm(); ResetForm();
await _steam.Initialize(_logProvider.GetLogFor<SteamService>()).ConfigureAwait(false); await _steam.Initialize(_logProvider.GetLogFor<SteamService>()).ConfigureAwait(false);
var (accountName, userSteamId, language) = var globalConfiguration =
await _goldberg.Initialize(_logProvider.GetLogFor<GoldbergService>()).ConfigureAwait(false); await _goldberg.Initialize(_logProvider.GetLogFor<GoldbergService>()).ConfigureAwait(false);
AccountName = accountName; AccountName = globalConfiguration.AccountName;
SteamId = userSteamId; SteamId = globalConfiguration.UserSteamId;
SelectedLanguage = language; SelectedLanguage = globalConfiguration.Language;
} }
catch (Exception e) catch (Exception e)
{ {
@ -221,7 +220,15 @@ namespace GoldbergGUI.Core.ViewModels
} }
} }
public bool DllSelected => !DllPath.Contains("Path to game's steam_api(64).dll"); public bool DllSelected
{
get
{
var value = !DllPath.Contains("Path to game's steam_api(64).dll");
if (!value) _log.Warn("No DLL selected! Skipping...");
return value;
}
}
public ObservableCollection<string> SteamLanguages public ObservableCollection<string> SteamLanguages
{ {
@ -389,12 +396,14 @@ namespace GoldbergGUI.Core.ViewModels
private async Task SaveConfig() private async Task SaveConfig()
{ {
_log.Info("Saving global settings..."); _log.Info("Saving global settings...");
await _goldberg.SetGlobalSettings(AccountName, SteamId, SelectedLanguage).ConfigureAwait(false); var globalConfiguration = new GoldbergGlobalConfiguration
if (!DllSelected)
{ {
_log.Error("No DLL selected!"); AccountName = AccountName,
return; UserSteamId = SteamId,
} Language = SelectedLanguage
};
await _goldberg.SetGlobalSettings(globalConfiguration).ConfigureAwait(false);
if (!DllSelected) return;
_log.Info("Saving Goldberg settings..."); _log.Info("Saving Goldberg settings...");
if (!GetDllPathDir(out var dirPath)) return; if (!GetDllPathDir(out var dirPath)) return;
@ -418,12 +427,11 @@ namespace GoldbergGUI.Core.ViewModels
private async Task ResetConfig() private async Task ResetConfig()
{ {
(AccountName, SteamId, SelectedLanguage) = await _goldberg.GetGlobalSettings().ConfigureAwait(false); var globalConfiguration = await _goldberg.GetGlobalSettings().ConfigureAwait(false);
if (!DllSelected) AccountName = globalConfiguration.AccountName;
{ SteamId = globalConfiguration.UserSteamId;
_log.Error("No DLL selected!"); SelectedLanguage = globalConfiguration.Language;
return; if (!DllSelected) return;
}
_log.Info("Reset form..."); _log.Info("Reset form...");
MainWindowEnabled = false; MainWindowEnabled = false;
@ -437,11 +445,7 @@ namespace GoldbergGUI.Core.ViewModels
private async Task GenerateSteamInterfaces() private async Task GenerateSteamInterfaces()
{ {
if (!DllSelected) if (!DllSelected) return;
{
_log.Error("No DLL selected!");
return;
}
_log.Info("Generate steam_interfaces.txt..."); _log.Info("Generate steam_interfaces.txt...");
MainWindowEnabled = false; MainWindowEnabled = false;
@ -536,7 +540,6 @@ namespace GoldbergGUI.Core.ViewModels
{ {
if (!DllSelected) if (!DllSelected)
{ {
_log.Error("No DLL selected!");
dirPath = null; dirPath = null;
return false; return false;
} }