Ejemplo n.º 1
0
        public async Task DownloadMetadataAsync(
            List <Game> games,
            GameDatabase database,
            MetadataDownloaderSettings settings,
            Action <Game, int, int> processCallback,
            CancellationTokenSource cancelToken)
        {
            await Task.Run(() =>
            {
                if (games == null || games.Count == 0)
                {
                    return;
                }

                for (int i = 0; i < games.Count; i++)
                {
                    if (cancelToken?.IsCancellationRequested == true)
                    {
                        return;
                    }

                    GameMetadata storeData = null;
                    GameMetadata igdbData  = null;
                    GameMetadata gameData  = null;

                    // We need to get new instance from DB in case game got edited or deleted.
                    // We don't want to block game editing while metadata is downloading for other games.
                    // TODO: Use Id instead of GameId once we replace LiteDB and have proper autoincrement Id
                    var game = database.GamesCollection.FindOne(a => a.PluginId == games[i].PluginId && a.GameId == games[i].GameId);
                    if (game == null)
                    {
                        logger.Warn($"Game {game.GameId} no longer in DB, skipping metadata download.");
                        processCallback?.Invoke(null, i, games.Count);
                        continue;
                    }

                    try
                    {
                        logger.Debug($"Downloading metadata for {game.PluginId} game {game.Name}, {game.GameId}");

                        // Name
                        if (!game.IsCustomGame && settings.Name.Import)
                        {
                            gameData = ProcessField(game, settings.Name, ref storeData, ref igdbData, (a) => a.GameData?.Name);
                            if (!string.IsNullOrEmpty(gameData?.GameData?.Name))
                            {
                                game.Name       = StringExtensions.RemoveTrademarks(gameData.GameData.Name);
                                var sortingName = StringExtensions.ConvertToSortableName(game.Name);
                                if (sortingName != game.Name)
                                {
                                    game.SortingName = sortingName;
                                }
                            }
                        }

                        // Genre
                        if (settings.Genre.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && ListExtensions.IsNullOrEmpty(game.Genres)))
                            {
                                gameData    = ProcessField(game, settings.Genre, ref storeData, ref igdbData, (a) => a.GameData?.Genres);
                                game.Genres = ListExtensions.IsNullOrEmpty(gameData?.GameData?.Genres) ? game.Genres : gameData.GameData.Genres;
                            }
                        }

                        // Release Date
                        if (settings.ReleaseDate.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && game.ReleaseDate == null))
                            {
                                gameData         = ProcessField(game, settings.ReleaseDate, ref storeData, ref igdbData, (a) => a.GameData?.ReleaseDate);
                                game.ReleaseDate = gameData?.GameData?.ReleaseDate ?? game.ReleaseDate;
                            }
                        }

                        // Developer
                        if (settings.Developer.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && ListExtensions.IsNullOrEmpty(game.Developers)))
                            {
                                gameData        = ProcessField(game, settings.Developer, ref storeData, ref igdbData, (a) => a.GameData?.Developers);
                                game.Developers = ListExtensions.IsNullOrEmpty(gameData?.GameData?.Developers) ? game.Developers : gameData.GameData.Developers;
                            }
                        }

                        // Publisher
                        if (settings.Publisher.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && ListExtensions.IsNullOrEmpty(game.Publishers)))
                            {
                                gameData        = ProcessField(game, settings.Publisher, ref storeData, ref igdbData, (a) => a.GameData?.Publishers);
                                game.Publishers = ListExtensions.IsNullOrEmpty(gameData?.GameData?.Publishers) ? game.Publishers : gameData.GameData.Publishers;
                            }
                        }

                        // Tags / Features
                        if (settings.Tag.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && ListExtensions.IsNullOrEmpty(game.Tags)))
                            {
                                gameData  = ProcessField(game, settings.Tag, ref storeData, ref igdbData, (a) => a.GameData?.Tags);
                                game.Tags = ListExtensions.IsNullOrEmpty(gameData?.GameData?.Tags) ? game.Tags : gameData.GameData.Tags;
                            }
                        }

                        // Description
                        if (settings.Description.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.Description)))
                            {
                                gameData         = ProcessField(game, settings.Description, ref storeData, ref igdbData, (a) => a.GameData?.Description);
                                game.Description = string.IsNullOrEmpty(gameData?.GameData?.Description) == true ? game.Description : gameData.GameData.Description;
                            }
                        }

                        // Links
                        if (settings.Links.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && game.Links == null))
                            {
                                gameData   = ProcessField(game, settings.Links, ref storeData, ref igdbData, (a) => a.GameData?.Links);
                                game.Links = gameData?.GameData?.Links ?? game.Links;
                            }
                        }

                        // Critic Score
                        if (settings.CriticScore.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && game.CriticScore == null))
                            {
                                gameData         = ProcessField(game, settings.CriticScore, ref storeData, ref igdbData, (a) => a.GameData?.CriticScore);
                                game.CriticScore = gameData?.GameData?.CriticScore == null ? game.CriticScore : gameData.GameData.CriticScore;
                            }
                        }

                        // Community Score
                        if (settings.CommunityScore.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && game.CommunityScore == null))
                            {
                                gameData            = ProcessField(game, settings.CommunityScore, ref storeData, ref igdbData, (a) => a.GameData?.CommunityScore);
                                game.CommunityScore = gameData?.GameData?.CommunityScore == null ? game.CommunityScore : gameData.GameData.CommunityScore;
                            }
                        }

                        // BackgroundImage
                        if (settings.BackgroundImage.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.BackgroundImage)))
                            {
                                gameData             = ProcessField(game, settings.BackgroundImage, ref storeData, ref igdbData, (a) => a.BackgroundImage);
                                game.BackgroundImage = string.IsNullOrEmpty(gameData?.BackgroundImage) ? game.BackgroundImage : gameData.BackgroundImage;
                            }
                        }

                        // Cover
                        if (settings.CoverImage.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.CoverImage)))
                            {
                                gameData = ProcessField(game, settings.CoverImage, ref storeData, ref igdbData, (a) => a.Image);
                                if (gameData?.Image != null)
                                {
                                    if (!string.IsNullOrEmpty(game.CoverImage))
                                    {
                                        database.DeleteImageSafe(game.CoverImage, game);
                                    }

                                    var imageId     = database.AddFileNoDuplicate(gameData.Image);
                                    game.CoverImage = imageId;
                                }
                            }
                        }

                        // Icon
                        if (settings.Icon.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.Icon)))
                            {
                                gameData = ProcessField(game, settings.Icon, ref storeData, ref igdbData, (a) => a.Icon);
                                if (gameData?.Icon != null)
                                {
                                    if (!string.IsNullOrEmpty(game.Icon))
                                    {
                                        database.DeleteImageSafe(game.Icon, game);
                                    }

                                    var iconId = database.AddFileNoDuplicate(gameData.Icon);
                                    game.Icon  = iconId;
                                }
                            }
                        }

                        // TODO make this configurable and re-downalodable manually
                        // Only update them if they don't exist yet
                        if (game.OtherActions?.Any() != true && storeData != null)
                        {
                            if (storeData?.GameData?.OtherActions?.Any() == true)
                            {
                                game.OtherActions = new System.Collections.ObjectModel.ObservableCollection <GameAction>();
                                foreach (var task in storeData.GameData.OtherActions)
                                {
                                    game.OtherActions.Add(task);
                                }
                            }
                        }

                        // Just to be sure check if somebody didn't remove game while downloading data
                        if (database.GamesCollection.FindOne(a => a.GameId == games[i].GameId) != null)
                        {
                            database.UpdateGameInDatabase(game);
                        }
                        else
                        {
                            logger.Warn($"Game {game.GameId} no longer in DB, skipping metadata update in DB.");
                        }
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(e, $"Failed to download metadata for game {game.Name}, {game.GameId}");
                    }
                    finally
                    {
                        processCallback?.Invoke(game, i, games.Count);
                    }
                }
            });
        }
Ejemplo n.º 2
0
        public async Task MissingDataTest()
        {
            // Tests that existing data are not overriden by empty metadata from external providers.
            var storeId         = Guid.NewGuid();
            var storeDownloader = GetTestLibraryProvider();

            storeDownloader.ReturnMetadata = GameMetadata.GetEmptyData();
            var testPlugin = GetTestPlugin();

            testPlugin.ReturnMetadata = GameMetadata.GetEmptyData();
            testPlugin.SetSupportedFields(new List <MetadataField>
            {
                MetadataField.Description,
                MetadataField.Icon,
                MetadataField.CoverImage,
                MetadataField.BackgroundImage,
                MetadataField.Links,
                MetadataField.Publishers,
                MetadataField.Developers,
                MetadataField.Tags,
                MetadataField.Genres,
                MetadataField.ReleaseDate
            });

            List <MetadataPlugin> metadataDownloaders = new List <MetadataPlugin>()
            {
                testPlugin
            };

            Dictionary <Guid, LibraryMetadataProvider> libraryDownloaders = new Dictionary <Guid, LibraryMetadataProvider>()
            {
                { storeId, storeDownloader }
            };

            using (var temp = TempDirectory.Create())
            {
                var db = new GameDatabase(temp.TempPath);
                db.OpenDatabase();
                Game.DatabaseReference = db;

                var importedGame = db.ImportGame(new GameInfo()
                {
                    Name   = "Game",
                    GameId = "storeId",
                    Genres = new List <string>()
                    {
                        "Genre"
                    },
                    ReleaseDate = new DateTime(2012, 6, 6),
                    Developers  = new List <string>()
                    {
                        "Developer"
                    },
                    Publishers = new List <string>()
                    {
                        "Publisher"
                    },
                    Tags = new List <string>()
                    {
                        "Tag"
                    },
                    Description = "Description",
                    Links       = new List <Link>()
                    {
                        new Link()
                    }
                });

                importedGame.PluginId        = storeId;
                importedGame.Icon            = "icon";
                importedGame.CoverImage      = "image";
                importedGame.BackgroundImage = "backImage";
                db.Games.Update(importedGame);

                var downloader = new MetadataDownloader(db, metadataDownloaders, libraryDownloaders);
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = false
                };

                var dbGames = db.Games.ToList();
                var f       = dbGames[0].ReleaseDate;
                var s       = importedGame.ReleaseDate;

                settings.ConfigureFields(new List <Guid> {
                    testPlugin.Id, Guid.Empty
                }, true);
                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, new PlayniteSettings(), null, null);

                dbGames = db.Games.ToList();
                Assert.AreEqual(1, testPlugin.CallCount);
                Assert.AreEqual(1, storeDownloader.CallCount);
                var game = dbGames[0];
                Assert.AreEqual("Description", game.Description);
                Assert.AreEqual("icon", game.Icon);
                Assert.AreEqual("image", game.CoverImage);
                Assert.AreEqual("backImage", game.BackgroundImage);
                Assert.AreEqual("Developer", game.Developers[0].Name);
                Assert.AreEqual("Publisher", game.Publishers[0].Name);
                Assert.AreEqual("Genre", game.Genres[0].Name);
                CollectionAssert.IsNotEmpty(game.Links);
                Assert.AreEqual("Tag", game.Tags[0].Name);
                Assert.AreEqual(2012, game.ReleaseDate.Value.Year);
            }
        }
