コード例 #1
0
        private async Task <ExpandedGame> GetMetadata(SdkModels.Game game)
        {
            var content  = new StringContent(Serialization.ToJson(game), Encoding.UTF8, MediaTypeNames.Application.Json);
            var response = await client.PostAsync(@"/igdb/metadata_v2", content);

            return(Serialization.FromJson <ServicesResponse <ExpandedGame> >(await response.Content.ReadAsStringAsync()).Data);
        }
コード例 #2
0
ファイル: MetadataTets.cs プロジェクト: xeddmc/Playnite
        public async Task SteamIdUseTest()
        {
            var steamGame = new SdkModels.Game
            {
                PluginId = Guid.Parse("CB91DFC9-B977-43BF-8E70-55F46E410FAB"),
                GameId   = "7200"
            };

            var metadata = await GetMetadata(steamGame);

            Assert.Equal("TrackMania United", metadata.name);
        }
コード例 #3
0
        public IgdbModels.ExpandedGame GetMetadata(SdkModels.Game game)
        {
            // Only serialize minimum amount of data needed
            var gameData = Serialization.ToJson(new Dictionary <string, object>
            {
                { nameof(SdkModels.Game.Name), game.Name },
                { nameof(SdkModels.Game.ReleaseDate), game.ReleaseDate },
                { nameof(SdkModels.Game.PluginId), game.PluginId },
                { nameof(SdkModels.Game.GameId), game.GameId }
            });

            return(ExecutePostRequest <IgdbModels.ExpandedGame>("/igdb/metadata_v2", gameData));
        }
コード例 #4
0
        private ulong MatchFun(SdkModels.Game game, string matchName, List <ExpandedGame> list)
        {
            var res = list.Where(a => string.Equals(matchName, a.name, StringComparison.InvariantCultureIgnoreCase));

            if (!res.Any())
            {
                res = list.Where(a =>
                                 a.alternative_names.HasItems() &&
                                 a.alternative_names.Select(b => b.name).ContainsString(matchName) == true);
            }

            if (res.Any())
            {
                if (res.Count() == 1)
                {
                    return(res.First().id);
                }
                else
                {
                    if (game.ReleaseDate != null)
                    {
                        var igdbGame = res.FirstOrDefault(a => a.first_release_date.ToDateFromUnixMs().Year == game.ReleaseDate.Value.Year);
                        if (igdbGame != null)
                        {
                            return(igdbGame.id);
                        }
                    }
                    else
                    {
                        // If multiple matches are found and we don't have release date then prioritize older game
                        if (res.All(a => a.first_release_date == 0))
                        {
                            return(res.First().id);
                        }
                        else
                        {
                            var igdbGame = res.OrderBy(a => a.first_release_date).First(a => a.first_release_date > 0);
                            return(igdbGame.id);
                        }
                    }
                }
            }

            return(0);
        }
コード例 #5
0
        public async Task ReleaseDateUseTest()
        {
            var game = new SdkModels.Game("Tomb Raider")
            {
                ReleaseDate = new DateTime(1996, 1, 1)
            };

            var metadata = await GetMetadata(game);

            Assert.Equal(1996, GetYearFromUnix(metadata.first_release_date));
            Assert.Equal("Core Design", metadata.involved_companies.Where(a => a.developer).First().company.name);

            game.ReleaseDate = new DateTime(2013, 1, 1);
            metadata         = await GetMetadata(game);

            Assert.Equal(2013, GetYearFromUnix(metadata.first_release_date));
            Assert.Equal("Crystal Dynamics", metadata.involved_companies.Where(a => a.developer).First().company.name);
        }
コード例 #6
0
ファイル: MetadataTets.cs プロジェクト: xeddmc/Playnite
        public async Task ReleaseDateUseTest()
        {
            var game = new SdkModels.Game("Tomb Raider")
            {
                ReleaseDate = new DateTime(1996, 1, 1)
            };

            var metadata = await GetMetadata(game);

            Assert.Equal(1996, metadata.first_release_date.ToDateFromUnixMs().Year);
            Assert.Equal("Core Design", metadata.developers[0]);

            game.ReleaseDate = new DateTime(2013, 1, 1);
            metadata         = await GetMetadata(game);

            Assert.Equal(2013, metadata.first_release_date.ToDateFromUnixMs().Year);
            Assert.Equal("Crystal Dynamics", metadata.developers[0]);
        }
