public void SetDefaultBackgroundImage()
        {
            if (GameContext.BackgroundImage.IsNullOrEmpty())
            {
                SetBackgroundImage();
            }
            else
            {
                string PathImage = ImageSourceManager.GetImagePath(GameContext.BackgroundImage);
                if (PathImage.IsNullOrEmpty())
                {
                    PathImage = PluginDatabase.PlayniteApi.Database.GetFullFilePath(GameContext.BackgroundImage);
                }

                SetBackgroundImage(PathImage);
            }
        }
        private List <GameField> GetDisplayDiffFields(Game testGame, GameMetadata metadata)
        {
            var diffFields = new List <GameField>();
            var newInfo    = metadata.GameInfo;

            void checkListChanged <T>(List <T> source, List <string> other, GameField field) where T : DatabaseObject
            {
                if (other.HasNonEmptyItems())
                {
                    if (source.HasItems() && !source.Select(a => a.Name).IsListEqual(other, new GameFieldComparer()))
                    {
                        diffFields.Add(field);
                    }
                }
            }

            void checkItemChanged <T>(T source, string other, GameField field) where T : DatabaseObject
            {
                if (!other.IsNullOrEmpty())
                {
                    if (source != null && !string.Equals(source.Name, other, StringComparison.OrdinalIgnoreCase))
                    {
                        diffFields.Add(field);
                    }
                }
            }

            if (!newInfo.Name.IsNullOrEmpty())
            {
                if (!testGame.Name.IsNullOrEmpty() && !string.Equals(testGame.Name, newInfo.Name, StringComparison.OrdinalIgnoreCase))
                {
                    diffFields.Add(GameField.Name);
                }
            }

            if (!newInfo.Description.IsNullOrEmpty())
            {
                if (!testGame.Description.IsNullOrEmpty() && !string.Equals(testGame.Description, newInfo.Description, StringComparison.Ordinal))
                {
                    diffFields.Add(GameField.Description);
                }
            }

            checkItemChanged(testGame.AgeRating, newInfo.AgeRating, GameField.AgeRating);
            checkItemChanged(testGame.Region, newInfo.Region, GameField.Region);
            checkItemChanged(testGame.Series, newInfo.Series, GameField.Series);
            checkItemChanged(testGame.Platform, newInfo.Platform, GameField.Platform);

            checkListChanged(testGame.Developers, newInfo.Developers, GameField.Developers);
            checkListChanged(testGame.Publishers, newInfo.Publishers, GameField.Publishers);
            checkListChanged(testGame.Genres, newInfo.Genres, GameField.Genres);
            checkListChanged(testGame.Tags, newInfo.Tags, GameField.Tags);
            checkListChanged(testGame.Features, newInfo.Features, GameField.Features);

            if (newInfo.ReleaseDate != null)
            {
                if (testGame.ReleaseDate != null && testGame.ReleaseDate != newInfo.ReleaseDate)
                {
                    diffFields.Add(GameField.ReleaseDate);
                }
            }

            if (newInfo.Links.HasItems())
            {
                if (testGame.Links.HasItems() && !testGame.Links.IsListEqualExact(newInfo.Links))
                {
                    diffFields.Add(GameField.Links);
                }
            }

            if (newInfo.CriticScore != null)
            {
                if (testGame.CriticScore != null && testGame.CriticScore != newInfo.CriticScore)
                {
                    diffFields.Add(GameField.CriticScore);
                }
            }

            if (newInfo.CommunityScore != null)
            {
                if (testGame.CommunityScore != null && testGame.CommunityScore != newInfo.CommunityScore)
                {
                    diffFields.Add(GameField.CommunityScore);
                }
            }

            if (metadata.Icon != null && !testGame.Icon.IsNullOrEmpty())
            {
                var newIcon = ProcessMetadataFile(metadata.Icon, tempIconFileName);
                if (newIcon != null)
                {
                    var currentPath = ImageSourceManager.GetImagePath(EditingGame.Icon);
                    if (currentPath.IsNullOrEmpty() ||
                        !File.Exists(currentPath) ||
                        !FileSystem.AreFileContentsEqual(newIcon, currentPath))
                    {
                        metadata.Icon = new MetadataFile(newIcon);
                        diffFields.Add(GameField.Icon);
                    }
                    else
                    {
                        metadata.Icon = null;
                    }
                }
                else
                {
                    metadata.Icon = null;
                }
            }

            if (metadata.CoverImage != null && !testGame.CoverImage.IsNullOrEmpty())
            {
                var newCover = ProcessMetadataFile(metadata.CoverImage, tempCoverFileName);
                if (newCover != null)
                {
                    var currentPath = ImageSourceManager.GetImagePath(EditingGame.CoverImage);
                    if (currentPath.IsNullOrEmpty() ||
                        !File.Exists(currentPath) ||
                        !FileSystem.AreFileContentsEqual(newCover, currentPath))
                    {
                        metadata.CoverImage = new MetadataFile(newCover);
                        diffFields.Add(GameField.CoverImage);
                    }
                    else
                    {
                        metadata.CoverImage = null;
                    }
                }
                else
                {
                    metadata.CoverImage = null;
                }
            }

            if (metadata.BackgroundImage != null && !testGame.BackgroundImage.IsNullOrEmpty())
            {
                var newBack = ProcessMetadataFile(metadata.BackgroundImage, tempBackgroundFileName);
                if (newBack != null)
                {
                    var currentPath = ImageSourceManager.GetImagePath(EditingGame.BackgroundImage);
                    if (currentPath.IsNullOrEmpty() ||
                        !File.Exists(currentPath) ||
                        !FileSystem.AreFileContentsEqual(newBack, currentPath))
                    {
                        metadata.BackgroundImage = new MetadataFile(newBack);
                        diffFields.Add(GameField.BackgroundImage);
                    }
                    else
                    {
                        metadata.BackgroundImage = null;
                    }
                }
                else
                {
                    metadata.BackgroundImage = null;
                }
            }

            return(diffFields);
        }