Ejemplo n.º 3
0
        public async Task SkipExistingTest()
        {
            // Tests that existing data are not overriden even if metadata provider has them.
            var testPlugin = GetTestPlugin();

            testPlugin.SetSupportedFields(new List <MetadataField>
            {
                MetadataField.Description,
                MetadataField.Icon,
                MetadataField.CoverImage,
                MetadataField.BackgroundImage,
                MetadataField.Links,
                MetadataField.Publishers,
                MetadataField.Developers,
                MetadataField.Tags,
                MetadataField.Genres,
                MetadataField.ReleaseDate
            });

            var gameId     = "Game1";
            var icon       = new MetadataFile($"IGDBIconName{gameId}.file", randomFile);
            var image      = new MetadataFile($"IGDBImageName{gameId}.file", randomFile);
            var background = new MetadataFile($"IGDB backgournd {gameId}");

            testPlugin.ReturnMetadata = new GameMetadata(new GameInfo()
            {
                Name        = "IGDB Game " + gameId,
                Description = $"IGDB Description {gameId}",
                Developers  = new List <string>()
                {
                    $"IGDB Developer {gameId}"
                },
                Genres = new List <string>()
                {
                    $"IGDB Genre {gameId}"
                },
                Links = new List <Link>()
                {
                    new Link($"IGDB link {gameId}", $"IGDB link url {gameId}")
                },
                Publishers = new List <string>()
                {
                    $"IGDB publisher {gameId}"
                },
                ReleaseDate = new DateTime(2012, 6, 6),
                Tags        = new List <string>()
                {
                    $"IGDB Tag {gameId}"
                }
            }, icon, image, background);


            List <MetadataPlugin> metadataDownloaders = new List <MetadataPlugin>()
            {
                testPlugin
            };

            using (var temp = TempDirectory.Create())
            {
                var db = new GameDatabase(temp.TempPath);
                db.OpenDatabase();
                Game.DatabaseReference = db;
                var addedGame = db.ImportGame(new GameInfo()
                {
                    Name        = "Game1",
                    Description = "Description",
                    Developers  = new List <string>()
                    {
                        "Developers"
                    },
                    Genres = new List <string>()
                    {
                        "Genres"
                    },
                    Links = new List <Link>()
                    {
                        new Link("Link", "URL")
                    },
                    Publishers = new List <string>()
                    {
                        "Publishers"
                    },
                    ReleaseDate = new DateTime(2012, 6, 6),
                    Tags        = new List <string>()
                    {
                        "Tags"
                    },
                    UserScore      = 1,
                    CommunityScore = 2,
                    CriticScore    = 3
                });

                addedGame.Icon            = "Icon";
                addedGame.CoverImage      = "Image";
                addedGame.BackgroundImage = "BackgroundImage";

                var downloader = new MetadataDownloader(db, metadataDownloaders, new List <LibraryPlugin>());
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = true
                };

                // No download - all values are kept
                settings.ConfigureFields(new List <Guid> {
                    testPlugin.Id
                }, true);
                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, new PlayniteSettings(), null, null);

                var dbGames = db.Games.ToList();
                Assert.AreEqual(0, testPlugin.CallCount);

                var game1 = dbGames[0];
                Assert.AreEqual("Description", game1.Description);
                Assert.AreEqual("Developers", game1.Developers[0].Name);
                Assert.AreEqual("Genres", game1.Genres[0].Name);
                Assert.AreEqual("Link", game1.Links[0].Name);
                Assert.AreEqual("URL", game1.Links[0].Url);
                Assert.AreEqual("Publishers", game1.Publishers[0].Name);
                Assert.AreEqual("Tags", game1.Tags[0].Name);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game1.BackgroundImage);
                Assert.IsNotEmpty(game1.Icon);
                Assert.IsNotEmpty(game1.CoverImage);

                // Single download - values are changed even when present
                settings.SkipExistingValues = false;
                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, new PlayniteSettings(), null, null);

                dbGames = db.Games.ToList();
                Assert.AreEqual(1, testPlugin.CallCount);

                game1 = dbGames[0];
                Assert.AreEqual("IGDB Description Game1", game1.Description);
                Assert.AreEqual("IGDB Developer Game1", game1.Developers[0].Name);
                Assert.AreEqual("IGDB Genre Game1", game1.Genres[0].Name);
                Assert.AreEqual("IGDB link Game1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url Game1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher Game1", game1.Publishers[0].Name);
                Assert.AreEqual("IGDB Tag Game1", game1.Tags[0].Name);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game1.BackgroundImage);
                Assert.IsNotEmpty(game1.Icon);
                Assert.IsNotEmpty(game1.CoverImage);

                // Single download - values are changed when skip enabled and values are not present
                testPlugin.CallCount        = 0;
                settings.SkipExistingValues = true;
                db.Games.Remove(game1);
                db.Games.Add(new Game("Game1"));

                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, new PlayniteSettings(), null, null);

                dbGames = db.Games.ToList();
                Assert.AreEqual(1, testPlugin.CallCount);

                game1 = dbGames[0];
                Assert.AreEqual("IGDB Description Game1", game1.Description);
                Assert.AreEqual("IGDB Developer Game1", game1.Developers[0].Name);
                Assert.AreEqual("IGDB Genre Game1", game1.Genres[0].Name);
                Assert.AreEqual("IGDB link Game1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url Game1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher Game1", game1.Publishers[0].Name);
                Assert.AreEqual("IGDB Tag Game1", game1.Tags[0].Name);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game1.BackgroundImage);
                Assert.IsNotEmpty(game1.Icon);
                Assert.IsNotEmpty(game1.CoverImage);
            }
        }
        public async Task IGDBSourceTest()
        {
            var db = new GameDatabase(null);

            using (db.OpenDatabase(new MemoryStream()))
            {
                int callCount   = 0;
                var storeCalled = false;

                var games = new List <Game>()
                {
                    new Game("Game1"),
                    new Game("Game2")
                    {
                        PluginId = storePluginId, GameId = "Game2"
                    },
                    new Game("Game3")
                };

                db.AddGames(games);

                var igdbProvider = new Mock <ILibraryMetadataProvider>();
                igdbProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;

                    if (g.Name == "Game3")
                    {
                        return(GameMetadata.GetEmptyData());
                    }

                    var gameId = g.Name;
                    var game   = new Game("IGDB Game " + gameId)
                    {
                        Description = $"IGDB Description {gameId}",
                        Developers  = new ComparableList <string>()
                        {
                            $"IGDB Developer {gameId}"
                        },
                        Genres = new ComparableList <string>()
                        {
                            $"IGDB Genre {gameId}"
                        },
                        Links = new ObservableCollection <Link>()
                        {
                            new Link($"IGDB link {gameId}", $"IGDB link url {gameId}")
                        },
                        Publishers = new ComparableList <string>()
                        {
                            $"IGDB publisher {gameId}"
                        },
                        ReleaseDate = new DateTime(2012, 6, 6),
                        Tags        = new ComparableList <string>()
                        {
                            $"IGDB Tag {gameId}"
                        }
                    };
                    var icon  = new MetadataFile($"IGDBIconPath{gameId}.file", $"IGDBIconName{gameId}.file", randomFile);
                    var image = new MetadataFile($"IGDBImagePath{gameId}.file", $"IGDBImageName{gameId}.file", randomFile);
                    return(new GameMetadata(game, icon, image, $"IGDB backgournd {gameId}"));
                });

                var storeProvider = new Mock <ILibraryMetadataProvider>();
                storeProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    storeCalled = true;
                    return(GameMetadata.GetEmptyData());
                });

                var downloader = new MetadataDownloader(igdbProvider.Object, GetLibraryPlugins(storeProvider.Object, storePluginId));
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = false
                };
                settings.ConfigureFields(MetadataSource.IGDB, true);
                await downloader.DownloadMetadataAsync(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings, null, null);

                var dbGames = db.GamesCollection.FindAll().ToList();
                Assert.IsFalse(storeCalled);
                Assert.AreEqual(3, callCount);
                var game1 = dbGames[0];
                Assert.AreEqual("IGDB Description Game1", game1.Description);
                Assert.AreEqual("IGDB Developer Game1", game1.Developers[0]);
                Assert.AreEqual("IGDB Genre Game1", game1.Genres[0]);
                Assert.AreEqual("IGDB link Game1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url Game1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher Game1", game1.Publishers[0]);
                Assert.AreEqual("IGDB Tag Game1", game1.Tags[0]);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.AreEqual("IGDB backgournd Game1", game1.BackgroundImage);
                Assert.AreEqual($"IGDBIconPathGame1.file", game1.Icon);
                Assert.AreEqual($"IGDBImagePathGame1.file", game1.CoverImage);
                var game2 = dbGames[1];
                Assert.AreEqual("IGDB Description Game2", game2.Description);

                Assert.AreEqual(4, db.Database.FileStorage.FindAll().Count());
            }
        }
        public async Task SkipExistingTest()
        {
            var db = new GameDatabase(null);

            using (db.OpenDatabase(new MemoryStream()))
            {
                int callCount = 0;
                db.AddGame(new Game("Game1")
                {
                    Description = "Description",
                    Developers  = new ComparableList <string>()
                    {
                        "Developers"
                    },
                    Genres = new ComparableList <string>()
                    {
                        "Genres"
                    },
                    Links = new ObservableCollection <Link>()
                    {
                        new Link("Link", "URL")
                    },
                    Publishers = new ComparableList <string>()
                    {
                        "Publishers"
                    },
                    ReleaseDate = new DateTime(2012, 6, 6),
                    Tags        = new ComparableList <string>()
                    {
                        "Tags"
                    },
                    Icon            = "Icon",
                    CoverImage      = "Image",
                    BackgroundImage = "BackgroundImage",
                    UserScore       = 1,
                    CommunityScore  = 2,
                    CriticScore     = 3
                });


                var igdbProvider = new Mock <ILibraryMetadataProvider>();
                igdbProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    var gameId = g.Name;
                    var game   = new Game("IGDB Game " + gameId)
                    {
                        Description = $"IGDB Description {gameId}",
                        Developers  = new ComparableList <string>()
                        {
                            $"IGDB Developer {gameId}"
                        },
                        Genres = new ComparableList <string>()
                        {
                            $"IGDB Genre {gameId}"
                        },
                        Links = new ObservableCollection <Link>()
                        {
                            new Link($"IGDB link {gameId}", $"IGDB link url {gameId}")
                        },
                        Publishers = new ComparableList <string>()
                        {
                            $"IGDB publisher {gameId}"
                        },
                        ReleaseDate = new DateTime(2012, 6, 6),
                        Tags        = new ComparableList <string>()
                        {
                            $"IGDB Tag {gameId}"
                        }
                    };
                    var icon  = new MetadataFile($"IGDBIconPath{gameId}.file", $"IGDBIconName{gameId}.file", randomFile);
                    var image = new MetadataFile($"IGDBImagePath{gameId}.file", $"IGDBImageName{gameId}.file", randomFile);
                    return(new GameMetadata(game, icon, image, $"IGDB backgournd {gameId}"));
                });

                var downloader = new MetadataDownloader(igdbProvider.Object, null);
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = true
                };

                // No download - all values are kept
                settings.ConfigureFields(MetadataSource.IGDB, true);
                await downloader.DownloadMetadataAsync(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings, null, null);

                var dbGames = db.GamesCollection.FindAll().ToList();
                Assert.AreEqual(0, callCount);

                var game1 = dbGames[0];
                Assert.AreEqual("Description", game1.Description);
                Assert.AreEqual("Developers", game1.Developers[0]);
                Assert.AreEqual("Genres", game1.Genres[0]);
                Assert.AreEqual("Link", game1.Links[0].Name);
                Assert.AreEqual("URL", game1.Links[0].Url);
                Assert.AreEqual("Publishers", game1.Publishers[0]);
                Assert.AreEqual("Tags", game1.Tags[0]);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.AreEqual("BackgroundImage", game1.BackgroundImage);
                Assert.AreEqual("Icon", game1.Icon);
                Assert.AreEqual("Image", game1.CoverImage);

                // Single download - values are changed even when present
                settings.SkipExistingValues = false;
                await downloader.DownloadMetadataAsync(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings, null, null);

                dbGames = db.GamesCollection.FindAll().ToList();
                Assert.AreEqual(1, callCount);

                game1 = dbGames[0];
                Assert.AreEqual("IGDB Description Game1", game1.Description);
                Assert.AreEqual("IGDB Developer Game1", game1.Developers[0]);
                Assert.AreEqual("IGDB Genre Game1", game1.Genres[0]);
                Assert.AreEqual("IGDB link Game1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url Game1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher Game1", game1.Publishers[0]);
                Assert.AreEqual("IGDB Tag Game1", game1.Tags[0]);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.AreEqual("IGDB backgournd Game1", game1.BackgroundImage);
                Assert.AreEqual($"IGDBIconPathGame1.file", game1.Icon);
                Assert.AreEqual($"IGDBImagePathGame1.file", game1.CoverImage);

                // Single download - values are changed when skip enabled and values are not present
                callCount = 0;
                settings.SkipExistingValues = true;
                db.DeleteGame(game1);
                db.AddGame(new Game("Game1"));

                await downloader.DownloadMetadataAsync(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings, null, null);

                dbGames = db.GamesCollection.FindAll().ToList();
                Assert.AreEqual(1, callCount);

                game1 = dbGames[0];
                Assert.AreEqual("IGDB Description Game1", game1.Description);
                Assert.AreEqual("IGDB Developer Game1", game1.Developers[0]);
                Assert.AreEqual("IGDB Genre Game1", game1.Genres[0]);
                Assert.AreEqual("IGDB link Game1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url Game1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher Game1", game1.Publishers[0]);
                Assert.AreEqual("IGDB Tag Game1", game1.Tags[0]);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.AreEqual("IGDB backgournd Game1", game1.BackgroundImage);
                Assert.AreEqual($"IGDBIconPathGame1.file", game1.Icon);
                Assert.AreEqual($"IGDBImagePathGame1.file", game1.CoverImage);
            }
        }
