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
/GoldbergGUI.Core/Utils/Secrets.cs
/README.bbcode

View File

@ -2,6 +2,12 @@ using System.Collections.Generic;
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 int AppId { get; set; }

View File

@ -8,6 +8,7 @@ using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using GoldbergGUI.Core.Models;
using GoldbergGUI.Core.Utils;
using MvvmCross.Logging;
namespace GoldbergGUI.Core.Services
@ -17,11 +18,11 @@ namespace GoldbergGUI.Core.Services
// does file copy stuff
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 Save(string path, GoldbergConfiguration configuration);
public Task<(string accountName, long steamId, string language)> GetGlobalSettings();
public Task SetGlobalSettings(string accountName, long userSteamId, string language);
public Task<GoldbergGlobalConfiguration> GetGlobalSettings();
public Task SetGlobalSettings(GoldbergGlobalConfiguration configuration);
public bool GoldbergApplied(string path);
public Task<bool> Download();
public Task Extract(string archivePath);
@ -76,7 +77,7 @@ namespace GoldbergGUI.Core.Services
// Call Download
// Get global settings
public async Task<(string accountName, long userSteamId, string language)> Initialize(IMvxLog log)
public async Task<GoldbergGlobalConfiguration> Initialize(IMvxLog log)
{
_log = log;
@ -85,7 +86,7 @@ namespace GoldbergGUI.Core.Services
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...");
var accountName = "Account name...";
@ -100,11 +101,19 @@ namespace GoldbergGUI.Core.Services
_log.Error("Invalid User Steam ID!");
if (File.Exists(_languagePath)) language = File.ReadLines(_languagePath).First().Trim();
}).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...");
if (accountName != null && accountName != "Account name...")
{
@ -324,16 +333,17 @@ namespace GoldbergGUI.Core.Services
}
}
_log.Info("Starting download...");
await StartDownload(client, match.Value).ConfigureAwait(false);
await StartDownload(match.Value).ConfigureAwait(false);
return true;
}
private async Task StartDownload(HttpClient client, string downloadUrl)
private async Task StartDownload(string downloadUrl)
{
var client = new HttpClient();
_log.Debug(downloadUrl);
await using var fileStream = File.OpenWrite(_goldbergZipPath);
//client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead)
var task = GetFileAsync(client, downloadUrl, fileStream).ConfigureAwait(false);
var task = client.GetFileAsync(downloadUrl, fileStream).ConfigureAwait(false);
await task;
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/
// Extract all from archive to subfolder ./goldberg/
public async Task Extract(string archivePath)
@ -361,7 +361,7 @@ namespace GoldbergGUI.Core.Services
Directory.Delete(_goldbergPath, true);
ZipFile.ExtractToDirectory(archivePath, _goldbergPath);
}).ConfigureAwait(false);
_log.Debug("Extract done!");
_log.Debug("Extraction done!");
}
// 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.Services;
using Microsoft.Win32;
using MvvmCross;
using MvvmCross.Commands;
using MvvmCross.Logging;
using MvvmCross.Navigation;
@ -71,11 +70,11 @@ namespace GoldbergGUI.Core.ViewModels
SteamLanguages = new ObservableCollection<string>(_goldberg.Languages());
ResetForm();
await _steam.Initialize(_logProvider.GetLogFor<SteamService>()).ConfigureAwait(false);
var (accountName, userSteamId, language) =
var globalConfiguration =
await _goldberg.Initialize(_logProvider.GetLogFor<GoldbergService>()).ConfigureAwait(false);
AccountName = accountName;
SteamId = userSteamId;
SelectedLanguage = language;
AccountName = globalConfiguration.AccountName;
SteamId = globalConfiguration.UserSteamId;
SelectedLanguage = globalConfiguration.Language;
}
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
{
@ -389,12 +396,14 @@ namespace GoldbergGUI.Core.ViewModels
private async Task SaveConfig()
{
_log.Info("Saving global settings...");
await _goldberg.SetGlobalSettings(AccountName, SteamId, SelectedLanguage).ConfigureAwait(false);
if (!DllSelected)
var globalConfiguration = new GoldbergGlobalConfiguration
{
_log.Error("No DLL selected!");
return;
}
AccountName = AccountName,
UserSteamId = SteamId,
Language = SelectedLanguage
};
await _goldberg.SetGlobalSettings(globalConfiguration).ConfigureAwait(false);
if (!DllSelected) return;
_log.Info("Saving Goldberg settings...");
if (!GetDllPathDir(out var dirPath)) return;
@ -418,12 +427,11 @@ namespace GoldbergGUI.Core.ViewModels
private async Task ResetConfig()
{
(AccountName, SteamId, SelectedLanguage) = await _goldberg.GetGlobalSettings().ConfigureAwait(false);
if (!DllSelected)
{
_log.Error("No DLL selected!");
return;
}
var globalConfiguration = await _goldberg.GetGlobalSettings().ConfigureAwait(false);
AccountName = globalConfiguration.AccountName;
SteamId = globalConfiguration.UserSteamId;
SelectedLanguage = globalConfiguration.Language;
if (!DllSelected) return;
_log.Info("Reset form...");
MainWindowEnabled = false;
@ -437,11 +445,7 @@ namespace GoldbergGUI.Core.ViewModels
private async Task GenerateSteamInterfaces()
{
if (!DllSelected)
{
_log.Error("No DLL selected!");
return;
}
if (!DllSelected) return;
_log.Info("Generate steam_interfaces.txt...");
MainWindowEnabled = false;
@ -536,7 +540,6 @@ namespace GoldbergGUI.Core.ViewModels
{
if (!DllSelected)
{
_log.Error("No DLL selected!");
dirPath = null;
return false;
}