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