From fb5e289de02a903e95fad792791029ee460168ad Mon Sep 17 00:00:00 2001 From: hevinci Date: Thu, 7 Jul 2022 19:10:44 +0800 Subject: [PATCH] Update patch system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化代码逻辑结构 --- .../Operations/UpdateManifestOperation.cs | 2 +- .../Operations/UpdatePackageOperation.cs | 2 +- .../Runtime/PatchSystem/PatchManifest.cs | 44 ++++++---- .../PlayMode/EditorSimulateModeImpl.cs | 18 ++-- .../PatchSystem/PlayMode/HostPlayModeImpl.cs | 84 +++++++++---------- .../PlayMode/OfflinePlayModeImpl.cs | 26 +++--- 6 files changed, 89 insertions(+), 87 deletions(-) diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs index 8bd39eb..8a2951e 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs @@ -382,7 +382,7 @@ namespace YooAsset // 忽略APP资源 // 注意:如果是APP资源并且哈希值相同,则不需要下载 - if (appPatchManifest.Bundles.TryGetValue(patchBundle.BundleName, out PatchBundle appPatchBundle)) + if (appPatchManifest.TryGetPatchBundle(patchBundle.BundleName, out PatchBundle appPatchBundle)) { if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash) continue; diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageOperation.cs index a3faccc..4fcb996 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageOperation.cs @@ -203,7 +203,7 @@ namespace YooAsset // 忽略APP资源 // 注意:如果是APP资源并且哈希值相同,则不需要下载 - if (_impl.AppPatchManifest.Bundles.TryGetValue(patchBundle.BundleName, out PatchBundle appPatchBundle)) + if (_impl.AppPatchManifest.TryGetPatchBundle(patchBundle.BundleName, out PatchBundle appPatchBundle)) { if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash) continue; diff --git a/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs b/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs index d5b10aa..035882c 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs @@ -42,13 +42,13 @@ namespace YooAsset /// 资源包集合(提供BundleName获取PatchBundle) /// [NonSerialized] - public readonly Dictionary Bundles = new Dictionary(); + public readonly Dictionary BundleDic = new Dictionary(); /// /// 资源映射集合(提供AssetPath获取PatchAsset) /// [NonSerialized] - public readonly Dictionary Assets = new Dictionary(); + public readonly Dictionary AssetDic = new Dictionary(); /// /// 资源路径映射集合 @@ -117,7 +117,7 @@ namespace YooAsset /// public string MappingToAssetPath(string location) { - if(string.IsNullOrEmpty(location)) + if (string.IsNullOrEmpty(location)) { YooLogger.Error("Failed to mapping location to asset path, The location is null or empty."); return string.Empty; @@ -138,18 +138,18 @@ namespace YooAsset } /// - /// 获取资源包名称 + /// 获取主资源包 /// 注意:传入的资源路径一定合法有效! /// - public string GetBundleName(string assetPath) + public PatchBundle GetMainPatchBundle(string assetPath) { - if (Assets.TryGetValue(assetPath, out PatchAsset patchAsset)) + if (AssetDic.TryGetValue(assetPath, out PatchAsset patchAsset)) { int bundleID = patchAsset.BundleID; if (bundleID >= 0 && bundleID < BundleList.Count) { var patchBundle = BundleList[bundleID]; - return patchBundle.BundleName; + return patchBundle; } else { @@ -166,17 +166,17 @@ namespace YooAsset /// 获取资源依赖列表 /// 注意:传入的资源路径一定合法有效! /// - public string[] GetAllDependencies(string assetPath) + public PatchBundle[] GetAllDependencies(string assetPath) { - if (Assets.TryGetValue(assetPath, out PatchAsset patchAsset)) + if (AssetDic.TryGetValue(assetPath, out PatchAsset patchAsset)) { - List result = new List(patchAsset.DependIDs.Length); + List result = new List(patchAsset.DependIDs.Length); foreach (var dependID in patchAsset.DependIDs) { if (dependID >= 0 && dependID < BundleList.Count) { var dependPatchBundle = BundleList[dependID]; - result.Add(dependPatchBundle.BundleName); + result.Add(dependPatchBundle); } else { @@ -191,6 +191,22 @@ namespace YooAsset } } + /// + /// 尝试获取补丁资源 + /// + public bool TryGetPatchAsset(string assetPath, out PatchAsset result) + { + return AssetDic.TryGetValue(assetPath, out result); + } + + /// + /// 尝试获取补丁资源包 + /// + public bool TryGetPatchBundle(string bundleName, out PatchBundle result) + { + return BundleDic.TryGetValue(bundleName, out result); + } + /// /// 序列化 @@ -212,7 +228,7 @@ namespace YooAsset foreach (var patchBundle in patchManifest.BundleList) { patchBundle.ParseFlagsValue(); - patchManifest.Bundles.Add(patchBundle.BundleName, patchBundle); + patchManifest.BundleDic.Add(patchBundle.BundleName, patchBundle); } // AssetList @@ -220,10 +236,10 @@ namespace YooAsset { // 注意:我们不允许原始路径存在重名 string assetPath = patchAsset.AssetPath; - if (patchManifest.Assets.ContainsKey(assetPath)) + if (patchManifest.AssetDic.ContainsKey(assetPath)) throw new Exception($"AssetPath have existed : {assetPath}"); else - patchManifest.Assets.Add(assetPath, patchAsset); + patchManifest.AssetDic.Add(assetPath, patchAsset); } return patchManifest; diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs index 8f3bc42..cb116d8 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs @@ -40,19 +40,13 @@ namespace YooAsset #region IBundleServices接口 BundleInfo IBundleServices.GetBundleInfo(AssetInfo assetInfo) { - if(assetInfo.IsInvalid) + if (assetInfo.IsInvalid) throw new Exception("Should never get here !"); - string bundleName = _simulatePatchManifest.GetBundleName(assetInfo.AssetPath); - if (_simulatePatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle patchBundle)) - { - BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromEditor, assetInfo.AssetPath); - return bundleInfo; - } - else - { - throw new Exception("Should never get here !"); - } + // 注意:如果补丁清单里未找到资源包会抛出异常! + var patchBundle = _simulatePatchManifest.GetMainPatchBundle(assetInfo.AssetPath); + BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromEditor, assetInfo.AssetPath); + return bundleInfo; } BundleInfo[] IBundleServices.GetAllDependBundleInfos(AssetInfo assetInfo) { @@ -64,7 +58,7 @@ namespace YooAsset } PatchAsset IBundleServices.TryGetPatchAsset(string assetPath) { - if (_simulatePatchManifest.Assets.TryGetValue(assetPath, out PatchAsset patchAsset)) + if (_simulatePatchManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset)) return patchAsset; else return null; diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs index 2fbe32e..396a1bb 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs @@ -56,7 +56,7 @@ namespace YooAsset OperationSystem.StartOperaiton(operation); return operation; } - + /// /// 异步更新补丁清单(弱联网) /// @@ -108,7 +108,7 @@ namespace YooAsset break; } } - if(used == false) + if (used == false) { YooLogger.Log($"Delete unused cache file : {fileInfo.Name}"); File.Delete(fileInfo.FullName); @@ -136,7 +136,7 @@ namespace YooAsset // 忽略APP资源 // 注意:如果是APP资源并且哈希值相同,则不需要下载 - if (AppPatchManifest.Bundles.TryGetValue(patchBundle.BundleName, out PatchBundle appPatchBundle)) + if (AppPatchManifest.TryGetPatchBundle(patchBundle.BundleName, out PatchBundle appPatchBundle)) { if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash) continue; @@ -168,7 +168,7 @@ namespace YooAsset // 忽略APP资源 // 注意:如果是APP资源并且哈希值相同,则不需要下载 - if (AppPatchManifest.Bundles.TryGetValue(patchBundle.BundleName, out PatchBundle appPatchBundle)) + if (AppPatchManifest.TryGetPatchBundle(patchBundle.BundleName, out PatchBundle appPatchBundle)) { if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash) continue; @@ -215,21 +215,17 @@ namespace YooAsset continue; } - string mainBundleName = LocalPatchManifest.GetBundleName(assetInfo.AssetPath); - if (LocalPatchManifest.Bundles.TryGetValue(mainBundleName, out PatchBundle mainBundle)) - { - if (checkList.Contains(mainBundle) == false) - checkList.Add(mainBundle); - } + // 注意:如果补丁清单里未找到资源包会抛出异常! + PatchBundle mainBundle = LocalPatchManifest.GetMainPatchBundle(assetInfo.AssetPath); + if (checkList.Contains(mainBundle) == false) + checkList.Add(mainBundle); - string[] dependBundleNames = LocalPatchManifest.GetAllDependencies(assetInfo.AssetPath); - foreach (var dependBundleName in dependBundleNames) + // 注意:如果补丁清单里未找到资源包会抛出异常! + PatchBundle[] dependBundles = LocalPatchManifest.GetAllDependencies(assetInfo.AssetPath); + foreach (var dependBundle in dependBundles) { - if (LocalPatchManifest.Bundles.TryGetValue(dependBundleName, out PatchBundle dependBundle)) - { - if (checkList.Contains(dependBundle) == false) - checkList.Add(dependBundle); - } + if (checkList.Contains(dependBundle) == false) + checkList.Add(dependBundle); } } @@ -242,7 +238,7 @@ namespace YooAsset // 忽略APP资源 // 注意:如果是APP资源并且哈希值相同,则不需要下载 - if (AppPatchManifest.Bundles.TryGetValue(patchBundle.BundleName, out PatchBundle appPatchBundle)) + if (AppPatchManifest.TryGetPatchBundle(patchBundle.BundleName, out PatchBundle appPatchBundle)) { if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash) continue; @@ -376,53 +372,51 @@ namespace YooAsset } #region IBundleServices接口 - private BundleInfo CreateBundleInfo(string bundleName) + private BundleInfo CreateBundleInfo(PatchBundle patchBundle) { - if (LocalPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle patchBundle)) + if (patchBundle == null) + throw new Exception("Should never get here !"); + + // 查询沙盒资源 + if (DownloadSystem.ContainsVerifyFile(patchBundle.Hash)) { - // 查询沙盒资源 - if (DownloadSystem.ContainsVerifyFile(patchBundle.Hash)) + BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromCache); + return bundleInfo; + } + + // 查询APP资源 + if (AppPatchManifest.TryGetPatchBundle(patchBundle.BundleName, out PatchBundle appPatchBundle)) + { + if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash) { - BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromCache); + BundleInfo bundleInfo = new BundleInfo(appPatchBundle, BundleInfo.ELoadMode.LoadFromStreaming); return bundleInfo; } - - // 查询APP资源 - if (AppPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle appPatchBundle)) - { - if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash) - { - BundleInfo bundleInfo = new BundleInfo(appPatchBundle, BundleInfo.ELoadMode.LoadFromStreaming); - return bundleInfo; - } - } - - // 从服务端下载 - return ConvertToDownloadInfo(patchBundle); - } - else - { - throw new Exception("Should never get here !"); } + + // 从服务端下载 + return ConvertToDownloadInfo(patchBundle); } BundleInfo IBundleServices.GetBundleInfo(AssetInfo assetInfo) { if (assetInfo.IsInvalid) throw new Exception("Should never get here !"); - string bundleName = LocalPatchManifest.GetBundleName(assetInfo.AssetPath); - return CreateBundleInfo(bundleName); + // 注意:如果补丁清单里未找到资源包会抛出异常! + var patchBundle = LocalPatchManifest.GetMainPatchBundle(assetInfo.AssetPath); + return CreateBundleInfo(patchBundle); } BundleInfo[] IBundleServices.GetAllDependBundleInfos(AssetInfo assetInfo) { if (assetInfo.IsInvalid) throw new Exception("Should never get here !"); + // 注意:如果补丁清单里未找到资源包会抛出异常! var depends = LocalPatchManifest.GetAllDependencies(assetInfo.AssetPath); List result = new List(depends.Length); - foreach (var bundleName in depends) + foreach (var patchBundle in depends) { - BundleInfo bundleInfo = CreateBundleInfo(bundleName); + BundleInfo bundleInfo = CreateBundleInfo(patchBundle); result.Add(bundleInfo); } return result.ToArray(); @@ -433,7 +427,7 @@ namespace YooAsset } PatchAsset IBundleServices.TryGetPatchAsset(string assetPath) { - if (LocalPatchManifest.Assets.TryGetValue(assetPath, out PatchAsset patchAsset)) + if (LocalPatchManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset)) return patchAsset; else return null; diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs index 1a06198..8a711d6 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs @@ -38,36 +38,34 @@ namespace YooAsset } #region IBundleServices接口 - private BundleInfo CreateBundleInfo(string bundleName) + private BundleInfo CreateBundleInfo(PatchBundle patchBundle) { - if (_appPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle patchBundle)) - { - BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromStreaming); - return bundleInfo; - } - else - { + if (patchBundle == null) throw new Exception("Should never get here !"); - } + + BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromStreaming); + return bundleInfo; } BundleInfo IBundleServices.GetBundleInfo(AssetInfo assetInfo) { if (assetInfo.IsInvalid) throw new Exception("Should never get here !"); - string bundleName = _appPatchManifest.GetBundleName(assetInfo.AssetPath); - return CreateBundleInfo(bundleName); + // 注意:如果补丁清单里未找到资源包会抛出异常! + var patchBundle = _appPatchManifest.GetMainPatchBundle(assetInfo.AssetPath); + return CreateBundleInfo(patchBundle); } BundleInfo[] IBundleServices.GetAllDependBundleInfos(AssetInfo assetInfo) { if (assetInfo.IsInvalid) throw new Exception("Should never get here !"); + // 注意:如果补丁清单里未找到资源包会抛出异常! var depends = _appPatchManifest.GetAllDependencies(assetInfo.AssetPath); List result = new List(depends.Length); - foreach (var bundleName in depends) + foreach (var patchBundle in depends) { - BundleInfo bundleInfo = CreateBundleInfo(bundleName); + BundleInfo bundleInfo = CreateBundleInfo(patchBundle); result.Add(bundleInfo); } return result.ToArray(); @@ -78,7 +76,7 @@ namespace YooAsset } PatchAsset IBundleServices.TryGetPatchAsset(string assetPath) { - if (_appPatchManifest.Assets.TryGetValue(assetPath, out PatchAsset patchAsset)) + if (_appPatchManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset)) return patchAsset; else return null;