2020-01-03 17:03:09 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-04-28 22:52:20 +00:00
|
|
|
|
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);
|
2020-04-22 12:24:49 +00:00
|
|
|
|
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
|
|
|
|
|
2021-03-26 13:23:23 +00:00
|
|
|
|
private List<AStoreGame> _storeGames;
|
2020-01-03 17:03:09 +00:00
|
|
|
|
|
2021-03-26 11:35:27 +00:00
|
|
|
|
public Dictionary<Game, AStoreGame> Games = new();
|
|
|
|
|
|
2021-03-26 18:10:56 +00:00
|
|
|
|
private void FindGames<THandler, TGame>(Func<THandler> getHandler)
|
2021-03-26 11:35:27 +00:00
|
|
|
|
where THandler : AStoreHandler<TGame>
|
|
|
|
|
where TGame : AStoreGame
|
2020-01-03 17:03:09 +00:00
|
|
|
|
{
|
2021-03-26 18:10:56 +00:00
|
|
|
|
var handler = getHandler();
|
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}");
|
2021-03-26 13:23:23 +00:00
|
|
|
|
_storeGames.Add(game);
|
2021-03-26 11:35:27 +00:00
|
|
|
|
}
|
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 13:23:23 +00:00
|
|
|
|
Utils.Error(e, $"Could not load all Games from the {handler}");
|
2020-04-22 12:02:58 +00:00
|
|
|
|
}
|
2021-03-26 11:35:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StoreHandler()
|
|
|
|
|
{
|
2021-03-26 13:23:23 +00:00
|
|
|
|
_storeGames = new List<AStoreGame>();
|
2020-12-16 21:41:28 +00:00
|
|
|
|
|
2021-03-26 18:10:56 +00:00
|
|
|
|
FindGames<SteamHandler, SteamGame>(() => SteamHandler);
|
|
|
|
|
FindGames<GOGHandler, GOGGame>(() => GOGHandler);
|
|
|
|
|
FindGames<BethNetHandler, BethNetGame>(() => BethNetHandler);
|
|
|
|
|
FindGames<EGSHandler, EGSGame>(() => 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
|
|
|
|
|
2021-03-26 13:23:23 +00:00
|
|
|
|
foreach (var storeGame in _storeGames)
|
2021-03-26 11:35:27 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 18:57:02 +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
|
|
|
|
}
|
2020-04-28 22:52:20 +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)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|