Ejemplo n.º 6
0
        public async Task IGDBSourceTest()
        {
            using (var temp = TempDirectory.Create())
            {
                var db = new GameDatabase(temp.TempPath);
                db.OpenDatabase();
                Game.DatabaseReference = db;
                int callCount   = 0;
                var storeCalled = false;

                var games = new List <Game>()
                {
                    new Game("Game1"),
                    new Game("Game2")
                    {
                        PluginId = storePluginId, GameId = "Game2"
                    },
                    new Game("Game3")
                };

                db.Games.Add(games);

                var igdbProvider = new Mock <LibraryMetadataProvider>();
                igdbProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;

                    if (g.Name == "Game3")
                    {
                        return(GameMetadata.GetEmptyData());
                    }

                    var gameId = g.Name;
                    var game   = new GameInfo()
                    {
                        Name        = "IGDB Game " + gameId,
                        Description = $"IGDB Description {gameId}",
                        Developers  = new List <string>()
                        {
                            $"IGDB Developer {gameId}"
                        },
                        Genres = new List <string>()
                        {
                            $"IGDB Genre {gameId}"
                        },
                        Links = new List <Link>()
                        {
                            new Link($"IGDB link {gameId}", $"IGDB link url {gameId}")
                        },
                        Publishers = new List <string>()
                        {
                            $"IGDB publisher {gameId}"
                        },
                        ReleaseDate = new DateTime(2012, 6, 6),
                        Tags        = new List <string>()
                        {
                            $"IGDB Tag {gameId}"
                        }
                    };
                    var icon       = new MetadataFile($"IGDBIconName{gameId}.file", randomFile);
                    var image      = new MetadataFile($"IGDBImageName{gameId}.file", randomFile);
                    var background = new MetadataFile($"IGDB backgournd {gameId}");
                    return(new GameMetadata(game, icon, image, background));
                });

                var storeProvider = new Mock <LibraryMetadataProvider>();
                storeProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    storeCalled = true;
                    return(GameMetadata.GetEmptyData());
                });

                var downloader = new MetadataDownloader(db, igdbProvider.Object, GetLibraryPlugins(storeProvider.Object, storePluginId));
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = false
                };
                settings.Name.Import = false;
                settings.ConfigureFields(MetadataSource.IGDB, true);
                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, null, null);


                Assert.IsFalse(storeCalled);
                Assert.AreEqual(3, callCount);
                var game1 = db.Games[games[0].Id];
                Assert.AreEqual("IGDB Description Game1", game1.Description);
                Assert.AreEqual("IGDB Developer Game1", game1.Developers[0].Name);
                Assert.AreEqual("IGDB Genre Game1", game1.Genres[0].Name);
                Assert.AreEqual("IGDB link Game1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url Game1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher Game1", game1.Publishers[0].Name);
                Assert.AreEqual("IGDB Tag Game1", game1.Tags[0].Name);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game1.BackgroundImage);
                Assert.IsNotEmpty(game1.Icon);
                Assert.IsNotEmpty(game1.CoverImage);
                var game2 = db.Games[games[1].Id];
                Assert.AreEqual("IGDB Description Game2", game2.Description);

                Assert.AreEqual(2, Directory.GetFiles(db.GetFileStoragePath(game1.Id)).Count());
                Assert.AreEqual(2, Directory.GetFiles(db.GetFileStoragePath(game2.Id)).Count());
            }
        }
        public async Task MissingDataTest()
        {
            var db = new GameDatabase(null);

            using (db.OpenDatabase(new MemoryStream()))
            {
                int callCount = 0;
                var games     = new List <Game>()
                {
                    new Game("Game")
                    {
                        PluginId = storePluginId,
                        GameId   = "storeId",
                        Genres   = new ComparableList <string>()
                        {
                            "Genre"
                        },
                        ReleaseDate = new DateTime(2012, 6, 6),
                        Developers  = new ComparableList <string>()
                        {
                            "Developer"
                        },
                        Publishers = new ComparableList <string>()
                        {
                            "Publisher"
                        },
                        Tags = new ComparableList <string>()
                        {
                            "Tag"
                        },
                        Description = "Description",
                        Links       = new ObservableCollection <Link>()
                        {
                            new Link()
                        },
                        Icon            = "icon",
                        CoverImage      = "image",
                        BackgroundImage = "backImage"
                    }
                };

                db.AddGames(games);

                var igdbProvider = new Mock <ILibraryMetadataProvider>();
                igdbProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    return(GameMetadata.GetEmptyData());
                });

                var storeProvider = new Mock <ILibraryMetadataProvider>();
                storeProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    var gameId = g.GameId;
                    return(new GameMetadata(new Game("Store Game " + gameId), null, null, null));
                });

                var downloader = new MetadataDownloader(igdbProvider.Object, GetLibraryPlugins(storeProvider.Object, storePluginId));
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = false
                };

                var dbGames = db.GamesCollection.FindAll().ToList();
                var f       = dbGames[0].ReleaseDate;
                var s       = games[0].ReleaseDate;

                settings.ConfigureFields(MetadataSource.Store, true);
                await downloader.DownloadMetadataAsync(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings, null, null);

                dbGames = db.GamesCollection.FindAll().ToList();
                Assert.AreEqual(1, callCount);
                var game = dbGames[0];
                Assert.AreEqual("Description", game.Description);
                Assert.AreEqual("icon", game.Icon);
                Assert.AreEqual("image", game.CoverImage);
                Assert.AreEqual("backImage", game.BackgroundImage);
                Assert.AreEqual("Developer", game.Developers[0]);
                Assert.AreEqual("Publisher", game.Publishers[0]);
                Assert.AreEqual("Genre", game.Genres[0]);
                CollectionAssert.IsNotEmpty(game.Links);
                Assert.AreEqual("Tag", game.Tags[0]);
                Assert.AreEqual(2012, game.ReleaseDate.Value.Year);
            }
        }
