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 = EntityFactory.Create <UnityWebRequestAsync>(this.Domain)) { await this.webRequest.DownloadAsync(Define.GetUrl() + "StreamingAssets/" + this.downloadingBundle); byte[] data = this.webRequest.Request.downloadHandler.data; string path = Path.Combine(Define.AppHotfixResPath, this.downloadingBundle); using (FileStream fs = new FileStream(path, FileMode.Create)) { fs.Write(data, 0, data.Length); } } } catch (Exception e) { Log.LogError($"download bundle error: {this.downloadingBundle}\n{e}"); continue; } break; } this.downloadedBundles.Add(this.downloadingBundle); this.downloadingBundle = ""; this.webRequest = null; } } catch (Exception e) { Log.LogError(e); } }
public async ETTask StartAsync() { // 获取远程的Version.txt string versionUrl = ""; try { using (UnityWebRequestAsync webRequestAsync = EntityFactory.Create <UnityWebRequestAsync>(this.Domain)) { versionUrl = Define.GetUrl() + "StreamingAssets/" + "Version.txt"; //Log.Debug(versionUrl); await webRequestAsync.DownloadAsync(versionUrl); remoteVersionConfig = JsonUtility.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(Define.AppResPath4Web, "Version.txt"); using (UnityWebRequestAsync request = EntityFactory.Create <UnityWebRequestAsync>(this.Domain)) { await request.DownloadAsync(versionPath); streamingVersionConfig = JsonUtility.FromJson <VersionConfig>(request.Request.downloadHandler.text); } // 删掉远程不存在的文件 DirectoryInfo directoryInfo = new DirectoryInfo(Define.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; } }