private static void CopyDirectory(string srcPath, string destPath) { try { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { if (!Directory.Exists(destPath + "\\" + i.Name)) { Directory.CreateDirectory(destPath + "\\" + i.Name); //目标目录下不存在此文件夹即创建子文件夹 } CopyDirectory(i.FullName, destPath + "\\" + i.Name); //递归调用复制子文件夹 } else { File.Copy(i.FullName, destPath + "\\" + i.Name, true); //不是文件夹即复制文件,true表示可以覆盖同名文件 } } } catch (Exception e) { DebugManager.LogError("拷贝文件夹失败!" + e.Message); } }
/// <summary> /// 创建文件 参数1:文件路径 参数2:文件数据 参数3:是否覆盖创建 /// </summary> public static bool CreateFile(string path, byte[] data, bool overCreate) { try { if (overCreate && File.Exists(path)) { File.Delete(path); } using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)) { if (data != null) { fs.Write(data, 0, data.Length); } fs.Close(); } DebugManager.Log(path + "文件创建成功!"); return(true); } catch (Exception e) { DebugManager.LogError(path + "文件创建失败!error:" + e.Message); } return(false); }
/// <summary> /// 启动任务下载器 /// </summary> private void StarDownloadTaskManage(AssetBundleDownloadTaskModel task) { if (task == null || downloadTaskMangageState || task.downloadNOModels == null) { return; } if (task.downloadNOModels.Count == 0) { task.taskState = TaskState.TaskOk; task.taskMessage = "下载完成"; downloadSuccessTasks.Add(task); return; } try { task.taskState = TaskState.Tasking;//任务开启中 task.taskMessage = "开始下载资源"; StartCoroutine(DownloadTaskMangage(task)); } catch (Exception e) { task.taskMessage = "下载出错!"; DebugManager.LogError(task + "任务下载出错!" + e.Message); downloadErrorTasks.Enqueue(task); downloadTaskMangageState = false;//出错将下载状态设置为false } }
/// <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> /// 拷贝一个文件夹到另一个文件夹 参数1:源文件夹 参数2:目标文件夹 /// </summary> public static void CopyDirectoryToOtherDir(string srcPath, string destPath) { if (!Directory.Exists(srcPath)) { DebugManager.LogError("源文件夹路径不存在!" + srcPath); return; } if (!Directory.Exists(destPath)) { DebugManager.LogError("目标文件夹路径不存在!" + destPath); return; } CopyDirectory(srcPath, destPath); }
/// <summary> /// 下载ab文件的方法 参数1:下载地址 参数2:byte[] 数据 /// </summary> private IEnumerator DownloadABFile(string downLoadUrl, DownloadModel downloadModel) { using (WWW www = new WWW(downLoadUrl, downloadModel.postDatas, abDownLoadHeads)) { yield return(www); if (www.isDone && string.IsNullOrEmpty(www.error)) { downloadModel.resultDatas = www.bytes; } else { DebugManager.LogError(downLoadUrl + "下载出错!error:" + www.error); } } }
/// <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:文件下载路径 回调参数:下载好的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> /// 【从参数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); } } } }