AkiraPixelWind/Assets/Scripts/Main/CustomsComponent/UpdateComponent.cs
2022-12-29 18:20:40 +08:00

188 lines
6.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 判断 版本号 是不是需要重新下载新的版本? 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 暂时不实现此功能
Log.Info($"请下载最新版本...local: {HotData.curHot.version_local} /remote: {HotData.curHot.version_remote}");
//网络错误,提示是否重新下载
AppEntry.ShowMessageBox("系统提示", "当前游戏版本过旧,是否前往更新", "确定", "退出",
() => { GotoUpdateApp(); },
() => { AppEntry.QuitGame(); });
return false;
}
}
}
#if UNITY_IOS
//1 如果是送检版本 不需要热更新
if (Application.version == HotData.xml.versionCheck || HotData.xml.versionCheck == "0.0")
{
//开始加载
HotData.curHot.state = 1;
HotData.curHot.diffCount = 0;
HotData.curHot.diff.Clear();
Debug.Log("送检版本 beginLoad..." + HotData.xml.versionCheck);
return false;
}
#endif
//如果没有本地文件,直接返回
//if (HotData.curHot.local.Count == 0)
//{
// _process = 1;
// HotData.curHot.diff = new List<FileData>(HotData.curHot.remote.Values);
// return true;
//}
//2 查找差分文件
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);
//从本地版本文件查找
if (jd != null)
{
//如果有,并且哈希和文件长度一样
if (jd.hash == d.hash && jd.length == d.length)
{
//默认本地路径为Application.persistentDataPath路径不再判断StreamAssets路径
string path = Utility.Path.GetUpdateWriteFilePath(d.filename);
bool isexitst = File.Exists(path);
//最后判断文件是否存在,如果在,就不需要更新
if (isexitst)
continue;
}
}
else
{
if (!HadFile(d))
continue;
}
//没有的文件是否删除?
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)
{
//如果本地版本文件里不存在
string path = Utility.Path.GetUpdateWriteFilePath(d.filename);
bool isexitst = File.Exists(path);
//判断文件是否存在,如果在
if (isexitst)
{
string filemd5 = string.Empty;
int fileLen = 0;
try
{
//计算md5
using (var fileStream = File.OpenRead(path))
{
fileLen = (int)fileStream.Length;
var md5 = MD5.Create();
var fileMD5Bytes = md5.ComputeHash(fileStream);//计算指定Stream 对象的哈希值
filemd5 = System.BitConverter.ToString(fileMD5Bytes).Replace("-", "").ToLower();//将byte[]装换成字符串
}
}
catch (System.Exception ex) { }
//并且哈希和文件长度一样,则不需要下载
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);
}
}
}