IEnumerator InitAssets() { ++InitCount; Debug.LogFormat("删除沙盒目录更新的资源"); if (Directory.Exists(PersistentABPath)) { DirectoryInfo info = new DirectoryInfo(PersistentABPath); info.Delete(true); info = null; } HotFixSystem.I.CheckVersionProgress = 0.1f; yield return(null); Debug.LogFormat("删除沙盒目录资源列表"); if (File.Exists(PersistentAssetsListPath)) { File.Delete(PersistentAssetsListPath); } HotFixSystem.I.CheckVersionProgress = 0.2f; yield return(null); Debug.LogFormat("保存版本信息到{0}", PersistentVersionPath); VersionHelper.RecodeVersion(PersistentVersionPath); HotFixSystem.I.CheckVersionProgress = 0.3f; yield return(null); Debug.LogFormat("尝试加载Streaming资源列表 {0}", StreamingAssetsListPath); WWW www = new WWW(Prefix + StreamingAssetsListPath); yield return(www); HotFixSystem.I.CheckVersionProgress = 0.8f; if (string.IsNullOrEmpty(www.error)) { MemoryStream ms = new MemoryStream(www.bytes); VersionHelper.SetPersistentAssetsList(ms); www.Dispose(); www = null; Debug.LogFormat("保存资源列表到{0}", PersistentAssetsListPath); VersionHelper.RecodeAssetList(PersistentAssetsListPath); } else { Debug.LogFormat("加载Streaming资源列表失败 {0} ", www.error); NextHotFixPipeline = EHotFixPipeline.Fail; } HotFixSystem.I.CheckVersionProgress = 1f; NextHotFixPipeline = EHotFixPipeline.CheckAssetsList; }
public int OnEnter() { downloadedBytes = 0; RecodeTime = 0; // GameObject go = Resources.Load<GameObject>("Panel_Hotfix"); // GameObject uiHotFix = GameObject.Instantiate<GameObject>(go); // uiHotFix.SetActive(true); VersionHelper.Clear(); HotFixSystem.I.HotFixPipeline = EHotFixPipeline.None; NextHotFixPipeline = EHotFixPipeline.CheckBuildVersion; return(0); }
IEnumerator Unzip() { while (!DownloadFinished || FileBuffs.Count > 0) { if (FileBuffs.Count > 0) { AssetsInfo assetsInfo = FileBuffs.Dequeue(); if (assetsInfo != null && assetsInfo.Datas != null) { string filePath = Path.Combine(PersistentABPath, assetsInfo.AssetName); filePath = filePath.Replace('\\', '/'); string dirPath = Path.GetDirectoryName(filePath); Debug.LogFormat("保存资源资源{0}到{1}", assetsInfo.AssetName, filePath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } using (FileStream fileStream = File.Create(filePath)) { yield return(fileStream.BeginWrite(assetsInfo.Datas, 0, assetsInfo.Datas.Length, null, null)); } VersionHelper.UpdateAssetsInfo(assetsInfo); VersionHelper.RecodeAssetList(PersistentAssetsListPath); ++HotFixSystem.I.DownloadIndex; } } yield return(null); } if (DownloadFail) { NextHotFixPipeline = EHotFixPipeline.Fail; } else { NextHotFixPipeline = EHotFixPipeline.CheckAssetsList; } }
public EAppState OnUpdate() { EAppState appState = EAppState.HotFix; if (NextHotFixPipeline != HotFixSystem.I.HotFixPipeline) { HotFixSystem.I.HotFixPipeline = NextHotFixPipeline; switch (HotFixSystem.I.HotFixPipeline) { case EHotFixPipeline.CheckBuildVersion: CoroutineUtil.I.StartCoroutine(CheckBuildVersionCoroutine()); break; case EHotFixPipeline.InitAssets: CoroutineUtil.I.StartCoroutine(InitAssets()); break; case EHotFixPipeline.CheckAssetsList: CoroutineUtil.I.StartCoroutine(CheckAssetsListCoroutine()); break; case EHotFixPipeline.WaitUserConfirm: HotFixSystem.I.UserConfirm = false; break; case EHotFixPipeline.FixAssets: RecodeTime = 0; downloadedBytes = 0; CoroutineUtil.I.StartCoroutine(DownLoadZip()); CoroutineUtil.I.StartCoroutine(Unzip()); break; case EHotFixPipeline.Success: appState = EAppState.GameRun; break; case EHotFixPipeline.FixBuild: case EHotFixPipeline.Fail: case EHotFixPipeline.FailRemote: if (VersionHelper.StreamingVersion == null || VersionHelper.StreamingVersion.BuildType.Equals("develop")) { appState = EAppState.GameRun; } break; default: break; } } if (HotFixSystem.I.HotFixPipeline == EHotFixPipeline.WaitUserConfirm) { if (HotFixSystem.I.UserConfirm) { NextHotFixPipeline = EHotFixPipeline.FixAssets; } } if (HttpReq != null) { HotFixSystem.I.HotFixSize = HotFixSystem.I.HotFixFullSize + HttpReq.downloadedBytes; RecodeTime += Time.unscaledDeltaTime; if (RecodeTime >= 0.5f) { HotFixSystem.I.DownloadSpeed = (ulong)((double)(HotFixSystem.I.HotFixSize - downloadedBytes) / (double)RecodeTime); downloadedBytes = HotFixSystem.I.HotFixSize; RecodeTime = 0; } } return(appState); }
IEnumerator CheckAssetsListCoroutine() { ++AssetsCheckCount; HotFixSystem.I.CheckVersionProgress = 0f; #region 尝试加载Remote资源列表============== Debug.LogFormat("尝试下载Remote资源列表 {0}", RemoteAssetsListUrl); using (UnityWebRequest req = UnityWebRequest.Get(RemoteAssetsListUrl)) { req.SetRequestHeader("Cache-Control", "max-age=0, no-cache, no-store"); req.SetRequestHeader("Pragma", "no-cache"); req.timeout = 5; yield return(req.Send()); if (!req.isNetworkError && req.responseCode == 200) { MemoryStream ms = new MemoryStream(req.downloadHandler.data); VersionHelper.SetRemoteAssetsList(ms); } else { Debug.LogWarningFormat("下载Remote资源列表失败 {0} responseCode == {1}", req.error, req.responseCode); NextHotFixPipeline = EHotFixPipeline.FailRemote; yield break; } } HotFixSystem.I.CheckVersionProgress = 0.1f; yield return(null); #endregion #region 尝试加载本地资源列表============== Debug.LogFormat("尝试加载Persistent资源列表 {0}", PersistentAssetsListPath); WWW www = new WWW(PersistentPrefix + PersistentAssetsListPath); yield return(www); if (string.IsNullOrEmpty(www.error)) { MemoryStream ms = new MemoryStream(www.bytes); VersionHelper.SetPersistentAssetsList(ms); www.Dispose(); www = null; } else { Debug.LogWarningFormat("加载本地资源列表失败 {0}", www.error); if (InitCount < 1) { NextHotFixPipeline = EHotFixPipeline.InitAssets; } else { NextHotFixPipeline = EHotFixPipeline.Fail; } yield break; } HotFixSystem.I.CheckVersionProgress = 0.3f; yield return(null); #endregion Debug.Log("计算需要更新的资源"); HotFixSystem.I.HotFixSize = 0L; HotFixSystem.I.HotFixTotalSize = 0L; HotFixSystem.I.HotFixFileCount = 0; FileBuffs.Clear(); NewAssets.Clear(); ExcessAssets.Clear(); //需要下载的内容 int index = 0; int count = VersionHelper.RemoteAssetsList.Count; foreach (AssetsInfo info in VersionHelper.RemoteAssetsList.Values) { AssetsInfo localInfo = null; if (!VersionHelper.PersistentAssetsList.TryGetValue(info.AssetName, out localInfo) || !info.AssetMD5.Equals(localInfo.AssetMD5)) { NewAssets.Enqueue(info); HotFixSystem.I.HotFixTotalSize += info.Size; ++HotFixSystem.I.HotFixFileCount; } ++index; if (index % 80 == 0 || index == count) { HotFixSystem.I.CheckVersionProgress = 0.3f + 0.3f * (float)index / (float)count; yield return(null); } } Debug.LogFormat("需要更新的资源 {0}", HotFixSystem.I.HotFixFileCount); Debug.Log("计算需要删除的资源"); //需要删除的内容 index = 0; count = VersionHelper.PersistentAssetsList.Count; foreach (AssetsInfo info in VersionHelper.PersistentAssetsList.Values) { if (!VersionHelper.RemoteAssetsList.ContainsKey(info.AssetName)) { ExcessAssets.Enqueue(info); } ++index; if (index % 80 == 0 || index == count) { HotFixSystem.I.CheckVersionProgress = 0.6f + 0.3f * (float)index / (float)count; yield return(null); } } Debug.LogFormat("需要删除的资源 {0}", ExcessAssets.Count); HotFixSystem.I.CheckVersionProgress = 1f; if (HotFixSystem.I.HotFixFileCount > 0) { NextHotFixPipeline = EHotFixPipeline.WaitUserConfirm; } else if (ExcessAssets.Count > 0) { NextHotFixPipeline = EHotFixPipeline.FixAssets; } else { NextHotFixPipeline = EHotFixPipeline.Success; } if (NextHotFixPipeline != EHotFixPipeline.Success && AssetsCheckCount > 1) { NextHotFixPipeline = EHotFixPipeline.Fail; } }
IEnumerator CheckBuildVersionCoroutine() { HotFixSystem.I.CheckVersionProgress = 0f; #region 尝试加载Streaming目录版本信息============== Debug.LogFormat("尝试加载Streaming版本信息 {0}", StreamingVersionPath); WWW www = new WWW(StreamingVersionPath); yield return(www); if (string.IsNullOrEmpty(www.error)) { MemoryStream ms = new MemoryStream(www.bytes); VersionHelper.SetStreamingVersion(ms); www.Dispose(); www = null; } else { Debug.LogWarningFormat("加载Streaming版本信息失败 {0}", www.error); } HotFixSystem.I.CheckVersionProgress = 0.3f; yield return(null); #endregion if (VersionHelper.StreamingVersion == null) { NextHotFixPipeline = EHotFixPipeline.FixBuild; HotFixSystem.I.CheckVersionProgress = 1f; yield break; } else { UrlPath = VersionHelper.StreamingVersion.HotFixAddress; #if UNITY_EDITOR || UNITY_STANDALONE_WIN DownloadUrl = UrlPath + "/StandaloneWindows"; #elif UNITY_IOS DownloadUrl = UrlPath + "/iOS"; #elif UNITY_ANDROID DownloadUrl = UrlPath + "/Android"; #endif DownloadABUrl = DownloadUrl + "/AssetBundle"; RemoteVersionPath = DownloadUrl + "/Version.txt"; RemoteAssetsListUrl = DownloadUrl + "/AssetsList.txt"; } #region 尝试加载Remote版本信息============== Debug.LogFormat("尝试加载Remote版本信息 {0}", RemoteVersionPath); using (UnityWebRequest req = UnityWebRequest.Get(RemoteVersionPath)) { req.SetRequestHeader("Cache-Control", "max-age=0, no-cache, no-store"); req.SetRequestHeader("Pragma", "no-cache"); req.timeout = 5; yield return(req.Send()); if (!req.isNetworkError && req.responseCode == 200) { MemoryStream ms = new MemoryStream(req.downloadHandler.data); VersionHelper.SetRemoteVersion(ms); } else { Debug.LogWarningFormat("尝试加载Remote版本信息失败 {0} responseCode == {1}", req.error, req.responseCode); } } HotFixSystem.I.CheckVersionProgress = 0.6f; yield return(null); #endregion #region 尝试加载沙盒目录版本信息============== Debug.LogFormat("尝试加载Persistent版本信息 {0}", PersistentVersionPath); www = new WWW(PersistentPrefix + PersistentVersionPath); yield return(www); if (string.IsNullOrEmpty(www.error)) { MemoryStream ms = new MemoryStream(www.bytes); VersionHelper.SetPersistentVersion(ms); www.Dispose(); www = null; } else { Debug.LogWarningFormat("加载Persistent版本信息失败 {0}", www.error); } HotFixSystem.I.CheckVersionProgress = 0.9f; yield return(null); #endregion //远程版本不存在,资源服务器维护中 if (VersionHelper.RemoteVersion == null) { NextHotFixPipeline = EHotFixPipeline.FailRemote; HotFixSystem.I.CheckVersionProgress = 1f; yield break; } //本地编译版本和远程编译版本不一样 if (VersionHelper.StreamingVersion == null || !string.Equals(VersionHelper.StreamingVersion.BuildVersion, VersionHelper.RemoteVersion.BuildVersion)) { NextHotFixPipeline = EHotFixPipeline.FixBuild; HotFixSystem.I.CheckVersionProgress = 1f; yield break; } //如果沙盒目录没有版本文件 或者 (沙盒目录和Streaming目录版本不一样) if (VersionHelper.PersistentVersion == null || !string.Equals(VersionHelper.StreamingVersion.BuildVersion, VersionHelper.PersistentVersion.BuildVersion)) { NextHotFixPipeline = EHotFixPipeline.InitAssets; HotFixSystem.I.CheckVersionProgress = 1f; yield break; } NextHotFixPipeline = EHotFixPipeline.CheckAssetsList; }