|
@@ -0,0 +1,155 @@
|
|
1
|
+using System;
|
|
2
|
+using System.Collections;
|
|
3
|
+using System.Collections.Generic;
|
|
4
|
+using System.IO;
|
|
5
|
+using System.Threading.Tasks;
|
|
6
|
+using UnityEngine;
|
|
7
|
+using UnityEngine.Networking;
|
|
8
|
+
|
|
9
|
+namespace TModule.Runtime
|
|
10
|
+{
|
|
11
|
+ public class DownloadRoutine
|
|
12
|
+ {
|
|
13
|
+ /// <summary>
|
|
14
|
+ /// 下载路径
|
|
15
|
+ /// </summary>
|
|
16
|
+ public string DownLoadUrl { get; set; }
|
|
17
|
+
|
|
18
|
+ /// <summary>
|
|
19
|
+ /// 文件保存路径
|
|
20
|
+ /// </summary>
|
|
21
|
+ public string FileSavePath { get; set; }
|
|
22
|
+
|
|
23
|
+ /// <summary>
|
|
24
|
+ /// 是否空闲
|
|
25
|
+ /// </summary>
|
|
26
|
+ public bool IsLeisure { get; set; } = true;
|
|
27
|
+
|
|
28
|
+ /// <summary>
|
|
29
|
+ /// 下载总大小
|
|
30
|
+ /// </summary>
|
|
31
|
+ private long _totalSzie;
|
|
32
|
+
|
|
33
|
+ public long TotalSize => _totalSzie;
|
|
34
|
+
|
|
35
|
+ /// <summary>
|
|
36
|
+ /// 已下载大小
|
|
37
|
+ /// </summary>
|
|
38
|
+ private long _currentDownloadSize;
|
|
39
|
+
|
|
40
|
+ public long CurrentDownloadSize => _currentDownloadSize;
|
|
41
|
+
|
|
42
|
+ //停止下载
|
|
43
|
+ private bool _isStop;
|
|
44
|
+
|
|
45
|
+ public bool IsStop
|
|
46
|
+ {
|
|
47
|
+ get => _isStop ||
|
|
48
|
+#if UNITY_EDITOR
|
|
49
|
+ !UnityEditor.EditorApplication.isPlaying
|
|
50
|
+
|
|
51
|
+#else
|
|
52
|
+ !Application.isPlaying
|
|
53
|
+#endif
|
|
54
|
+ ;
|
|
55
|
+ set => _isStop = value;
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ public Action onDownloadOver;
|
|
59
|
+
|
|
60
|
+ /// <summary>
|
|
61
|
+ /// 下载中事件 下载进度 总大小 已下载大小
|
|
62
|
+ /// </summary>
|
|
63
|
+ public Action<float,long,long> onProgress;
|
|
64
|
+
|
|
65
|
+ public Action<string> onError;
|
|
66
|
+
|
|
67
|
+ /// <summary>
|
|
68
|
+ /// 开始下载
|
|
69
|
+ /// </summary>
|
|
70
|
+ /// <param name="url"></param>
|
|
71
|
+ /// <returns></returns>
|
|
72
|
+ public async Task StartDownload(string url,string fileSavePath)
|
|
73
|
+ {
|
|
74
|
+ DownLoadUrl = url;
|
|
75
|
+ FileSavePath = fileSavePath;
|
|
76
|
+ IsStop = false;
|
|
77
|
+ _currentDownloadSize = 0;
|
|
78
|
+ IsLeisure = false;
|
|
79
|
+ UnityWebRequest webRequest = UnityWebRequest.Head(DownLoadUrl);
|
|
80
|
+ await webRequest.SendWebRequest();
|
|
81
|
+ if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError)
|
|
82
|
+ {
|
|
83
|
+ onError?.Invoke(webRequest.error);
|
|
84
|
+ Debug.Log(webRequest.error); //出现错误 输出错误信息
|
|
85
|
+ }
|
|
86
|
+ else
|
|
87
|
+ {
|
|
88
|
+ string info = webRequest.GetResponseHeader("Content-Length");
|
|
89
|
+ _totalSzie = long.Parse(info);
|
|
90
|
+ await DownloadFile();
|
|
91
|
+ }
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ public void StopDownload()
|
|
95
|
+ {
|
|
96
|
+ IsStop = true;
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ public async Task ContinueDownload()
|
|
100
|
+ {
|
|
101
|
+ if (IsLeisure) return;
|
|
102
|
+ IsStop = false;
|
|
103
|
+ await DownloadFile();
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ async Task DownloadFile()
|
|
107
|
+ {
|
|
108
|
+ string dirPath = Path.GetDirectoryName(FileSavePath);
|
|
109
|
+ if (!Directory.Exists(dirPath))
|
|
110
|
+ Directory.CreateDirectory(dirPath);
|
|
111
|
+ using (FileStream fs = new FileStream(FileSavePath, FileMode.OpenOrCreate, FileAccess.Write,FileShare.ReadWrite))
|
|
112
|
+ {
|
|
113
|
+ _currentDownloadSize = fs.Length;
|
|
114
|
+
|
|
115
|
+ if (_currentDownloadSize < _totalSzie)
|
|
116
|
+ {
|
|
117
|
+ fs.Seek(_currentDownloadSize, SeekOrigin.Begin);
|
|
118
|
+ UnityWebRequest request = UnityWebRequest.Get(DownLoadUrl);
|
|
119
|
+ request.SetRequestHeader("Range", $"bytes={_currentDownloadSize}-");
|
|
120
|
+ request.SendWebRequest();
|
|
121
|
+ if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
|
|
122
|
+ {
|
|
123
|
+ onError?.Invoke(request.error);
|
|
124
|
+ Debug.Log(request.error); //出现错误 输出错误信息
|
|
125
|
+ }
|
|
126
|
+ else
|
|
127
|
+ {
|
|
128
|
+ long index = 0;
|
|
129
|
+ while (_currentDownloadSize < _totalSzie)
|
|
130
|
+ {
|
|
131
|
+ if (IsStop) break;
|
|
132
|
+ byte[] data = request.downloadHandler.data;
|
|
133
|
+ if (data != null)
|
|
134
|
+ {
|
|
135
|
+ long length = data.Length - index;
|
|
136
|
+ fs.Write(data, (int)index, (int)length);
|
|
137
|
+ index += length;
|
|
138
|
+ _currentDownloadSize += length;
|
|
139
|
+ onProgress?.Invoke((float)Math.Round((double)_currentDownloadSize / _totalSzie,2), _totalSzie, _currentDownloadSize);
|
|
140
|
+ if (_currentDownloadSize >= _totalSzie)
|
|
141
|
+ {
|
|
142
|
+ //下载完成
|
|
143
|
+ IsLeisure = true;
|
|
144
|
+ onDownloadOver?.Invoke();
|
|
145
|
+ break;
|
|
146
|
+ }
|
|
147
|
+ }
|
|
148
|
+ await Task.Yield();
|
|
149
|
+ }
|
|
150
|
+ }
|
|
151
|
+ }
|
|
152
|
+ }
|
|
153
|
+ }
|
|
154
|
+ }
|
|
155
|
+}
|