コード例 #7
0
        public async Task <ServicesResponse <ExpandedGame> > Post([FromBody] SdkModels.Game game)
        {
            var   isKnownPlugin = game.PluginId != Guid.Empty;
            var   isSteamPlugin = BuiltinExtensions.GetIdFromExtension(BuiltinExtension.SteamLibrary) == game.PluginId;
            ulong igdbId        = 0;
            var   matchId       = $"{game.GameId}{game.PluginId}".MD5();
            var   searchId      = $"{game.Name}{game.ReleaseDate?.Year}".MD5();

            logger.Debug($"IGDB metadata: {game.GameId},{game.Name},{game.PluginId},{game.ReleaseDate}");

            // Check if match was previously found
            if (isKnownPlugin)
            {
                if (isSteamPlugin)
                {
                    igdbId = await GamesBySteamIdController.GetIgdbMatch(ulong.Parse(game.GameId));
                }
                else
                {
                    var match = Database.IGBDGameIdMatches.FindById(matchId);
                    if (match != null)
                    {
                        igdbId = match.IgdbId;
                    }
                }
            }

            if (igdbId == 0)
            {
                var match = Database.IGDBSearchIdMatches.FindById(searchId);
                if (match != null)
                {
                    igdbId = match.IgdbId;
                }
            }

            var foundMetadata = new ExpandedGame();

            if (igdbId != 0)
            {
                return(new ServicesResponse <ExpandedGame>(await GameParsedController.GetExpandedGame(igdbId)));
            }
            else
            {
                igdbId = await TryMatchGame(game);

                if (igdbId != 0)
                {
                    foundMetadata = await GameParsedController.GetExpandedGame(igdbId);
                }
            }

            // Update match database if match was found
            if (igdbId != 0)
            {
                if (isKnownPlugin && !isSteamPlugin)
                {
                    Database.IGBDGameIdMatches.Upsert(new GameIdMatch
                    {
                        GameId  = game.GameId,
                        Id      = matchId,
                        IgdbId  = igdbId,
                        Library = game.PluginId
                    });
                }

                Database.IGDBSearchIdMatches.Upsert(new SearchIdMatch
                {
                    Term   = game.Name,
                    Id     = searchId,
                    IgdbId = igdbId
                });
            }

            return(new ServicesResponse <ExpandedGame>(foundMetadata));
        }
コード例 #8
0
        private async Task <ulong> TryMatchGame(SdkModels.Game game)
        {
            if (game.Name.IsNullOrEmpty())
            {
                return(0);
            }

            ulong matchedGame = 0;
            var   copyGame    = game.GetClone();

            copyGame.Name = StringExtensions.NormalizeGameName(game.Name);
            var name = copyGame.Name;

            name = Regex.Replace(name, @"\s+RHCP$", "", RegexOptions.IgnoreCase);
            name = Regex.Replace(name, @"\s+RU$", "", RegexOptions.IgnoreCase);

            var results = await GamesController.GetSearchResults(name);

            results.ForEach(a => a.name = StringExtensions.NormalizeGameName(a.name));
            string testName = string.Empty;

            // Direct comparison
            matchedGame = MatchFun(game, name, results);
            if (matchedGame > 0)
            {
                return(matchedGame);
            }

            // Try replacing roman numerals: 3 => III
            testName    = Regex.Replace(name, @"\d+", ReplaceNumsForRomans);
            matchedGame = MatchFun(game, testName, results);
            if (matchedGame > 0)
            {
                return(matchedGame);
            }

            // Try adding The
            testName    = "The " + name;
            matchedGame = MatchFun(game, testName, results);
            if (matchedGame > 0)
            {
                return(matchedGame);
            }

            // Try chaning & / and
            testName    = Regex.Replace(name, @"\s+and\s+", " & ", RegexOptions.IgnoreCase);
            matchedGame = MatchFun(game, testName, results);
            if (matchedGame > 0)
            {
                return(matchedGame);
            }

            // Try removing apostrophes
            var resCopy = results.GetClone();

            resCopy.ForEach(a => a.name = a.name.Replace("'", ""));
            matchedGame = MatchFun(game, name, resCopy);
            if (matchedGame > 0)
            {
                return(matchedGame);
            }

            // Try removing all ":" and "-"
            testName = Regex.Replace(name, @"\s*(:|-)\s*", " ");
            resCopy  = results.GetClone();
            foreach (var res in resCopy)
            {
                res.name = Regex.Replace(res.name, @"\s*(:|-)\s*", " ");
                res.alternative_names?.ForEach(a => a.name = Regex.Replace(a.name, @"\s*(:|-)\s*", " "));
            }

            matchedGame = MatchFun(game, testName, resCopy);
            if (matchedGame > 0)
            {
                return(matchedGame);
            }

            // Try without subtitle
            var testResult = results.OrderBy(a => a.first_release_date).FirstOrDefault(a =>
            {
                if (a.first_release_date == 0)
                {
                    return(false);
                }

                if (!string.IsNullOrEmpty(a.name) && a.name.Contains(":"))
                {
                    return(string.Equals(name, a.name.Split(':')[0], StringComparison.InvariantCultureIgnoreCase));
                }

                return(false);
            });

            if (testResult != null)
            {
                return(testResult.id);
            }

            return(0);
        }
