Example #1
0
    /// <summary>
    /// Implements IDB.CachePlatformdata() Update the local DB cache of platform associated metadata
    /// </summary>
    /// <param name="platform">Robin.Platform associated with the DBPlatorm to update.</param>
    public void CachePlatformData(Platform platform)
    {
        Reporter.Tic("Getting " + platform.Title + " data from Games DB...", out int tic1);

        GDBPlatform GDBPlatform = platform.GDBPlatform;

        XDocument xdoc;
        string    url;

        string urlbase = $"{baseUrl}GetPlatform.php?id=";

        using (WebClient webclient = new())
        {
            // Assemble the platformsdb url from the platform data and the base API url
            url = urlbase + GDBPlatform.ID;

            // Pull down the xml file containing platform data from gamesdb
            if (webclient.SafeDownloadStringDB(url, out string downloadtext))
            {
                xdoc = XDocument.Parse(downloadtext);

                GDBPlatform.Title        = xdoc.SafeGetB("Platform", "Platform");
                GDBPlatform.Developer    = xdoc.SafeGetB("Platform", "developer");
                GDBPlatform.Manufacturer = xdoc.SafeGetB("Platform", "manufacturer");
                GDBPlatform.Cpu          = xdoc.SafeGetB("Platform", "cpu");
                GDBPlatform.Sound        = xdoc.SafeGetB("Platform", "sound");
                GDBPlatform.Display      = xdoc.SafeGetB("Platform", "display");
                GDBPlatform.Media        = xdoc.SafeGetB("Platform", "media");
                GDBPlatform.Controllers  = xdoc.SafeGetB("Platform", "maxcontrollers");
                GDBPlatform.Rating       = double.Parse(xdoc.SafeGetB("Platform", "rating") ?? "0");
                GDBPlatform.Overview     = xdoc.SafeGetB("Platform", "overview");

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

                if (BaseImageUrl != null)
                {
                    string BoxFrontUrl   = xdoc.SafeGetBoxArt("front", type: "Platform");
                    string BoxBackUrl    = xdoc.SafeGetBoxArt("back", type: "Platform");
                    string BannerUrl     = xdoc.SafeGetB("Platform", "Images", "banner");
                    string ConsoleUrl    = xdoc.SafeGetB("Platform", "Images", "consoleart");
                    string ControllerUrl = xdoc.SafeGetB("Platform", "Images", "controllerart");

                    GDBPlatform.BoxFrontUrl   = BoxFrontUrl != null ? BaseImageUrl + BoxFrontUrl : null;
                    GDBPlatform.BoxBackUrl    = BoxBackUrl != null ? BaseImageUrl + BoxBackUrl : null;
                    GDBPlatform.BannerUrl     = BannerUrl != null ? BaseImageUrl + BannerUrl : null;
                    GDBPlatform.ConsoleUrl    = ConsoleUrl != null ? BaseImageUrl + ConsoleUrl : null;
                    GDBPlatform.ControllerUrl = ControllerUrl != null ? BaseImageUrl + ControllerUrl : null;
                }
            }
            else
            {
                Reporter.Warn("Failure getting " + GDBPlatform.Title + " data from Games DB.");
            }
        }
        Reporter.Toc(tic1);
    }
Example #2
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;
    }