Ejemplo n.º 8
0
        public Task DownloadMetadataAsync(
            List <Game> games,
            MetadataDownloaderSettings settings,
            Action <Game, int, int> processCallback,
            CancellationTokenSource cancelToken)
        {
            return(Task.Run(() =>
            {
                if (games == null || games.Count == 0)
                {
                    return;
                }

                for (int i = 0; i < games.Count; i++)
                {
                    Game game = null;

                    try
                    {
                        if (cancelToken?.IsCancellationRequested == true)
                        {
                            return;
                        }

                        GameMetadata storeData = null;
                        GameMetadata igdbData = null;
                        GameMetadata gameData = null;

                        // We need to get new instance from DB in case game got edited or deleted.
                        // We don't want to block game editing while metadata is downloading for other games.
                        game = database.Games[games[i].Id]?.GetClone();
                        if (game == null)
                        {
                            logger.Warn($"Game {game.GameId} no longer in DB, skipping metadata download.");
                            processCallback?.Invoke(null, i, games.Count);
                            continue;
                        }

                        logger.Debug($"Downloading metadata for {game.Name}, {game.GameId}, {game.PluginId}");

                        // Name
                        if (!game.IsCustomGame && settings.Name.Import)
                        {
                            gameData = ProcessField(game, settings.Name, ref storeData, ref igdbData, (a) => a.GameInfo?.Name);
                            if (!string.IsNullOrEmpty(gameData?.GameInfo?.Name))
                            {
                                game.Name = StringExtensions.RemoveTrademarks(gameData.GameInfo.Name);
                                var sortingName = StringExtensions.ConvertToSortableName(game.Name);
                                if (sortingName != game.Name)
                                {
                                    game.SortingName = sortingName;
                                }
                            }
                        }

                        // Genre
                        if (settings.Genre.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.GenreIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Genre, ref storeData, ref igdbData, (a) => a.GameInfo?.Genres);
                                if (gameData?.GameInfo?.Genres.HasNonEmptyItems() == true)
                                {
                                    game.GenreIds = database.Genres.Add(gameData.GameInfo.Genres).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Release Date
                        if (settings.ReleaseDate.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && game.ReleaseDate == null))
                            {
                                gameData = ProcessField(game, settings.ReleaseDate, ref storeData, ref igdbData, (a) => a.GameInfo?.ReleaseDate);
                                game.ReleaseDate = gameData?.GameInfo?.ReleaseDate ?? game.ReleaseDate;
                            }
                        }

                        // Developer
                        if (settings.Developer.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.DeveloperIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Developer, ref storeData, ref igdbData, (a) => a.GameInfo?.Developers);
                                if (gameData?.GameInfo?.Developers.HasNonEmptyItems() == true)
                                {
                                    game.DeveloperIds = database.Companies.Add(gameData.GameInfo.Developers).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Publisher
                        if (settings.Publisher.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.PublisherIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Publisher, ref storeData, ref igdbData, (a) => a.GameInfo?.Publishers);
                                if (gameData?.GameInfo?.Publishers.HasNonEmptyItems() == true)
                                {
                                    game.PublisherIds = database.Companies.Add(gameData.GameInfo.Publishers).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Tags / Features
                        if (settings.Tag.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.TagIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Tag, ref storeData, ref igdbData, (a) => a.GameInfo?.Tags);
                                if (gameData?.GameInfo?.Tags.HasNonEmptyItems() == true)
                                {
                                    game.TagIds = database.Tags.Add(gameData.GameInfo.Tags).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Description
                        if (settings.Description.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.Description)))
                            {
                                gameData = ProcessField(game, settings.Description, ref storeData, ref igdbData, (a) => a.GameInfo?.Description);
                                game.Description = string.IsNullOrEmpty(gameData?.GameInfo?.Description) == true ? game.Description : gameData.GameInfo.Description;
                            }
                        }

                        // Links
                        if (settings.Links.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.Links.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Links, ref storeData, ref igdbData, (a) => a.GameInfo?.Links);
                                if (gameData?.GameInfo?.Links.HasItems() == true)
                                {
                                    game.Links = gameData.GameInfo.Links.ToObservable();
                                }
                            }
                        }

                        // Critic Score
                        if (settings.CriticScore.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && game.CriticScore == null))
                            {
                                gameData = ProcessField(game, settings.CriticScore, ref storeData, ref igdbData, (a) => a.GameInfo?.CriticScore);
                                game.CriticScore = gameData?.GameInfo?.CriticScore == null ? game.CriticScore : gameData.GameInfo.CriticScore;
                            }
                        }

                        // Community Score
                        if (settings.CommunityScore.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && game.CommunityScore == null))
                            {
                                gameData = ProcessField(game, settings.CommunityScore, ref storeData, ref igdbData, (a) => a.GameInfo?.CommunityScore);
                                game.CommunityScore = gameData?.GameInfo?.CommunityScore == null ? game.CommunityScore : gameData.GameInfo.CommunityScore;
                            }
                        }

                        // BackgroundImage
                        if (settings.BackgroundImage.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.BackgroundImage)))
                            {
                                gameData = ProcessField(game, settings.BackgroundImage, ref storeData, ref igdbData, (a) => a.BackgroundImage);
                                if (gameData?.BackgroundImage != null)
                                {
                                    if (gameData.BackgroundImage.HasContent)
                                    {
                                        game.BackgroundImage = database.AddFile(gameData.BackgroundImage, game.Id);
                                    }
                                    else
                                    {
                                        game.BackgroundImage = gameData.BackgroundImage.OriginalUrl;
                                    }
                                }
                            }
                        }

                        // Cover
                        if (settings.CoverImage.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.CoverImage)))
                            {
                                gameData = ProcessField(game, settings.CoverImage, ref storeData, ref igdbData, (a) => a.CoverImage);
                                if (gameData?.CoverImage != null)
                                {
                                    game.CoverImage = database.AddFile(gameData.CoverImage, game.Id);
                                }
                            }
                        }

                        // Icon
                        if (settings.Icon.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.Icon)))
                            {
                                gameData = ProcessField(game, settings.Icon, ref storeData, ref igdbData, (a) => a.Icon);
                                if (gameData?.Icon != null)
                                {
                                    game.Icon = database.AddFile(gameData.Icon, game.Id);
                                }
                            }
                        }

                        // Only update them if they don't exist yet
                        if (game.OtherActions?.Any() != true && storeData != null)
                        {
                            if (storeData?.GameInfo?.OtherActions?.Any() == true)
                            {
                                game.OtherActions = new System.Collections.ObjectModel.ObservableCollection <GameAction>();
                                foreach (var task in storeData.GameInfo.OtherActions)
                                {
                                    game.OtherActions.Add(task);
                                }
                            }
                        }

                        // Just to be sure check if somebody didn't remove game while downloading data
                        if (database.Games.FirstOrDefault(a => a.GameId == games[i].GameId) != null)
                        {
                            database.Games.Update(game);
                        }
                        else
                        {
                            logger.Warn($"Game {game.GameId} no longer in DB, skipping metadata update in DB.");
                        }
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(e, $"Failed to download metadata for game {game?.Name}, {game?.GameId}");
                    }
                    finally
                    {
                        if (game != null)
                        {
                            processCallback?.Invoke(game, i, games.Count);
                        }
                    }
                }
            }));
        }
