mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
TEngine全面更新,升级YooAsset2.1.1、UniTask、UIWindow、I2Localization
TEngine全面更新,升级YooAsset2.1.1、UniTask、UIWindow、I2Localization
This commit is contained in:
@@ -5,106 +5,148 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public abstract class AsyncOperationBase : IEnumerator
|
||||
{
|
||||
// 用户请求的回调
|
||||
private Action<AsyncOperationBase> _callback;
|
||||
public abstract class AsyncOperationBase : IEnumerator, IComparable<AsyncOperationBase>
|
||||
{
|
||||
// 用户请求的回调
|
||||
private Action<AsyncOperationBase> _callback;
|
||||
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
public EOperationStatus Status { get; protected set; } = EOperationStatus.None;
|
||||
// 是否已经完成
|
||||
internal bool IsFinish = false;
|
||||
|
||||
/// <summary>
|
||||
/// 错误信息
|
||||
/// </summary>
|
||||
public string Error { get; protected set; }
|
||||
/// <summary>
|
||||
/// 所属包裹
|
||||
/// </summary>
|
||||
public string PackageName { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 处理进度
|
||||
/// </summary>
|
||||
public float Progress { get; protected set; }
|
||||
/// <summary>
|
||||
/// 优先级
|
||||
/// </summary>
|
||||
public uint Priority { set; get; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经完成
|
||||
/// </summary>
|
||||
public bool IsDone
|
||||
{
|
||||
get
|
||||
{
|
||||
return Status == EOperationStatus.Failed || Status == EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
public EOperationStatus Status { get; protected set; } = EOperationStatus.None;
|
||||
|
||||
/// <summary>
|
||||
/// 完成事件
|
||||
/// </summary>
|
||||
public event Action<AsyncOperationBase> Completed
|
||||
{
|
||||
add
|
||||
{
|
||||
if (IsDone)
|
||||
value.Invoke(this);
|
||||
else
|
||||
_callback += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
_callback -= value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 错误信息
|
||||
/// </summary>
|
||||
public string Error { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作任务
|
||||
/// </summary>
|
||||
public Task Task
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_taskCompletionSource == null)
|
||||
{
|
||||
_taskCompletionSource = new TaskCompletionSource<object>();
|
||||
if (IsDone)
|
||||
_taskCompletionSource.SetResult(null);
|
||||
}
|
||||
return _taskCompletionSource.Task;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 处理进度
|
||||
/// </summary>
|
||||
public float Progress { get; protected set; }
|
||||
|
||||
internal abstract void Start();
|
||||
internal abstract void Update();
|
||||
/// <summary>
|
||||
/// 是否已经完成
|
||||
/// </summary>
|
||||
public bool IsDone
|
||||
{
|
||||
get
|
||||
{
|
||||
return Status == EOperationStatus.Failed || Status == EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
|
||||
internal void SetFinish()
|
||||
{
|
||||
Progress = 1f;
|
||||
_callback?.Invoke(this); //注意:如果完成回调内发生异常,会导致Task无限期等待
|
||||
if (_taskCompletionSource != null)
|
||||
_taskCompletionSource.TrySetResult(null);
|
||||
}
|
||||
internal void SetStart()
|
||||
{
|
||||
Status = EOperationStatus.Processing;
|
||||
}
|
||||
/// <summary>
|
||||
/// 完成事件
|
||||
/// </summary>
|
||||
public event Action<AsyncOperationBase> Completed
|
||||
{
|
||||
add
|
||||
{
|
||||
if (IsDone)
|
||||
value.Invoke(this);
|
||||
else
|
||||
_callback += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
_callback -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空完成回调
|
||||
/// </summary>
|
||||
protected void ClearCompletedCallback()
|
||||
{
|
||||
_callback = null;
|
||||
}
|
||||
/// <summary>
|
||||
/// 异步操作任务
|
||||
/// </summary>
|
||||
public Task Task
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_taskCompletionSource == null)
|
||||
{
|
||||
_taskCompletionSource = new TaskCompletionSource<object>();
|
||||
if (IsDone)
|
||||
_taskCompletionSource.SetResult(null);
|
||||
}
|
||||
return _taskCompletionSource.Task;
|
||||
}
|
||||
}
|
||||
|
||||
#region 异步编程相关
|
||||
bool IEnumerator.MoveNext()
|
||||
{
|
||||
return !IsDone;
|
||||
}
|
||||
void IEnumerator.Reset()
|
||||
{
|
||||
}
|
||||
object IEnumerator.Current => null;
|
||||
internal abstract void InternalOnStart();
|
||||
internal abstract void InternalOnUpdate();
|
||||
internal virtual void InternalOnAbort() { }
|
||||
|
||||
private TaskCompletionSource<object> _taskCompletionSource;
|
||||
#endregion
|
||||
}
|
||||
internal void SetPackageName(string packageName)
|
||||
{
|
||||
PackageName = packageName;
|
||||
}
|
||||
internal void SetStart()
|
||||
{
|
||||
Status = EOperationStatus.Processing;
|
||||
InternalOnStart();
|
||||
}
|
||||
internal void SetFinish()
|
||||
{
|
||||
IsFinish = true;
|
||||
|
||||
// 进度百分百完成
|
||||
Progress = 1f;
|
||||
|
||||
//注意:如果完成回调内发生异常,会导致Task无限期等待
|
||||
_callback?.Invoke(this);
|
||||
|
||||
if (_taskCompletionSource != null)
|
||||
_taskCompletionSource.TrySetResult(null);
|
||||
}
|
||||
internal void SetAbort()
|
||||
{
|
||||
if (IsDone == false)
|
||||
{
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = "user abort";
|
||||
YooLogger.Warning($"Async operaiton has been abort : {this.GetType().Name}");
|
||||
InternalOnAbort();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空完成回调
|
||||
/// </summary>
|
||||
protected void ClearCompletedCallback()
|
||||
{
|
||||
_callback = null;
|
||||
}
|
||||
|
||||
#region 排序接口实现
|
||||
public int CompareTo(AsyncOperationBase other)
|
||||
{
|
||||
return other.Priority.CompareTo(this.Priority);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 异步编程相关
|
||||
bool IEnumerator.MoveNext()
|
||||
{
|
||||
return !IsDone;
|
||||
}
|
||||
void IEnumerator.Reset()
|
||||
{
|
||||
}
|
||||
object IEnumerator.Current => null;
|
||||
|
||||
private TaskCompletionSource<object> _taskCompletionSource;
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -1,11 +1,11 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public enum EOperationStatus
|
||||
{
|
||||
None,
|
||||
Processing,
|
||||
Succeed,
|
||||
Failed
|
||||
}
|
||||
public enum EOperationStatus
|
||||
{
|
||||
None,
|
||||
Processing,
|
||||
Succeed,
|
||||
Failed
|
||||
}
|
||||
}
|
@@ -1,33 +1,42 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public abstract class GameAsyncOperation : AsyncOperationBase
|
||||
{
|
||||
internal override void Start()
|
||||
{
|
||||
OnStart();
|
||||
}
|
||||
internal override void Update()
|
||||
{
|
||||
OnUpdate();
|
||||
}
|
||||
public abstract class GameAsyncOperation : AsyncOperationBase
|
||||
{
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
OnStart();
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
OnUpdate();
|
||||
}
|
||||
internal override void InternalOnAbort()
|
||||
{
|
||||
OnAbort();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作开始
|
||||
/// </summary>
|
||||
protected abstract void OnStart();
|
||||
/// <summary>
|
||||
/// 异步操作开始
|
||||
/// </summary>
|
||||
protected abstract void OnStart();
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作更新
|
||||
/// </summary>
|
||||
protected abstract void OnUpdate();
|
||||
/// <summary>
|
||||
/// 异步操作更新
|
||||
/// </summary>
|
||||
protected abstract void OnUpdate();
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作系统是否繁忙
|
||||
/// </summary>
|
||||
protected bool IsBusy()
|
||||
{
|
||||
return OperationSystem.IsBusy;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 异步操作终止
|
||||
/// </summary>
|
||||
protected abstract void OnAbort();
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作系统是否繁忙
|
||||
/// </summary>
|
||||
protected bool IsBusy()
|
||||
{
|
||||
return OperationSystem.IsBusy;
|
||||
}
|
||||
}
|
||||
}
|
@@ -4,90 +4,147 @@ using System.Diagnostics;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class OperationSystem
|
||||
{
|
||||
private static readonly List<AsyncOperationBase> _operations = new List<AsyncOperationBase>(100);
|
||||
private static readonly List<AsyncOperationBase> _newList = new List<AsyncOperationBase>(100);
|
||||
internal class OperationSystem
|
||||
{
|
||||
private static readonly List<AsyncOperationBase> _operations = new List<AsyncOperationBase>(1000);
|
||||
private static readonly List<AsyncOperationBase> _newList = new List<AsyncOperationBase>(1000);
|
||||
|
||||
// 计时器相关
|
||||
private static Stopwatch _watch;
|
||||
private static long _frameTime;
|
||||
// 计时器相关
|
||||
private static Stopwatch _watch;
|
||||
private static long _frameTime;
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作的最小时间片段
|
||||
/// </summary>
|
||||
public static long MaxTimeSlice { set; get; } = long.MaxValue;
|
||||
/// <summary>
|
||||
/// 异步操作的最小时间片段
|
||||
/// </summary>
|
||||
public static long MaxTimeSlice { set; get; } = long.MaxValue;
|
||||
|
||||
/// <summary>
|
||||
/// 处理器是否繁忙
|
||||
/// </summary>
|
||||
public static bool IsBusy
|
||||
{
|
||||
get
|
||||
{
|
||||
return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 处理器是否繁忙
|
||||
/// </summary>
|
||||
public static bool IsBusy
|
||||
{
|
||||
get
|
||||
{
|
||||
return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 初始化异步操作系统
|
||||
/// </summary>
|
||||
public static void Initialize()
|
||||
{
|
||||
_watch = Stopwatch.StartNew();
|
||||
}
|
||||
/// <summary>
|
||||
/// 初始化异步操作系统
|
||||
/// </summary>
|
||||
public static void Initialize()
|
||||
{
|
||||
_watch = Stopwatch.StartNew();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新异步操作系统
|
||||
/// </summary>
|
||||
public static void Update()
|
||||
{
|
||||
_frameTime = _watch.ElapsedMilliseconds;
|
||||
/// <summary>
|
||||
/// 更新异步操作系统
|
||||
/// </summary>
|
||||
public static void Update()
|
||||
{
|
||||
_frameTime = _watch.ElapsedMilliseconds;
|
||||
|
||||
// 添加新的异步操作
|
||||
if (_newList.Count > 0)
|
||||
{
|
||||
_operations.AddRange(_newList);
|
||||
_newList.Clear();
|
||||
}
|
||||
// 添加新增的异步操作
|
||||
if (_newList.Count > 0)
|
||||
{
|
||||
bool sorting = false;
|
||||
foreach (var operation in _newList)
|
||||
{
|
||||
if (operation.Priority > 0)
|
||||
{
|
||||
sorting = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新所有的异步操作
|
||||
for (int i = _operations.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (IsBusy)
|
||||
break;
|
||||
_operations.AddRange(_newList);
|
||||
_newList.Clear();
|
||||
|
||||
var operation = _operations[i];
|
||||
operation.Update();
|
||||
if (operation.IsDone)
|
||||
{
|
||||
_operations.RemoveAt(i);
|
||||
operation.SetFinish(); //注意:如果业务端发生异常,保证异步操作提前移除。
|
||||
}
|
||||
}
|
||||
}
|
||||
// 重新排序优先级
|
||||
if (sorting)
|
||||
_operations.Sort();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁异步操作系统
|
||||
/// </summary>
|
||||
public static void DestroyAll()
|
||||
{
|
||||
_operations.Clear();
|
||||
_newList.Clear();
|
||||
_watch = null;
|
||||
_frameTime = 0;
|
||||
MaxTimeSlice = long.MaxValue;
|
||||
}
|
||||
// 更新进行中的异步操作
|
||||
for (int i = 0; i < _operations.Count; i++)
|
||||
{
|
||||
if (IsBusy)
|
||||
break;
|
||||
|
||||
/// <summary>
|
||||
/// 开始处理异步操作类
|
||||
/// </summary>
|
||||
public static void StartOperation(AsyncOperationBase operation)
|
||||
{
|
||||
_newList.Add(operation);
|
||||
operation.SetStart();
|
||||
operation.Start();
|
||||
}
|
||||
}
|
||||
var operation = _operations[i];
|
||||
if (operation.IsFinish)
|
||||
continue;
|
||||
|
||||
if (operation.IsDone == false)
|
||||
operation.InternalOnUpdate();
|
||||
|
||||
if (operation.IsDone)
|
||||
operation.SetFinish();
|
||||
}
|
||||
|
||||
// 移除已经完成的异步操作
|
||||
for (int i = _operations.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var operation = _operations[i];
|
||||
if (operation.IsFinish)
|
||||
_operations.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁异步操作系统
|
||||
/// </summary>
|
||||
public static void DestroyAll()
|
||||
{
|
||||
_operations.Clear();
|
||||
_newList.Clear();
|
||||
_watch = null;
|
||||
_frameTime = 0;
|
||||
MaxTimeSlice = long.MaxValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁包裹的所有任务
|
||||
/// </summary>
|
||||
public static void ClearPackageOperation(string packageName)
|
||||
{
|
||||
// 终止临时队列里的任务
|
||||
foreach (var operation in _newList)
|
||||
{
|
||||
if (operation.PackageName == packageName)
|
||||
{
|
||||
operation.SetAbort();
|
||||
}
|
||||
}
|
||||
|
||||
// 终止正在进行的任务
|
||||
foreach (var operation in _operations)
|
||||
{
|
||||
if (operation.PackageName == packageName)
|
||||
{
|
||||
operation.SetAbort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始处理异步操作类
|
||||
/// </summary>
|
||||
public static void StartOperation(string packageName, AsyncOperationBase operation)
|
||||
{
|
||||
_newList.Add(operation);
|
||||
operation.SetPackageName(packageName);
|
||||
operation.SetStart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始处理异步操作类
|
||||
/// </summary>
|
||||
public static void StartOperation(AsyncOperationBase operation)
|
||||
{
|
||||
_newList.Add(operation);
|
||||
operation.SetStart();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user