|
| 1 | +using Flow.Launcher.Plugin; |
| 2 | +using GameFinder.Common; |
| 3 | +using GameFinder.RegistryUtils; |
| 4 | +using GameFinder.StoreHandlers.GOG; |
| 5 | +using GamesLauncher.Platforms.SyncEngines.Common.Interfaces; |
| 6 | +using NexusMods.Paths; |
| 7 | + |
| 8 | +namespace GamesLauncher.Platforms.SyncEngines.Gog |
| 9 | +{ |
| 10 | + internal class GogSyncEngine : ISyncEngine |
| 11 | + { |
| 12 | + public string PlatformName => "GOG Galaxy"; |
| 13 | + public IEnumerable<Game> SynchronizedGames { get; private set; } = Array.Empty<Game>(); |
| 14 | + |
| 15 | + |
| 16 | + private readonly GOGHandler handler = new (WindowsRegistry.Shared, FileSystem.Shared); |
| 17 | + private readonly IPublicAPI publicApi; |
| 18 | + |
| 19 | + public GogSyncEngine(IPublicAPI publicApi) |
| 20 | + { |
| 21 | + this.publicApi = publicApi; |
| 22 | + } |
| 23 | + |
| 24 | + public async Task SynchronizeGames() |
| 25 | + { |
| 26 | + var gogClient = await GogGalaxyClient.GetIfInstalled(); |
| 27 | + if (gogClient is null) |
| 28 | + return; |
| 29 | + |
| 30 | + var games = handler.FindAllGames().Where(x => x.IsGame()).Select(x => x.AsGame()); |
| 31 | + var syncedGames = new List<Game>(); |
| 32 | + |
| 33 | + foreach (var game in games) |
| 34 | + { |
| 35 | + syncedGames.Add(MapGogGameToGame(game, gogClient)); |
| 36 | + } |
| 37 | + |
| 38 | + SynchronizedGames = syncedGames; |
| 39 | + } |
| 40 | + |
| 41 | + private Game MapGogGameToGame(GOGGame gogGame, GogGalaxyClient gogClient) |
| 42 | + { |
| 43 | + return new Game( |
| 44 | + title: gogGame.Name, |
| 45 | + runTask: GetGameRunTask(gogGame, gogClient), |
| 46 | + iconPath: GetIconPath(gogGame, gogClient), |
| 47 | + platform: PlatformName, |
| 48 | + iconDelegate: null, |
| 49 | + uninstallAction: GetGogUninstallTask(gogGame) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + private Func<Task> GetGameRunTask(GOGGame gogGame, GogGalaxyClient gogClient) |
| 54 | + { |
| 55 | + var argString = $"/command=runGame /gameId={gogGame.Id} /path=\"{gogGame.Path}\""; |
| 56 | + |
| 57 | + return async () => |
| 58 | + { |
| 59 | + publicApi.ShellRun(argString, gogClient.ExePath); |
| 60 | + await Task.CompletedTask; |
| 61 | + }; |
| 62 | + } |
| 63 | + private static string GetIconPath(GOGGame gogGame, GogGalaxyClient gogClient) |
| 64 | + { |
| 65 | + var icoPath = Path.Combine(gogGame.Path.ToString(), $"goggame-{gogGame.Id}.ico"); |
| 66 | + |
| 67 | + if (File.Exists(icoPath)) |
| 68 | + return icoPath; |
| 69 | + else |
| 70 | + return gogClient.IconPath; |
| 71 | + } |
| 72 | + |
| 73 | + private UninstallAction? GetGogUninstallTask(GOGGame gogGame) |
| 74 | + { |
| 75 | + var uninstallExe = Path.Combine(gogGame.Path.ToString(), "unins000.exe"); |
| 76 | + |
| 77 | + if (!File.Exists(uninstallExe)) |
| 78 | + return null; |
| 79 | + |
| 80 | + return new UninstallAction(async () => |
| 81 | + { |
| 82 | + var directory = Path.GetDirectoryName(uninstallExe); |
| 83 | + var uninstallFileExe = Path.GetFileName(uninstallExe); |
| 84 | + publicApi.ShellRun($"cd /d \"{directory}\" && start \"\" \"{uninstallFileExe}\""); |
| 85 | + |
| 86 | + for (int i = 0; i < 12; i++) |
| 87 | + { |
| 88 | + var game = handler.FindAllGames().Where(x => x.IsGame()).Select(x => x.AsGame()).FirstOrDefault(x=> x.Id == gogGame.Id); |
| 89 | + |
| 90 | + if (game is null) |
| 91 | + break; |
| 92 | + |
| 93 | + await Task.Delay(TimeSpan.FromSeconds(5)); |
| 94 | + } |
| 95 | + |
| 96 | + await SynchronizeGames(); |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments