Esempio n. 1
0
            public void GetDownloadDetail(long productID, Action <AssetStoreDownloadInfo> doneCallbackAction)
            {
                var httpRequest = ApplicationUtil.instance.GetASyncHTTPClient($"{host}{k_DownloadInfoUri}/{productID}");

                HandleHttpRequest(httpRequest,
                                  result =>
                {
                    var downloadInfo = AssetStoreDownloadInfo.ParseDownloadInfo(result);
                    doneCallbackAction?.Invoke(downloadInfo);
                },
                                  error =>
                {
                    var downloadInfo = new AssetStoreDownloadInfo
                    {
                        isValid      = false,
                        errorMessage = error.message
                    };
                    doneCallbackAction?.Invoke(downloadInfo);
                });
            }
        public void Download(bool resume)
        {
            m_State = resume ? DownloadState.ResumeRequested : DownloadState.DownloadRequested;
            var productId = long.Parse(m_ProductId);

            m_AssetStoreRestAPI.GetDownloadDetail(productId, downloadInfo =>
            {
                m_DownloadInfo = downloadInfo;
                if (!downloadInfo.isValid)
                {
                    OnErrorMessage(downloadInfo.errorMessage);
                    return;
                }

                var dest = downloadInfo.destination;

                var json = m_AssetStoreUtils.CheckDownload(
                    $"{k_AssetStoreDownloadPrefix}{downloadInfo.productId}",
                    downloadInfo.url, dest,
                    downloadInfo.key);

                var resumeOK = false;
                try
                {
                    var current = Json.Deserialize(json) as IDictionary <string, object>;
                    if (current == null)
                    {
                        throw new ArgumentException("Invalid JSON");
                    }

                    var inProgress = current.ContainsKey("in_progress") && (current["in_progress"] is bool?(bool)current["in_progress"] : false);
                    if (inProgress)
                    {
                        if (!isInPause)
                        {
                            m_State = DownloadState.Downloading;
                        }
                        return;
                    }

                    if (current.ContainsKey("download") && current["download"] is IDictionary <string, object> )
                    {
                        var download    = (IDictionary <string, object>)current["download"];
                        var existingUrl = download.ContainsKey("url") ? download["url"] as string : string.Empty;
                        var existingKey = download.ContainsKey("key") ? download["key"] as string : string.Empty;
                        resumeOK        = (existingUrl == downloadInfo.url && existingKey == downloadInfo.key);
                    }
                }
                catch (Exception e)
                {
                    OnErrorMessage(e.Message);
                    return;
                }

                json = $"{{\"download\":{{\"url\":\"{downloadInfo.url}\",\"key\":\"{downloadInfo.key}\"}}}}";
                m_AssetStoreUtils.Download(
                    $"{k_AssetStoreDownloadPrefix}{downloadInfo.productId}",
                    downloadInfo.url,
                    dest,
                    downloadInfo.key,
                    json,
                    resumeOK && resume);

                m_State = DownloadState.Connecting;
            });
        }
Esempio n. 3
0
        public void Download()
        {
            var productId = long.Parse(m_ProductId);

            AssetStoreRestAPI.instance.GetDownloadDetail(productId, downloadInfo =>
            {
                m_DownloadInfo = downloadInfo;
                if (!downloadInfo.isValid)
                {
                    OnErrorMessage(downloadInfo.errorMessage);
                    return;
                }

                var dest = downloadInfo.destination;

                var json = AssetStoreUtils.instance.CheckDownload(
                    $"{k_AssetStoreDownloadPrefix}{downloadInfo.productId}",
                    downloadInfo.url, dest,
                    downloadInfo.key);

                var resumeOK = false;
                try
                {
                    json = Regex.Replace(json, "\"url\":(?<url>\"?[^,]+\"?),\"", "\"url\":\"${url}\",\"");
                    json = Regex.Replace(json, "\"key\":(?<key>\"?[0-9a-zA-Z]*\"?)\\}", "\"key\":\"${key}\"}");
                    json = Regex.Replace(json, "\"+(?<value>[^\"]+)\"+", "\"${value}\"");

                    var current = Json.Deserialize(json) as IDictionary <string, object>;
                    if (current == null)
                    {
                        throw new ArgumentException("Invalid JSON");
                    }

                    var inProgress = current.ContainsKey("in_progress") && (current["in_progress"] is bool?(bool)current["in_progress"] : false);
                    if (inProgress)
                    {
                        m_State = DownloadState.Downloading;
                        return;
                    }

                    if (current.ContainsKey("download") && current["download"] is IDictionary <string, object> )
                    {
                        var download    = (IDictionary <string, object>)current["download"];
                        var existingUrl = download.ContainsKey("url") ? download["url"] as string : string.Empty;
                        var existingKey = download.ContainsKey("key") ? download["key"] as string : string.Empty;
                        resumeOK        = (existingUrl == downloadInfo.url && existingKey == downloadInfo.key);
                    }
                }
                catch (Exception e)
                {
                    OnErrorMessage(e.Message);
                    return;
                }

                json = $"{{\"download\":{{\"url\":\"{downloadInfo.url}\",\"key\":\"{downloadInfo.key}\"}}}}";
                AssetStoreUtils.instance.Download(
                    $"{k_AssetStoreDownloadPrefix}{downloadInfo.productId}",
                    downloadInfo.url,
                    dest,
                    downloadInfo.key,
                    json,
                    resumeOK);

                m_State = DownloadState.Connecting;
            });
        }