Ejemplo n.º 9
0
        public Task DownloadMetadataAsync(
            List <Game> games,
            MetadataDownloaderSettings settings,
            PlayniteSettings playniteSettings,
            Action <Game, int, int> progressCallback,
            CancellationToken cancelToken)
        {
            return(Task.Run(() =>
            {
                if (games == null || games.Count == 0)
                {
                    return;
                }

                for (int i = 0; i < games.Count; i++)
                {
                    Game game = null;
                    var existingStoreData = new Dictionary <Guid, GameMetadata>();
                    var existingPluginData = new Dictionary <Guid, OnDemandMetadataProvider>();

                    try
                    {
                        if (cancelToken.IsCancellationRequested == true)
                        {
                            return;
                        }

                        GameMetadata gameData = null;

                        // We need to get new instance from DB in case game got edited or deleted.
                        // We don't want to block game editing while metadata is downloading for other games.
                        game = database.Games[games[i].Id]?.GetClone();
                        if (game == null)
                        {
                            logger.Warn($"Game {game.Id} no longer in DB, skipping metadata download.");
                            progressCallback?.Invoke(null, i, games.Count);
                            continue;
                        }

                        var dataModified = false;
                        game.PropertyChanged += (_, __) => dataModified = true;

                        if (game != null)
                        {
                            progressCallback?.Invoke(game, i, games.Count);
                        }

                        logger.Debug($"Downloading metadata for {game.Name}, {game.GameId}, {game.PluginId}");

                        // Name
                        if (!game.IsCustomGame && settings.Name.Import)
                        {
                            gameData = ProcessField(game, settings.Name, MetadataField.Name, (a) => a.Name, existingStoreData, existingPluginData, cancelToken);
                            if (!string.IsNullOrEmpty(gameData?.Name))
                            {
                                game.Name = StringExtensions.RemoveTrademarks(gameData.Name);
                                var sortingName = StringExtensions.ConvertToSortableName(game.Name);
                                if (sortingName != game.Name)
                                {
                                    game.SortingName = sortingName;
                                }
                            }
                        }

                        // Genre
                        if (settings.Genre.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.GenreIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Genre, MetadataField.Genres, (a) => a.Genres, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.Genres.HasItems() == true)
                                {
                                    game.GenreIds = database.Genres.Add(gameData.Genres).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Release Date
                        if (settings.ReleaseDate.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && game.ReleaseDate == null))
                            {
                                gameData = ProcessField(game, settings.ReleaseDate, MetadataField.ReleaseDate, (a) => a.ReleaseDate, existingStoreData, existingPluginData, cancelToken);
                                game.ReleaseDate = gameData?.ReleaseDate ?? game.ReleaseDate;
                            }
                        }

                        // Developer
                        if (settings.Developer.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.DeveloperIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Developer, MetadataField.Developers, (a) => a.Developers, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.Developers.HasItems() == true)
                                {
                                    game.DeveloperIds = database.Companies.Add(gameData.Developers).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Publisher
                        if (settings.Publisher.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.PublisherIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Publisher, MetadataField.Publishers, (a) => a.Publishers, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.Publishers.HasItems() == true)
                                {
                                    game.PublisherIds = database.Companies.Add(gameData.Publishers).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Tags
                        if (settings.Tag.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.TagIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Tag, MetadataField.Tags, (a) => a.Tags, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.Tags.HasItems() == true)
                                {
                                    game.TagIds = database.Tags.Add(gameData.Tags).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Features
                        if (settings.Feature.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.FeatureIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Feature, MetadataField.Features, (a) => a.Features, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.Features.HasItems() == true)
                                {
                                    game.FeatureIds = database.Features.Add(gameData.Features).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Description
                        if (settings.Description.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.Description)))
                            {
                                gameData = ProcessField(game, settings.Description, MetadataField.Description, (a) => a.Description, existingStoreData, existingPluginData, cancelToken);
                                game.Description = string.IsNullOrEmpty(gameData?.Description) == true ? game.Description : gameData.Description;
                            }
                        }

                        // Links
                        if (settings.Links.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.Links.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Links, MetadataField.Links, (a) => a.Links, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.Links.HasItems() == true)
                                {
                                    game.Links = gameData.Links.ToObservable();
                                }
                            }
                        }

                        // Age rating
                        if (settings.AgeRating.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.AgeRatingIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.AgeRating, MetadataField.AgeRating, (a) => a.AgeRatings, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.AgeRatings.HasItems() == true)
                                {
                                    game.AgeRatingIds = database.AgeRatings.Add(gameData.AgeRatings).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Region
                        if (settings.Region.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.RegionIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Region, MetadataField.Region, (a) => a.Regions, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.Regions.HasItems() == true)
                                {
                                    game.RegionIds = database.Regions.Add(gameData.Regions).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Series
                        if (settings.Series.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.SeriesIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Series, MetadataField.Series, (a) => a.Series, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.Series.HasItems() == true)
                                {
                                    game.SeriesIds = database.Series.Add(gameData.Series).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Platform
                        if (settings.Platform.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && !game.PlatformIds.HasItems()))
                            {
                                gameData = ProcessField(game, settings.Platform, MetadataField.Platform, (a) => a.Platforms, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.Platforms.HasItems() == true)
                                {
                                    game.PlatformIds = database.Platforms.Add(gameData.Platforms).Select(a => a.Id).ToList();
                                }
                            }
                        }

                        // Critic Score
                        if (settings.CriticScore.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && game.CriticScore == null))
                            {
                                gameData = ProcessField(game, settings.CriticScore, MetadataField.CriticScore, (a) => a.CriticScore, existingStoreData, existingPluginData, cancelToken);
                                game.CriticScore = gameData?.CriticScore == null ? game.CriticScore : gameData.CriticScore;
                            }
                        }

                        // Community Score
                        if (settings.CommunityScore.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && game.CommunityScore == null))
                            {
                                gameData = ProcessField(game, settings.CommunityScore, MetadataField.CommunityScore, (a) => a.CommunityScore, existingStoreData, existingPluginData, cancelToken);
                                game.CommunityScore = gameData?.CommunityScore == null ? game.CommunityScore : gameData.CommunityScore;
                            }
                        }

                        // BackgroundImage
                        if (settings.BackgroundImage.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.BackgroundImage)))
                            {
                                gameData = ProcessField(game, settings.BackgroundImage, MetadataField.BackgroundImage, (a) => a.BackgroundImage, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.BackgroundImage != null)
                                {
                                    if (playniteSettings.DownloadBackgroundsImmediately && gameData.BackgroundImage.HasImageData)
                                    {
                                        game.BackgroundImage = database.AddFile(gameData.BackgroundImage, game.Id, true);
                                    }
                                    else if (!playniteSettings.DownloadBackgroundsImmediately &&
                                             !gameData.BackgroundImage.Path.IsNullOrEmpty())
                                    {
                                        game.BackgroundImage = gameData.BackgroundImage.Path;
                                    }
                                    else if (gameData.BackgroundImage.HasImageData)
                                    {
                                        game.BackgroundImage = database.AddFile(gameData.BackgroundImage, game.Id, true);
                                    }
                                }
                            }
                        }

                        // Cover
                        if (settings.CoverImage.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.CoverImage)))
                            {
                                gameData = ProcessField(game, settings.CoverImage, MetadataField.CoverImage, (a) => a.CoverImage, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.CoverImage != null)
                                {
                                    game.CoverImage = database.AddFile(gameData.CoverImage, game.Id, true);
                                }
                            }
                        }

                        // Icon
                        if (settings.Icon.Import)
                        {
                            if (!settings.SkipExistingValues || (settings.SkipExistingValues && string.IsNullOrEmpty(game.Icon)))
                            {
                                gameData = ProcessField(game, settings.Icon, MetadataField.Icon, (a) => a.Icon, existingStoreData, existingPluginData, cancelToken);
                                if (gameData?.Icon != null)
                                {
                                    game.Icon = database.AddFile(gameData.Icon, game.Id, true);
                                }
                            }
                        }

                        // Just to be sure check if somebody didn't remove game while downloading data
                        if (database.Games.FirstOrDefault(a => a.Id == games[i].Id) != null && dataModified)
                        {
                            game.Modified = DateTime.Now;
                            database.Games.Update(game);
                        }
                        else
                        {
                            logger.Warn($"Game {game.Id} no longer in DB, skipping metadata update in DB.");
                        }
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(e, $"Failed to download metadata for game {game?.Name}, {game?.Id}");
                    }
                    finally
                    {
                        foreach (var plugin in existingPluginData.Values)
                        {
                            plugin.Dispose();
                        }
                    }
                }
            }));
        }
        public async Task IGDBStoreCombinedTest()
        {
            using (var temp = TempDirectory.Create())
            {
                var db = new GameDatabase(temp.TempPath);
                db.OpenDatabase();
                int callCount = 0;
                var games     = new List <Game>()
                {
                    new Game("Game1"),
                    new Game("Game2")
                    {
                        PluginId = storePluginId, GameId = "Game2"
                    },
                    new Game("Game3")
                };

                db.Games.Add(games);

                var igdbProvider = new Mock <ILibraryMetadataProvider>();
                igdbProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    var gameId       = g.Name;
                    var game         = new Game("IGDB Game " + gameId);
                    game.Description = $"IGDB Description {gameId}";
                    game.Genres      = new ComparableList <string>()
                    {
                        $"IGDB Genre {gameId}"
                    };
                    game.Links = new ObservableCollection <Link>()
                    {
                        new Link($"IGDB link {gameId}", $"IGDB link url {gameId}")
                    };
                    game.Publishers = new ComparableList <string>()
                    {
                        $"IGDB publisher {gameId}"
                    };
                    game.ReleaseDate = new DateTime(2012, 6, 6);
                    var icon         = new MetadataFile($"IGDBIconName{gameId}.file", randomFile);
                    var image        = new MetadataFile($"IGDBImageName{gameId}.file", randomFile);
                    return(new GameMetadata(game, icon, image, $"IGDB backgournd {gameId}"));
                });

                var storeProvider = new Mock <ILibraryMetadataProvider>();
                storeProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    var gameId       = g.GameId;
                    var game         = new Game(gameId);
                    game.Description = $"Store Description {gameId}";
                    game.Developers  = new ComparableList <string>()
                    {
                        $"Store Developer {gameId}"
                    };
                    game.Links = new ObservableCollection <Link>()
                    {
                        new Link($"Store link {gameId}", $"Store link url {gameId}")
                    };
                    game.Publishers = new ComparableList <string>()
                    {
                        $"Store publisher {gameId}"
                    };
                    game.ReleaseDate = new DateTime(2016, 2, 2);
                    var icon         = new MetadataFile($"StoreIconName{gameId}.file", randomFile);
                    var image        = new MetadataFile($"StoreImageName{gameId}.file", randomFile);
                    return(new GameMetadata(game, icon, image, $"Store backgournd {gameId}"));
                });

                var downloader = new MetadataDownloader(db, igdbProvider.Object, GetLibraryPlugins(storeProvider.Object, storePluginId));
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = false
                };

                // IGDB over Store
                settings.ConfigureFields(MetadataSource.IGDBOverStore, true);
                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, null, null);

                Assert.AreEqual(4, callCount);

                var game1 = db.Games[games[0].Id];
                Assert.AreEqual("IGDB Description Game1", game1.Description);
                Assert.IsNull(game1.Developers);
                Assert.AreEqual("IGDB Genre Game1", game1.Genres[0]);
                Assert.AreEqual("IGDB link Game1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url Game1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher Game1", game1.Publishers[0]);
                Assert.IsNull(game1.Tags);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game1.BackgroundImage);
                Assert.IsNotEmpty(game1.Icon);
                Assert.IsNotEmpty(game1.CoverImage);

                var game2 = db.Games[games[1].Id];
                Assert.AreEqual("IGDB Description Game2", game2.Description);
                Assert.AreEqual("Store Developer Game2", game2.Developers[0]);
                Assert.AreEqual("IGDB Genre Game2", game2.Genres[0]);
                Assert.AreEqual("IGDB link Game2", game2.Links[0].Name);
                Assert.AreEqual("IGDB link url Game2", game2.Links[0].Url);
                Assert.AreEqual("IGDB publisher Game2", game2.Publishers[0]);
                Assert.IsNull(game2.Tags);
                Assert.AreEqual(2012, game2.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game2.BackgroundImage);
                Assert.IsNotEmpty(game2.Icon);
                Assert.IsNotEmpty(game2.CoverImage);

                // Store over IGDB
                callCount = 0;
                settings.ConfigureFields(MetadataSource.StoreOverIGDB, true);
                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, null, null);

                Assert.AreEqual(4, callCount);

                game1 = db.Games[games[0].Id];
                Assert.AreEqual("IGDB Description Game1", game1.Description);
                Assert.IsNull(game1.Developers);
                Assert.AreEqual("IGDB Genre Game1", game1.Genres[0]);
                Assert.AreEqual("IGDB link Game1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url Game1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher Game1", game1.Publishers[0]);
                Assert.IsNull(game1.Tags);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game1.BackgroundImage);
                Assert.IsNotEmpty(game1.Icon);
                Assert.IsNotEmpty(game1.CoverImage);

                game2 = db.Games[games[1].Id];
                Assert.AreEqual("Store Description Game2", game2.Description);
                Assert.AreEqual("Store Developer Game2", game2.Developers[0]);
                Assert.AreEqual("IGDB Genre Game2", game2.Genres[0]);
                Assert.AreEqual("Store link Game2", game2.Links[0].Name);
                Assert.AreEqual("Store link url Game2", game2.Links[0].Url);
                Assert.AreEqual("Store publisher Game2", game2.Publishers[0]);
                Assert.IsNull(game2.Tags);
                Assert.AreEqual(2016, game2.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game2.BackgroundImage);
                Assert.IsNotEmpty(game2.Icon);
                Assert.IsNotEmpty(game2.CoverImage);
            }
        }
