Ejemplo n.º 1
0
    private IEnumerator GetFromURL(TileRequest request, ProviderCallback <TileRequest> callback)
    {
        var www = UnityEngine.Networking.UnityWebRequest.Get(request.url);

        yield return(www.Send());

        if (www.isError)
        {
            request.Error = www.error;
            request.State = RequestState.Failed;
        }
        else
        {
            if (www.responseCode == 200)    // 200 = HttpStatusCode.OK
            {
                request.SetData(www.downloadHandler.data);
                request.State = RequestState.Succeeded;

                // Add it to cache and disk
                AddToDisk(request, www.downloadHandler.data);
            }
            else
            {
                request.Error = "Response code: " + www.responseCode;
                request.State = RequestState.Failed;
            }
        }
        callback(request);
    }
Ejemplo n.º 2
0
    public void Run(TileRequest request, ProviderCallback <TileRequest> callback)
    {
        // Try finding the texture in the cache
        Texture texture;

        if (cache.TryRemove(request.id, out texture))
        {
            request.texture = texture;
            request.State   = RequestState.Succeeded;
            callback(request);
            return;
        }

#if UNITY_STANDALONE || UNITY_IOS
        // If not, try loading it on disk
        if (File.Exists(request.file))
        {
            ReadFromDisk(request);
            callback(request);
            return;
        }
#endif

        // Otherwise, get it from the url
        behaviour.StartCoroutine(GetFromURL(request, callback));
    }
Ejemplo n.º 3
0
    public void Run(TileRequest request, ProviderCallback <TileRequest> callback)
    {
        // Try finding the texture in the cache
        if (cache.TryRemove(request.id, out Texture texture))
        {
            request.texture = texture;
            request.State   = RequestState.Succeeded;
            callback(request);
            return;
        }

#if UNITY_STANDALONE || UNITY_IOS
        // If not, try loading it on disk
        if (File.Exists(request.file))
        {
            ReadFromDisk(request);
            callback(request);
            return;
        }
#endif

        // Otherwise, get it from the url (if there's access to the internet)
        if (Application.internetReachability == NetworkReachability.NotReachable)
        {
            request.State = RequestState.Canceled;
            callback(request);
        }
        else
        {
            behaviour.StartCoroutine(GetFromURL(request, callback));
        }
    }
Ejemplo n.º 4
0
    private void OnPatchLoaded(PatchRequest request, ProviderCallback <PatchRequest> callback)
    {
        if (!request.IsCanceled)
        {
            request.State = RequestState.Succeeded;
        }

        callback(request);
    }
Ejemplo n.º 5
0
    public void Run(PatchRequest request, ProviderCallback <PatchRequest> callback)
    {
#if SAFETY_CHECK
        // Try finding it in the cache
        if (cache.TryRemove(request.patch))
        {
            Debug.LogError("This shouldn't happen: requested patch is already in the cache: " + request.file);
            request.State = RequestState.Succeeded;
            callback(request);
            return;
        }
#endif

        // Get it from the file/url
        behaviour.StartCoroutine(request.patch.LoadData((p) => OnPatchLoaded(request, callback)));
    }
Ejemplo n.º 6
0
    private IEnumerator GetFromURL(TileRequest request, ProviderCallback <TileRequest> callback)
    {
        WWW www = new WWW(request.url);

        do
        {
            yield return(null);

            if (request.IsCanceled)
            {
                www.Dispose();
                callback(request);
                yield break;
            }
        } while (!www.isDone);


        if (!string.IsNullOrEmpty(www.error))
        {
            request.Error = www.error;
            request.State = RequestState.Failed;
        }
        else
        {
            if (!www.text.Contains("404 Not Found"))
            {
                request.SetData(www.bytes);
                request.State = RequestState.Succeeded;

                // Add it to cache and disk
                AddToDisk(request, www.bytes);
            }
            else
            {
                request.Error = www.text;
                request.State = RequestState.Failed;
            }
        }
        callback(request);
    }
Ejemplo n.º 7
0
    private IEnumerator GetFromURL(TileRequest request, ProviderCallback <TileRequest> callback)
    {
        using (var webRequest = UnityEngine.Networking.UnityWebRequest.Get(request.url))
        {
            // Request and wait for the desired page.
            yield return(webRequest.SendWebRequest());

            if (!webRequest.isNetworkError && !webRequest.isHttpError && webRequest.responseCode == 200)                // 200 = HttpStatusCode.OK
            {
                request.SetData(webRequest.downloadHandler.data);
                request.State = RequestState.Succeeded;

                // Add it to cache and disk
                AddToDisk(request, webRequest.downloadHandler.data);
            }
            else
            {
                request.Error = "Response (code " + webRequest.responseCode + "): " + webRequest.error;
                request.State = RequestState.Failed;
            }

            callback(request);
        }
    }