string GetURL(string type) { LBImage LBImage = LBImages.FirstOrDefault(x => x.Type == type); if (LBImage != null) { return(Launchbox.IMAGESURL + LBImage.FileName); } else { return(null); } }
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 } } }