diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheVerifier.cs b/Assets/YooAsset/Runtime/CacheSystem/CacheVerifier.cs deleted file mode 100644 index fd97b20..0000000 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheVerifier.cs +++ /dev/null @@ -1,181 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Threading; - -namespace YooAsset -{ - /// - /// 本地缓存文件验证器 - /// - internal abstract class CacheVerifier - { - public abstract void InitVerifier(List verifyInfos); - public abstract bool UpdateVerifier(); - public abstract float GetVerifierProgress(); - - public List VerifySuccessList { protected set; get; } - public List VerifyFailList { protected set; get; } - } - - /// - /// 本地缓存文件验证器(线程版) - /// - internal class CacheVerifierWithThread : CacheVerifier - { - private readonly ThreadSyncContext _syncContext = new ThreadSyncContext(); - private readonly List _waitingList = new List(1000); - private readonly List _verifyingList = new List(100); - private int _verifyMaxNum; - private int _verifyTotalCount; - - public override void InitVerifier(List verifyInfos) - { - _waitingList.AddRange(verifyInfos); - VerifySuccessList = new List(verifyInfos.Count); - VerifyFailList = new List(verifyInfos.Count); - - // 设置同时验证的最大数 - ThreadPool.GetMaxThreads(out int workerThreads, out int ioThreads); - YooLogger.Log($"Work threads : {workerThreads}, IO threads : {ioThreads}"); - _verifyMaxNum = Math.Min(workerThreads, ioThreads); - _verifyTotalCount = _waitingList.Count; - if (_verifyMaxNum < 1) - _verifyMaxNum = 1; - } - public override bool UpdateVerifier() - { - _syncContext.Update(); - - if (_waitingList.Count == 0 && _verifyingList.Count == 0) - return true; - - if (_verifyingList.Count >= _verifyMaxNum) - return false; - - for (int i = _waitingList.Count - 1; i >= 0; i--) - { - if (_verifyingList.Count >= _verifyMaxNum) - break; - - var verifyIno = _waitingList[i]; - if (VerifyFileWithThread(verifyIno)) - { - _waitingList.RemoveAt(i); - _verifyingList.Add(verifyIno); - } - else - { - YooLogger.Warning("The thread pool is failed queued."); - break; - } - } - - return false; - } - public override float GetVerifierProgress() - { - if (_verifyTotalCount == 0) - return 1f; - return (float)(VerifySuccessList.Count + VerifyFailList.Count) / _verifyTotalCount; - } - - private bool VerifyFileWithThread(VerifyInfo verifyInfo) - { - return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), verifyInfo); - } - private void VerifyInThread(object infoObj) - { - VerifyInfo verifyInfo = (VerifyInfo)infoObj; - verifyInfo.Result = CacheSystem.VerifyBundle(verifyInfo.VerifyBundle, CacheSystem.InitVerifyLevel); - _syncContext.Post(VerifyCallback, verifyInfo); - } - private void VerifyCallback(object obj) - { - VerifyInfo verifyIno = (VerifyInfo)obj; - if (verifyIno.Result == EVerifyResult.Succeed) - { - VerifySuccessList.Add(verifyIno); - CacheSystem.CacheBundle(verifyIno.VerifyBundle); - } - else - { - VerifyFailList.Add(verifyIno); - - // NOTE:不期望删除断点续传的资源文件 - /* - if (File.Exists(patchBundle.CachedBundleFilePath)) - File.Delete(patchBundle.CachedBundleFilePath); - */ - } - _verifyingList.Remove(verifyIno); - } - } - - /// - /// 本地缓存文件验证器(非线程版) - /// - internal class CacheVerifierWithoutThread : CacheVerifier - { - private readonly List _waitingList = new List(1000); - private readonly List _verifyingList = new List(100); - private int _verifyMaxNum; - private int _verifyTotalCount; - - public override void InitVerifier(List verifyInfos) - { - _waitingList.AddRange(verifyInfos); - VerifySuccessList = new List(verifyInfos.Count); - VerifyFailList = new List(verifyInfos.Count); - - // 设置同时验证的最大数 - _verifyMaxNum = 32; - _verifyTotalCount = _waitingList.Count; - } - public override bool UpdateVerifier() - { - if (_waitingList.Count == 0 && _verifyingList.Count == 0) - return true; - - for (int i = _waitingList.Count - 1; i >= 0; i--) - { - if (_verifyingList.Count >= _verifyMaxNum) - break; - - var verifyIno = _waitingList[i]; - VerifyFileWithoutThread(verifyIno); - _waitingList.RemoveAt(i); - _verifyingList.Add(verifyIno); - } - - _verifyingList.Clear(); - return false; - } - public override float GetVerifierProgress() - { - if (_verifyTotalCount == 0) - return 1f; - return (float)(VerifySuccessList.Count + VerifyFailList.Count) / _verifyTotalCount; - } - - private void VerifyFileWithoutThread(VerifyInfo verifyIno) - { - var verifyResult = CacheSystem.VerifyAndCacheLocalBundleFile(verifyIno.VerifyBundle, CacheSystem.InitVerifyLevel); - if (verifyResult == EVerifyResult.Succeed) - { - VerifySuccessList.Add(verifyIno); - } - else - { - VerifyFailList.Add(verifyIno); - - // NOTE:不期望删除断点续传的资源文件 - /* - if (File.Exists(patchBundle.CachedBundleFilePath)) - File.Delete(patchBundle.CachedBundleFilePath); - */ - } - } - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/CacheFilesVerifyOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/CacheFilesVerifyOperation.cs new file mode 100644 index 0000000..fd6b4e0 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/CacheFilesVerifyOperation.cs @@ -0,0 +1,295 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Threading; + +namespace YooAsset +{ + /// + /// 本地缓存文件验证 + /// + internal abstract class CacheFilesVerifyOperation : AsyncOperationBase + { + public List VerifySuccessList { protected set; get; } + public List VerifyFailList { protected set; get; } + } + + /// + /// 本地缓存文件验证(线程版) + /// + internal class CacheFilesVerifyWithThreadOperation : CacheFilesVerifyOperation + { + private enum ESteps + { + None, + InitVerify, + PrepareVerify, + UpdateVerify, + Done, + } + + private readonly PatchManifest _patchManifest; + private readonly IQueryServices _queryServices; + private ESteps _steps = ESteps.None; + + private readonly ThreadSyncContext _syncContext = new ThreadSyncContext(); + private List _waitingList; + private List _verifyingList; + private int _verifyMaxNum; + private int _verifyTotalCount; + + public CacheFilesVerifyWithThreadOperation(PatchManifest patchManifest, IQueryServices queryServices) + { + _patchManifest = patchManifest; + _queryServices = queryServices; + } + internal override void Start() + { + _steps = ESteps.InitVerify; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.InitVerify) + { + int bundleCount = _patchManifest.BundleList.Count; + VerifySuccessList = new List(bundleCount); + VerifyFailList = new List(bundleCount); + + // 设置同时验证的最大数 + ThreadPool.GetMaxThreads(out int workerThreads, out int ioThreads); + YooLogger.Log($"Work threads : {workerThreads}, IO threads : {ioThreads}"); + _verifyMaxNum = Math.Min(workerThreads, ioThreads); + _verifyTotalCount = bundleCount; + if (_verifyMaxNum < 1) + _verifyMaxNum = 1; + + _waitingList = new List(bundleCount); + _verifyingList = new List(_verifyMaxNum); + _steps = ESteps.PrepareVerify; + } + + if (_steps == ESteps.PrepareVerify) + { + foreach (var patchBundle in _patchManifest.BundleList) + { + if (CacheSystem.IsCached(patchBundle)) + continue; + + bool isBuildinFile = IsBuildinFile(patchBundle); + VerifyInfo verifyInfo = new VerifyInfo(isBuildinFile, patchBundle); + _waitingList.Add(verifyInfo); + } + _steps = ESteps.UpdateVerify; + } + + if (_steps == ESteps.UpdateVerify) + { + _syncContext.Update(); + + Progress = GetVerifierProgress(); + if (_waitingList.Count == 0 && _verifyingList.Count == 0) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + + for (int i = _waitingList.Count - 1; i >= 0; i--) + { + if (OperationSystem.IsBusy) + break; + + if (_verifyingList.Count >= _verifyMaxNum) + break; + + var verifyIno = _waitingList[i]; + if (VerifyFileWithThread(verifyIno)) + { + _waitingList.RemoveAt(i); + _verifyingList.Add(verifyIno); + } + else + { + YooLogger.Warning("The thread pool is failed queued."); + break; + } + } + } + } + + private float GetVerifierProgress() + { + if (_verifyTotalCount == 0) + return 1f; + return (float)(VerifySuccessList.Count + VerifyFailList.Count) / _verifyTotalCount; + } + private bool IsBuildinFile(PatchBundle patchBundle) + { + if (_queryServices == null) + return true; + + return _queryServices.QueryStreamingAssets(patchBundle.FileName); + } + private bool VerifyFileWithThread(VerifyInfo verifyInfo) + { + return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), verifyInfo); + } + private void VerifyInThread(object infoObj) + { + VerifyInfo verifyInfo = (VerifyInfo)infoObj; + verifyInfo.Result = CacheSystem.VerifyBundle(verifyInfo.VerifyBundle, CacheSystem.InitVerifyLevel); + _syncContext.Post(VerifyCallback, verifyInfo); + } + private void VerifyCallback(object obj) + { + VerifyInfo verifyIno = (VerifyInfo)obj; + if (verifyIno.Result == EVerifyResult.Succeed) + { + VerifySuccessList.Add(verifyIno); + CacheSystem.CacheBundle(verifyIno.VerifyBundle); + } + else + { + VerifyFailList.Add(verifyIno); + + // 删除验证失败的缓存文件 + if (File.Exists(verifyIno.VerifyBundle.CachedFilePath)) + { + YooLogger.Warning($"Delete verify failed bundle file : {verifyIno.VerifyBundle.CachedFilePath}"); + File.Delete(verifyIno.VerifyBundle.CachedFilePath); + } + } + _verifyingList.Remove(verifyIno); + } + } + + /// + /// 本地缓存文件验证(非线程版) + /// + internal class CacheFilesVerifyWithoutThreadOperation : CacheFilesVerifyOperation + { + private enum ESteps + { + None, + InitVerify, + PrepareVerify, + UpdateVerify, + Done, + } + + private readonly PatchManifest _patchManifest; + private readonly IQueryServices _queryServices; + private ESteps _steps = ESteps.None; + + private List _waitingList; + private List _verifyingList; + private int _verifyMaxNum; + private int _verifyTotalCount; + + public CacheFilesVerifyWithoutThreadOperation(PatchManifest patchManifest, IQueryServices queryServices) + { + _patchManifest = patchManifest; + _queryServices = queryServices; + } + internal override void Start() + { + _steps = ESteps.InitVerify; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.InitVerify) + { + int bundleCount = _patchManifest.BundleList.Count; + VerifySuccessList = new List(bundleCount); + VerifyFailList = new List(bundleCount); + + // 设置同时验证的最大数 + _verifyMaxNum = bundleCount; + _verifyTotalCount = _waitingList.Count; + + _waitingList = new List(bundleCount); + _verifyingList = new List(_verifyMaxNum); + _steps = ESteps.PrepareVerify; + } + + if (_steps == ESteps.PrepareVerify) + { + foreach (var patchBundle in _patchManifest.BundleList) + { + if (CacheSystem.IsCached(patchBundle)) + continue; + + bool isBuildinFile = IsBuildinFile(patchBundle); + VerifyInfo verifyInfo = new VerifyInfo(isBuildinFile, patchBundle); + _waitingList.Add(verifyInfo); + } + _steps = ESteps.UpdateVerify; + } + + if (_steps == ESteps.UpdateVerify) + { + Progress = GetVerifierProgress(); + if (_waitingList.Count == 0 && _verifyingList.Count == 0) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + + for (int i = _waitingList.Count - 1; i >= 0; i--) + { + if (OperationSystem.IsBusy) + break; + + if (_verifyingList.Count >= _verifyMaxNum) + break; + + var verifyIno = _waitingList[i]; + VerifyFileWithoutThread(verifyIno); + _waitingList.RemoveAt(i); + _verifyingList.Add(verifyIno); + } + + _verifyingList.Clear(); + } + } + + private float GetVerifierProgress() + { + if (_verifyTotalCount == 0) + return 1f; + return (float)(VerifySuccessList.Count + VerifyFailList.Count) / _verifyTotalCount; + } + private bool IsBuildinFile(PatchBundle patchBundle) + { + if (_queryServices == null) + return true; + + return _queryServices.QueryStreamingAssets(patchBundle.FileName); + } + private void VerifyFileWithoutThread(VerifyInfo verifyIno) + { + var verifyResult = CacheSystem.VerifyAndCacheLocalBundleFile(verifyIno.VerifyBundle, CacheSystem.InitVerifyLevel); + if (verifyResult == EVerifyResult.Succeed) + { + VerifySuccessList.Add(verifyIno); + } + else + { + VerifyFailList.Add(verifyIno); + + // 删除验证失败的缓存文件 + if (File.Exists(verifyIno.VerifyBundle.CachedFilePath)) + { + YooLogger.Warning($"Delete verify failed bundle file : {verifyIno.VerifyBundle.CachedFilePath}"); + File.Delete(verifyIno.VerifyBundle.CachedFilePath); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheVerifier.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/CacheFilesVerifyOperation.cs.meta similarity index 83% rename from Assets/YooAsset/Runtime/CacheSystem/CacheVerifier.cs.meta rename to Assets/YooAsset/Runtime/PatchSystem/Operations/CacheFilesVerifyOperation.cs.meta index db73986..c0aa9ba 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheVerifier.cs.meta +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/CacheFilesVerifyOperation.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 223f217ed81460541b5ea7eb7a7d82a4 +guid: 868188d19436b674b8c403d2d90afeaf MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckPackageContentsOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckPackageContentsOperation.cs index 7d5ddd5..922d6aa 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckPackageContentsOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/CheckPackageContentsOperation.cs @@ -44,14 +44,14 @@ namespace YooAsset { None, CheckLoadedManifest, - InitVerifyingCache, - UpdateVerifyingCache, + StartVerifyOperation, + CheckVerifyOperation, Done, } private readonly HostPlayModeImpl _impl; private readonly string _packageName; - private readonly CacheVerifier _cacheVerifier; + private CacheFilesVerifyOperation _verifyOperation; private ESteps _steps = ESteps.None; private float _verifyTime; @@ -59,12 +59,6 @@ namespace YooAsset { _impl = impl; _packageName = packageName; - -#if UNITY_WEBGL - _cacheVerifier = new CacheVerifierWithoutThread(); -#else - _cacheVerifier = new CacheVerifierWithThread(); -#endif } internal override void Start() { @@ -85,28 +79,33 @@ namespace YooAsset } else { - _steps = ESteps.InitVerifyingCache; + _steps = ESteps.StartVerifyOperation; } } - if (_steps == ESteps.InitVerifyingCache) + if (_steps == ESteps.StartVerifyOperation) { - var verifyInfos = _impl.GetVerifyInfoList(true); - _cacheVerifier.InitVerifier(verifyInfos); +#if UNITY_WEBGL + _verifyOperation = new CacheFilesVerifyWithoutThreadOperation(_impl.LocalPatchManifest, _impl.QueryServices); +#else + _verifyOperation = new CacheFilesVerifyWithThreadOperation(_impl.LocalPatchManifest, _impl.QueryServices); +#endif + + OperationSystem.StartOperation(_verifyOperation); _verifyTime = UnityEngine.Time.realtimeSinceStartup; - _steps = ESteps.UpdateVerifyingCache; + _steps = ESteps.CheckVerifyOperation; } - if (_steps == ESteps.UpdateVerifyingCache) + if (_steps == ESteps.CheckVerifyOperation) { - Progress = _cacheVerifier.GetVerifierProgress(); - if (_cacheVerifier.UpdateVerifier()) + Progress = _verifyOperation.Progress; + if (_verifyOperation.IsDone) { float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyTime; - YooLogger.Log($"Verify result : Success {_cacheVerifier.VerifySuccessList.Count}, Fail {_cacheVerifier.VerifyFailList.Count}, Elapsed time {costTime} seconds"); + YooLogger.Log($"Verify result : Success {_verifyOperation.VerifySuccessList.Count}, Fail {_verifyOperation.VerifyFailList.Count}, Elapsed time {costTime} seconds"); bool verifySucceed = true; - foreach (var verifyInfo in _cacheVerifier.VerifyFailList) + foreach (var verifyInfo in _verifyOperation.VerifyFailList) { // 注意:跳过内置资源文件 if (verifyInfo.IsBuildinFile) diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs index a26e0a3..31c878e 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs @@ -83,16 +83,16 @@ namespace YooAsset None, QueryAppPackageVersion, LoadAppManifest, - InitVerifyingCache, - UpdateVerifyingCache, + StartVerifyOperation, + CheckVerifyOperation, Done, } private readonly OfflinePlayModeImpl _impl; private readonly string _packageName; - private readonly CacheVerifier _cacheVerifier; private readonly AppPackageVersionQuerier _appPackageVersionQuerier; private AppManifestLoader _appManifestLoader; + private CacheFilesVerifyOperation _verifyOperation; private ESteps _steps = ESteps.None; private float _verifyTime; @@ -101,12 +101,6 @@ namespace YooAsset _impl = impl; _packageName = packageName; _appPackageVersionQuerier = new AppPackageVersionQuerier(packageName); - -#if UNITY_WEBGL - _cacheVerifier = new CacheVerifierWithoutThread(); -#else - _cacheVerifier = new CacheVerifierWithThread(); -#endif } internal override void Start() { @@ -155,27 +149,32 @@ namespace YooAsset { InitializedPackageVersion = manifest.PackageVersion; _impl.SetAppPatchManifest(manifest); - _steps = ESteps.InitVerifyingCache; + _steps = ESteps.StartVerifyOperation; } } - if (_steps == ESteps.InitVerifyingCache) + if (_steps == ESteps.StartVerifyOperation) { - var verifyInfos = _impl.GetVerifyInfoList(); - _cacheVerifier.InitVerifier(verifyInfos); +#if UNITY_WEBGL + _verifyOperation = new CacheFilesVerifyWithoutThreadOperation(_impl.AppPatchManifest, null); +#else + _verifyOperation = new CacheFilesVerifyWithThreadOperation(_impl.AppPatchManifest, null); +#endif + + OperationSystem.StartOperation(_verifyOperation); _verifyTime = UnityEngine.Time.realtimeSinceStartup; - _steps = ESteps.UpdateVerifyingCache; + _steps = ESteps.CheckVerifyOperation; } - if (_steps == ESteps.UpdateVerifyingCache) + if (_steps == ESteps.CheckVerifyOperation) { - Progress = _cacheVerifier.GetVerifierProgress(); - if (_cacheVerifier.UpdateVerifier()) + Progress = _verifyOperation.Progress; + if (_verifyOperation.IsDone) { _steps = ESteps.Done; Status = EOperationStatus.Succeed; float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyTime; - YooLogger.Log($"Verify result : Success {_cacheVerifier.VerifySuccessList.Count}, Fail {_cacheVerifier.VerifyFailList.Count}, Elapsed time {costTime} seconds"); + YooLogger.Log($"Verify result : Success {_verifyOperation.VerifySuccessList.Count}, Fail {_verifyOperation.VerifyFailList.Count}, Elapsed time {costTime} seconds"); } } } @@ -195,17 +194,17 @@ namespace YooAsset QueryAppPackageVersion, CopyAppManifest, LoadAppManifest, - InitVerifyingCache, - UpdateVerifyingCache, + StartVerifyOperation, + CheckVerifyOperation, Done, } private readonly HostPlayModeImpl _impl; private readonly string _packageName; - private readonly CacheVerifier _cacheVerifier; private readonly AppPackageVersionQuerier _appPackageVersionQuerier; private AppManifestCopyer _appManifestCopyer; private AppManifestLoader _appManifestLoader; + private CacheFilesVerifyOperation _verifyOperation; private ESteps _steps = ESteps.None; private float _verifyTime; @@ -214,12 +213,6 @@ namespace YooAsset _impl = impl; _packageName = packageName; _appPackageVersionQuerier = new AppPackageVersionQuerier(packageName); - -#if UNITY_WEBGL - _cacheVerifier = new CacheVerifierWithoutThread(); -#else - _cacheVerifier = new CacheVerifierWithThread(); -#endif } internal override void Start() { @@ -254,7 +247,7 @@ namespace YooAsset var manifest = PersistentHelper.LoadCacheManifestFile(_packageName); InitializedPackageVersion = manifest.PackageVersion; _impl.SetLocalPatchManifest(manifest); - _steps = ESteps.InitVerifyingCache; + _steps = ESteps.StartVerifyOperation; } catch (System.Exception e) { @@ -330,27 +323,32 @@ namespace YooAsset { InitializedPackageVersion = manifest.PackageVersion; _impl.SetLocalPatchManifest(manifest); - _steps = ESteps.InitVerifyingCache; + _steps = ESteps.StartVerifyOperation; } } - if (_steps == ESteps.InitVerifyingCache) + if (_steps == ESteps.StartVerifyOperation) { - var verifyInfos = _impl.GetVerifyInfoList(false); - _cacheVerifier.InitVerifier(verifyInfos); +#if UNITY_WEBGL + _verifyOperation = new CacheFilesVerifyWithoutThreadOperation(_impl.LocalPatchManifest, _impl.QueryServices); +#else + _verifyOperation = new CacheFilesVerifyWithThreadOperation(_impl.LocalPatchManifest, _impl.QueryServices); +#endif + + OperationSystem.StartOperation(_verifyOperation); _verifyTime = UnityEngine.Time.realtimeSinceStartup; - _steps = ESteps.UpdateVerifyingCache; + _steps = ESteps.CheckVerifyOperation; } - if (_steps == ESteps.UpdateVerifyingCache) + if (_steps == ESteps.CheckVerifyOperation) { - Progress = _cacheVerifier.GetVerifierProgress(); - if (_cacheVerifier.UpdateVerifier()) + Progress = _verifyOperation.Progress; + if (_verifyOperation.IsDone) { _steps = ESteps.Done; Status = EOperationStatus.Succeed; float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyTime; - YooLogger.Log($"Verify result : Success {_cacheVerifier.VerifySuccessList.Count}, Fail {_cacheVerifier.VerifyFailList.Count}, Elapsed time {costTime} seconds"); + YooLogger.Log($"Verify result : Success {_verifyOperation.VerifySuccessList.Count}, Fail {_verifyOperation.VerifyFailList.Count}, Elapsed time {costTime} seconds"); } } } diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs index 2c39b05..48bb7c1 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageManifestOperation.cs @@ -59,8 +59,8 @@ namespace YooAsset LoadCacheManifest, LoadWebManifest, CheckWebManifest, - InitVerifyingCache, - UpdateVerifyingCache, + StartVerifyOperation, + CheckVerifyOperation, Done, } @@ -68,10 +68,10 @@ namespace YooAsset private readonly HostPlayModeImpl _impl; private readonly string _packageName; private readonly string _packageVersion; - private readonly CacheVerifier _cacheVerifier; private readonly int _timeout; private UnityWebDataRequester _downloader1; private UnityWebDataRequester _downloader2; + private CacheFilesVerifyOperation _verifyOperation; private string _cacheManifestHash; private ESteps _steps = ESteps.None; @@ -83,12 +83,6 @@ namespace YooAsset _packageName = packageName; _packageVersion = packageVersion; _timeout = timeout; - -#if UNITY_WEBGL - _cacheVerifier = new CacheVerifierWithoutThread(); -#else - _cacheVerifier = new CacheVerifierWithThread(); -#endif } internal override void Start() { @@ -158,7 +152,7 @@ namespace YooAsset { var manifest = PersistentHelper.LoadCacheManifestFile(_packageName); _impl.SetLocalPatchManifest(manifest); - _steps = ESteps.InitVerifyingCache; + _steps = ESteps.StartVerifyOperation; } catch (System.Exception e) { @@ -198,7 +192,7 @@ namespace YooAsset var manifest = PersistentHelper.SaveCacheManifestFile(_packageName, bytesData); _impl.SetLocalPatchManifest(manifest); FoundNewManifest = true; - _steps = ESteps.InitVerifyingCache; + _steps = ESteps.StartVerifyOperation; } catch (Exception e) { @@ -210,23 +204,28 @@ namespace YooAsset _downloader2.Dispose(); } - if (_steps == ESteps.InitVerifyingCache) + if (_steps == ESteps.StartVerifyOperation) { - var verifyInfos = _impl.GetVerifyInfoList(false); - _cacheVerifier.InitVerifier(verifyInfos); +#if UNITY_WEBGL + _verifyOperation = new CacheFilesVerifyWithoutThreadOperation(_impl.LocalPatchManifest, _impl.QueryServices); +#else + _verifyOperation = new CacheFilesVerifyWithThreadOperation(_impl.LocalPatchManifest, _impl.QueryServices); +#endif + + OperationSystem.StartOperation(_verifyOperation); _verifyTime = UnityEngine.Time.realtimeSinceStartup; - _steps = ESteps.UpdateVerifyingCache; + _steps = ESteps.CheckVerifyOperation; } - if (_steps == ESteps.UpdateVerifyingCache) + if (_steps == ESteps.CheckVerifyOperation) { - Progress = _cacheVerifier.GetVerifierProgress(); - if (_cacheVerifier.UpdateVerifier()) + Progress = _verifyOperation.Progress; + if (_verifyOperation.IsDone) { _steps = ESteps.Done; Status = EOperationStatus.Succeed; float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyTime; - YooLogger.Log($"Verify result : Success {_cacheVerifier.VerifySuccessList.Count}, Fail {_cacheVerifier.VerifyFailList.Count}, Elapsed time {costTime} seconds"); + YooLogger.Log($"Verify result : Success {_verifyOperation.VerifySuccessList.Count}, Fail {_verifyOperation.VerifyFailList.Count}, Elapsed time {costTime} seconds"); } } } diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs index db6e114..fc88644 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs @@ -14,7 +14,7 @@ namespace YooAsset private bool _locationToLower; private string _defaultHostServer; private string _fallbackHostServer; - private IQueryServices _queryServices; + public IQueryServices QueryServices { private set; get; } /// /// 异步初始化 @@ -24,7 +24,7 @@ namespace YooAsset _locationToLower = locationToLower; _defaultHostServer = defaultHostServer; _fallbackHostServer = fallbackHostServer; - _queryServices = queryServices; + QueryServices = queryServices; var operation = new HostPlayModeInitializationOperation(this, packageName); OperationSystem.StartOperation(operation); @@ -312,38 +312,6 @@ namespace YooAsset return bundleInfo; } - internal List GetVerifyInfoList(bool weaklyUpdateMode) - { - List result = new List(LocalPatchManifest.BundleList.Count); - - // 遍历所有文件然后验证并缓存合法文件 - foreach (var patchBundle in LocalPatchManifest.BundleList) - { - // 忽略缓存文件 - if (CacheSystem.IsCached(patchBundle)) - continue; - - // 注意:在弱联网模式下,我们需要验证指定资源版本的所有资源完整性 - if (weaklyUpdateMode) - { - bool isBuildinFile = IsBuildinPatchBundle(patchBundle); - VerifyInfo verifyInfo = new VerifyInfo(isBuildinFile, patchBundle); - result.Add(verifyInfo); - } - else - { - string filePath = patchBundle.CachedFilePath; - if (File.Exists(filePath)) - { - bool isBuildinFile = IsBuildinPatchBundle(patchBundle); - VerifyInfo verifyInfo = new VerifyInfo(isBuildinFile, patchBundle); - result.Add(verifyInfo); - } - } - } - - return result; - } internal void SetLocalPatchManifest(PatchManifest patchManifest) { LocalPatchManifest = patchManifest; @@ -351,7 +319,7 @@ namespace YooAsset } internal bool IsBuildinPatchBundle(PatchBundle patchBundle) { - return _queryServices.QueryStreamingAssets(patchBundle.FileName); + return QueryServices.QueryStreamingAssets(patchBundle.FileName); } #region IBundleServices接口 diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs index b38e3bb..9b4feb1 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs @@ -7,7 +7,7 @@ namespace YooAsset { internal class OfflinePlayModeImpl : IBundleServices { - private PatchManifest _appPatchManifest; + public PatchManifest AppPatchManifest { private set; get; } private bool _locationToLower; /// @@ -26,37 +26,15 @@ namespace YooAsset /// public string GetPackageVersion() { - if (_appPatchManifest == null) + if (AppPatchManifest == null) return string.Empty; - return _appPatchManifest.PackageVersion; + return AppPatchManifest.PackageVersion; } - internal List GetVerifyInfoList() - { - List result = new List(_appPatchManifest.BundleList.Count); - - // 遍历所有文件然后验证并缓存合法文件 - foreach (var patchBundle in _appPatchManifest.BundleList) - { - // 忽略缓存文件 - if (CacheSystem.IsCached(patchBundle)) - continue; - - string filePath = patchBundle.CachedFilePath; - if (File.Exists(filePath)) - { - bool isBuildinFile = true; - VerifyInfo verifyInfo = new VerifyInfo(isBuildinFile, patchBundle); - result.Add(verifyInfo); - } - } - - return result; - } internal void SetAppPatchManifest(PatchManifest patchManifest) { - _appPatchManifest = patchManifest; - _appPatchManifest.InitAssetPathMapping(_locationToLower); + AppPatchManifest = patchManifest; + AppPatchManifest.InitAssetPathMapping(_locationToLower); } #region IBundleServices接口 @@ -84,7 +62,7 @@ namespace YooAsset throw new Exception("Should never get here !"); // 注意:如果补丁清单里未找到资源包会抛出异常! - var patchBundle = _appPatchManifest.GetMainPatchBundle(assetInfo.AssetPath); + var patchBundle = AppPatchManifest.GetMainPatchBundle(assetInfo.AssetPath); return CreateBundleInfo(patchBundle); } BundleInfo[] IBundleServices.GetAllDependBundleInfos(AssetInfo assetInfo) @@ -93,7 +71,7 @@ namespace YooAsset throw new Exception("Should never get here !"); // 注意:如果补丁清单里未找到资源包会抛出异常! - var depends = _appPatchManifest.GetAllDependencies(assetInfo.AssetPath); + var depends = AppPatchManifest.GetAllDependencies(assetInfo.AssetPath); List result = new List(depends.Length); foreach (var patchBundle in depends) { @@ -104,34 +82,34 @@ namespace YooAsset } AssetInfo[] IBundleServices.GetAssetInfos(string[] tags) { - return _appPatchManifest.GetAssetsInfoByTags(tags); + return AppPatchManifest.GetAssetsInfoByTags(tags); } PatchAsset IBundleServices.TryGetPatchAsset(string assetPath) { - if (_appPatchManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset)) + if (AppPatchManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset)) return patchAsset; else return null; } string IBundleServices.MappingToAssetPath(string location) { - return _appPatchManifest.MappingToAssetPath(location); + return AppPatchManifest.MappingToAssetPath(location); } string IBundleServices.TryMappingToAssetPath(string location) { - return _appPatchManifest.TryMappingToAssetPath(location); + return AppPatchManifest.TryMappingToAssetPath(location); } string IBundleServices.GetPackageName() { - return _appPatchManifest.PackageName; + return AppPatchManifest.PackageName; } bool IBundleServices.IsIncludeBundleFile(string fileName) { - return _appPatchManifest.IsIncludeBundleFile(fileName); + return AppPatchManifest.IsIncludeBundleFile(fileName); } bool IBundleServices.IsServicesValid() { - return _appPatchManifest != null; + return AppPatchManifest != null; } #endregion }