/// <summary>
 /// 通过http请求一个字符串
 /// 请求的可以是一个文本,也会以字符串形式返回
 /// </summary>
 /// <param name="_url"></param>
 /// <param name="_callback"></param>
 public void GetString(string _url, Action <string> _callback)
 {
     if (false == HKRequestTools.IsCheckUrl(_url))
     {
         return;
     }
     httpClient.GetString(new Uri(_url), response =>
     {
         if (null != _callback)
         {
             if (true == response.IsSuccessStatusCode)
             {
                 if (null != response.Data)
                 {
                     _callback(response.Data);
                 }
             }
             else
             {
                 this.Error(StringCacheFactory.GetFree().Add("Download fail url = ")
                            .Add(_url).Add("\terror code = ").Add(response.StatusCode));
                 _callback(null);
             }
         }
     });
 }
Beispiel #2
0
        /// <summary>
        /// 下载成功处理
        /// </summary>
        private void DownloadSuccessExecute(HKDownloadTaskItem _item, object _data)
        {
            if (null != _item)
            {
                _item.DownloadResult = DownloadResult.Success;

                updateInfo.CurrentProgress = 100; // 更新一次满进度,主要考虑到UI相关的更新
                if (null != downloadUpateInfoAction)
                {
                    downloadUpateInfoAction(updateInfo);
                }

                if (_item.DownloadResType == DownloadResType.StringContent) // string 类型
                {
                    if (null != _item.CompleteAction)
                    {
                        _item.CompleteAction(_item.Url, _data as string, _item.UserData);
                    }
                }
                else if (_item.DownloadResType == DownloadResType.AssetBundle) // AssetBundle类型
                {
                    if (true == _data is byte[])
                    {
                        // 将数据写入到本地
                        HKRequestTools.WriteAssetBundleToLcoal(_item.Url, (byte[])_data);

                        // 处理回调
                        if (null != _item.CompleteAction)
                        {
                            var assetBundle = AssetBundle.LoadFromMemory((byte[])_data);
                            _item.CompleteAction(_item.Url, assetBundle, _item.UserData);
                        }
                    }
                }
                else if (_item.DownloadResType == DownloadResType.ByteArray)  // 二进制类型
                {
                    if (true == _data is byte[])
                    {
                        // 处理回调
                        if (null != _item.CompleteAction)
                        {
                            _item.CompleteAction(_item.Url, _data, _item.UserData);
                        }
                    }
                }

                // 使用完后,就回收到内存池中
                HKDownloadTaskItem.downloadItemCache.Recover(_item);
            }

            // 检测当前剩余下载对象,并继续下载
            CheckQueueComplete();
        }
        /// <summary>
        /// 获取byte数组数据
        /// </summary>
        /// <param name="_url"></param>
        /// <param name="_successEvent"></param>
        /// <param name="_failEvent"></param>
        /// <param name="_progress"></param>
        public void GetBytes(string _url, Action <byte[]> _successEvent = null, Action <HttpStatusCode> _failEvent = null,
                             Action <int> _progress = null)
        {
            if (false == HKRequestTools.IsCheckUrl(_url))
            {
                return;
            }
            cacheBytes.Clear();
            httpClient.GetByteArray(new Uri(_url), HttpCompletionOption.StreamResponseContent, response =>
            {
                if (null != _progress)
                {
                    _progress(response.PercentageComplete);
                }
                if (null != response.Data)
                {
                    cacheBytes.AddRange(response.Data);
                }

                if (true == response.IsSuccessStatusCode)
                {
                    if (response.PercentageComplete >= 100)
                    {
                        byte[] byteContent = cacheBytes.ToArray();

                        //  如果需要回调,则创建一个AssetBundle进行回调
                        if (null != _successEvent)
                        {
                            _successEvent(byteContent);
                        }
                        // 清空队列
                        cacheBytes.Clear();
                    }
                }
                else
                {
                    if (null != _failEvent)
                    {
                        _failEvent(response.StatusCode);
                    }
                }
            });
        }
Beispiel #4
0
        /// <summary>
        /// 向任务队列中,添加一个下载任务
        /// </summary>
        /// <param name="_url">地址</param>
        /// <param name="_resType">资源类型</param>
        /// <param name="_complete">回调接口</param>
        /// <param name="_userData"></param>
        public IRequestTaskQueue AddTask(string _url, DownloadResType _resType, Action <string, object, object> _complete, object _userData)
        {
            if (false == HKRequestTools.IsCheckUrl(_url))
            {
                return(this);
            }

            var item = HKDownloadTaskItem.downloadItemCache.GetFree();

            if (null == item)
            {
                return(this);
            }

            item.Url              = _url;
            item.DownloadResType  = _resType;
            item.downloadPriority = 0;
            item.CompleteAction   = _complete;
            item.UserData         = _userData;

            // 添加到待下载队列中
            downloadSortSet.Add(item, item.downloadPriority);
            return(this);
        }
        /// <summary>
        /// 加载一个Asset Bundle,加载完后,会自动写入到可读写目录下
        /// 如果存在相同的文件,会被覆盖
        /// </summary>
        /// <param name="_url"></param>
        /// <param name="_progrss"></param>
        /// <param name="_compolete"></param>
        public void GetAssetBundle(string _url, Action <AssetBundle> _compolete = null, Action <int> _progrss = null)
        {
            if (false == HKRequestTools.IsCheckUrl(_url))
            {
                return;
            }
            cacheBytes.Clear();
            httpClient.GetByteArray(new Uri(_url), HttpCompletionOption.StreamResponseContent, response =>
            {
                if (null != _progrss)
                {
                    _progrss(response.PercentageComplete);
                }
                if (null != response.Data)
                {
                    cacheBytes.AddRange(response.Data);
                }
                if (response.PercentageComplete >= 100 && true == response.IsSuccessStatusCode)
                {
                    byte[] byteContent = cacheBytes.ToArray();

                    // 写入本地
                    HKRequestTools.WriteAssetBundleToLcoal(_url, byteContent);

                    //  如果需要回调,则创建一个AssetBundle进行回调
                    if (null != _compolete)
                    {
                        var assetBundle = AssetBundle.LoadFromMemory(byteContent);
                        _compolete(assetBundle);
                    }

                    // 清空队列
                    cacheBytes.Clear();
                }
            });
        }