wabbajack/Wabbajack.Downloaders.GameFile/GameLocator.cs

102 lines
2.8 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using GameFinder.StoreHandlers.EGS;
using GameFinder.StoreHandlers.GOG;
using GameFinder.StoreHandlers.Origin;
using GameFinder.StoreHandlers.Steam;
using Microsoft.Extensions.Logging;
using Wabbajack.DTOs;
using Wabbajack.Paths;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Downloaders.GameFile;
public class GameLocator : IGameLocator
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
private readonly EGSHandler _egs;
private readonly GOGHandler _gog;
private readonly Dictionary<Game, AbsolutePath> _locationCache;
private readonly ILogger<GameLocator> _logger;
private readonly OriginHandler? _origin;
private readonly SteamHandler _steam;
public GameLocator(ILogger<GameLocator> logger)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
_logger = logger;
_steam = new SteamHandler(logger);
if (OperatingSystem.IsWindows())
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
_origin = new OriginHandler(true, false, logger);
_gog = new GOGHandler(logger);
2021-10-23 16:51:17 +00:00
_egs = new EGSHandler(logger);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
_locationCache = new Dictionary<Game, AbsolutePath>();
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
_steam.FindAllGames();
_origin?.FindAllGames();
_gog?.FindAllGames();
_egs?.FindAllGames();
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public AbsolutePath GameLocation(Game game)
{
if (TryFindLocation(game, out var path))
return path;
throw new Exception($"Can't find game {game}");
}
public bool IsInstalled(Game game)
{
return TryFindLocation(game, out _);
}
public bool TryFindLocation(Game game, out AbsolutePath path)
{
lock (_locationCache)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
if (_locationCache.TryGetValue(game, out path))
return true;
if (TryFindLocationInner(game, out path))
{
_locationCache.Add(game, path);
return true;
}
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
return false;
}
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)))
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
path = steamGame!.Path.ToAbsolutePath();
return true;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
foreach (var gogGame in _gog.Games.Where(gogGame => metaData.GOGIDs.Contains(gogGame.GameID)))
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
path = gogGame!.Path.ToAbsolutePath();
return true;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
foreach (var egsGame in _egs.Games.Where(egsGame => metaData.EpicGameStoreIDs.Contains(egsGame.CatalogItemId)))
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
path = egsGame!.Path.ToAbsolutePath();
return true;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
if (_origin != null)
foreach (var originGame in _origin.Games.Where(originGame =>
metaData.EpicGameStoreIDs.Contains(originGame.Id)))
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
path = originGame.Path.ToAbsolutePath();
2021-09-27 12:42:46 +00:00
return true;
}
2021-10-23 16:51:17 +00:00
path = default;
return false;
2021-09-27 12:42:46 +00:00
}
}