diff --git a/Assets/YooAsset/Runtime/AssetsPackage.cs b/Assets/YooAsset/Runtime/AssetsPackage.cs index 3235757..f456fc2 100644 --- a/Assets/YooAsset/Runtime/AssetsPackage.cs +++ b/Assets/YooAsset/Runtime/AssetsPackage.cs @@ -211,11 +211,11 @@ namespace YooAsset /// 更新的包裹版本 /// 自动激活清单 /// 超时时间(默认值:60秒) - public UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, bool autoSaveManifestFile = true, int timeout = 60) + public UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, int timeout = 60) { DebugCheckInitialize(); DebugCheckUpdateManifest(); - return _playModeServices.UpdatePackageManifestAsync(packageVersion, autoSaveManifestFile, timeout); + return _playModeServices.UpdatePackageManifestAsync(packageVersion, timeout); } /// @@ -244,7 +244,9 @@ namespace YooAsset public string GetPackageVersion() { DebugCheckInitialize(); - return _playModeServices.GetPackageVersion(); + if (_playModeServices.ActiveManifest == null) + return string.Empty; + return _playModeServices.ActiveManifest.PackageVersion; } /// diff --git a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs index 5eca183..cdd3918 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs @@ -11,8 +11,8 @@ namespace YooAsset /// internal class UnityWebDataRequester { - protected UnityWebRequest _webRequest; - protected UnityWebRequestAsyncOperation _operationHandle; + private UnityWebRequest _webRequest; + private UnityWebRequestAsyncOperation _operationHandle; /// /// 请求URL地址 diff --git a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs index eccdfe6..9277324 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine.Networking; +using UnityEngine; namespace YooAsset { @@ -11,8 +12,14 @@ namespace YooAsset /// internal class UnityWebFileRequester { - protected UnityWebRequest _webRequest; - protected UnityWebRequestAsyncOperation _operationHandle; + private UnityWebRequest _webRequest; + private UnityWebRequestAsyncOperation _operationHandle; + + // 超时相关 + private float _timeout; + private bool _isAbort = false; + private ulong _latestDownloadBytes; + private float _latestDownloadRealtime; /// /// 请求URL地址 @@ -23,11 +30,12 @@ namespace YooAsset /// /// 发送GET请求 /// - public void SendRequest(string url, string savePath) + public void SendRequest(string url, string savePath, float timeout = 60) { if (_webRequest == null) { URL = url; + _timeout = timeout; _webRequest = new UnityWebRequest(URL, UnityWebRequest.kHttpVerbGET); DownloadHandlerFile handler = new DownloadHandlerFile(savePath); handler.removeFileOnAbort = true; @@ -96,5 +104,28 @@ namespace YooAsset } return string.Empty; } + + /// + /// 检测超时 + /// + public void CheckTimeout() + { + // 注意:在连续时间段内无新增下载数据及判定为超时 + if (_isAbort == false) + { + if (_latestDownloadBytes != _webRequest.downloadedBytes) + { + _latestDownloadBytes = _webRequest.downloadedBytes; + _latestDownloadRealtime = Time.realtimeSinceStartup; + } + + float offset = Time.realtimeSinceStartup - _latestDownloadRealtime; + if (offset > _timeout) + { + _webRequest.Abort(); + _isAbort = true; + } + } + } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckContentsIntegrityOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckContentsIntegrityOperation.cs index 40e42d1..b3217fb 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckContentsIntegrityOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckContentsIntegrityOperation.cs @@ -44,14 +44,13 @@ namespace YooAsset { None, CheckLoadedManifest, - StartVerifyOperation, - CheckVerifyOperation, + VerifyPackage, Done, } private readonly HostPlayModeImpl _impl; private readonly string _packageName; - private VerifyCacheFilesOperation _verifyOperation; + private VerifyPackageOperation _verifyOperation; private ESteps _steps = ESteps.None; internal HostPlayModeCheckContentsIntegrityOperation(HostPlayModeImpl impl, string packageName) @@ -70,7 +69,7 @@ namespace YooAsset if (_steps == ESteps.CheckLoadedManifest) { - if (_impl.ActivePatchManifest == null) + if (_impl.ActiveManifest == null) { _steps = ESteps.Done; Status = EOperationStatus.Failed; @@ -78,19 +77,18 @@ namespace YooAsset } else { - _steps = ESteps.StartVerifyOperation; + _steps = ESteps.VerifyPackage; } } - if (_steps == ESteps.StartVerifyOperation) + if (_steps == ESteps.VerifyPackage) { - _verifyOperation = VerifyCacheFilesOperation.CreateOperation(_impl.ActivePatchManifest, _impl); - OperationSystem.StartOperation(_verifyOperation); - _steps = ESteps.CheckVerifyOperation; - } + if (_verifyOperation == null) + { + _verifyOperation = VerifyPackageOperation.CreateOperation(_impl.ActiveManifest, _impl); + OperationSystem.StartOperation(_verifyOperation); + } - if (_steps == ESteps.CheckVerifyOperation) - { Progress = _verifyOperation.Progress; if (_verifyOperation.IsDone == false) return; diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs index f7d19ea..22b03ac 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs @@ -10,10 +10,7 @@ namespace YooAsset /// public abstract class InitializationOperation : AsyncOperationBase { - /// - /// 初始化内部加载的包裹版本 - /// - public string InitializedPackageVersion; + public string PackageVersion { protected set; get; } } /// @@ -24,53 +21,41 @@ namespace YooAsset private enum ESteps { None, - LoadManifestFileData, - CheckDeserializeManifest, + LoadEditorManifest, Done, } private readonly EditorSimulateModeImpl _impl; - private readonly string _simulatePatchManifestPath; - private DeserializeManifestOperation _deserializer; + private readonly string _simulateManifestPath; + private LoadEditorManifestOperation _loadEditorManifestOp; private ESteps _steps = ESteps.None; - internal EditorSimulateModeInitializationOperation(EditorSimulateModeImpl impl, string simulatePatchManifestPath) + internal EditorSimulateModeInitializationOperation(EditorSimulateModeImpl impl, string simulateManifestPath) { _impl = impl; - _simulatePatchManifestPath = simulatePatchManifestPath; + _simulateManifestPath = simulateManifestPath; } internal override void Start() { - _steps = ESteps.LoadManifestFileData; + _steps = ESteps.LoadEditorManifest; } internal override void Update() { - if (_steps == ESteps.LoadManifestFileData) + if (_steps == ESteps.LoadEditorManifest) { - if (File.Exists(_simulatePatchManifestPath) == false) + if (_loadEditorManifestOp == null) { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Not found simulation manifest file : {_simulatePatchManifestPath}"; - return; + _loadEditorManifestOp = new LoadEditorManifestOperation(_simulateManifestPath); + OperationSystem.StartOperation(_loadEditorManifestOp); } - YooLogger.Log($"Load simulation manifest file : {_simulatePatchManifestPath}"); - byte[] bytesData = FileUtility.ReadAllBytes(_simulatePatchManifestPath); - _deserializer = new DeserializeManifestOperation(bytesData); - OperationSystem.StartOperation(_deserializer); - _steps = ESteps.CheckDeserializeManifest; - } - - if (_steps == ESteps.CheckDeserializeManifest) - { - if (_deserializer.IsDone == false) + if (_loadEditorManifestOp.IsDone == false) return; - if (_deserializer.Status == EOperationStatus.Succeed) + if (_loadEditorManifestOp.Status == EOperationStatus.Succeed) { - InitializedPackageVersion = _deserializer.Manifest.PackageVersion; - _impl.ActivePatchManifest = _deserializer.Manifest; + PackageVersion = _loadEditorManifestOp.Manifest.PackageVersion; + _impl.ActiveManifest = _loadEditorManifestOp.Manifest; _steps = ESteps.Done; Status = EOperationStatus.Succeed; } @@ -78,7 +63,7 @@ namespace YooAsset { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = _deserializer.Error; + Error = _loadEditorManifestOp.Error; } } } @@ -94,18 +79,17 @@ namespace YooAsset None, QueryBuildinPackageVersion, LoadBuildinManifest, - StartVerifyOperation, - CheckVerifyOperation, + VerifyPackage, Done, } private readonly OfflinePlayModeImpl _impl; private readonly string _packageName; - private QueryBuildinPackageVersionOperation _buildinPackageVersionQuery; - private LoadBuildinManifestOperation _buildinManifestLoad; - private VerifyCacheFilesOperation _verifyOperation; + private QueryBuildinPackageVersionOperation _queryBuildinPackageVersionOp; + private LoadBuildinManifestOperation _loadBuildinManifestOp; + private VerifyPackageOperation _verifyOperation; private ESteps _steps = ESteps.None; - + internal OfflinePlayModeInitializationOperation(OfflinePlayModeImpl impl, string packageName) { _impl = impl; @@ -122,16 +106,16 @@ namespace YooAsset if (_steps == ESteps.QueryBuildinPackageVersion) { - if (_buildinPackageVersionQuery == null) + if (_queryBuildinPackageVersionOp == null) { - _buildinPackageVersionQuery = new QueryBuildinPackageVersionOperation(_packageName); - OperationSystem.StartOperation(_buildinPackageVersionQuery); + _queryBuildinPackageVersionOp = new QueryBuildinPackageVersionOperation(_packageName); + OperationSystem.StartOperation(_queryBuildinPackageVersionOp); } - if (_buildinPackageVersionQuery.IsDone == false) + if (_queryBuildinPackageVersionOp.IsDone == false) return; - if (_buildinPackageVersionQuery.Status == EOperationStatus.Succeed) + if (_queryBuildinPackageVersionOp.Status == EOperationStatus.Succeed) { _steps = ESteps.LoadBuildinManifest; } @@ -139,45 +123,44 @@ namespace YooAsset { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = _buildinPackageVersionQuery.Error; + Error = _queryBuildinPackageVersionOp.Error; } } if (_steps == ESteps.LoadBuildinManifest) { - if (_buildinManifestLoad == null) + if (_loadBuildinManifestOp == null) { - _buildinManifestLoad = new LoadBuildinManifestOperation(_packageName, _buildinPackageVersionQuery.Version); - OperationSystem.StartOperation(_buildinManifestLoad); + _loadBuildinManifestOp = new LoadBuildinManifestOperation(_packageName, _queryBuildinPackageVersionOp.PackageVersion); + OperationSystem.StartOperation(_loadBuildinManifestOp); } - Progress = _buildinManifestLoad.Progress; - if (_buildinManifestLoad.IsDone == false) + Progress = _loadBuildinManifestOp.Progress; + if (_loadBuildinManifestOp.IsDone == false) return; - if (_buildinManifestLoad.Status == EOperationStatus.Succeed) + if (_loadBuildinManifestOp.Status == EOperationStatus.Succeed) { - InitializedPackageVersion = _buildinManifestLoad.Manifest.PackageVersion; - _impl.ActivePatchManifest = _buildinManifestLoad.Manifest; - _steps = ESteps.StartVerifyOperation; + PackageVersion = _loadBuildinManifestOp.Manifest.PackageVersion; + _impl.ActiveManifest = _loadBuildinManifestOp.Manifest; + _steps = ESteps.VerifyPackage; } else { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = _buildinManifestLoad.Error; + Error = _loadBuildinManifestOp.Error; } } - if (_steps == ESteps.StartVerifyOperation) + if (_steps == ESteps.VerifyPackage) { - _verifyOperation = VerifyCacheFilesOperation.CreateOperation(_impl.ActivePatchManifest, _impl); - OperationSystem.StartOperation(_verifyOperation); - _steps = ESteps.CheckVerifyOperation; - } + if (_verifyOperation == null) + { + _verifyOperation = VerifyPackageOperation.CreateOperation(_impl.ActiveManifest, _impl); + OperationSystem.StartOperation(_verifyOperation); + } - if (_steps == ESteps.CheckVerifyOperation) - { Progress = _verifyOperation.Progress; if (_verifyOperation.IsDone) { @@ -198,22 +181,25 @@ namespace YooAsset { None, CheckAppFootPrint, + QueryCachePackageVersion, TryLoadCacheManifest, QueryBuildinPackageVersion, + CopyBuildinPackageHash, CopyBuildinManifest, LoadBuildinManifest, - StartVerifyOperation, - CheckVerifyOperation, + VerifyPackage, Done, } private readonly HostPlayModeImpl _impl; private readonly string _packageName; - private QueryBuildinPackageVersionOperation _buildinPackageVersionQuery; - private CopyBuildinManifestOperation _buildinManifestCopy; - private LoadBuildinManifestOperation _buildinManifestLoad; - private LoadCacheManifestOperation _cacheManifestLoad; - private VerifyCacheFilesOperation _verifyOperation; + private QueryBuildinPackageVersionOperation _queryBuildinPackageVersionOp; + private QueryCachePackageVersionOperation _queryCachePackageVersionOp; + private CopyBuildinPackageHashOperation _copyBuildinPackageHashOp; + private CopyBuildinManifestOperation _copyBuildinManifestOp; + private LoadBuildinManifestOperation _loadBuildinManifestOp; + private LoadCacheManifestOperation _loadCacheManifestOp; + private VerifyPackageOperation _verifyOperation; private ESteps _steps = ESteps.None; internal HostPlayModeInitializationOperation(HostPlayModeImpl impl, string packageName) @@ -242,25 +228,46 @@ namespace YooAsset appFootPrint.Coverage(); YooLogger.Log("Delete manifest files when application foot print dirty !"); } - _steps = ESteps.TryLoadCacheManifest; + _steps = ESteps.QueryCachePackageVersion; + } + + if (_steps == ESteps.QueryCachePackageVersion) + { + if (_queryCachePackageVersionOp == null) + { + _queryCachePackageVersionOp = new QueryCachePackageVersionOperation(_packageName); + OperationSystem.StartOperation(_queryCachePackageVersionOp); + } + + if (_queryCachePackageVersionOp.IsDone == false) + return; + + if (_queryCachePackageVersionOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.TryLoadCacheManifest; + } + else + { + _steps = ESteps.QueryBuildinPackageVersion; + } } if (_steps == ESteps.TryLoadCacheManifest) { - if (_cacheManifestLoad == null) + if (_loadCacheManifestOp == null) { - _cacheManifestLoad = new LoadCacheManifestOperation(_packageName); - OperationSystem.StartOperation(_cacheManifestLoad); + _loadCacheManifestOp = new LoadCacheManifestOperation(_packageName, _queryCachePackageVersionOp.PackageVersion); + OperationSystem.StartOperation(_loadCacheManifestOp); } - if (_cacheManifestLoad.IsDone == false) + if (_loadCacheManifestOp.IsDone == false) return; - if (_cacheManifestLoad.Status == EOperationStatus.Succeed) + if (_loadCacheManifestOp.Status == EOperationStatus.Succeed) { - InitializedPackageVersion = _cacheManifestLoad.Manifest.PackageVersion; - _impl.ActivePatchManifest = _cacheManifestLoad.Manifest; - _steps = ESteps.StartVerifyOperation; + PackageVersion = _loadCacheManifestOp.Manifest.PackageVersion; + _impl.ActiveManifest = _loadCacheManifestOp.Manifest; + _steps = ESteps.VerifyPackage; } else { @@ -270,42 +277,64 @@ namespace YooAsset if (_steps == ESteps.QueryBuildinPackageVersion) { - if (_buildinPackageVersionQuery == null) + if (_queryBuildinPackageVersionOp == null) { - _buildinPackageVersionQuery = new QueryBuildinPackageVersionOperation(_packageName); - OperationSystem.StartOperation(_buildinPackageVersionQuery); + _queryBuildinPackageVersionOp = new QueryBuildinPackageVersionOperation(_packageName); + OperationSystem.StartOperation(_queryBuildinPackageVersionOp); } - if (_buildinPackageVersionQuery.IsDone == false) + if (_queryBuildinPackageVersionOp.IsDone == false) return; // 注意:为了兼容MOD模式,初始化动态新增的包裹的时候,如果内置清单不存在也不需要报错! - if (_buildinPackageVersionQuery.Status == EOperationStatus.Succeed) + if (_queryBuildinPackageVersionOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.CopyBuildinPackageHash; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + string error = _queryBuildinPackageVersionOp.Error; + YooLogger.Log($"Failed to load buildin package version file : {error}"); + } + } + + if (_steps == ESteps.CopyBuildinPackageHash) + { + if (_copyBuildinPackageHashOp == null) + { + _copyBuildinPackageHashOp = new CopyBuildinPackageHashOperation(_packageName, _queryBuildinPackageVersionOp.PackageVersion); + OperationSystem.StartOperation(_copyBuildinPackageHashOp); + } + + if (_copyBuildinPackageHashOp.IsDone == false) + return; + + if (_copyBuildinPackageHashOp.Status == EOperationStatus.Succeed) { _steps = ESteps.CopyBuildinManifest; } else { _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - string error = _buildinPackageVersionQuery.Error; - YooLogger.Log($"Failed to load buildin package version file : {error}"); + Status = EOperationStatus.Failed; + Error = _copyBuildinPackageHashOp.Error; } } if (_steps == ESteps.CopyBuildinManifest) { - if (_buildinManifestCopy == null) + if (_copyBuildinManifestOp == null) { - _buildinManifestCopy = new CopyBuildinManifestOperation(_packageName, _buildinPackageVersionQuery.Version); - OperationSystem.StartOperation(_buildinManifestCopy); + _copyBuildinManifestOp = new CopyBuildinManifestOperation(_packageName, _queryBuildinPackageVersionOp.PackageVersion); + OperationSystem.StartOperation(_copyBuildinManifestOp); } - Progress = _buildinManifestCopy.Progress; - if (_buildinManifestCopy.IsDone == false) + if (_copyBuildinManifestOp.IsDone == false) return; - if (_buildinManifestCopy.Status == EOperationStatus.Succeed) + if (_copyBuildinManifestOp.Status == EOperationStatus.Succeed) { _steps = ESteps.LoadBuildinManifest; } @@ -313,45 +342,44 @@ namespace YooAsset { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = _buildinManifestCopy.Error; + Error = _copyBuildinManifestOp.Error; } } if (_steps == ESteps.LoadBuildinManifest) { - if (_buildinManifestLoad == null) + if (_loadBuildinManifestOp == null) { - _buildinManifestLoad = new LoadBuildinManifestOperation(_packageName, _buildinPackageVersionQuery.Version); - OperationSystem.StartOperation(_buildinManifestLoad); + _loadBuildinManifestOp = new LoadBuildinManifestOperation(_packageName, _queryBuildinPackageVersionOp.PackageVersion); + OperationSystem.StartOperation(_loadBuildinManifestOp); } - Progress = _buildinManifestLoad.Progress; - if (_buildinManifestLoad.IsDone == false) + Progress = _loadBuildinManifestOp.Progress; + if (_loadBuildinManifestOp.IsDone == false) return; - if (_buildinManifestLoad.Status == EOperationStatus.Succeed) - { - InitializedPackageVersion = _buildinManifestLoad.Manifest.PackageVersion; - _impl.ActivePatchManifest = _buildinManifestLoad.Manifest; - _steps = ESteps.StartVerifyOperation; + if (_loadBuildinManifestOp.Status == EOperationStatus.Succeed) + { + PackageVersion = _loadBuildinManifestOp.Manifest.PackageVersion; + _impl.ActiveManifest = _loadBuildinManifestOp.Manifest; + _steps = ESteps.VerifyPackage; } else { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = _buildinManifestLoad.Error; + Error = _loadBuildinManifestOp.Error; } } - if (_steps == ESteps.StartVerifyOperation) + if (_steps == ESteps.VerifyPackage) { - _verifyOperation = VerifyCacheFilesOperation.CreateOperation(_impl.ActivePatchManifest, _impl); - OperationSystem.StartOperation(_verifyOperation); - _steps = ESteps.CheckVerifyOperation; - } + if (_verifyOperation == null) + { + _verifyOperation = VerifyPackageOperation.CreateOperation(_impl.ActiveManifest, _impl); + OperationSystem.StartOperation(_verifyOperation); + } - if (_steps == ESteps.CheckVerifyOperation) - { Progress = _verifyOperation.Progress; if (_verifyOperation.IsDone) { @@ -362,7 +390,6 @@ namespace YooAsset } } - /// /// 应用程序水印 /// diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinManifestOperation.cs index 68fa3d0..58a03e2 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinManifestOperation.cs @@ -1,16 +1,12 @@  namespace YooAsset { - /// - /// 内置补丁清单复制器 - /// internal class CopyBuildinManifestOperation : AsyncOperationBase { private enum ESteps { None, - CopyBuildinManifest, - CheckCopyBuildinManifest, + CopyBuildinManifestFile, Done, } @@ -19,7 +15,6 @@ namespace YooAsset private UnityWebFileRequester _downloader; private ESteps _steps = ESteps.None; - public CopyBuildinManifestOperation(string buildinPackageName, string buildinPackageVersion) { _buildinPackageName = buildinPackageName; @@ -27,26 +22,25 @@ namespace YooAsset } internal override void Start() { - _steps = ESteps.CopyBuildinManifest; + _steps = ESteps.CopyBuildinManifestFile; } internal override void Update() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.CopyBuildinManifest) + if (_steps == ESteps.CopyBuildinManifestFile) { - string savePath = PersistentHelper.GetCacheManifestFilePath(_buildinPackageName); - string fileName = YooAssetSettingsData.GetPatchManifestBinaryFileName(_buildinPackageName, _buildinPackageVersion); - string filePath = PathHelper.MakeStreamingLoadPath(fileName); - string url = PathHelper.ConvertToWWWPath(filePath); - _downloader = new UnityWebFileRequester(); - _downloader.SendRequest(url, savePath); - _steps = ESteps.CheckCopyBuildinManifest; - } + 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 (_steps == ESteps.CheckCopyBuildinManifest) - { if (_downloader.IsDone() == false) return; diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs new file mode 100644 index 0000000..0cf8244 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs @@ -0,0 +1,63 @@ + +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/CopyBuildinPackageHashOperation.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs.meta new file mode 100644 index 0000000..335bb87 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/CopyBuildinPackageHashOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bb073431e4f3b434e8431b3a8a808dfb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DeserializeManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DeserializeManifestOperation.cs index 2f621dc..8fe9464 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DeserializeManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DeserializeManifestOperation.cs @@ -16,14 +16,18 @@ namespace YooAsset DeserializeBundleList, Done, } - - public PatchManifest Manifest { private set; get; } + private readonly BufferReader _buffer; private int _patchAssetCount; private int _patchBundleCount; private int _progressTotalValue; private ESteps _steps = ESteps.None; + /// + /// 解析的清单实例 + /// + public PatchManifest Manifest { private set; get; } + public DeserializeManifestOperation(byte[] binaryData) { _buffer = new BufferReader(binaryData); diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DownloadManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DownloadManifestOperation.cs new file mode 100644 index 0000000..4e17ce2 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DownloadManifestOperation.cs @@ -0,0 +1,111 @@ + +namespace YooAsset +{ + internal class DownloadManifestOperation : AsyncOperationBase + { + private enum ESteps + { + None, + DownloadPackageHash, + DownloadManifest, + Done, + } + + private static int RequestCount = 0; + private readonly HostPlayModeImpl _impl; + private readonly string _packageName; + private readonly string _packageVersion; + private readonly int _timeout; + private UnityWebFileRequester _downloader1; + private UnityWebFileRequester _downloader2; + private ESteps _steps = ESteps.None; + + internal DownloadManifestOperation(HostPlayModeImpl impl, string packageName, string packageVersion, int timeout) + { + _impl = impl; + _packageName = packageName; + _packageVersion = packageVersion; + _timeout = timeout; + } + internal override void Start() + { + RequestCount++; + _steps = ESteps.DownloadPackageHash; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.DownloadPackageHash) + { + if (_downloader1 == null) + { + string savePath = PersistentHelper.GetCachePackageHashFilePath(_packageName, _packageVersion); + string fileName = YooAssetSettingsData.GetPackageHashFileName(_packageName, _packageVersion); + string webURL = GetDownloadRequestURL(fileName); + YooLogger.Log($"Beginning to download package hash file : {webURL}"); + _downloader1 = new UnityWebFileRequester(); + _downloader1.SendRequest(webURL, savePath, _timeout); + } + + _downloader1.CheckTimeout(); + if (_downloader1.IsDone() == false) + return; + + if (_downloader1.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader1.GetError(); + } + else + { + _steps = ESteps.DownloadManifest; + } + + _downloader1.Dispose(); + } + + if (_steps == ESteps.DownloadManifest) + { + if (_downloader2 == null) + { + string savePath = PersistentHelper.GetCacheManifestFilePath(_packageName, _packageVersion); + string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_packageName, _packageVersion); + string webURL = GetDownloadRequestURL(fileName); + YooLogger.Log($"Beginning to download manifest file : {webURL}"); + _downloader2 = new UnityWebFileRequester(); + _downloader2.SendRequest(webURL, savePath, _timeout); + } + + _downloader2.CheckTimeout(); + 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(); + } + } + + private string GetDownloadRequestURL(string fileName) + { + // 轮流返回请求地址 + if (RequestCount % 2 == 0) + return _impl.GetPatchDownloadFallbackURL(fileName); + else + return _impl.GetPatchDownloadMainURL(fileName); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DownloadManifestOperation.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DownloadManifestOperation.cs.meta new file mode 100644 index 0000000..adeccf8 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/DownloadManifestOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fccaa9437207a174d858ce44f14f5a03 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadBuildinManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadBuildinManifestOperation.cs index a878068..10e183d 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadBuildinManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadBuildinManifestOperation.cs @@ -1,16 +1,12 @@  namespace YooAsset { - /// - /// 内置补丁清单加载器 - /// internal class LoadBuildinManifestOperation : AsyncOperationBase { private enum ESteps { None, LoadBuildinManifest, - CheckLoadBuildinManifest, CheckDeserializeManifest, Done, } @@ -22,7 +18,7 @@ namespace YooAsset private ESteps _steps = ESteps.None; /// - /// 加载结果 + /// 加载的清单实例 /// public PatchManifest Manifest { private set; get; } @@ -43,16 +39,15 @@ namespace YooAsset if (_steps == ESteps.LoadBuildinManifest) { - string fileName = YooAssetSettingsData.GetPatchManifestBinaryFileName(_buildinPackageName, _buildinPackageVersion); - string filePath = PathHelper.MakeStreamingLoadPath(fileName); - string url = PathHelper.ConvertToWWWPath(filePath); - _downloader = new UnityWebDataRequester(); - _downloader.SendRequest(url); - _steps = ESteps.CheckLoadBuildinManifest; - } + if (_downloader == null) + { + string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_buildinPackageName, _buildinPackageVersion); + string filePath = PathHelper.MakeStreamingLoadPath(fileName); + string url = PathHelper.ConvertToWWWPath(filePath); + _downloader = new UnityWebDataRequester(); + _downloader.SendRequest(url); + } - if (_steps == ESteps.CheckLoadBuildinManifest) - { if (_downloader.IsDone() == false) return; diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadCacheManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadCacheManifestOperation.cs index 9120573..93f651f 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadCacheManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadCacheManifestOperation.cs @@ -2,54 +2,97 @@ namespace YooAsset { - /// - /// 沙盒补丁清单加载器 - /// internal class LoadCacheManifestOperation : AsyncOperationBase { private enum ESteps { None, - LoadCacheManifestFile, + QueryCachePackageHash, + VerifyFileHash, + LoadCacheManifest, CheckDeserializeManifest, Done, } private readonly string _packageName; + private readonly string _packageVersion; + private QueryCachePackageHashOperation _queryCachePackageHashOp; private DeserializeManifestOperation _deserializer; private string _manifestFilePath; private ESteps _steps = ESteps.None; /// - /// 加载结果 + /// 加载的清单实例 /// public PatchManifest Manifest { private set; get; } - public LoadCacheManifestOperation(string packageName) + public LoadCacheManifestOperation(string packageName, string packageVersion) { _packageName = packageName; + _packageVersion = packageVersion; } internal override void Start() { - _steps = ESteps.LoadCacheManifestFile; + _steps = ESteps.QueryCachePackageHash; } internal override void Update() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.LoadCacheManifestFile) + if (_steps == ESteps.QueryCachePackageHash) { - _manifestFilePath = PersistentHelper.GetCacheManifestFilePath(_packageName); + if (_queryCachePackageHashOp == null) + { + _queryCachePackageHashOp = new QueryCachePackageHashOperation(_packageName, _packageVersion); + OperationSystem.StartOperation(_queryCachePackageHashOp); + } + + if (_queryCachePackageHashOp.IsDone == false) + return; + + if (_queryCachePackageHashOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.VerifyFileHash; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _queryCachePackageHashOp.Error; + ClearCacheFile(); + } + } + + if (_steps == ESteps.VerifyFileHash) + { + _manifestFilePath = PersistentHelper.GetCacheManifestFilePath(_packageName, _packageVersion); if (File.Exists(_manifestFilePath) == false) { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = $"Manifest file not found : {_manifestFilePath}"; + Error = $"Not found cache manifest file : {_manifestFilePath}"; + ClearCacheFile(); return; } + string fileHash = HashUtility.FileMD5(_manifestFilePath); + if (fileHash != _queryCachePackageHashOp.PackageHash) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Failed to verify cache manifest file hash !"; + ClearCacheFile(); + } + else + { + _steps = ESteps.LoadCacheManifest; + } + } + + if (_steps == ESteps.LoadCacheManifest) + { byte[] bytesData = File.ReadAllBytes(_manifestFilePath); _deserializer = new DeserializeManifestOperation(bytesData); OperationSystem.StartOperation(_deserializer); @@ -65,7 +108,7 @@ namespace YooAsset if (_deserializer.Status == EOperationStatus.Succeed) { Manifest = _deserializer.Manifest; - _steps = ESteps.Done; + _steps = ESteps.Done; Status = EOperationStatus.Succeed; } else @@ -73,16 +116,26 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = _deserializer.Error; - - // 注意:如果加载沙盒内的清单报错,为了避免流程被卡住,我们主动把损坏的文件删除。 - if (File.Exists(_manifestFilePath)) - { - YooLogger.Warning($"Failed to load cache manifest file : {Error}"); - YooLogger.Warning($"Invalid cache manifest file have been removed : {_manifestFilePath}"); - File.Delete(_manifestFilePath); - } + ClearCacheFile(); } } } + + private void ClearCacheFile() + { + // 注意:如果加载沙盒内的清单报错,为了避免流程被卡住,主动把损坏的文件删除。 + if (File.Exists(_manifestFilePath)) + { + YooLogger.Warning($"Failed to load cache manifest file : {Error}"); + YooLogger.Warning($"Invalid cache manifest file have been removed : {_manifestFilePath}"); + File.Delete(_manifestFilePath); + } + + string hashFilePath = PersistentHelper.GetCachePackageHashFilePath(_packageName, _packageVersion); + if (File.Exists(hashFilePath)) + { + File.Delete(hashFilePath); + } + } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadEditorManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadEditorManifestOperation.cs new file mode 100644 index 0000000..1fd210d --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadEditorManifestOperation.cs @@ -0,0 +1,76 @@ +using System.IO; + +namespace YooAsset +{ + internal class LoadEditorManifestOperation : AsyncOperationBase + { + private enum ESteps + { + None, + LoadEditorManifest, + CheckDeserializeManifest, + Done, + } + + private readonly string _manifestFilePath; + private DeserializeManifestOperation _deserializer; + private ESteps _steps = ESteps.None; + + /// + /// 加载的清单实例 + /// + public PatchManifest Manifest { private set; get; } + + + public LoadEditorManifestOperation(string manifestFilePath) + { + _manifestFilePath = manifestFilePath; + } + internal override void Start() + { + _steps = ESteps.LoadEditorManifest; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.LoadEditorManifest) + { + if (File.Exists(_manifestFilePath) == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Not found simulation manifest file : {_manifestFilePath}"; + return; + } + + YooLogger.Log($"Load editor manifest file : {_manifestFilePath}"); + byte[] bytesData = FileUtility.ReadAllBytes(_manifestFilePath); + _deserializer = new DeserializeManifestOperation(bytesData); + OperationSystem.StartOperation(_deserializer); + _steps = ESteps.CheckDeserializeManifest; + } + + if (_steps == ESteps.CheckDeserializeManifest) + { + Progress = _deserializer.Progress; + if (_deserializer.IsDone == false) + return; + + if (_deserializer.Status == EOperationStatus.Succeed) + { + Manifest = _deserializer.Manifest; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _deserializer.Error; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadEditorManifestOperation.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadEditorManifestOperation.cs.meta new file mode 100644 index 0000000..a306e38 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/LoadEditorManifestOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 246df4d20b431c648a0821231a805e6b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryBuildinPackageVersionOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryBuildinPackageVersionOperation.cs index a8ac6dd..046793e 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryBuildinPackageVersionOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryBuildinPackageVersionOperation.cs @@ -1,16 +1,12 @@  namespace YooAsset { - /// - /// 内置补丁清单版本查询器 - /// internal class QueryBuildinPackageVersionOperation : AsyncOperationBase { private enum ESteps { None, - LoadPackageVersion, - CheckLoadPackageVersion, + LoadBuildinPackageVersionFile, Done, } @@ -19,9 +15,9 @@ namespace YooAsset private ESteps _steps = ESteps.None; /// - /// 内置包裹版本 + /// 包裹版本 /// - public string Version { private set; get; } + public string PackageVersion { private set; get; } public QueryBuildinPackageVersionOperation(string packageName) @@ -30,25 +26,24 @@ namespace YooAsset } internal override void Start() { - _steps = ESteps.LoadPackageVersion; + _steps = ESteps.LoadBuildinPackageVersionFile; } internal override void Update() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.LoadPackageVersion) + if (_steps == ESteps.LoadBuildinPackageVersionFile) { - string fileName = YooAssetSettingsData.GetPatchManifestVersionFileName(_packageName); - string filePath = PathHelper.MakeStreamingLoadPath(fileName); - string url = PathHelper.ConvertToWWWPath(filePath); - _downloader = new UnityWebDataRequester(); - _downloader.SendRequest(url); - _steps = ESteps.CheckLoadPackageVersion; - } + if (_downloader == null) + { + string fileName = YooAssetSettingsData.GetPackageVersionFileName(_packageName); + string filePath = PathHelper.MakeStreamingLoadPath(fileName); + string url = PathHelper.ConvertToWWWPath(filePath); + _downloader = new UnityWebDataRequester(); + _downloader.SendRequest(url); + } - if (_steps == ESteps.CheckLoadPackageVersion) - { if (_downloader.IsDone() == false) return; @@ -60,8 +55,8 @@ namespace YooAsset } else { - Version = _downloader.GetText(); - if (string.IsNullOrEmpty(Version)) + PackageVersion = _downloader.GetText(); + if (string.IsNullOrEmpty(PackageVersion)) { _steps = ESteps.Done; Status = EOperationStatus.Failed; diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageHashOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageHashOperation.cs new file mode 100644 index 0000000..80caaab --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageHashOperation.cs @@ -0,0 +1,64 @@ +using System.IO; + +namespace YooAsset +{ + internal class QueryCachePackageHashOperation : AsyncOperationBase + { + private enum ESteps + { + None, + LoadCachePackageHashFile, + Done, + } + + private readonly string _packageName; + private readonly string _packageVersion; + private ESteps _steps = ESteps.None; + + /// + /// 包裹哈希值 + /// + public string PackageHash { private set; get; } + + + public QueryCachePackageHashOperation(string packageName, string packageVersion) + { + _packageName = packageName; + _packageVersion = packageVersion; + } + internal override void Start() + { + _steps = ESteps.LoadCachePackageHashFile; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.LoadCachePackageHashFile) + { + string filePath = PersistentHelper.GetCachePackageHashFilePath(_packageName, _packageVersion); + if (File.Exists(filePath) == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Cache package hash file not found : {filePath}"; + return; + } + + PackageHash = FileUtility.ReadAllText(filePath); + if (string.IsNullOrEmpty(PackageHash)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Cache package hash file content is empty !"; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageHashOperation.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageHashOperation.cs.meta new file mode 100644 index 0000000..14ff2ea --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageHashOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 60db6a6586340664ab7e9f85cec0eef4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageVersionOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageVersionOperation.cs new file mode 100644 index 0000000..c56a767 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageVersionOperation.cs @@ -0,0 +1,62 @@ +using System.IO; + +namespace YooAsset +{ + internal class QueryCachePackageVersionOperation : AsyncOperationBase + { + private enum ESteps + { + None, + LoadCachePackageVersionFile, + Done, + } + + private readonly string _packageName; + private ESteps _steps = ESteps.None; + + /// + /// 包裹版本 + /// + public string PackageVersion { private set; get; } + + + public QueryCachePackageVersionOperation(string packageName) + { + _packageName = packageName; + } + internal override void Start() + { + _steps = ESteps.LoadCachePackageVersionFile; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.LoadCachePackageVersionFile) + { + string filePath = PersistentHelper.GetCachePackageVersionFilePath(_packageName); + if (File.Exists(filePath) == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Cache package version file not found : {filePath}"; + return; + } + + PackageVersion = FileUtility.ReadAllText(filePath); + if (string.IsNullOrEmpty(PackageVersion)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Cache package version file content is empty !"; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageVersionOperation.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageVersionOperation.cs.meta new file mode 100644 index 0000000..0465bbc --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/QueryCachePackageVersionOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 29a2cbdd051ba1247a24693d56cdc2c3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/VerifyCacheFilesOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/VerifyPackageOperation.cs similarity index 85% rename from Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/VerifyCacheFilesOperation.cs rename to Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/VerifyPackageOperation.cs index a3769a9..6600ffa 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/VerifyCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/VerifyPackageOperation.cs @@ -6,20 +6,17 @@ using System.Threading; namespace YooAsset { - /// - /// 本地缓存文件验证 - /// - internal abstract class VerifyCacheFilesOperation : AsyncOperationBase + internal abstract class VerifyPackageOperation : AsyncOperationBase { public List VerifySuccessList { protected set; get; } public List VerifyFailList { protected set; get; } - public static VerifyCacheFilesOperation CreateOperation(PatchManifest patchManifest, IPlayModeServices playModeServices) + public static VerifyPackageOperation CreateOperation(PatchManifest manifest, IPlayModeServices playModeServices) { #if UNITY_WEBGL - VerifyCacheFilesOperation operation = new VerifyCacheFilesWithoutThreadOperation(patchManifest, playModeServices); + VerifyPackageOperation operation = new VerifyPackageWithoutThreadOperation(manifest, playModeServices); #else - VerifyCacheFilesOperation operation = new VerifyCacheFilesWithThreadOperation(patchManifest, playModeServices); + VerifyPackageOperation operation = new VerifyPackageWithThreadOperation(manifest, playModeServices); #endif return operation; } @@ -28,7 +25,7 @@ namespace YooAsset /// /// 本地缓存文件验证(线程版) /// - internal class VerifyCacheFilesWithThreadOperation : VerifyCacheFilesOperation + internal class VerifyPackageWithThreadOperation : VerifyPackageOperation { private enum ESteps { @@ -39,7 +36,7 @@ namespace YooAsset Done, } - private readonly PatchManifest _patchManifest; + private readonly PatchManifest _manifest; private readonly IPlayModeServices _playModeServices; private readonly ThreadSyncContext _syncContext = new ThreadSyncContext(); private List _waitingList; @@ -49,9 +46,9 @@ namespace YooAsset private float _verifyStartTime; private ESteps _steps = ESteps.None; - public VerifyCacheFilesWithThreadOperation(PatchManifest patchManifest, IPlayModeServices playModeServices) + public VerifyPackageWithThreadOperation(PatchManifest manifest, IPlayModeServices playModeServices) { - _patchManifest = patchManifest; + _manifest = manifest; _playModeServices = playModeServices; } internal override void Start() @@ -66,7 +63,7 @@ namespace YooAsset if (_steps == ESteps.InitVerify) { - int bundleCount = _patchManifest.BundleList.Count; + int bundleCount = _manifest.BundleList.Count; VerifySuccessList = new List(bundleCount); VerifyFailList = new List(bundleCount); @@ -85,7 +82,7 @@ namespace YooAsset if (_steps == ESteps.PrepareVerify) { - foreach (var patchBundle in _patchManifest.BundleList) + foreach (var patchBundle in _manifest.BundleList) { if (CacheSystem.IsCached(patchBundle)) continue; @@ -175,7 +172,7 @@ namespace YooAsset /// /// 本地缓存文件验证(非线程版) /// - internal class VerifyCacheFilesWithoutThreadOperation : VerifyCacheFilesOperation + internal class VerifyPackageWithoutThreadOperation : VerifyPackageOperation { private enum ESteps { @@ -186,7 +183,7 @@ namespace YooAsset Done, } - private readonly PatchManifest _patchManifest; + private readonly PatchManifest _manifest; private readonly IPlayModeServices _playModeServices; private List _waitingList; private List _verifyingList; @@ -195,9 +192,9 @@ namespace YooAsset private float _verifyStartTime; private ESteps _steps = ESteps.None; - public VerifyCacheFilesWithoutThreadOperation(PatchManifest patchManifest, IPlayModeServices playModeServices) + public VerifyPackageWithoutThreadOperation(PatchManifest manifest, IPlayModeServices playModeServices) { - _patchManifest = patchManifest; + _manifest = manifest; _playModeServices = playModeServices; } internal override void Start() @@ -212,7 +209,7 @@ namespace YooAsset if (_steps == ESteps.InitVerify) { - int bundleCount = _patchManifest.BundleList.Count; + int bundleCount = _manifest.BundleList.Count; VerifySuccessList = new List(bundleCount); VerifyFailList = new List(bundleCount); @@ -227,7 +224,7 @@ namespace YooAsset if (_steps == ESteps.PrepareVerify) { - foreach (var patchBundle in _patchManifest.BundleList) + foreach (var patchBundle in _manifest.BundleList) { if (CacheSystem.IsCached(patchBundle)) continue; diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/VerifyCacheFilesOperation.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/VerifyPackageOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/VerifyCacheFilesOperation.cs.meta rename to Assets/YooAsset/Runtime/PatchSystem/Operations/Internal/VerifyPackageOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs index 94fb64f..33246d8 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs @@ -10,20 +10,6 @@ namespace YooAsset /// public abstract class UpdatePackageManifestOperation : AsyncOperationBase { - /// - /// 发现了新的清单 - /// - public bool FoundNewManifest { protected set; get; } = false; - - /// - /// 手动保存清单文件 - /// - public virtual void SaveManifestFile() { } - - /// - /// 还原补丁清单 - /// - public virtual void RevertManifest() { } } /// @@ -69,177 +55,134 @@ namespace YooAsset private enum ESteps { None, - TryLoadCacheHash, - DownloadWebHash, - CheckDownloadWebHash, - DownloadWebManifest, - CheckDownloadWebManifest, - CheckDeserializeWebManifest, - StartVerifyOperation, - CheckVerifyOperation, + CheckActiveManifest, + TryLoadCacheManifest, + DownloadManifest, + LoadCacheManifest, + CheckDeserializeManifest, + VerifyPackage, Done, } - private static int RequestCount = 0; private readonly HostPlayModeImpl _impl; private readonly string _packageName; private readonly string _packageVersion; - private readonly bool _autoSaveManifestFile; private readonly int _timeout; - private UnityWebDataRequester _downloader1; - private UnityWebDataRequester _downloader2; - private DeserializeManifestOperation _deserializer; - private VerifyCacheFilesOperation _verifyOperation; - - internal PatchManifest _prePatchManifest; - private string _cacheManifestHash; - private byte[] _fileBytesData = null; + private LoadCacheManifestOperation _tryLoadCacheManifestOp; + private LoadCacheManifestOperation _loadCacheManifestOp; + private DownloadManifestOperation _downloadManifestOp; + private VerifyPackageOperation _verifyOperation; private ESteps _steps = ESteps.None; - internal HostPlayModeUpdatePackageManifestOperation(HostPlayModeImpl impl, string packageName, string packageVersion, bool autoSaveManifestFile, int timeout) + + internal HostPlayModeUpdatePackageManifestOperation(HostPlayModeImpl impl, string packageName, string packageVersion, int timeout) { _impl = impl; _packageName = packageName; _packageVersion = packageVersion; - _autoSaveManifestFile = autoSaveManifestFile; _timeout = timeout; } internal override void Start() { - RequestCount++; - _steps = ESteps.TryLoadCacheHash; + _steps = ESteps.CheckActiveManifest; } internal override void Update() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.TryLoadCacheHash) + if (_steps == ESteps.CheckActiveManifest) { - string filePath = PersistentHelper.GetCacheManifestFilePath(_packageName); - if (File.Exists(filePath)) + // 检测当前激活的清单对象 + if (_impl.ActiveManifest != null) { - _cacheManifestHash = HashUtility.FileMD5(filePath); - _steps = ESteps.DownloadWebHash; - } - else - { - _steps = ESteps.DownloadWebManifest; - } - } - - if (_steps == ESteps.DownloadWebHash) - { - string fileName = YooAssetSettingsData.GetPatchManifestHashFileName(_packageName, _packageVersion); - string webURL = GetPatchManifestRequestURL(fileName); - YooLogger.Log($"Beginning to request patch manifest hash : {webURL}"); - _downloader1 = new UnityWebDataRequester(); - _downloader1.SendRequest(webURL, _timeout); - _steps = ESteps.CheckDownloadWebHash; - } - - if (_steps == ESteps.CheckDownloadWebHash) - { - if (_downloader1.IsDone() == false) - return; - - if (_downloader1.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader1.GetError(); - } - else - { - string webManifestHash = _downloader1.GetText(); - if (_cacheManifestHash == webManifestHash) + if (_impl.ActiveManifest.PackageVersion == _packageVersion) { - YooLogger.Log($"Not found new package : {_packageName}"); - FoundNewManifest = false; _steps = ESteps.Done; Status = EOperationStatus.Succeed; - } - else - { - YooLogger.Log($"Package {_packageName} is change : {_cacheManifestHash} -> {webManifestHash}"); - FoundNewManifest = true; - _steps = ESteps.DownloadWebManifest; + return; } } - _downloader1.Dispose(); + _steps = ESteps.TryLoadCacheManifest; } - if (_steps == ESteps.DownloadWebManifest) + if (_steps == ESteps.TryLoadCacheManifest) { - string fileName = YooAssetSettingsData.GetPatchManifestBinaryFileName(_packageName, _packageVersion); - string webURL = GetPatchManifestRequestURL(fileName); - YooLogger.Log($"Beginning to request patch manifest : {webURL}"); - _downloader2 = new UnityWebDataRequester(); - _downloader2.SendRequest(webURL, _timeout); - _steps = ESteps.CheckDownloadWebManifest; - } + if (_tryLoadCacheManifestOp == null) + { + _tryLoadCacheManifestOp = new LoadCacheManifestOperation(_packageName, _packageVersion); + OperationSystem.StartOperation(_tryLoadCacheManifestOp); + } - if (_steps == ESteps.CheckDownloadWebManifest) - { - if (_downloader2.IsDone() == false) + if (_tryLoadCacheManifestOp.IsDone == false) return; - if (_downloader2.HasError()) + if (_tryLoadCacheManifestOp.Status == EOperationStatus.Succeed) { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader2.GetError(); + _impl.ActiveManifest = _tryLoadCacheManifestOp.Manifest; + _steps = ESteps.VerifyPackage; } else { - byte[] bytesData = _downloader2.GetData(); - if (_autoSaveManifestFile) - { - SaveManifestFileInternal(bytesData); - } - else - { - _fileBytesData = bytesData; - } - - // 解析二进制数据 - _deserializer = new DeserializeManifestOperation(bytesData); - OperationSystem.StartOperation(_deserializer); - _steps = ESteps.CheckDeserializeWebManifest; + _steps = ESteps.DownloadManifest; } - - _downloader2.Dispose(); } - if (_steps == ESteps.CheckDeserializeWebManifest) + if (_steps == ESteps.DownloadManifest) { - Progress = _deserializer.Progress; - if (_deserializer.IsDone == false) + if (_downloadManifestOp == null) + { + _downloadManifestOp = new DownloadManifestOperation(_impl, _packageName, _packageVersion, _timeout); + OperationSystem.StartOperation(_downloadManifestOp); + } + + if (_downloadManifestOp.IsDone == false) return; - if (_deserializer.Status == EOperationStatus.Succeed) + if (_downloadManifestOp.Status == EOperationStatus.Succeed) { - _prePatchManifest = _impl.ActivePatchManifest; - _impl.ActivePatchManifest = _deserializer.Manifest; - _steps = ESteps.StartVerifyOperation; + _steps = ESteps.LoadCacheManifest; } else { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = _deserializer.Error; + Error = _downloadManifestOp.Error; } } - if (_steps == ESteps.StartVerifyOperation) + if (_steps == ESteps.LoadCacheManifest) { - _verifyOperation = VerifyCacheFilesOperation.CreateOperation(_deserializer.Manifest, _impl); - OperationSystem.StartOperation(_verifyOperation); - _steps = ESteps.CheckVerifyOperation; + if (_loadCacheManifestOp == null) + { + _loadCacheManifestOp = new LoadCacheManifestOperation(_packageName, _packageVersion); + OperationSystem.StartOperation(_loadCacheManifestOp); + } + + if (_loadCacheManifestOp.IsDone == false) + return; + + if (_loadCacheManifestOp.Status == EOperationStatus.Succeed) + { + _impl.ActiveManifest = _loadCacheManifestOp.Manifest; + _steps = ESteps.VerifyPackage; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _loadCacheManifestOp.Error; + } } - if (_steps == ESteps.CheckVerifyOperation) + if (_steps == ESteps.VerifyPackage) { + if (_verifyOperation == null) + { + _verifyOperation = VerifyPackageOperation.CreateOperation(_impl.ActiveManifest, _impl); + OperationSystem.StartOperation(_verifyOperation); + } + Progress = _verifyOperation.Progress; if (_verifyOperation.IsDone) { @@ -248,61 +191,5 @@ namespace YooAsset } } } - - /// - /// 手动保存清单文件 - /// - public override void SaveManifestFile() - { - if (IsDone == false) - { - YooLogger.Warning($"{nameof(UpdatePackageManifestOperation)} is not done !"); - return; - } - - if (Status == EOperationStatus.Succeed) - { - if (_fileBytesData != null) - { - SaveManifestFileInternal(_fileBytesData); - _fileBytesData = null; - } - } - } - - /// - /// 还原补丁清单 - /// - public override void RevertManifest() - { - if (IsDone == false) - { - YooLogger.Warning($"{nameof(UpdatePackageManifestOperation)} is not done !"); - return; - } - - if (Status == EOperationStatus.Succeed) - { - if (_prePatchManifest != null) - { - _impl.ActivePatchManifest = _prePatchManifest; - _prePatchManifest = null; - } - } - } - - private void SaveManifestFileInternal(byte[] bytesData) - { - string savePath = PersistentHelper.GetCacheManifestFilePath(_packageName); - FileUtility.CreateFile(savePath, bytesData); - } - private string GetPatchManifestRequestURL(string fileName) - { - // 轮流返回请求地址 - if (RequestCount % 2 == 0) - return _impl.GetPatchDownloadFallbackURL(fileName); - else - return _impl.GetPatchDownloadMainURL(fileName); - } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageVersionOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageVersionOperation.cs index 9ca4cf7..2f5886a 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageVersionOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageVersionOperation.cs @@ -51,8 +51,7 @@ namespace YooAsset private enum ESteps { None, - LoadPackageVersion, - CheckLoadPackageVersion, + DownloadPackageVersion, Done, } @@ -74,25 +73,24 @@ namespace YooAsset internal override void Start() { RequestCount++; - _steps = ESteps.LoadPackageVersion; + _steps = ESteps.DownloadPackageVersion; } internal override void Update() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.LoadPackageVersion) + if (_steps == ESteps.DownloadPackageVersion) { - string fileName = YooAssetSettingsData.GetPatchManifestVersionFileName(_packageName); - string webURL = GetPackageVersionRequestURL(fileName); - YooLogger.Log($"Beginning to request package version : {webURL}"); - _downloader = new UnityWebDataRequester(); - _downloader.SendRequest(webURL, _timeout); - _steps = ESteps.CheckLoadPackageVersion; - } + 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); + } - if (_steps == ESteps.CheckLoadPackageVersion) - { Progress = _downloader.Progress(); if (_downloader.IsDone() == false) return; diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs index 1332285..63286a7 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs @@ -6,7 +6,7 @@ namespace YooAsset { internal class EditorSimulateModeImpl : IPlayModeServices, IBundleServices { - private PatchManifest _activePatchManifest; + private PatchManifest _activeManifest; private string _packageName; private bool _locationToLower; @@ -23,24 +23,18 @@ namespace YooAsset } #region IPlayModeServices接口 - public PatchManifest ActivePatchManifest + public PatchManifest ActiveManifest { set { - _activePatchManifest = value; - _activePatchManifest.InitAssetPathMapping(_locationToLower); + _activeManifest = value; + _activeManifest.InitAssetPathMapping(_locationToLower); } get { - return _activePatchManifest; + return _activeManifest; } } - public string GetPackageVersion() - { - if (_activePatchManifest == null) - return string.Empty; - return _activePatchManifest.PackageVersion; - } public bool IsBuildinPatchBundle(PatchBundle patchBundle) { return true; @@ -52,7 +46,7 @@ namespace YooAsset OperationSystem.StartOperation(operation); return operation; } - UpdatePackageManifestOperation IPlayModeServices.UpdatePackageManifestAsync(string packageVersion, bool autoSaveManifestFile, int timeout) + UpdatePackageManifestOperation IPlayModeServices.UpdatePackageManifestAsync(string packageVersion, int timeout) { var operation = new EditorPlayModeUpdatePackageManifestOperation(); OperationSystem.StartOperation(operation); @@ -95,7 +89,7 @@ namespace YooAsset throw new Exception("Should never get here !"); // 注意:如果补丁清单里未找到资源包会抛出异常! - var patchBundle = _activePatchManifest.GetMainPatchBundle(assetInfo.AssetPath); + var patchBundle = _activeManifest.GetMainPatchBundle(assetInfo.AssetPath); BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromEditor, assetInfo.AssetPath); return bundleInfo; } @@ -105,22 +99,22 @@ namespace YooAsset } AssetInfo[] IBundleServices.GetAssetInfos(string[] tags) { - return _activePatchManifest.GetAssetsInfoByTags(tags); + return _activeManifest.GetAssetsInfoByTags(tags); } PatchAsset IBundleServices.TryGetPatchAsset(string assetPath) { - if (_activePatchManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset)) + if (_activeManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset)) return patchAsset; else return null; } string IBundleServices.MappingToAssetPath(string location) { - return _activePatchManifest.MappingToAssetPath(location); + return _activeManifest.MappingToAssetPath(location); } string IBundleServices.TryMappingToAssetPath(string location) { - return _activePatchManifest.TryMappingToAssetPath(location); + return _activeManifest.TryMappingToAssetPath(location); } string IBundleServices.GetPackageName() { @@ -128,11 +122,11 @@ namespace YooAsset } bool IBundleServices.IsIncludeBundleFile(string fileName) { - return _activePatchManifest.IsIncludeBundleFile(fileName); + return _activeManifest.IsIncludeBundleFile(fileName); } bool IBundleServices.IsServicesValid() { - return _activePatchManifest != null; + return _activeManifest != null; } #endregion } diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs index 3a229a9..c6061c0 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs @@ -6,7 +6,7 @@ namespace YooAsset { internal class HostPlayModeImpl : IPlayModeServices, IBundleServices { - private PatchManifest _activePatchManifest; + private PatchManifest _activeManifest; // 参数相关 private string _packageName; @@ -80,24 +80,19 @@ namespace YooAsset } #region IPlayModeServices接口 - public PatchManifest ActivePatchManifest + public PatchManifest ActiveManifest { set { - _activePatchManifest = value; - _activePatchManifest.InitAssetPathMapping(_locationToLower); + _activeManifest = value; + _activeManifest.InitAssetPathMapping(_locationToLower); + PersistentHelper.SaveCachePackageVersionFile(_packageName, _activeManifest.PackageVersion); } get { - return _activePatchManifest; + return _activeManifest; } } - public string GetPackageVersion() - { - if (_activePatchManifest == null) - return string.Empty; - return _activePatchManifest.PackageVersion; - } public bool IsBuildinPatchBundle(PatchBundle patchBundle) { return _queryServices.QueryStreamingAssets(patchBundle.FileName); @@ -109,9 +104,9 @@ namespace YooAsset OperationSystem.StartOperation(operation); return operation; } - UpdatePackageManifestOperation IPlayModeServices.UpdatePackageManifestAsync(string packageVersion, bool autoSaveManifestFile, int timeout) + UpdatePackageManifestOperation IPlayModeServices.UpdatePackageManifestAsync(string packageVersion, int timeout) { - var operation = new HostPlayModeUpdatePackageManifestOperation(this, _packageName, packageVersion, autoSaveManifestFile, timeout); + var operation = new HostPlayModeUpdatePackageManifestOperation(this, _packageName, packageVersion, timeout); OperationSystem.StartOperation(operation); return operation; } @@ -124,11 +119,11 @@ namespace YooAsset PatchDownloaderOperation IPlayModeServices.CreatePatchDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout) { - List downloadList = GetDownloadListByAll(_activePatchManifest); + List downloadList = GetDownloadListByAll(_activeManifest); var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout); return operation; } - private List GetDownloadListByAll(PatchManifest patchManifest) + public List GetDownloadListByAll(PatchManifest patchManifest) { List downloadList = new List(1000); foreach (var patchBundle in patchManifest.BundleList) @@ -149,11 +144,11 @@ namespace YooAsset PatchDownloaderOperation IPlayModeServices.CreatePatchDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout) { - List downloadList = GetDownloadListByTags(_activePatchManifest, tags); + List downloadList = GetDownloadListByTags(_activeManifest, tags); var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout); return operation; } - private List GetDownloadListByTags(PatchManifest patchManifest, string[] tags) + public List GetDownloadListByTags(PatchManifest patchManifest, string[] tags) { List downloadList = new List(1000); foreach (var patchBundle in patchManifest.BundleList) @@ -186,7 +181,7 @@ namespace YooAsset PatchDownloaderOperation IPlayModeServices.CreatePatchDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout) { - List downloadList = GetDownloadListByPaths(_activePatchManifest, assetInfos); + List downloadList = GetDownloadListByPaths(_activeManifest, assetInfos); var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout); return operation; } @@ -235,7 +230,7 @@ namespace YooAsset PatchUnpackerOperation IPlayModeServices.CreatePatchUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout) { - List unpcakList = GetUnpackListByAll(_activePatchManifest); + List unpcakList = GetUnpackListByAll(_activeManifest); var operation = new PatchUnpackerOperation(unpcakList, upackingMaxNumber, failedTryAgain, timeout); return operation; } @@ -259,7 +254,7 @@ namespace YooAsset PatchUnpackerOperation IPlayModeServices.CreatePatchUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout) { - List unpcakList = GetUnpackListByTags(_activePatchManifest, tags); + List unpcakList = GetUnpackListByTags(_activeManifest, tags); var operation = new PatchUnpackerOperation(unpcakList, upackingMaxNumber, failedTryAgain, timeout); return operation; } @@ -315,7 +310,7 @@ namespace YooAsset throw new Exception("Should never get here !"); // 注意:如果补丁清单里未找到资源包会抛出异常! - var patchBundle = _activePatchManifest.GetMainPatchBundle(assetInfo.AssetPath); + var patchBundle = _activeManifest.GetMainPatchBundle(assetInfo.AssetPath); return CreateBundleInfo(patchBundle); } BundleInfo[] IBundleServices.GetAllDependBundleInfos(AssetInfo assetInfo) @@ -324,7 +319,7 @@ namespace YooAsset throw new Exception("Should never get here !"); // 注意:如果补丁清单里未找到资源包会抛出异常! - var depends = _activePatchManifest.GetAllDependencies(assetInfo.AssetPath); + var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); List result = new List(depends.Length); foreach (var patchBundle in depends) { @@ -335,22 +330,22 @@ namespace YooAsset } AssetInfo[] IBundleServices.GetAssetInfos(string[] tags) { - return _activePatchManifest.GetAssetsInfoByTags(tags); + return _activeManifest.GetAssetsInfoByTags(tags); } PatchAsset IBundleServices.TryGetPatchAsset(string assetPath) { - if (_activePatchManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset)) + if (_activeManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset)) return patchAsset; else return null; } string IBundleServices.MappingToAssetPath(string location) { - return _activePatchManifest.MappingToAssetPath(location); + return _activeManifest.MappingToAssetPath(location); } string IBundleServices.TryMappingToAssetPath(string location) { - return _activePatchManifest.TryMappingToAssetPath(location); + return _activeManifest.TryMappingToAssetPath(location); } string IBundleServices.GetPackageName() { @@ -358,11 +353,11 @@ namespace YooAsset } bool IBundleServices.IsIncludeBundleFile(string fileName) { - return _activePatchManifest.IsIncludeBundleFile(fileName); + return _activeManifest.IsIncludeBundleFile(fileName); } bool IBundleServices.IsServicesValid() { - return _activePatchManifest != null; + return _activeManifest != null; } #endregion } diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs index e959477..76d72d2 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs @@ -6,7 +6,7 @@ namespace YooAsset { internal class OfflinePlayModeImpl : IPlayModeServices, IBundleServices { - private PatchManifest _activePatchManifest; + private PatchManifest _activeManifest; private string _packageName; private bool _locationToLower; @@ -23,24 +23,18 @@ namespace YooAsset } #region IPlayModeServices接口 - public PatchManifest ActivePatchManifest + public PatchManifest ActiveManifest { set { - _activePatchManifest = value; - _activePatchManifest.InitAssetPathMapping(_locationToLower); + _activeManifest = value; + _activeManifest.InitAssetPathMapping(_locationToLower); } get { - return _activePatchManifest; + return _activeManifest; } } - public string GetPackageVersion() - { - if (_activePatchManifest == null) - return string.Empty; - return _activePatchManifest.PackageVersion; - } public bool IsBuildinPatchBundle(PatchBundle patchBundle) { return true; @@ -52,7 +46,7 @@ namespace YooAsset OperationSystem.StartOperation(operation); return operation; } - UpdatePackageManifestOperation IPlayModeServices.UpdatePackageManifestAsync(string packageVersion, bool autoSaveManifestFile, int timeout) + UpdatePackageManifestOperation IPlayModeServices.UpdatePackageManifestAsync(string packageVersion, int timeout) { var operation = new OfflinePlayModeUpdatePackageManifestOperation(); OperationSystem.StartOperation(operation); @@ -113,7 +107,7 @@ namespace YooAsset throw new Exception("Should never get here !"); // 注意:如果补丁清单里未找到资源包会抛出异常! - var patchBundle = _activePatchManifest.GetMainPatchBundle(assetInfo.AssetPath); + var patchBundle = _activeManifest.GetMainPatchBundle(assetInfo.AssetPath); return CreateBundleInfo(patchBundle); } BundleInfo[] IBundleServices.GetAllDependBundleInfos(AssetInfo assetInfo) @@ -122,7 +116,7 @@ namespace YooAsset throw new Exception("Should never get here !"); // 注意:如果补丁清单里未找到资源包会抛出异常! - var depends = _activePatchManifest.GetAllDependencies(assetInfo.AssetPath); + var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); List result = new List(depends.Length); foreach (var patchBundle in depends) { @@ -133,22 +127,22 @@ namespace YooAsset } AssetInfo[] IBundleServices.GetAssetInfos(string[] tags) { - return _activePatchManifest.GetAssetsInfoByTags(tags); + return _activeManifest.GetAssetsInfoByTags(tags); } PatchAsset IBundleServices.TryGetPatchAsset(string assetPath) { - if (_activePatchManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset)) + if (_activeManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset)) return patchAsset; else return null; } string IBundleServices.MappingToAssetPath(string location) { - return _activePatchManifest.MappingToAssetPath(location); + return _activeManifest.MappingToAssetPath(location); } string IBundleServices.TryMappingToAssetPath(string location) { - return _activePatchManifest.TryMappingToAssetPath(location); + return _activeManifest.TryMappingToAssetPath(location); } string IBundleServices.GetPackageName() { @@ -156,11 +150,11 @@ namespace YooAsset } bool IBundleServices.IsIncludeBundleFile(string fileName) { - return _activePatchManifest.IsIncludeBundleFile(fileName); + return _activeManifest.IsIncludeBundleFile(fileName); } bool IBundleServices.IsServicesValid() { - return _activePatchManifest != null; + return _activeManifest != null; } #endregion } diff --git a/Assets/YooAsset/Runtime/Services/IPlayModeServices.cs b/Assets/YooAsset/Runtime/Services/IPlayModeServices.cs index 4098c1d..fe5dbeb 100644 --- a/Assets/YooAsset/Runtime/Services/IPlayModeServices.cs +++ b/Assets/YooAsset/Runtime/Services/IPlayModeServices.cs @@ -6,12 +6,7 @@ namespace YooAsset /// /// 激活的清单 /// - PatchManifest ActivePatchManifest { set; get; } - - /// - /// 获取激活包裹的版本信息 - /// - string GetPackageVersion(); + PatchManifest ActiveManifest { set; get; } /// /// 是否为内置资源文件 @@ -26,19 +21,19 @@ namespace YooAsset /// /// 向网络端请求并更新补丁清单 /// - UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, bool autoSaveManifestFile, int timeout); + UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, int timeout); /// /// 检查包裹内容的完整性 /// CheckContentsIntegrityOperation CheckContentsIntegrityAsync(); - // 下载相关方法 + // 下载相关 PatchDownloaderOperation CreatePatchDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout); PatchDownloaderOperation CreatePatchDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout); PatchDownloaderOperation CreatePatchDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout); - // 解压相关方法 + // 解压相关 PatchUnpackerOperation CreatePatchUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout); PatchUnpackerOperation CreatePatchUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout); } diff --git a/Assets/YooAsset/Runtime/Settings/YooAssetSettingsData.cs b/Assets/YooAsset/Runtime/Settings/YooAssetSettingsData.cs index 056c5c7..05cbf2a 100644 --- a/Assets/YooAsset/Runtime/Settings/YooAssetSettingsData.cs +++ b/Assets/YooAsset/Runtime/Settings/YooAssetSettingsData.cs @@ -41,41 +41,33 @@ namespace YooAsset } /// - /// 获取补丁清单文件不带版本号的名称 + /// 获取清单文件完整名称 /// - public static string GetPatchManifestFileNameWithoutVersion(string packageName) - { - return $"{Setting.PatchManifestFileName}_{packageName}.bytes"; - } - - /// - /// 获取补丁清单文件完整名称 - /// - public static string GetPatchManifestBinaryFileName(string packageName, string packageVersion) + public static string GetManifestBinaryFileName(string packageName, string packageVersion) { return $"{Setting.PatchManifestFileName}_{packageName}_{packageVersion}.bytes"; } /// - /// 获取补丁清单文件完整名称 + /// 获取清单文件完整名称 /// - public static string GetPatchManifestJsonFileName(string packageName, string packageVersion) + public static string GetManifestJsonFileName(string packageName, string packageVersion) { return $"{Setting.PatchManifestFileName}_{packageName}_{packageVersion}.json"; } /// - /// 获取补丁清单哈希文件完整名称 + /// 获取包裹的哈希文件完整名称 /// - public static string GetPatchManifestHashFileName(string packageName, string packageVersion) + public static string GetPackageHashFileName(string packageName, string packageVersion) { return $"{Setting.PatchManifestFileName}_{packageName}_{packageVersion}.hash"; } /// - /// 获取补丁清单版本文件完整名称 + /// 获取包裹的版本文件完整名称 /// - public static string GetPatchManifestVersionFileName(string packageName) + public static string GetPackageVersionFileName(string packageName) { return $"{Setting.PatchManifestFileName}_{packageName}.version"; } diff --git a/Assets/YooAsset/Runtime/Utility/YooHelper.cs b/Assets/YooAsset/Runtime/Utility/YooHelper.cs index 11dd2ff..a8e4835 100644 --- a/Assets/YooAsset/Runtime/Utility/YooHelper.cs +++ b/Assets/YooAsset/Runtime/Utility/YooHelper.cs @@ -87,6 +87,7 @@ namespace YooAsset private const string ManifestFolderName = "ManifestFiles"; private const string AppFootPrintFileName = "ApplicationFootPrint.bytes"; + /// /// 删除沙盒总目录 /// @@ -117,6 +118,7 @@ namespace YooAsset Directory.Delete(root, true); } + /// /// 获取缓存文件夹路径 /// @@ -137,10 +139,37 @@ namespace YooAsset /// /// 获取沙盒内清单文件的路径 /// - public static string GetCacheManifestFilePath(string packageName) + public static string GetCacheManifestFilePath(string packageName, string packageVersion) { - string fileName = YooAssetSettingsData.GetPatchManifestFileNameWithoutVersion(packageName); + string fileName = YooAssetSettingsData.GetManifestBinaryFileName(packageName, packageVersion); return PathHelper.MakePersistentLoadPath($"{ManifestFolderName}/{fileName}"); } + + /// + /// 获取沙盒内包裹的哈希文件的路径 + /// + public static string GetCachePackageHashFilePath(string packageName, string packageVersion) + { + string fileName = YooAssetSettingsData.GetPackageHashFileName(packageName, packageVersion); + return PathHelper.MakePersistentLoadPath($"{ManifestFolderName}/{fileName}"); + } + + /// + /// 获取沙盒内包裹的版本文件的路径 + /// + public static string GetCachePackageVersionFilePath(string packageName) + { + string fileName = YooAssetSettingsData.GetPackageVersionFileName(packageName); + return PathHelper.MakePersistentLoadPath($"{ManifestFolderName}/{fileName}"); + } + + /// + /// 保存默认的包裹版本 + /// + public static void SaveCachePackageVersionFile(string packageName, string version) + { + string filePath = GetCachePackageVersionFilePath(packageName); + FileUtility.CreateFile(filePath, version); + } } } \ No newline at end of file