using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using GameFinder; using GameFinder.StoreHandlers.BethNet; using GameFinder.StoreHandlers.EGS; using GameFinder.StoreHandlers.GOG; using GameFinder.StoreHandlers.Steam; namespace Wabbajack.Common.StoreHandlers { public class StoreHandler { private static readonly Lazy _instance = new(() => new StoreHandler(), isThreadSafe: true); public static StoreHandler Instance => _instance.Value; private static readonly Lazy _steamHandler = new(() => new SteamHandler()); public SteamHandler SteamHandler = _steamHandler.Value; private static readonly Lazy _gogHandler = new(() => new GOGHandler()); public GOGHandler GOGHandler = _gogHandler.Value; private static readonly Lazy _bethNetHandler = new(() => new BethNetHandler()); public BethNetHandler BethNetHandler = _bethNetHandler.Value; private static readonly Lazy _epicGameStoreHandler = new(() => new EGSHandler()); public EGSHandler EpicGameStoreHandler = _epicGameStoreHandler.Value; private static readonly Lazy _originHandler = new(() => new OriginHandler()); public OriginHandler OriginHandler = _originHandler.Value; public List StoreGames; public Dictionary Games = new(); private void FindGames(THandler handler, string name) where THandler : AStoreHandler where TGame : AStoreGame { try { handler.FindAllGames(); foreach (var game in handler.Games) { Utils.Log($"{handler.StoreType}: Found game {game}"); StoreGames.Add(game); } } catch (Exception e) { Utils.Error(e, $"Could not load all Games from the {name}"); } } public StoreHandler() { StoreGames = new List(); FindGames(SteamHandler, "SteamHandler"); FindGames(GOGHandler, "GOGHandler"); FindGames(BethNetHandler, "BethNetHandler"); FindGames(EpicGameStoreHandler, "EpicGameStoreHandler"); if (OriginHandler.Init()) { if (!OriginHandler.LoadAllGames()) 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!")); } foreach (var storeGame in StoreGames) { IEnumerable>? 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; var game = list.First().Key; if (Games.ContainsKey(game)) continue; Games.Add(game, storeGame); } } public AbsolutePath? TryGetGamePath(Game game) { if (Games.TryGetValue(game, out var storeGame)) return (AbsolutePath) storeGame.Path; return OriginHandler.Games.FirstOrDefault(x => x.Game == game)?.Path; } public static void Warmup() { Task.Run(() => _instance.Value).FireAndForget(); } } public class StoreException : Exception { public StoreException(string msg) : base(msg) { } } }