123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- using MRStore.Util;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- namespace XRTool.Util
- {
-
-
-
-
-
-
- public enum DownState
- {
-
-
-
- Prepare = 0,
-
-
-
- Downing = 1,
-
-
-
- Suspend = 2
- }
-
-
-
- public class DownInfo
- {
-
-
-
-
- public string url;
-
-
-
- public string localPath;
-
-
-
- public DownState state { get; private set; }
-
-
-
- public event Action<DownInfo> beginDownload;
-
-
-
- public event Action<float> downProcess;
-
-
-
- public event Action<DownInfo, string> downComplete;
-
-
-
- public event Action<string> 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<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;
- }
-
-
-
-
-
- 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);
-
- }
-
-
-
-
- public void ActiveTotalLength(long urlSize)
- {
- totalSize = urlSize;
- beginDownload?.Invoke(this);
- state = DownState.Downing;
- ActiveProcess(downloadedFileLen * 1.0f / totalSize);
-
- if (downloadedFileLen == totalSize)
- {
-
- }
- }
-
-
-
-
-
- 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);
- }
- }
- }
- }
|