IEnumerator CheckFileListMd5(VersionConfig serverVersionConfig, FileUpdateVo versionVo) { string content = File.ReadAllText(versionVo.PersistentPath); VersionConfig persistentVersionConfig = JsonUtility.FromJson <VersionConfig>(content); int persistentVersion = persistentVersionConfig.resVersion; int serverVersion = serverVersionConfig.resVersion; if (serverVersion >= persistentVersion) { bool versionConfigChange = false; FileUpdateVo fileListVo = new FileUpdateVo(AppConst.FileListName, versionVo.Url, versionVo.Random, string.Empty, 100); string fileListMd5 = Util.Md5file(fileListVo.PersistentPath); FileCheckResult checkResult = null; byte[] fileListBytes = null; if (!serverVersionConfig.fileListMd5.Equals(fileListMd5)) { versionConfigChange = true; WWW www = new WWW(fileListVo.FileUrl); yield return(www); if (string.IsNullOrEmpty(www.error)) { string[] fileList = www.text.Split(new[] { Environment.NewLine }, StringSplitOptions.None); List <string> serverFileList = new List <string>(fileList); fileList = File.ReadAllLines(fileListVo.PersistentPath); List <string> persistentFileList = new List <string>(fileList); checkResult = Util.GetChangList(persistentFileList, serverFileList); fileListBytes = www.bytes; } else { UDebug.LogError(string.Format("{0}down failed:{1}", fileListVo.FileUrl, www.error)); yield break; } www.Dispose(); //下载云端list } else if (serverVersion > persistentVersion)//只是version改变了,实际资源没有变,或者上次资源下载好了,但是version写入没有成功 { versionConfigChange = true; } VersionConfig toWriteVersionConfig = versionConfigChange ? serverVersionConfig : null; CheckResourceComplete(toWriteVersionConfig, checkResult, fileListBytes, versionVo); } else { if (GameStateManager.Instance.showGameStateLog) { UDebug.Log("本资源比较超前,不做资源版本更新: " + "persistentVersion" + persistentVersion.ToString() + " serverVersion:" + serverVersion.ToString()); } CheckResourceComplete(null, null, null, versionVo); } yield return(new WaitForEndOfFrame()); }
private void CheckAppVersionComplete(VersionConfig serverVersionConfig, VersionConfig streamVersionConfig, FileUpdateVo versionVo) { if (GameStateManager.Instance.showGameStateLog) { UDebug.Log("CheckAppVersionComplete"); } Hashtable hash = new Hashtable(); hash["serverVersionConfig"] = serverVersionConfig; hash["streamVersionConfig"] = streamVersionConfig; hash["versionVo"] = versionVo; this.stateMachine.SetState <State_CopyToPersistent>(hash); }
public override void EnterState(Hashtable hash) { GameStateManager.Instance.GameStateChang(GameStates.CheckResource); #if UNITY_EDITOR if (!AssetBundleManager.ResUpdateInEditor) { CheckResourceComplete(null, null, null, null); return; } #endif VersionConfig serverVersionConfig = (VersionConfig)hash["serverVersionConfig"]; FileUpdateVo versionVo = (FileUpdateVo)hash["versionVo"]; TaskManager.Instance.StartCoroutine(CheckFileListMd5(serverVersionConfig, versionVo)); }
public override void EnterState(Hashtable hash) { GameStateManager.Instance.GameStateChang(GameStates.CopyToPersistent); #if UNITY_EDITOR if (!AssetBundleManager.ResUpdateInEditor) { UnzipStreamingComplete(null); return; } #endif this.serverVersionConfig = (VersionConfig)hash["serverVersionConfig"]; versionVo = (FileUpdateVo)hash["versionVo"]; VersionConfig streamVersionConfig = (VersionConfig)hash["streamVersionConfig"]; TaskManager.Instance.StartCoroutine(LoadResVersion(versionVo, streamVersionConfig)); }
/// <summary> /// /// </summary> /// <param name="serverVersionConfig">如果不为空,则需要写入</param> /// <param name="checkResult">如果不为空,则需要删除文件或者下载文件</param> /// <param name="fileListBytes">下载之后的文本列表</param> /// <param name="url"></param> /// <param name="random"></param> /// <returns></returns> IEnumerator StartDownRes(VersionConfig serverVersionConfig, byte[] fileListBytes, List <FileUpdateVo> fileVoList, FileUpdateVo versionVo) { FileUpdateVo fileUpdateVo; string url = versionVo.Url; string random = versionVo.Random; float allSize = Util.SumSize(fileVoList, fileVoList.Count); for (int i = 0; i < fileVoList.Count; i++) { GameStateManager.Instance.DownProgressHandle(GameStates.DownResource, fileVoList, i, allSize); fileUpdateVo = fileVoList[i]; WWW www = new WWW(fileUpdateVo.FileUrl); yield return(www); if (string.IsNullOrEmpty(www.error)) { Util.WriteFile(fileUpdateVo.PersistentPath, www.bytes); } else { UDebug.LogError(string.Format("{0}down failed:{1}", fileUpdateVo.FileUrl, www.error)); yield break; } www.Dispose(); } if (fileListBytes != null) { fileUpdateVo = new FileUpdateVo(AppConst.FileListName, url, random, string.Empty, 0); Util.WriteFile(fileUpdateVo.PersistentPath, fileListBytes); } if (serverVersionConfig != null) { if (File.Exists(versionVo.PersistentPath)) { File.Delete(versionVo.PersistentPath); string json = JsonUtility.ToJson(serverVersionConfig); File.WriteAllText(versionVo.PersistentPath, json); } } GameStateManager.Instance.DownProgressHandle(GameStates.DownResource, fileVoList, fileVoList.Count, allSize); yield return(new WaitForEndOfFrame()); DownResourceComplete(); }
IEnumerator LoadResVersion(FileUpdateVo vo, VersionConfig streamVersionConfig) { bool isExists = File.Exists(vo.PersistentPath); bool needCopy = !isExists; VersionConfig persistentAssetsConfig = null; if (isExists) { string persistentStr = File.ReadAllText(vo.PersistentPath); if (GameStateManager.Instance.showGameStateLog) { UDebug.Log("persistentStr " + persistentStr); } persistentAssetsConfig = JsonUtility.FromJson <VersionConfig>(persistentStr); if (persistentAssetsConfig.packageVersion != streamVersionConfig.packageVersion) { needCopy = true; } } if (needCopy) { string resRoot = PathHelper.PersistentPath + "/" + AppConst.ResRoot; if (Directory.Exists(resRoot)) { Directory.Delete(resRoot, true); } } if (!needCopy) { if (GameStateManager.Instance.showGameStateLog) { UDebug.Log("不需要解压文件"); } UnzipStreamingComplete(persistentAssetsConfig); yield break;; } else { TaskManager.Instance.StartCoroutine(OnExtractResource(vo.Url, vo.Random, streamVersionConfig)); } }
private void CheckResourceComplete(VersionConfig serverVersionConfig, FileCheckResult checkResult, byte[] fileListBytes, FileUpdateVo versionVo) { Hashtable hash = new Hashtable(); hash["checkResult"] = checkResult; hash["serverVersionConfig"] = serverVersionConfig; hash["fileListBytes"] = fileListBytes; hash["versionVo"] = versionVo; if (checkResult != null) //&& checkResult.downSize > 1000)//大于某个值了才提示 { //弹窗,确认之后才能继续更新 GameStateManager.Instance.ShowPop(true, string.Format("有{0}b资源更新,点击确定开始更新!", checkResult.downSize), () => { this.stateMachine.SetState <State_DownResource>(hash); }, GameStateManager.Instance.Quit); } else { this.stateMachine.SetState <State_DownResource>(hash); } }
private List <FileUpdateVo> PrepareDownRes(VersionConfig serverVersionConfig, FileCheckResult checkResult, byte[] fileListBytes, FileUpdateVo versionVo) { string url = versionVo.Url; string random = versionVo.Random; FileUpdateVo fileUpdateVo; string persistentMd5 = null; foreach (FileCheckInfo item in checkResult.DeleteList) { fileUpdateVo = new FileUpdateVo(item.name, url, random, item.md5, item.size); if (File.Exists(fileUpdateVo.PersistentPath)) { File.Delete(fileUpdateVo.PersistentPath); } } List <FileUpdateVo> fileVoList = new List <FileUpdateVo>(); for (int i = 0; i < checkResult.DownList.Count; i++) { FileCheckInfo item = checkResult.DownList[i]; fileUpdateVo = new FileUpdateVo(item.name, url, random, item.md5, item.size); if (File.Exists(fileUpdateVo.PersistentPath)) { persistentMd5 = Util.Md5file(fileUpdateVo.PersistentPath); } else { persistentMd5 = string.Empty; } if (persistentMd5 != fileUpdateVo.Md5)//需要下载 { fileVoList.Add(fileUpdateVo); } } return(fileVoList); }
public override void EnterState(Hashtable hash) { GameStateManager.Instance.GameStateChang(GameStates.DownResource); #if UNITY_EDITOR if (!AssetBundleManager.ResUpdateInEditor) { DownResourceComplete(); return; } #endif FileCheckResult checkResult = (FileCheckResult)hash["checkResult"]; if (checkResult != null) { VersionConfig serverVersionConfig = (VersionConfig)hash["serverVersionConfig"]; byte[] fileListBytes = (byte[])hash["fileListBytes"]; FileUpdateVo versionVo = (FileUpdateVo)hash["versionVo"]; List <FileUpdateVo> fileVoList = PrepareDownRes(serverVersionConfig, checkResult, fileListBytes, versionVo); TaskManager.Instance.StartCoroutine(StartDownRes(serverVersionConfig, fileListBytes, fileVoList, versionVo)); } else { DownResourceComplete(); } }
/// <summary> /// 解包逻辑,装完包第一次运行时候做 /// 现在用的比较笨的方法,真实项目可以用多线程 /// </summary> /// <returns></returns> IEnumerator OnExtractResource(string url, string random, VersionConfig streamVersionConfig) { string message = "正在解包文件:>files.txt"; string tempStr; if (GameStateManager.Instance.showGameStateLog) { UDebug.Log(message); } List <FileUpdateVo> fileVoList = new List <FileUpdateVo>(); //files.txt FileUpdateVo fileUpdateVo = new FileUpdateVo(AppConst.FileListName, url, random, string.Empty, 100); if (Application.isMobilePlatform) //读取files.txt { WWW www = new WWW(fileUpdateVo.StreamingPath); yield return(www); tempStr = www.text; www.Dispose(); } else { tempStr = File.ReadAllText(fileUpdateVo.StreamingPath); } string[] fileList = tempStr.Split(new[] { Environment.NewLine }, StringSplitOptions.None); string[] strArr; foreach (var file in fileList) { string fileStr = file.Trim(); if (!string.IsNullOrEmpty(fileStr)) { strArr = fileStr.Split('|'); fileUpdateVo = new FileUpdateVo(strArr[0], url, random, strArr[2], float.Parse(strArr[1])); fileVoList.Add(fileUpdateVo); } } fileUpdateVo = new FileUpdateVo(AppConst.FileListName, url, random, string.Empty, 100); fileVoList.Add(fileUpdateVo); fileUpdateVo = new FileUpdateVo(AppConst.ResVersionConfigFile, url, random, string.Empty, 100, false); fileVoList.Add(fileUpdateVo); float allSize = Util.SumSize(fileVoList, fileVoList.Count); for (int i = 0; i < fileVoList.Count; i++) { GameStateManager.Instance.DownProgressHandle(GameStates.CopyToPersistent, fileVoList, i, allSize); fileUpdateVo = fileVoList[i]; String strPath = Path.GetDirectoryName(fileUpdateVo.PersistentPath); if (!Directory.Exists(strPath)) { Directory.CreateDirectory(strPath); } if (Application.isMobilePlatform) { WWW www = new WWW(fileUpdateVo.StreamingPath); yield return(www); if (www.isDone) { File.WriteAllBytes(fileUpdateVo.PersistentPath, www.bytes); www.Dispose(); } if (GameStateManager.Instance.showGameStateLog) { UDebug.Log(fileUpdateVo.FileName + " 文件写入成功"); } } else { File.Copy(fileUpdateVo.StreamingPath, fileUpdateVo.PersistentPath, true); } } GameStateManager.Instance.DownProgressHandle(GameStates.GameMain, fileVoList, fileVoList.Count, allSize); message = "解包完成!!!"; if (GameStateManager.Instance.showGameStateLog) { UDebug.Log(message); } yield return(new WaitForEndOfFrame()); message = string.Empty; UnzipStreamingComplete(streamVersionConfig); }
IEnumerator LoadResVersion(string versionFile, string _webUrl) { string random = DateTime.Now.ToString("yyyymmddhhmmss"); FileUpdateVo versionVo = new FileUpdateVo(versionFile, _webUrl, random, string.Empty, 10, false); string configStr; WWW www; if (Application.isMobilePlatform) { www = new WWW(versionVo.StreamingPath); yield return(www); if (!string.IsNullOrEmpty(www.error)) { GameStateManager.Instance.ShowDownError(versionVo.StreamingPath, www.error); } configStr = www.text; www.Dispose(); } else { configStr = File.ReadAllText(versionVo.StreamingPath); } VersionConfig streamVersionConfig = JsonUtility.FromJson <VersionConfig>(configStr); if (GameStateManager.Instance.showGameStateLog) { UDebug.Log("开始加载:" + versionVo.FileUrl); } www = new WWW(versionVo.FileUrl); yield return(www); if (!string.IsNullOrEmpty(www.error)) { GameStateManager.Instance.ShowDownError(versionVo.FileUrl, www.error); yield break; } if (GameStateManager.Instance.showGameStateLog) { UDebug.Log("加载结束:" + versionVo.FileName + www.text); } configStr = www.text; www.Dispose(); VersionConfig serverVersionConfig = JsonUtility.FromJson <VersionConfig>(configStr); UDebug.enableLog = serverVersionConfig.enableLog; //GameStateManager.Instance.showGameStateLog = serverVersionConfig.enableLog; Version myVersion = new Version(streamVersionConfig.packageVersion); Version cloudVersion = new Version(serverVersionConfig.packageVersion); if (cloudVersion > myVersion) { GameStateManager.Instance.ShowPop(true, "请下载最新版本!", () => { string url = AppConst.PackageUrl + "/MyGame_v" + serverVersionConfig.packageVersion + ".rar"; //www = new WWW(url); Application.OpenURL(url); GameStateManager.Instance.Quit(); }, GameStateManager.Instance.Quit); } else { CheckAppVersionComplete(serverVersionConfig, streamVersionConfig, versionVo); } }