Пример #1
0
        public bool HasData()
        {
            using (var metaDataContext = new MetaDataContext(GetPluginUserDataPath()))
            {
                if (metaDataContext.Games.Any())
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #2
0
        public async Task <string> UpdateMetadata(GlobalProgressActionArgs progress)
        {
            var newMetadataHash = await _lbgdbApi.GetMetadataHash();

            var zipFile = await _lbgdbApi.DownloadMetadata();

            await Task.Run(async() =>
            {
                using (var zipArchive = new ZipArchive(zipFile, ZipArchiveMode.Read))
                {
                    var metaData = zipArchive.Entries.FirstOrDefault(entry =>
                                                                     entry.Name.Equals(Settings.MetaDataFileName, StringComparison.OrdinalIgnoreCase));

                    if (metaData != null)
                    {
                        //progress.Text = "Updating database...";
                        using (var context = new MetaDataContext(GetPluginUserDataPath()))
                        {
                            await context.Database.EnsureDeletedAsync();
                            await context.Database.MigrateAsync();
                        }
                        progress.CurrentProgressValue++;

                        //progress.Text = "Importing games...";
                        using (var metaDataStream = metaData.Open())
                        {
                            await ImportXml <LaunchBox.Metadata.Game>(metaDataStream);
                        }
                        progress.CurrentProgressValue++;

                        //progress.Text = "Importing alternate game names...";
                        using (var metaDataStream = metaData.Open())
                        {
                            await ImportXml <GameAlternateName>(metaDataStream);
                        }
                        progress.CurrentProgressValue++;

                        //progress.Text = "Importing media...";
                        using (var metaDataStream = metaData.Open())
                        {
                            await ImportXml <GameImage>(metaDataStream);
                        }
                        progress.CurrentProgressValue++;
                    }
                }
            });

            Settings.OldMetadataHash = newMetadataHash;
            Settings.EndEdit();

            return(newMetadataHash);
        }
Пример #3
0
        private async Task <bool> ImportXml <T>(Stream metaDataStream, int bufferSize = 10000) where T : class
        {
            var           xElementList  = metaDataStream.AsEnumerableXml(typeof(T).Name);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            using (var context = new MetaDataContext(GetPluginUserDataPath()))
            {
                context.ChangeTracker.AutoDetectChangesEnabled = false;
                var objectList = new List <T>(bufferSize);
                foreach (var xElement in xElementList)
                {
                    T deserializedObject;
                    using (var reader = xElement.CreateReader())
                    {
                        deserializedObject = (T)xmlSerializer.Deserialize(reader);
                    }

                    switch (deserializedObject)
                    {
                    case LaunchBox.Metadata.Game game:
                        game.NameSearch     = game.Name.Sanitize();
                        game.PlatformSearch = game.Platform.Sanitize();
                        if (game.CommunityRating != null)
                        {
                            game.CommunityRating = Math.Round(((decimal)game.CommunityRating / 5) * 100, 0);
                        }
                        break;

                    case GameAlternateName game:
                        game.NameSearch = game.AlternateName.Sanitize();
                        break;
                    }

                    objectList.Add(deserializedObject);

                    if (objectList.Count >= bufferSize)
                    {
                        await context.BulkInsertAsync(objectList);

                        objectList.Clear();
                    }
                }

                if (objectList.Count > 0)
                {
                    await context.BulkInsertAsync(objectList);
                }
            }

            return(true);
        }
Пример #4
0
        public LbgdbMetadataPlugin(IPlayniteAPI playniteAPI) : base(playniteAPI)
        {
            using (var metadataContext = new MetaDataContext(GetPluginUserDataPath()))
            {
                metadataContext.Database.Migrate();
            }
            Settings = new LbgdbMetadataSettings(this);
            var apiOptions = new Options
            {
                MetaDataFileName = Settings.MetaDataFileName,
                MetaDataURL      = Settings.MetaDataURL
            };

            _lbgdbApi = new LbgdbApi(apiOptions);
        }
        public override MetadataFile GetBackgroundImage()
        {
            var game = GetGame();

            if (game != null)
            {
                using (var context = new MetaDataContext(_plugin.GetPluginUserDataPath()))
                {
                    var backgroundImage = GetBestImage(context.GameImages.Where(image => image.DatabaseID == game.DatabaseID && LaunchBox.Image.ImageType.Background.Contains(image.Type)).ToList(), LaunchBox.Image.ImageType.Background);
                    if (backgroundImage != null)
                    {
                        return(new MetadataFile("https://images.launchbox-app.com/" + backgroundImage.FileName));
                    }
                }
            }

            return(base.GetBackgroundImage());
        }
        public override MetadataFile GetIcon()
        {
            var game = GetGame();

            if (game != null)
            {
                using (var context = new MetaDataContext(_plugin.GetPluginUserDataPath()))
                {
                    var icon =
                        GetBestImage(
                            context.GameImages.Where(image =>
                                                     image.DatabaseID == game.DatabaseID &&
                                                     LaunchBox.Image.ImageType.Icon.Contains(image.Type)).ToList(),
                            LaunchBox.Image.ImageType.Icon);
                    if (icon != null)
                    {
                        var imageData = _plugin.HttpClient.GetByteArrayAsync("https://images.launchbox-app.com/" + icon.FileName).Result;

                        using (Image image = Image.Load(imageData))
                        {
                            image.Mutate(x =>
                            {
                                x.Resize(new ResizeOptions
                                {
                                    Size = new Size(256, 256),
                                    Mode = ResizeMode.Pad
                                });
                            });

                            using (MemoryStream ms = new MemoryStream())
                            {
                                image.Save(ms, new PngEncoder());
                                return(new MetadataFile(icon.FileName, ms.ToArray(), "https://images.launchbox-app.com/" + icon.FileName));
                            }
                        }
                    }
                }
            }

            return(base.GetIcon());
        }
Пример #7
0
        public void UpdateMetadata(string filename)
        {
            using (var zipArchive = ZipFile.Open(filename, ZipArchiveMode.Read))
            {
                var metaData = zipArchive.Entries.FirstOrDefault(entry =>
                                                                 entry.Name.Equals("MetaData.xml", StringComparison.OrdinalIgnoreCase));

                if (metaData != null)
                {
                    using (var metaDataStream = metaData.Open())
                    {
                        var games         = metaDataStream.AsEnumerableXml("Game");
                        var xmlSerializer = new XmlSerializer(typeof(LaunchBox.Metadata.Game));

                        var i       = 0;
                        var context = new MetaDataContext(GetPluginUserDataPath());
                        context.ChangeTracker.AutoDetectChangesEnabled = false;

                        foreach (var xElement in games)
                        {
                            var gameMetaData =
                                (LaunchBox.Metadata.Game)xmlSerializer.Deserialize(xElement.CreateReader());
                            i++;
                            if (i++ > 1000)
                            {
                                context.SaveChanges();
                                i = 0;
                                context.Dispose();
                                context = new MetaDataContext(GetPluginUserDataPath());
                                context.ChangeTracker.AutoDetectChangesEnabled = false;
                            }

                            context.Games.Add(gameMetaData);
                        }

                        context.Dispose();
                    }
                }
            }
        }
        private Game GetGame()
        {
            if (_game is null)
            {
                var gameSearchName = "";
                if (!string.IsNullOrWhiteSpace(_options?.GameData?.Name))
                {
                    gameSearchName = _options.GameData.Name.Sanitize();
                }

                if (!string.IsNullOrWhiteSpace(gameSearchName))
                {
                    if (_options?.GameData != null && _regionPriority.Count < 1)
                    {
                        if (_options.GameData.Region != null && !string.IsNullOrWhiteSpace(_options.GameData.Region.Name))
                        {
                            _regionPriority = _options.GameData.Region.Name.GetRegionPriorityList();
                        }
                        else
                        {
                            if (!string.IsNullOrWhiteSpace(_options.GameData.GameImagePath))
                            {
                                var noIntoRegion = _options.GameData.GameImagePath.GetRegionNoIntro();
                                if (!string.IsNullOrWhiteSpace(noIntoRegion))
                                {
                                    _regionPriority = noIntoRegion.GetRegionPriorityList();
                                }
                            }
                        }
                    }

                    var platformSearchName = "";
                    if (!string.IsNullOrWhiteSpace(_options?.GameData?.Platform?.Name))
                    {
                        var sanitizedPlatform = _options.GameData.Platform.Name.Sanitize();
                        platformSearchName = _plugin.PlatformTranslationTable.ContainsKey(sanitizedPlatform)
                            ? _plugin.PlatformTranslationTable[sanitizedPlatform]
                            : sanitizedPlatform;
                    }

                    using (var context = new MetaDataContext(_plugin.GetPluginUserDataPath()))
                    {
                        /* Can't tell which region the actual game name is from in the game object...
                         * if (_regionPriority.Count > 0)
                         * {
                         *  var alternateNames = context.GameAlternateName.Where(game =>
                         *      game.Game.PlatformSearch == platformSearchName && (game.NameSearch == gameSearchName || game.Game.NameSearch == gameSearchName)).ToList();
                         *
                         *  var regionGameName = alternateNames.Where(game => _regionPriority.ContainsKey(game.Region ?? "")).OrderBy((n) =>
                         *  {
                         *      if (_regionPriority.ContainsKey(n.Region ?? ""))
                         *      {
                         *          return _regionPriority[n.Region ?? ""];
                         *      }
                         *
                         *      return int.MaxValue;
                         *  }).FirstOrDefault();
                         *
                         *  if (regionGameName != null)
                         *  {
                         *      _game = context.Games.FirstOrDefault(
                         *          game => game.DatabaseID == regionGameName.DatabaseID);
                         *
                         *      if (_game != null)
                         *      {
                         *          if (!string.IsNullOrWhiteSpace(regionGameName.AlternateName))
                         *          {
                         *              _game.Name = regionGameName.AlternateName;
                         *          }
                         *      }
                         *  }
                         * }
                         */
                        if (_game is null)
                        {
                            _game = context.Games.FirstOrDefault(game =>
                                                                 game.PlatformSearch == platformSearchName && (game.NameSearch == gameSearchName ||
                                                                                                               game.AlternateNames.Any(alternateName =>
                                                                                                                                       alternateName.NameSearch ==
                                                                                                                                       gameSearchName)));

                            if (_game?.NameSearch != null && _game?.NameSearch != gameSearchName)
                            {
                                var alternateGameNames = context.GameAlternateName.Where(alternateName =>
                                                                                         alternateName.DatabaseID == _game.DatabaseID && alternateName.NameSearch ==
                                                                                         gameSearchName);

                                var numberOfNames = alternateGameNames.Count();

                                if (numberOfNames > 0)
                                {
                                    var gameName = alternateGameNames.First();
                                    if (!string.IsNullOrWhiteSpace(gameName.AlternateName))
                                    {
                                        _game.Name = gameName.AlternateName;
                                    }

                                    if (numberOfNames < 2 && !string.IsNullOrWhiteSpace(gameName.Region))
                                    {
                                        if (_regionPriority.Count < 1)
                                        {
                                            _regionPriority = gameName.Region.GetRegionPriorityList();
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (_game != null && _regionPriority.Count < 1)
                    {
                        _regionPriority = LaunchBox.Region.GetRegionPriorityList(null);
                    }
                }
            }

            return(_game);
        }