//-------------------------------------------------------- // [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 Snxxz.UI; using System.Text; [XLua.LuaCallCSharp] public class VersionUtility : Singleton { public static readonly string[] VERSION_URL = new string[] { "http://pub.game.2460web.com:11000/appversion/?" , "http://111.230.234.252:11000/appversion/?"}; public string androidRoot { get { return StringUtility.Contact(SDKUtility.Instance.DeviceRootPath, "/", VersionConfig.Get().bundleIdentifier); } } public float progress { get { return RemoteFile.TotalDownloadedSize / ((float)GetApkSize() * 1024); } } public VersionInfo versionInfo { get; private set; } public JsonData maoerVersion; Dictionary apkDownUrls = new Dictionary(); 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 downLoadStepChangeEvent; int urlIndex = 0; public void RequestVersionCheck() { m_Step = Step.None; var tables = new Dictionary(); tables["channel"] = VersionConfig.Get().appId; tables["versioncode"] = VersionConfig.Get().version; if (VersionConfig.Get().branch != 0) { tables["branch"] = VersionConfig.Get().branch.ToString(); } var url = StringUtility.Contact(VERSION_URL[urlIndex % 2], HttpRequest.HashtablaToString(tables)); urlIndex++; HttpRequest.Instance.RequestHttpGet(url, HttpRequest.defaultHttpContentType, 1, OnVersionCheckResult); } private void OnVersionCheckResult(bool _ok, string _result) { if (_ok) { versionInfo = JsonMapper.ToObject(_result); if (NeedNormalUpdate()) { if (Application.isMobilePlatform || Application.platform == RuntimePlatform.WindowsPlayer) { 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 { step = Step.None; Clock.AlarmAt(DateTime.Now + new TimeSpan(TimeSpan.TicksPerSecond), RequestVersionCheck); } } public void RequestApkDownConfigs() { var config = ApkUpdateUrlConfig.Get(VersionConfig.Get().appId, VersionConfig.Get().branch); HttpRequest.Instance.RequestHttpGet(config.url, HttpRequest.defaultHttpContentType, 1, OnGetApkDownConfigs); } private void OnGetApkDownConfigs(bool _ok, string _result) { if (_ok) { var lines = _result.Split(new string[] { FileExtersion.lineSplit }, StringSplitOptions.RemoveEmptyEntries); apkDownUrls.Clear(); for (int i = 3; i < lines.Length; i++) { var apkDownUrl = new ApkDownUrl(lines[i]); apkDownUrls[apkDownUrl.flag] = apkDownUrl; } switch (Application.platform) { case RuntimePlatform.Android: if (File.Exists(GetApkLocalUrl())) { step = Step.ApkExist; WindowCenter.Instance.OpenFromLocal(); } else { step = Step.DownLoadPrepared; WindowCenter.Instance.OpenFromLocal(); } break; case RuntimePlatform.IPhonePlayer: step = Step.DownLoadPrepared; WindowCenter.Instance.OpenFromLocal(); break; case RuntimePlatform.WindowsPlayer: step = Step.DownLoadPrepared; WindowCenter.Instance.OpenFromLocal(); break; default: step = Step.Completed; break; } } else { Clock.AlarmAt(DateTime.Now + new TimeSpan(TimeSpan.TicksPerSecond), RequestApkDownConfigs); } } public bool NeedMaoErUpdate() { if (IsMaoErGame()) { return true; } else { return false; } } public bool NeedNormalUpdate() { if (IsMaoErGame()) { return false; } else { return versionInfo != null && versionInfo.downAsset == 1 && versionInfo.VersionCount > 0; } } public string GetApkLocalUrl() { if (IsMaoErGame()) { return StringUtility.Contact(androidRoot, "/", "maoErGame.apk"); } else { var remoteURL = GetApkRemoteUrl(); var fileName = Path.GetFileName(remoteURL); return StringUtility.Contact(androidRoot, "/", fileName); } } public string GetApkRemoteUrl() { if (versionInfo == null) { return string.Empty; } if (IsMaoErGame()) { if (maoerVersion.Keys.Contains("url")) { return maoerVersion["url"].ToString(); } else { return string.Empty; } } else { var flag = SDKUtility.Instance.GetSplicePackageID(); if (apkDownUrls.ContainsKey(flag)) { return apkDownUrls[flag].url; } else { return string.Empty; } } } public int GetApkSize() { if (versionInfo == null) { return 0; } if (IsMaoErGame()) { return versionInfo.GetLatestVersion().file_size; } else { var flag = SDKUtility.Instance.GetSplicePackageID(); if (apkDownUrls.ContainsKey(flag)) { return apkDownUrls[flag].size; } else { return 0; } } } public string GetUpdateContent() { if (IsMaoErGame()) { if (maoerVersion != null && maoerVersion.Keys.Contains("content")) { return StringUtility.Contact(Language.GetFromLocal(34), "\r\n", maoerVersion["content"].ToString()); } else { return string.Empty; } } else { return string.Empty; } } public void StartDownLoad() { step = Step.DownLoad; var remoteURL = GetApkRemoteUrl(); var apkLocalUrl = GetApkLocalUrl(); RemoteFile.Prepare(); var remoteFile = new RemoteFile(); remoteFile.Init(remoteURL, apkLocalUrl, null); remoteFile.Begin(OnDownLoadApkCompleted); } private void OnDownLoadApkCompleted(bool _ok, AssetVersion _assetVersion) { if (_ok) { step = Step.Completed; WindowCenter.Instance.Close(); SDKUtility.Instance.InstallAPK(GetApkLocalUrl()); } else { step = Step.DownLoadFailed; } } public void SkipVersion() { step = Step.Completed; } static List maoerGameAppId = new List { "mrgame", "mrgameios" }; public bool IsMaoErGame() { return maoerGameAppId.Contains(VersionConfig.Get().appId); } public void RequestMaoErVersionCheck() { if (IsMaoErGame() && versionInfo.VersionCount > 0) { step = Step.None; var tables = new Dictionary(); tables["uid"] = ModelCenter.Instance.GetModel().sdkLoginResult.account; var url = "https://api.maoergame.com/update/download/url"; HttpRequest.Instance.RequestHttpPost(url, tables, HttpRequest.defaultHttpContentType, 1, OnMaoErVersionCheckResult); } } private void OnMaoErVersionCheckResult(bool ok, string result) { if (ok) { try { maoerVersion = JsonMapper.ToObject(result); if (maoerVersion.Keys.Contains("code") && maoerVersion["code"].ToString() == "0") { step = Step.DownLoadPrepared; WindowCenter.Instance.OpenFromLocal(); } else { step = Step.None; Clock.AlarmAt(DateTime.Now + new TimeSpan(TimeSpan.TicksPerSecond), RequestMaoErVersionCheck); } } catch (System.Exception ex) { Debug.Log(ex); step = Step.None; Clock.AlarmAt(DateTime.Now + new TimeSpan(TimeSpan.TicksPerSecond), RequestMaoErVersionCheck); } } else { step = Step.None; Clock.AlarmAt(DateTime.Now + new TimeSpan(TimeSpan.TicksPerSecond), RequestMaoErVersionCheck); } } public bool InIosAuditTime() { DateTime dateTime; try { var textAsset = Resources.Load("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 (IsMaoErGame()) { if (versionInfo == null) { return false; } if (versionInfo.VersionCount > 0) { return false; } return versionInfo.downAsset == 1 && VersionConfig.Get().assetAccess != InstalledAsset.IngoreDownLoad; } else { if (versionInfo != null && versionInfo.downAsset == 1 && VersionConfig.Get().assetAccess != InstalledAsset.IngoreDownLoad) { return true; } else { return false; } } } public bool IsShangGu() { return VersionConfig.Get().appId == "815035927"; } public class VersionInfo { public int ForceCount; public int VersionCount; public JsonData resource_url; public JsonData notice_flag; public Version[] versions; public int downAsset = 1; public Version GetLatestVersion() { if (versions.Length > 0) { return versions[0]; } else { return default(Version); } } 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) { DebugEx.Log(ex); return string.Empty; } } } public struct Version { public int is_fullpackage; public int file_size; public string version_desc; public DateTime public_time; public int update_force; public string version_name; } public struct MaoErVersion { public string msg; public string code; public string content; public string url; } public struct ApkDownUrl { public string flag; public string url; public int size; public ApkDownUrl(string content) { var strings = content.Split('\t'); flag = strings[0]; url = strings[1]; int.TryParse(strings[2], out size); } } public enum Step { None, ApkExist, DownLoadPrepared, DownLoad, DownLoadFailed, Completed, } }