示例#1
0
 public GameViewEntry(Game game, string category, GamesCollectionView view, ILibraryPlugin plugin)
 {
     this.plugin           = plugin;
     this.view             = view;
     Category              = new CategoryView(category);
     Game                  = game;
     Game.PropertyChanged += Game_PropertyChanged;
     Platform              = new PlatformView(game.PlatformId, view.Database.Platforms[game.PlatformId]);
 }
        private async void LoadGames(bool downloadLibUpdates)
        {
            if (GamesLoaderHandler.ProgressTask != null && GamesLoaderHandler.ProgressTask.Status == TaskStatus.Running)
            {
                GamesLoaderHandler.CancelToken.Cancel();
                await GamesLoaderHandler.ProgressTask;
            }

            GameAdditionAllowed = false;

            try
            {
                if (string.IsNullOrEmpty(Config.DatabasePath))
                {
                    return;
                }

                var database = GameDatabase.Instance;

                try
                {
                    database.OpenDatabase(Config.DatabasePath);
                }
                catch (Exception exc)
                {
                    GameAdditionAllowed = false;
                    PlayniteMessageBox.Show("Failed to open library database: " + exc.Message, "Database Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                LiteDBImageToImageConverter.ClearCache();
                GamesView?.Dispose();
                GamesView = new GamesCollectionView(database, Config);
                BindingOperations.EnableCollectionSynchronization(GamesView.Items, gamesLock);

                try
                {
                    GamesEditor.Instance.UpdateJumpList();
                }
                catch (Exception exc)
                {
                    logger.Error(exc, "Failed to set update JumpList data: ");
                }

                ListGamesView.ItemsSource   = GamesView.CollectionView;
                ImagesGamesView.ItemsSource = GamesView.CollectionView;
                GridGamesView.ItemsSource   = GamesView.CollectionView;

                if (downloadLibUpdates)
                {
                    GamesLoaderHandler.CancelToken  = new CancellationTokenSource();
                    GamesLoaderHandler.ProgressTask = Task.Factory.StartNew(() =>
                    {
                        ProgressControl.Visible       = Visibility.Visible;
                        ProgressControl.ProgressValue = 0;
                        ProgressControl.Text          = "Importing installed games...";

                        try
                        {
                            if (Config.UplaySettings.IntegrationEnabled)
                            {
                                database.UpdateInstalledGames(Provider.Uplay);
                                NotificationsWin.RemoveMessage(NotificationCodes.UplayInstalledImportError);
                            }
                        }
                        catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                        {
                            logger.Error(e, "Failed to import installed Uplay games.");
                            NotificationsWin.AddMessage(new NotificationMessage(NotificationCodes.UplayInstalledImportError, "Failed to import installed Uplay games:" + e.Message, NotificationType.Error, () =>
                            {
                            }));
                        }

                        try
                        {
                            if (Config.GOGSettings.IntegrationEnabled)
                            {
                                database.UpdateInstalledGames(Provider.GOG);
                                NotificationsWin.RemoveMessage(NotificationCodes.GOGLInstalledImportError);
                            }
                        }
                        catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                        {
                            logger.Error(e, "Failed to import installed GOG games.");
                            NotificationsWin.AddMessage(new NotificationMessage(NotificationCodes.GOGLInstalledImportError, "Failed to import installed GOG games:" + e.Message, NotificationType.Error, () =>
                            {
                            }));
                        }

                        try
                        {
                            if (Config.SteamSettings.IntegrationEnabled)
                            {
                                database.UpdateInstalledGames(Provider.Steam);
                                NotificationsWin.RemoveMessage(NotificationCodes.SteamInstalledImportError);
                            }
                        }
                        catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                        {
                            logger.Error(e, "Failed to import installed Steam games.");
                            NotificationsWin.AddMessage(new NotificationMessage(NotificationCodes.SteamInstalledImportError, "Failed to import installed Steam games: " + e.Message, NotificationType.Error, () =>
                            {
                            }));
                        }

                        try
                        {
                            if (Config.OriginSettings.IntegrationEnabled)
                            {
                                database.UpdateInstalledGames(Provider.Origin);
                                NotificationsWin.RemoveMessage(NotificationCodes.OriginInstalledImportError);
                            }
                        }
                        catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                        {
                            logger.Error(e, "Failed to import installed Origin games.");
                            NotificationsWin.AddMessage(new NotificationMessage(NotificationCodes.OriginInstalledImportError, "Failed to import installed Origin games: " + e.Message, NotificationType.Error, () =>
                            {
                            }));
                        }

                        ProgressControl.Text = "Downloading GOG library updates...";

                        try
                        {
                            if (Config.GOGSettings.IntegrationEnabled && Config.GOGSettings.LibraryDownloadEnabled)
                            {
                                database.UpdateOwnedGames(Provider.GOG);
                                NotificationsWin.RemoveMessage(NotificationCodes.GOGLibDownloadError);
                            }
                        }
                        catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                        {
                            logger.Error(e, "Failed to download GOG library updates.");
                            NotificationsWin.AddMessage(new NotificationMessage(NotificationCodes.GOGLibDownloadError, "Failed to download GOG library updates: " + e.Message, NotificationType.Error, () =>
                            {
                            }));
                        }

                        ProgressControl.Text = "Downloading Steam library updates...";

                        try
                        {
                            if (Config.SteamSettings.IntegrationEnabled && Config.SteamSettings.LibraryDownloadEnabled)
                            {
                                if (config.SteamSettings.IdSource == SteamIdSource.Name)
                                {
                                    database.SteamUserName = Config.SteamSettings.AccountName;
                                }
                                else
                                {
                                    database.SteamUserName = Config.SteamSettings.AccountId.ToString();
                                }

                                database.UpdateOwnedGames(Provider.Steam);
                                NotificationsWin.RemoveMessage(NotificationCodes.SteamLibDownloadError);
                            }
                        }
                        catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                        {
                            logger.Error(e, "Failed to download Steam library updates.");
                            NotificationsWin.AddMessage(new NotificationMessage(NotificationCodes.SteamLibDownloadError, "Failed to download Steam library updates: " + e.Message, NotificationType.Error, () =>
                            {
                            }));
                        }

                        if (importSteamCatWizard && importSteamCatWizardId != 0)
                        {
                            ProgressControl.Text = "Importing Steam categories...";

                            try
                            {
                                var steamLib = new SteamLibrary();
                                GameDatabase.Instance.ImportCategories(steamLib.GetCategorizedGames(importSteamCatWizardId));
                            }
                            catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                            {
                                logger.Error(e, "Failed to import Steam categories.");
                                NotificationsWin.AddMessage(new NotificationMessage(NotificationCodes.SteamLibDownloadError, "Failed to import Steam categories: " + e.Message, NotificationType.Error, () =>
                                {
                                }));
                            }
                        }

                        ProgressControl.Text = "Downloading Origin library updates...";

                        try
                        {
                            if (Config.OriginSettings.IntegrationEnabled && Config.OriginSettings.LibraryDownloadEnabled)
                            {
                                database.UpdateOwnedGames(Provider.Origin);
                                NotificationsWin.RemoveMessage(NotificationCodes.OriginLibDownloadError);
                            }
                        }
                        catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                        {
                            logger.Error(e, "Failed to download Origin library updates.");
                            NotificationsWin.AddMessage(new NotificationMessage(NotificationCodes.OriginLibDownloadError, "Failed to download Origin library updates: " + e.Message, NotificationType.Error, () =>
                            {
                            }));
                        }

                        ProgressControl.Text        = "Downloading images and game details...";
                        ProgressControl.ProgressMin = 0;

                        var gamesCount = 0;
                        gamesCount     = database.GamesCollection.Count(a => a.Provider != Provider.Custom && !a.IsProviderDataUpdated);
                        if (gamesCount > 0)
                        {
                            gamesCount -= 1;
                        }

                        ProgressControl.ProgressMax = gamesCount;

                        var tasks = new List <Task>
                        {
                            // Steam metada download thread
                            Task.Factory.StartNew(() =>
                            {
                                DownloadMetadata(database, Provider.Steam, ProgressControl, GamesLoaderHandler.CancelToken.Token);
                            }),

                            // Origin metada download thread
                            Task.Factory.StartNew(() =>
                            {
                                DownloadMetadata(database, Provider.Origin, ProgressControl, GamesLoaderHandler.CancelToken.Token);
                            }),

                            // GOG metada download thread
                            Task.Factory.StartNew(() =>
                            {
                                DownloadMetadata(database, Provider.GOG, ProgressControl, GamesLoaderHandler.CancelToken.Token);
                            }),

                            // Uplay metada download thread
                            Task.Factory.StartNew(() =>
                            {
                                DownloadMetadata(database, Provider.Uplay, ProgressControl, GamesLoaderHandler.CancelToken.Token);
                            })
                        };

                        Task.WaitAll(tasks.ToArray());

                        ProgressControl.Text = "Library update finished";

                        Thread.Sleep(1500);
                        ProgressControl.Visible = Visibility.Collapsed;
                    });

                    await GamesLoaderHandler.ProgressTask;
                }
            }
            finally
            {
                GamesEditor.Instance.OnPropertyChanged("LastGames");
                GameAdditionAllowed = true;
            }
        }