/// <summary> /// 解析cache.txt 文本 string ab源资源名字(Assets/perfabs/cube.perfab),value为该ab的哈希值等其他详细信息 /// </summary> private Dictionary <string, CacheFileDataModel> ResolveCacheFile(string cacheFileText) { try { Dictionary <string, CacheFileDataModel> cacheFileDataModelDic = new Dictionary <string, CacheFileDataModel>(); using (StringReader sr = new StringReader(cacheFileText)) { //TODO 版本比较(暂时不作处理) string vString = sr.ReadLine(); //读取缓存ab的信息 while (true) { string path = sr.ReadLine();//ab资源的源路径(打包前的路径) if (path == null) { break; } CacheFileDataModel cache = new CacheFileDataModel(); cache.fileHash = sr.ReadLine(); cache.metaHash = sr.ReadLine(); cache.bundleCrc = sr.ReadLine(); int depsCount = Convert.ToInt32(sr.ReadLine()); cache.depNames = new string[depsCount]; for (int i = 0; i < depsCount; i++) { cache.depNames[i] = sr.ReadLine(); } cacheFileDataModelDic.Add(path, cache); } } return(cacheFileDataModelDic); } catch (Exception e) { DebugManager.LogError("解析cache.txt 文本 出错!" + e.Message); } return(null); }
/// <summary> /// 【从参数1文件夹】中拷贝【参数3中的文件名】到【参数2文件夹】中 /// </summary> private IEnumerator CopyAssetBundle(string resDicUrl, string targetDicUrl, IEnumerable fileNames) { foreach (var v in fileNames) { using (WWW www = new WWW(resDicUrl + v)) { yield return(www); if (www.isDone && string.IsNullOrEmpty(www.error)) { using (FileStream fs = new FileStream(targetDicUrl + v, FileMode.OpenOrCreate)) { fs.Write(www.bytes, 0, www.bytes.Length); fs.Close(); } } else { DebugManager.LogError("文件拷贝出错!" + resDicUrl + v + "error:" + www.error); } } } }
/// <summary> /// 下载文本文件 参数1:文件下载路径 回调参数:下载好的text /// </summary> private static IEnumerator DownLoadFileText(string url, Action <string> onComplete) { using (WWW www = new WWW(url)) { yield return(www); if (www.isDone) { if (string.IsNullOrEmpty(www.error)) { onComplete(www.text); } else { DebugManager.LogError("文本文件下载出错!error:" + www.error + " url:" + url); } } else { DebugManager.LogError("文本文件下载失败 !url:" + url); } } }
/// <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; }