예제 #1
0
        string GetURL(string type)
        {
            LBImage lbImage = LBImages.FirstOrDefault(x => x.Type == type);

            if (lbImage != null)
            {
                return(Launchbox.IMAGESURL + lbImage.FileName);
            }

            else
            {
                return(null);
            }
        }
예제 #2
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.UNKNOWN_REGION_ID;
                    }

                    else if (!RegionDictionary.TryGetValue(regionText, out regionID))
                    {
                        regionID = CONSTANTS.UNKNOWN_REGION_ID;
                        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
                }
            }
        }