三国卡牌客户端基础资源仓库
yyl
2025-04-23 7816f15f2e98a0faa2bdbc50307c7506cbb9a6b7
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cysharp.Threading.Tasks;
using System;
 
    /// <summary>
    /// 热更新状态
    /// </summary>
    public class HotUpdateState : ILaunchState
    {
        private bool isHotUpdateEnabled;
        private LaunchUtility.UpdateType updateType;
        
        // 使用硬编码索引
        public string StateName => FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.HotUpdate);
        
        public HotUpdateState(bool isHotUpdateEnabled, LaunchUtility.UpdateType updateType)
        {
            this.isHotUpdateEnabled = isHotUpdateEnabled;
            this.updateType = updateType;
        }
        
        public async UniTask OnEnter()
        {
            Debug.Log("进入热更新状态");
            
            // 使用硬编码索引设置当前状态
            LaunchLoadingWin.Instance.SetCurrentState(FirstPackLang.TextIndex.HotUpdate);
            await LaunchLoadingWin.Instance.ShowProgressAsync(0.2f, 0.8f);
        }
        
        public async UniTask<bool> OnExecute()
        {
            Debug.Log("执行热更新");
            
            // 如果热更新未启用或在编辑器模式下,跳过热更新
            if (!isHotUpdateEnabled || Application.isEditor)
            {
                Debug.Log("热更新未启用或在编辑器模式下,跳过热更新");
                
                LaunchLoadingWin.Instance.SetTip(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.SkipHotUpdate));
                await LaunchLoadingWin.Instance.ShowProgressAsync(0.2f, 0.9f);
                
                return true;
            }
            
            // 根据更新类型执行不同的更新逻辑
            switch (updateType)
            {
                case LaunchUtility.UpdateType.None:
                    Debug.Log("当前已是最新版本,无需更新");
                    LaunchLoadingWin.Instance.SetTip(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.LatestVersion));
                    await LaunchLoadingWin.Instance.ShowProgressAsync(0.2f, 0.9f);
                    break;
                    
                case LaunchUtility.UpdateType.PatchUpdate:
                    Debug.Log("检测到小版本更新,更新后可直接进入游戏");
                    LaunchLoadingWin.Instance.SetTip(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.DownloadingUpdate));
                    await DownloadAndApplyUpdate(false);
                    break;
                    
                case LaunchUtility.UpdateType.MinorUpdate:
                    Debug.Log("检测到中版本更新,更新后需要重启");
                    LaunchLoadingWin.Instance.SetTip(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.DownloadingUpdate));
                    await DownloadAndApplyUpdate(true);
                    return false; // 不进入下一个状态,因为需要重启
                    
                case LaunchUtility.UpdateType.MajorUpdate:
                    Debug.Log("检测到大版本更新,需要重新下载");
                    ShowMajorUpdateNotification();
                    // 大版本更新需要用户确认,所以这里不会自动进入下一个状态
                    return false;
            }
            
            
            // 返回true表示该状态已完成,可以进入下一个状态
            return true;
        }
        
        public async UniTask OnExit()
        {
            Debug.Log("退出热更新状态");
            await UniTask.CompletedTask;
        }
        
        /// <summary>
        /// 下载并应用更新
        /// </summary>
        private async UniTask DownloadAndApplyUpdate(bool needRestart)
        {
            // 实现下载和应用更新的逻辑
            Debug.Log("下载并应用更新");
            
            // 模拟下载过程
            for (int i = 0; i < 5; i++)
            {
                LaunchLoadingWin.Instance.SetTip($"{FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.DownloadingUpdate)} {(i + 1) * 20}%");
                float progress = 0.8f + (i * 0.04f);
                await LaunchLoadingWin.Instance.ShowProgressAsync(0.2f, progress);
                
                await UniTask.Delay(200);
            }
            
            // 模拟应用更新
            LaunchLoadingWin.Instance.SetTip(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.ApplyingUpdate));
            await LaunchLoadingWin.Instance.ShowProgressAsync(0.5f, 1.0f);
            
            
            await UniTask.Delay(500);
 
            if (needRestart)
            {
                // 应用更新后需要重启
                RestartApplication();
            }
            
            Debug.Log("更新应用完成");
        }
        
        /// <summary>
        /// 重启应用
        /// </summary>
        private void RestartApplication()
        {
            Debug.Log("重启应用");
            
            // 根据不同平台实现重启逻辑
            #if UNITY_EDITOR
                // 编辑器模式下无法真正重启,只能模拟
                Debug.Log("编辑器模式下模拟重启");
                ShowRestartRequiredDialog();
            #elif UNITY_STANDALONE_WIN
                // Windows平台重启
                System.Diagnostics.Process.Start(Application.dataPath.Replace("_Data", ".exe"));
                Application.Quit();
            #elif UNITY_STANDALONE_OSX
                // macOS平台重启
                string bundlePath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("/Contents"));
                System.Diagnostics.Process.Start("open", bundlePath);
                Application.Quit();
            #elif UNITY_ANDROID
                try {
                    // 尝试使用Android的Intent重启应用
                    using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
                        AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
                        AndroidJavaObject packageManager = currentActivity.Call<AndroidJavaObject>("getPackageManager");
                        AndroidJavaObject intent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", Application.identifier);
                        
                        if (intent != null) {
                            intent.Call<AndroidJavaObject>("addFlags", 0x20000000); // FLAG_ACTIVITY_CLEAR_TASK
                            intent.Call<AndroidJavaObject>("addFlags", 0x00008000); // FLAG_ACTIVITY_NEW_TASK
                            currentActivity.Call("startActivity", intent);
                            currentActivity.Call("finish");
                            
                            // 强制退出进程
                            AndroidJavaClass process = new AndroidJavaClass("android.os.Process");
                            int pid = process.CallStatic<int>("myPid");
                            process.CallStatic("killProcess", pid);
                        } else {
                            // 无法获取启动Intent,显示提示
                            ShowRestartRequiredDialog();
                        }
                    }
                } catch (Exception e) {
                    Debug.LogError($"Android重启失败: {e.Message}");
                    ShowRestartRequiredDialog();
                }
            #elif UNITY_IOS
                // iOS平台无法直接重启应用,显示提示
                ShowRestartRequiredDialog();
            #elif UNITY_WEBGL
                // WebGL平台通过刷新页面重启
                Application.ExternalEval("window.location.reload();");
            #elif UNITY_WECHAT || UNITY_MINI_PROGRAM
                // 小程序平台无法直接重启,显示提示
                ShowRestartRequiredDialog();
            #else
                // 其他平台显示提示
                ShowRestartRequiredDialog();
            #endif
        }
        
        /// <summary>
        /// 显示需要重启的提示对话框
        /// </summary>
        private void ShowRestartRequiredDialog()
        {
            Debug.Log("显示需要重启提示");
            
            // 实现显示重启提示的逻辑
            // 这里可以显示一个对话框,提示用户需要手动重启应用
            
            // 示例:可以通过事件系统触发UI显示
            // EventSystem.Trigger("ShowRestartRequiredDialog");
            
            // 或者直接显示一个简单的UI
            LaunchLoadingWin.Instance.SetTip(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.RestartRequired));
        }
        
        /// <summary>
        /// 显示大版本更新通知
        /// </summary>
        private void ShowMajorUpdateNotification()
        {
            Debug.Log("显示大版本更新通知");
            
            // 实现显示大版本更新通知的逻辑
            // 这里可以显示一个对话框,提示用户需要下载新版本
            
            // 示例:可以通过事件系统触发UI显示
            // EventSystem.Trigger("ShowMajorUpdateDialog");
        }
    }