123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading;
- namespace YooAsset
- {
-
-
-
- internal abstract class PatchCacheVerifier
- {
- public abstract bool InitVerifier(PatchManifest appPatchManifest, PatchManifest localPatchManifest, bool weaklyUpdate);
- public abstract bool UpdateVerifier();
- public abstract float GetVerifierProgress();
- public int VerifySuccessCount { protected set; get; } = 0;
- public int VerifyFailCount { protected set; get; } = 0;
- }
-
-
-
- internal class PatchCacheVerifierWithThread : PatchCacheVerifier
- {
- private class ThreadInfo
- {
- public EVerifyResult Result;
- public string FilePath { private set; get; }
- public PatchBundle Bundle { private set; get; }
- public ThreadInfo(string filePath, PatchBundle bundle)
- {
- FilePath = filePath;
- Bundle = bundle;
- }
- }
- private readonly ThreadSyncContext _syncContext = new ThreadSyncContext();
- private readonly List<PatchBundle> _waitingList = new List<PatchBundle>(1000);
- private readonly List<PatchBundle> _verifyingList = new List<PatchBundle>(100);
- private int _verifyMaxNum;
- private int _verifyTotalCount;
- public override bool InitVerifier(PatchManifest appPatchManifest, PatchManifest localPatchManifest, bool weaklyUpdate)
- {
-
- foreach (var patchBundle in localPatchManifest.BundleList)
- {
-
- if (CacheSystem.IsCached(patchBundle))
- continue;
-
-
- if (appPatchManifest.TryGetPatchBundle(patchBundle.BundleName, out PatchBundle appPatchBundle))
- {
- if (appPatchBundle.IsBuildin && appPatchBundle.Equals(patchBundle))
- continue;
- }
-
- if (weaklyUpdate)
- {
- string filePath = patchBundle.CachedFilePath;
- if (File.Exists(filePath))
- _waitingList.Add(patchBundle);
- else
- return false;
- }
- else
- {
- string filePath = patchBundle.CachedFilePath;
- if (File.Exists(filePath))
- _waitingList.Add(patchBundle);
- }
- }
-
- 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;
- return true;
- }
- 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 patchBundle = _waitingList[i];
- if (VerifyFile(patchBundle))
- {
- _waitingList.RemoveAt(i);
- _verifyingList.Add(patchBundle);
- }
- else
- {
- YooLogger.Warning("The thread pool is failed queued.");
- break;
- }
- }
- return false;
- }
- public override float GetVerifierProgress()
- {
- if (_verifyTotalCount == 0)
- return 1f;
- return (float)(VerifySuccessCount + VerifyFailCount) / _verifyTotalCount;
- }
- private bool VerifyFile(PatchBundle patchBundle)
- {
- string filePath = patchBundle.CachedFilePath;
- ThreadInfo info = new ThreadInfo(filePath, patchBundle);
- return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), info);
- }
- private void VerifyInThread(object infoObj)
- {
- ThreadInfo info = (ThreadInfo)infoObj;
- info.Result = CacheSystem.VerifyBundle(info.Bundle, CacheSystem.InitVerifyLevel);
- _syncContext.Post(VerifyCallback, info);
- }
- private void VerifyCallback(object obj)
- {
- ThreadInfo info = (ThreadInfo)obj;
- if (info.Result == EVerifyResult.Succeed)
- {
- VerifySuccessCount++;
- CacheSystem.CacheBundle(info.Bundle);
- }
- else
- {
- VerifyFailCount++;
- YooLogger.Warning($"Failed to verify file : {info.Bundle.CachedFilePath}");
-
-
- }
- _verifyingList.Remove(info.Bundle);
- }
- }
-
-
-
- internal class PatchCacheVerifierWithoutThread : PatchCacheVerifier
- {
- private readonly List<PatchBundle> _waitingList = new List<PatchBundle>(1000);
- private readonly List<PatchBundle> _verifyingList = new List<PatchBundle>(100);
- private int _verifyMaxNum;
- private int _verifyTotalCount;
- public override bool InitVerifier(PatchManifest appPatchManifest, PatchManifest localPatchManifest, bool weaklyUpdate)
- {
-
- foreach (var patchBundle in localPatchManifest.BundleList)
- {
-
- if (CacheSystem.IsCached(patchBundle))
- continue;
-
-
- if (appPatchManifest.TryGetPatchBundle(patchBundle.BundleName, out PatchBundle appPatchBundle))
- {
- if (appPatchBundle.IsBuildin && appPatchBundle.Equals(patchBundle))
- continue;
- }
-
- if (weaklyUpdate)
- {
- string filePath = patchBundle.CachedFilePath;
- if (File.Exists(filePath))
- _waitingList.Add(patchBundle);
- else
- return false;
- }
- else
- {
- string filePath = patchBundle.CachedFilePath;
- if (File.Exists(filePath))
- _waitingList.Add(patchBundle);
- }
- }
-
- _verifyMaxNum = 32;
- _verifyTotalCount = _waitingList.Count;
- return true;
- }
- 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 patchBundle = _waitingList[i];
- VerifyFile(patchBundle);
- _waitingList.RemoveAt(i);
- _verifyingList.Add(patchBundle);
- }
- _verifyingList.Clear();
- return false;
- }
- public override float GetVerifierProgress()
- {
- if (_verifyTotalCount == 0)
- return 1f;
- return (float)(VerifySuccessCount + VerifyFailCount) / _verifyTotalCount;
- }
- private void VerifyFile(PatchBundle patchBundle)
- {
- var verifyResult = CacheSystem.VerifyAndCacheBundle(patchBundle, CacheSystem.InitVerifyLevel);
- if (verifyResult == EVerifyResult.Succeed)
- {
- VerifySuccessCount++;
- }
- else
- {
- VerifyFailCount++;
- YooLogger.Warning($"Failed to verify file : {patchBundle.CachedFilePath}");
-
-
- }
- }
- }
- }
|