private async void UpdateAsync() { while (true) { if (this.bundles.Count == 0) { break; } this.downloadingBundle = this.bundles.Dequeue(); while (true) { try { using (this.downloadingRequest = EntityFactory.Create <UnityWebRequestAsync>()) { await this.downloadingRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle); byte[] data = this.downloadingRequest.Request.downloadHandler.data; string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle); if (!Directory.Exists(Path.GetDirectoryName(path))) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } using (FileStream fs = new FileStream(path, FileMode.Create)) { fs.Write(data, 0, data.Length); } } } catch (Exception e) { Log.Error($"download bundle error: {this.downloadingBundle}\n{e}"); continue; } break; } this.downloadedBundles.Add(this.downloadingBundle); this.downloadingBundle = ""; this.downloadingRequest = null; } using (FileStream fs = new FileStream(Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"), FileMode.Create)) using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(MongoHelper.ToJson(this.VersionConfig)); } this.Tcs?.SetResult(true); }
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 Task StartAsync() { using (UnityWebRequestAsync request = EntityFactory.Create <UnityWebRequestAsync>()) { string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt"; Log.Debug(versionUrl); await request.DownloadAsync(versionUrl); this.VersionConfig = MongoHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); } Log.Debug("WebVersion:\n" + MongoHelper.ToJson(this.VersionConfig)); // 对比本地的Version.txt string versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"); if (!File.Exists(versionPath)) { foreach (FileVersionInfo versionInfo in this.VersionConfig.FileVersionInfos) { if (versionInfo.File == "Version.txt") { continue; } this.bundles.Enqueue(versionInfo.File); this.TotalSize += versionInfo.Size; } } else { VersionConfig localVersionConfig = MongoHelper.FromJson <VersionConfig>(File.ReadAllText(versionPath)); Log.Debug("LocalVersion:\n" + MongoHelper.ToJson(localVersionConfig)); // 先删除服务器端没有的ab foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileVersionInfos) { if (this.VersionConfig.FileInfoDict.ContainsKey(fileVersionInfo.File)) { continue; } string abPath = Path.Combine(PathHelper.AppHotfixResPath, fileVersionInfo.File); File.Delete(abPath); } // 再下载 foreach (FileVersionInfo fileVersionInfo in this.VersionConfig.FileVersionInfos) { FileVersionInfo localVersionInfo; if (localVersionConfig.FileInfoDict.TryGetValue(fileVersionInfo.File, out localVersionInfo)) { if (fileVersionInfo.MD5 == localVersionInfo.MD5) { continue; } } if (fileVersionInfo.File == "Version.txt") { continue; } this.bundles.Enqueue(fileVersionInfo.File); this.TotalSize += fileVersionInfo.Size; } } if (this.bundles.Count == 0) { return; } Log.Debug($"need download bundles: {this.bundles.ToList().ListToString()}"); await this.WaitAsync(); }
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; } }