using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using GameCore; using System.Text; namespace ResUpdate { /// /// 游戏更新状态 /// public enum GameUpdateState { //下载游戏版本 DownLoadGameConfigs, //下载游戏版本失败 DownLoadGameConfigsFailed, //需要更新APP NeedUpdateApp, //下载APK DownLoadAPKProgress, //下载APK失败 DownLoadAPKFailed, //游戏更新成功 Success } /// /// 游戏更新管理器 /// public class GameUpdateManager : UnitySingleton { class appJsonInfo { public string operate; public string time; public string sign; } /// /// 游戏更新回调 /// Action gameUpdateAction; /// /// 开始热更新 /// /// public void BeginUpdate(Action gameUpdateAction) { if (BaseLogger.isDebug) BaseLogger.LogFormat("==========================ResUpdate.GameUpdateManager=====>BeginUpdate"); this.gameUpdateAction = gameUpdateAction; StartCoroutine(GetAppConfigs()); } /// /// 获取游戏版本信息 /// /// IEnumerator GetAppConfigs() { SetUpdateState(false, GameUpdateState.DownLoadGameConfigs); TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); var appkey = "dsfasdtcxv88!0stponxfa="; var time = Convert.ToInt64(ts.TotalSeconds).ToString(); appJsonInfo data = new appJsonInfo(); data.operate = "appInfo"; data.time = time; data.sign = FileToCRC32.GetStrCRC32(time + "&" + appkey); var jsonData = JsonUtility.ToJson(data); Debug.LogError(jsonData); WWW www = new WWW(UpdateConfigs.GameVersionUrl, Encoding.UTF8.GetBytes(jsonData)); yield return www; if (string.IsNullOrEmpty(www.error)) { Debug.LogFormat("ResUpdate.GameUpdateManager=={0}===>GetAppConfigs:{1}",www.url, www.text); try { //AppConfigs.appConfigs = JsonUtility.FromJson(www.text); //if( !string.IsNullOrEmpty(AppConfigs.appConfigs.error)) //{ // Debug.LogError("--------------errorInfo:: " + AppConfigs.appConfigs.error); // SetUpdateState(true, GameUpdateState.DownLoadGameConfigsFailed); // www.Dispose(); // yield break; //} //if (AppConfigs.appConfigs.CompareVersion(Application.version)) //{ // UpdateApp(); //} //else //{ // SetUpdateState(true, GameUpdateState.Success); //} } catch(Exception e) { Debug.LogFormat("appConfig "+e.Message); } } else { Debug.LogError(www.url+"--------------error:: "+www.error); SetUpdateState(true, GameUpdateState.DownLoadGameConfigsFailed); } www.Dispose(); } /// /// 设置更新状态 /// /// /// /// void SetUpdateState(bool isFinish, GameUpdateState result, object param = null) { ThreadManager.Instance.QueueOnMainThread(() => { if (gameUpdateAction != null) { gameUpdateAction(isFinish, result, param); } if (isFinish) { gameUpdateAction = null; } }); } /// /// 安装APP /// public void UpdateApp() { if (Application.platform == RuntimePlatform.Android) { UpdateAPK(false); } else if (Application.platform == RuntimePlatform.IPhonePlayer) { UpdateIPA(); } else { SetUpdateState(true,GameUpdateState.Success); } } /// /// 安装APK /// public void UpdateAPK(bool allowDownLoadFromNoWifi) { if (IsNetWorkReachable(allowDownLoadFromNoWifi)) { //ResourceDownloadManager.Instance.StartDownload("test.apk", AppConfigs.appConfigs.androidURL, "apks/", DownLoadApkProgresCallBack, DownLoadApkFinishCallBack); } else { } } /// /// 网络是否可用 /// /// /// bool IsNetWorkReachable(bool allowDownLoadFromNoWifi) { if (!allowDownLoadFromNoWifi && Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork) return false; return true; } /// /// 下载APK进度回调 /// /// /// void DownLoadApkProgresCallBack(string name,DownLoadProgress downLoadProgress) { SetUpdateState(false, GameUpdateState.DownLoadAPKProgress, downLoadProgress); } /// /// 下载完成APK回调 /// /// /// void DownLoadApkFinishCallBack(string name, bool result) { if (result) { //TODO 安装APK //try //{ // var Intent = new AndroidJavaClass("android.content.Intent"); // var ACTION_VIEW = Intent.GetStatic("ACTION_VIEW"); // var FLAG_ACTIVITY_NEW_TASK = Intent.GetStatic("FLAG_ACTIVITY_NEW_TASK"); // var intent = new AndroidJavaObject("android.content.Intent", ACTION_VIEW); // var file = new AndroidJavaObject("java.io.File", path); // var Uri = new AndroidJavaClass("android.net.Uri"); // var uri = Uri.CallStatic("fromFile", file); // intent.Call("setDataAndType", uri, "application/vnd.android.package-archive"); // intent.Call("addFlags", FLAG_ACTIVITY_NEW_TASK); // intent.Call("setClassName", "com.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity"); // var UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); // var currentActivity = UnityPlayer.GetStatic("currentActivity"); // currentActivity.Call("startActivity", intent); //} //catch (System.Exception e) //{ // Debug.LogError("Error:" + e.Message + " -- " + e.StackTrace); //} } else { SetUpdateState(true, GameUpdateState.DownLoadAPKFailed); } } /// /// 安装IPA /// void UpdateIPA() { //Application.OpenURL(AppConfigs.appConfigs.iosURL); Application.Quit(); } } }