少年修仙传客户端基础资源
dabaoji
2025-06-09 8ee0256378cbf5dbc9d76ed10b60b65a844ef4dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
 
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using UnityEngine;
using UnityEngine.Networking;
 
 
namespace StartAot
{
    public class DownloadMgr : SingletonMonobehaviour<DownloadMgr>
    {
        #region 配置
        public static int MaxRetryTimes = 5;//最大重试的次数
        public static int MaxDownLoadTask = 10;//最大同时下载数量
        public static int TimeOut = 20;//下载超时时间
        #endregion
 
        #region 任务列表
        private Dictionary<string, DownloadTask> allTask = new Dictionary<string, DownloadTask>();    //所有任务组
        private Queue<DownloadTask> waitTask = new Queue<DownloadTask>();    //当前等待下载的任务
        private Dictionary<string, DownloadTask> downloadTask = new Dictionary<string, DownloadTask>();    //当前正在下载的任务
        #endregion 任务列表
 
        private bool isBegin;
        private float startTickcount = 0;
        private Func<bool, DownloadTask, bool> onTaskCompleted;//文件下载完成回调,<是否全部完成,任务对象,return true:立即重试,false:到队尾重试>
 
        //总速度
        private float speed;
        public float Speed { get { UpdateValue(); return speed; } }
        public string SpeedFormat { get { return StringUtility.FormatSpeed(Speed); } }
 
        //总已下载大小
        private long finishedBytes;//已完成任务的大小
        private long downloadedBytes;
        private long downloadedBytesRef;
        public long DownloadedBytes { get { UpdateValue(); return downloadedBytes; } }
 
        public bool IsBegin { get { return isBegin; } }
        public bool IsFinished { get { return allTask.Count == 0; } }
 
 
        //更新数值
        public void UpdateValue()
        {
            if (startTickcount == 0)//任务还未开始
                return;
            float second = Mathf.Abs(Time.time - startTickcount);
            //DebugEx.LogFormat("更新数值:{0}  {1}  {2}", Time.time, startTickcount, second);
            if (second < 1f)//刷新间隔1秒
                return;
            long bytes = 0;
            long length = 0;
            foreach (var pair in downloadTask)
            {
                var task = pair.Value;
                bytes += task.downloadedBytes;
                length += task.remoteLength;
            }
            downloadedBytes = finishedBytes + bytes;
            // totalLength = length;
 
            var delta = downloadedBytes < downloadedBytesRef || downloadedBytesRef == 0 ? 0 : downloadedBytes - downloadedBytesRef;
            downloadedBytesRef = downloadedBytes;
            speed = delta / second;
            startTickcount = Time.time;
        }
 
        //添加任务
        public DownloadMgr AddTask(DownloadTask task)
        {
            if (allTask.ContainsKey(task.Key))
            {
                DebugEx.LogFormat("DownloadMgr-已存在相同的下载任务:{0}", task.Key);
                return this;
            }
            allTask.Add(task.Key, task);
            waitTask.Enqueue(task);
            return this;
        }
 
        //准备下载,重置状态
        public DownloadMgr Prepare()
        {
            Stop();
            this.onTaskCompleted = null;
            //清除所有任务
            allTask.Clear();
            waitTask.Clear();
            downloadTask.Clear();
            finishedBytes = 0;
            return this;
        }
 
        /// <summary>
        /// 开始下载
        /// </summary>
        /// <param name="onTaskCompleted">任务结束回调</param>
        /// <returns></returns>
        public DownloadMgr Begin(Func<bool, DownloadTask, bool> onTaskCompleted = null)
        {
            this.onTaskCompleted = onTaskCompleted;
            startTickcount = Time.time;
            downloadedBytesRef = 0;
            isBegin = true;
            foreach (var pair in downloadTask)
            {
                //重新开始下载任务
                if (pair.Value.BeginDownload(OnTaskCompleted))
                    DebugEx.LogFormat("重新开始下载:{0}", pair.Key);
            }
            return this;
        }
 
        //停止下载
        public void Stop()
        {
            startTickcount = 0;
            isBegin = false;
            foreach (var pair in downloadTask)
            {
                pair.Value.Stop();//停止所有下载任务
            }
        }
 