Ejemplo n.º 11
0
        public async Task SkipExistingTest()
        {
            var path = Path.Combine(Playnite.PlayniteTests.TempPath, "metadownload.db");
            FileSystem.DeleteFile(path);

            var db = new GameDatabase(null);
            using (db.OpenDatabase(path))
            {
                db.AddGame(new Game("Game1")
                {
                    Description = "Description",
                    Developers = new ComparableList<string>() { "Developers" },
                    Genres = new ComparableList<string>() { "Genres" },
                    Links = new ObservableCollection<Link>() { new Link("Link", "URL") },
                    Publishers = new ComparableList<string>() { "Publishers" },
                    ReleaseDate = new DateTime(2012, 6, 6),
                    Tags = new ComparableList<string>() { "Tags" },
                    Icon = "Icon",
                    Image = "Image",
                    BackgroundImage = "BackgroundImage",
                    UserScore = 1,
                    CommunityScore = 2,
                    CriticScore = 3
                });

                var igdbProvider = new MockMetadataProvider
                {
                    GetSupportsIdSearchHandler = () => false,
                    GetGameDataHandler = gameId =>
                    {
                        var game = new Game("IGDB Game " + gameId)
                        {
                            Description = $"IGDB Description {gameId}",
                            Developers = new ComparableList<string>() { $"IGDB Developer {gameId}" },
                            Genres = new ComparableList<string>() { $"IGDB Genre {gameId}" },
                            Links = new ObservableCollection<Link>() { new Link($"IGDB link {gameId}", $"IGDB link url {gameId}") },
                            Publishers = new ComparableList<string>() { $"IGDB publisher {gameId}" },
                            ReleaseDate = new DateTime(2012, 6, 6),
                            Tags = new ComparableList<string>() { $"IGDB Tag {gameId}" }
                        };
                        var icon = new FileDefinition($"IGDBIconPath{gameId}.file", $"IGDBIconName{gameId}.file", randomFile);
                        var image = new FileDefinition($"IGDBImagePath{gameId}.file", $"IGDBImageName{gameId}.file", randomFile);
                        return new GameMetadata(game, icon, image, $"IGDB backgournd {gameId}");
                    },
                    SearchGamesHandler = gameName =>
                    {
                        return new List<MetadataSearchResult>()
                        {
                            new MetadataSearchResult("igdbid1", "Game1", DateTime.Now)
                        };
                    }
                };

                var downloader = new MockMetadataDownloader(null, null, null, null, igdbProvider);
                var settings = new MetadataDownloaderSettings() { SkipExistingValues = true };
                
                // No download - all values are kept
                settings.ConfigureFields(MetadataSource.IGDB, true);
                await downloader.DownloadMetadata(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings);

                var dbGames = db.GamesCollection.FindAll().ToList();
                Assert.AreEqual(0, downloader.CallCount);

                var game1 = dbGames[0];
                Assert.AreEqual("Description", game1.Description);
                Assert.AreEqual("Developers", game1.Developers[0]);
                Assert.AreEqual("Genres", game1.Genres[0]);
                Assert.AreEqual("Link", game1.Links[0].Name);
                Assert.AreEqual("URL", game1.Links[0].Url);
                Assert.AreEqual("Publishers", game1.Publishers[0]);
                Assert.AreEqual("Tags", game1.Tags[0]);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.AreEqual("BackgroundImage", game1.BackgroundImage);
                Assert.AreEqual("Icon", game1.Icon);
                Assert.AreEqual("Image", game1.Image);

                // Single download - values are changed even when present
                settings.SkipExistingValues = false;
                await downloader.DownloadMetadata(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings);

                dbGames = db.GamesCollection.FindAll().ToList();
                Assert.AreEqual(1, downloader.CallCount);

                game1 = dbGames[0];
                Assert.AreEqual("IGDB Description igdbid1", game1.Description);
                Assert.AreEqual("IGDB Developer igdbid1", game1.Developers[0]);
                Assert.AreEqual("IGDB Genre igdbid1", game1.Genres[0]);
                Assert.AreEqual("IGDB link igdbid1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url igdbid1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher igdbid1", game1.Publishers[0]);
                Assert.AreEqual("IGDB Tag igdbid1", game1.Tags[0]);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.AreEqual("IGDB backgournd igdbid1", game1.BackgroundImage);
                Assert.AreEqual($"IGDBIconPathigdbid1.file", game1.Icon);
                Assert.AreEqual($"IGDBImagePathigdbid1.file", game1.Image);

                // Single download - values are changed when skip enabled and values are not present
                downloader.CallCount = 0;
                settings.SkipExistingValues = true;
                db.DeleteGame(game1);
                db.AddGame(new Game("Game1"));

                await downloader.DownloadMetadata(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings);

                dbGames = db.GamesCollection.FindAll().ToList();
                Assert.AreEqual(1, downloader.CallCount);

                game1 = dbGames[0];
                Assert.AreEqual("IGDB Description igdbid1", game1.Description);
                Assert.AreEqual("IGDB Developer igdbid1", game1.Developers[0]);
                Assert.AreEqual("IGDB Genre igdbid1", game1.Genres[0]);
                Assert.AreEqual("IGDB link igdbid1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url igdbid1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher igdbid1", game1.Publishers[0]);
                Assert.AreEqual("IGDB Tag igdbid1", game1.Tags[0]);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.AreEqual("IGDB backgournd igdbid1", game1.BackgroundImage);
                Assert.AreEqual($"IGDBIconPathigdbid1.file", game1.Icon);
                Assert.AreEqual($"IGDBImagePathigdbid1.file", game1.Image);
            }
        }
        public async Task StoreSourceTest()
        {
            using (var temp = TempDirectory.Create())
            {
                var db = new GameDatabase(temp.TempPath);
                db.OpenDatabase();
                int callCount  = 0;
                var igdbCalled = false;

                var games = new List <Game>()
                {
                    new Game("Game1"),
                    new Game("Game2")
                    {
                        PluginId = storePluginId, GameId = "storeId"
                    },
                    new Game("Game3")
                };

                db.Games.Add(games);

                var igdbProvider = new Mock <ILibraryMetadataProvider>();
                igdbProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    igdbCalled = true;
                    return(GameMetadata.GetEmptyData());
                });

                var storeProvider = new Mock <ILibraryMetadataProvider>();
                storeProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    var gameId = g.GameId;
                    var game   = new Game("Store Game " + gameId)
                    {
                        Description = $"Store Description {gameId}",
                        Developers  = new ComparableList <string>()
                        {
                            $"Store Developer {gameId}"
                        },
                        Genres = new ComparableList <string>()
                        {
                            $"Store Genre {gameId}"
                        },
                        Links = new ObservableCollection <Link>()
                        {
                            new Link($"Store link {gameId}", $"Store link url {gameId}")
                        },
                        Publishers = new ComparableList <string>()
                        {
                            $"Store publisher {gameId}"
                        },
                        ReleaseDate = new DateTime(2016, 2, 2),
                        Tags        = new ComparableList <string>()
                        {
                            $"Store Tag {gameId}"
                        }
                    };
                    var icon  = new MetadataFile($"StoreIconName{gameId}.file", randomFile);
                    var image = new MetadataFile($"StoreImageName{gameId}.file", randomFile);
                    return(new GameMetadata(game, icon, image, $"Store backgournd {gameId}"));
                });

                var downloader = new MetadataDownloader(db, igdbProvider.Object, GetLibraryPlugins(storeProvider.Object, storePluginId));
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = false
                };
                settings.ConfigureFields(MetadataSource.Store, true);
                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, null, null);

                Assert.AreEqual(1, callCount);
                Assert.IsFalse(igdbCalled);
                var game2 = db.Games[games[1].Id];
                Assert.AreEqual("Store Description storeId", game2.Description);
                Assert.AreEqual("Store Developer storeId", game2.Developers[0]);
                Assert.AreEqual("Store Genre storeId", game2.Genres[0]);
                Assert.AreEqual("Store link storeId", game2.Links[0].Name);
                Assert.AreEqual("Store link url storeId", game2.Links[0].Url);
                Assert.AreEqual("Store publisher storeId", game2.Publishers[0]);
                Assert.AreEqual("Store Tag storeId", game2.Tags[0]);
                Assert.AreEqual(2016, game2.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game2.BackgroundImage);
                Assert.IsNotEmpty(game2.Icon);
                Assert.IsNotEmpty(game2.CoverImage);
                var game1 = db.Games[games[0].Id];
                Assert.IsNull(game1.Description);

                Assert.AreEqual(2, Directory.GetFiles(db.GetFileStoragePath(game2.Id)).Count());
                Assert.AreEqual(0, Directory.GetFiles(db.GetFileStoragePath(game1.Id)).Count());
            }
        }
Ejemplo n.º 13
0
        public async Task StoreSourceTest()
        {
            var db = new GameDatabase(null);

            using (db.OpenDatabase(new MemoryStream()))
            {
                int callCount = 0;

                var games = new List <Game>()
                {
                    new Game("Game1"),
                    new Game("Game2")
                    {
                        Provider = Provider.Steam, ProviderId = "storeId"
                    },
                    new Game("Game3")
                };

                db.AddGames(games);

                var igdbProvider = new Mock <IMetadataProvider>();
                igdbProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    return(GameMetadata.Empty);
                });

                var storeProvider = new Mock <IMetadataProvider>();
                storeProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    var gameId = g.ProviderId;
                    var game   = new Game("Store Game " + gameId)
                    {
                        Description = $"Store Description {gameId}",
                        Developers  = new ComparableList <string>()
                        {
                            $"Store Developer {gameId}"
                        },
                        Genres = new ComparableList <string>()
                        {
                            $"Store Genre {gameId}"
                        },
                        Links = new ObservableCollection <Link>()
                        {
                            new Link($"Store link {gameId}", $"Store link url {gameId}")
                        },
                        Publishers = new ComparableList <string>()
                        {
                            $"Store publisher {gameId}"
                        },
                        ReleaseDate = new DateTime(2016, 2, 2),
                        Tags        = new ComparableList <string>()
                        {
                            $"Store Tag {gameId}"
                        }
                    };
                    var icon  = new MetadataFile($"StoreIconPath{gameId}.file", $"StoreIconName{gameId}.file", randomFile);
                    var image = new MetadataFile($"StoreImagePath{gameId}.file", $"StoreImageName{gameId}.file", randomFile);
                    return(new GameMetadata(game, icon, image, $"Store backgournd {gameId}"));
                });

                var downloader = new MetadataDownloader(storeProvider.Object, storeProvider.Object, storeProvider.Object, storeProvider.Object, igdbProvider.Object);
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = false
                };
                settings.ConfigureFields(MetadataSource.Store, true);
                await downloader.DownloadMetadataAsync(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings, null, null);

                var dbGames = db.GamesCollection.FindAll().ToList();
                Assert.AreEqual(1, callCount);
                var game2 = dbGames[1];
                Assert.AreEqual("Store Description storeId", game2.Description);
                Assert.AreEqual("Store Developer storeId", game2.Developers[0]);
                Assert.AreEqual("Store Genre storeId", game2.Genres[0]);
                Assert.AreEqual("Store link storeId", game2.Links[0].Name);
                Assert.AreEqual("Store link url storeId", game2.Links[0].Url);
                Assert.AreEqual("Store publisher storeId", game2.Publishers[0]);
                Assert.AreEqual("Store Tag storeId", game2.Tags[0]);
                Assert.AreEqual(2016, game2.ReleaseDate.Value.Year);
                Assert.AreEqual("Store backgournd storeId", game2.BackgroundImage);
                Assert.AreEqual($"StoreIconPathstoreId.file", game2.Icon);
                Assert.AreEqual($"StoreImagePathstoreId.file", game2.Image);
                var game1 = dbGames[0];
                Assert.IsNull(game1.Description);

                Assert.AreEqual(2, db.Database.FileStorage.FindAll().Count());
            }
        }
