三国卡牌客户端基础资源仓库
yyl
2025-05-19 2769f4c6422de2d85436b38ee7f5ad92236ed0cf
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
using UnityEngine;
using Cysharp.Threading;
using Cysharp.Threading.Tasks;
using System;
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine.Networking;
using LaunchCommon;
 
public class Launch : MonoBehaviour
{
 
 
#if UNITY_EDITOR
    public bool EnableNetLog = true;
    public bool EnableLog = true;
    public bool EnableLogWarning = true;
    public bool EnableLogError = true;
#else
    public bool EnableNetLog = false;
    public bool EnableLog = false;
    public bool EnableLogWarning = false;
    public bool EnableLogError = false;
#endif
 
    private static Assembly _hotUpdateAss;
 
//网络获取
    private static List<string> AOTMetaAssemblyFiles { get; } = new List<string>();
 
    private static Dictionary<string, byte[]> s_assetDatas = new Dictionary<string, byte[]>();
 
    public static Launch Instance
    {
        get
        {
            return m_Instance;
        }
    }
 
    private static Launch m_Instance;
    private void Awake()
    {
        if (m_Instance != null)
        {
            Debug.LogError("Launch Instance is not null");
            return;
        }
        
        m_Instance = this;
    }
 
    // 启动入口
    async void Start()
    {
        Debug.Log("Launch Start");
 
        InitPlugins();
        InitSetting();
        SDKInit();
 
        // 1. 打开加载界面
        // LaunchLoadingWin.OpenWindow();
    }
 
    private void InitSetting()
    {
        System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
        System.Globalization.CultureInfo.CurrentCulture = culture;
        System.Globalization.CultureInfo.CurrentUICulture = culture;
        System.Globalization.CultureInfo.DefaultThreadCurrentCulture = culture;
        System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = culture;
        System.Net.ServicePointManager.DefaultConnectionLimit = 100;
 
#if UNITY_EDITOR
        //内网下载测试
        _hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Main");
        Type type = _hotUpdateAss.GetType("InGameDownTestUtility");
        LocalResManager.Instance.isPCTestDownLoad = (bool)type.GetMethod("GetReadVerionEx").Invoke(null, null);
#endif
        SDKInit();
        LocalResManager.Instance.Init();
        LocalResManager.Instance.InitTable(() =>
        {
            LocalResManager.Instance.InitDefaultLanguage();
            LaunchLoadingWin.OpenWindow();
            // LocalResManager.Instance.OpenWindow("LaunchExWin", m_UICanvas);
#if !UNITY_EDITOR
            LocalResManager.step = LocalResManager.LoadDllStep.RequestVersion;
#else
            if (LocalResManager.Instance.isPCTestDownLoad)
            {
                LocalResManager.step = LocalResManager.LoadDllStep.RequestVersion;
            }
            else
            {
                StartGame();
            }
#endif
        });
    }
 
    private void InitPlugins()
    {
        DOTween.Init();
    }
 
    private void SDKInit()
    {
 
    }
    
    private void StartGame()
    {
#if !UNITY_EDITOR
        LoadMetadataForAOTAssemblies();
        _hotUpdateAss = Assembly.Load(ReadBytesFromStreamingAssets("Main.dll.bytes"));
        s_assetDatas = null;
#else
        if (_hotUpdateAss == null)
            _hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Main");
#endif
        LocalResManager.step = LocalResManager.LoadDllStep.None;
        // m_UICanvas.gameObject.SetActive(false);
        DestroySingleton();
 
        Type type = _hotUpdateAss.GetType("Launch");
        GameObject go = new GameObject("Launch");
        go.AddComponent(type);
        Debug.Log("进入游戏流程");
    }
 
    private void DestroySingleton()
    {
        if (LocalResManager.IsValid())
        {
            LocalResManager.Destroy();
        }
        if (DownloadMgr.IsValid())
        {
            Destroy(DownloadMgr.Instance);
        }
        if (LogicEngine.IsValid())
        {
            Destroy(LogicEngine.Instance);
        }
        if (HttpRequest.IsValid())
        {
            Destroy(HttpRequest.Instance);
        }
        if (DownLoadAndDiscompressTask.IsValid())
        {
            DownLoadAndDiscompressTask.Destroy();
        }
        stop = true;
    }
 
    bool stop = false;
 
