Пример #1
0
        /// <summary>
        /// 添加指定的下载器进行下载,并且缓存没过期就返回缓存
        /// </summary>
        /// <param name="priority">优先级,越大优先级越高</param>
        /// <param name="url">下载地址</param>
        /// <param name="savePath">文件保存路径</param>
        /// <param name="fileName">文件名.默认为下载的文件名</param>
        /// <param name="timeOut">毫秒,默认为统一超时时间</param>
        /// <param name="cacheExpiredDay">过期天数</param>
        /// <param name="retryCount">重试次数</param>
        /// <param name="downloadProgressHandle">下载中</param>
        /// <param name="completeHandle">下载完成</param>
        /// <param name="errorHandle">下载失败</param>
        /// <param name="isFindCacheLibrary">是否查找缓存库,默认查找</param>
        public IDownload AddDownload <T>(int priority, string url, string savePath, string fileName = "", int timeOut = -1,
                                         int cacheExpiredDay            = -1,
                                         int retryCount                 = -1, Action <DownloadInfo> downloadStartHandle = null, Action <DownloadProgressInfo, string> downloadProgressHandle = null,
                                         Action <string> completeHandle = null,
                                         Action <String> errorHandle    = null, bool isFindCacheLibrary = true) where T : IDownload, new()
        {
            Init();
            _error = false;
            IDownload        download = new T();
            DownloadUnitInfo info     = new DownloadUnitInfo()
            {
                DownloadUtil           = download,
                Priority               = priority,
                Url                    = url,
                SavePath               = savePath,
                FileName               = fileName,
                TimeOut                = timeOut,
                RetryCount             = retryCount,
                CacheExpiredDay        = cacheExpiredDay,
                DownloadStartHandle    = downloadStartHandle,
                DownloadProgressHandle = downloadProgressHandle,
                CompleteHandle         = completeHandle,
                ErrorHandle            = errorHandle,
                IsFindCacheLibrary     = isFindCacheLibrary
            };

            _downloadQueue.Add(info);
            ++WaitDownloadCount;
            Update();
            return(download);
        }
Пример #2
0
 private FileCacheEntity _getCache(DownloadUnitInfo info)
 {
     if (_hasCache(info))
     {
         return(_cacheFileLibraryContent[_getKey(info)]);
     }
     return(null);
 }
Пример #3
0
 private void _complete(DownloadUnitInfo download)
 {
     --CurrentDownloadCount;
     _addOrUpdateCache(download);
     download.CompleteHandle?.Invoke(_getCache(download).FilePath);
     download.DownloadUtil.Dispose();
     Update();
 }
Пример #4
0
        private void _startdownload(DownloadUnitInfo download)
        {
            //如果没有指定文件名就将文件名设置为远程文件名
            if (string.IsNullOrEmpty(download.FileName))
            {
                var p = download.Url.Split('/', '\\');
                download.FileName = p.Last();
            }

            //todo 查找缓存
            if (download.IsFindCacheLibrary)
            {
                //命中
                if (_hasCache(download))
                {
                    //文件存在就返回
                    if (Utility.FileUtil.IsFileExists(Path.Combine(download.SavePath, download.FileName)))
                    {
                        _complete(download);
                        return;
                    }
                }
            }

            download.TimeOut         = download.TimeOut < 0 ? TimeOut : download.TimeOut;
            download.RetryCount      = download.RetryCount < 0 ? RetryCount : download.RetryCount;
            download.CacheExpiredDay = download.CacheExpiredDay < 0
                ? CacheExpiredDay
                : download.CacheExpiredDay;

            download.DownloadUtil.Download(
                download.Url, download.Headers, download.SavePath, download.FileName, download.DownloadStartHandle, download.DownloadProgressHandle,
                x =>
            {
                _complete(download);
            }, ex =>
            {
                _error = true;
                if (download.CurrentRetryCount < download.RetryCount)
                {
                    ++download.CurrentRetryCount;
                    UnityEngine.Debug.Log($"失败重来,当前重试次数:{download.CurrentRetryCount},失败信息:\n{ex}");
                    _startdownload(download);
                    return;
                }
                --CurrentDownloadCount;
                download.ErrorHandle?.Invoke(ex);
                download.DownloadUtil.Dispose();
                Update();
            });
        }
Пример #5
0
        private void _addOrUpdateCache(DownloadUnitInfo info)
        {
            _sb.Clear();
            var   cacheTime        = (ulong)DateTime.Now.Ticks;
            ulong cacheExpiredTime = 0;

            if (info.CacheExpiredDay > 0)
            {
                DateTime centuryBegin = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + CacheExpiredDay);
                cacheExpiredTime = (ulong)centuryBegin.Ticks;
            }

            var expiredTime = cacheExpiredTime;

            if (!_hasCache(info))
            {
                _cacheFileLibraryContent.Add(_getKey(info), new FileCacheEntity()
                {
                    Url         = info.Url,
                    FilePath    = Utility.FileUtil.UpdatePathDirectorySeparator(Path.Combine(info.SavePath, info.FileName)),
                    CacheTime   = cacheTime,
                    ExpiredTime = expiredTime
                });
            }
            else
            {
                //更新缓存的缓存时间和过期时间
                _cacheFileLibraryContent[_getKey(info)].CacheTime   = cacheTime;
                _cacheFileLibraryContent[_getKey(info)].ExpiredTime = expiredTime;
            }
            foreach (var cacheEntity in _cacheFileLibraryContent)
            {
                _sb.AppendLine(FileCacheEntity.Serialization(cacheEntity.Value));
            }
            File.WriteAllText(GetCacheLibraryFilePath(), _sb.ToString());
        }
Пример #6
0
 private bool _hasCache(DownloadUnitInfo info)
 {
     return(_cacheFileLibraryContent.ContainsKey(_getKey(info)));
 }
Пример #7
0
 private string _getKey(DownloadUnitInfo info)
 {
     return(_getKey(info.Url, info.SavePath, info.FileName));
 }