Ejemplo n.º 14
0
        public async Task SkipExistingTest()
        {
            using (var temp = TempDirectory.Create())
            {
                var db = new GameDatabase(temp.TempPath);
                db.OpenDatabase();
                Game.DatabaseReference = db;
                int callCount = 0;
                var addedGame = db.ImportGame(new GameInfo()
                {
                    Name        = "Game1",
                    Description = "Description",
                    Developers  = new List <string>()
                    {
                        "Developers"
                    },
                    Genres = new List <string>()
                    {
                        "Genres"
                    },
                    Links = new List <Link>()
                    {
                        new Link("Link", "URL")
                    },
                    Publishers = new List <string>()
                    {
                        "Publishers"
                    },
                    ReleaseDate = new DateTime(2012, 6, 6),
                    Tags        = new List <string>()
                    {
                        "Tags"
                    },
                    UserScore      = 1,
                    CommunityScore = 2,
                    CriticScore    = 3
                });

                addedGame.Icon            = "Icon";
                addedGame.CoverImage      = "Image";
                addedGame.BackgroundImage = "BackgroundImage";

                var igdbProvider = new Mock <LibraryMetadataProvider>();
                igdbProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    var gameId = g.Name;
                    var game   = new GameInfo()
                    {
                        Name        = "IGDB Game " + gameId,
                        Description = $"IGDB Description {gameId}",
                        Developers  = new List <string>()
                        {
                            $"IGDB Developer {gameId}"
                        },
                        Genres = new List <string>()
                        {
                            $"IGDB Genre {gameId}"
                        },
                        Links = new List <Link>()
                        {
                            new Link($"IGDB link {gameId}", $"IGDB link url {gameId}")
                        },
                        Publishers = new List <string>()
                        {
                            $"IGDB publisher {gameId}"
                        },
                        ReleaseDate = new DateTime(2012, 6, 6),
                        Tags        = new List <string>()
                        {
                            $"IGDB Tag {gameId}"
                        }
                    };
                    var icon       = new MetadataFile($"IGDBIconName{gameId}.file", randomFile);
                    var image      = new MetadataFile($"IGDBImageName{gameId}.file", randomFile);
                    var background = new MetadataFile($"IGDB backgournd {gameId}");
                    return(new GameMetadata(game, icon, image, background));
                });

                var downloader = new MetadataDownloader(db, igdbProvider.Object, null);
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = true
                };

                // No download - all values are kept
                settings.ConfigureFields(MetadataSource.IGDB, true);
                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, null, null);

                var dbGames = db.Games.ToList();
                Assert.AreEqual(0, callCount);

                var game1 = dbGames[0];
                Assert.AreEqual("Description", game1.Description);
                Assert.AreEqual("Developers", game1.Developers[0].Name);
                Assert.AreEqual("Genres", game1.Genres[0].Name);
                Assert.AreEqual("Link", game1.Links[0].Name);
                Assert.AreEqual("URL", game1.Links[0].Url);
                Assert.AreEqual("Publishers", game1.Publishers[0].Name);
                Assert.AreEqual("Tags", game1.Tags[0].Name);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game1.BackgroundImage);
                Assert.IsNotEmpty(game1.Icon);
                Assert.IsNotEmpty(game1.CoverImage);

                // Single download - values are changed even when present
                settings.SkipExistingValues = false;
                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, null, null);

                dbGames = db.Games.ToList();
                Assert.AreEqual(1, callCount);

                game1 = dbGames[0];
                Assert.AreEqual("IGDB Description Game1", game1.Description);
                Assert.AreEqual("IGDB Developer Game1", game1.Developers[0].Name);
                Assert.AreEqual("IGDB Genre Game1", game1.Genres[0].Name);
                Assert.AreEqual("IGDB link Game1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url Game1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher Game1", game1.Publishers[0].Name);
                Assert.AreEqual("IGDB Tag Game1", game1.Tags[0].Name);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game1.BackgroundImage);
                Assert.IsNotEmpty(game1.Icon);
                Assert.IsNotEmpty(game1.CoverImage);

                // Single download - values are changed when skip enabled and values are not present
                callCount = 0;
                settings.SkipExistingValues = true;
                db.Games.Remove(game1);
                db.Games.Add(new Game("Game1"));

                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, null, null);

                dbGames = db.Games.ToList();
                Assert.AreEqual(1, callCount);

                game1 = dbGames[0];
                Assert.AreEqual("IGDB Description Game1", game1.Description);
                Assert.AreEqual("IGDB Developer Game1", game1.Developers[0].Name);
                Assert.AreEqual("IGDB Genre Game1", game1.Genres[0].Name);
                Assert.AreEqual("IGDB link Game1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url Game1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher Game1", game1.Publishers[0].Name);
                Assert.AreEqual("IGDB Tag Game1", game1.Tags[0].Name);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.IsNotEmpty(game1.BackgroundImage);
                Assert.IsNotEmpty(game1.Icon);
                Assert.IsNotEmpty(game1.CoverImage);
            }
        }
Ejemplo n.º 15
0
        public async Task StoreSourceTest()
        {
            var path = Path.Combine(PlayniteTests.TempPath, "metadownload.db");

            FileSystem.DeleteFile(path);

            var db = new GameDatabase(null);

            using (db.OpenDatabase(path))
            {
                var games = new List <Game>()
                {
                    new Game("Game1"),
                    new Game("Game2")
                    {
                        Provider = Provider.Steam, ProviderId = "storeId"
                    },
                    new Game("Game3") // just to test that nonexistent game doesn't throw exception
                };

                db.AddGames(games);

                var igdbProvider  = new MockMetadataProvider();
                var storeProvider = new MockMetadataProvider
                {
                    GetSupportsIdSearchHandler = () => true,
                    GetGameDataHandler         = (gameId) =>
                    {
                        var game = new Game("Store Game " + gameId)
                        {
                            Description = $"Store Description {gameId}",
                            Developers  = new ComparableList <string>()
                            {
                                $"Store Developer {gameId}"
                            },
                            Genres = new ComparableList <string>()
                            {
                                $"Store Genre {gameId}"
                            },
                            Links = new ObservableCollection <Link>()
                            {
                                new Link($"Store link {gameId}", $"Store link url {gameId}")
                            },
                            Publishers = new ComparableList <string>()
                            {
                                $"Store publisher {gameId}"
                            },
                            ReleaseDate = new DateTime(2016, 2, 2),
                            Tags        = new ComparableList <string>()
                            {
                                $"Store Tag {gameId}"
                            }
                        };
                        var icon  = new FileDefinition($"StoreIconPath{gameId}.file", $"StoreIconName{gameId}.file", randomFile);
                        var image = new FileDefinition($"StoreImagePath{gameId}.file", $"StoreImageName{gameId}.file", randomFile);
                        return(new GameMetadata(game, icon, image, $"Store backgournd {gameId}"));
                    }
                };

                var downloader = new MockMetadataDownloader(storeProvider, storeProvider, storeProvider, storeProvider, igdbProvider);
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = false
                };
                settings.ConfigureFields(MetadataSource.Store, true);
                await downloader.DownloadMetadata(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings);

                var dbGames = db.GamesCollection.FindAll().ToList();
                Assert.AreEqual(1, downloader.CallCount);
                var game2 = dbGames[1];
                Assert.AreEqual("Store Description storeId", game2.Description);
                Assert.AreEqual("Store Developer storeId", game2.Developers[0]);
                Assert.AreEqual("Store Genre storeId", game2.Genres[0]);
                Assert.AreEqual("Store link storeId", game2.Links[0].Name);
                Assert.AreEqual("Store link url storeId", game2.Links[0].Url);
                Assert.AreEqual("Store publisher storeId", game2.Publishers[0]);
                Assert.AreEqual("Store Tag storeId", game2.Tags[0]);
                Assert.AreEqual(2016, game2.ReleaseDate.Value.Year);
                Assert.AreEqual("Store backgournd storeId", game2.BackgroundImage);
                Assert.AreEqual($"StoreIconPathstoreId.file", game2.Icon);
                Assert.AreEqual($"StoreImagePathstoreId.file", game2.Image);
                var game1 = dbGames[0];
                Assert.IsNull(game1.Description);

                Assert.AreEqual(2, db.Database.FileStorage.FindAll().Count());
            }
        }
        public async Task MissingDataTest()
        {
            // Test that downloader doesn't change existing values to null when missing by provider
            var path = Path.Combine(Playnite.PlayniteTests.TempPath, "metadownload.db");

            FileSystem.DeleteFile(path);

            var db = new GameDatabase(null);

            using (db.OpenDatabase(path))
            {
                var games = new List <IGame>()
                {
                    new Game("Game")
                    {
                        Provider   = Provider.Steam,
                        ProviderId = "storeId",
                        Genres     = new ComparableList <string>()
                        {
                            "Genre"
                        },
                        ReleaseDate = new DateTime(2012, 6, 6),
                        Developers  = new ComparableList <string>()
                        {
                            "Developer"
                        },
                        Publishers = new ComparableList <string>()
                        {
                            "Publisher"
                        },
                        Tags = new ComparableList <string>()
                        {
                            "Tag"
                        },
                        Description = "Description",
                        Links       = new ObservableCollection <Link>()
                        {
                            new Link()
                        },
                        Icon            = "icon",
                        Image           = "image",
                        BackgroundImage = "backImage"
                    }
                };

                db.AddGames(games);

                var igdbProvider  = new MockMetadataProvider();
                var storeProvider = new MockMetadataProvider
                {
                    GetSupportsIdSearchHandler = () => true,
                    GetGameDataHandler         = (gameId) =>
                    {
                        return(new GameMetadata(new Game("Store Game " + gameId), null, null, null));
                    }
                };

                var downloader = new MockMetadataDownloader(storeProvider, storeProvider, storeProvider, storeProvider, igdbProvider);
                var settings   = new MetadataDownloaderSettings();


                var dbGames = db.GamesCollection.FindAll().ToList();
                var f       = dbGames[0].ReleaseDate;
                var s       = games[0].ReleaseDate;

                settings.ConfigureFields(MetadataSource.Store, true);
                await downloader.DownloadMetadata(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings);

                dbGames = db.GamesCollection.FindAll().ToList();
                Assert.AreEqual(1, downloader.CallCount);
                var game = dbGames[0];
                Assert.AreEqual("Description", game.Description);
                Assert.AreEqual("icon", game.Icon);
                Assert.AreEqual("image", game.Image);
                Assert.AreEqual("backImage", game.BackgroundImage);
                Assert.AreEqual("Developer", game.Developers[0]);
                Assert.AreEqual("Publisher", game.Publishers[0]);
                Assert.AreEqual("Genre", game.Genres[0]);
                CollectionAssert.IsNotEmpty(game.Links);
                Assert.AreEqual("Tag", game.Tags[0]);
                Assert.AreEqual(2012, game.ReleaseDate.Value.Year);
            }
        }
