From f3e3f7cf853c9c71b8952d225da4bca77a9d8a27 Mon Sep 17 00:00:00 2001 From: hevinci Date: Mon, 26 Dec 2022 00:48:56 +0800 Subject: [PATCH] update runtime code --- Assets/YooAsset/Runtime/AssetsPackage.cs | 19 +++- ...on.cs => CheckPackageContentsOperation.cs} | 59 +++++++--- ... => CheckPackageContentsOperation.cs.meta} | 0 .../Operations/InitializationOperation.cs | 45 ++------ .../Internal/CopyBuildinManifestOperation.cs | 63 ----------- .../CopyBuildinPackageHashOperation.cs | 63 ----------- .../Internal/DownloadManifestOperation.cs | 12 +- .../QueryRemotePackageVersionOperation.cs | 103 ++++++++++++++++++ ...ueryRemotePackageVersionOperation.cs.meta} | 2 +- .../UnpackBuildinManifestOperation.cs | 94 ++++++++++++++++ ...=> UnpackBuildinManifestOperation.cs.meta} | 0 .../Operations/PreDownloadPackageOperation.cs | 2 - .../UpdatePackageManifestOperation.cs | 19 ++-- .../UpdatePackageVersionOperation.cs | 62 +++-------- .../PlayMode/EditorSimulateModeImpl.cs | 14 ++- .../PatchSystem/PlayMode/HostPlayModeImpl.cs | 10 +- .../PlayMode/OfflinePlayModeImpl.cs | 12 +- .../Runtime/Services/IPlayModeServices.cs | 7 +- 18 files changed, 327 insertions(+), 259 deletions(-) rename Assets/YooAsset/Runtime/PatchSystem/Operations/{CheckContentsIntegrityOperation.cs => CheckPackageContentsOperation.cs} (53%) rename Assets/YooAsset/Runtime/PatchSystem/Operations/{CheckContentsIntegrityOperation.cs.meta => CheckPackageContentsOperation.cs.meta} (100%) delete mode 100644 Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinManifestOperation.cs delete mode 100644 Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs create mode 100644 Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryRemotePackageVersionOperation.cs rename Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/{CopyBuildinPackageHashOperation.cs.meta => QueryRemotePackageVersionOperation.cs.meta} (83%) create mode 100644 Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/UnpackBuildinManifestOperation.cs rename Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/{CopyBuildinManifestOperation.cs.meta => UnpackBuildinManifestOperation.cs.meta} (100%) diff --git a/Assets/YooAsset/Runtime/AssetsPackage.cs b/Assets/YooAsset/Runtime/AssetsPackage.cs index 083651e..a25b032 100644 --- a/Assets/YooAsset/Runtime/AssetsPackage.cs +++ b/Assets/YooAsset/Runtime/AssetsPackage.cs @@ -90,7 +90,7 @@ namespace YooAsset _assetSystemImpl.Initialize(PackageName, true, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); var initializeParameters = parameters as EditorSimulateModeParameters; - initializeOperation = editorSimulateModeImpl.InitializeAsync(PackageName, initializeParameters.LocationToLower, initializeParameters.SimulatePatchManifestPath); + initializeOperation = editorSimulateModeImpl.InitializeAsync(initializeParameters.LocationToLower, initializeParameters.SimulatePatchManifestPath); } else if (_playMode == EPlayMode.OfflinePlayMode) { @@ -219,12 +219,23 @@ namespace YooAsset } /// - /// 检查包裹内容的完整性 + /// 预下载指定版本的包裹资源 /// - public CheckContentsIntegrityOperation CheckContentsIntegrityAsync() + /// 下载的包裹版本 + /// 超时时间(默认值:60秒) + public PreDownloadPackageOperation PreDownloadPackageAsync(string packageVersion, int timeout = 60) { DebugCheckInitialize(); - return _playModeServices.CheckContentsIntegrityAsync(); + return _playModeServices.PreDownloadPackageAsync(packageVersion, timeout); + } + + /// + /// 检查包裹内容的完整性 + /// + public CheckPackageContentsOperation CheckPackageContentsAsync(string packageVersion) + { + DebugCheckInitialize(); + return _playModeServices.CheckPackageContentsOperation(packageVersion); } /// diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckContentsIntegrityOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckPackageContentsOperation.cs similarity index 53% rename from Assets/YooAsset/Runtime/PatchSystem/Operations/CheckContentsIntegrityOperation.cs rename to Assets/YooAsset/Runtime/PatchSystem/Operations/CheckPackageContentsOperation.cs index b3217fb..f2e58a6 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckContentsIntegrityOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckPackageContentsOperation.cs @@ -8,13 +8,13 @@ namespace YooAsset /// /// 检查包裹内容的完整性 /// - public abstract class CheckContentsIntegrityOperation : AsyncOperationBase + public abstract class CheckPackageContentsOperation : AsyncOperationBase { } - internal sealed class EditorSimulateModeCheckContentsIntegrityOperation : CheckContentsIntegrityOperation + internal sealed class EditorSimulateModeCheckPackageContentsOperation : CheckPackageContentsOperation { - internal EditorSimulateModeCheckContentsIntegrityOperation() + internal EditorSimulateModeCheckPackageContentsOperation() { } internal override void Start() @@ -25,9 +25,9 @@ namespace YooAsset { } } - internal sealed class OfflinePlayModeCheckContentsIntegrityOperation : CheckContentsIntegrityOperation + internal sealed class OfflinePlayModeCheckPackageContentsOperation : CheckPackageContentsOperation { - internal OfflinePlayModeCheckContentsIntegrityOperation() + internal OfflinePlayModeCheckPackageContentsOperation() { } internal override void Start() @@ -38,54 +38,83 @@ namespace YooAsset { } } - internal sealed class HostPlayModeCheckContentsIntegrityOperation : CheckContentsIntegrityOperation + internal sealed class HostPlayModeCheckPackageContentsOperation : CheckPackageContentsOperation { private enum ESteps { None, - CheckLoadedManifest, + CheckActiveManifest, + LoadCacheManifest, VerifyPackage, Done, } private readonly HostPlayModeImpl _impl; private readonly string _packageName; + private readonly string _packageVersion; + private LoadCacheManifestOperation _loadCacheManifestOp; private VerifyPackageOperation _verifyOperation; + private PatchManifest _verifyManifest; private ESteps _steps = ESteps.None; - internal HostPlayModeCheckContentsIntegrityOperation(HostPlayModeImpl impl, string packageName) + internal HostPlayModeCheckPackageContentsOperation(HostPlayModeImpl impl, string packageName, string packageVersion) { _impl = impl; _packageName = packageName; + _packageVersion = packageVersion; } internal override void Start() { - _steps = ESteps.CheckLoadedManifest; + _steps = ESteps.CheckActiveManifest; } internal override void Update() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.CheckLoadedManifest) + if (_steps == ESteps.CheckActiveManifest) { - if (_impl.ActiveManifest == null) + // 检测当前激活的清单对象 + if (_impl.ActiveManifest != null && _impl.ActiveManifest.PackageVersion == _packageVersion) { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Not found loaded package : {_packageName}"; + _verifyManifest = _impl.ActiveManifest; + _steps = ESteps.VerifyPackage; } else { + _steps = ESteps.LoadCacheManifest; + } + } + + if (_steps == ESteps.LoadCacheManifest) + { + if (_loadCacheManifestOp == null) + { + _loadCacheManifestOp = new LoadCacheManifestOperation(_packageName, _packageVersion); + OperationSystem.StartOperation(_loadCacheManifestOp); + } + + if (_loadCacheManifestOp.IsDone == false) + return; + + if (_loadCacheManifestOp.Status == EOperationStatus.Succeed) + { + _verifyManifest = _loadCacheManifestOp.Manifest; _steps = ESteps.VerifyPackage; } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _loadCacheManifestOp.Error; + } } if (_steps == ESteps.VerifyPackage) { if (_verifyOperation == null) { - _verifyOperation = VerifyPackageOperation.CreateOperation(_impl.ActiveManifest, _impl); + _verifyOperation = VerifyPackageOperation.CreateOperation(_verifyManifest, _impl); OperationSystem.StartOperation(_verifyOperation); } diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckContentsIntegrityOperation.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckPackageContentsOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/PatchSystem/Operations/CheckContentsIntegrityOperation.cs.meta rename to Assets/YooAsset/Runtime/PatchSystem/Operations/CheckPackageContentsOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs index 22b03ac..a2d9fdd 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs @@ -184,8 +184,7 @@ namespace YooAsset QueryCachePackageVersion, TryLoadCacheManifest, QueryBuildinPackageVersion, - CopyBuildinPackageHash, - CopyBuildinManifest, + UnpackBuildinManifest, LoadBuildinManifest, VerifyPackage, Done, @@ -195,8 +194,7 @@ namespace YooAsset private readonly string _packageName; private QueryBuildinPackageVersionOperation _queryBuildinPackageVersionOp; private QueryCachePackageVersionOperation _queryCachePackageVersionOp; - private CopyBuildinPackageHashOperation _copyBuildinPackageHashOp; - private CopyBuildinManifestOperation _copyBuildinManifestOp; + private UnpackBuildinManifestOperation _unpackBuildinManifestOp; private LoadBuildinManifestOperation _loadBuildinManifestOp; private LoadCacheManifestOperation _loadCacheManifestOp; private VerifyPackageOperation _verifyOperation; @@ -289,7 +287,7 @@ namespace YooAsset // 注意:为了兼容MOD模式,初始化动态新增的包裹的时候,如果内置清单不存在也不需要报错! if (_queryBuildinPackageVersionOp.Status == EOperationStatus.Succeed) { - _steps = ESteps.CopyBuildinPackageHash; + _steps = ESteps.UnpackBuildinManifest; } else { @@ -300,41 +298,18 @@ namespace YooAsset } } - if (_steps == ESteps.CopyBuildinPackageHash) + if (_steps == ESteps.UnpackBuildinManifest) { - if (_copyBuildinPackageHashOp == null) + if (_unpackBuildinManifestOp == null) { - _copyBuildinPackageHashOp = new CopyBuildinPackageHashOperation(_packageName, _queryBuildinPackageVersionOp.PackageVersion); - OperationSystem.StartOperation(_copyBuildinPackageHashOp); + _unpackBuildinManifestOp = new UnpackBuildinManifestOperation(_packageName, _queryBuildinPackageVersionOp.PackageVersion); + OperationSystem.StartOperation(_unpackBuildinManifestOp); } - if (_copyBuildinPackageHashOp.IsDone == false) + if (_unpackBuildinManifestOp.IsDone == false) return; - if (_copyBuildinPackageHashOp.Status == EOperationStatus.Succeed) - { - _steps = ESteps.CopyBuildinManifest; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _copyBuildinPackageHashOp.Error; - } - } - - if (_steps == ESteps.CopyBuildinManifest) - { - if (_copyBuildinManifestOp == null) - { - _copyBuildinManifestOp = new CopyBuildinManifestOperation(_packageName, _queryBuildinPackageVersionOp.PackageVersion); - OperationSystem.StartOperation(_copyBuildinManifestOp); - } - - if (_copyBuildinManifestOp.IsDone == false) - return; - - if (_copyBuildinManifestOp.Status == EOperationStatus.Succeed) + if (_unpackBuildinManifestOp.Status == EOperationStatus.Succeed) { _steps = ESteps.LoadBuildinManifest; } @@ -342,7 +317,7 @@ namespace YooAsset { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = _copyBuildinManifestOp.Error; + Error = _unpackBuildinManifestOp.Error; } } diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinManifestOperation.cs deleted file mode 100644 index 58a03e2..0000000 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinManifestOperation.cs +++ /dev/null @@ -1,63 +0,0 @@ - -namespace YooAsset -{ - internal class CopyBuildinManifestOperation : AsyncOperationBase - { - private enum ESteps - { - None, - CopyBuildinManifestFile, - Done, - } - - private readonly string _buildinPackageName; - private readonly string _buildinPackageVersion; - private UnityWebFileRequester _downloader; - private ESteps _steps = ESteps.None; - - public CopyBuildinManifestOperation(string buildinPackageName, string buildinPackageVersion) - { - _buildinPackageName = buildinPackageName; - _buildinPackageVersion = buildinPackageVersion; - } - internal override void Start() - { - _steps = ESteps.CopyBuildinManifestFile; - } - internal override void Update() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; - - if (_steps == ESteps.CopyBuildinManifestFile) - { - if (_downloader == null) - { - string savePath = PersistentHelper.GetCacheManifestFilePath(_buildinPackageName, _buildinPackageVersion); - string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_buildinPackageName, _buildinPackageVersion); - string filePath = PathHelper.MakeStreamingLoadPath(fileName); - string url = PathHelper.ConvertToWWWPath(filePath); - _downloader = new UnityWebFileRequester(); - _downloader.SendRequest(url, savePath); - } - - if (_downloader.IsDone() == false) - return; - - if (_downloader.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader.GetError(); - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - - _downloader.Dispose(); - } - } - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs deleted file mode 100644 index 0cf8244..0000000 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs +++ /dev/null @@ -1,63 +0,0 @@ - -namespace YooAsset -{ - internal class CopyBuildinPackageHashOperation : AsyncOperationBase - { - private enum ESteps - { - None, - CopyBuildinPackageHashFile, - Done, - } - - private readonly string _buildinPackageName; - private readonly string _buildinPackageVersion; - private UnityWebFileRequester _downloader; - private ESteps _steps = ESteps.None; - - public CopyBuildinPackageHashOperation(string buildinPackageName, string buildinPackageVersion) - { - _buildinPackageName = buildinPackageName; - _buildinPackageVersion = buildinPackageVersion; - } - internal override void Start() - { - _steps = ESteps.CopyBuildinPackageHashFile; - } - internal override void Update() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; - - if (_steps == ESteps.CopyBuildinPackageHashFile) - { - if (_downloader == null) - { - string savePath = PersistentHelper.GetCachePackageHashFilePath(_buildinPackageName, _buildinPackageVersion); - string fileName = YooAssetSettingsData.GetPackageHashFileName(_buildinPackageName, _buildinPackageVersion); - string filePath = PathHelper.MakeStreamingLoadPath(fileName); - string url = PathHelper.ConvertToWWWPath(filePath); - _downloader = new UnityWebFileRequester(); - _downloader.SendRequest(url, savePath); - } - - if (_downloader.IsDone() == false) - return; - - if (_downloader.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader.GetError(); - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - - _downloader.Dispose(); - } - } - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DownloadManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DownloadManifestOperation.cs index 4e17ce2..2d68804 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DownloadManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DownloadManifestOperation.cs @@ -6,8 +6,8 @@ namespace YooAsset private enum ESteps { None, - DownloadPackageHash, - DownloadManifest, + DownloadPackageHashFile, + DownloadManifestFile, Done, } @@ -30,14 +30,14 @@ namespace YooAsset internal override void Start() { RequestCount++; - _steps = ESteps.DownloadPackageHash; + _steps = ESteps.DownloadPackageHashFile; } internal override void Update() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.DownloadPackageHash) + if (_steps == ESteps.DownloadPackageHashFile) { if (_downloader1 == null) { @@ -61,13 +61,13 @@ namespace YooAsset } else { - _steps = ESteps.DownloadManifest; + _steps = ESteps.DownloadManifestFile; } _downloader1.Dispose(); } - if (_steps == ESteps.DownloadManifest) + if (_steps == ESteps.DownloadManifestFile) { if (_downloader2 == null) { diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryRemotePackageVersionOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryRemotePackageVersionOperation.cs new file mode 100644 index 0000000..8988138 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryRemotePackageVersionOperation.cs @@ -0,0 +1,103 @@ +using System.IO; + +namespace YooAsset +{ + internal class QueryRemotePackageVersionOperation : AsyncOperationBase + { + private enum ESteps + { + None, + DownloadPackageVersion, + Done, + } + + private static int RequestCount = 0; + private readonly HostPlayModeImpl _impl; + private readonly string _packageName; + private readonly bool _appendTimeTicks; + private readonly int _timeout; + private UnityWebDataRequester _downloader; + private ESteps _steps = ESteps.None; + + /// + /// 包裹版本 + /// + public string PackageVersion { private set; get; } + + + public QueryRemotePackageVersionOperation(HostPlayModeImpl impl, string packageName, bool appendTimeTicks, int timeout) + { + _impl = impl; + _packageName = packageName; + _appendTimeTicks = appendTimeTicks; + _timeout = timeout; + } + internal override void Start() + { + RequestCount++; + _steps = ESteps.DownloadPackageVersion; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.DownloadPackageVersion) + { + if (_downloader == null) + { + string fileName = YooAssetSettingsData.GetPackageVersionFileName(_packageName); + string webURL = GetPackageVersionRequestURL(fileName); + YooLogger.Log($"Beginning to request package version : {webURL}"); + _downloader = new UnityWebDataRequester(); + _downloader.SendRequest(webURL, _timeout); + } + + Progress = _downloader.Progress(); + if (_downloader.IsDone() == false) + return; + + if (_downloader.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader.GetError(); + } + else + { + PackageVersion = _downloader.GetText(); + if (string.IsNullOrEmpty(PackageVersion)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Remote package version is empty : {_downloader.URL}"; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + + _downloader.Dispose(); + } + } + + private string GetPackageVersionRequestURL(string fileName) + { + string url; + + // 轮流返回请求地址 + if (RequestCount % 2 == 0) + url = _impl.GetPatchDownloadFallbackURL(fileName); + else + url = _impl.GetPatchDownloadMainURL(fileName); + + // 在URL末尾添加时间戳 + if (_appendTimeTicks) + return $"{url}?{System.DateTime.UtcNow.Ticks}"; + else + return url; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryRemotePackageVersionOperation.cs.meta similarity index 83% rename from Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs.meta rename to Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryRemotePackageVersionOperation.cs.meta index 335bb87..608c0d1 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs.meta +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryRemotePackageVersionOperation.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: bb073431e4f3b434e8431b3a8a808dfb +guid: 1d702a1a39789a34da99cbb854708b82 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/UnpackBuildinManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/UnpackBuildinManifestOperation.cs new file mode 100644 index 0000000..beadbe1 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/UnpackBuildinManifestOperation.cs @@ -0,0 +1,94 @@ + +namespace YooAsset +{ + internal class UnpackBuildinManifestOperation : AsyncOperationBase + { + private enum ESteps + { + None, + UnpackManifestHashFile, + UnpackManifestFile, + Done, + } + + private readonly string _buildinPackageName; + private readonly string _buildinPackageVersion; + private UnityWebFileRequester _downloader1; + private UnityWebFileRequester _downloader2; + private ESteps _steps = ESteps.None; + + public UnpackBuildinManifestOperation(string buildinPackageName, string buildinPackageVersion) + { + _buildinPackageName = buildinPackageName; + _buildinPackageVersion = buildinPackageVersion; + } + internal override void Start() + { + _steps = ESteps.UnpackManifestHashFile; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if(_steps == ESteps.UnpackManifestHashFile) + { + if (_downloader1 == null) + { + string savePath = PersistentHelper.GetCachePackageHashFilePath(_buildinPackageName, _buildinPackageVersion); + string fileName = YooAssetSettingsData.GetPackageHashFileName(_buildinPackageName, _buildinPackageVersion); + string filePath = PathHelper.MakeStreamingLoadPath(fileName); + string url = PathHelper.ConvertToWWWPath(filePath); + _downloader1 = new UnityWebFileRequester(); + _downloader1.SendRequest(url, savePath); + } + + if (_downloader1.IsDone() == false) + return; + + if (_downloader1.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader1.GetError(); + } + else + { + _steps = ESteps.UnpackManifestFile; + } + + _downloader1.Dispose(); + } + + if (_steps == ESteps.UnpackManifestFile) + { + if (_downloader2 == null) + { + string savePath = PersistentHelper.GetCacheManifestFilePath(_buildinPackageName, _buildinPackageVersion); + string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_buildinPackageName, _buildinPackageVersion); + string filePath = PathHelper.MakeStreamingLoadPath(fileName); + string url = PathHelper.ConvertToWWWPath(filePath); + _downloader2 = new UnityWebFileRequester(); + _downloader2.SendRequest(url, savePath); + } + + if (_downloader2.IsDone() == false) + return; + + if (_downloader2.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader2.GetError(); + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + + _downloader2.Dispose(); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinManifestOperation.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/UnpackBuildinManifestOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinManifestOperation.cs.meta rename to Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/UnpackBuildinManifestOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/PreDownloadPackageOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/PreDownloadPackageOperation.cs index f652e8c..fe3034c 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/PreDownloadPackageOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/PreDownloadPackageOperation.cs @@ -64,7 +64,6 @@ namespace YooAsset { } } - public class OfflinePlayModePreDownloadPackageOperation : PreDownloadPackageOperation { internal override void Start() @@ -75,7 +74,6 @@ namespace YooAsset { } } - public class HostPlayModePreDownloadPackageOperation : PreDownloadPackageOperation { private enum ESteps diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs index ebffa48..48654e1 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs @@ -47,7 +47,7 @@ namespace YooAsset /// /// 联机模式的更新清单操作 - /// 注意:优先比对沙盒清单哈希值,如果有变化就更新远端清单文件,并保存到本地。 + /// 注意:优先加载沙盒里缓存的清单文件,如果有变化就更新远端清单文件,并保存到本地。 /// internal sealed class HostPlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation { @@ -92,17 +92,16 @@ namespace YooAsset if (_steps == ESteps.CheckActiveManifest) { - // 检测当前激活的清单对象 - if (_impl.ActiveManifest != null) + // 检测当前激活的清单对象 + if (_impl.ActiveManifest != null && _impl.ActiveManifest.PackageVersion == _packageVersion) { - if (_impl.ActiveManifest.PackageVersion == _packageVersion) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - return; - } + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.TryLoadCacheManifest; } - _steps = ESteps.TryLoadCacheManifest; } if (_steps == ESteps.TryLoadCacheManifest) diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageVersionOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageVersionOperation.cs index 2f5886a..fca4280 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageVersionOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageVersionOperation.cs @@ -51,16 +51,15 @@ namespace YooAsset private enum ESteps { None, - DownloadPackageVersion, + QueryRemotePackageVersion, Done, } - private static int RequestCount = 0; private readonly HostPlayModeImpl _impl; private readonly string _packageName; private readonly bool _appendTimeTicks; private readonly int _timeout; - private UnityWebDataRequester _downloader; + private QueryRemotePackageVersionOperation _queryRemotePackageVersionOp; private ESteps _steps = ESteps.None; internal HostPlayModeUpdatePackageVersionOperation(HostPlayModeImpl impl, string packageName, bool appendTimeTicks, int timeout) @@ -72,70 +71,37 @@ namespace YooAsset } internal override void Start() { - RequestCount++; - _steps = ESteps.DownloadPackageVersion; + _steps = ESteps.QueryRemotePackageVersion; } internal override void Update() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.DownloadPackageVersion) + if (_steps == ESteps.QueryRemotePackageVersion) { - if (_downloader == null) + if (_queryRemotePackageVersionOp == null) { - string fileName = YooAssetSettingsData.GetPackageVersionFileName(_packageName); - string webURL = GetPackageVersionRequestURL(fileName); - YooLogger.Log($"Beginning to request package version : {webURL}"); - _downloader = new UnityWebDataRequester(); - _downloader.SendRequest(webURL, _timeout); + _queryRemotePackageVersionOp = new QueryRemotePackageVersionOperation(_impl, _packageName, _appendTimeTicks, _timeout); + OperationSystem.StartOperation(_queryRemotePackageVersionOp); } - Progress = _downloader.Progress(); - if (_downloader.IsDone() == false) + if (_queryRemotePackageVersionOp.IsDone == false) return; - if (_downloader.HasError()) + if (_queryRemotePackageVersionOp.Status == EOperationStatus.Succeed) { + PackageVersion = _queryRemotePackageVersionOp.PackageVersion; _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader.GetError(); + Status = EOperationStatus.Succeed; } else { - PackageVersion = _downloader.GetText(); - if (string.IsNullOrEmpty(PackageVersion)) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Package version is empty : {_downloader.URL}"; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _queryRemotePackageVersionOp.Error; } - - _downloader.Dispose(); } } - - private string GetPackageVersionRequestURL(string fileName) - { - string url; - - // 轮流返回请求地址 - if (RequestCount % 2 == 0) - url = _impl.GetPatchDownloadFallbackURL(fileName); - else - url = _impl.GetPatchDownloadMainURL(fileName); - - // 在URL末尾添加时间戳 - if (_appendTimeTicks) - return $"{url}?{System.DateTime.UtcNow.Ticks}"; - else - return url; - } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs index 97bee8c..72ff0ce 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs @@ -7,15 +7,13 @@ namespace YooAsset internal class EditorSimulateModeImpl : IPlayModeServices, IBundleServices { private PatchManifest _activeManifest; - private string _packageName; private bool _locationToLower; /// /// 异步初始化 /// - public InitializationOperation InitializeAsync(string packageName, bool locationToLower, string simulatePatchManifestPath) + public InitializationOperation InitializeAsync(bool locationToLower, string simulatePatchManifestPath) { - _packageName = packageName; _locationToLower = locationToLower; var operation = new EditorSimulateModeInitializationOperation(this, simulatePatchManifestPath); OperationSystem.StartOperation(operation); @@ -52,9 +50,15 @@ namespace YooAsset OperationSystem.StartOperation(operation); return operation; } - CheckContentsIntegrityOperation IPlayModeServices.CheckContentsIntegrityAsync() + PreDownloadPackageOperation IPlayModeServices.PreDownloadPackageAsync(string packageVersion, int timeout) { - var operation = new EditorSimulateModeCheckContentsIntegrityOperation(); + var operation = new EditorPlayModePreDownloadPackageOperation(); + OperationSystem.StartOperation(operation); + return operation; + } + CheckPackageContentsOperation IPlayModeServices.CheckPackageContentsOperation(string packageVersion) + { + var operation = new EditorSimulateModeCheckPackageContentsOperation(); OperationSystem.StartOperation(operation); return operation; } diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs index de211c5..a8bbf9a 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs @@ -110,9 +110,15 @@ namespace YooAsset OperationSystem.StartOperation(operation); return operation; } - CheckContentsIntegrityOperation IPlayModeServices.CheckContentsIntegrityAsync() + PreDownloadPackageOperation IPlayModeServices.PreDownloadPackageAsync(string packageVersion, int timeout) { - var operation = new HostPlayModeCheckContentsIntegrityOperation(this, _packageName); + var operation = new HostPlayModePreDownloadPackageOperation(this, _packageName, packageVersion, timeout); + OperationSystem.StartOperation(operation); + return operation; + } + CheckPackageContentsOperation IPlayModeServices.CheckPackageContentsOperation(string packageVersion) + { + var operation = new HostPlayModeCheckPackageContentsOperation(this, _packageName, packageVersion); OperationSystem.StartOperation(operation); return operation; } diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs index a437700..c5a9a91 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs @@ -7,7 +7,6 @@ namespace YooAsset internal class OfflinePlayModeImpl : IPlayModeServices, IBundleServices { private PatchManifest _activeManifest; - private string _packageName; private bool _locationToLower; /// @@ -15,7 +14,6 @@ namespace YooAsset /// public InitializationOperation InitializeAsync(string packageName, bool locationToLower) { - _packageName = packageName; _locationToLower = locationToLower; var operation = new OfflinePlayModeInitializationOperation(this, packageName); OperationSystem.StartOperation(operation); @@ -52,9 +50,15 @@ namespace YooAsset OperationSystem.StartOperation(operation); return operation; } - CheckContentsIntegrityOperation IPlayModeServices.CheckContentsIntegrityAsync() + PreDownloadPackageOperation IPlayModeServices.PreDownloadPackageAsync(string packageVersion, int timeout) { - var operation = new OfflinePlayModeCheckContentsIntegrityOperation(); + var operation = new OfflinePlayModePreDownloadPackageOperation(); + OperationSystem.StartOperation(operation); + return operation; + } + CheckPackageContentsOperation IPlayModeServices.CheckPackageContentsOperation(string packageVersion) + { + var operation = new OfflinePlayModeCheckPackageContentsOperation(); OperationSystem.StartOperation(operation); return operation; } diff --git a/Assets/YooAsset/Runtime/Services/IPlayModeServices.cs b/Assets/YooAsset/Runtime/Services/IPlayModeServices.cs index fe5dbeb..9a73a21 100644 --- a/Assets/YooAsset/Runtime/Services/IPlayModeServices.cs +++ b/Assets/YooAsset/Runtime/Services/IPlayModeServices.cs @@ -23,10 +23,15 @@ namespace YooAsset /// UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, int timeout); + /// + /// 预下载指定版本的包裹资源 + /// + PreDownloadPackageOperation PreDownloadPackageAsync(string packageVersion, int timeout); + /// /// 检查包裹内容的完整性 /// - CheckContentsIntegrityOperation CheckContentsIntegrityAsync(); + CheckPackageContentsOperation CheckPackageContentsOperation(string packageVersion); // 下载相关 PatchDownloaderOperation CreatePatchDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout);