Delete the file whose verification failed

初始化的时候,删除验证失败的资源文件。
pull/9/head
hevinci 2022-05-01 14:47:59 +08:00
parent 650d8689ee
commit b0397981ce
1 changed files with 41 additions and 17 deletions

View File

@ -177,7 +177,7 @@ namespace YooAsset
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyTime; float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyTime;
YooLogger.Log($"Verify files total time : {costTime}"); YooLogger.Log($"Verify result : Success {_verifySuccessCount}, Fail {_verifyFailCount}, Elapsed time {costTime} seconds");
} }
} }
} }
@ -249,10 +249,12 @@ namespace YooAsset
} }
} }
private readonly List<PatchBundle> _cacheList = new List<PatchBundle>(1000); private readonly List<PatchBundle> _waitingList = new List<PatchBundle>(1000);
private readonly List<PatchBundle> _verifyList = new List<PatchBundle>(100); private readonly List<PatchBundle> _verifyingList = new List<PatchBundle>(100);
private readonly ThreadSyncContext _syncContext = new ThreadSyncContext(); private readonly ThreadSyncContext _syncContext = new ThreadSyncContext();
private const int VerifyMaxCount = 32; private int _verifyMaxNum = 32;
private int _verifySuccessCount = 0;
private int _verifyFailCount = 0;
private void InitPrepareCache() private void InitPrepareCache()
{ {
@ -276,33 +278,38 @@ namespace YooAsset
if (File.Exists(filePath) == false) if (File.Exists(filePath) == false)
continue; continue;
_cacheList.Add(patchBundle); _waitingList.Add(patchBundle);
} }
// 设置同时验证的最大数
ThreadPool.GetMaxThreads(out int workerThreads, out int ioThreads);
YooLogger.Log($"Work threads : {workerThreads}, IO threads : {ioThreads}");
_verifyMaxNum = Math.Min(workerThreads, ioThreads);
} }
private bool UpdatePrepareCache() private bool UpdatePrepareCache()
{ {
_syncContext.Update(); _syncContext.Update();
if (_cacheList.Count == 0 && _verifyList.Count == 0) if (_waitingList.Count == 0 && _verifyingList.Count == 0)
return true; return true;
if (_verifyList.Count >= VerifyMaxCount) if (_verifyingList.Count >= _verifyMaxNum)
return false; return false;
for (int i = _cacheList.Count - 1; i >= 0; i--) for (int i = _waitingList.Count - 1; i >= 0; i--)
{ {
if (_verifyList.Count >= VerifyMaxCount) if (_verifyingList.Count >= _verifyMaxNum)
break; break;
var patchBundle = _cacheList[i]; var patchBundle = _waitingList[i];
if (RunThread(patchBundle)) if (RunThread(patchBundle))
{ {
_cacheList.RemoveAt(i); _waitingList.RemoveAt(i);
_verifyList.Add(patchBundle); _verifyingList.Add(patchBundle);
} }
else else
{ {
YooLogger.Warning("Failed to run verify thread."); YooLogger.Warning("The thread pool is failed queued.");
break; break;
} }
} }
@ -313,21 +320,38 @@ namespace YooAsset
{ {
string filePath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash); string filePath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
ThreadInfo info = new ThreadInfo(filePath, patchBundle); ThreadInfo info = new ThreadInfo(filePath, patchBundle);
return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyFile), info); return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), info);
} }
private void VerifyFile(object infoObj) private void VerifyInThread(object infoObj)
{ {
// 验证沙盒内的文件 // 验证沙盒内的文件
ThreadInfo info = (ThreadInfo)infoObj; ThreadInfo info = (ThreadInfo)infoObj;
info.Result = DownloadSystem.CheckContentIntegrity(info.FilePath, info.Bundle.SizeBytes, info.Bundle.CRC); try
{
info.Result = DownloadSystem.CheckContentIntegrity(info.FilePath, info.Bundle.SizeBytes, info.Bundle.CRC);
}
catch (Exception)
{
info.Result = false;
}
_syncContext.Post(VerifyCallback, info); _syncContext.Post(VerifyCallback, info);
} }
private void VerifyCallback(object obj) private void VerifyCallback(object obj)
{ {
ThreadInfo info = (ThreadInfo)obj; ThreadInfo info = (ThreadInfo)obj;
if (info.Result) if (info.Result)
{
_verifySuccessCount++;
DownloadSystem.CacheVerifyFile(info.Bundle.Hash, info.Bundle.BundleName); DownloadSystem.CacheVerifyFile(info.Bundle.Hash, info.Bundle.BundleName);
_verifyList.Remove(info.Bundle); }
else
{
_verifyFailCount++;
YooLogger.Warning($"Failed to verify file : {info.FilePath}");
if (File.Exists(info.FilePath))
File.Delete(info.FilePath);
}
_verifyingList.Remove(info.Bundle);
} }
#endregion #endregion
} }