Exemplo n.º 1
0
    public void CachePlatformImages(Platform platform)
    {
        if (launchboxFile == null)
        {
            GetLaunchBoxFile();
        }
#if DEBUG
        int i = 0;
#endif
        Dictionary <string, LBImage> existingLBImageDict = R.Data.LBImages.ToDictionary(x => x.FileName);

        Reporter.Report("Caching " + platform.LBPlatform.Title + " images.");
        int j         = 0;
        int gameCount = platform.LBPlatform.LBGames.Count;

        foreach (LBGame LBGame in platform.LBPlatform.LBGames)
        {
            // Reporting only
            if ((gameCount / 10) != 0 && ++j % (gameCount / 10) == 0)
            {
                Reporter.Report($"  Working {j} / {gameCount} {platform.LBPlatform.Title} games in the local cache.");
            }
#if DEBUG
            Stopwatch watch1 = Stopwatch.StartNew();
#endif
            var gameImageElements = imageElementLookupByGameID[LBGame.ID.ToString()];
#if DEBUG
            Debug.WriteLine("Game: " + watch1.ElapsedMilliseconds); watch1.Restart();
#endif
            // Cache images for this game from the launchbox file
            foreach (XElement imageElement in gameImageElements)
            {
                string fileName = imageElement.Element("FileName")?.Value;

                // Check if image already exists in the local cache before creating a new one. Whether new or old, overwrite properties.
                if (!existingLBImageDict.TryGetValue(fileName, out LBImage LBImage))
                {
                    LBImage = new LBImage {
                        FileName = fileName
                    };
                }

                LBImage.Type = imageElement.Element("Type")?.Value ?? LBImage.Type;

                string regionText = imageElement.Element("Region")?.Value;
                long   regionID;
                if (regionText == null)
                {
                    regionID = CONSTANTS.Region_ID.Unk;
                }

                else if (!RegionDictionary.TryGetValue(regionText, out regionID))
                {
                    regionID = CONSTANTS.Region_ID.Unk;
                    Reporter.Report("Couldn't find {regionText} in the region dictionary.");
                }
#if DEBUG
                Debug.WriteLine("IB: " + watch1.ElapsedMilliseconds); watch1.Restart();
#endif
                // Create a release to hold the image or attach it to it
                LBRelease LBRelease = LBGame.LBReleases.FirstOrDefault(x => x.Region_ID == regionID);
                if (LBRelease == null)
                {
                    LBRelease = new LBRelease();
                    LBGame.LBReleases.Add(LBRelease);
                    LBRelease.Region_ID = regionID;
                    LBRelease.Title     = LBGame.Title;
                }
#if DEBUG
                Debug.WriteLine("IC: " + watch1.ElapsedMilliseconds); watch1.Restart();
#endif
                // This is a hack to avoid trying to add the image to multiple releases, which will bonk
                // because the foreign key relation is 1 or 0. The correct answer is to make this many-to-many,
                // but that seems like a pain in the ass since there are very few images related to more than one release.
                if (LBImage.LBRelease == null)
                {
                    LBRelease.LBImages.Add(LBImage);
                }

#if DEBUG
                Debug.WriteLine("ID: " + watch1.ElapsedMilliseconds); watch1.Restart();
                Debug.WriteLine($"Image #: {i++}.");
#endif
            }
        }
    }
Exemplo n.º 2
0
    public void CachePlatformReleases(Platform platform)
    {
        Reporter.Tic($"Cache {platform.LBPlatform.Title} releases begun...", out int tic1);
        CachePlatformGamesAsync(platform);

        if (launchboxFile == null)
        {
            GetLaunchBoxFile();
        }

        // Create a dictionary of existing LBReleases to speed lookups
        //Dictionary<long, LBRelease> existingLBReleaseDict = R.Data.LBReleases.ToDictionary(x => x.ID);

        // Create a Hashset of LBReleases to store any new LBReleases that we discover
        //HashSet<LBRelease> newLBReleases = new HashSet<LBRelease>();

        int gameCount = platform.LBPlatform.LBGames.Count;
        int j         = 0;

        foreach (LBGame LBGame in platform.LBPlatform.LBGames)
        {
            // Reporting only
            if ((gameCount / 10) != 0 && ++j % (gameCount / 10) == 0)
            {
                Reporter.Report($"  Working {j} / {gameCount} {platform.LBPlatform.Title} games.");
            }

            var gameReleaseElements = releaseElementLookupByGameID[LBGame.ID.ToString()];

            // Cache releases for this game from the launchbox file
            foreach (XElement releaseElement in gameReleaseElements)
            {
                string regionText = releaseElement.Element("Region")?.Value;
                long   regionID;

                if (regionText == null)
                {
                    regionID = CONSTANTS.Region_ID.Unk;
                }

                else if (!RegionDictionary.TryGetValue(regionText, out regionID))
                {
                    regionID = CONSTANTS.Region_ID.Unk;
                    Reporter.Report($"Couldn't find {regionText} in LB image dictionary.");
                }
#if DEBUG
                Stopwatch watch1 = Stopwatch.StartNew();
#endif
                LBRelease LBRelease = LBGame.LBReleases.FirstOrDefault(x => x.Region_ID == regionID);
#if DEBUG
                Debug.WriteLine($"RA: " + watch1.ElapsedMilliseconds); watch1.Restart();
#endif
                if (LBRelease == null)
                {
                    LBRelease = new LBRelease();
                    LBGame.LBReleases.Add(LBRelease);
                    LBRelease.Region_ID = regionID;
                }

                LBRelease.Title = releaseElement.Element("AlternateName").Value;
            }
        }
        CachePlatformImages(platform);
        Reporter.Toc(tic1, $"Cache {platform.LBPlatform.Title} releases finished.");
    }