/// <summary> /// 从文件解析 /// </summary> /// <param name="path"></param> /// <returns></returns> public int parseLocalVersionXml(string path) { UpdateLog.INFO_LOG(_TAG + "parseLocalVersionXml(string path): " + path); int ret = CodeDefine.RET_SUCCESS; try { var sp = MonoXmlUtils.LoadXmlEx(path); if (sp == null || sp.ToXml() == null) { UpdateLog.ERROR_LOG(_TAG + "File not exist or invalid: " + path); return(CodeDefine.RET_FAIL_PARSE_LOCAL_XML_FILE); } dom = sp.ToXml(); _localVersionXml = path; parse(dom); } catch (System.Exception ex) { ret = CodeDefine.RET_FAIL_PARSE_LOCAL_XML_FILE; UpdateLog.ERROR_LOG(_TAG + ex.Message + "\n" + ex.StackTrace); UpdateLog.EXCEPTION_LOG(ex); } return(ret); }
//下载 private int downloadRemoteXml(string remoteUrl, string storeDir) { UpdateLog.INFO_LOG("下载resourceVersionXml " + remoteUrl); int ret = CodeDefine.RET_SUCCESS; if (string.IsNullOrEmpty(_remoteXmlName)) { UpdateLog.ERROR_LOG("downloadResourceVersion(): resourceXmlName == null || \"\".Equals(resourceXmlName)"); ret = CodeDefine.RET_FAIL_RES_XML_PATH_ERROR; return(ret); } string savePath = (storeDir + "/" + _remoteXmlName).Replace("\\", "/").Replace("//", "/"); if (!Directory.Exists(storeDir)) { Directory.CreateDirectory(storeDir); } if (File.Exists(savePath)) { File.Delete(savePath); Thread.Sleep(1); } ret = _fileDownload.DownloadUseBackCdn(savePath, remoteUrl, 0, false); if (ret < CodeDefine.RET_SUCCESS) { UpdateLog.ERROR_LOG("downloadResourceVersion(): resource file is not exist or download fail!"); ret = CodeDefine.RET_FAIL_DOWNLOAD_RES_XML; } return(ret); }
/// <summary> /// 下载单个文件 /// </summary> /// <param name="savePath">保存路径</param> /// <param name="url">下载地址</param> /// <param name="isContinue">是否断点续传</param> /// <param name="isRetryAllways">失败后是否一直重试</param> /// <returns>小于0失败</returns> public int DownloadFile(string savePath, string url, bool isContinue = true) { UpdateLog.INFO_LOG(_TAG + "DownloadFile(string strFileName, string url, bool isContinue = true) : " + savePath + "," + url + "," + isContinue); int downloadRet = CodeDefine.RET_FAIL; long SPosition = 0; //如果本地有下载文件,则判断是否断点续传 FileStream FStream = null; try { ExistFileSize = 0; if (File.Exists(savePath) && isContinue) { //定向文件写入位置 FStream = File.Open(savePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); //已存在文件大小 ExistFileSize = (int)SPosition; } else { FStream = new FileStream(savePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); SPosition = 0; } //开始下载 downloadRet = HttpDownload(url, FStream, SPosition); if (FStream.CanWrite) { FStream.Close(); } Thread.Sleep(_SLEEP_TIME); } catch (System.Exception ex) { UpdateLog.ERROR_LOG(ex.Message); UpdateLog.EXCEPTION_LOG(ex); } finally { if (FStream != null && FStream.CanWrite) { FStream.Close(); } } return(downloadRet); }
public int DownloadBaseRes(bool downloadAll) { int ret = CodeDefine.RET_FAIL; _needDownloadList = needDownloadBaseResList(); if (_needDownloadList.Count == 0) { UpdateLog.INFO_LOG("do not need download base res, the version is the latest one. : version = " + _localBaseResVersion); return(CodeDefine.RET_SUCCESS); } int currentState = 0; for (int i = 0; i < _needDownloadList.Count; i++) { currentState = int.Parse(_needDownloadList[i].ToVersion); var toDownloadModel = _needDownloadList[i]; string resourceUrl = toDownloadModel.ResourceUrl.Replace("\\", "/"); string resName = resourceUrl.Substring(resourceUrl.LastIndexOf("/") + 1); string storePath = System.IO.Path.Combine(_storeDir, resName); long downloadedSize = 0; if (checkFinishDownload(storePath, toDownloadModel, out downloadedSize)) { ret = CodeDefine.RET_SUCCESS; UpdateLog.INFO_LOG("分段资源已经下载好了: " + storePath); continue; } long totalSize = long.Parse(toDownloadModel.FileSize); long mapSize = long.Parse(toDownloadModel.Map_size); long needDownloadSize = totalSize - downloadedSize; UpdateLog.INFO_LOG("UpdateFlow: 需要下载基础资源 " + storePath + "totalSize = " + totalSize + " needDownloadSize=" + needDownloadSize); //后台下载不做提示 if (!_backDownload && !Pause((int)totalSize)) { return(CodeDefine.RET_SKIP_BY_CANCEL); } ret = _fileDownload.DownloadUseBackCdn(storePath, toDownloadModel.ResourceUrl, (int)totalSize, true); //失败、非all、非后台下载都中断 if (ret <= CodeDefine.RET_FAIL || !downloadAll || !_backDownload) { break; } } UpdateLog.DEBUG_LOG("下载base资源---"); return(ret); }
/// <summary> /// 保存文件 /// </summary> /// <param name="baseVersion"></param> /// <param name="patchVersion"></param> /// <param name="hasCopy"></param> /// <param name="appVersion"></param> /// <returns></returns> public int save(string baseVersion = "", string patchVersion = "", string hasCopy = "", string appVersion = "") { UpdateLog.INFO_LOG(_TAG + "save()"); int ret = CodeDefine.RET_SUCCESS; bool hasChange = false; try { if (!"".Equals(baseVersion) && baseVersion != _baseResVersion) { _baseResVersion = baseVersion; set(dom, "local_info/local_base_res_version", baseVersion); hasChange = true; } if (!"".Equals(patchVersion) && patchVersion != _patchResVersion) { _patchResVersion = patchVersion; hasChange = true; set(dom, "local_info/local_patch_res_version", patchVersion); } if (!"".Equals(hasCopy) && hasCopy != _hasCopy) { _hasCopy = hasCopy; hasChange = true; set(dom, "local_info/hasCopy", hasCopy); } if (!"".Equals(appVersion) && appVersion != _localAppVersion) { _localAppVersion = appVersion; hasChange = true; set(dom, "local_info/local_app_version", appVersion); } if (hasChange) { MonoXmlUtils.SaveXml(_localVersionXml, dom); } else { UpdateLog.DEBUG_LOG("没有改动,不保存localversion.xml"); } } catch (System.Exception ex) { ret = CodeDefine.RET_FAIL_SAVE_LOCAL_XML_FILE; UpdateLog.ERROR_LOG(_TAG + ex.Message + "\n" + ex.StackTrace); UpdateLog.EXCEPTION_LOG(ex); } return(ret); }
/// <summary> /// 保存 /// </summary> /// <param name="localVersion"></param> /// <returns></returns> public int save(LocalVersionXml localVersion) { UpdateLog.INFO_LOG(_TAG + "save()"); int ret = CodeDefine.RET_SUCCESS; try { set(dom, "local_info/local_base_res_version", localVersion.BaseResVersion); set(dom, "local_info/local_patch_res_version", localVersion.PatchResVersion); set(dom, "local_info/hasCopy", localVersion.HasCopy); set(dom, "local_info/local_app_version", localVersion.LocalAppVersion); MonoXmlUtils.SaveXml(_localVersionXml, dom); } catch (System.Exception ex) { ret = CodeDefine.RET_FAIL_SAVE_LOCAL_XML_FILE; UpdateLog.ERROR_LOG(_TAG + ex.Message + "\n" + ex.StackTrace); UpdateLog.EXCEPTION_LOG(ex); } return(ret); }
private int downloadMapFile() { UpdateLog.INFO_LOG("downloadMapFile +++"); int num = 1; List <VersionModel> list = new List <VersionModel>(); // if (this._currentData.VersionModelBaseList.Count > 0) { list.AddRange(this._currentData.VersionModelBaseList); } if (list.Count == 0) { num = 0; } for (int i = 0; i < list.Count; i++) { VersionModel model = list[i]; string str = model.Map_url.Replace(@"\", "/"); string str2 = str.Substring(str.LastIndexOf("/") + 1); string localMapFile = Path.Combine(BaseFlow._storeDir, str2); if (this.checkNeedDownloadMapFile(localMapFile, model.Map_md5)) { string url = model.Map_url; int num3 = this._fileDownload.DownloadUseBackCdn(localMapFile, url, this.ParseInt(model.Map_size), false); if ((num3 >= 0) && this.checkNeedDownloadMapFile(localMapFile, model.Map_md5)) { string str5 = MD5.MD5File(localMapFile); UpdateLog.ERROR_LOG("Download map file error md5: " + localMapFile + " md5=" + str5 + "\n online md5: " + model.Map_url + " md5=" + model.Map_md5); num3 = -6; } if (num3 <= -1) { return(num3); } } } UpdateLog.INFO_LOG("downloadMapFile ---"); return(num); }
public int parseMapFile(string mapFile, string resUrl, string saveDir) { _saveDir = saveDir; UpdateLog.INFO_LOG(_TAG + "parseMapFile(string mapFile, string resUrl):" + mapFile + "," + resUrl); int ret = CodeDefine.RET_SUCCESS; List <MapFileData> mapFileDataList = _mapFileDataList; FileStream mapFileStream = null; try { mapFileStream = new FileStream(mapFile, FileMode.Open); long filePosition = 0; long mapFileSize = mapFileStream.Length; while (mapFileSize > 0 && filePosition != mapFileSize) { if (filePosition >= mapFileSize) { UpdateLog.ERROR_LOG("解析出错了"); return(ret); } MapFileData mapFileData = new MapFileData(); mapFileData.Begin = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); if (mapFileData.Begin == -1) { UpdateLog.WARN_LOG("解析map文件,出现异常,请检查: " + mapFile); return(ret); } mapFileData.End = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); mapFileData.DirLen = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); mapFileData.NameLen = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); mapFileData.Md5Len = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); mapFileData.FileSize = parseInt(read(mapFileStream, 10, filePosition, out filePosition)); mapFileData.Dir = read(mapFileStream, mapFileData.DirLen, filePosition, out filePosition); mapFileData.Name = read(mapFileStream, mapFileData.NameLen, filePosition, out filePosition); mapFileData.Md5 = read(mapFileStream, mapFileData.Md5Len, filePosition, out filePosition); mapFileData.ResUrl = resUrl; mapFileData.SaveDir = _saveDir; UnityEngine.Debug.Log("mapFileData.Name :" + mapFileData.Name); mapFileDataList.Add(mapFileData); } } catch (System.Exception ex) { ret = CodeDefine.RET_FAIL_PARSE_MAP_FILE; UpdateLog.ERROR_LOG(_TAG + ex.Message + "\n" + ex.StackTrace); UpdateLog.EXCEPTION_LOG(ex); } finally { if (mapFileStream != null) { mapFileStream.Close(); } } return(ret); }
/// <summary> /// 释放资源 /// 文件结构 4x32 文件头 |32位记录路径长度|32位记录文件名|32位记录md5长度|32位记录文件大小| /// 然后根据这4个结构获取具体的路径、文件名、md5值、文件内容 /// </summary> /// <returns></returns> public int UnzipRes() { UpdateLog.INFO_LOG(_TAG + "unzipRes()"); int ret = CodeDefine.RET_SUCCESS; FileStream resFileStream = null; long filePostion = 0; long fileSize = 0; try { resFileStream = new FileStream(_resPath, FileMode.Open, FileAccess.Read, FileShare.Read); fileSize = resFileStream.Length; List <ResourceFileData> fileDataList = new List <ResourceFileData>(); //启动线程池 // var threadPool = new HttpThreadPool<UnzipData>(4, ThreadFunc); while (fileSize > 0 && filePostion < fileSize) { ResourceFileData fileData = new ResourceFileData(); //4x32长度的头 fileData.DirLen = int.Parse(read(resFileStream, 32, filePostion, out filePostion)); fileData.NameLen = int.Parse(read(resFileStream, 32, filePostion, out filePostion)); fileData.Md5Len = int.Parse(read(resFileStream, 32, filePostion, out filePostion)); fileData.FileSize = long.Parse(read(resFileStream, 32, filePostion, out filePostion)); //读取内容 fileData.Dir = read(resFileStream, fileData.DirLen, filePostion, out filePostion); fileData.Name = read(resFileStream, fileData.NameLen, filePostion, out filePostion); fileData.Md5 = read(resFileStream, fileData.Md5Len, filePostion, out filePostion); //跳过localversion的释放 if (fileData.Name.ToLower().Equals("localversion.xml")) { resFileStream.Seek(fileData.FileSize, SeekOrigin.Current); } else { writeFile(resFileStream, fileData.FileSize, fileData.Dir, fileData.Name); //resFileStream.Seek(fileData.FileSize, SeekOrigin.Current); //UnzipData ud = new UnzipData(fileData, filePostion, fileSize); //threadPool.addTask(ud); } filePostion += fileData.FileSize; } //等待所有文件下载完 //threadPool.waitWhileWorking(); } catch (System.Exception ex) { ret = CodeDefine.RET_FAIL_UNZIP_RES_FILE; UpdateLog.ERROR_LOG(_TAG + ex.Message + "\n" + ex.StackTrace); UpdateLog.EXCEPTION_LOG(ex); } finally { if (resFileStream != null) { resFileStream.Close(); } } return(ret); }
//解析xml public int parseResouceVersionXml(string path) { UpdateLog.INFO_LOG(_TAG + "parseResouceVersionXml(string path):" + path); int ret = CodeDefine.RET_SUCCESS; try { var sp = MonoXmlUtils.LoadXmlEx(path); if (sp == null || sp.ToXml() == null) { return(CodeDefine.RET_FAIL_PARSE_RES_XML_FILE); } var dom = sp.ToXml(); //正式流程 parse(dom, "ResourceVersion/VersionBase", NormalFollow.VersionModelBaseList); parse(dom, "ResourceVersion/VersionPatch", NormalFollow.VersionModelPatchList); NormalFollow.AppVersion = parse(dom, "ResourceVersion/CodeVersion_last/Version"); NormalFollow.ClientUrl = parse(dom, "ResourceVersion/CodeVersion_last/url"); NormalFollow.AppSize = parse(dom, "ResourceVersion/CodeVersion_last/size"); NormalFollow.Language = parse(dom, "ResourceVersion/loginSever/language"); bool enable = true; if (!Boolean.TryParse(parse(dom, "ResourceVersion/ForceUpdate"), out enable)) { enable = true; } NormalFollow.EnableForceUpdate = enable; NormalFollow.PatchVersion = GetMaxPatchVersion(NormalFollow.VersionModelPatchList); //测试流程 parse(dom, "ResourceVersion/test_tag/VersionBase", TestFollow.VersionModelBaseList); parse(dom, "ResourceVersion/test_tag/VersionPatch", TestFollow.VersionModelPatchList); TestFollow.AppVersion = parse(dom, "ResourceVersion/test_tag/app_current_version"); TestFollow.ClientUrl = parse(dom, "ResourceVersion/test_tag/app_update_url"); TestFollow.AppSize = parse(dom, "ResourceVersion/test_tag/test_size"); TestFollow.Language = parse(dom, "ResourceVersion/test_tag/language"); if (!Boolean.TryParse(parse(dom, "ResourceVersion/test_tag/ForceUpdate"), out enable)) { enable = true; } TestFollow.EnableForceUpdate = enable; TestFollow.PatchVersion = GetMaxPatchVersion(TestFollow.VersionModelPatchList); //白名单 mac地址 var macList = parseNodes(dom, "ResourceVersion/test_tag/legal_client_machine_list/legal_client_machine"); for (int i = 0; macList != null && i < macList.Length; i++) { if (macList[i] == null) { continue; } WhiteCode.Add(macList[i].Text); } //白名单 用户名 var userList = parseNodes(dom, "ResourceVersion/test_tag/legal_client_user_list/legal_client_user"); for (int i = 0; userList != null && i < userList.Length; i++) { if (userList[i] == null) { continue; } WhiteUsers.Add(userList[i].Text); } //白名单 ip地址 var ipList = parseNodes(dom, "ResourceVersion/test_tag/legal_client_ip_list/legal_client_ip"); for (int i = 0; ipList != null && i < ipList.Length; i++) { if (ipList[i] == null) { continue; } WhiteIp.Add(ipList[i].Text); } } catch (System.Exception ex) { ret = CodeDefine.RET_FAIL_PARSE_RES_XML_FILE; UpdateLog.ERROR_LOG(_TAG + ex.Message + "/n" + ex.StackTrace); UpdateLog.EXCEPTION_LOG(ex); } return(ret); }