Esempio n. 3
0
        private List <GameField> GetDisplayDiffFields(Game oldGame, ComparableMetadatGameData newGame)
        {
            var diffFields = new List <GameField>();

            void checkListChanged <T>(List <T> oldData, List <T> newData, GameField field) where T : DatabaseObject
            {
                if (!oldData.HasItems() && newData.HasItems())
                {
                    diffFields.Add(field);
                    return;
                }

                if (newData.HasItems() && oldData.HasItems() && !oldData.Select(a => a.Id).IsListEqual(newData.Select(b => b.Id)))
                {
                    diffFields.Add(field);
                }
            }

            //void checkItemChanged<T>(T source, string other, GameField field) where T : DatabaseObject
            //{
            //    if (!other.IsNullOrEmpty())
            //    {
            //        if (source != null && !string.Equals(source.Name, other, StringComparison.OrdinalIgnoreCase))
            //        {
            //            diffFields.Add(field);
            //        }
            //    }
            //}

            if (!newGame.Name.IsNullOrEmpty())
            {
                if (!oldGame.Name.IsNullOrEmpty() && !string.Equals(oldGame.Name, newGame.Name, StringComparison.OrdinalIgnoreCase))
                {
                    diffFields.Add(GameField.Name);
                }
            }

            if (!newGame.Description.IsNullOrEmpty())
            {
                if (!oldGame.Description.IsNullOrEmpty() && !string.Equals(oldGame.Description, newGame.Description, StringComparison.Ordinal))
                {
                    diffFields.Add(GameField.Description);
                }
            }

            checkListChanged(oldGame.AgeRatings, newGame.AgeRatings, GameField.AgeRatings);
            checkListChanged(oldGame.Regions, newGame.Regions, GameField.Regions);
            checkListChanged(oldGame.Series, newGame.Series, GameField.Series);
            checkListChanged(oldGame.Platforms, newGame.Platforms, GameField.Platforms);
            checkListChanged(oldGame.Developers, newGame.Developers, GameField.Developers);
            checkListChanged(oldGame.Publishers, newGame.Publishers, GameField.Publishers);
            checkListChanged(oldGame.Genres, newGame.Genres, GameField.Genres);
            checkListChanged(oldGame.Tags, newGame.Tags, GameField.Tags);
            checkListChanged(oldGame.Features, newGame.Features, GameField.Features);

            if (newGame.ReleaseDate != null)
            {
                if (oldGame.ReleaseDate != null && oldGame.ReleaseDate != newGame.ReleaseDate)
                {
                    diffFields.Add(GameField.ReleaseDate);
                }
            }

            if (newGame.Links.HasItems())
            {
                if (oldGame.Links.HasItems() && !oldGame.Links.IsListEqualExact(newGame.Links))
                {
                    diffFields.Add(GameField.Links);
                }
            }

            if (newGame.CriticScore != null)
            {
                if (oldGame.CriticScore != null && oldGame.CriticScore != newGame.CriticScore)
                {
                    diffFields.Add(GameField.CriticScore);
                }
            }

            if (newGame.CommunityScore != null)
            {
                if (oldGame.CommunityScore != null && oldGame.CommunityScore != newGame.CommunityScore)
                {
                    diffFields.Add(GameField.CommunityScore);
                }
            }

            if (newGame.Icon != null && !oldGame.Icon.IsNullOrEmpty())
            {
                var newIcon = ProcessMetadataFile(newGame.Icon, tempIconFileName);
                if (newIcon != null)
                {
                    var currentPath = ImageSourceManager.GetImagePath(EditingGame.Icon);
                    if (currentPath.IsNullOrEmpty() ||
                        !File.Exists(currentPath) ||
                        !FileSystem.AreFileContentsEqual(newIcon, currentPath))
                    {
                        newGame.Icon = new MetadataFile(newIcon);
                        diffFields.Add(GameField.Icon);
                    }
                    else
                    {
                        newGame.Icon = null;
                    }
                }
                else
                {
                    newGame.Icon = null;
                }
            }

            if (newGame.CoverImage != null && !oldGame.CoverImage.IsNullOrEmpty())
            {
                var newCover = ProcessMetadataFile(newGame.CoverImage, tempCoverFileName);
                if (newCover != null)
                {
                    var currentPath = ImageSourceManager.GetImagePath(EditingGame.CoverImage);
                    if (currentPath.IsNullOrEmpty() ||
                        !File.Exists(currentPath) ||
                        !FileSystem.AreFileContentsEqual(newCover, currentPath))
                    {
                        newGame.CoverImage = new MetadataFile(newCover);
                        diffFields.Add(GameField.CoverImage);
                    }
                    else
                    {
                        newGame.CoverImage = null;
                    }
                }
                else
                {
                    newGame.CoverImage = null;
                }
            }

            if (newGame.BackgroundImage != null && !oldGame.BackgroundImage.IsNullOrEmpty())
            {
                var newBack = ProcessMetadataFile(newGame.BackgroundImage, tempBackgroundFileName);
                if (newBack != null)
                {
                    var currentPath = ImageSourceManager.GetImagePath(EditingGame.BackgroundImage);
                    if (currentPath.IsNullOrEmpty() ||
                        !File.Exists(currentPath) ||
                        !FileSystem.AreFileContentsEqual(newBack, currentPath))
                    {
                        newGame.BackgroundImage = new MetadataFile(newBack);
                        diffFields.Add(GameField.BackgroundImage);
                    }
                    else
                    {
                        newGame.BackgroundImage = null;
                    }
                }
                else
                {
                    newGame.BackgroundImage = null;
                }
            }

            return(diffFields);
        }
        public override GameBackgroundImages Get(Guid Id, bool OnlyCache = false, bool Force = false)
        {
            GameBackgroundImages gameBackgroundImages = GetOnlyCache(Id);

            if (gameBackgroundImages == null)
            {
                Game game = PlayniteApi.Database.Games.Get(Id);
                if (game != null)
                {
                    gameBackgroundImages = GetDefault(game);
                    Add(gameBackgroundImages);
                }
                else
                {
                    return(gameBackgroundImages);
                }
            }

            // Check default background
            if (gameBackgroundImages.Items.Find(x => x.IsDefault && !x.IsCover) != null)
            {
                int Index = gameBackgroundImages.Items.FindIndex(x => x.IsDefault && !x.IsCover);
                if (Index != -1)
                {
                    gameBackgroundImages.Items.RemoveAt(Index);
                }
            }
            if (!gameBackgroundImages.BackgroundImage.IsNullOrEmpty() && gameBackgroundImages.Items.Find(x => x.IsDefault && !x.IsCover) == null)
            {
                string PathImage = ImageSourceManager.GetImagePath(gameBackgroundImages.BackgroundImage);
                if (PathImage.IsNullOrEmpty() && !File.Exists(PathImage))
                {
                    PathImage = PlayniteApi.Database.GetFullFilePath(gameBackgroundImages.BackgroundImage);
                }

                gameBackgroundImages.Items.Insert(0, new ItemImage
                {
                    Name      = PathImage,
                    IsCover   = false,
                    IsDefault = true
                });
            }

            // Check default cover
            if (gameBackgroundImages.Items.Find(x => x.IsDefault && x.IsCover) != null)
            {
                int Index = gameBackgroundImages.Items.FindIndex(x => x.IsDefault && x.IsCover);
                if (Index != -1)
                {
                    gameBackgroundImages.Items.RemoveAt(Index);
                }
            }
            if (!gameBackgroundImages.CoverImage.IsNullOrEmpty() && gameBackgroundImages.Items.Find(x => x.IsDefault && x.IsCover) == null)
            {
                string PathImage = ImageSourceManager.GetImagePath(gameBackgroundImages.CoverImage);
                if (PathImage.IsNullOrEmpty() && !File.Exists(PathImage))
                {
                    PathImage = PlayniteApi.Database.GetFullFilePath(gameBackgroundImages.CoverImage);
                }

                gameBackgroundImages.Items.Insert(0, new ItemImage
                {
                    Name      = PathImage,
                    IsCover   = true,
                    IsDefault = true
                });
            }

            return(gameBackgroundImages);
        }