/// <summary> /// Finishs the download. /// </summary> /// <param name="fileName">File name.</param> private void FinishDownload(string fileName) { // Loom.QueueOnMainThread (()=>{ // ResourcesUpdatePanel.Instance.SetProgress (downloadFileIndex, totalFileCount, false); // }); string localFilePath = AssetBundleFilePath.ServerExtensionDataPath + fileName + ".7z"; string decompressFilePath = AssetBundleFilePath.ServerExtensionDataPath + fileName + ".temp7z"; //中间文件 Loom.RunAsync(() => { DecompressFileLZMA(fileName, localFilePath, decompressFilePath); }); }
static void CompressFile() { Debug.Log("开始压缩文件"); //清理文件 string outPutPath = Application.dataPath.Replace("Assets", "") + "AssetBundles" + "/ServerCompressAssets"; if (!Directory.Exists(outPutPath)) { Directory.CreateDirectory(outPutPath); } string[] paths = Directory.GetFiles(outPutPath); for (int i = 0; i < paths.Length; ++i) { File.Delete(paths[i]); } //copy csv info file File.Copy(GetOutPutPath() + "/" + csvFileName, outPutPath + "/" + csvFileName); File.Copy(GetOutPutPath() + "/" + "resVer.txt", outPutPath + "/" + "resVer.txt"); //压缩文件 String[] names = AssetDatabase.GetAllAssetBundleNames(); List <string> fileList = new List <string>(); fileList.AddRange(names); string bundleName = GetPlatformFolderForAssetBundles(EditorUserBuildSettings.activeBuildTarget); fileList.Add(bundleName); string sourcesPath = GetOutPutPath(); Loom.RunAsync(() => { for (int i = 0; i < fileList.Count; ++i) { CompressFileLZMA(sourcesPath + "/" + fileList[i], outPutPath + "/" + fileList[i] + ".7z"); Debug.Log("Compress " + fileList[i]); } Debug.Log("完成 打包成服务器资源"); }); }
/// <summary> /// 下载要更新的文件 /// 执行顺序 DownloadNextFile -> FinishDownload ->UpdateResoureceInfoData ->DownloadNextFile /// 直到结束 EndDownload /// </summary> /// <param name="fileList">File list.</param> void DownLoadNextFile() { if (downLoadFileList.Count < 1) { return; } //ResourcesUpdatePanel.Instance.SetProgress (downloadFileIndex, totalFileCount, true); string fileName = downLoadFileList [0]; string serverFilePath = serverAddr + fileName + ".7z"; string localFilePath = AssetBundleFilePath.ServerExtensionDataPath + fileName + ".7z"; Debug.Log("downloading" + serverFilePath); downingFileCount++; float progress = 0f; bool isDone = false; //开启子线程下载,使用匿名方法 Loom.RunAsync(() => { //使用流操作文件 FileStream fs = new FileStream(localFilePath, FileMode.OpenOrCreate, FileAccess.Write); //获取文件现在的长度 long fileLength = fs.Length; //获取下载文件的总长度 long totalLength = GetLength(serverFilePath); //如果没下载完 if (fileLength < totalLength) { //断点续传核心,设置本地文件流的起始位置 fs.Seek(fileLength, SeekOrigin.Begin); HttpWebRequest request = HttpWebRequest.Create(serverFilePath) as HttpWebRequest; //断点续传核心,设置远程访问文件流的起始位置 request.AddRange((int)fileLength); Stream stream = request.GetResponse().GetResponseStream(); byte[] buffer = new byte[1024]; //使用流读取内容到buffer中 //注意方法返回值代表读取的实际长度,并不是buffer有多大,stream就会读进去多少 int length = stream.Read(buffer, 0, buffer.Length); while (length > 0) { if (isClose) { break; } //将内容再写入本地文件中 fs.Write(buffer, 0, length); //计算进度 fileLength += length; progress = (float)fileLength / (float)totalLength; //UnityEngine.Debug.Log(progress); //类似尾递归 length = stream.Read(buffer, 0, buffer.Length); } stream.Close(); stream.Dispose(); } else { progress = 1; } fs.Close(); fs.Dispose(); //如果下载完毕,执行回调 if (progress == 1) { isDone = true; Debug.Log(fileName + "Download finished!"); downingFileCount--; FinishDownload(fileName); } else { if (isClose) { return; } downingFileCount--; //多次重试 失败后 直接开始游戏 if (downloadRetryCount > 4) { Loom.QueueOnMainThread(() => { EndHotFixUpdate(); }); return; } //下载出错了 再次下载 Loom.QueueOnMainThread(() => { ++downloadRetryCount; DownLoadNextFile(); }); return; } }); }