コード例 #9
0
        private async Task <ServicesResponse <T> > GetMetadata <T>(SdkModels.Game game, Func <ulong, Task <T> > expandFunc) where T : new()
        {
            var   isKnownPlugin = game.PluginId != Guid.Empty;
            var   isSteamPlugin = BuiltinExtensions.GetIdFromExtension(BuiltinExtension.SteamLibrary) == game.PluginId;
            ulong igdbId        = 0;
            var   matchId       = $"{game.GameId}{game.PluginId}".MD5();
            var   searchId      = $"{game.Name}{game.ReleaseDate?.Year}".MD5();

            // Check if match was previously found
            if (isKnownPlugin)
            {
                if (isSteamPlugin)
                {
                    igdbId = await igdbApi.GetSteamIgdbMatch(ulong.Parse(game.GameId));
                }
                else
                {
                    var match = Database.IGBDGameIdMatches.FindById(matchId);
                    if (match != null)
                    {
                        igdbId = match.IgdbId;
                    }
                }
            }

            if (igdbId == 0)
            {
                var match = Database.IGDBSearchIdMatches.FindById(searchId);
                if (match != null)
                {
                    igdbId = match.IgdbId;
                }
            }

            var foundMetadata = new T();

            if (igdbId != 0)
            {
                return(new ServicesResponse <T>(await expandFunc(igdbId)));
            }
            else
            {
                igdbId = await TryMatchGame(game, false);

                var useAlt = settings.Settings.IGDB.AlternativeSearch && !game.Name.ContainsAny(whereQueryBlacklist);
                if (useAlt && igdbId == 0)
                {
                    igdbId = await TryMatchGame(game, true);
                }

                if (igdbId != 0)
                {
                    foundMetadata = await expandFunc(igdbId);
                }
            }

            // Update match database if match was found
            if (igdbId != 0)
            {
                if (isKnownPlugin && !isSteamPlugin)
                {
                    Database.IGBDGameIdMatches.Upsert(new GameIdMatch
                    {
                        GameId  = game.GameId,
                        Id      = matchId,
                        IgdbId  = igdbId,
                        Library = game.PluginId
                    });
                }

                Database.IGDBSearchIdMatches.Upsert(new SearchIdMatch
                {
                    Term   = game.Name,
                    Id     = searchId,
                    IgdbId = igdbId
                });
            }

            return(new ServicesResponse <T>(foundMetadata));
        }
コード例 #10
0
 public async Task <ServicesResponse <ExpandedGameLegacy> > PostMetadata([FromBody] SdkModels.Game game)
 {
     return(await GetMetadata(game, parsedController.GetExpandedGame));
 }