GoldbergGUI/GoldbergGUI.Core/Services/GoldbergService.cs

704 lines
30 KiB
C#
Raw Normal View History

2021-12-22 07:15:28 -05:00
using GoldbergGUI.Core.Models;
using GoldbergGUI.Core.Utils;
2022-05-12 15:40:28 -04:00
using Microsoft.Extensions.Logging;
2021-01-08 12:36:57 -05:00
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
2021-01-08 12:36:57 -05:00
using System.Linq;
using System.Net.Http;
2021-01-08 12:36:57 -05:00
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
2021-01-08 12:36:57 -05:00
namespace GoldbergGUI.Core.Services
{
// downloads and updates goldberg emu
// sets up config files
// does file copy stuff
public interface IGoldbergService
{
2022-05-12 15:40:28 -04:00
public Task<GoldbergGlobalConfiguration> Initialize(ILogger<IGoldbergService> log);
public Task<GoldbergConfiguration> Read(string path);
public Task Save(string path, GoldbergConfiguration configuration);
public Task<GoldbergGlobalConfiguration> GetGlobalSettings();
public Task SetGlobalSettings(GoldbergGlobalConfiguration configuration);
2021-01-08 12:36:57 -05:00
public bool GoldbergApplied(string path);
public Task GenerateInterfacesFile(string filePath);
public List<string> Languages();
2021-01-08 12:36:57 -05:00
}
2021-01-08 12:36:57 -05:00
// ReSharper disable once UnusedType.Global
// ReSharper disable once ClassNeverInstantiated.Global
2021-01-08 12:36:57 -05:00
public class GoldbergService : IGoldbergService
{
2022-05-12 15:40:28 -04:00
private ILogger<IGoldbergService> _log;
private const string DefaultAccountName = "Mr_Goldberg";
private const long DefaultSteamId = 76561197960287930;
2021-03-21 08:01:32 -04:00
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");
2021-01-08 12:36:57 -05:00
private readonly string _goldbergPath = Path.Combine(Directory.GetCurrentDirectory(), "goldberg");
2021-01-08 12:36:57 -05:00
private static readonly string GlobalSettingsPath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Goldberg SteamEmu Saves");
2021-01-08 12:36:57 -05:00
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");
2021-04-07 08:22:45 -04:00
private readonly string _customBroadcastIpsPath =
Path.Combine(GlobalSettingsPath, "settings/custom_broadcasts.txt");
// ReSharper disable StringLiteralTypo
2021-01-08 12:36:57 -05:00
private readonly List<string> _interfaceNames = new List<string>
{
"SteamClient",
"SteamGameServer",
"SteamGameServerStats",
"SteamUser",
"SteamFriends",
"SteamUtils",
"SteamMatchMaking",
"SteamMatchMakingServers",
"STEAMUSERSTATS_INTERFACE_VERSION",
"STEAMAPPS_INTERFACE_VERSION",
"SteamNetworking",
"STEAMREMOTESTORAGE_INTERFACE_VERSION",
"STEAMSCREENSHOTS_INTERFACE_VERSION",
"STEAMHTTP_INTERFACE_VERSION",
"STEAMUNIFIEDMESSAGES_INTERFACE_VERSION",
"STEAMUGC_INTERFACE_VERSION",
"STEAMAPPLIST_INTERFACE_VERSION",
"STEAMMUSIC_INTERFACE_VERSION",
"STEAMMUSICREMOTE_INTERFACE_VERSION",
"STEAMHTMLSURFACE_INTERFACE_VERSION_",
"STEAMINVENTORY_INTERFACE_V",
"SteamController",
"SteamMasterServerUpdater",
"STEAMVIDEO_INTERFACE_V"
};
// Call Download
// Get global settings
2022-05-12 15:40:28 -04:00
public async Task<GoldbergGlobalConfiguration> Initialize(ILogger<IGoldbergService> log)
2021-01-08 12:36:57 -05:00
{
_log = log;
var download = await Download().ConfigureAwait(false);
if (download)
{
await Extract(_goldbergZipPath).ConfigureAwait(false);
}
2021-04-07 08:22:45 -04:00
return await GetGlobalSettings().ConfigureAwait(false);
}
public async Task<GoldbergGlobalConfiguration> GetGlobalSettings()
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Getting global settings...");
var accountName = DefaultAccountName;
var steamId = DefaultSteamId;
var language = DefaultLanguage;
var customBroadcastIps = new List<string>();
if (!File.Exists(GlobalSettingsPath)) Directory.CreateDirectory(Path.Join(GlobalSettingsPath, "settings"));
2021-01-08 12:36:57 -05:00
await Task.Run(() =>
{
if (File.Exists(_accountNamePath)) accountName = File.ReadLines(_accountNamePath).First().Trim();
if (File.Exists(_userSteamIdPath) &&
!long.TryParse(File.ReadLines(_userSteamIdPath).First().Trim(), out steamId) &&
steamId < 76561197960265729 && steamId > 76561202255233023)
{
2022-05-12 15:40:28 -04:00
_log.LogError("Invalid User Steam ID! Using default Steam ID...");
2021-04-07 08:51:49 -04:00
steamId = DefaultSteamId;
}
2021-04-07 08:22:45 -04:00
if (File.Exists(_languagePath)) language = File.ReadLines(_languagePath).First().Trim();
if (File.Exists(_customBroadcastIpsPath))
customBroadcastIps.AddRange(
File.ReadLines(_customBroadcastIpsPath).Select(line => line.Trim()));
2021-01-08 12:36:57 -05:00
}).ConfigureAwait(false);
2022-05-12 15:40:28 -04:00
_log.LogInformation("Got global settings.");
return new GoldbergGlobalConfiguration
{
AccountName = accountName,
UserSteamId = steamId,
Language = language,
CustomBroadcastIps = customBroadcastIps
};
}
2021-01-08 12:36:57 -05:00
public async Task SetGlobalSettings(GoldbergGlobalConfiguration c)
{
var accountName = c.AccountName;
var userSteamId = c.UserSteamId;
var language = c.Language;
var customBroadcastIps = c.CustomBroadcastIps;
2022-05-12 15:40:28 -04:00
_log.LogInformation("Setting global settings...");
// Account Name
if (!string.IsNullOrEmpty(accountName))
2021-01-13 15:26:35 -05:00
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Setting account name...");
if (!File.Exists(_accountNamePath))
await File.Create(_accountNamePath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_accountNamePath, accountName).ConfigureAwait(false);
2021-01-13 15:26:35 -05:00
}
else
2021-01-13 15:26:35 -05:00
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Invalid account name! Skipping...");
if (!File.Exists(_accountNamePath))
await File.Create(_accountNamePath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_accountNamePath, DefaultAccountName).ConfigureAwait(false);
2021-01-13 15:26:35 -05:00
}
2021-04-07 08:22:45 -04:00
// User SteamID
if (userSteamId >= 76561197960265729 && userSteamId <= 76561202255233023)
2021-01-13 15:26:35 -05:00
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Setting user Steam ID...");
2021-04-07 08:22:45 -04:00
if (!File.Exists(_userSteamIdPath))
await File.Create(_userSteamIdPath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_userSteamIdPath, userSteamId.ToString()).ConfigureAwait(false);
2021-01-13 15:26:35 -05:00
}
else
2021-01-13 15:26:35 -05:00
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Invalid user Steam ID! Skipping...");
2021-04-07 08:22:45 -04:00
if (!File.Exists(_userSteamIdPath))
await File.Create(_userSteamIdPath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_userSteamIdPath, DefaultSteamId.ToString()).ConfigureAwait(false);
2021-01-13 15:26:35 -05:00
}
2021-04-07 08:22:45 -04:00
// Language
if (!string.IsNullOrEmpty(language))
2021-01-13 15:26:35 -05:00
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Setting language...");
if (!File.Exists(_languagePath))
await File.Create(_languagePath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_languagePath, language).ConfigureAwait(false);
2021-01-13 15:26:35 -05:00
}
else
2021-01-13 15:26:35 -05:00
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Invalid language! Skipping...");
if (!File.Exists(_languagePath))
await File.Create(_languagePath).DisposeAsync().ConfigureAwait(false);
await File.WriteAllTextAsync(_languagePath, DefaultLanguage).ConfigureAwait(false);
2021-01-13 15:26:35 -05:00
}
2021-04-07 08:22:45 -04:00
// Custom Broadcast IPs
if (customBroadcastIps != null && customBroadcastIps.Count > 0)
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Setting custom broadcast IPs...");
2021-04-07 08:22:45 -04:00
var result =
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);
}
else
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Empty list of custom broadcast IPs! Skipping...");
await Task.Run(() => File.Delete(_customBroadcastIpsPath)).ConfigureAwait(false);
}
2022-05-12 15:40:28 -04:00
_log.LogInformation("Setting global configuration finished.");
2021-01-08 12:36:57 -05:00
}
// If first time, call GenerateInterfaces
// else try to read config
public async Task<GoldbergConfiguration> Read(string path)
2021-01-08 12:36:57 -05:00
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Reading configuration...");
2021-01-08 12:36:57 -05:00
var appId = -1;
var achievementList = new List<Achievement>();
var dlcList = new List<DlcApp>();
2021-01-08 12:36:57 -05:00
var steamAppidTxt = Path.Combine(path, "steam_appid.txt");
if (File.Exists(steamAppidTxt))
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Getting AppID...");
2021-01-08 12:36:57 -05:00
await Task.Run(() => int.TryParse(File.ReadLines(steamAppidTxt).First().Trim(), out appId))
.ConfigureAwait(false);
}
2021-01-13 15:26:35 -05:00
else
{
2022-05-12 15:40:28 -04:00
_log.LogInformation(@"""steam_appid.txt"" missing! Skipping...");
2021-01-13 15:26:35 -05:00
}
var achievementJson = Path.Combine(path, "steam_settings", "achievements.json");
if (File.Exists(achievementJson))
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Getting achievements...");
var json = await File.ReadAllTextAsync(achievementJson)
.ConfigureAwait(false);
achievementList = System.Text.Json.JsonSerializer.Deserialize<List<Achievement>>(json);
}
else
{
2022-05-12 15:40:28 -04:00
_log.LogInformation(@"""steam_settings/achievements.json"" missing! Skipping...");
}
2021-01-08 12:36:57 -05:00
var dlcTxt = Path.Combine(path, "steam_settings", "DLC.txt");
var appPathTxt = Path.Combine(path, "steam_settings", "app_paths.txt");
2021-01-08 12:36:57 -05:00
if (File.Exists(dlcTxt))
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Getting DLCs...");
2021-01-08 12:36:57 -05:00
var readAllLinesAsync = await File.ReadAllLinesAsync(dlcTxt).ConfigureAwait(false);
var expression = new Regex(@"(?<id>.*) *= *(?<name>.*)");
2021-01-13 15:26:35 -05:00
// ReSharper disable once LoopCanBeConvertedToQuery
2021-01-08 12:36:57 -05:00
foreach (var line in readAllLinesAsync)
{
var match = expression.Match(line);
if (match.Success)
dlcList.Add(new DlcApp()
{
AppId = Convert.ToInt32(match.Groups["id"].Value),
Name = match.Groups["name"].Value
});
2021-01-08 12:36:57 -05:00
}
// ReSharper disable once InvertIf
if (File.Exists(appPathTxt))
{
2021-04-29 10:44:37 -04:00
var appPathAllLinesAsync = await File.ReadAllLinesAsync(appPathTxt).ConfigureAwait(false);
var appPathExpression = new Regex(@"(?<id>.*) *= *(?<appPath>.*)");
foreach (var line in appPathAllLinesAsync)
{
var match = appPathExpression.Match(line);
2021-04-29 10:44:37 -04:00
if (!match.Success) continue;
2021-12-22 07:15:28 -05:00
var i = dlcList.FindIndex(x =>
2021-04-29 10:44:37 -04:00
x.AppId.Equals(Convert.ToInt32(match.Groups["id"].Value)));
dlcList[i].AppPath = match.Groups["appPath"].Value;
}
}
2021-01-08 12:36:57 -05:00
}
2021-01-13 15:26:35 -05:00
else
{
2022-05-12 15:40:28 -04:00
_log.LogInformation(@"""steam_settings/DLC.txt"" missing! Skipping...");
2021-01-13 15:26:35 -05:00
}
return new GoldbergConfiguration
{
AppId = appId,
Achievements = achievementList,
DlcList = dlcList,
Offline = File.Exists(Path.Combine(path, "steam_settings", "offline.txt")),
DisableNetworking = File.Exists(Path.Combine(path, "steam_settings", "disable_networking.txt")),
DisableOverlay = File.Exists(Path.Combine(path, "steam_settings", "disable_overlay.txt"))
};
2021-01-08 12:36:57 -05:00
}
// If first time, rename original SteamAPI DLL to steam_api(64)_o.dll
// If not, rename current SteamAPI DLL to steam_api(64).dll.backup
// Copy Goldberg DLL to path
// Save configuration files
public async Task Save(string path, GoldbergConfiguration c)
2021-01-08 12:36:57 -05:00
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Saving configuration...");
// DLL setup
2022-05-12 15:40:28 -04:00
_log.LogInformation("Running DLL setup...");
2021-01-08 12:36:57 -05:00
const string x86Name = "steam_api";
const string x64Name = "steam_api64";
if (File.Exists(Path.Combine(path, $"{x86Name}.dll")))
{
CopyDllFiles(path, x86Name);
}
if (File.Exists(Path.Combine(path, $"{x64Name}.dll")))
{
CopyDllFiles(path, x64Name);
}
2022-05-12 15:40:28 -04:00
_log.LogInformation("DLL setup finished!");
2021-01-08 12:36:57 -05:00
// Create steam_settings folder if missing
2022-05-12 15:40:28 -04:00
_log.LogInformation("Saving settings...");
2021-01-08 12:36:57 -05:00
if (!Directory.Exists(Path.Combine(path, "steam_settings")))
{
Directory.CreateDirectory(Path.Combine(path, "steam_settings"));
}
// create steam_appid.txt
2021-04-07 08:22:45 -04:00
await File.WriteAllTextAsync(Path.Combine(path, "steam_appid.txt"), c.AppId.ToString())
.ConfigureAwait(false);
// Achievements + Images
if (c.Achievements.Count > 0)
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Downloading images...");
var imagePath = Path.Combine(path, "steam_settings", "images");
Directory.CreateDirectory(imagePath);
foreach (var achievement in c.Achievements)
{
await DownloadImageAsync(imagePath, achievement.Icon);
await DownloadImageAsync(imagePath, achievement.IconGray);
// Update achievement list to point to local images instead
achievement.Icon = $"images/{Path.GetFileName(achievement.Icon)}";
achievement.IconGray = $"images/{Path.GetFileName(achievement.IconGray)}";
}
2022-05-12 15:40:28 -04:00
_log.LogInformation("Saving achievements...");
var achievementJson = System.Text.Json.JsonSerializer.Serialize(
c.Achievements,
new System.Text.Json.JsonSerializerOptions
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = true
});
await File.WriteAllTextAsync(Path.Combine(path, "steam_settings", "achievements.json"), achievementJson)
.ConfigureAwait(false);
2022-05-12 15:40:28 -04:00
_log.LogInformation("Finished saving achievements.");
}
else
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("No achievements set! Removing achievement files...");
var imagePath = Path.Combine(path, "steam_settings", "images");
if (Directory.Exists(imagePath))
{
Directory.Delete(imagePath);
}
var achievementPath = Path.Combine(path, "steam_settings", "achievements");
if (File.Exists(achievementPath))
{
File.Delete(achievementPath);
}
2022-05-12 15:40:28 -04:00
_log.LogInformation("Removed achievement files.");
}
// DLC + App path
if (c.DlcList.Count > 0)
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Saving DLC settings...");
var dlcContent = "";
//var depotContent = "";
var appPathContent = "";
c.DlcList.ForEach(x =>
{
dlcContent += $"{x}\n";
//depotContent += $"{x.DepotId}\n";
2021-04-29 10:44:37 -04:00
if (!string.IsNullOrEmpty(x.AppPath))
appPathContent += $"{x.AppId}={x.AppPath}\n";
});
await File.WriteAllTextAsync(Path.Combine(path, "steam_settings", "DLC.txt"), dlcContent)
.ConfigureAwait(false);
2021-12-22 07:15:28 -05:00
2021-04-29 10:44:37 -04:00
/*if (!string.IsNullOrEmpty(depotContent))
{
await File.WriteAllTextAsync(Path.Combine(path, "steam_settings", "depots.txt"), depotContent)
.ConfigureAwait(false);
}*/
2021-12-22 07:15:28 -05:00
2021-04-29 10:44:37 -04:00
if (!string.IsNullOrEmpty(appPathContent))
{
await File.WriteAllTextAsync(Path.Combine(path, "steam_settings", "app_paths.txt"), appPathContent)
.ConfigureAwait(false);
}
2021-04-29 10:44:37 -04:00
else
{
if (File.Exists(Path.Combine(path, "steam_settings", "app_paths.txt")))
File.Delete(Path.Combine(path, "steam_settings", "app_paths.txt"));
}
2022-05-12 15:40:28 -04:00
_log.LogInformation("Saved DLC settings.");
}
else
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("No DLC set! Removing DLC configuration files...");
if (File.Exists(Path.Combine(path, "steam_settings", "DLC.txt")))
File.Delete(Path.Combine(path, "steam_settings", "DLC.txt"));
if (File.Exists(Path.Combine(path, "steam_settings", "app_paths.txt")))
File.Delete(Path.Combine(path, "steam_settings", "app_paths.txt"));
2022-05-12 15:40:28 -04:00
_log.LogInformation("Removed DLC configuration files.");
}
// Offline
if (c.Offline)
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Create offline.txt");
await File.Create(Path.Combine(path, "steam_settings", "offline.txt")).DisposeAsync()
.ConfigureAwait(false);
}
else
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Delete offline.txt if it exists");
File.Delete(Path.Combine(path, "steam_settings", "offline.txt"));
}
// Disable Networking
if (c.DisableNetworking)
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Create disable_networking.txt");
await File.Create(Path.Combine(path, "steam_settings", "disable_networking.txt")).DisposeAsync()
.ConfigureAwait(false);
}
else
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Delete disable_networking.txt if it exists");
File.Delete(Path.Combine(path, "steam_settings", "disable_networking.txt"));
}
// Disable Overlay
if (c.DisableOverlay)
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Create disable_overlay.txt");
await File.Create(Path.Combine(path, "steam_settings", "disable_overlay.txt")).DisposeAsync()
.ConfigureAwait(false);
}
else
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Delete disable_overlay.txt if it exists");
File.Delete(Path.Combine(path, "steam_settings", "disable_overlay.txt"));
}
2021-01-08 12:36:57 -05:00
}
private void CopyDllFiles(string path, string name)
{
var steamApiDll = Path.Combine(path, $"{name}.dll");
var originalDll = Path.Combine(path, $"{name}_o.dll");
var guiBackup = Path.Combine(path, $".{name}.dll.GOLDBERGGUIBACKUP");
2021-01-08 12:36:57 -05:00
var goldbergDll = Path.Combine(_goldbergPath, $"{name}.dll");
2021-01-08 12:36:57 -05:00
if (!File.Exists(originalDll))
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Back up original Steam API DLL...");
2021-01-08 12:36:57 -05:00
File.Move(steamApiDll, originalDll);
}
2021-01-08 12:36:57 -05:00
else
{
File.Move(steamApiDll, guiBackup, true);
File.SetAttributes(guiBackup, FileAttributes.Hidden);
}
2022-05-12 15:40:28 -04:00
_log.LogInformation("Copy Goldberg DLL to target path...");
2021-01-08 12:36:57 -05:00
File.Copy(goldbergDll, steamApiDll);
}
public bool GoldbergApplied(string path)
{
var steamSettingsDirExists = Directory.Exists(Path.Combine(path, "steam_settings"));
var steamAppIdTxtExists = File.Exists(Path.Combine(path, "steam_appid.txt"));
2022-05-12 15:40:28 -04:00
_log.LogDebug($"Goldberg applied? {steamSettingsDirExists && steamAppIdTxtExists}");
return steamSettingsDirExists && steamAppIdTxtExists;
2021-01-08 12:36:57 -05:00
}
private async Task<bool> Download()
2021-01-08 12:36:57 -05:00
{
// Get webpage
// Get job id, compare with local if exists, save it if false or missing
// Get latest archive if mismatch, call Extract
2022-05-12 15:40:28 -04:00
_log.LogInformation("Initializing download...");
if (!Directory.Exists(_goldbergPath)) Directory.CreateDirectory(_goldbergPath);
var client = new HttpClient();
var response = await client.GetAsync(GoldbergUrl).ConfigureAwait(false);
var body = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var regex = new Regex(
@"https:\/\/gitlab\.com\/Mr_Goldberg\/goldberg_emulator\/-\/jobs\/(?<jobid>.*)\/artifacts\/download");
var jobIdPath = Path.Combine(_goldbergPath, "job_id");
var match = regex.Match(body);
if (File.Exists(jobIdPath))
{
2021-04-07 08:22:45 -04:00
try
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Check if update is needed...");
2021-04-07 08:22:45 -04:00
var jobIdLocal = Convert.ToInt32(File.ReadLines(jobIdPath).First().Trim());
var jobIdRemote = Convert.ToInt32(match.Groups["jobid"].Value);
2022-05-12 15:40:28 -04:00
_log.LogDebug($"job_id: local {jobIdLocal}; remote {jobIdRemote}");
2021-04-07 08:22:45 -04:00
if (jobIdLocal.Equals(jobIdRemote))
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Latest Goldberg emulator is already available! Skipping...");
2021-04-07 08:22:45 -04:00
return false;
}
}
catch (Exception)
{
2022-05-12 15:40:28 -04:00
_log.LogError("An error occured, local Goldberg setup might be broken!");
}
}
2021-04-07 08:22:45 -04:00
2022-05-12 15:40:28 -04:00
_log.LogInformation("Starting download...");
await StartDownload(match.Value).ConfigureAwait(false);
2021-01-13 15:26:35 -05:00
return true;
}
private async Task StartDownload(string downloadUrl)
{
try
{
var client = new HttpClient();
2022-05-12 15:40:28 -04:00
_log.LogDebug(downloadUrl);
await using var fileStream = File.OpenWrite(_goldbergZipPath);
//client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead)
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, downloadUrl);
var headResponse = await client.SendAsync(httpRequestMessage).ConfigureAwait(false);
var contentLength = headResponse.Content.Headers.ContentLength;
2021-04-07 08:51:49 -04:00
await client.GetFileAsync(downloadUrl, fileStream).ContinueWith(async t =>
{
// ReSharper disable once AccessToDisposedClosure
await fileStream.DisposeAsync().ConfigureAwait(false);
var fileLength = new FileInfo(_goldbergZipPath).Length;
// Environment.Exit(128);
if (contentLength == fileLength)
{
2022-05-12 15:40:28 -04:00
_log.LogInformation("Download finished!");
}
else
{
throw new Exception("File size does not match!");
}
2021-04-07 08:51:49 -04:00
}).ConfigureAwait(false);
}
catch (Exception e)
{
ShowErrorMessage();
2022-05-12 15:40:28 -04:00
_log.LogError(e.ToString());
Environment.Exit(1);
}
}
2021-01-08 12:36:57 -05:00
// Empty subfolder ./goldberg/
// Extract all from archive to subfolder ./goldberg/
private async Task Extract(string archivePath)
2021-01-08 12:36:57 -05:00
{
2021-04-07 08:22:45 -04:00
var errorOccured = false;
2022-05-12 15:40:28 -04:00
_log.LogDebug("Start extraction...");
2021-04-07 08:22:45 -04:00
Directory.Delete(_goldbergPath, true);
Directory.CreateDirectory(_goldbergPath);
using (var archive = await Task.Run(() => ZipFile.OpenRead(archivePath)).ConfigureAwait(false))
{
2021-04-07 08:22:45 -04:00
foreach (var entry in archive.Entries)
{
2021-04-07 08:22:45 -04:00
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;
2022-05-12 15:40:28 -04:00
_log.LogError($"Error while trying to extract {entry.FullName}");
_log.LogError(e.ToString());
2021-04-07 08:22:45 -04:00
}
}).ConfigureAwait(false);
}
2021-04-07 08:22:45 -04:00
}
if (errorOccured)
{
ShowErrorMessage();
2022-05-12 15:40:28 -04:00
_log.LogWarning("Error occured while extraction! Please setup Goldberg manually");
2021-04-07 08:22:45 -04:00
}
2022-05-12 15:40:28 -04:00
_log.LogInformation("Extraction was successful!");
2021-01-08 12:36:57 -05:00
}
private void ShowErrorMessage()
{
if (Directory.Exists(_goldbergPath))
{
Directory.Delete(_goldbergPath, true);
}
2021-04-07 08:22:45 -04:00
Directory.CreateDirectory(_goldbergPath);
MessageBox.Show("Could not setup Goldberg Emulator!\n" +
"Please download it manually and extract its content into the \"goldberg\" subfolder!");
2021-01-08 12:36:57 -05:00
}
// https://gitlab.com/Mr_Goldberg/goldberg_emulator/-/blob/master/generate_interfaces_file.cpp
// (maybe) check DLL date first
public async Task GenerateInterfacesFile(string filePath)
{
2022-05-12 15:40:28 -04:00
_log.LogDebug($"GenerateInterfacesFile {filePath}");
2021-01-08 12:36:57 -05:00
//throw new NotImplementedException();
// Get DLL content
var result = new HashSet<string>();
var dllContent = await File.ReadAllTextAsync(filePath).ConfigureAwait(false);
// find interfaces
foreach (var name in _interfaceNames)
{
FindInterfaces(ref result, dllContent, new Regex($"{name}\\d{{3}}"));
if (!FindInterfaces(ref result, dllContent, new Regex(@"STEAMCONTROLLER_INTERFACE_VERSION\d{3}")))
{
FindInterfaces(ref result, dllContent, new Regex("STEAMCONTROLLER_INTERFACE_VERSION"));
}
}
2021-01-08 12:36:57 -05:00
var dirPath = Path.GetDirectoryName(filePath);
if (dirPath == null) return;
await using var destination = File.CreateText(dirPath + "/steam_interfaces.txt");
foreach (var s in result)
{
await destination.WriteLineAsync(s).ConfigureAwait(false);
}
}
public List<string> Languages() => new List<string>
{
DefaultLanguage,
"arabic",
"bulgarian",
"schinese",
"tchinese",
"czech",
"danish",
"dutch",
"finnish",
"french",
"german",
"greek",
"hungarian",
"italian",
"japanese",
"koreana",
"norwegian",
"polish",
"portuguese",
"brazilian",
"romanian",
"russian",
"spanish",
"swedish",
"thai",
"turkish",
"ukrainian"
};
2021-01-08 12:36:57 -05:00
private static bool FindInterfaces(ref HashSet<string> result, string dllContent, Regex regex)
{
var success = false;
var matches = regex.Matches(dllContent);
foreach (Match match in matches)
{
success = true;
//result += $@"{match.Value}\n";
result.Add(match.Value);
}
2021-01-08 12:36:57 -05:00
return success;
}
private async Task DownloadImageAsync(string imageFolder, string imageUrl)
{
var fileName = Path.GetFileName(imageUrl);
var targetPath = Path.Combine(imageFolder, fileName);
if (File.Exists(targetPath))
{
return;
}
else if (imageUrl.StartsWith("images/"))
{
2022-05-12 15:40:28 -04:00
_log.LogWarning($"Previously downloaded image '{imageUrl}' is now missing!");
}
var wc = new System.Net.WebClient();
await wc.DownloadFileTaskAsync(new Uri(imageUrl, UriKind.Absolute), targetPath);
}
2021-01-08 12:36:57 -05:00
}
}