// Invoked when a load requeset has failed to download the material data it needs
        // Requests are retried RETRY_LIMIT times if they fail more than that the request is abandoned (error is logged)
        // When this happens if any dependent cubes want the resource that failed one request is re-queued to try again (under that request's Retry quota)
        private void FailGetMaterialDataRequest(LoadCubeRequest loadRequest, string materialPath)
        {
            CheckIfBackgroundThread();
            loadRequest.Failures++;
            lock (MaterialDataCache)
            {
                // Remove the 'in progress' marker from the cache
                MaterialDataCache.Remove(materialPath);
            }

            if (RETRY_LIMIT > loadRequest.Failures)
            {
                Debug.LogError("Retry limit hit for: " + materialPath);
                Debug.LogError("Cube load failed for " + loadRequest);

                lock (_dependentCubes)
                {
                    LinkedList <LoadCubeRequest> dependentRequests;
                    if (_dependentCubes.TryGetValue(materialPath, out dependentRequests))
                    {
                        var request = dependentRequests.Last.Value;
                        dependentRequests.RemoveLast();
                        _loadMaterialQueue.ConcurrentEnqueue(request).Wait();
                    }
                }
            }
            else
            {
                // Queue for retry
                _loadMaterialQueue.ConcurrentEnqueue(loadRequest).Wait();
            }
        }
Exemple #2
0
        private IEnumerator GetMaterialForRequest(LoadCubeRequest loadRequest)
        {
            //var pyriteLevel = loadRequest.Query.DetailLevels[loadRequest.LodIndex];
            //var textureCoordinates = pyriteLevel.TextureCoordinatesForCube(loadRequest.X, loadRequest.Y);
            //var texturePath = loadRequest.Query.GetTexturePath(loadRequest.LodIndex, (int)textureCoordinates.x, (int)textureCoordinates.y);

            var pyriteLevel        = pyriteQuery.DetailLevels[loadRequest.LodIndex];
            var textureCoordinates = pyriteLevel.TextureCoordinatesForCube(loadRequest.X, loadRequest.Y);
            var texturePath        = pyriteQuery.GetTexturePath(loadRequest.LodIndex, (int)textureCoordinates.x, (int)textureCoordinates.y);

            while (!Monitor.TryEnter(MaterialDataCache))
            {
                yield return(null);
            }
            // If the material data is not in the cache or in the middle of being constructed add this request as a dependency
            if (!MaterialDataCache.ContainsKey(texturePath) || MaterialDataCache[texturePath] == null)
            {
                // Add this request to list of requests that is waiting for the data
                yield return(StartCoroutine(AddDependentRequest(loadRequest, texturePath)));

                // Check if this is the first request for material (or it isn't in the cache)
                if (!MaterialDataCache.ContainsKey(texturePath))
                {
                    if (UseWwwForTextures)
                    {
                        // Material data was not in cache nor being constructed
                        // Set to null to signal to other tasks that the key is in the process
                        // of being filled
                        MaterialDataCache[texturePath] = null;
                        var materialData = CubeBuilderHelpers.GetDefaultMaterialData(
                            (int)textureCoordinates.x,
                            (int)textureCoordinates.y,
                            loadRequest.LodIndex,
                            texturePath);
                        var cachePath = CacheWebRequest.GetCacheFilePath(texturePath);
                        if (!CacheFill)
                        {
                            WWW textureWww; // = new WWW(texturePath);
                            if (CacheWebRequest.IsItemInCache(cachePath))
                            {
                                textureWww = new WWW("file:///" + cachePath);
                                yield return(textureWww);
                            }
                            else
                            {
                                textureWww = new WWW(texturePath);
                                yield return(textureWww);

                                CacheWebRequest.AddToCache(cachePath, textureWww.bytes);
                            }
                            materialData.DiffuseTex      = textureWww.texture;
                            materialData.DiffuseTex.name = materialData.Name;
                        }
                        MaterialDataCache[texturePath] = materialData;
                        MaterialDataCache.AddRef(texturePath);
                        // Move forward dependent requests that wanted this material data
                        yield return(StartCoroutine(SucceedGetMaterialDataRequests(texturePath, materialData)));
                    }
                    else
                    {
                        // Material data was not in cache nor being constructed
                        // Set to null to signal to other tasks that the key is in the process
                        // of being filled
                        MaterialDataCache[texturePath] = null;
                        var materialData = CubeBuilderHelpers.GetDefaultMaterialData(
                            (int)textureCoordinates.x,
                            (int)textureCoordinates.y,
                            loadRequest.LodIndex,
                            texturePath);
                        _partiallyConstructedMaterialDatas[texturePath] = materialData;

                        CacheWebRequest.GetBytes(texturePath, textureResponse =>
                        {
                            CheckIfBackgroundThread();
                            if (textureResponse.Status == CacheWebRequest.CacheWebResponseStatus.Error)
                            {
                                Debug.LogError("Error getting texture [" + texturePath + "] " + textureResponse.ErrorMessage);
                                FailGetMaterialDataRequest(loadRequest, texturePath);
                            }
                            else if (textureResponse.Status == CacheWebRequest.CacheWebResponseStatus.Cancelled)
                            {
                                lock (MaterialDataCache)
                                {
                                    MaterialDataCache.Remove(texturePath);
                                }
                            }
                            else
                            {
                                if (textureResponse.IsCacheHit)
                                {
                                    //FileCacheHits++;
                                }
                                else
                                {
                                    //FileCacheMisses++;
                                }
                                _texturesReadyForMaterialDataConstruction.ConcurrentEnqueue(
                                    new KeyValuePair <string, byte[]>(texturePath, textureResponse.Content)).Wait();
                            }
                        }, DependentRequestsExistBlocking);
                    }
                }
            }
            else // The material was in the cache
            {
                // Material data ready get it and move on
                //MaterialCacheHits++;
                loadRequest.MaterialData = MaterialDataCache[texturePath];
                MaterialDataCache.AddRef(texturePath);
                MoveRequestForward(loadRequest);
            }
            Monitor.Exit(MaterialDataCache);
        }