示例#1
0
    /// <summary>
    /// Implement IDB.CachePlatfomrReleases(). Go out to gamesdb.com and cache all known releases for the specified platform. Update the list of releases and store metadata for each one.
    /// </summary>
    /// <param name="platform">Robin.Platform associated with the GDBPlatform to cache.</param>
    public void CachePlatformReleases(Platform platform)
    {
        Reporter.Tic($"Getting {platform.Title} release list from Games DB...", out int tic1);
        GDBPlatform GDBPlatform = platform.GDBPlatform;

        // Update list of GDBReleases for this platform from xml file
        using (WebClient webclient = new())
        {
            // API to get xml file containing all gamesdb releases for this platform.
            string url = $"{baseUrl}GetPlatformGames.php?platform={GDBPlatform.ID}";

            // Put existing GDBReleases in a dictionary for lookup performance
            var existingGDBReleaseDict          = R.Data.GDBReleases.ToDictionary(x => x.ID);
            HashSet <GDBRelease> newGDBReleases = new();

            if (webclient.SafeDownloadStringDB(url, out string downloadText))
            {
                XDocument xDocument = XDocument.Parse(downloadText);

                foreach (XElement element in xDocument.Root.Elements("Game"))
                {
                    // Don't create this game if the title is null
                    string title = element.Element("GameTitle")?.Value;
                    if (string.IsNullOrEmpty(title))
                    {
                        continue;
                    }

                    // Check if gbdRelease exists before creating new one. Whether it exists or not, overwrite properties with properties from xml file.
                    long id = long.Parse(element.Element("id")?.Value);
                    if (!existingGDBReleaseDict.TryGetValue(id, out GDBRelease GDBRelease))
                    {
                        GDBRelease = new GDBRelease {
                            ID = id
                        };
                        newGDBReleases.Add(GDBRelease);
                    }

                    GDBRelease.Title = title;
                    GDBRelease.Date  = DateTimeRoutines.SafeGetDate(element.SafeGetA("ReleaseDate") ?? "01-01-1901");

                    // If a release has changed platforms, catch it and zero out match
                    if (GDBRelease.GDBPlatform_ID != GDBPlatform.ID)
                    {
                        GDBRelease.GDBPlatform_ID = GDBPlatform.ID;
                        Release release = R.Data.Releases.FirstOrDefault(x => x.ID_GDB == id);
                        if (release != null)
                        {
                            release.ID_GDB = null;
                        }
                    }
                }
            }
            GDBPlatform.GDBReleases.UnionWith(newGDBReleases);
            Reporter.Toc(tic1);
        }

        // Temporarily set wait time to 1 ms while caching tens of thousands of games. Sorry GDB.
        int waitTimeHolder = DBTimers.GamesDB.WaitTime;

        DBTimers.GamesDB.WaitTime = 1;

        int releaseCount = GDBPlatform.GDBReleases.Count;
        int i            = 0;

        // Cache metadata for each individual game
        foreach (GDBRelease GDBRelease in GDBPlatform.GDBReleases)
        {
            if (releaseCount / 10 != 0 && i++ % (releaseCount / 10) == 0)
            {
                Reporter.Report($"{i} / {releaseCount}");
            }

            CacheReleaseData(GDBRelease);
        }
        DBTimers.GamesDB.WaitTime = waitTimeHolder;
    }
示例#2
0
    /// <summary>
    /// Cache metadata from gamesdb.com API for a GDBRelease.
    /// </summary>
    /// <param name="GDBRelease">GDBRelease whose metadat is to be cached.</param>
    public void CacheReleaseData(GDBRelease GDBRelease)
    {
        // URL of gamesdb API to cache metadata for one release
        string url = $"{baseUrl}GetGame.php?id=" + GDBRelease.ID;

        using WebClient webclient = new();
        // Pull down the xml file containing game data from gamesdb
        if (webclient.SafeDownloadStringDB(url, out string downloadText))
        {
            XDocument xDocument = XDocument.Parse(downloadText);

            GDBRelease.Title     = xDocument.SafeGetB("Game", "GameTitle") ?? GDBRelease.Title;
            GDBRelease.Developer = xDocument.SafeGetB("Game", "Developer") ?? GDBRelease.Developer;
            GDBRelease.Publisher = xDocument.SafeGetB("Game", "Publisher") ?? GDBRelease.Publisher;
            GDBRelease.Players   = xDocument.SafeGetB("Game", "Players") ?? GDBRelease.Players;
            GDBRelease.Overview  = xDocument.SafeGetB("Game", "Overview") ?? GDBRelease.Overview;

            GDBRelease.Rating = double.Parse(xDocument.SafeGetB("Game", "Rating") ?? "0", CultureInfo.InvariantCulture);
            GDBRelease.Genre  = string.Join(",", xDocument.Root.Descendants("genre").Select(x => x.Value));
            GDBRelease.Date   = DateTimeRoutines.SafeGetDate(xDocument.SafeGetB("Game", "ReleaseDate"));

            string coop = xDocument.SafeGetB("Game", "Co-op");
            if ((coop != null) && ((coop.ToLower() == "true") || (coop.ToLower() == "yes")))
            {
                GDBRelease.Coop = true;
            }
            else
            {
                GDBRelease.Coop = false;
            }

            string BaseImageUrl = xDocument.SafeGetB("baseImgUrl");

            if (BaseImageUrl != null)
            {
                url = xDocument.SafeGetBoxArt("front");
                if (url != null)
                {
                    GDBRelease.BoxFrontUrl = BaseImageUrl + url;
                }

                url = xDocument.SafeGetBoxArt("back");
                if (url != null)
                {
                    GDBRelease.BoxBackUrl = BaseImageUrl + url;
                }

                url = xDocument.SafeGetB("Game", "Images", "banner");
                if (url != null)
                {
                    GDBRelease.BannerUrl = BaseImageUrl + url;
                }

                url = xDocument.SafeGetB("Game", "Images", "screenshot", "original");
                if (url != null)
                {
                    GDBRelease.ScreenUrl = BaseImageUrl + url;
                }

                url = xDocument.SafeGetB("Game", "Images", "clearlogo");
                if (url != null)
                {
                    GDBRelease.LogoUrl = BaseImageUrl + url;
                }
            }
        }
        else
        {
            Reporter.Report("Failure getting " + GDBRelease.Title + ", ID " + GDBRelease.ID + " from Games DB.");
        }
    }