using MRStore.Util; using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; namespace XRTool.Util { /// /// 当前的状态 /// 0代表准备下载 /// 1代表启动下载,只有当状态为0时才会自动启动下载 /// 2代表暂停下载,主动点击时会暂停下载,需要主动点击解除暂停状态 /// public enum DownState { /// /// 0代表准备下载 /// Prepare = 0, /// /// 1代表启动下载,只有当状态为0时才会自动启动下载 /// Downing = 1, /// ///2代表暂停下载,主动点击时会暂停下载,需要主动点击解除暂停状态 /// Suspend = 2 } /// /// 下载的信息与事件 /// public class DownInfo { /// /// 请求的地址 /// 完整的下载地址 /// public string url; /// /// 本地的下载路径 /// public string localPath; /// /// 当前的下载状态 /// public DownState state { get; private set; } /// /// 获得文件总大小的事件 /// public event Action beginDownload; /// /// 下载进度更新 /// public event Action downProcess; /// /// 下载完成后回调下载的路径 /// public event Action downComplete; /// /// 下载出错 /// public event Action downError; /// /// 下载中的文件后缀 /// public const string Extension = ".downing"; /// /// 文件名 /// public string fileName { get; private set; } /// /// /// 本地临时文件名 /// public string tmpFilePath { get; private set; } /// /// 本地真实文件名 /// public string realFilePath { get; private set; } /// /// 文件总大小 /// public long totalSize { get; private set; } /// /// 文件已下载的大小 /// public long downloadedFileLen { get; private set; } private DownInfoHandler handler; /// /// 构造一个下载器 /// /// /// public DownInfo(string url, string localPath, Action downComplete = null, Action downProcess = null, Action downError = null, Action beginDownload = null) { this.url = url; this.localPath = localPath; this.downComplete = downComplete; this.downProcess = downProcess; this.downError = downError; this.beginDownload = beginDownload; ///单纯的获取文件名+后缀 fileName = Path.GetFileName(url); ///真实文件名 realFilePath = Path.Combine(localPath, fileName); ///完整文件名 tmpFilePath = realFilePath + Extension; } /// /// 判断是否需要下载 /// 如果已存在真实文件,则无需下载 /// /// public bool IsNeedDown() { if (File.Exists(realFilePath)) { downComplete?.Invoke(this, realFilePath); return false; } return true; } /// /// 下载失败或者出现错误后 /// /// public void ActiveError(string error) { state = DownState.Suspend; downError?.Invoke(error); //DataFileUtil.DelFile(tmpFilePath); } /// /// 获得文件的总大小 = 本地已下载+服务器剩余大小 /// /// public void ActiveTotalLength(long urlSize) { totalSize = urlSize; beginDownload?.Invoke(this); state = DownState.Downing; ActiveProcess(downloadedFileLen * 1.0f / totalSize); ///服务端无新数据,完成下载 if (downloadedFileLen == totalSize) { //ActiveComplete(); } } /// /// 进度更新事件激活 /// 此为文件的总进度 = 已下载的进度+当前的进度*剩余的进度比例 /// /// public void ActiveProcess(float p) { downProcess?.Invoke(p); } /// /// 停止下载 /// public bool StopDown() { if (state == DownState.Downing) { if (handler != null && handler.webRequest != null) { handler.webRequest.Dispose(); handler.ErrorDispose(null); } state = DownState.Suspend; handler = null; return true; } return false; } /// /// 取消下载 /// public void CancelDown() { StopDown(); DataFileUtil.DelFile(tmpFilePath); DataFileUtil.DelFile(realFilePath); } public void SetDownHandler(DownInfoHandler handler) { this.handler = handler; } /// /// 激活下载完成的事件 /// 下载完成后,改名 /// public void ActiveComplete() { ///下载完成的判断 ///已下载的文件等于总文件大小 if (downloadedFileLen >= totalSize) { DataFileUtil.ReNameFile(tmpFilePath, realFilePath); downComplete?.Invoke(this, realFilePath); } } } }