Ejemplo n.º 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 WebClient())
            {
                // 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 HashSet <GDBRelease>();

                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;
        }
Ejemplo n.º 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 WebClient())
            {
                // 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 = decimal.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.");
                }
            }
        }