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
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Thursday, March 15, 2018
//--------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
using LitJson;
using System.IO;
using System.Text;
 
 
 
public class VersionUtility : Singleton<VersionUtility>
{
    public static readonly string[] VERSION_URL = new string[] {
        "http://gamecenter.secondworld.net.cn:11000/center/appversion_new.php/?"};
 
    public string androidRoot { get { return string.Empty;/*StringUtility.Contact(SDKUtils.Instance.DeviceRootPath, "/", VersionConfig.Get().bundleIdentifier);*/ } }
 
    public DownloadTask downloadTask;
    private string versionUrl;
    public float progress
    {
        get { return downloadTask == null ? 0 : (downloadTask.downloadedBytes / ((float)GetApkSize() * 1024)); }
    }
 
    public VersionInfo versionInfo { get; private set; }
    Dictionary<string, ApkDownUrl> apkDownUrls = new Dictionary<string, ApkDownUrl>();
 
    public bool completed { get { return step == Step.Completed; } }
 
    Step m_Step = Step.None;
    public Step step
    {
        get { return m_Step; }
        private set
        {
            if (m_Step != value)
            {
                m_Step = value;
                if (downLoadStepChangeEvent != null)
                {
                    downLoadStepChangeEvent(m_Step);
                }
            }
        }
    }
 
    public event Action<Step> downLoadStepChangeEvent;
 
    int urlIndex = 0;
 
    public void RequestVersionCheck()
    {
        m_Step = Step.None;
        //var tables = new Dictionary<string, string>();
        //tables["channel"] = VersionConfig.Get().appId;
        //tables["versioncode"] = VersionConfig.Get().version;
        //if (VersionConfig.Get().branch != 0)
        //{
        //    tables["branch"] = VersionConfig.Get().branch.ToString();
        //}
 
        //tables["game"] = VersionConfig.Get().gameId;
 
        //var url = StringUtility.Contact(VERSION_URL[urlIndex % 2], HttpRequest.HashtablaToString(tables));
        //urlIndex++;
        //versionUrl = url;
        //Debug.Log("http地址:versionUrl  " + url);
        //HttpRequest.Instance.RequestHttpGet(url, HttpRequest.defaultHttpContentType, 1, OnVersionCheckResult);
        OnVersionCheckResult(true, LocalResManager.versionUrlResult);
    }
 
    private void OnVersionCheckResult(bool _ok, string _result)
    {
        if (_ok)
        {
            //_result = _result.Replace("{}", "null");
            versionInfo = JsonMapper.ToObject<VersionInfo>(_result);
            versionInfo.downAsset = 1;
            if (NeedNormalUpdate())
            {
                if (Application.isMobilePlatform || Application.platform == RuntimePlatform.WindowsEditor)
                {
                    RequestApkDownConfigs();
                }
                else
                {
                    step = Step.Completed;
                }
            }
            else
            {
                step = Step.Completed;
 
                var apkFiles = new DirectoryInfo(ResourcesPath.Instance.ExternalStorePath).GetFiles("*.apk");
                for (int i = apkFiles.Length - 1; i >= 0; i--)
                {
                    File.Delete(apkFiles[i].FullName);
                }
            }
        }
        else
        {
            Debug.Log("Aot 请求失败" + _result);
            //Debug.Log("http 数据通讯: VersionUtility:" + versionUrl + "  result:" + _result);
            ////BuglyAgent.ReportException(new System.Exception(),"http 数据通讯: VersionUtility:" +versionUrl + "  result:" + _result);
            //step = Step.None;
            //Clock.AlarmAt(DateTime.Now + new TimeSpan(TimeSpan.TicksPerSecond), RequestVersionCheck);
        }
    }
 
 
    public void RequestApkDownConfigs()
    {
        //  YLTODO
        // UIManager.Instance.OpenWindow<VersionUpdateWin>();
        //  // WindowCenter.Instance.OpenFromLocal<VersionUpdateWin>();
    }
 
    public bool NeedNormalUpdate()
    {
        return versionInfo != null && versionInfo.downAsset == 1 && versionInfo.VersionCount > 0;
    }
 
    public ApkDownUrl GetApkDownUrl()
    {
        var flag = SDKUtils.Instance.GetSplicePackageID();
        if (apkDownUrls.ContainsKey(flag))
            return apkDownUrls[flag];
        else return null;
    }
 
