少年修仙传客户端代码仓库
lcy
2024-12-16 a39c35fc6449430cd02bccb681c4a0a880e46cd9
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using vnxbqy.UI;
using System.Threading;
 
 
public class DownLoadAndDiscompressTask : Singleton<DownLoadAndDiscompressTask>
 
{
    public const int BYTE_PER_KILOBYTE = 1024;
    public const int BYTE_PER_MILLIONBYTE = 1048576;
 
    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 bool restartApp { get; private set; }
    public bool reinitedBuiltInAsset { 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;
        restartApp = false;
        reinitedBuiltInAsset = false;
        totalSize = 0;
 
        for (int i = 0; i < tasks.Count; i++)
        {
            var task = tasks[i];
            totalSize += task.size;
#if UNITY_ANDROID
            if (!restartApp && task.GetAssetCategory() == AssetVersion.AssetCategory.Dll)
            {
                restartApp = true;
            }
#endif
 
            if (!reinitedBuiltInAsset && task.relativePath.Contains("builtin"))
            {
                reinitedBuiltInAsset = true;
            }
        }
 
        if (totalSize > BYTE_PER_MILLIONBYTE)
        {
            WindowCenter.Instance.OpenFromLocal<DownLoadWin>();
        }
        else
        {
            StartDownLoad();
        }
        ynmbxxjUtil.Instance.DownloadStart();
    }
 
    public void StartDownLoad()
    {
        step = Step.DownLoad;
        SnxxzGame.Instance.StartCoroutine(Co_StartDownLoad());
 
        GameNotice.OpenGameNotice();
    }
 
    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(VersionUtility.Instance.versionInfo.GetResourcesURL(VersionConfig.Get().branch), Language.fixPath, "/", assetVersion.relativePath);
                var localURL = StringUtility.Contact(ResourcesPath.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(true);
                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;
        WindowCenter.Instance.Close<DownLoadWin>();
        WindowCenter.Instance.Close<InGameDownLoadWin>();
 
        if (downLoadOkCallBack != null)
        {
            downLoadOkCallBack();
            downLoadOkCallBack = null;
        }
 
        try
        {
            if (reinitedBuiltInAsset)
            {
                AssetBundleUtility.Instance.ReInitBuiltInAsset();
            }
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex);
        }
 
        if (restartApp)
        {
            ynmbxxjUtil.Instance.RestartApp();
        }
        ynmbxxjUtil.Instance.DownloadEnd();
    }
 
    // private void OnFileDownLoadCompleted(bool _ok, AssetVersion _assetVersion)
    // {
    //     var correctFile = false;
    //     if (_ok)
    //     {
    //         correctFile = _assetVersion.CheckLocalFileValid(true);
    //     }
 
    //     if (_ok && correctFile)
    //     {
    //         okCount++;
    //         _assetVersion.localValid = true;
    //     }
    //     else
    //     {
    //         ExceptionCatcher.ReportException("游戏启动阶段文件下载失败", "文件名:" + _assetVersion.relativePath);
    //         var remoteURL = StringUtility.Contact(VersionUtility.Instance.versionInfo.GetResourcesURL(VersionConfig.Get().branch), "/", Language.Id, "/", _assetVersion.relativePath);
    //         var localURL = StringUtility.Contact(ResourcesPath.Instance.ExternalStorePath, _assetVersion.relativePath);
 
    //         var remoteFile = new RemoteFile();
    //         remoteFile.Init(remoteURL, localURL, _assetVersion);
    //         remoteFile.Begin(OnFileDownLoadCompleted);
    //     }
    // }
 
    public enum Step
    {
        None,
        DownLoadPrepared,
        DownLoad,
        Completed,
    }
 
}