/// <summary> /// 下载Version.info /// </summary> /// <param name="serverUrl"></param> /// <param name="localSaveAssetsPath"></param> /// <returns></returns> async private Task <Tuple <string, AssetsVersionInfo, AssetsVersionInfo> > DownloadAssetVersionInfo(string serverUrl, string localSaveAssetsPath = null) { var platform = BApplication.RuntimePlatform; //本地、服务器版本信息的路径 var serverAssetsVersionInfoUrl = BResources.GetServerAssetsVersionInfoPath(serverUrl, platform); string err = null; var serverVersionInfo = new AssetsVersionInfo(); var localVersionInfo = new AssetsVersionInfo(); //开始下载服务器配置 for (int i = 0; i < RETRY_COUNT; i++) { try { var task = await webClient.DownloadStringTaskAsync(serverAssetsVersionInfoUrl); serverVersionInfo = JsonMapper.ToObject <AssetsVersionInfo>(task); err = null; break; } catch (Exception e) { err = e.Message; BDebug.LogError(err); } } //判断本地路径 if (!string.IsNullOrEmpty(localSaveAssetsPath)) { var localAssetsVersionInfoPath = BResources.GetServerAssetsVersionInfoPath(localSaveAssetsPath, platform); if (File.Exists(localAssetsVersionInfoPath)) { localVersionInfo = JsonMapper.ToObject <AssetsVersionInfo>(File.ReadAllText(localAssetsVersionInfoPath)); if (localVersionInfo == null) { localVersionInfo = new AssetsVersionInfo(); } } } //返回 return(new Tuple <string, AssetsVersionInfo, AssetsVersionInfo>(err, serverVersionInfo, localVersionInfo)); }
/// <summary> /// 开始版本控制逻辑 /// </summary> /// <param name="serverUrl">服务器配置根目录</param> /// <param name="localSaveAssetsPath">本地根目录</param> /// <param name="onDownloadProccess">任务进度通知(下载完不等于任务完成!)</param> /// <param name="onTaskEndCallback">任务成功\失败通知!</param> /// 返回码: -1:error 0:success async private Task StartVersionControl(UpdateMode updateMode, string serverUrl, string localSaveAssetsPath, string subPackageName, Action <AssetItem, List <AssetItem> > onDownloadProccess, Action <RetStatus, string> onTaskEndCallback) { var platform = BApplication.RuntimePlatform; //目录准备 var platformStr = BApplication.GetRuntimePlatformPath(); var localSavePlatformPath = IPath.Combine(localSaveAssetsPath, platformStr); if (!Directory.Exists(localSavePlatformPath)) { Directory.CreateDirectory(localSavePlatformPath); } //子包模式判断 bool isDownloadSubPackageMode = !string.IsNullOrEmpty(subPackageName); //1.下载服务器version config var serverVersionInfo = new AssetsVersionInfo(); var localVersionInfo = new AssetsVersionInfo(); #region AssetVersion.info下载 BDebug.Log("【版本控制】1.获取版本信息~", "red"); { var ret = await DownloadAssetVersionInfo(serverUrl, localSaveAssetsPath); if (ret.Item1 != null) { await UniTask.SwitchToMainThread(); onTaskEndCallback?.Invoke(RetStatus.Error, ret.Item1); return; } serverVersionInfo = ret.Item2; localVersionInfo = ret.Item3; } #endregion //2.对比版本、获取对应数据 BDebug.Log("【版本控制】2.对比版本信息~", "red"); string err = null; string suc = null; var serverAssetsInfoList = new List <AssetItem>(); var localAssetsInfoList = new List <AssetItem>(); var serverAssetsContent = ""; // switch (updateMode) { case UpdateMode.Compare: case UpdateMode.CompareWithRepair: //CP模式对比版本与Compare一致 { if (isDownloadSubPackageMode) { //分包模式 (err, suc, serverAssetsInfoList, localAssetsInfoList, serverAssetsContent) = GetDownloadSubPackageData(serverUrl, subPackageName, platform, serverVersionInfo, localVersionInfo); } else { //全量下载 (err, suc, serverAssetsInfoList, localAssetsInfoList, serverAssetsContent) = GetDownloadAssetsData(serverUrl, platform, serverVersionInfo, localVersionInfo); } } break; case UpdateMode.Repair: { //服务器路径 var serverAssetInfosUrl = BResources.GetAssetsInfoPath(serverUrl, platform); //下载服务器Assets.info (err, serverAssetsInfoList, serverAssetsContent) = LoadServerAssetInfo(serverAssetInfosUrl); } break; } //返回返回结果,是否继续下载 if (err != null) { BDebug.LogError(err); await UniTask.SwitchToMainThread(); onTaskEndCallback?.Invoke(RetStatus.Error, err); return; } if (suc != null) { BDebug.Log(suc); await UniTask.SwitchToMainThread(); onTaskEndCallback?.Invoke(RetStatus.Success, suc); return; } //3.生成差异列表 BDebug.Log("【版本控制】3.获取差异列表~", "red"); Queue <AssetItem> diffDownloadQueue = null; #region 生成差异文件 switch (updateMode) { case UpdateMode.Compare: { diffDownloadQueue = Compare(localAssetsInfoList, serverAssetsInfoList, platform); } break; case UpdateMode.Repair: case UpdateMode.CompareWithRepair: //CP 获取差异模式与Repair一致 { diffDownloadQueue = Repair(serverAssetsInfoList, platform); } break; } BDebug.Log($"【版本控制】 配置数量:{serverAssetsInfoList.Count} ,本地存在{serverAssetsInfoList.Count - diffDownloadQueue.Count},下载文件数量{diffDownloadQueue.Count}", "yellow"); #endregion //4.开始下载 #region 根据差异文件下载 BDebug.Log("【版本控制】4.下载资源~", "red"); { var failDownloadList = await DownloadAssets(serverUrl, localSaveAssetsPath, diffDownloadQueue, onDownloadProccess); if (failDownloadList.Count > 0) { onTaskEndCallback(RetStatus.Error, "部分资源未下载完毕!"); return; } } #endregion //5.写入配置到本地 #region 存储配置到本地 BDebug.Log("【版本控制】5.写入配置~", "red"); string localAssetInfoPath = ""; if (isDownloadSubPackageMode) { localAssetInfoPath = BResources.GetAssetsSubPackageInfoPath(BApplication.persistentDataPath, platform, subPackageName); } else { localAssetInfoPath = BResources.GetAssetsInfoPath(BApplication.persistentDataPath, platform); } //写入Asset.Info File.WriteAllText(localAssetInfoPath, serverAssetsContent); BDebug.Log($"【版本控制】写入{Path.GetFileName(localAssetInfoPath)} \n {serverAssetsContent}"); //写入Version.Info if (isDownloadSubPackageMode) { localVersionInfo.Platfrom = serverVersionInfo.Platfrom; //子包版本信息 localVersionInfo.SubPckMap[subPackageName] = serverVersionInfo.SubPckMap[subPackageName]; } else { localVersionInfo.Platfrom = serverVersionInfo.Platfrom; //全量包信息 localVersionInfo.Version = serverVersionInfo.Version; } var localAssetsVersionInfoPath = BResources.GetServerAssetsVersionInfoPath(localSaveAssetsPath, platform); File.WriteAllText(localAssetsVersionInfoPath, JsonMapper.ToJson(localVersionInfo)); BDebug.Log($"【版本控制】写入{Path.GetFileName(localAssetsVersionInfoPath)}"); #endregion // 6.删除过期资源 BDebug.Log("【版本控制】6.冗余资源检查~", "red"); if (!isDownloadSubPackageMode) { var artAssetsPath = IPath.Combine(localSavePlatformPath, BResources.ART_ASSET_ROOT_PATH); var persistentArtAssets = Directory.GetFiles(artAssetsPath, "*", SearchOption.AllDirectories); var replacePath = localSavePlatformPath + "/"; foreach (var assetPath in persistentArtAssets) { var localPath = assetPath.Replace(replacePath, "").Replace("\\", "/"); var ret = serverAssetsInfoList.FirstOrDefault((info) => info.LocalPath.Equals(localPath)); if (ret == null) { BDebug.Log("【版本控制】删除过期资源:" + localPath); File.Delete(assetPath); } } } // 7.资源校验文件 BDebug.Log("【版本控制】7.整包资源校验~", "red"); err = null; foreach (var serverAssetItem in serverAssetsInfoList) { var ret = BResources.IsExsitAssetWithCheckHash(platform, serverAssetItem.LocalPath, serverAssetItem.HashName); if (!ret) { if (string.IsNullOrEmpty(err)) { err = "资源不存在:"; } err += $"\n {serverAssetItem.LocalPath}"; } } //the end. BDebug.Log("【版本控制】end.完成~", "red"); await UniTask.SwitchToMainThread(); if (err == null) { onTaskEndCallback?.Invoke(RetStatus.Success, null); } else { onTaskEndCallback?.Invoke(RetStatus.Error, err); } }