少年修仙传客户端基础资源
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using StartAotSDK;
 
 
namespace StartAot
{
    public class DownLoadAndDiscompressTask : Singleton<DownLoadAndDiscompressTask>
 
    {
        public const int BYTE_PER_KILOBYTE = 1024;
 
        public float progress { get { return Mathf.Clamp01((float)DownloadMgr.Instance.DownloadedBytes / totalSize); } }
 
        List<AssetVersion> tasks = new List<AssetVersion>();
        public bool isDone { get { return step == Step.Completed; } }
        public int totalSize { get; private set; }
        public int totalCount { get; private set; }
 
        public event Action<Step> downLoadStepChangeEvent;
 
        Action downLoadOkCallBack;
 
        Step m_Step = Step.None;
        public Step step
        {
            get { return m_Step; }
            set
            {
                if (m_Step != value)
                {
                    m_Step = value;
 
                    if (downLoadStepChangeEvent != null)
                    {
                        downLoadStepChangeEvent(m_Step);
                    }
                }
            }
        }
 
        public void Prepare(List<AssetVersion> _downLoadTasks, Action _downLoadOkCallBack)
        {
            tasks = _downLoadTasks;
            downLoadOkCallBack = _downLoadOkCallBack;
 
            totalCount = tasks.Count;
            step = Step.DownLoadPrepared;
            totalSize = 0;
 
            for (int i = 0; i < tasks.Count; i++)
            {
                var task = tasks[i];
                totalSize += task.size;
            }
 
            StartDownLoad();
 
        }
 
        public void StartDownLoad()
        {
            step = Step.DownLoad;
            DownloadMgr.Instance.StartCoroutine(Co_StartDownLoad());
 
        }
 
        IEnumerator Co_StartDownLoad()
        {
            while (tasks.Count > 0)
            {
                DownloadMgr.MaxDownLoadTask = 10;
                DownloadMgr.Instance.Prepare();
                for (int i = 0; i < tasks.Count; i++)
                {
                    var assetVersion = tasks[i];
                    var remoteURL = StringUtility.Contact(ResourcesModel.Instance.versionInfo.GetResourcesURL(VersionConfigEx.Get().branch), ResourcesModel.fixPath, "/", assetVersion.relativePath);
                    var localURL = StringUtility.Contact(ResourcesModel.Instance.ExternalStorePath, assetVersion.relativePath);
                    DownloadMgr.Instance.AddTask(new DownloadTask(remoteURL, localURL));
                }
 
                DownloadMgr.Instance.Begin();
 
                while (!DownloadMgr.Instance.IsFinished)
                {
                    yield return null;
                }
 
                //检查下载完成后的文件是否有效
                var filledTasks = new List<AssetVersion>();//无效的文件
                for (int i = 0; i < tasks.Count; i++)
                {
                    var assetVersion = tasks[i];
                    var correctFile = assetVersion.CheckLocalFileValid();
                    if (correctFile)
                        assetVersion.localValid = true;
                    else
                        filledTasks.Add(assetVersion);
                }
                if (filledTasks.Count > 0)
                    DebugEx.LogErrorFormat("资源下载完后有{0}个文件无效({1}),重新开始下载", filledTasks.Count, filledTasks[0].fileName);
                tasks = filledTasks;
            }
 
            DebugEx.Log("资源全部下载完成");
 
            step = Step.Completed;
 
            if (downLoadOkCallBack != null)
            {
                downLoadOkCallBack();
                downLoadOkCallBack = null;
            }
 
        }
 
        public enum Step
        {
            None,
            DownLoadPrepared,
            DownLoad,
            Completed,
        }
 
    }
 
}