wabbajack/Wabbajack.Common/StoreHandlers/StoreHandler.cs

119 lines
4.5 KiB
C#
Raw Normal View History

2020-01-03 17:03:09 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2021-03-26 11:35:27 +00:00
using GameFinder;
using GameFinder.StoreHandlers.BethNet;
using GameFinder.StoreHandlers.EGS;
using GameFinder.StoreHandlers.GOG;
using GameFinder.StoreHandlers.Steam;
2020-01-03 17:03:09 +00:00
namespace Wabbajack.Common.StoreHandlers
{
public class StoreHandler
{
2021-03-26 11:35:27 +00:00
private static readonly Lazy<StoreHandler> _instance = new(() => new StoreHandler(), isThreadSafe: true);
public static StoreHandler Instance => _instance.Value;
2020-01-03 17:03:09 +00:00
2021-03-26 11:35:27 +00:00
private static readonly Lazy<SteamHandler> _steamHandler = new(() => new SteamHandler());
2020-01-03 17:03:09 +00:00
public SteamHandler SteamHandler = _steamHandler.Value;
2021-03-26 11:35:27 +00:00
private static readonly Lazy<GOGHandler> _gogHandler = new(() => new GOGHandler());
2020-01-03 17:03:09 +00:00
public GOGHandler GOGHandler = _gogHandler.Value;
2021-03-26 11:35:27 +00:00
private static readonly Lazy<BethNetHandler> _bethNetHandler = new(() => new BethNetHandler());
2020-04-22 12:02:58 +00:00
public BethNetHandler BethNetHandler = _bethNetHandler.Value;
2020-12-16 21:41:28 +00:00
2021-03-26 11:35:27 +00:00
private static readonly Lazy<EGSHandler> _epicGameStoreHandler = new(() => new EGSHandler());
public EGSHandler EpicGameStoreHandler = _epicGameStoreHandler.Value;
2021-01-05 05:07:06 +00:00
2021-03-26 11:35:27 +00:00
private static readonly Lazy<OriginHandler> _originHandler = new(() => new OriginHandler());
2021-01-05 05:07:06 +00:00
public OriginHandler OriginHandler = _originHandler.Value;
2020-04-22 12:02:58 +00:00
2020-01-03 17:03:09 +00:00
public List<AStoreGame> StoreGames;
2021-03-26 11:35:27 +00:00
public Dictionary<Game, AStoreGame> Games = new();
private void FindGames<THandler, TGame>(THandler handler, string name)
where THandler : AStoreHandler<TGame>
where TGame : AStoreGame
2020-01-03 17:03:09 +00:00
{
2021-03-26 11:35:27 +00:00
try
2020-01-03 17:03:09 +00:00
{
2021-03-26 11:35:27 +00:00
handler.FindAllGames();
foreach (var game in handler.Games)
{
Utils.Log($"{handler.StoreType}: Found game {game}");
StoreGames.Add(game);
}
2020-01-03 17:03:09 +00:00
}
2021-03-26 11:35:27 +00:00
catch (Exception e)
2020-04-22 12:02:58 +00:00
{
2021-03-26 11:35:27 +00:00
Utils.Error(e, $"Could not load all Games from the {name}");
2020-04-22 12:02:58 +00:00
}
2021-03-26 11:35:27 +00:00
}
public StoreHandler()
{
StoreGames = new List<AStoreGame>();
2020-12-16 21:41:28 +00:00
2021-03-26 11:35:27 +00:00
FindGames<SteamHandler, SteamGame>(SteamHandler, "SteamHandler");
FindGames<GOGHandler, GOGGame>(GOGHandler, "GOGHandler");
FindGames<BethNetHandler, BethNetGame>(BethNetHandler, "BethNetHandler");
FindGames<EGSHandler, EGSGame>(EpicGameStoreHandler, "EpicGameStoreHandler");
2021-01-05 05:07:06 +00:00
if (OriginHandler.Init())
{
2021-03-26 11:35:27 +00:00
if (!OriginHandler.LoadAllGames())
2021-01-05 05:07:06 +00:00
Utils.Error(new StoreException("Could not load all Games from the OriginHandler, check previous error messages!"));
}
else
{
Utils.Error(new StoreException("Could not Init the OriginHandler, check previous error messages!"));
}
2021-03-26 11:35:27 +00:00
foreach (var storeGame in StoreGames)
{
IEnumerable<KeyValuePair<Game, GameMetaData>>? enumerable = storeGame switch
{
SteamGame steamGame => GameRegistry.Games.Where(y => y.Value.SteamIDs?.Contains(steamGame.ID) ?? false),
GOGGame gogGame => GameRegistry.Games.Where(y => y.Value.GOGIDs?.Contains(gogGame.GameID) ?? false),
BethNetGame bethNetGame => GameRegistry.Games.Where(y => y.Value.BethNetID.Equals((int)bethNetGame.ID)),
EGSGame egsGame => GameRegistry.Games.Where(y => y.Value.EpicGameStoreIDs.Contains(egsGame.CatalogItemId ?? string.Empty)),
_ => null
};
if (enumerable == null) continue;
var list = enumerable.ToList();
if (list.Count == 0) continue;
2021-03-26 12:14:17 +00:00
var game = list.First().Key;
if (Games.ContainsKey(game)) continue;
2021-03-26 11:35:27 +00:00
2021-03-26 12:14:17 +00:00
Games.Add(game, storeGame);
2021-03-26 11:35:27 +00:00
}
2020-01-03 17:03:09 +00:00
}
public AbsolutePath? TryGetGamePath(Game game)
2020-01-03 17:03:09 +00:00
{
2021-03-26 11:35:27 +00:00
if (Games.TryGetValue(game, out var storeGame))
return (AbsolutePath) storeGame.Path;
return OriginHandler.Games.FirstOrDefault(x => x.Game == game)?.Path;
2020-01-03 17:03:09 +00:00
}
public static void Warmup()
{
Task.Run(() => _instance.Value).FireAndForget();
}
2020-01-03 17:03:09 +00:00
}
2021-03-26 11:35:27 +00:00
2020-01-03 17:03:09 +00:00
public class StoreException : Exception
{
public StoreException(string msg) : base(msg)
{
}
}
}