Exemple #1
0
        /// <summary>
        /// Load tile either from local cache or from web (in this case, send an asynchronous request that will be received later)
        /// Returns true if load happened (either from cache or from web), returns false if it didn't (because too many requests are pending)
        /// </summary>
        /// <param name="zoom"></param>
        /// <param name="coordX"></param>
        /// <param name="coordY"></param>
        /// <returns></returns>
        public bool LoadTileImageFromZoomAndCoords(int zoom, int coordX, int coordY)
        {
            string key      = MapGeneratorOSM.GetKeyFromZoomAndCoords(zoom, coordX, coordY);
            string tilePath = GetTilePathFromZoomAndCoords(zoom, coordX, coordY);

            if (!allTileTexturesFilePathDico.ContainsKey(key))
            {
                // We don't know tile path, it means we never created the file nor made a request to download it

                bool fileExists = false;
                lock (fileAccessLock)
                {
                    fileExists = File.Exists(tilePath);
                    if (fileExists)
                    {
                        // check if file really contains something (never too careful)
                        FileInfo info = new FileInfo(tilePath);
                        if (info.Length <= 0)
                        {
                            // file exist but is empty
                            fileExists = false;
                        }
                    }
                }

                if (fileExists)
                {
                    // but it already exists in cache ! So we should just ask for rendering
                    mapGenerator.EnqueueTileToDisplay(key);
                }
                else
                {
                    // it doesn't exist yet, so we should create it and download its content
                    CreateTileDirectoriesIfNeeded(zoom, coordX);

                    // except if there are too many requests pending
                    if (webRequestCount >= webRequestMaxCount)
                    {
                        //Debug.LogWarning("Prevent download of tile : " + key + " because webRequestCount is = " + webRequestCount);
                        return(false);
                    }

                    allTileTexturesFilePathDico.Add(key, tilePath);
                    // Call web request to download image
                    lock (fileAccessLock)
                    {
                        try
                        {
                            string url = RandomServer() + "/" + zoom + "/" + coordX + "/" + coordY + ".png";
                            //Debug.Log(url);
                            WebClient client = new WebClient();

                            // sync
                            //client.DownloadFile(new Uri(url), tilePath);

                            // async
                            client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadDataCallback);
                            client.DownloadFileAsync(new Uri(url), tilePath, key);

                            webRequestCount++;
                        }
                        catch (System.Exception ex)
                        {
                            Debug.LogError("LoadTileImageFromZoomAndCoords got exception : " + ex.Message);
                        }
                    }
                }
            }
            return(true);
        }