        private void Update()
        {
            if (!isBegin)
                return;
            if (waitTask.Count == 0)
                return;
            if (downloadTask.Count >= MaxDownLoadTask)
                return;
            var task = waitTask.Dequeue();
            if (task.IsDone)
                return;
            //开始下载
            if (!downloadTask.ContainsKey(task.Key))
                downloadTask.Add(task.Key, task);
            task.BeginDownload(OnTaskCompleted);
        }
 
        // 任务下载完成
        private void OnTaskCompleted(DownloadTask task)
        {
            downloadTask.Remove(task.Key);
            if (task.IsDone)//下载成功
            {
                finishedBytes += task.downloadedBytes;
                allTask.Remove(task.Key);
                this.onTaskCompleted?.Invoke(IsFinished, task);
            }
            else//下载失败
            {
                var prior = this.onTaskCompleted == null ? false : this.onTaskCompleted(IsFinished, task);//返回值为是否立即重试
                                                                                                          //所有任务中不存在,说明已经被清除了,就不重新排队
                if (allTask.ContainsKey(task.Key))
                {
                    if (prior) //不排队,直接重新开始
                    {
                        downloadTask.Add(task.Key, task);
                        if (isBegin)
                            task.BeginDownload(OnTaskCompleted);
                    }
                    else
                        waitTask.Enqueue(task); //重新排队
                }
            }
            //TODO:这里暂时用无限重复下载,不做上限处理
            // if (task.failedTimes >= MaxRetryTimes)//重试次数已达到上限,任务失败
            // task.team.OnFailed(task);
            // else//重试次数未达到上限,重新加入等待队列
            // waitTask.Enqueue(task);
        }
    }
 
 
 
    public class DownloadTask
    {
        //下载任务状态
        public enum TaskState
        {
            Wait,
            Working,
            Succeed,
            Failed
        }
        public readonly string remoteUrl; //远程连接地址
        public readonly string localFile; //本地地址
        public readonly string tempFile;//临时文件地址
        private readonly bool clearCache;//是否清除旧的下载缓存,(断点续传)
        public readonly object obj; //自定义对象
        private Action<DownloadTask> onCompleted;//下载完成回调
        private TaskState state;//下载状态
 
        public bool isStop;//下载被停止
        public int failedTimes = 0;//失败的次数
        private bool requestHeaderOk = false;//是否已获取头信息
        private System.DateTime remoteLastModified; //远端文件最后修改时间
        public long remoteLength;//远端文件大小
        private bool isAcceptRange = false;//服务端是否支持断点续传
        private string errorStr;//错误信息
        public long downloadedBytes;//当前已下载数据
        public float downloadedSpeed;//当前下载速度
        private float beginTickCount;
 
        private UnityWebRequest fileRequest;
 
        public string Key { get { return remoteUrl; } }
 
        public string SpeedFormat { get { return StringUtility.FormatSpeed(downloadedSpeed); } }
 
        //本地文件大小
        public long LocalLength { get { return (File.Exists(tempFile)) ? (new FileInfo(tempFile)).Length : 0L; } }
 
        //本地文件最后修改时间
        private System.DateTime LocalLastModified { get { return File.Exists(tempFile) ? File.GetLastWriteTime(tempFile) : DateTime.MinValue; } }
        public bool IsWorking { get { return state == TaskState.Working; } }
        public bool IsDone { get { return state == TaskState.Succeed; } }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="remoteUrl">下载地址</param>
        /// <param name="localUrl">本地保存地址</param>
        /// <param name="obj">自定义对象</param>
        /// <param name="clearCache">是否清除上次下载的缓存</param>
        public DownloadTask(string remoteUrl, string localUrl, object obj = null, bool clearCache = false)
        {
            this.remoteUrl = remoteUrl;
            this.localFile = localUrl;
            this.tempFile = localFile + ".temp";
            this.obj = obj;
            this.clearCache = clearCache;
            this.state = TaskState.Wait;
        }
 
