From e6bc17cc8bc9f81089a6034d99272ddfea598c98 Mon Sep 17 00:00:00 2001 From: erri120 Date: Sat, 15 Oct 2022 19:18:22 +0200 Subject: [PATCH] Update GameFinder to 2.0.0 --- Wabbajack.Downloaders.GameFile/GameLocator.cs | 135 +++++++++++++----- .../Wabbajack.Downloaders.GameFile.csproj | 8 +- .../Wabbajack.Installer.csproj | 5 - 3 files changed, 102 insertions(+), 46 deletions(-) diff --git a/Wabbajack.Downloaders.GameFile/GameLocator.cs b/Wabbajack.Downloaders.GameFile/GameLocator.cs index d2b28daf..5dbae2bb 100644 --- a/Wabbajack.Downloaders.GameFile/GameLocator.cs +++ b/Wabbajack.Downloaders.GameFile/GameLocator.cs @@ -1,3 +1,5 @@ +using System.Runtime.InteropServices; +using GameFinder.RegistryUtils; using GameFinder.StoreHandlers.EGS; using GameFinder.StoreHandlers.GOG; using GameFinder.StoreHandlers.Origin; @@ -5,37 +7,102 @@ using GameFinder.StoreHandlers.Steam; using Microsoft.Extensions.Logging; using Wabbajack.DTOs; using Wabbajack.Paths; +using Wabbajack.Paths.IO; namespace Wabbajack.Downloaders.GameFile; public class GameLocator : IGameLocator { - private readonly EGSHandler? _egs; + private readonly SteamHandler _steam; private readonly GOGHandler? _gog; + private readonly EGSHandler? _egs; + private readonly OriginHandler? _origin; + + private readonly Dictionary _steamGames = new(); + private readonly Dictionary _gogGames = new(); + private readonly Dictionary _egsGames = new(); + private readonly Dictionary _originGames = new(); + private readonly Dictionary _locationCache; private readonly ILogger _logger; - private readonly OriginHandler? _origin; - private readonly SteamHandler _steam; public GameLocator(ILogger logger) { _logger = logger; - _steam = new SteamHandler(logger); - if (OperatingSystem.IsWindows()) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - _origin = new OriginHandler(true, false, logger); - _gog = new GOGHandler(logger); + var windowsRegistry = new WindowsRegistry(); + + _steam = new SteamHandler(windowsRegistry); + _gog = new GOGHandler(windowsRegistry); + _egs = new EGSHandler(windowsRegistry); + _origin = new OriginHandler(); + } + else + { + _steam = new SteamHandler(null); + } + + _locationCache = new Dictionary(); + + FindAllGames(); + } - _egs = new EGSHandler(logger); + private void FindAllGames() + { + FindStoreGames(_steam.FindAllGames(), _steamGames, + steamGame => steamGame.Path, + steamGame => steamGame.AppId); + + if (_gog is not null) + { + FindStoreGames(_gog.FindAllGames(), _gogGames, + gogGame => gogGame.Path, + gogGame => gogGame.Id); } - _locationCache = new Dictionary(); + if (_egs is not null) + { + FindStoreGames(_egs.FindAllGames(), _egsGames, + egsGame => egsGame.InstallLocation, + egsGame => egsGame.CatalogItemId); + } - _steam.FindAllGames(); - _origin?.FindAllGames(); - _gog?.FindAllGames(); - _egs?.FindAllGames(); + if (_origin is not null) + { + FindStoreGames(_origin.FindAllGames(), _originGames, + originGame => originGame.InstallPath, + originGame => originGame.Id); + } + } + + private void FindStoreGames( + IEnumerable<(TGame? game, string? error)> games, + IDictionary paths, + Func getPath, + Func getId) + where TGame : class + { + foreach (var (game, error) in games) + { + if (game is not null) + { + var path = getPath(game).ToAbsolutePath(); + if (path.DirectoryExists()) + { + paths[getId(game)] = path; + } + else + { + _logger.LogError("Game {} does not exist at {}", game, path); + } + } + else + { + _logger.LogError("{}", error); + } + } } public AbsolutePath GameLocation(Game game) @@ -70,39 +137,33 @@ public class GameLocator : IGameLocator private bool TryFindLocationInner(Game game, out AbsolutePath path) { var metaData = game.MetaData(); - foreach (var steamGame in _steam.Games.Where(steamGame => metaData.SteamIDs.Contains(steamGame.ID))) + + int? steamId = metaData.SteamIDs.FirstOrDefault(id => _steamGames.ContainsKey(id)); + if (steamId.HasValue) { - path = steamGame!.Path.ToAbsolutePath(); + path = _steamGames[steamId.Value]; return true; } - if (_gog != null) + int? gogId = metaData.GOGIDs.FirstOrDefault(id => _gogGames.ContainsKey(id)); + if (gogId.HasValue) { - foreach (var gogGame in _gog.Games.Where(gogGame => metaData.GOGIDs.Contains(gogGame.GameID))) - { - path = gogGame!.Path.ToAbsolutePath(); - return true; - } + path = _gogGames[gogId.Value]; + return true; } - - if (_egs != null) + + var egsId = metaData.EpicGameStoreIDs.FirstOrDefault(id => _egsGames.ContainsKey(id)); + if (egsId is not null) { - foreach (var egsGame in _egs.Games.Where(egsGame => - metaData.EpicGameStoreIDs.Contains(egsGame.CatalogItemId))) - { - path = egsGame!.Path.ToAbsolutePath(); - return true; - } + path = _egsGames[egsId]; + return true; } - - if (_origin != null) + + var originId = metaData.OriginIDs.FirstOrDefault(id => _originGames.ContainsKey(id)); + if (originId is not null) { - foreach (var originGame in _origin.Games.Where(originGame => - metaData.EpicGameStoreIDs.Contains(originGame.Id))) - { - path = originGame.Path.ToAbsolutePath(); - return true; - } + path = _originGames[originId]; + return true; } path = default; diff --git a/Wabbajack.Downloaders.GameFile/Wabbajack.Downloaders.GameFile.csproj b/Wabbajack.Downloaders.GameFile/Wabbajack.Downloaders.GameFile.csproj index 0095b51a..4ee68a0c 100644 --- a/Wabbajack.Downloaders.GameFile/Wabbajack.Downloaders.GameFile.csproj +++ b/Wabbajack.Downloaders.GameFile/Wabbajack.Downloaders.GameFile.csproj @@ -18,10 +18,10 @@ - - - - + + + + diff --git a/Wabbajack.Installer/Wabbajack.Installer.csproj b/Wabbajack.Installer/Wabbajack.Installer.csproj index e5ed7579..23356d4d 100644 --- a/Wabbajack.Installer/Wabbajack.Installer.csproj +++ b/Wabbajack.Installer/Wabbajack.Installer.csproj @@ -23,11 +23,6 @@ - - - - -