    void Update()
    {
        if (stop)
            return;
        if (LocalResManager.step == LocalResManager.LoadDllStep.None || LocalResManager.step == LocalResManager.LoadDllStep.Wait)
            return;
        else if (LocalResManager.step == LocalResManager.LoadDllStep.RequestVersion)
        {
            LocalResManager.step = LocalResManager.LoadDllStep.Wait;
            LocalResManager.Instance.RequestVersionCheck();
        }
        else if (LocalResManager.step == LocalResManager.LoadDllStep.PrepareDownLoad)
        {
            LocalResManager.step = LocalResManager.LoadDllStep.Wait;
            //下载前准备,读表判断是否需要多语言不同下载路径
            PrepareDownLoad();
        }
        else if (LocalResManager.step == LocalResManager.LoadDllStep.DownLoad)
        {
            LocalResManager.step = LocalResManager.LoadDllStep.Wait;
            BeginDownload();
        }
        else if (LocalResManager.step == LocalResManager.LoadDllStep.ReadBytes)
        {
            LocalResManager.step = LocalResManager.LoadDllStep.Wait;
            ReadDllBytes(this.StartGame);
        }
        //else if (LocalResManager.step == LocalResManager.LoadDllStep.Completed)
        //{
        //    LocalResManager.step = LocalResManager.LoadDllStep.None;
        //    m_UICanvas.gameObject.SetActive(false);
        //    DestroySingleton();
        //}
    }
 
 
    private string GetWebRequestPath(string asset)
    {
        var path = LocalResManager.Instance.GetAssetFilePath(string.Concat(LocalResManager.bytesFolderName, asset));
 
        if (!path.Contains("file:"))
        {
            //ExternalStorePath 路径需要添加
            path = "file://" + path;
        }
        return path;
    }
 
    private async void ReadDllBytes(Action callback)
    {
        foreach (var assetVersion in LocalResManager.Instance.assetVersions.Values)
        {
            if (assetVersion.localValid)
            {
                AOTMetaAssemblyFiles.Add(string.Concat(assetVersion.fileName, assetVersion.extersion));
            }
            else
            {
                Debug.LogErrorFormat("文件无效 {0}", assetVersion.fileName);
            }
        }
 
        foreach (var asset in AOTMetaAssemblyFiles)
        {
            string dllPath = GetWebRequestPath(asset);
            Debug.Log($"dllPath:{dllPath}");
            UnityWebRequest www = UnityWebRequest.Get(dllPath);
            await www.SendWebRequest();
 
#if UNITY_2020_1_OR_NEWER
            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.Log(www.error);
            }
#else
            if (www.isHttpError || www.isNetworkError)
            {
                Debug.Log(www.error);
            }
#endif
            else
            {
                // 特殊处理
                byte[] assetData;
                if (asset == "Assembly-CSharp.dll.bytes")
                {
                    assetData = new byte[www.downloadHandler.data.Length - 3];
                    Array.Copy(www.downloadHandler.data, 3, assetData, 0, assetData.Length);
                }
                else
                {
                    assetData = www.downloadHandler.data;
                }
 
                Debug.Log($"dll:{asset}  size:{assetData.Length}");
                s_assetDatas[asset] = assetData;
            }
        }
 
        callback?.Invoke();
    }
 
    private void PrepareDownLoad()
    {
        LocalResManager.Instance.RequestLogicBytes();
    }
 
    private void BeginDownload()
    {
        List<AssetVersion> priorDownLoadAssetVersions = new List<AssetVersion>();
        foreach (var assetVersion in LocalResManager.Instance.assetVersions.Values)
        {
            AssetVersion localAssetVersion = null;
            LocalResManager.Instance.localAssetVersions.TryGetValue(assetVersion.relativePath, out localAssetVersion);
            if (!assetVersion.CheckLocalFileValid(localAssetVersion))
            {
                priorDownLoadAssetVersions.Add(assetVersion);
                assetVersion.localValid = false;
            }
            else
            {
                assetVersion.localValid = true;
            }
        }
 
        Debug.LogFormat("需要下载的文件数量:{0}", priorDownLoadAssetVersions.Count);
        if (priorDownLoadAssetVersions.Count == 0)
        {
            DownloadComplete();
            return;
        }
        var targetDirectory = LocalResManager.Instance.ExternalStorePath;
        if (!Directory.Exists(targetDirectory))
        {
            Directory.CreateDirectory(targetDirectory);
        }
 
        DownLoadAndDiscompressTask.Instance.Prepare(priorDownLoadAssetVersions, DownloadComplete);
    }
 
    void DownloadComplete()
    {
        LocalResManager.step = LocalResManager.LoadDllStep.ReadBytes;
    }
}