        private bool IsOutdated //本地文件是否已过时
        {
            get
            {
                if (File.Exists(tempFile))
                    return remoteLastModified > LocalLastModified;
                return false;
            }
        }
 
        private void SetSucceed()
        {
            state = TaskState.Succeed;
        }
 
        private void SetFailed(string error)
        {
            this.state = TaskState.Failed;
            this.errorStr = error;
            this.failedTimes++;
        }
 
        public void BeginGetHeader()
        {
            if (IsWorking)
                return;
            DownloadMgr.Instance.StartCoroutine(Co_GetHeader());
        }
 
        public bool BeginDownload(Action<DownloadTask> onCompleted)
        {
            if ((IsWorking && requestHeaderOk) || IsDone)//已经在下载任务中或已经下载完成了
                return false;
            if (state == TaskState.Wait)//第一次尝试
                failedTimes = 0;
            this.onCompleted = onCompleted;
            this.isStop = false;
            DownloadMgr.Instance.StartCoroutine(Co_DownloadFile());
            return true;
        }
 
        //停止下载
        public void Stop()
        {
            isStop = true;
            if (fileRequest != null && !fileRequest.isDone)
                fileRequest.Abort();
            DebugEx.LogFormat("暂停任务:{0}", remoteUrl);
        }
 
