AkiraPixelWind/Assets/Scripts/Main/CustomsComponent/UpdateComponent.cs

188 lines
6.2 KiB
C#
Raw Permalink Normal View History

2022-12-29 18:20:40 +08:00
using Axibug;
using Axibug.Runtime;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using UnityEngine;
using static Axibug.Utility;
namespace Game
{
public class UpdateComponent : GameComponent
{
private float _process = 0;
public float ProcessValue { get { return _process; } }
public bool CheckVersionList()
{
//1 <20>ж<EFBFBD> <20><EFBFBD><E6B1BE> <20>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µİ汾<C4B0><E6B1BE> 1.0 2.0
string[] arr1 = HotData.curHot.version_local.Split('.');
string[] arr2 = HotData.curHot.version_remote.Split('.');
if (HotData.curHot.filename == HotPlatform.versionTxt)
{
if (arr1.Length == arr2.Length && arr1.Length == 2)
{
if (arr1[0] != arr2[0])
{
//@Test <20><>ʱ<EFBFBD><CAB1>ʵ<EFBFBD>ִ˹<D6B4><CBB9><EFBFBD>
Log.Info($"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>°汾...local: {HotData.curHot.version_local} /remote: {HotData.curHot.version_remote}");
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
AppEntry.ShowMessageBox("ϵͳ<CFB5><CDB3>ʾ", "<22><>ǰ<EFBFBD><C7B0>Ϸ<EFBFBD><EFBFBD><E6B1BE><EFBFBD>ɣ<EFBFBD><C9A3>Ƿ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", <><C8B7>", "<22>˳<EFBFBD>",
() => { GotoUpdateApp(); },
() => { AppEntry.QuitGame(); });
return false;
}
}
}
#if UNITY_IOS
//1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><20><><EFBFBD><EFBFBD>Ҫ<EFBFBD>ȸ<EFBFBD><C8B8><EFBFBD>
if (Application.version == HotData.xml.versionCheck || HotData.xml.versionCheck == "0.0")
{
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
HotData.curHot.state = 1;
HotData.curHot.diffCount = 0;
HotData.curHot.diff.Clear();
Debug.Log("<22>ͼ<EFBFBD><CDBC>汾 beginLoad..." + HotData.xml.versionCheck);
return false;
}
#endif
//<2F><><EFBFBD><EFBFBD>û<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>
//if (HotData.curHot.local.Count == 0)
//{
// _process = 1;
// HotData.curHot.diff = new List<FileData>(HotData.curHot.remote.Values);
// return true;
//}
//2 <20><><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>ļ<EFBFBD>
HotData.curHot.curBytes = 0;
HotData.curHot.totalBytes = 0;
int idx = 0;
foreach (var d in HotData.curHot.remote.Values)
{
idx++;
_process = (float)idx / HotData.curHot.remote.Count;
FileData jd = null;
HotData.curHot.local.TryGetValue(d.filename, out jd);
//<2F>ӱ<EFBFBD><D3B1>ذ汾<D8B0>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
if (jd != null)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>ҹ<EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
if (jd.hash == d.hash && jd.length == d.length)
{
//Ĭ<>ϱ<EFBFBD><CFB1><EFBFBD>·<EFBFBD><C2B7>ΪApplication.persistentDataPath·<68><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>StreamAssets·<73><C2B7>
string path = Utility.Path.GetUpdateWriteFilePath(d.filename);
bool isexitst = File.Exists(path);
//<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3>Ͳ<EFBFBD><CDB2><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
if (isexitst)
continue;
}
}
else
{
if (!HadFile(d))
continue;
}
//û<>е<EFBFBD><D0B5>ļ<EFBFBD><C4BC>Ƿ<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>
HotData.curHot.totalBytes += d.length;
HotData.curHot.diff.Add(d);
}
HotData.curHot.diffCount = HotData.curHot.diff.Count;
return HotData.curHot.diffCount > 0;
}
private bool HadFile(FileData d)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ذ汾<D8B0>ļ<EFBFBD><C4BC><EFBFBD><EFB2BB><EFBFBD><EFBFBD>
string path = Utility.Path.GetUpdateWriteFilePath(d.filename);
bool isexitst = File.Exists(path);
//<2F>ж<EFBFBD><D0B6>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (isexitst)
{
string filemd5 = string.Empty;
int fileLen = 0;
try
{
//<2F><><EFBFBD><EFBFBD>md5
using (var fileStream = File.OpenRead(path))
{
fileLen = (int)fileStream.Length;
var md5 = MD5.Create();
var fileMD5Bytes = md5.ComputeHash(fileStream);//<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>Stream <20><><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD>ϣֵ
filemd5 = System.BitConverter.ToString(fileMD5Bytes).Replace("-", "").ToLower();//<2F><>byte[]װ<><D7B0><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
}
}
catch (System.Exception ex) { }
//<2F><><EFBFBD>ҹ<EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
if (d.hash == filemd5 && d.length == fileLen)
return false;
}
return true;
}
private void GotoUpdateApp()
{
string url = null;
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
url = AppEntry.BuiltinData.BuildInfo.WindowsAppUrl;
#elif UNITY_IOS
url = AppEntry.BuiltinData.BuildInfo.IOSAppUrl;
#elif UNITY_ANDROID
url = AppEntry.BuiltinData.BuildInfo.AndroidAppUrl;
#endif
if (!string.IsNullOrEmpty(url))
{
UnityEngine.Application.OpenURL(url);
}
}
public string GetByteLengthString(long byteLength)
{
if (byteLength < 1024L) // 2 ^ 10
{
return Text.Format("{0} Bytes", byteLength);
}
if (byteLength < 1048576L) // 2 ^ 20
{
return Text.Format("{0:F2} KB", byteLength / 1024f);
}
if (byteLength < 1073741824L) // 2 ^ 30
{
return Text.Format("{0:F2} MB", byteLength / 1048576f);
}
if (byteLength < 1099511627776L) // 2 ^ 40
{
return Text.Format("{0:F2} GB", byteLength / 1073741824f);
}
if (byteLength < 1125899906842624L) // 2 ^ 50
{
return Text.Format("{0:F2} TB", byteLength / 1099511627776f);
}
if (byteLength < 1152921504606846976L) // 2 ^ 60
{
return Text.Format("{0:F2} PB", byteLength / 1125899906842624f);
}
return Text.Format("{0:F2} EB", byteLength / 1152921504606846976f);
}
}
}