    public string GetApkLocalUrl()
    {
        var apk = GetApkDownUrl();
        if (apk == null)
            return string.Empty;
        if (!apk.isApk)
            return string.Empty;
        var remoteURL = GetApkRemoteUrl();
        var fileName = Path.GetFileName(remoteURL);
        return StringUtility.Contact(androidRoot, "/", fileName);
    }
 
    public string GetApkRemoteUrl()
    {
        if (versionInfo == null)
        {
            return string.Empty;
        }
 
        var flag = SDKUtils.Instance.GetSplicePackageID();
        if (apkDownUrls.ContainsKey(flag))
        {
            return apkDownUrls[flag].url;
        }
        else
        {
            return string.Empty;
        }
 
    }
 
    public int GetApkSize()
    {
        if (versionInfo == null)
        {
            return 0;
        }
 
 
        var flag = SDKUtils.Instance.GetSplicePackageID();
        if (apkDownUrls.ContainsKey(flag))
        {
            return apkDownUrls[flag].size;
        }
        else
        {
            return 0;
        }
 
    }
 
    public string GetUpdateContent()
    {
        var apk = GetApkDownUrl();
        if (apk == null)
            return string.Empty;
        return apk.updateContent;
    }
 
    public void StartDownLoad()
    {
        step = Step.DownLoad;
        var remoteURL = GetApkRemoteUrl();
        var apkLocalUrl = GetApkLocalUrl();
 
        downloadTask = new DownloadTask(remoteURL, apkLocalUrl);
        downloadTask.BeginDownload(OnDownLoadApkCompleted);
    }
 
    private void OnDownLoadApkCompleted(DownloadTask task)
    {
        if (task.IsDone)
        {
            step = Step.Completed;
            UIManager.Instance.CloseWindow<VersionUpdateWin>();
            //  // WindowCenter.Instance.Close<VersionUpdateWin>();
            SDKUtils.Instance.InstallAPK(GetApkLocalUrl());
        }
        else
        {
            step = Step.DownLoadFailed;
        }
    }
 
    public void SkipVersion()
    {
        step = Step.Completed;
    }
 
 
    /// <summary>
    /// 是否为ios审核时间内
    /// </summary>
    /// <returns></returns>
    public bool InIosAuditTime()
    {
        DateTime dateTime;
 
        try
        {
            var textAsset = Resources.Load<UnityEngine.TextAsset>("Config/AuditTime");
            var content = string.Empty;
            if (textAsset != null)
            {
                content = textAsset.text;
                if (DateTime.TryParse(content, out dateTime))
                {
                    return DateTime.Now < dateTime;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        catch (System.Exception ex)
        {
            return false;
        }
    }
 
    public bool NeedDownAsset()
    {
        if (versionInfo != null && versionInfo.downAsset == 1 && VersionConfig.Get().assetAccess != InstalledAsset.IngoreDownLoad)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
 
    public class VersionInfo
    {
        public JsonData notice_flag;
        public JsonData resource_url;
        public string ResourceAward;
        public int downAsset = 1;   //默认是1 没有代表意义 不清楚逻辑可不要改动
        public string downUrl;
        public int VersionCount;    //大于0代表需要强更
 
 
        public string GetResourcesURL(int _branch)
        {
            if (resource_url != null)
            {
                return resource_url[_branch.ToString()].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
 
        public string GetNoticeURL(int _branch)
        {
            try
            {
                if (notice_flag != null)
                {
                    return notice_flag[_branch.ToString()].ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                Debug.Log(ex);
                return string.Empty;
            }
        }
 
    }
 
    public class ApkDownUrl
    {
        public string flag;
        public string url;
        public int size;
        public bool isApk;//是否是apk下载地址,0:否,1:是
        public string updateContent;//更新内容提示
 
        public ApkDownUrl(string content)
        {
            var strings = content.Split('\t');
 
            flag = strings[0];
            url = strings[1];
            int.TryParse(strings[2], out size);
            isApk = strings[3] == "1";
            updateContent = strings[4];
        }
    }
 
    public enum Step
    {
        None,
        ApkExist,
        DownLoadPrepared,
        DownLoad,
        DownLoadFailed,
        Completed,
    }
 
}