2021-01-08 12:36:57 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2021-01-09 14:17:09 -05:00
|
|
|
using System.IO.Compression;
|
2021-01-08 12:36:57 -05:00
|
|
|
using System.Linq;
|
2021-01-09 14:17:09 -05:00
|
|
|
using System.Net.Http;
|
2021-01-08 12:36:57 -05:00
|
|
|
using System.Text.RegularExpressions;
|
2021-01-09 14:17:09 -05:00
|
|
|
using System.Threading;
|
2021-01-08 12:36:57 -05:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using GoldbergGUI.Core.Models;
|
2021-01-15 11:46:13 -05:00
|
|
|
using GoldbergGUI.Core.Utils;
|
2021-01-08 12:36:57 -05:00
|
|
|
using MvvmCross.Logging;
|
|
|
|
|
|
|
|
namespace GoldbergGUI.Core.Services
|
|
|
|
{
|
|
|
|
// downloads and updates goldberg emu
|
|
|
|
// sets up config files
|
|
|
|
// does file copy stuff
|
|
|
|
public interface IGoldbergService
|
|
|
|
{
|
2021-01-15 11:46:13 -05:00
|
|
|
public Task<GoldbergGlobalConfiguration> Initialize(IMvxLog log);
|
2021-01-10 10:08:12 -05:00
|
|
|
public Task<GoldbergConfiguration> Read(string path);
|
|
|
|
public Task Save(string path, GoldbergConfiguration configuration);
|
2021-01-15 11:46:13 -05:00
|
|
|
public Task<GoldbergGlobalConfiguration> GetGlobalSettings();
|
|
|
|
public Task SetGlobalSettings(GoldbergGlobalConfiguration configuration);
|
2021-01-08 12:36:57 -05:00
|
|
|
public bool GoldbergApplied(string path);
|
2021-01-09 14:17:09 -05:00
|
|
|
public Task<bool> Download();
|
|
|
|
public Task Extract(string archivePath);
|
2021-01-08 12:36:57 -05:00
|
|
|
public Task GenerateInterfacesFile(string filePath);
|
2021-01-10 10:08:12 -05:00
|
|
|
public List<string> Languages();
|
2021-01-08 12:36:57 -05:00
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
|
2021-01-08 12:36:57 -05:00
|
|
|
// ReSharper disable once UnusedType.Global
|
|
|
|
public class GoldbergService : IGoldbergService
|
|
|
|
{
|
|
|
|
private IMvxLog _log;
|
2021-01-09 14:17:09 -05:00
|
|
|
private const string GoldbergUrl = "https://mr_goldberg.gitlab.io/goldberg_emulator/";
|
2021-01-10 10:08:12 -05:00
|
|
|
private const string DefaultLanguage = "english";
|
2021-01-09 14:17:09 -05:00
|
|
|
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-10 10:08:12 -05:00
|
|
|
|
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-10 10:08:12 -05:00
|
|
|
|
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");
|
2021-01-10 10:08:12 -05:00
|
|
|
private readonly string _languagePath = Path.Combine(GlobalSettingsPath, "settings/language.txt");
|
2021-02-15 14:52:58 -05:00
|
|
|
private readonly string _customBroadcastIpsPath = Path.Combine(GlobalSettingsPath, "settings/custom_broadcasts.txt");
|
2021-01-10 10:08:12 -05:00
|
|
|
|
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
|
2021-01-15 11:46:13 -05:00
|
|
|
public async Task<GoldbergGlobalConfiguration> Initialize(IMvxLog log)
|
2021-01-08 12:36:57 -05:00
|
|
|
{
|
|
|
|
_log = log;
|
2021-01-09 14:17:09 -05:00
|
|
|
|
|
|
|
var download = await Download().ConfigureAwait(false);
|
|
|
|
if (download) await Extract(_goldbergZipPath).ConfigureAwait(false);
|
2021-01-10 10:08:12 -05:00
|
|
|
return await GetGlobalSettings().ConfigureAwait(false);
|
|
|
|
}
|
2021-01-09 14:17:09 -05:00
|
|
|
|
2021-01-15 11:46:13 -05:00
|
|
|
public async Task<GoldbergGlobalConfiguration> GetGlobalSettings()
|
2021-01-10 10:08:12 -05:00
|
|
|
{
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Info("Getting global settings...");
|
2021-01-08 12:36:57 -05:00
|
|
|
var accountName = "Account name...";
|
|
|
|
long steamId = -1;
|
2021-01-10 10:08:12 -05:00
|
|
|
var language = DefaultLanguage;
|
2021-02-15 14:52:58 -05:00
|
|
|
var customBroadcastIps = new List<string>();
|
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) &&
|
2021-01-10 10:08:12 -05:00
|
|
|
!long.TryParse(File.ReadLines(_userSteamIdPath).First().Trim(), out steamId) &&
|
|
|
|
steamId < 76561197960265729 && steamId > 76561202255233023)
|
2021-02-15 14:52:58 -05:00
|
|
|
{
|
2021-01-08 12:36:57 -05:00
|
|
|
_log.Error("Invalid User Steam ID!");
|
2021-02-15 14:52:58 -05:00
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
if (File.Exists(_languagePath)) language = File.ReadLines(_languagePath).First().Trim();
|
2021-02-15 14:52:58 -05:00
|
|
|
if (File.Exists(_customBroadcastIpsPath))
|
|
|
|
customBroadcastIps.AddRange(
|
|
|
|
File.ReadLines(_customBroadcastIpsPath).Select(line => line.Trim()));
|
2021-01-08 12:36:57 -05:00
|
|
|
}).ConfigureAwait(false);
|
2021-01-15 11:46:13 -05:00
|
|
|
return new GoldbergGlobalConfiguration
|
|
|
|
{
|
|
|
|
AccountName = accountName,
|
|
|
|
UserSteamId = steamId,
|
2021-02-15 14:52:58 -05:00
|
|
|
Language = language,
|
|
|
|
CustomBroadcastIps = customBroadcastIps
|
2021-01-15 11:46:13 -05:00
|
|
|
};
|
2021-01-10 10:08:12 -05:00
|
|
|
}
|
2021-01-08 12:36:57 -05:00
|
|
|
|
2021-01-15 11:46:13 -05:00
|
|
|
public async Task SetGlobalSettings(GoldbergGlobalConfiguration c)
|
2021-01-10 10:08:12 -05:00
|
|
|
{
|
2021-01-15 11:46:13 -05:00
|
|
|
var accountName = c.AccountName;
|
|
|
|
var userSteamId = c.UserSteamId;
|
|
|
|
var language = c.Language;
|
2021-02-15 14:52:58 -05:00
|
|
|
var customBroadcastIps = c.CustomBroadcastIps;
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Info("Setting global settings...");
|
2021-02-15 14:52:58 -05:00
|
|
|
// Account Name
|
2021-01-10 10:08:12 -05:00
|
|
|
if (accountName != null && accountName != "Account name...")
|
2021-01-13 15:26:35 -05:00
|
|
|
{
|
|
|
|
_log.Info("Setting account name...");
|
2021-01-10 10:08:12 -05:00
|
|
|
await File.WriteAllTextAsync(_accountNamePath, accountName).ConfigureAwait(false);
|
2021-01-13 15:26:35 -05:00
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
else
|
2021-01-13 15:26:35 -05:00
|
|
|
{
|
|
|
|
_log.Info("Invalid account name! Skipping...");
|
2021-01-10 10:08:12 -05:00
|
|
|
await File.WriteAllTextAsync(_accountNamePath, "Goldberg").ConfigureAwait(false);
|
2021-01-13 15:26:35 -05:00
|
|
|
}
|
2021-02-15 14:52:58 -05:00
|
|
|
// User SteamID
|
2021-01-10 10:08:12 -05:00
|
|
|
if (userSteamId >= 76561197960265729 && userSteamId <= 76561202255233023)
|
2021-01-13 15:26:35 -05:00
|
|
|
{
|
|
|
|
_log.Info("Setting user Steam ID...");
|
2021-01-10 10:08:12 -05:00
|
|
|
await File.WriteAllTextAsync(_userSteamIdPath, userSteamId.ToString()).ConfigureAwait(false);
|
2021-01-13 15:26:35 -05:00
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
else
|
2021-01-13 15:26:35 -05:00
|
|
|
{
|
|
|
|
_log.Info("Invalid user Steam ID! Skipping...");
|
2021-01-10 10:08:12 -05:00
|
|
|
await Task.Run(() => File.Delete(_userSteamIdPath)).ConfigureAwait(false);
|
2021-01-13 15:26:35 -05:00
|
|
|
}
|
2021-02-15 14:52:58 -05:00
|
|
|
// Language
|
2021-01-10 10:08:12 -05:00
|
|
|
if (language != null)
|
2021-01-13 15:26:35 -05:00
|
|
|
{
|
|
|
|
_log.Info("Setting language...");
|
2021-01-10 10:08:12 -05:00
|
|
|
await File.WriteAllTextAsync(_languagePath, language).ConfigureAwait(false);
|
2021-01-13 15:26:35 -05:00
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
else
|
2021-01-13 15:26:35 -05:00
|
|
|
{
|
|
|
|
_log.Info("Invalid language! Skipping...");
|
2021-01-10 10:08:12 -05:00
|
|
|
await File.WriteAllTextAsync(_languagePath, DefaultLanguage).ConfigureAwait(false);
|
2021-01-13 15:26:35 -05:00
|
|
|
}
|
2021-02-15 14:52:58 -05:00
|
|
|
// Custom Broadcast IPs
|
|
|
|
if (customBroadcastIps.Count > 0)
|
|
|
|
{
|
|
|
|
_log.Info("Setting custom broadcast IPs...");
|
|
|
|
var result =
|
|
|
|
customBroadcastIps.Aggregate("", (current, address) => $"{current}{address}\n");
|
|
|
|
await File.WriteAllTextAsync(_customBroadcastIpsPath, result).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_log.Info("Empty list of custom broadcast IPs! Skipping...");
|
|
|
|
await Task.Run(() => File.Delete(_customBroadcastIpsPath)).ConfigureAwait(false);
|
|
|
|
}
|
2021-01-08 12:36:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// If first time, call GenerateInterfaces
|
|
|
|
// else try to read config
|
2021-01-10 10:08:12 -05:00
|
|
|
public async Task<GoldbergConfiguration> Read(string path)
|
2021-01-08 12:36:57 -05:00
|
|
|
{
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Info("Reading configuration...");
|
2021-01-08 12:36:57 -05:00
|
|
|
var appId = -1;
|
|
|
|
var dlcList = new List<SteamApp>();
|
|
|
|
var steamAppidTxt = Path.Combine(path, "steam_appid.txt");
|
|
|
|
if (File.Exists(steamAppidTxt))
|
|
|
|
{
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Info("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
|
|
|
|
{
|
|
|
|
_log.Info(@"""steam_appid.txt"" missing! Skipping...");
|
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
|
2021-01-08 12:36:57 -05:00
|
|
|
var dlcTxt = Path.Combine(path, "steam_settings", "DLC.txt");
|
|
|
|
if (File.Exists(dlcTxt))
|
|
|
|
{
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Info("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)
|
2021-01-10 10:08:12 -05:00
|
|
|
dlcList.Add(new SteamApp
|
|
|
|
{
|
|
|
|
AppId = Convert.ToInt32(match.Groups["id"].Value),
|
|
|
|
Name = match.Groups["name"].Value
|
|
|
|
});
|
2021-01-08 12:36:57 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-13 15:26:35 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_log.Info(@"""steam_settings/DLC.txt"" missing! Skipping...");
|
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
|
|
|
|
return new GoldbergConfiguration
|
|
|
|
{
|
|
|
|
AppId = appId,
|
|
|
|
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
|
2021-01-10 10:08:12 -05:00
|
|
|
public async Task Save(string path, GoldbergConfiguration c)
|
2021-01-08 12:36:57 -05:00
|
|
|
{
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Info("Saving configuration...");
|
2021-01-09 14:17:09 -05:00
|
|
|
// DLL setup
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Info("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);
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:17:09 -05:00
|
|
|
// Create steam_settings folder if missing
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Info("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"));
|
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
|
2021-01-09 14:17:09 -05:00
|
|
|
// create steam_appid.txt
|
2021-01-10 10:08:12 -05:00
|
|
|
await File.WriteAllTextAsync(Path.Combine(path, "steam_appid.txt"), c.AppId.ToString()).ConfigureAwait(false);
|
|
|
|
|
2021-01-09 14:17:09 -05:00
|
|
|
// DLC
|
2021-01-10 10:08:12 -05:00
|
|
|
if (c.DlcList.Count > 0)
|
2021-01-09 14:17:09 -05:00
|
|
|
{
|
|
|
|
var dlcString = "";
|
2021-01-10 10:08:12 -05:00
|
|
|
c.DlcList.ForEach(x => dlcString += $"{x}\n");
|
2021-01-09 14:17:09 -05:00
|
|
|
await File.WriteAllTextAsync(Path.Combine(path, "steam_settings", "DLC.txt"), dlcString)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (File.Exists(Path.Combine(path, "steam_settings", "DLC.txt")))
|
|
|
|
File.Delete(Path.Combine(path, "steam_settings", "DLC.txt"));
|
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
|
2021-01-09 14:17:09 -05:00
|
|
|
// Offline
|
2021-01-10 10:08:12 -05:00
|
|
|
if (c.Offline)
|
2021-01-09 14:17:09 -05:00
|
|
|
{
|
2021-01-10 10:08:12 -05:00
|
|
|
await File.Create(Path.Combine(path, "steam_settings", "offline.txt")).DisposeAsync()
|
|
|
|
.ConfigureAwait(false);
|
2021-01-09 14:17:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
File.Delete(Path.Combine(path, "steam_settings", "offline.txt"));
|
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
|
2021-01-09 14:17:09 -05:00
|
|
|
// Disable Networking
|
2021-01-10 10:08:12 -05:00
|
|
|
if (c.DisableNetworking)
|
2021-01-09 14:17:09 -05:00
|
|
|
{
|
2021-01-10 10:08:12 -05:00
|
|
|
await File.Create(Path.Combine(path, "steam_settings", "disable_networking.txt")).DisposeAsync()
|
|
|
|
.ConfigureAwait(false);
|
2021-01-09 14:17:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
File.Delete(Path.Combine(path, "steam_settings", "disable_networking.txt"));
|
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
|
2021-01-09 14:17:09 -05:00
|
|
|
// Disable Overlay
|
2021-01-10 10:08:12 -05:00
|
|
|
if (c.DisableOverlay)
|
2021-01-09 14:17:09 -05:00
|
|
|
{
|
2021-01-10 10:08:12 -05:00
|
|
|
await File.Create(Path.Combine(path, "steam_settings", "disable_overlay.txt")).DisposeAsync()
|
|
|
|
.ConfigureAwait(false);
|
2021-01-09 14:17:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
var goldbergDll = Path.Combine(_goldbergPath, $"{name}.dll");
|
2021-01-10 10:08:12 -05:00
|
|
|
|
2021-01-08 12:36:57 -05:00
|
|
|
if (!File.Exists(originalDll))
|
|
|
|
File.Move(steamApiDll, originalDll);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
File.Move(steamApiDll, guiBackup, true);
|
|
|
|
File.SetAttributes(guiBackup, FileAttributes.Hidden);
|
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
|
2021-01-08 12:36:57 -05:00
|
|
|
File.Copy(goldbergDll, steamApiDll);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool GoldbergApplied(string path)
|
|
|
|
{
|
2021-01-09 14:17:09 -05:00
|
|
|
var steamSettingsDirExists = Directory.Exists(Path.Combine(path, "steam_settings"));
|
|
|
|
var steamAppIdTxtExists = File.Exists(Path.Combine(path, "steam_appid.txt"));
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Debug($"Goldberg applied? {(steamSettingsDirExists && steamAppIdTxtExists).ToString()}");
|
2021-01-09 14:17:09 -05:00
|
|
|
return steamSettingsDirExists && steamAppIdTxtExists;
|
2021-01-08 12:36:57 -05:00
|
|
|
}
|
2021-01-10 10:08:12 -05:00
|
|
|
|
2021-01-08 12:36:57 -05:00
|
|
|
// Get webpage
|
2021-01-09 14:17:09 -05:00
|
|
|
// Get job id, compare with local if exists, save it if false or missing
|
2021-01-08 12:36:57 -05:00
|
|
|
// Get latest archive if mismatch, call Extract
|
2021-01-09 14:17:09 -05:00
|
|
|
public async Task<bool> Download()
|
2021-01-08 12:36:57 -05:00
|
|
|
{
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Info("Initializing download...");
|
2021-01-09 14:17:09 -05:00
|
|
|
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-01-13 15:26:35 -05:00
|
|
|
_log.Info("Check if update is needed...");
|
2021-01-09 14:17:09 -05:00
|
|
|
var jobIdLocal = Convert.ToInt32(File.ReadLines(jobIdPath).First().Trim());
|
|
|
|
var jobIdRemote = Convert.ToInt32(match.Groups["jobid"].Value);
|
|
|
|
_log.Debug($"job_id: local {jobIdLocal}; remote {jobIdRemote}");
|
2021-01-13 15:26:35 -05:00
|
|
|
if (jobIdLocal.Equals(jobIdRemote))
|
2021-01-09 14:17:09 -05:00
|
|
|
{
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Info("Latest Goldberg emulator is already available! Skipping...");
|
|
|
|
return false;
|
2021-01-09 14:17:09 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Info("Starting download...");
|
2021-01-15 11:46:13 -05:00
|
|
|
await StartDownload(match.Value).ConfigureAwait(false);
|
2021-01-13 15:26:35 -05:00
|
|
|
return true;
|
2021-01-09 14:17:09 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 11:46:13 -05:00
|
|
|
private async Task StartDownload(string downloadUrl)
|
2021-01-09 14:17:09 -05:00
|
|
|
{
|
2021-01-15 11:46:13 -05:00
|
|
|
var client = new HttpClient();
|
2021-01-09 14:17:09 -05:00
|
|
|
_log.Debug(downloadUrl);
|
|
|
|
await using var fileStream = File.OpenWrite(_goldbergZipPath);
|
|
|
|
//client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead)
|
2021-01-15 11:46:13 -05:00
|
|
|
var task = client.GetFileAsync(downloadUrl, fileStream).ConfigureAwait(false);
|
2021-01-09 14:17:09 -05:00
|
|
|
await task;
|
|
|
|
if (task.GetAwaiter().IsCompleted)
|
|
|
|
{
|
|
|
|
_log.Info("Download finished!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 12:36:57 -05:00
|
|
|
// Empty subfolder ./goldberg/
|
|
|
|
// Extract all from archive to subfolder ./goldberg/
|
2021-01-09 14:17:09 -05:00
|
|
|
public async Task Extract(string archivePath)
|
2021-01-08 12:36:57 -05:00
|
|
|
{
|
2021-01-13 15:26:35 -05:00
|
|
|
_log.Debug("Start extraction...");
|
2021-01-09 14:17:09 -05:00
|
|
|
await Task.Run(() =>
|
|
|
|
{
|
|
|
|
Directory.Delete(_goldbergPath, true);
|
|
|
|
ZipFile.ExtractToDirectory(archivePath, _goldbergPath);
|
|
|
|
}).ConfigureAwait(false);
|
2021-01-15 11:46:13 -05:00
|
|
|
_log.Debug("Extraction done!");
|
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)
|
|
|
|
{
|
2021-01-09 14:17:09 -05:00
|
|
|
_log.Debug($"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-10 10:08:12 -05:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 10:08:12 -05:00
|
|
|
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-10 10:08:12 -05:00
|
|
|
|
2021-01-08 12:36:57 -05:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|