/// <summary>Requests the image for a given locator.</summary> public virtual void RequestModGalleryImage(int modId, GalleryImageLocator locator, ModGalleryImageSize size, Action <Texture2D> onImageReceived, Action <Texture2D> onFallbackFound, Action <WebRequestError> onError) { Debug.Assert(locator != null); Debug.Assert(onImageReceived != null); // set loading function Func <Texture2D> loadFromDisk = () => CacheClient.LoadModGalleryImage(modId, locator.GetFileName(), size); // set saving function Action <Texture2D> saveToDisk = null; if (this.storeIfSubscribed) { saveToDisk = (t) => { if (LocalUser.SubscribedModIds.Contains(modId)) { CacheClient.SaveModGalleryImage(modId, locator.GetFileName(), size, t); } }; } // do the work this.RequestImage_Internal(locator, size, loadFromDisk, saveToDisk, onImageReceived, onFallbackFound, onError); }
private void MediaPreview_GalleryImage(ImageDisplayComponent display) { ImageDisplayData imageData = display.data; selectedMediaPreview.data = imageData; if (imageData.GetImageTexture(selectedMediaPreview.useOriginal) == null) { bool original = selectedMediaPreview.useOriginal; ModGalleryImageSize size = (original ? ModGalleryImageSize.Original : ImageDisplayData.galleryThumbnailSize); ModManager.GetModGalleryImage(profile, display.data.fileName, size, (t) => { if (Application.isPlaying && selectedMediaPreview.data.Equals(imageData)) { imageData.SetImageTexture(original, t); selectedMediaPreview.data = imageData; } }, WebRequestError.LogAsWarning); } }
// TODO(@jackson): Take ModMediaCollection instead of profile public static ImageRequest DownloadModGalleryImage(ModProfile profile, string imageFileName, ModGalleryImageSize size) { Debug.Assert(profile != null, "[mod.io] Profile parameter cannot be null"); Debug.Assert(!String.IsNullOrEmpty(imageFileName), "[mod.io] imageFileName parameter needs to be not null or empty (used as identifier for gallery images)"); ImageRequest request = null; if (profile.media == null) { Debug.LogWarning("[mod.io] The given mod profile has no media information"); } else { GalleryImageLocator locator = profile.media.GetGalleryImageWithFileName(imageFileName); if (locator == null) { Debug.LogWarning("[mod.io] Unable to find mod gallery image with the file name \'" + imageFileName + "\' for the mod profile \'" + profile.name + "\'[" + profile.id + "]"); } else { request = DownloadClient.DownloadModGalleryImage(locator, size); } } return(request); }
public static void GetModGalleryImage(ModProfile profile, string imageFileName, ModGalleryImageSize size, Action <Texture2D> onSuccess, Action <WebRequestError> onError) { var cachedImageTexture = CacheClient.LoadModGalleryImage(profile.id, imageFileName, size); if (cachedImageTexture != null) { if (onSuccess != null) { onSuccess(cachedImageTexture); } } else { // - Fetch from Server - var download = DownloadClient.DownloadModGalleryImage(profile, imageFileName, size); download.succeeded += (d) => { CacheClient.SaveModGalleryImage(profile.id, imageFileName, size, d.imageTexture); }; download.succeeded += (d) => onSuccess(d.imageTexture); download.failed += (d) => onError(d.error); } }
/// <summary>Generates the file path for a mod galley image.</summary> public static string GenerateModGalleryImageFilePath(int modId, string imageFileName, ModGalleryImageSize size) { return(IOUtilities.CombinePath(GenerateModMediaDirectoryPath(modId), "images_" + size.ToString(), Path.GetFileNameWithoutExtension(imageFileName) + ".png")); }
public static string GenerateModGalleryImageFilePath(int modId, string imageFileName, ModGalleryImageSize size) { return(GenerateModGalleryImageDirectoryPath(modId) + size.ToString() + "/" + Path.GetFileNameWithoutExtension(imageFileName) + ".png"); }
public static Texture2D LoadModGalleryImage(int modId, string imageFileName, ModGalleryImageSize size) { string imageFilePath = CacheClient.GenerateModGalleryImageFilePath(modId, imageFileName, size); Texture2D imageTexture = CacheClient.ReadImageFile(imageFilePath); return(imageTexture); }
public static ImageRequest DownloadModGalleryImage(GalleryImageLocator imageLocator, ModGalleryImageSize size) { Debug.Assert(imageLocator != null, "[mod.io] imageLocator parameter cannot be null."); Debug.Assert(!String.IsNullOrEmpty(imageLocator.fileName), "[mod.io] imageFileName parameter needs to be not null or empty (used as identifier for gallery images)"); ImageRequest request = null; request = DownloadImage(imageLocator.GetSizeURL(size)); return(request); }
/// <summary>Retrieves a mod gallery image from the cache.</summary> public static Texture2D LoadModGalleryImage(int modId, string imageFileName, ModGalleryImageSize size) { Debug.Assert(!String.IsNullOrEmpty(imageFileName)); string imageFilePath = CacheClient.GenerateModGalleryImageFilePath(modId, imageFileName, size); Texture2D imageTexture = IOUtilities.ReadImageFile(imageFilePath); return(imageTexture); }
public static void SaveModGalleryImage(int modId, string imageFileName, ModGalleryImageSize size, Texture2D imageTexture) { Debug.Assert(modId > 0, "[mod.io] Cannot cache a mod image without a mod id"); string imageFilePath = CacheClient.GenerateModGalleryImageFilePath(modId, imageFileName, size); CacheClient.WritePNGFile(imageFilePath, imageTexture); }
protected virtual void OnModGalleryImageUpdated(int modId, string imageFileName, ModGalleryImageSize size, Texture2D texture) { if (profile != null && profile.id == modId && size == IMAGE_PREVIEW_SIZE && textureCache.ContainsKey(imageFileName)) { textureCache[imageFileName] = texture; isRepaintRequired = true; } }
/// <summary>Stores a mod gallery image in the cache.</summary> public static bool SaveModGalleryImage(int modId, string imageFileName, ModGalleryImageSize size, Texture2D imageTexture) { Debug.Assert(!String.IsNullOrEmpty(imageFileName)); Debug.Assert(imageTexture != null); string imageFilePath = CacheClient.GenerateModGalleryImageFilePath(modId, imageFileName, size); return(IOUtilities.WritePNGFile(imageFilePath, imageTexture)); }
/// <summary>Stores a mod gallery image in the cache.</summary> public static bool SaveModGalleryImage(int modId, string imageFileName, ModGalleryImageSize size, Texture2D imageTexture) { Debug.Assert(!String.IsNullOrEmpty(imageFileName)); Debug.Assert(imageTexture != null); string imageFilePath = CacheClient.GenerateModGalleryImageFilePath(modId, imageFileName, size); byte[] imageData = imageTexture.EncodeToPNG(); return(LocalDataStorage.WriteFile(imageFilePath, imageData)); }
public static ImageRequest DownloadModGalleryImage(ModProfile profile, string imageFileName, ModGalleryImageSize size) { ImageRequest request = new ImageRequest(); string imageURL = profile.media.GetGalleryImageWithFileName(imageFileName).GetSizeURL(size); UnityWebRequest webRequest = UnityWebRequest.Get(imageURL); webRequest.downloadHandler = new DownloadHandlerTexture(true); var operation = webRequest.SendWebRequest(); operation.completed += (o) => DownloadClient.OnImageDownloadCompleted(operation, request); return(request); }
/// <summary>Retrieves a mod gallery image from the cache.</summary> public static Texture2D LoadModGalleryImage(int modId, string imageFileName, ModGalleryImageSize size) { Debug.Assert(!String.IsNullOrEmpty(imageFileName)); string imageFilePath = CacheClient.GenerateModGalleryImageFilePath(modId, imageFileName, size); byte[] imageData; if (LocalDataStorage.ReadFile(imageFilePath, out imageData) && imageData != null) { return(IOUtilities.ParseImageData(imageData)); } else { return(null); } }
public void DisplayGalleryImage(int modId, GalleryImageLocator locator) { Debug.Assert(locator != null); bool original = m_useOriginal; ModGalleryImageSize size = (original ? ModGalleryImageSize.Original : ImageDisplayData.galleryThumbnailSize); ImageDisplayData displayData = new ImageDisplayData() { modId = modId, mediaType = ImageDisplayData.MediaType.ModGalleryImage, fileName = locator.fileName, originalTexture = null, thumbnailTexture = null, }; m_data = displayData; DisplayLoading(); ModManager.GetModGalleryImage(displayData.modId, locator, size, (t) => { if (!Application.isPlaying) { return; } if (m_data.Equals(displayData)) { m_data.SetImageTexture(original, t); PresentData(); } }, WebRequestError.LogAsWarning); }
/// <summary>Requests the image for a given ImageDisplayData.</summary> public virtual void RequestImageForData(ImageDisplayData data, bool original, Action <Texture2D> onSuccess, Action <WebRequestError> onError) { string url = data.GetImageURL(original); // asserts Debug.Assert(onSuccess != null); Debug.Assert(!string.IsNullOrEmpty(url)); // create delegates Func <Texture2D> loadFromDisk = null; Action <Texture2D> saveToDisk = null; switch (data.descriptor) { case ImageDescriptor.ModLogo: { LogoSize size = (original ? LogoSize.Original : ImageDisplayData.logoThumbnailSize); loadFromDisk = () => CacheClient.LoadModLogo(data.ownerId, data.imageId, size); if (this.storeIfSubscribed) { saveToDisk = (t) => { if (LocalUser.SubscribedModIds.Contains(data.ownerId)) { CacheClient.SaveModLogo(data.ownerId, data.imageId, size, t); } }; } } break; case ImageDescriptor.ModGalleryImage: { ModGalleryImageSize size = (original ? ModGalleryImageSize.Original : ImageDisplayData.galleryThumbnailSize); loadFromDisk = () => CacheClient.LoadModGalleryImage(data.ownerId, data.imageId, size); if (this.storeIfSubscribed) { saveToDisk = (t) => { if (LocalUser.SubscribedModIds.Contains(data.ownerId)) { CacheClient.SaveModGalleryImage(data.ownerId, data.imageId, size, t); } }; } } break; case ImageDescriptor.YouTubeThumbnail: { loadFromDisk = () => CacheClient.LoadModYouTubeThumbnail(data.ownerId, data.imageId); if (this.storeIfSubscribed) { saveToDisk = (t) => { if (LocalUser.SubscribedModIds.Contains(data.ownerId)) { CacheClient.SaveModYouTubeThumbnail(data.ownerId, data.imageId, t); } }; } } break; case ImageDescriptor.UserAvatar: { UserAvatarSize size = (original ? UserAvatarSize.Original : ImageDisplayData.avatarThumbnailSize); loadFromDisk = () => CacheClient.LoadUserAvatar(data.ownerId, size); } break; } // request image this.RequestImage_Internal(url, loadFromDisk, saveToDisk, onSuccess, onError); }
/// <summary>Requests the image for a given locator.</summary> public virtual void RequestModGalleryImage(int modId, GalleryImageLocator locator, ModGalleryImageSize size, Action <Texture2D> onImageReceived, Action <Texture2D> onFallbackFound, Action <WebRequestError> onError) { Debug.Assert(locator != null); Debug.Assert(onImageReceived != null); string url = locator.GetSizeURL(size); string fileName = locator.GetFileName(); // check cache and existing callbacks if (this.TryGetCacheOrSetCallbacks(url, onImageReceived, onFallbackFound, onError)) { return; } // - Start new request - Callbacks callbacks = this.CreateCallbacksEntry(url, onImageReceived, onError); // check for fallback callbacks.fallback = this.FindFallbackTexture(locator); if (onFallbackFound != null && callbacks.fallback != null) { onFallbackFound.Invoke(callbacks.fallback); } // add save function to download callback if (this.storeIfSubscribed) { callbacks.onTextureDownloaded = (texture) => { if (LocalUser.SubscribedModIds.Contains(modId)) { CacheClient.SaveModGalleryImage(modId, fileName, size, texture, null); } }; } // start process by checking the cache CacheClient.LoadModGalleryImage(modId, fileName, size, (texture) => { if (this == null) { return; } if (texture != null) { this.OnRequestSucceeded(url, texture); } else { // do the download this.DownloadImage(url); } }); }