void DownloadPatch(Dictionary <string, Patch> .Enumerator enumerator, int i, int totalToDownload) { if (!enumerator.MoveNext()) { //下载完毕,将新版本号写入patchIndex,并保存 _patchIndexLocal.Version = VersionCode; _patchIndexLocal.PatchVersion = _patchIndexNew.PatchVersion; _patchIndexLocal.WriteToFile(); ResManager.Ins.ReadDownloadDir(); if (Listener != null) { Listener.OnPatchDownloadFinish(); } CheckUpdate(); //补丁更新结束后再次检查更新 return; } i++; var patchKV = enumerator.Current; var name = patchKV.Key; var patch = patchKV.Value; if (patch.NeedDelete) //如果这个补丁是删除更新,则删除 { FileInfo file = UResources.ReqFile(name); if (file != null) { File.Delete(file.FullName); } if (Listener != null) { Listener.OnPatchDownloadProgress(name, 1, i, totalToDownload); } _patchIndexLocal.UpdatePatch(name, patch); _patchIndexLocal.WriteToFile(); DownloadPatch(enumerator, i, totalToDownload); return; } //下载更新 string url = String.Format(_patchIndexNew.PatchBaseURL, name, patch.Version); HttpManager.Get(url, (error, www) => { if (!string.IsNullOrEmpty(error)) { UpdaterLog.LogError("下载补丁包出错" + name + " " + www.error + " url:" + www.url); if (Listener != null) { Listener.OnPatchDownloadFail(error); } return; } string filePath = ResManager.FullDownloadDir + name; File.WriteAllBytes(filePath, www.bytes); //下载完一个文件后立刻写入PatchIndex,这样中途断开网络后重新下载可以不下载该文件 _patchIndexLocal.UpdatePatch(name, patch); _patchIndexLocal.WriteToFile(); DownloadPatch(enumerator, i, totalToDownload); return; }, progress => { if (Listener != null) { Listener.OnPatchDownloadProgress(name, progress, i, totalToDownload); } }); }
private void CheckUpdate() { //检查是否有patchIndex文件,并读取内容 var indexFile = UResources.ReqFile(INDEX_FILE_NAME); if (indexFile != null) { string fileJson = DESFileUtil.ReadAllText(indexFile.FullName, DESKey.DefaultKey, DESKey.DefaultIV); _patchIndexLocal = new PatchIndex(this.VersionCode, JObject.Parse(fileJson)); if (_patchIndexLocal.Version != this.VersionCode) { UpdaterLog.Log("发现patchIndex中的主版本不等于目前的主版本,说明刚经历过主包更新,清空补丁"); foreach (var file in ResManager.Ins.DownloadedFiles.Values) { File.Delete(file.FullName); } ResManager.Ins.ReadDownloadDir(); _patchIndexLocal = new PatchIndex(); } } else { _patchIndexLocal = new PatchIndex(); try { //清空下载目录 Directory.Delete(ResManager.FullDownloadDir, true); } catch { } try { // 重新创建目录,并写回PatchIndex Directory.CreateDirectory(ResManager.FullDownloadDir); _patchIndexLocal.WriteToFile(); } catch (Exception e) { Debug.LogException(e); } } //构建检查更新需要的表单 JObject json = new JObject(); json["Version"] = this.VersionCode; json["PatchVersion"] = _patchIndexLocal.PatchVersion; if (CustomData != null) { foreach (var kv in CustomData) { json[kv.Key.ToString()] = kv.Value.ToString(); } } UpdaterLog.Log(json.ToFormatString()); WWWForm form = new WWWForm(); form.AddField("Data", json.ToString()); HttpManager.Post(CheckUpdateUrl, form, (error, www) => { if (!string.IsNullOrEmpty(error)) { Listener.OnError(ErrorCode.NET_CONNECT_ERROR); return; } UpdaterLog.Log(www.text); JObject responseJObj = JObject.Parse(www.text); int code = responseJObj["code"].OptInt(ErrorCode.DEFAULT.Code); if (code == ErrorCode.SUCCESS.Code) { _updateType = responseJObj["Type"].OptEnum <UpdateType>(UpdateType.NoUpdate); switch (_updateType) { case UpdateType.NoUpdate: _patchIndexNew = new PatchIndex(this.VersionCode, responseJObj); //没有找到更新,验证文件 Listener.OnVerifyStart(); if (VerifyPatches()) { Listener.OnVerifySuccess(); Listener.OnPassUpdate(responseJObj); } else { Listener.OnVerifyFail(); } break; case UpdateType.MainPak: _mainPakIndexFromServer = new MainPakIndex(this.VersionCode, responseJObj); Listener.OnFindMainUpdate(_mainPakIndexFromServer.Info); break; case UpdateType.Patch: _patchIndexNew = new PatchIndex(this.VersionCode, responseJObj); if (Listener != null) { Listener.OnFindPatchUpdate(_patchIndexNew.PatchInfo, _patchIndexNew.TotalPatchSize); } break; } } else { Listener.OnError(new ErrorCode(code, responseJObj["error"].OptObject())); } }, null, 15); }