wabbajack/Wabbajack.Downloaders.GameFile/GameLocator.cs

172 lines
4.4 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
{
2022-10-15 17:18:22 +00:00
private readonly EGSHandler? _egs;
2022-10-19 22:17:25 +00:00
private readonly GOGHandler? _gog;
2021-10-23 16:51:17 +00:00
private readonly Dictionary<Game, AbsolutePath> _locationCache;
private readonly ILogger<GameLocator> _logger;
2022-10-19 22:17:25 +00:00
private readonly OriginHandler? _origin;
private readonly SteamHandler _steam;
2021-10-23 16:51:17 +00:00
public GameLocator(ILogger<GameLocator> logger)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
_logger = logger;
2022-10-19 22:17:25 +00:00
_steam = new SteamHandler(logger);
2021-10-23 16:51:17 +00:00
2022-10-19 22:17:25 +00:00
if (OperatingSystem.IsWindows())
2021-09-27 12:42:46 +00:00
{
2022-10-19 22:17:25 +00:00
_origin = new OriginHandler(true, false, logger);
_gog = new GOGHandler(logger);
_egs = new EGSHandler(logger);
2022-10-15 17:18:22 +00:00
}
2022-10-19 22:17:25 +00:00
_locationCache = new Dictionary<Game, AbsolutePath>();
2022-10-19 22:17:25 +00:00
try
2022-10-15 17:18:22 +00:00
{
2022-10-19 22:17:25 +00:00
_steam.FindAllGames();
}
catch (Exception ex)
{
_logger.LogError(ex, "While finding Steam games");
2021-10-23 16:51:17 +00:00
}
2021-09-27 12:42:46 +00:00
2022-10-19 22:17:25 +00:00
try
2022-10-15 17:18:22 +00:00
{
2022-10-19 22:17:25 +00:00
_origin?.FindAllGames();
}
catch (Exception ex)
{
_logger.LogInformation(ex, "While finding Origin games");
2022-10-15 17:18:22 +00:00
}
2021-09-27 12:42:46 +00:00
2022-10-19 22:17:25 +00:00
try
2022-10-15 17:18:22 +00:00
{
2022-10-19 22:17:25 +00:00
_gog?.FindAllGames();
}
catch (Exception ex)
{
_logger.LogInformation(ex, "While finding GoG games");
2022-10-15 17:18:22 +00:00
}
2022-10-19 22:17:25 +00:00
try
2022-10-15 17:18:22 +00:00
{
2022-10-19 22:17:25 +00:00
_egs?.FindAllGames();
}
catch (Exception ex)
{
_logger.LogInformation(ex, "While finding Epic Games");
2022-10-15 17:18:22 +00:00
}
2021-10-23 16:51:17 +00:00
}
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();
2022-10-15 17:18:22 +00:00
2022-10-19 17:28:07 +00:00
try
2021-09-27 12:42:46 +00:00
{
2022-10-19 22:17:25 +00:00
foreach (var steamGame in _steam.Games.Where(steamGame => metaData.SteamIDs.Contains(steamGame.ID)))
2022-10-19 17:28:07 +00:00
{
2022-10-19 22:17:25 +00:00
path = steamGame!.Path.ToAbsolutePath();
2022-10-19 17:28:07 +00:00
return true;
}
2021-09-27 12:42:46 +00:00
}
2022-10-19 17:28:07 +00:00
catch (Exception ex)
2021-09-27 12:42:46 +00:00
{
2022-10-19 22:17:25 +00:00
_logger.LogInformation(ex, "While finding {Game} from Steam", game);
2021-09-27 12:42:46 +00:00
}
2022-10-19 17:28:07 +00:00
try
{
2022-10-19 22:17:25 +00:00
if (_gog != null)
2022-10-19 17:28:07 +00:00
{
2022-10-19 22:17:25 +00:00
foreach (var gogGame in _gog.Games.Where(gogGame => metaData.GOGIDs.Contains(gogGame.GameID)))
{
path = gogGame!.Path.ToAbsolutePath();
return true;
}
2022-10-19 17:28:07 +00:00
}
}
catch (Exception ex)
2021-09-27 12:42:46 +00:00
{
2022-10-19 22:17:25 +00:00
_logger.LogInformation(ex, "While finding {Game} from GoG", game);
2021-10-23 16:51:17 +00:00
}
2022-10-19 17:28:07 +00:00
try
2021-10-24 04:39:57 +00:00
{
2022-10-19 22:17:25 +00:00
if (_egs != null)
2022-10-19 17:28:07 +00:00
{
2022-10-19 22:17:25 +00:00
foreach (var egsGame in _egs.Games.Where(egsGame =>
metaData.EpicGameStoreIDs.Contains(egsGame.CatalogItemId)))
{
path = egsGame!.Path.ToAbsolutePath();
return true;
}
2022-10-19 17:28:07 +00:00
}
2021-10-24 04:39:57 +00:00
}
2022-10-19 17:28:07 +00:00
catch (Exception ex)
{
2022-10-19 22:17:25 +00:00
_logger.LogInformation(ex, "While finding {Game} from Epic", game);
2022-10-19 17:28:07 +00:00
}
try
{
2022-10-19 22:17:25 +00:00
if (_origin != null)
2022-10-19 17:28:07 +00:00
{
2022-10-19 22:17:25 +00:00
foreach (var originGame in _origin.Games.Where(originGame =>
metaData.EpicGameStoreIDs.Contains(originGame.Id)))
{
path = originGame.Path.ToAbsolutePath();
return true;
}
2022-10-19 17:28:07 +00:00
}
}
catch (Exception ex)
{
2022-10-19 22:17:25 +00:00
_logger.LogInformation(ex, "While finding {Game} from Origin", game);
2022-10-19 17:28:07 +00:00
}
2021-10-23 16:51:17 +00:00
path = default;
return false;
2021-09-27 12:42:46 +00:00
}
}