Init commit

This commit is contained in:
Jeddunk 2021-01-08 18:36:57 +01:00
commit 0c711f7da6
26 changed files with 1733 additions and 0 deletions

View file

@ -0,0 +1,217 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using GoldbergGUI.Core.Models;
using MvvmCross.Logging;
namespace GoldbergGUI.Core.Services
{
// downloads and updates goldberg emu
// sets up config files
// does file copy stuff
public interface IGoldbergService
{
public Task<(string accountName, long userSteamId)> Initialize(IMvxLog log);
public Task<(int appId, List<SteamApp> dlcList)> Read(string path);
public Task Save(string path, int appId, List<SteamApp> dlcList);
public bool GoldbergApplied(string path);
public void Download();
public void Extract(string archivePath);
public Task GenerateInterfacesFile(string filePath);
}
// ReSharper disable once UnusedType.Global
public class GoldbergService : IGoldbergService
{
private IMvxLog _log;
//private const string GoldbergUrl = "https://mr_goldberg.gitlab.io/goldberg_emulator/";
private readonly string _goldbergPath = Path.Combine(Directory.GetCurrentDirectory(), "goldberg");
private static readonly string GlobalSettingsPath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Goldberg SteamEmu Saves");
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 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
public async Task<(string accountName, long userSteamId)> Initialize(IMvxLog log)
{
_log = log;
var accountName = "Account name...";
long steamId = -1;
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))
_log.Error("Invalid User Steam ID!");
}).ConfigureAwait(false);
return (accountName, steamId);
}
// If first time, call GenerateInterfaces
// else try to read config
public async Task<(int appId, List<SteamApp> dlcList)> Read(string path)
{
var appId = -1;
var dlcList = new List<SteamApp>();
var steamAppidTxt = Path.Combine(path, "steam_appid.txt");
if (File.Exists(steamAppidTxt))
{
await Task.Run(() => int.TryParse(File.ReadLines(steamAppidTxt).First().Trim(), out appId))
.ConfigureAwait(false);
}
var dlcTxt = Path.Combine(path, "steam_settings", "DLC.txt");
if (File.Exists(dlcTxt))
{
var readAllLinesAsync = await File.ReadAllLinesAsync(dlcTxt).ConfigureAwait(false);
var expression = new Regex(@"(?<id>.*) *= *(?<name>.*)");
foreach (var line in readAllLinesAsync)
{
var match = expression.Match(line);
if (match.Success)
dlcList.Add(new SteamApp {AppId = Convert.ToInt32(match.Groups["id"].Value),
Name = match.Groups["name"].Value});
}
}
return (appId, dlcList);
}
// 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, int appId, List<SteamApp> dlcList)
{
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);
}
if (!Directory.Exists(Path.Combine(path, "steam_settings")))
{
Directory.CreateDirectory(Path.Combine(path, "steam_settings"));
}
await File.WriteAllTextAsync(Path.Combine(path, "steam_appid.txt"), appId.ToString()).ConfigureAwait(false);
var dlcString = "";
dlcList.ForEach(x => dlcString += $"{x}\n");
await File.WriteAllTextAsync(Path.Combine(path, "steam_settings", "DLC.txt"), dlcString).ConfigureAwait(false);
}
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");
if (!File.Exists(originalDll))
File.Move(steamApiDll, originalDll);
else
{
File.Move(steamApiDll, guiBackup, true);
File.SetAttributes(guiBackup, FileAttributes.Hidden);
}
File.Copy(goldbergDll, steamApiDll);
}
public bool GoldbergApplied(string path)
{
//throw new NotImplementedException();
return true;
}
// Get webpage
// Get commit, compare with local if exists, save it if false or missing
// Get latest archive if mismatch, call Extract
public void Download()
{
//throw new NotImplementedException();
}
// Empty subfolder ./goldberg/
// Extract all from archive to subfolder ./goldberg/
public void Extract(string archivePath)
{
//throw new NotImplementedException();
}
// https://gitlab.com/Mr_Goldberg/goldberg_emulator/-/blob/master/generate_interfaces_file.cpp
// (maybe) check DLL date first
public async Task GenerateInterfacesFile(string filePath)
{
//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"));
}
}
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);
}
}
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);
}
return success;
}
}
}

View file

