private static void ExpireCacheEntries()
 {
     while (CacheFull)
     {
         string key = null;
         CachedAssetStoreImage image = null;
         foreach (KeyValuePair <string, CachedAssetStoreImage> pair in CachedAssetStoreImages)
         {
             if ((image == null) || (image.lastUsed > pair.Value.lastUsed))
             {
                 image = pair.Value;
                 key   = pair.Key;
             }
         }
         CachedAssetStoreImages.Remove(key);
         AssetStorePreviewManager instance = Instance;
         instance.m_CacheRemove++;
         if (image == null)
         {
             Debug.LogError("Null entry found while removing cache entry");
             break;
         }
         if (image.client != null)
         {
             image.client.Abort();
             image.client = null;
         }
         if (image.image != null)
         {
             UnityEngine.Object.DestroyImmediate(image.image);
         }
     }
 }
 // Make room for image if needed (because of cache limits)
 private static void ExpireCacheEntries()
 {
     while (CacheFull)
     {
         string oldestUrl = null;
         CachedAssetStoreImage oldestEntry = null;
         foreach (KeyValuePair <string, CachedAssetStoreImage> kv in CachedAssetStoreImages)
         {
             if (oldestEntry == null || oldestEntry.lastUsed > kv.Value.lastUsed)
             {
                 oldestEntry = kv.Value;
                 oldestUrl   = kv.Key;
             }
         }
         CachedAssetStoreImages.Remove(oldestUrl);
         Instance.m_CacheRemove++;
         if (oldestEntry == null)
         {
             Debug.LogError("Null entry found while removing cache entry");
             break;
         }
         if (oldestEntry.client != null)
         {
             oldestEntry.client.Abort();
             oldestEntry.client = null;
         }
         if (oldestEntry.image != null)
         {
             Object.DestroyImmediate(oldestEntry.image);
         }
     }
 }
 public static void AbortOlderThan(double timestamp)
 {
     foreach (KeyValuePair <string, CachedAssetStoreImage> pair in CachedAssetStoreImages)
     {
         CachedAssetStoreImage image = pair.Value;
         if ((image.lastUsed < timestamp) && (image.client != null))
         {
             image.requestedWidth = -1;
             image.client.Abort();
             image.client = null;
         }
     }
     Instance.m_ConvertedThisTick = 0;
 }
 private static CachedAssetStoreImage RenderEntry(CachedAssetStoreImage cached, GUIStyle labelStyle, GUIStyle iconStyle)
 {
     if ((cached.label != null) && (cached.image != null))
     {
         Texture2D image = cached.image;
         cached.image = new Texture2D(cached.requestedWidth, cached.requestedWidth, TextureFormat.RGB24, false, true);
         ScaleImage(cached.requestedWidth, cached.requestedWidth, image, cached.image, iconStyle);
         UnityEngine.Object.DestroyImmediate(image);
         cached.label = null;
         AssetStorePreviewManager instance = Instance;
         instance.m_ConvertedThisTick++;
     }
     return(cached);
 }
        /*
         * When the new GUI system is is place this should render the label to the cached icon
         * to speed up rendering. For now we just scale the incoming image and render the label
         * separately
         */
        private static CachedAssetStoreImage RenderEntry(CachedAssetStoreImage cached, GUIStyle labelStyle, GUIStyle iconStyle)
        {
            if (cached.label == null || cached.image == null)
            {
                return(cached);
            }
            Texture2D tmp = cached.image;

            cached.image = new Texture2D(cached.requestedWidth, cached.requestedWidth, TextureFormat.RGB24, false, true);
            ScaleImage(cached.requestedWidth, cached.requestedWidth, tmp, cached.image, iconStyle);
            // Compressing creates artifacts on the images
            // cached.image.Compress(true);
            Object.DestroyImmediate(tmp);
            cached.label = null;
            Instance.m_ConvertedThisTick++;
            return(cached);
        }
        // Abort fetching all reviews that haven't been used since timestamp
        public static void AbortOlderThan(double timestamp)
        {
            foreach (KeyValuePair <string, CachedAssetStoreImage> kv in AssetStorePreviewManager.CachedAssetStoreImages)
            {
                CachedAssetStoreImage entry = kv.Value;
                if (entry.lastUsed >= timestamp || entry.client == null)
                {
                    continue;
                }
                entry.requestedWidth = -1;
                entry.client.Abort();
                entry.client = null;
            }

            // @TODO: Currently we know that AbortOlderThan is called exactly once each repaint.
            //        Therefore this counter can be reset here. Should probably be moved to a
            //        more intuitive location.
            Instance.m_ConvertedThisTick = 0;
        }
        private static AsyncHTTPClient SetupTextureDownload(CachedAssetStoreImage cached, string url, string tag)
        {
            AsyncHTTPClient client = new AsyncHTTPClient(url);

            cached.client       = client;
            client.tag          = tag;
            client.doneCallback = delegate(IAsyncHTTPClient c) {
                // Debug.Log("Got image " + EditorApplication.timeSinceStartup.ToString());
                cached.client = null;
                if (!client.IsSuccess())
                {
                    if (client.state != AsyncHTTPClient.State.ABORTED)
                    {
                        string err = "error " + client.text + " " + client.state + " '" + url + "'";
                        if (ObjectListArea.s_Debug)
                        {
                            Debug.LogError(err);
                        }
                        else
                        {
                            System.Console.Write(err);
                        }
                    }
                    else
                    {
                        Instance.m_Aborted++;
                    }
                    return;
                }

                // In the case of refetch because of resize first destroy the current image
                if (cached.image != null)
                {
                    Object.DestroyImmediate(cached.image);
                }

                cached.image = c.texture;

                s_NeedsRepaint = true;
                Instance.m_Success++;
            };
            return(client);
        }
        /**
         * Return a texture from a url that points to an image resource
         *
         * This method does not block but queues a request to fetch the image and return null.
         * When the image has been fetched this method will return the image texture downloaded.
         */
        public static CachedAssetStoreImage TextureFromUrl(string url, string label, int textureSize, GUIStyle labelStyle, GUIStyle iconStyle, bool onlyCached)
        {
            if (string.IsNullOrEmpty(url))
            {
                return(Instance.m_DummyItem);
            }

            CachedAssetStoreImage cached;
            bool newentry = true;

            if (CachedAssetStoreImages.TryGetValue(url, out cached))
            {
                cached.lastUsed = EditorApplication.timeSinceStartup;

                // Refetch the image if the size has changed and is not in the progress of being fetched
                bool refetchInitiated = cached.requestedWidth == textureSize;
                bool correctSize      = cached.image != null && cached.image.width == textureSize;

                bool cacheRequestAborted = cached.requestedWidth == -1;

                if ((correctSize || refetchInitiated || onlyCached) && !cacheRequestAborted)
                {
                    Instance.CacheHit++;

                    // Use cached image (that may be null) if we're in progress of fetching the image
                    // or if we have rendered the images correctly
                    //return cached;
                    bool fetchingImage       = cached.client != null;
                    bool labelDrawn          = cached.label == null;
                    bool valid               = fetchingImage || labelDrawn;
                    bool convPerTickExceeded = Instance.m_ConvertedThisTick > kMaxConvertionsPerTick;
                    s_NeedsRepaint = s_NeedsRepaint || convPerTickExceeded;
                    return((valid || convPerTickExceeded) ?
                           cached :
                           RenderEntry(cached, labelStyle, iconStyle));
                }
                //Debug.Log(string.Format("Found {0} {1} {2} {3}", correctSize, refetchInitiated, onlyCached, cacheRequestAborted));
                newentry = false;
                if (Downloading >= kMaxConcurrentDownloads)
                {
                    return(cached.image == null ? Instance.m_DummyItem : cached);
                }
            }
            else
            {
                if (onlyCached || Downloading >= kMaxConcurrentDownloads)
                {
                    return(Instance.m_DummyItem);
                }
                cached          = new CachedAssetStoreImage();
                cached.image    = null;
                cached.lastUsed = EditorApplication.timeSinceStartup;
                //Debug.Log("url is " + textureSize.ToString() + " " + url);
            }

            // Only set fetch time when there is not image in order to use it for
            // fading in the image when it becomes available
            if (cached.image == null)
            {
                cached.lastFetched = EditorApplication.timeSinceStartup;
            }

            cached.requestedWidth = textureSize;
            cached.label          = label;

            AsyncHTTPClient client = null;

            client = SetupTextureDownload(cached, url, "previewSize-" + textureSize);

            ExpireCacheEntries();

            if (newentry)
            {
                CachedAssetStoreImages.Add(url, cached);
            }

            client.Begin();

            Instance.Requested++;
            return(cached);
        }
 private static AsyncHTTPClient SetupTextureDownload(CachedAssetStoreImage cached, string url, string tag)
 {