/// <summary> /// 比较cache.txt文件获取差异ab资源 /// </summary> public void CompareCacheFileGetDifferentAssetBundles(OnGetUpdateABCompleteHandel onComplete) { string cacheTxtFilePath = ABDataConfig.localABFullName + ABDataConfig.localCacheFileName;//沙盒cache文本路径 string cacheFileTxt = null; if (File.Exists(cacheTxtFilePath)) { DebugManager.LogWarning(cacheTxtFilePath + "沙盒cache文本存在!开始对比差异!"); //cacheTxtFilePath = Application.dataPath + ABDataConfig.localCacheFilePath;//本地cache文本路径 //TODO 沙盒下的应该解密读取 cacheFileTxt = File.ReadAllText(cacheTxtFilePath); } else { DebugManager.LogWarning(cacheTxtFilePath + "沙盒cache文本文件不存在!默认将服务器cache文本当做所有资源列表!"); } string serverCacheUrl = ABDataConfig.GetAssetBundleUrl + ABDataConfig.abCacheFileApiName;//服务器cache文件路径 //下载服务器cache文件 Util.DownLoadFileText(serverCacheUrl, (serverCacheString) => { serverCacheFileText = serverCacheString;//保存服务器cache文件 GetUpdateDownLoadAssetBundles(cacheFileTxt, serverCacheString); onComplete(waitDownloadAssetBundleList); }, this); }
/// <summary> /// 保存cache文件到沙盒路径在ab文件下载结束后 /// </summary> public void SaveCacheFileOfAssetBundleDownloadOk() { if (!string.IsNullOrEmpty(serverCacheFileText)) { string cacheFilePath = ABDataConfig.localABFullName + ABDataConfig.localCacheFileName; FileManager.CreateFile(cacheFilePath, System.Text.Encoding.UTF8.GetBytes(serverCacheFileText)); DebugManager.LogWarning(cacheFilePath + "文件保存成功!"); cacheFilePath = null; } }
/// <summary> /// 比较本地ab的cache.txt文件的文本与服务器的文本得出差异 /// 参数1:本地cache.txt文本 参数2:服务器cache.txt 文本 /// </summary> private void GetUpdateDownLoadAssetBundles(string localCacheFileText, string serverCacheFileText) { if (string.IsNullOrEmpty(serverCacheFileText)) { DebugManager.LogError("服务器 cache.txt 的文本存在空值!serverCacheFileText:" + serverCacheFileText); return; } if (serverCacheFileText.Equals(localCacheFileText)) { DebugManager.LogWarning("cache.txt 本地与服务器文本相同,不做比较,继续更新! localCacheFileText:" + localCacheFileText + "serverCacheFileText:" + serverCacheFileText); //return; } var serverCacheFileData = ResolveCacheFile(serverCacheFileText);//服务器cache.txt数据 //Dictionary<string, CacheFileDataModel> localCacheFileData = null; //if (localCacheFileText == null) //{ // localCacheFileData = new Dictionary<string, CacheFileDataModel>(); //} //else //{ // localCacheFileData = ResolveCacheFile(localCacheFileText);//本地cache.txt数据 //} if (waitDownloadAssetBundleList == null) { waitDownloadAssetBundleList = new List <AssetBundleDownloadModel>(); } //对比服务器与本地的数据 foreach (var v in serverCacheFileData) { var tempABData = AssetBundleDepFileManager.Instance.GetAssetBundleDataOfABFullName(v.Key); //dep.ll 文件中的ab信息 var tempFileUrl = ABDataConfig.localABFullName + tempABData.fullName; //源资源文件路径 //得到ab文件与ab依赖文件的哈希值组合 var tempFileHash = HashUtil.GetFileHash(tempFileUrl) + "|" + HashUtil.GetFileHash(tempFileUrl + ABDataConfig.assetBundleManifestFilePostfixName); if (v.Value.fileHash.Equals(tempFileHash)) { DebugManager.Log(tempABData.fullName + " " + v.Key + " 资源文件与依赖文件哈希值与服务器一致!不用更新!"); continue; } DebugManager.Log(tempABData.fullName + " " + v.Key + " 需要更新!添加到更新列表!"); waitDownloadAssetBundleList.Add(new AssetBundleDownloadModel { assetBundleFileName = tempABData.fullName, assetBundleManifestFileName = tempABData.fullName + ABDataConfig.assetBundleManifestFilePostfixName, assetBundleName = tempABData.debugName, }); } }
/// <summary> /// 结束ab资源更新(注意:必须在ab资源全部更新完成后才能调用!) /// </summary> public void ExitUpdateAssetBundle() { //保存cache文件 AssetBundleCacheFileManager.Instance.SaveCacheFileOfAssetBundleDownloadOk(); //卸载dep manager AssetBundleDepFileManager.DestroyInstance(); //卸载cache mananer AssetBundleCacheFileManager.DestroyInstance(); //卸载abdownload manager AssetBundleDownloadManager.DestroyInstance(); DebugManager.LogWarning("ab下载器卸载成功!"); }
/// <summary> /// 初始化当前最新的dep.all ab列表 /// </summary> private void InitDepList(string depFilePath) { curentDepFileReader = new AssetBundleDataReader(); using (FileStream fs = new FileStream(depFilePath, FileMode.Open, FileAccess.Read)) { curentDepFileReader.Read(fs); DebugManager.Log(depFilePath + " dep文件读取成功!读取到的ab资源个数为:" + curentDepFileReader.infoMap.Count); curentDepFileABDataDic = new Dictionary <string, AssetBundleData>(); foreach (var v in curentDepFileReader.infoMap) { if (!curentDepFileABDataDic.ContainsKey(v.Value.debugName)) { curentDepFileABDataDic.Add(v.Value.debugName, v.Value); } else { DebugManager.LogWarning("dep文件读取到的" + v.Value.debugName + "资源重复!"); } } } }
/// <summary> /// 任务下载器 /// </summary> private IEnumerator DownloadTaskMangage(AssetBundleDownloadTaskModel task) { downloadTaskMangageState = true;//状态位标志 string downLoadUrl = ABDataConfig.GetAssetBundleUrl + ABDataConfig.getAssetBundleApiName; DebugManager.Log("任务开始下载:" + task.ToString()); AssetBundleDownloadModel tempABModel = null;//中间变量 #region 遍历未下载列表资源,开启下载 for (int i = 0; i < task.downloadNOModels.Count; ++i) { tempABModel = task.downloadNOModels[i]; DownloadModel downModel = new DownloadModel(); //下载资源文件 task.taskMessage = "下载资源" + tempABModel.assetBundleFileName; downModel.postDatas = System.Text.Encoding.UTF8.GetBytes(tempABModel.assetBundleFileName); yield return(DownloadABFile(downLoadUrl, downModel)); tempABModel.assetBundleData = downModel.resultDatas; //下载资源mainfest文件 task.taskMessage = "下载资源" + tempABModel.assetBundleManifestFileName; downModel.postDatas = System.Text.Encoding.UTF8.GetBytes(tempABModel.assetBundleManifestFileName); yield return(DownloadABFile(downLoadUrl, downModel)); tempABModel.assetBundleManifestData = downModel.resultDatas; if (tempABModel != null && tempABModel.assetBundleManifestData != null && tempABModel.assetBundleData != null) { DebugManager.Log("资源:" + tempABModel.assetBundleName + "下载成功!准备写入本地文件!"); //TODO 写入文件 bool result = FileManager.CreateFile(ABDataConfig.localABFullName + tempABModel.assetBundleFileName, tempABModel.assetBundleData); if (result) { result = FileManager.CreateFile(ABDataConfig.localABFullName + tempABModel.assetBundleManifestFileName, tempABModel.assetBundleManifestData); } if (result) { task.downloadOKModels.Add(tempABModel);//添加到成功队列中 } else { DebugManager.Log("资源:" + tempABModel.assetBundleName + "下载成功,写入文件异常!"); task.downloadErrorModels.Add(tempABModel);//添加到失败队列中 } } else { DebugManager.Log("资源:" + tempABModel.assetBundleName + "下载异常!数据错误!"); task.downloadErrorModels.Add(tempABModel);//添加到失败队列中 } task.downloadNOModels.RemoveAt(i--);//移出未下载队列 tempABModel = null; } #endregion //判断任务是否成功或者失败 if (task.downloadErrorModels != null && task.downloadErrorModels.Count > 0) { DebugManager.LogWarning("任务:" + task.ToString() + "下载失败!"); task.taskState = TaskState.TaskError; task.taskMessage = task.downloadErrorModels.Count + "个资源下载失败!重新下载!"; downloadErrorTasks.Enqueue(task); } else { DebugManager.LogWarning("任务:" + task.ToString() + "下载成功!"); task.taskState = TaskState.TaskOk; task.taskMessage = "下载完成"; downloadSuccessTasks.Add(task); } //当前任务下载结束 downloadTaskMangageState = false; }