Ejemplo n.º 17
0
        public async Task IGDBStoreCombinedTest()
        {
            var path = Path.Combine(PlayniteTests.TempPath, "metadownload.db");

            FileSystem.DeleteFile(path);

            var db = new GameDatabase(null);

            using (db.OpenDatabase(path))
            {
                var games = new List <Game>()
                {
                    new Game("Game1"),
                    new Game("Game2")
                    {
                        Provider = Provider.Steam, ProviderId = "Game2"
                    },
                    new Game("Game3") // just to test that nonexistent game doesn't throw exception
                };

                db.AddGames(games);

                var igdbProvider = new MockMetadataProvider
                {
                    GetSupportsIdSearchHandler = () => false,
                    GetGameDataHandler         = gameId =>
                    {
                        var game = new Game("IGDB Game " + gameId);
                        game.Description = $"IGDB Description {gameId}";
                        game.Genres      = new ComparableList <string>()
                        {
                            $"IGDB Genre {gameId}"
                        };
                        game.Links = new ObservableCollection <Link>()
                        {
                            new Link($"IGDB link {gameId}", $"IGDB link url {gameId}")
                        };
                        game.Publishers = new ComparableList <string>()
                        {
                            $"IGDB publisher {gameId}"
                        };
                        game.ReleaseDate = new DateTime(2012, 6, 6);
                        var icon  = new FileDefinition($"IGDBIconPath{gameId}.file", $"IGDBIconName{gameId}.file", randomFile);
                        var image = new FileDefinition($"IGDBImagePath{gameId}.file", $"IGDBImageName{gameId}.file", randomFile);
                        return(new GameMetadata(game, icon, image, $"IGDB backgournd {gameId}"));
                    },
                    SearchGamesHandler = gameName =>
                    {
                        return(new List <MetadataSearchResult>()
                        {
                            new MetadataSearchResult("igdbid1", "Game1", DateTime.Now),
                            new MetadataSearchResult("igdbid2", "Game2", DateTime.Now)
                        });
                    }
                };

                var storeProvider = new MockMetadataProvider
                {
                    GetSupportsIdSearchHandler = () => true,
                    GetGameDataHandler         = (gameId) =>
                    {
                        var game = new Game(gameId);
                        game.Description = $"Store Description {gameId}";
                        game.Developers  = new ComparableList <string>()
                        {
                            $"Store Developer {gameId}"
                        };
                        game.Links = new ObservableCollection <Link>()
                        {
                            new Link($"Store link {gameId}", $"Store link url {gameId}")
                        };
                        game.Publishers = new ComparableList <string>()
                        {
                            $"Store publisher {gameId}"
                        };
                        game.ReleaseDate = new DateTime(2016, 2, 2);
                        var icon  = new FileDefinition($"StoreIconPath{gameId}.file", $"StoreIconName{gameId}.file", randomFile);
                        var image = new FileDefinition($"StoreImagePath{gameId}.file", $"StoreImageName{gameId}.file", randomFile);
                        return(new GameMetadata(game, icon, image, $"Store backgournd {gameId}"));
                    }
                };

                var downloader = new MockMetadataDownloader(storeProvider, storeProvider, storeProvider, storeProvider, igdbProvider);
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = false
                };

                // IGDB over Store
                settings.ConfigureFields(MetadataSource.IGDBOverStore, true);
                await downloader.DownloadMetadata(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings);

                Assert.AreEqual(4, downloader.CallCount);

                var dbGames = db.GamesCollection.FindAll().ToList();
                var game1   = dbGames[0];
                Assert.AreEqual("IGDB Description igdbid1", game1.Description);
                Assert.IsNull(game1.Developers);
                Assert.AreEqual("IGDB Genre igdbid1", game1.Genres[0]);
                Assert.AreEqual("IGDB link igdbid1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url igdbid1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher igdbid1", game1.Publishers[0]);
                Assert.IsNull(game1.Tags);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.AreEqual("IGDB backgournd igdbid1", game1.BackgroundImage);
                Assert.AreEqual($"IGDBIconPathigdbid1.file", game1.Icon);
                Assert.AreEqual($"IGDBImagePathigdbid1.file", game1.Image);

                var game2 = dbGames[1];
                Assert.AreEqual("IGDB Description igdbid2", game2.Description);
                Assert.AreEqual("Store Developer Game2", game2.Developers[0]);
                Assert.AreEqual("IGDB Genre igdbid2", game2.Genres[0]);
                Assert.AreEqual("IGDB link igdbid2", game2.Links[0].Name);
                Assert.AreEqual("IGDB link url igdbid2", game2.Links[0].Url);
                Assert.AreEqual("IGDB publisher igdbid2", game2.Publishers[0]);
                Assert.IsNull(game2.Tags);
                Assert.AreEqual(2012, game2.ReleaseDate.Value.Year);
                Assert.AreEqual("IGDB backgournd igdbid2", game2.BackgroundImage);
                Assert.AreEqual($"IGDBIconPathigdbid2.file", game2.Icon);
                Assert.AreEqual($"IGDBImagePathigdbid2.file", game2.Image);

                // Store over IGDB
                downloader.CallCount = 0;
                settings.ConfigureFields(MetadataSource.StoreOverIGDB, true);
                await downloader.DownloadMetadata(
                    db.GamesCollection.FindAll().ToList(),
                    db, settings);

                Assert.AreEqual(4, downloader.CallCount);

                dbGames = db.GamesCollection.FindAll().ToList();
                game1   = dbGames[0];
                Assert.AreEqual("IGDB Description igdbid1", game1.Description);
                Assert.IsNull(game1.Developers);
                Assert.AreEqual("IGDB Genre igdbid1", game1.Genres[0]);
                Assert.AreEqual("IGDB link igdbid1", game1.Links[0].Name);
                Assert.AreEqual("IGDB link url igdbid1", game1.Links[0].Url);
                Assert.AreEqual("IGDB publisher igdbid1", game1.Publishers[0]);
                Assert.IsNull(game1.Tags);
                Assert.AreEqual(2012, game1.ReleaseDate.Value.Year);
                Assert.AreEqual("IGDB backgournd igdbid1", game1.BackgroundImage);
                Assert.AreEqual($"IGDBIconPathigdbid1.file", game1.Icon);
                Assert.AreEqual($"IGDBImagePathigdbid1.file", game1.Image);

                game2 = dbGames[1];
                Assert.AreEqual("Store Description Game2", game2.Description);
                Assert.AreEqual("Store Developer Game2", game2.Developers[0]);
                Assert.AreEqual("IGDB Genre igdbid2", game2.Genres[0]);
                Assert.AreEqual("Store link Game2", game2.Links[0].Name);
                Assert.AreEqual("Store link url Game2", game2.Links[0].Url);
                Assert.AreEqual("Store publisher Game2", game2.Publishers[0]);
                Assert.IsNull(game2.Tags);
                Assert.AreEqual(2016, game2.ReleaseDate.Value.Year);
                Assert.AreEqual("Store backgournd Game2", game2.BackgroundImage);
                Assert.AreEqual($"StoreIconPathGame2.file", game2.Icon);
                Assert.AreEqual($"StoreImagePathGame2.file", game2.Image);
            }
        }
Ejemplo n.º 18
0
        public async Task MissingDataTest()
        {
            using (var temp = TempDirectory.Create())
            {
                var db = new GameDatabase(temp.TempPath);
                db.OpenDatabase();
                Game.DatabaseReference = db;
                int callCount = 0;

                var importedGame = db.ImportGame(new GameInfo()
                {
                    Name   = "Game",
                    GameId = "storeId",
                    Genres = new List <string>()
                    {
                        "Genre"
                    },
                    ReleaseDate = new DateTime(2012, 6, 6),
                    Developers  = new List <string>()
                    {
                        "Developer"
                    },
                    Publishers = new List <string>()
                    {
                        "Publisher"
                    },
                    Tags = new List <string>()
                    {
                        "Tag"
                    },
                    Description = "Description",
                    Links       = new List <Link>()
                    {
                        new Link()
                    }
                });

                importedGame.PluginId        = storePluginId;
                importedGame.Icon            = "icon";
                importedGame.CoverImage      = "image";
                importedGame.BackgroundImage = "backImage";
                db.Games.Update(importedGame);

                var igdbProvider = new Mock <LibraryMetadataProvider>();
                igdbProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    return(GameMetadata.GetEmptyData());
                });

                var storeProvider = new Mock <LibraryMetadataProvider>();
                storeProvider.Setup(x => x.GetMetadata(It.IsAny <Game>())).Returns((Game g) =>
                {
                    callCount++;
                    var gameId = g.GameId;
                    return(new GameMetadata(new GameInfo()
                    {
                        Name = "Store Game " + gameId
                    }, null, null, null));
                });

                var downloader = new MetadataDownloader(db, igdbProvider.Object, GetLibraryPlugins(storeProvider.Object, storePluginId));
                var settings   = new MetadataDownloaderSettings()
                {
                    SkipExistingValues = false
                };

                var dbGames = db.Games.ToList();
                var f       = dbGames[0].ReleaseDate;
                var s       = importedGame.ReleaseDate;

                settings.ConfigureFields(MetadataSource.Store, true);
                await downloader.DownloadMetadataAsync(
                    db.Games.ToList(), settings, null, null);

                dbGames = db.Games.ToList();
                Assert.AreEqual(1, callCount);
                var game = dbGames[0];
                Assert.AreEqual("Description", game.Description);
                Assert.AreEqual("icon", game.Icon);
                Assert.AreEqual("image", game.CoverImage);
                Assert.AreEqual("backImage", game.BackgroundImage);
                Assert.AreEqual("Developer", game.Developers[0].Name);
                Assert.AreEqual("Publisher", game.Publishers[0].Name);
                Assert.AreEqual("Genre", game.Genres[0].Name);
                CollectionAssert.IsNotEmpty(game.Links);
                Assert.AreEqual("Tag", game.Tags[0].Name);
                Assert.AreEqual(2012, game.ReleaseDate.Value.Year);
            }
        }