// 加载资源 public static async Task LoadRes() { string fileName = ""; try { string versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"); ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent <ResourcesComponent>(); VersionConfig localVersionConfig = JsonHelper.FromJson <VersionConfig>(File.ReadAllText(versionPath)); foreach (var data in localVersionConfig.FileInfoDict) { fileName = data.Value.File; if ((fileName.Equals("Version.txt")) || (fileName.Equals("StreamingAssets"))) { continue; } await resourcesComponent.LoadBundleAsync(fileName); } //foreach (var str in fileList) //{ // fileName = str; // if ((fileName.Equals("Version.txt")) || // (fileName.Equals("StreamingAssets"))) // { // continue; // } // try // { // await resourcesComponent.LoadBundleAsync(fileName); // } // catch (Exception ex) // { // Log.Debug("LoadRes异常:" + ex + "----" + fileName); // } //} } catch (Exception ex) { Log.Debug("LoadRes异常:" + ex + "----" + fileName); ToastScript.createToast("加载音频出错:" + fileName); } }
public static string GetBundleMD5(VersionConfig streamingVersionConfig, string bundleName) { string path = Path.Combine(PathHelper.AppHotfixResPath, bundleName); if (File.Exists(path)) { return(MD5Helper.FileMD5(path)); } if (streamingVersionConfig.FileInfoDict.ContainsKey(bundleName)) { return(streamingVersionConfig.FileInfoDict[bundleName].MD5); } return(""); }
/// <summary> /// 获取本地AB包MD5值 /// </summary> /// <param name="streamingVersionConfig"></param> /// <param name="bundleName"></param> /// <returns></returns> public static string GetBundleMD5(VersionConfig streamingVersionConfig, string bundleName) { //如果本地有这个AB包 就生成MD5值返回 string path = Path.Combine(PathHelper.AppHotfixResPath, bundleName); if (File.Exists(path)) { return(MD5Helper.FileMD5(path)); } //如果没有,就看看本地Version中是否有这个文件的的MD5值 if (streamingVersionConfig.FileInfoDict.ContainsKey(bundleName)) { return(streamingVersionConfig.FileInfoDict[bundleName].MD5); } //否则就返回空 return(""); }
/// <summary> /// 检测当前AB是否需要加载 /// </summary> /// <param name="AbName"></param> /// <returns></returns> private void CheckABLoad(string AbName, VersionConfig remoteVersionConfig, VersionConfig streamingVersionConfig) { //则将本地MD5与远程MD5对比 foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values) { if (fileVersionInfo.File == AbName.StringToAB()) { string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File); if (fileVersionInfo.MD5 != localFileMD5) { this.bundles.Enqueue(fileVersionInfo.File); this.TotalSize += fileVersionInfo.Size; } break; } } }
/// <summary> /// 获取本地版本文件 /// </summary> /// <returns></returns> public static async ETTask <VersionConfig> GetLocalVersionConfig() { VersionConfig streamingVersionConfig = new VersionConfig(); string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt"); try { using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>()) { //Log.Debug("本地版本文件路径:" + versionPath); await request.DownloadAsync(versionPath); streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); } } catch (Exception e) { Log.Error($"获取本地版本文件 version.txt error:{e}"); } return(streamingVersionConfig); }
public override void Dispose() { if (this.IsDisposed) { return; } if (this.Parent.IsDisposed) { return; } base.Dispose(); this.remoteVersionConfig = null; this.TotalSize = 0; this.bundles = null; this.downloadedBundles = null; this.downloadingBundle = null; this.webRequest?.Dispose(); this.Parent.RemoveComponent <BundleDownloaderComponent>(); }
private static void GenerateVersionProto(string dir, VersionConfig versionProto, string relativePath) { foreach (string file in Directory.GetFiles(dir)) { string md5 = MD5Helper.FileMD5(file); FileInfo fi = new FileInfo(file); long size = fi.Length; string filePath = relativePath == "" ? fi.Name : $"{relativePath}/{fi.Name}"; versionProto.FileInfoDict.Add(filePath, new FileVersionInfo { File = filePath, MD5 = md5, Size = size, }); } foreach (string directory in Directory.GetDirectories(dir)) { DirectoryInfo dinfo = new DirectoryInfo(directory); string rel = relativePath == "" ? dinfo.Name : $"{relativePath}/{dinfo.Name}"; GenerateVersionProto($"{dir}/{dinfo.Name}", versionProto, rel); } }
/// <summary> /// 检测更新 /// </summary> /// <param name="item"></param> /// <param name="remoteVersionConfig"></param> /// <param name="streamingVersionConfig"></param> public async void CheckUpdate(ResGroupData item, VersionConfig remoteVersionConfig, VersionConfig streamingVersionConfig) { Log.Debug("检查更新....."); this.Reset();//重置 if (item.ResNames.Length == 0) { return; } foreach (string AbName in item.ResNames) { CheckABLoad(AbName, remoteVersionConfig, streamingVersionConfig); } Log.Debug("需要加载的AB资源数量:" + this.bundles.Count); if (this.bundles.Count > 0) { StartAsync().Coroutine(); CheckDownLoadTimeOut().Coroutine(); await this.DownloadAsync(); } else { Log.Debug("不需要下载任何资源!!!!!"); this.UpdateFlag.gameObject.SetActive(false); } }
public async ETTask StartAsync() { // 获取远程的Version.txt string versionUrl = ""; try { using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>()) { versionUrl = PathHelper.RemoteLoadPath + "/Version.txt"; await webRequestAsync.DownloadAsync(versionUrl); remoteVersionConfigData = webRequestAsync.Request.downloadHandler.data; remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text); } } catch (Exception e) { throw new Exception($"url: {versionUrl}", e); } try { // 获取streaming目录的Version.txt VersionConfig streamingVersionConfig; string versionPath = PathHelper.SavePath + "Version.txt"; if (File.Exists(versionPath)) { streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(File.ReadAllText(versionPath)); } else { streamingVersionConfig = new VersionConfig(); } // 删掉远程不存在的文件 DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.SavePath); if (directoryInfo.Exists) { FileInfo[] fileInfos = directoryInfo.GetFiles(); int directoryFolderLength = directoryInfo.FullName.Length; foreach (FileInfo fileInfo in fileInfos) { if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.FullName.Substring(directoryFolderLength))) { continue; } if (fileInfo.Name == "Version.txt") { continue; } fileInfo.Delete(); } } else { directoryInfo.Create(); } // 对比MD5 foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values) { // 对比md5 string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File); if (fileVersionInfo.MD5 == localFileMD5) { hasDonwLoadedFile.Add(fileVersionInfo.File, fileVersionInfo); continue; } this.bundles.Enqueue(fileVersionInfo.File); this.TotalSize += fileVersionInfo.Size; } } catch (Exception ex) { UnityEngine.Debug.LogError(ex.StackTrace); } }
/// <summary> /// 返回是否需要下载 /// </summary> /// <returns></returns> public async Task<bool> LoadInfo() { isNetNotReach = false; string versionUploadSelfResUrl = GlobalConfigComponent.Instance.GlobalProto.VersionUploadSelfResUrl; Log.Debug($"versionUploadSelfResUrl:{versionUploadSelfResUrl}"); if (ETModel.Define.isInnetNet) { versionUploadSelfResUrl = GlobalConfigComponent.Instance.GlobalProto.VersionUploadSelfResUrl_InnerNet; Log.Debug($"VersionUploadSelfResUrl_InnerNet:{versionUploadSelfResUrl}"); } UnityWebRequestAsync webRequestSelfVersionAsync = ComponentFactory.Create<UnityWebRequestAsync>(); UnityWebRequestAsync webRequestAsync = ComponentFactory.Create<UnityWebRequestAsync>(); string versionText = string.Empty; try { //下载外层资源文件夹版本,即父目录 var webRequestSelfResVersionTask = webRequestSelfVersionAsync.DownloadAsync(versionUploadSelfResUrl); await webRequestSelfResVersionTask; ZLog.Info($"webRequestSelfResVersionText:{webRequestSelfVersionAsync.Request.downloadHandler.text}"); ETModel.Define.ParentResABDirectory = webRequestSelfVersionAsync.Request.downloadHandler.text; ZLog.Info($"ParentDirectory:{ETModel.Define.ParentResABDirectory}"); webRequestSelfVersionAsync.Dispose(); //下载bundle流程 string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt"; if (ETModel.Define.IsABNotFromServer) { versionUrl = GetUrlWithPlatform(ETModel.Define.SelfResourceServerIpAndPort + "/") + "StreamingAssets/" + "Version.txt"; } Log.Debug($"versionUrl:{versionUrl}"); var webRequestTask = webRequestAsync.DownloadAsync(versionUrl); await webRequestTask; versionText = webRequestAsync.Request.downloadHandler.text; webRequestSelfVersionAsync.Dispose(); } catch (Exception e) { if (e.Message.Contains("request error")) { webRequestSelfVersionAsync.Dispose(); webRequestAsync.Dispose(); ZLog.Info("load Version err", e.Message); Define.isUseStreamingAssetRes = true; OnFileServerNotReach(e.Message); return false; } } ZLog.Info($"versionText:{versionText}"); if (!versionText.StartsWith("{")) { this.VersionConfig = null; return false; } this.VersionConfig = JsonHelper.FromJson<VersionConfig>(versionText); //Log.Debug(JsonHelper.ToJson(this.VersionConfig)); if (isNetNotReach)//文件服务器没开启 { //var timeTask = DelayFrame(); //this.TagDownloadFinish(); //await timeTask; return false; } else //成功的事情 { VersionConfig localVersionConfig; // 对比本地的Version.txt string versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"); if (File.Exists(versionPath)) { localVersionConfig = JsonHelper.FromJson<VersionConfig>(File.ReadAllText(versionPath)); } else { versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt"); using (UnityWebRequestAsync request = ComponentFactory.Create<UnityWebRequestAsync>()) { try { await request.DownloadAsync(versionPath); localVersionConfig = JsonHelper.FromJson<VersionConfig>(request.Request.downloadHandler.text); } catch (System.Exception e) { Log.Debug(e.ToString()); localVersionConfig = null; } } } if (localVersionConfig != null) { // 先删除服务器端没有的ab foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileInfoDict.Values) { if (this.VersionConfig.FileInfoDict.ContainsKey(fileVersionInfo.File)) { continue; } string abPath = Path.Combine(PathHelper.AppHotfixResPath, fileVersionInfo.File); if(File.Exists(abPath))File.Delete(abPath); } } // 再下载 foreach (FileVersionInfo fileVersionInfo in this.VersionConfig.FileInfoDict.Values) { FileVersionInfo localVersionInfo; if (localVersionConfig != null && localVersionConfig.FileInfoDict.TryGetValue(fileVersionInfo.File, out localVersionInfo)) { if (fileVersionInfo.MD5 == localVersionInfo.MD5) { continue; } } if (fileVersionInfo.File == "Version.txt") { continue; } this.bundles.Enqueue(fileVersionInfo.File); this.TotalSize += fileVersionInfo.Size; } DownloadInfo.TotalSize = TotalSize; //if (this.bundles.Count == 0) //{ // return; //} //Log.Debug($"need download bundles: {this.bundles.ToList().ListToString()}"); return true; //await Down(); } }
public async Task StartAsync() { using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>()) { string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt"; Log.Debug(versionUrl); await request.DownloadAsync(versionUrl); this.VersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); } Log.Debug("WebVersion:\n" + JsonHelper.ToJson(this.VersionConfig)); // 对比本地的Version.txt string versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"); if (!File.Exists(versionPath)) { foreach (FileVersionInfo versionInfo in this.VersionConfig.FileInfoDict.Values) { if (versionInfo.File == "Version.txt") { continue; } this.bundles.Enqueue(versionInfo.File); this.TotalSize += versionInfo.Size; } } else { VersionConfig localVersionConfig = JsonHelper.FromJson <VersionConfig>(File.ReadAllText(versionPath)); Log.Debug("LocalVersion:\n" + JsonHelper.ToJson(localVersionConfig)); // 先删除服务器端没有的ab foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileInfoDict.Values) { if (this.VersionConfig.FileInfoDict.ContainsKey(fileVersionInfo.File)) { continue; } string abPath = Path.Combine(PathHelper.AppHotfixResPath, fileVersionInfo.File); File.Delete(abPath); } // 再下载 foreach (FileVersionInfo fileVersionInfo in this.VersionConfig.FileInfoDict.Values) { FileVersionInfo localVersionInfo; if (localVersionConfig.FileInfoDict.TryGetValue(fileVersionInfo.File, out localVersionInfo)) { if (fileVersionInfo.MD5 == localVersionInfo.MD5) { continue; } } if (fileVersionInfo.File == "Version.txt") { continue; } this.bundles.Enqueue(fileVersionInfo.File); this.TotalSize += fileVersionInfo.Size; } } if (this.bundles.Count == 0) { return; } Log.Debug($"need download bundles: {this.bundles.ToList().ListToString()}"); await this.WaitAsync(); }
public async ETTask DownloadAsync() { if (this.bundles.Count == 0 && this.downloadingBundle == "") { return; } try { while (true) { if (this.bundles.Count == 0) { break; } this.downloadingBundle = this.bundles.Dequeue(); while (true) { try { using (this.webRequest = ComponentFactory.Create <UnityWebRequestAsync>()) { await this.webRequest.DownloadAsync(PathHelper.RemoteLoadPath + "/" + this.downloadingBundle); byte[] data = this.webRequest.Request.downloadHandler.data; string path = PathHelper.SavePath + this.downloadingBundle; using (FileStream fs = new FileStream(path, FileMode.Create)) { fs.Write(data, 0, data.Length); } hasDonwLoadedFile.Add(this.downloadingBundle, remoteVersionConfig.FileInfoDict[this.downloadingBundle]); VersionConfig config = new VersionConfig(); config.FileInfoDict = hasDonwLoadedFile; var byteArray = System.Text.Encoding.UTF8.GetBytes(JsonMapper.ToJson(config)); using (FileStream fs = new FileStream(PathHelper.SavePath + "Version.txt", FileMode.Create)) { fs.Write(byteArray, 0, byteArray.Length); } } } catch (Exception e) { Log.Error($"download bundle error: {this.downloadingBundle}\n{e}"); continue; } break; } this.downloadedBundles.Add(this.downloadingBundle); this.downloadingBundle = ""; this.webRequest = null; } } catch (Exception e) { Log.Error(e); } }
public async Task StartAsync() { // 获取远程的Version.txt string versionUrl = ""; try { using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>()) { versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/Version.txt"; Log.Error("server version=" + versionUrl); await webRequestAsync.DownloadAsync(versionUrl); remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text); Log.Warning("server version json=" + JsonHelper.ToJson(remoteVersionConfig)); } } catch (Exception e) { throw new Exception($"url: {versionUrl}", e); } // 获取HotfixResPath目录的Version.txt string versionPath = Path.Combine($"file://{PathHelper.AppHotfixResPath}", "Version.txt"); Log.Debug("local version=" + versionPath); using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>())//未下载的 { try { await request.DownloadAsync(versionPath); streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); } catch (Exception e) { Log.Debug($"获取本地目录的Version.txt 失败! Message: {e.Message}"); } } // 获取streaming目录的Version.txt if (null == streamingVersionConfig) { // string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt"); versionPath = Path.Combine($"file://{PathHelper.AppResPath}", "Version.txt"); using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>()) { try { await request.DownloadAsync(versionPath); streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); } catch (Exception e) { Log.Debug($"获取本地目录的Version.txt 失败! Message: {e.Message}"); streamingVersionConfig = new VersionConfig(); } } var verLocal = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt");//创建路径 if (Directory.Exists(PathHelper.AppHotfixResPath) == false) { Directory.CreateDirectory(PathHelper.AppHotfixResPath); } using (FileStream fs = new FileStream(verLocal, FileMode.Create))//创建 { VersionConfig t = new VersionConfig() { Version = remoteVersionConfig.Version, TotalSize = 0, ABSecurity = remoteVersionConfig.ABSecurity, FileInfoDict = new Dictionary <string, FileVersionInfo>() }; var json = JsonHelper.ToJson(t); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(json); fs.Write(bytes, 0, bytes.Length); } } // 删掉远程不存在的文件 DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath); if (directoryInfo.Exists) { FileInfo[] fileInfos = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfos) { if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name)) { continue; } if (fileInfo.Name == "Version.txt") { continue; } fileInfo.Delete(); } } else { directoryInfo.Create(); } // 对比MD5 foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values) { // 对比md5 string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File); if (fileVersionInfo.MD5 == localFileMD5 || fileVersionInfo.File == "Version.txt")//txt不去对比md5 { continue; } bundles.Enqueue(fileVersionInfo.File); TotalSize += fileVersionInfo.Size; } }
/// <summary> /// 检测是否需要热更新 /// </summary> /// <param name="item"></param> /// <param name="remoteVersionConfig"></param> /// <param name="streamingVersionConfig"></param> /// <returns></returns> public bool CheckModuleNeedLoad(ResGroupData item, VersionConfig remoteVersionConfig, VersionConfig streamingVersionConfig) { Log.Debug("检查是否需要热更新....."); this.Reset();//重置 if (item.ResNames.Length == 0) { return(false); } foreach (string AbName in item.ResNames) { CheckABLoad(AbName, remoteVersionConfig, streamingVersionConfig); } Log.Debug("需要加载的AB资源数量:" + this.bundles.Count); if (this.bundles.Count > 0) { return(true); } return(false); }
//对比本地资源和服务器资源 public async ETTask StartAsync(string fileFoldr = "", string versionName = "Version.txt") { downloadFoldr = fileFoldr; // 获取远程的Version.txt string versionUrl = ""; try { using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>()) { versionUrl = Path.Combine(GlobalConfigComponent.Instance.GlobalProto.GetUrl(), "StreamingAssets", downloadFoldr, versionName); await webRequestAsync.DownloadAsync(versionUrl); remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text); } } catch (Exception e) { pLoserCall?.Invoke(e.ToString()); throw new Exception($"url: {versionUrl}", e); } // 删掉远程不存在的文件 DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(PathHelper.AppHotfixResPath, downloadFoldr)); if (directoryInfo.Exists) { FileInfo[] fileInfos = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfos) { if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name)) { continue; } if (fileInfo.Name == versionName) { continue; } fileInfo.Delete(); } } else { directoryInfo.Create(); } // 对比MD5 foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values) { // 对比md5 string localFileMD5 = BundleHelper.GetBundleMD5(fileVersionInfo.File); if (fileVersionInfo.MD5 == localFileMD5) { continue; } this.bundles.Enqueue(fileVersionInfo.File); this.TotalSize += fileVersionInfo.Size; } await DownloadAsync(); }
/// <summary> /// 开始下载远程Version文件进行本地对比 确认需要下载的AB包文件 /// </summary> /// <returns></returns> public async Task StartAsync() { // 获取远程的Version.txt string versionUrl = ""; try { using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>()) { //从Resources文件夹读取“VK”预制体上的TXT文档取得AB资源服务区地址 得到最新的Version.txt versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt"; //Log.Debug(versionUrl); await webRequestAsync.DownloadAsync(versionUrl); //下载资源 remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text); //Log.Debug(JsonHelper.ToJson(this.VersionConfig)); } } catch (Exception e) { throw new Exception($"url: {versionUrl}", e); } // 获取streaming目录的Version.txt VersionConfig streamingVersionConfig; //得到本地Version文件地址 string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt"); using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>()) { await request.DownloadAsync(versionPath); streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); } // 删掉远程不存在的文件 获取本地热更新文件夹 DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath); if (directoryInfo.Exists) //如果有这个文件夹 { //得到文件夹内所有文件 FileInfo[] fileInfos = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfos) { //新的版本配置表中没有这个文件 就删除此文件 if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name)) { continue; } if (fileInfo.Name == "Version.txt") { continue; } fileInfo.Delete(); } } else { //否则就创建新的热更新资源文件夹 directoryInfo.Create(); } // 对比MD5 foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values) { // 本地 string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File); if (fileVersionInfo.MD5 == localFileMD5) { continue; } this.bundles.Enqueue(fileVersionInfo.File); //需要重新下载的AB包的名字 this.TotalSize += fileVersionInfo.Size; //需要下载的资源的总大小 } }
public async ETTask StartAsync() { // 获取远程的Version.txt string versionUrl = ""; try { using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>()) { //UnityWebRequest UnityWebRequest www下载 versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt"; //Log.Debug(versionUrl); await webRequestAsync.DownloadAsync(versionUrl); remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text); //Log.Debug(JsonHelper.ToJson(this.VersionConfig)); } } catch (Exception e) { throw new Exception($"url: {versionUrl}", e); } // 获取streaming目录的Version.txt VersionConfig streamingVersionConfig; string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt"); using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>()) { await request.DownloadAsync(versionPath); streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); } // 删掉远程不存在的文件 DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath); //如果存放热更新资源的文件夹存在 if (directoryInfo.Exists) { //获取到里面的所有子文件 FileInfo[] fileInfos = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfos) { //就是远程下载下来的版本文件 json反序列化成一个实体类 FileInfoDict这个字典就包含了所有AB文件信息 if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name)) { continue; } if (fileInfo.Name == "Version.txt") { continue; } //为什么要删除掉远程不存在的文件呢 主要是为了避免加载资源出错 资源重复 会导致到报错 fileInfo.Delete(); } } //如果保存热更新资源的文件夹不存在的话 通过Create进行创建 else { directoryInfo.Create(); } // 对比MD5 foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values) { // 对比md5 跟我们本地的文件进行MD5对比 string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File); //如果相等 就忽略 表示两个版本中 这个文件并未做任何改动 if (fileVersionInfo.MD5 == localFileMD5) { continue; } //如果两个文件的MD5不一致 把要下载的文件 压入到队列 this.bundles.Enqueue(fileVersionInfo.File); //下载的总大小也加上这个文件的大小 this.TotalSize += fileVersionInfo.Size; } }
/// <summary> /// 拿到远程和本地不需要热更的bundle,添加到bundles /// </summary> /// <returns></returns> public async ETTask StartAsync() { // 获取远程的Version.txt string versionUrl = ""; try { using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>()) { //web资源服务器地址 本地为http://127.0.0.1:8080/PC/ (PC为平台) versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt"; Log.Debug("web资源服务器地址" + versionUrl); //开始下载版本信息 await webRequestAsync.DownloadAsync(versionUrl); //将下载到的远程版本配置信息反序列化为VersionConfig remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text); Log.Debug("热更新到的VersionConfig:" + JsonHelper.ToJson(this.remoteVersionConfig)); } } catch (Exception e) { throw new Exception($"url: {versionUrl}", e); } // 获取客户端的Version.txt // 获取streaming目录的Version.txt VersionConfig streamingVersionConfig; //地址合并 拿到本地版本信息文件路径 string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt"); Log.Debug("本地Version.txt地址:" + versionPath); using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>()) { await request.DownloadAsync(versionPath); streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); Log.Debug("本地Version.txt内容:" + streamingVersionConfig); } // 删掉远程不存在的文件 DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath); Log.Debug("应用程序外部资源路径存放路径(热更新资源路径):" + PathHelper.AppHotfixResPath); if (directoryInfo.Exists) { //有此AppHotfixResPath我呢见驾就获取其中文件 FileInfo[] fileInfos = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfos) { //远程版本文件中如果有此文件直接跳过 if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name)) { continue; } //是远程版本文件也跳过 if (fileInfo.Name == "Version.txt") { continue; } //删除远程版本文件中不存在的资源 fileInfo.Delete(); } } else { //热更新资源路径不存在则创建此路径 directoryInfo.Create(); } // 对比MD5 foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values) { // 对比md5 (远程文件对应的本地文件的MD5) string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File); if (fileVersionInfo.MD5 == localFileMD5) { continue; } //添加到bundles队列 this.bundles.Enqueue(fileVersionInfo.File); //总计大小 this.TotalSize += fileVersionInfo.Size; } }
public async ETTask StartAsync() { // 获取远程的Version.txt string versionUrl = ""; try { using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>()) { versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt"; //Log.Debug(versionUrl); await webRequestAsync.DownloadAsync(versionUrl); remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text); //Log.Debug(JsonHelper.ToJson(this.VersionConfig)); } } catch (Exception e) { throw new Exception($"url: {versionUrl}", e); } // 获取streaming目录的Version.txt VersionConfig streamingVersionConfig; string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt"); using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>()) { await request.DownloadAsync(versionPath); streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); } // 删掉远程不存在的文件 DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath); if (directoryInfo.Exists) { FileInfo[] fileInfos = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfos) { if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name)) { continue; } if (fileInfo.Name == "Version.txt") { continue; } fileInfo.Delete(); } } else { directoryInfo.Create(); } // 对比MD5 foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values) { // 对比md5 string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File); if (fileVersionInfo.MD5 == localFileMD5) { continue; } this.bundles.Enqueue(fileVersionInfo.File); this.TotalSize += fileVersionInfo.Size; } }
public async ETTask StartAsync() { alreadyDownloadBytes = 0; TotalSize = 0; CurrVersionBytes = 0; string versionUrl = ""; versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt"; Log.Debug($"当前资源为AB模式,远程版本文件路径:" + versionUrl); try { using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>()) { await webRequestAsync.DownloadAsync(versionUrl); CurrVersionBytes += (long)webRequestAsync.Request.downloadedBytes; // 获取远程的Version.txt remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text); } } catch (Exception e) { Log.Error($"download version.txt error: {e}"); return; } // 获取streaming目录的Version.txt VersionConfig streamingVersionConfig; string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt"); using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>()) { Log.Debug("本地版本文件路径:" + versionPath); await request.DownloadAsync(versionPath); streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); } // 删掉远程不存在的文件 DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath); if (directoryInfo.Exists) { FileInfo[] fileInfos = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfos) { if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name)) { continue; } if (fileInfo.Name == "Version.txt") { continue; } fileInfo.Delete(); } } else { directoryInfo.Create(); } // 对比MD5 foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values) { // 对比md5 string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File); //如果MD5相同,且当前项目不含有这个文件 if (fileVersionInfo.MD5 == localFileMD5) { continue; } if (IsFilterRes(fileVersionInfo.File)) { continue; } this.bundles.Enqueue(fileVersionInfo.File); this.TotalSize += fileVersionInfo.Size; } }