@ -0,0 +1,201 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AngleSharp.Dom;
using AngleSharp.Html.Parser;
using GoldbergGUI.Core.Models;
using GoldbergGUI.Core.Utils;
using MvvmCross.Logging;
using NinjaNye.SearchExtensions;
using SteamStorefrontAPI;
namespace GoldbergGUI.Core.Services
{
// gets info from steam api
public interface ISteamService
{
public Task Initialize(IMvxLog log);
public IEnumerable<SteamApp> GetListOfAppsByName(string name);
public SteamApp GetAppByName(string name);
public SteamApp GetAppById(int appid);
public Task<List<SteamApp>> GetListOfDlc(SteamApp steamApp, bool useSteamDb);
}
// ReSharper disable once UnusedType.Global
public class SteamService : ISteamService
{
private const string CachePath = "steamapps.json";
private const string SteamUri = "https://api.steampowered.com/ISteamApps/GetAppList/v2/";
private const string UserAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/87.0.4280.88 Safari/537.36";
private HashSet<SteamApp> _cache = new HashSet<SteamApp>();
private IMvxLog _log;
public async Task Initialize(IMvxLog log)
{
_log = log;
_log.Info("Updating cache...");
var updateNeeded = DateTime.Now.Subtract(File.GetLastWriteTimeUtc(CachePath)).TotalDays >= 1;
string cacheString;
if (updateNeeded)
{
_log.Info("Getting content from API...");
var client = new HttpClient();
var httpCall = client.GetAsync(SteamUri);
var response = await httpCall.ConfigureAwait(false);
var readAsStringAsync = response.Content.ReadAsStringAsync();
var responseBody = await readAsStringAsync.ConfigureAwait(false);
_log.Info("Got content from API successfully. Writing to file...");
await File.WriteAllTextAsync(CachePath, responseBody, Encoding.UTF8).ConfigureAwait(false);
cacheString = responseBody;
_log.Info("Cache written to file successfully.");
}
else
{
_log.Info("Cache already up to date!");
cacheString = await File.ReadAllTextAsync(CachePath).ConfigureAwait(false);
}
var steamApps = JsonSerializer.Deserialize<SteamApps>(cacheString);
_cache = new HashSet<SteamApp>(steamApps.AppList.Apps);
_log.Info("Loaded cache into memory!");
}
public IEnumerable<SteamApp> GetListOfAppsByName(string name)
{
var listOfAppsByName = _cache.Search(x => x.Name)
.SetCulture(StringComparison.OrdinalIgnoreCase)
.ContainingAll(name.Split(' ')).ToHashSet();
foreach (var steamApp in listOfAppsByName)
{
var sa = Task.Run(async () => await AppDetails.GetAsync(steamApp.AppId).ConfigureAwait(false)).Result;
if (sa.Type != AppType.Game) listOfAppsByName.Remove(steamApp);
}
return listOfAppsByName;
}
public SteamApp GetAppByName(string name)
{
_log.Info($"Trying to get app {name}");
var comparableName = Regex.Replace(name, Misc.SpecialCharsRegex, "").ToLower();
var app = _cache.FirstOrDefault(x => x.CompareName(comparableName));
if (app != null) _log.Info($"Successfully got app {app}");
return app;
}
public SteamApp GetAppById(int appid)
{
_log.Info($"Trying to get app with ID {appid}");
var app = _cache.FirstOrDefault(x => x.AppId.Equals(appid));
if (app != null) _log.Info($"Successfully got app {app}");
return app;
}
public async Task<List<SteamApp>> GetListOfDlc(SteamApp steamApp, bool useSteamDb)
{
_log.Info("Get DLC");
var dlcList = new List<SteamApp>();
if (steamApp != null)
{
var task = AppDetails.GetAsync(steamApp.AppId);
var steamAppDetails = await task.ConfigureAwait(true);
if (steamAppDetails.Type == AppType.Game)
{
steamAppDetails.DLC.ForEach(x =>
{
var result = _cache.FirstOrDefault(y => y.AppId.Equals(x)) ??
new SteamApp {AppId = x, Name = $"Unknown DLC {x}"};
dlcList.Add(result);
});
dlcList.ForEach(x => _log.Debug($"{x.AppId}={x.Name}"));
_log.Info("Got DLC successfully...");
// Get DLC from SteamDB
// Get Cloudflare cookie
// Scrape and parse HTML page
// Add missing to DLC list
if (useSteamDb)
{
var steamDbUri = new Uri($"https://steamdb.info/app/{steamApp.AppId}/dlc/");
/* var handler = new ClearanceHandler();
var client = new HttpClient(handler);
var content = client.GetStringAsync(steamDbUri).Result;
_log.Debug(content); */
var client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
_log.Info("Get SteamDB App");
var httpCall = client.GetAsync(steamDbUri);
var response = await httpCall.ConfigureAwait(false);
_log.Debug(httpCall.Status.ToString());
_log.Debug(response.EnsureSuccessStatusCode().ToString());
var readAsStringAsync = response.Content.ReadAsStringAsync();
var responseBody = await readAsStringAsync.ConfigureAwait(false);
_log.Debug(readAsStringAsync.Status.ToString());
var parser = new HtmlParser();
var doc = parser.ParseDocument(responseBody);
// Console.WriteLine(doc.DocumentElement.OuterHtml);
var query1 = doc.QuerySelector("#dlc");
if (query1 != null)
{
var query2 = query1.QuerySelectorAll(".app");
foreach (var element in query2)
{
var dlcId = element.GetAttribute("data-appid");
var dlcName = $"Unknown DLC {dlcId}";
var query3 = element.QuerySelectorAll("td");
if (query3 != null) dlcName = query3[1].Text().Replace("\n", "").Trim();
var dlcApp = new SteamApp {AppId = Convert.ToInt32(dlcId), Name = dlcName};
var i = dlcList.FindIndex(x => x.AppId.Equals(dlcApp.AppId));
if (i > -1)
{
if (dlcList[i].Name.Contains("Unknown DLC")) dlcList[i] = dlcApp;
}
else
{
dlcList.Add(dlcApp);
}
}
dlcList.ForEach(x => _log.Debug($"{x.AppId}={x.Name}"));
_log.Info("Got DLC from SteamDB successfully...");
}
else
{
_log.Error("Could not get DLC from SteamDB!");
}
}
}
else
{
_log.Error("Could not get DLC: Steam App is not of type \"game\"");
}
}
else
{
_log.Error("Could not get DLC: Invalid Steam App");
}
return dlcList;
}
}
}