示例#1
0
        CustomWWW getCachedWWW(string url, TileInfo ti)
        {
            string    filePath = GetLocalFilePathForURL(url, ti);
            CustomWWW www;
            bool      useCached = false;

            useCached = _tileEnableLocalCache && System.IO.File.Exists(filePath);
            if (useCached)
            {
                if (!_tilePreloadTiles || !filePath.Contains(PREFIX_MIN_ZOOM_LEVEL))
                {
                    //check how old
                    System.DateTime written    = File.GetLastWriteTimeUtc(filePath);
                    System.DateTime now        = System.DateTime.UtcNow;
                    double          totalHours = now.Subtract(written).TotalHours;
                    if (totalHours > 300)
                    {
                        File.Delete(filePath);
                        useCached = false;
                    }
                }
            }
            ti.source = useCached ? TILE_SOURCE.Cache : TILE_SOURCE.Online;
            if (useCached)
            {
#if UNITY_STANDALONE_WIN || UNITY_WSA
                string pathforwww = "file:///" + filePath;
#else
                string pathforwww = "file://" + filePath;
#endif
                www = new CustomWWW(pathforwww);
            }
            else
            {
                www = new CustomWWW(url);
            }
            return(www);
        }
示例#2
0
        internal IEnumerator LoadTileContentBackground(TileInfo ti)
        {
            yield return(new WaitForEndOfFrame());

            string url = GetTileURL(_tileServer, ti);

            if (string.IsNullOrEmpty(url))
            {
                Debug.LogError("Tile server url not set. Aborting");
                yield break;
            }

            long   downloadedBytes = 0;
            string error           = null;
            string filePath        = "";

            byte[] textureBytes = null;
            ti.source = TILE_SOURCE.Unknown;

            // Check if tile is given by external event
            if (OnTileRequest != null)
            {
                if (OnTileRequest(ti.zoomLevel, ti.x, ti.y, out ti.texture, out error) && ti.texture != null)
                {
                    ti.source = TILE_SOURCE.Resources;
                }
            }

            // Check if tile is in Resources
            if (ti.source == TILE_SOURCE.Unknown && _tileEnableOfflineTiles)
            {
                string          path    = GetTileResourcePath(ti.x, ti.y, ti.zoomLevel, false);
                ResourceRequest request = Resources.LoadAsync <Texture2D> (path);
                yield return(request);

                if (request.asset != null)
                {
                    ti.texture = (Texture2D)request.asset;
                    ti.source  = TILE_SOURCE.Resources;
                }
                else if (tileOfflineTilesOnly)
                {
                    ti.texture = tileResourceFallbackTexture;
                    ti.source  = TILE_SOURCE.Resources;
                }
            }

            CustomWWW www = null;

            if (ti.source == TILE_SOURCE.Unknown)
            {
                www = getCachedWWW(url, ti);
                yield return(www);

                error = www.error;
            }

            for (int tries = 0; tries < 100; tries++)
            {
                if (spreadLoadAmongFrames > 0)
                {
                    break;
                }
                yield return(new WaitForEndOfFrame());
            }
            spreadLoadAmongFrames--;
            _concurrentLoads--;

            if (!ti.visible)                    // non visible textures are ignored
            {
                ti.loadStatus = TILE_LOAD_STATUS.InQueue;
                yield break;
            }

            if (!string.IsNullOrEmpty(error))
            {
                _tileLastError     = "Error getting tile: " + error + " url=" + url;
                _tileLastErrorDate = DateTime.Now;
                if (_tileDebugErrors)
                {
                    Debug.Log(_tileLastErrorDate + " " + _tileLastError);
                }
                ti.loadStatus = TILE_LOAD_STATUS.InQueue;
                yield break;
            }

            // Load texture
            if (ti.source != TILE_SOURCE.Resources)
            {
                downloadedBytes = www.bytesDownloaded;
                textureBytes    = www.bytes;
                ti.texture      = www.textureNonReadable;
                www.Dispose();
                www = null;

                // Check texture consistency
                if (ti.loadedFromCache || _tileEnableLocalCache)
                {
                    filePath = GetLocalFilePathForURL(url, ti);
                }

                if (ti.loadedFromCache && ti.texture.width <= 16)                   // Invalid texture in local cache, retry
                {
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                    ti.loadStatus = TILE_LOAD_STATUS.Inactive;
                    ti.queueTime  = Time.time;
                    yield break;
                }
            }

            ti.texture.wrapMode = TextureWrapMode.Clamp;
            _tileSize           = ti.texture.width;

            // Save texture
            if (_tileEnableLocalCache && ti.source != TILE_SOURCE.Resources && !File.Exists(filePath))
            {
                _tileCurrentCacheUsage += textureBytes.Length;
                BackgroundSaver saver = new BackgroundSaver(textureBytes, filePath);
                saver.Start();
            }

            // Update stats
            switch (ti.source)
            {
            case TILE_SOURCE.Cache:
                _cacheLoads++;
                _cacheLoadTotalSize += downloadedBytes;
                break;

            case TILE_SOURCE.Resources:
                _resourceLoads++;
                break;

            default:
                _webDownloads++;
                _webDownloadTotalSize += downloadedBytes;
                break;
            }

            if (loadQueue.Contains(ti))
            {
                loadQueue.Remove(ti);
            }

            FinishLoadingTile(ti);
        }
        void Estimate()
        {
            // Manage downloads
            int activeDownloads = 0;

            for (int k = 0; k < concurrentDownloads; k++)
            {
                if (downloads [k].busy)
                {
                    CustomWWW www = downloads [k].www;
                    if (www.isDone)
                    {
                        bool good = string.IsNullOrEmpty(www.error);
                        // Check texture consistency
                        if (good)
                        {
                            Texture2D tex = www.textureNonReadable;
                            if (tex == null)
                            {
                                good = false;
                            }
                            else if (tex.width < 16)
                            {
                                good = false;
                                DestroyImmediate(tex);
                            }
                        }
                        if (good)
                        {
                            // save to resources
                            estimationTotalSize += www.bytesDownloaded * www.bytesDownloaded;
                            estimationDownloads++;
                            storageSize        = numTiles * Mathf.Sqrt(estimationTotalSize / estimationDownloads) / ONE_MEGABYTE;
                            downloads [k].busy = false;
                        }
                        else
                        {
                            if (downloads [k].retries < 3)
                            {
                                downloads [k].retries++;
                                Debug.LogError("Retrying " + downloads [k].retries + " times due error on tile XYZ = " + downloads [k].x + "/" + downloads [k].y + "/" + downloads [k].zoomLevel + " : " + www.error);
                                downloads [k].www = new CustomWWW(www.url);
                                activeDownloads++;
                            }
                            else
                            {
                                downloads [k].busy = false;
                            }
                        }
                        www.Dispose();
                    }
                    else
                    {
                        activeDownloads++;
                    }
                }
            }

            for (int k = 0; k < concurrentDownloads; k++)
            {
                if (!downloads [k].busy)
                {
                    if (activeDownloads >= concurrentDownloads)
                    {
                        return;
                    }

                    // Get next tile
                    bool remainingTiles = GetNextTileRandom();

                    // Have we finished?
                    if (!remainingTiles)
                    {
                        if (activeDownloads == 0)
                        {
                            StopOperation();
                        }
                        return;
                    }

                    ti.x         = x;
                    ti.y         = y;
                    ti.zoomLevel = zoomLevel;
                    string url = map.GetTileURL(map.tileServer, ti);
                    downloads [k].busy      = true;
                    downloads [k].retries   = 0;
                    downloads [k].x         = x;
                    downloads [k].y         = y;
                    downloads [k].zoomLevel = zoomLevel;
                    downloads [k].www       = new CustomWWW(url);
                    activeDownloads++;
                }
            }
        }
        void Download()
        {
            // Manage downloads
            int activeDownloads = 0;

            for (int k = 0; k < concurrentDownloads; k++)
            {
                if (downloads [k].busy)
                {
                    CustomWWW www = downloads [k].www;
                    if (www.isDone)
                    {
                        bool good = string.IsNullOrEmpty(www.error);
                        // Check texture consistency
                        if (good)
                        {
                            Texture2D tex = www.textureNonReadable;
                            if (tex == null)
                            {
                                good = false;
                            }
                            else if (tex.width < 16)
                            {
                                good = false;
                                DestroyImmediate(tex);
                            }
                        }
                        if (good)
                        {
                            // save to resources
                            bytesDownloaded += www.bytes.Length;
                            File.WriteAllBytes(downloads [k].path, www.bytes);
                            downloadedTilesCount++;
                            downloadedTilesSize += bytesDownloaded;
                            downloads [k].busy   = false;
                        }
                        else
                        {
                            if (downloads [k].retries < 3)
                            {
                                downloads [k].retries++;
                                Debug.LogError("Retrying " + downloads [k].retries + " times due error on tile XYZ = " + downloads [k].x + "/" + downloads [k].y + "/" + downloads [k].zoomLevel + " : " + www.error);
                                downloads [k].www = new CustomWWW(www.url);
                                activeDownloads++;
                            }
                            else
                            {
                                downloads [k].busy = false;
                            }
                        }
                        www.Dispose();
                    }
                    else
                    {
                        activeDownloads++;
                    }
                }
            }

            int iterations = 0;

            for (int k = 0; k < concurrentDownloads; k++)
            {
                if (!downloads [k].busy)
                {
                    if (activeDownloads >= concurrentDownloads)
                    {
                        return;
                    }

                    // Get next tile
                    bool remainingTiles = GetNextTile();

                    // Have we finished?
                    if (!remainingTiles)
                    {
                        if (activeDownloads == 0)
                        {
                            StopOperation();
                        }
                        return;
                    }

                    ti.x         = x;
                    ti.y         = y;
                    ti.zoomLevel = zoomLevel;
                    string url = map.GetTileURL(map.tileServer, ti);
                    // Is current tile already in Resources path?
                    string tilePath = map.GetTileResourcePath(x, y, zoomLevel);
                    if (File.Exists(tilePath))
                    {
                        if (++iterations < 128)
                        {
                            k--;
                        }
                    }
                    else
                    {
                        downloads [k].busy      = true;
                        downloads [k].retries   = 0;
                        downloads [k].x         = x;
                        downloads [k].y         = y;
                        downloads [k].zoomLevel = zoomLevel;
                        downloads [k].path      = tilePath;
                        downloads [k].www       = new CustomWWW(url);
                        activeDownloads++;
                    }
                }
            }
        }