        //获取文件头信息
        private IEnumerator Co_GetHeader()
        {
            this.state = TaskState.Working;
            FileExtersion.MakeSureDirectory(tempFile);
            DebugEx.LogFormat("开始获取头信息:{0}", remoteUrl);
            using (var www = UnityWebRequest.Head(remoteUrl))
            {
                www.timeout = DownloadMgr.TimeOut;
                yield return www.SendWebRequest();
                if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
                {
                    DebugEx.LogErrorFormat("头信息获取失败:{0};error:{1}", remoteUrl, www.error);
                    requestHeaderOk = false;
                    SetFailed(www.error);
                    www.Dispose();
                    yield break;
                }
                long.TryParse(www.GetResponseHeader(HttpHeader.ContentLength), out this.remoteLength);
                this.remoteLastModified = DateTime.Parse(www.GetResponseHeader(HttpHeader.LastModified));
                var acceptRange = www.GetResponseHeader(HttpHeader.AcceptRanges);
                this.isAcceptRange = acceptRange != "none";
                DebugEx.LogFormat("头信息获取成功:{0};大小:{1};修改时间:{2};是否支持续传:{3}", remoteUrl, remoteLength, remoteLastModified, acceptRange);
                requestHeaderOk = true;
                state = TaskState.Wait;
                www.Dispose();
            }
        }
        private IEnumerator Co_DownloadFile()
        {
            state = TaskState.Working;
            while (Time.time < beginTickCount + 5)//限制重新下载的时间间隔
            {
                if (isStop)
                    yield break;
                else
                    yield return null;
            }
            beginTickCount = Time.time;
            if (!requestHeaderOk)
                yield return Co_GetHeader();
            if (!requestHeaderOk)//头文件获取失败
            {
                onCompleted?.Invoke(this);
                yield break;
            }
            try
            {
                if (clearCache && File.Exists(tempFile))
                {
                    File.Delete(localFile);//清除上次下载的临时文件
                    DebugEx.LogFormat("已清除缓存文件:{0}", tempFile);
                }
            }
            catch (Exception e)
            {
                DebugEx.LogFormat("清除缓存文件失败:{0};error:{1}", tempFile, e);
            }
            if (File.Exists(tempFile))//临时文件存在
            {
                var _localLength = LocalLength;
                var _outdated = IsOutdated;
                //文件完整且未过期,无须再下载了
                if (_localLength == remoteLength && !_outdated)
                {
                    yield return Move(tempFile, localFile);//把临时文件改名为正式文件
                    onCompleted?.Invoke(this);
                    yield break;
                }
                else if (_localLength > remoteLength || _outdated)//文件不对或过期了
                {
                    if (!_outdated) DebugEx.LogFormat("本地文件大于服务器端文件:{0}", tempFile);
                    if (_outdated) DebugEx.LogFormat("文件已过期:{0}", tempFile);
                    try
                    {
                        if (File.Exists(tempFile))
                            File.Delete(tempFile);
                    }
                    catch (Exception e)
                    {
                        DebugEx.LogErrorFormat("删除临时文件出错:{0};error:{1}", tempFile, e);
                        isAcceptRange = false;//如果删除临时文件出错,取消续传,覆写文件
                    }
                }
            }
            if (isStop)//任务被暂停
            {
                state = TaskState.Wait;
                yield break;
            }
            DebugEx.LogFormat("下载任务开始:{0}", remoteUrl);
            fileRequest = UnityWebRequest.Get(remoteUrl);
 
            fileRequest.downloadHandler = new DownloadHandlerFile(tempFile, this.isAcceptRange);
            if (LocalLength != 0L && isAcceptRange)
                fileRequest.SetRequestHeader(HttpHeader.Range, "bytes=" + LocalLength + "-");
            DebugEx.LogFormat("下载请求范围:{0} ;bytes={1}", remoteUrl, LocalLength);
            var localLength = LocalLength;
            this.downloadedBytes = localLength;
            fileRequest.SendWebRequest();
            while (!fileRequest.isDone)
            {
                var length = localLength + (long)fileRequest.downloadedBytes;//本地文件大小+本次下载的数据大小
                this.downloadedSpeed = Math.Max(0, (length - this.downloadedBytes) / Time.deltaTime);
                this.downloadedBytes = length;
                yield return null;
            }
            if (fileRequest.result == UnityWebRequest.Result.ConnectionError || fileRequest.result == UnityWebRequest.Result.ProtocolError)
            {
                if (isStop)//任务被暂停
                {
                    state = TaskState.Wait;
                }
                else //任务失败
                {
                    SetFailed(fileRequest.error);
                    onCompleted?.Invoke(this);
                    DebugEx.LogErrorFormat("文件下载失败:{0};error:{1}", remoteUrl, fileRequest.error);
                }
                fileRequest.Dispose();
                fileRequest = null;
                yield break;
            }
            DebugEx.LogFormat("文件下载成功:{0};", tempFile);
            fileRequest.Dispose();
            fileRequest = null;
 
            FileInfo tempFileInfo = new FileInfo(tempFile);
            if (tempFileInfo.Exists)
            {
                //判断临时文件和远程文件大小是否一致
                if (tempFileInfo.Length != remoteLength && remoteLength != 0L)
                {
                    DebugEx.LogErrorFormat("下载完成后但是大小不一致:{0}; {1} || {2}", remoteUrl, tempFileInfo.Length, remoteLastModified);
                    SetFailed("文件下载完成后但是大小不一致");
                }
                else
                {  //大小一致 
                    yield return Move(tempFile, localFile);
                }
            }
            else
            {   //临时文件不存在
                DebugEx.LogErrorFormat("下载完成但是临时文件不存在:{0}", tempFile);
                SetFailed("下载完成但是文件不存在");
            }
            onCompleted?.Invoke(this);
            yield return null;
        }
 
        //拷贝文件,把临时文件改成正式文件
        private IEnumerator Move(string sourceFile, string destFile)
        {
            var copyState = 0;//复制文件状态,0等待,1成功,2失败
            try
            {
                // if (speedLimit)
                ThreadPool.QueueUserWorkItem((object _obj) =>
                {
                    if (File.Exists(destFile))
                        File.Delete(destFile);
                    File.Move(sourceFile, destFile);
                    System.Threading.Interlocked.Exchange(ref copyState, 1);
                });
                // else
                // {
                //     if (File.Exists(destFile))
                //         File.Delete(destFile);
                //     File.Move(sourceFile, destFile);
                //     copyState = 1;
                // }
            }
            catch (Exception ex)
            {
                DebugEx.LogErrorFormat("临时文件改名出错:{0}", ex);
                copyState = 1;
            }
            while (copyState == 0)
                yield return null;
            if (copyState == 1)
            {
                DebugEx.LogFormat("文件复制成功:{0}", localFile);
                SetSucceed();
            }
            else
                SetFailed("临时文件改名出错");
        }
 
    }
 
}