update cache system

移除ThreadSyncContext类
pull/455/head
何冠峰 2024-12-31 10:16:20 +08:00
parent 51b688bbdd
commit 769134eaf9
5 changed files with 24 additions and 69 deletions

View File

@ -10,10 +10,14 @@ namespace YooAsset
public string DataFilePath { private set; get; }
public string InfoFilePath { private set; get; }
public EFileVerifyResult Result;
public string DataFileCRC;
public long DataFileSize;
/// <summary>
/// 注意:原子操作对象
/// </summary>
public int Result = 0;
public CacheFileElement(string packageName, string bundleGUID, string fileRootPath, string dataFilePath, string infoFilePath)
{
PackageName = packageName;

View File

@ -19,7 +19,6 @@ namespace YooAsset
Done,
}
private readonly ThreadSyncContext _syncContext = new ThreadSyncContext();
private readonly ICacheSystem _cacheSystem;
private readonly EFileVerifyLevel _verifyLevel;
private List<CacheFileElement> _waitingList;
@ -66,7 +65,17 @@ namespace YooAsset
if (_steps == ESteps.UpdateVerify)
{
_syncContext.Update();
// 检测校验结果
for (int i = _verifyingList.Count - 1; i >= 0; i--)
{
var verifyElement = _verifyingList[i];
int result = verifyElement.Result;
if (result != 0)
{
_verifyingList.Remove(verifyElement);
RecordVerifyFile(verifyElement);
}
}
Progress = GetProgress();
if (_waitingList.Count == 0 && _verifyingList.Count == 0)
@ -86,7 +95,8 @@ namespace YooAsset
break;
var element = _waitingList[i];
if (BeginVerifyFileWithThread(element))
bool succeed = ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), element);
if (succeed)
{
_waitingList.RemoveAt(i);
_verifyingList.Add(element);
@ -106,22 +116,15 @@ namespace YooAsset
return 1f;
return (float)(_succeedCount + _failedCount) / _verifyTotalCount;
}
private bool BeginVerifyFileWithThread(CacheFileElement element)
{
return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), element);
}
private void VerifyInThread(object obj)
{
CacheFileElement element = (CacheFileElement)obj;
element.Result = VerifyingCacheFile(element, _verifyLevel);
_syncContext.Post(VerifyCallback, element);
int verifyResult = (int)VerifyingCacheFile(element, _verifyLevel);
element.Result = verifyResult;
}
private void VerifyCallback(object obj)
private void RecordVerifyFile(CacheFileElement element)
{
CacheFileElement element = (CacheFileElement)obj;
_verifyingList.Remove(element);
if (element.Result == EFileVerifyResult.Succeed)
if (element.Result == (int)EFileVerifyResult.Succeed)
{
_succeedCount++;
var fileWrapper = new CacheWrapper(element.InfoFilePath, element.DataFilePath, element.DataFileCRC, element.DataFileSize);

View File

@ -40,7 +40,8 @@ namespace YooAsset
if (_steps == ESteps.VerifyFile)
{
if (BeginVerifyFileWithThread(_element))
bool succeed = ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), _element);
if (succeed)
{
_steps = ESteps.Waiting;
}
@ -77,10 +78,6 @@ namespace YooAsset
}
}
private bool BeginVerifyFileWithThread(TempFileElement element)
{
return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), element);
}
private void VerifyInThread(object obj)
{
TempFileElement element = (TempFileElement)obj;

View File

@ -1,38 +0,0 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Threading;
namespace YooAsset
{
/// <summary>
/// 同步其它线程里的回调到主线程里
/// 注意Unity3D中需要设置Scripting Runtime Version为.NET4.6
/// </summary>
internal sealed class ThreadSyncContext : SynchronizationContext
{
private readonly ConcurrentQueue<Action> _safeQueue = new ConcurrentQueue<Action>();
/// <summary>
/// 更新同步队列
/// </summary>
public void Update()
{
while (true)
{
if (_safeQueue.TryDequeue(out Action action) == false)
return;
action.Invoke();
}
}
/// <summary>
/// 向同步队列里投递一个回调方法
/// </summary>
public override void Post(SendOrPostCallback callback, object state)
{
Action action = new Action(() => { callback(state); });
_safeQueue.Enqueue(action);
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 1c8ce2c52a3e9964fa50a9c031e4e593
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: