private async Task <ImageSource> LoadAssetAsync(string itemName, Task <ImageSource> defaultImage) { var fileName = string.Format(AssetPathFormat, itemName); if (!File.Exists(fileName)) { if (_options.DownloadMissingItemImages) { await _wikiLoader.ProduceAsync(itemName, fileName).ConfigureAwait(false); } else { return(await defaultImage.ConfigureAwait(false)); } } return(BitmapImageFactory.Create(fileName)); }
/// <summary> /// Returns the image for the ItemClass. Each ItemClass image is only loaded once. /// </summary> public ImageSource LoadDefaultImage(ItemClass itemClass) { return(_defaultImageCache.GetOrAdd(itemClass, c => { if (Application.Current == null) { return _errorImage; } try { var path = string.Format(ResourcePathFormat, c); return BitmapImageFactory.Create(path); } catch (Exception e) { Log.Warn(e, "Could not load default file for ItemClass " + c); return _errorImage; } })); }
private async Task <ImageSource> LoadOfficialAsync(string imageUrl, Task <ImageSource> defaultImage) { // Remove the query part, remove the host part, remove image/Art/2DItems/, trim slashes var relevantPart = Regex.Replace(imageUrl, @"(\?.*)|(.*(\.net|\.com)/)|(image/Art/2DItems/)", "").Trim('/'); var match = Regex.Match(relevantPart, @"gen/image/.*?/([a-zA-Z0-9]*)/Item\.png"); var isRelic = imageUrl.Contains("&relic=1"); if (match.Success) { // These names are too long. // They contain groups of 20 chars (as folders) and end with a unique identifier. relevantPart = $"gen/{match.Groups[1]}.png"; } else if (isRelic) { // Non-generated images for relics and normal uniques have the same url, the relic flag is in the // query part. As the query is removed, the file name needs to be adjusted for relics. relevantPart = relevantPart.Replace(".png", "Relic.png"); } var fileName = string.Format(DownloadedPathFormat, relevantPart); if (!File.Exists(fileName)) { if (!_options.DownloadMissingItemImages) { return(await defaultImage.ConfigureAwait(false)); } var imgData = await _httpClient.GetByteArrayAsync(imageUrl).ConfigureAwait(false); CreateDirectories(fileName); WikiApiUtils.SaveImage(imgData, fileName, false); Log.Info($"Downloaded item image {fileName} to the file system."); } return(BitmapImageFactory.Create(fileName)); }