// 初始化操作 public IEnumerator init() { if (!isLoadFromAB) { this._isOK = true; yield break; } string version = getABDataPath("assets/version.json"); string fixVersion = getABPath("assets/version.json"); // 固定文件发布专用 WWW wwwfix = new WWW(fixVersion); yield return(wwwfix); _fixInfo = new XPackInfo(); if (!string.IsNullOrEmpty(wwwfix.error) || !_fixInfo.load(wwwfix.text)) { XDebug.LogError("装载安装文件失败:" + fixVersion + wwwfix.error); wwwfix.Dispose(); yield break; } string wwwfixtext = wwwfix.text; wwwfix.Dispose(); wwwfix = null; // 新文件存在,读取应该可以读出来,有可能损坏了,读取出错则清空,当成没有 do { string versionWrite = getABDataWritePath("assets/version.json"); if (XUtil.isExistFile(versionWrite)) { WWW wwwver = new WWW(version); yield return(wwwver); _info = new XPackInfo(); if (!string.IsNullOrEmpty(wwwver.error) || !_info.load(wwwver.text)) { XDebug.LogError("装载版本文件失败:采取删除版本文件进行修复" + version + wwwver.error); wwwver.Dispose(); wwwver = null; _info = null; XUtil.deleteFile(version); break; } // 清理 wwwver.Dispose(); wwwver = null; // 查看 // 被重新安装了 if (isReinstall) { // 那么直接清空所有的更新文件夹 XDebug.LogError("版本被重新安装了!直接删除所有缓存目录"); XUtil.clearDirectory(getABDataWritePath("")); _info = null; } } }while (false); // _fixInfo肯定为好的 if (_info == null) { _info = new XPackInfo(); // 如果不存在fixInfo,那么 fix指向 _info.load(wwwfixtext); } // 装载信息完毕 this._isOK = true; }
// 更新流程 public IEnumerator updateVersion(string publishUrl) { // 转化下平台接口 publishUrl = getPlatformPublishUrl(publishUrl); // 切换进度条 XLoading.instance.beginShow(0, this); string urlver = XUtil.getUrl(publishUrl, "ver.json"); WWW wwwver = new WWW(urlver); _www = wwwver; _wwwinfo = "获取" + urlver + "中"; yield return(wwwver); XVer ver = new XVer(); if (!string.IsNullOrEmpty(wwwver.error) || !ver.load(wwwver.text)) { XDebug.LogError("远程版本文件" + urlver + wwwver.error); wwwver.Dispose(); _www = null; _status = XUpdateStatus.FAIL; yield break; } wwwver.Dispose(); XDebug.Log("更新开始远程版本号:v" + ver.ver + "当前版本号:v" + XLoad.instance.localver); if (ver.ver != XLoad.instance.localver) { string urlversion = XUtil.getUrl(ver.ab, "v" + ver.ver + "/assets/version.json"); WWW wwwWebInfo = new WWW(urlversion); _www = wwwWebInfo; _wwwinfo = "获取" + urlversion + "中"; yield return(wwwWebInfo); XPackInfo webInfo = new XPackInfo(); if (!string.IsNullOrEmpty(wwwWebInfo.error) || !webInfo.load(wwwWebInfo.text)) { XDebug.LogError("远程版本文件" + urlver + wwwWebInfo.error); wwwWebInfo.Dispose(); _www = null; _status = XUpdateStatus.FAIL; yield break; } wwwWebInfo.Dispose(); Dictionary <string, XBundleInfo> modify = XLoad.instance._info.compareWebList(webInfo); uint allsize = 0; int filecount = modify.Count; foreach (var bd in modify) { allsize += (uint)(bd.Value.fileSize); } if (filecount <= 0) { XDebug.Log("文件一样无需更新修改版本号"); XLoad.instance._info._version = ver.ver; // 保存文件 XLoad.instance._info.saveFile(XLoad.getABDataWritePath("assets/version.json")); _status = XUpdateStatus.SUCESS; // todo 修改版本号 yield break; } XDebug.Log(string.Format("一共{0}个文件,大小需要更新{1}", filecount, XUtil.getSizeString(allsize))); // 更新目录 this.oldver = XLoad.instance.localver; this.newver = ver.ver; string updateDirName = getUpdateDirName(); string updateFileName = getUpdateFileName("update.json"); if (XUtil.isExistDirectory(updateDirName) && XUtil.isExistFile(updateFileName)) { if (!loadFile(updateFileName)) { alreadyDownloadDict.Clear(); XUtil.clearDirectory(updateDirName); } } int fileno = 0; foreach (var bd in modify) { fileno++; string ufilename = getUpdateFileName(bd.Key, bd.Value.isDirectory); if (alreadyDownloadDict.ContainsKey(bd.Key)) { XBundleInfo bundleInfo = alreadyDownloadDict[bd.Key]; if (bundleInfo.fileSize == bd.Value.fileSize && bundleInfo.hash == bd.Value.hash && XUtil.isExistFile(ufilename)) { XDebug.Log("文件已下载完毕,跳过" + ufilename); continue; } } if (XUtil.isExistFile(ufilename)) { XUtil.deleteFile(ufilename); } string webufile = getUpdateWebName(ver.ab, "v" + ver.ver + "/" + bd.Key, bd.Value.isDirectory); WWW wwwufile = new WWW(webufile); _www = wwwufile; _wwwinfo = string.Format("下载({0}/{1}){2}", fileno, filecount, bd.Key); yield return(wwwufile); if (!string.IsNullOrEmpty(wwwufile.error)) { XDebug.LogError("远程文件" + webufile + wwwufile.error); wwwufile.Dispose(); _www = null; _status = XUpdateStatus.FAIL; yield break; } if (!XUtil.writeFileAllBytes(ufilename, wwwufile.bytes)) { XDebug.LogError("写入文件失败" + ufilename); wwwufile.Dispose(); _www = null; _status = XUpdateStatus.FAIL; yield break; } alreadyDownloadDict[bd.Key] = bd.Value; // 保存一下记录 saveFile(updateFileName); wwwufile.Dispose(); _www = null; } // 已经成功了,拷贝文件到目标文件 _wwwinfo = "拷贝文件中"; foreach (var bd in modify) { string ufilename = getUpdateFileName(bd.Key, bd.Value.isDirectory); string dfilename = XLoad.getABDataWritePath(bd.Key, bd.Value.isDirectory); if (!XUtil.copyFile(ufilename, dfilename)) { XDebug.LogError("拷贝文件失败" + ufilename); _status = XUpdateStatus.FAIL; break; } XLoad.instance._info.addBundle(bd.Key, bd.Value); } _wwwinfo = "重新生成版本文件"; XLoad.instance._info.callDependAllSize(); // 变更版本 XLoad.instance._info._version = ver.ver; // 保存文件 if (!XLoad.instance._info.saveFile(XLoad.getABDataWritePath("assets/version.json"))) { _status = XUpdateStatus.FAIL; yield break; } _status = XUpdateStatus.SUCESS; } else { // XDebug.Log("版本一致没有什么不同无需更新"); _status = XUpdateStatus.SUCESS; } }