123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using System;
- using System.IO;
- using UnityEngine.Networking;
- using XRTool.Util;
- namespace MRStore.Util
- {
- /// <summary>
- /// 断点下载的辅助器
- /// </summary>
- public class DownInfoHandler : DownloadHandlerScript
- {
- /// <summary>
- /// 文件流
- /// </summary>
- private FileStream stream;
- /// <summary>
- /// 下载资源的结构体
- /// </summary>
- public DownInfo info { get; private set; }
- public UnityWebRequest webRequest { get; private set; }
- public void SetWebRequest(UnityWebRequest req, long contentLength)
- {
- webRequest = req;
- ReadLocalData();
- if (info != null)
- {
- info.ActiveTotalLength((long)contentLength);
- }
- }
- /// <summary>
- /// 读取本地的数据
- /// 读取临时数据缓存区tmppath
- /// </summary>
- private void ReadLocalData()
- {
- if (stream == null)
- {
- try
- {
- FileMode model = FileMode.Append;
- if (!File.Exists(info.tmpFilePath))
- {
- model = FileMode.OpenOrCreate;
- }
- stream = new FileStream(info.tmpFilePath, model, FileAccess.Write, FileShare.Write);
- }
- catch (Exception ex)
- {
- UnityLog.LogError(info.tmpFilePath + "File Stream Error" + ex.ToString());
- if (stream != null)
- {
- stream.Dispose();
- }
- stream = null;
- }
- }
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="info"></param>
- public DownInfoHandler(DownInfo info) : base(new byte[1024*256])
- {
- this.info = info;
- this.info.SetDownHandler(this);
- }
- /// <summary>
- /// 服务器返回此文件的剩余大小
- /// </summary>
- /// <param name="contentLength"></param>
- protected override void ReceiveContentLengthHeader(ulong contentLength)
- {
- if (contentLength == 0)
- {
- CompleteContent();
- }
- base.ReceiveContentLengthHeader(contentLength);
- }
- /// <summary>
- /// 从服务器接收到数据
- /// </summary>
- /// <param name="data"></param>
- /// <param name="dataLength"></param>
- /// <returns></returns>
- protected override bool ReceiveData(byte[] data, int dataLength)
- {
- WriteFile(data, dataLength);
- return base.ReceiveData(data, dataLength);
- }
- /// <summary>
- /// 下载,写入文件
- /// </summary>
- /// <param name="data"></param>
- /// <param name="dataLength"></param>
- public void WriteFile(byte[] data, int dataLength)
- {
- if (data != null && data.Length > 0)
- {
- if (stream != null)
- {
- stream.Write(data, 0, dataLength);
- info.ActiveProcess(GetProgress());
- }
- }
- }
- protected override float GetProgress()
- {
- return info.downloadedFileLen*1.0f / info.totalSize;
- }
- /// <summary>
- /// 下载完成
- /// 网络停止时也有可能会调用此方法
- /// </summary>
- protected override void CompleteContent()
- {
- if (stream != null)
- {
- stream.Flush();
- stream.Dispose();
- }
- stream = null;
- info.ActiveComplete();
- base.CompleteContent();
- Dispose();
- }
- /// <summary>
- /// 下载错误,程序异常退出,网络停止时等异常情况下,停止此资源
- /// </summary>
- public void ErrorDispose(string error)
- {
- if (stream != null)
- {
- stream.Flush();
- stream.Dispose();
- }
- if (info != null && !string.IsNullOrEmpty(error))
- {
- info.ActiveError(error);
- }
- stream = null;
- webRequest = null;
- Dispose();
- }
- }
- }
|