8
Assets/Scripts/NBC/Runtime/Core/FTask/Builder.meta
Normal file
8
Assets/Scripts/NBC/Runtime/Core/FTask/Builder.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8882af7b2bcd247b79a45e5a7f166996
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using System.Runtime.InteropServices;
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
public struct AsyncFTaskCompletedMethodBuilder
|
||||
{
|
||||
public FTaskCompleted Task => default;
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static AsyncFTaskCompletedMethodBuilder Create()
|
||||
{
|
||||
return new AsyncFTaskCompletedMethodBuilder();
|
||||
}
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
stateMachine.MoveNext();
|
||||
}
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetException(Exception exception)
|
||||
{
|
||||
ExceptionDispatchInfo.Capture(exception).Throw();
|
||||
}
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetResult() { }
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
awaiter.OnCompleted(stateMachine.MoveNext);
|
||||
}
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
awaiter.UnsafeOnCompleted(stateMachine.MoveNext);
|
||||
}
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetStateMachine(IAsyncStateMachine stateMachine) { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8444248debce04e9c82848c9649bf183
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,140 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
#pragma warning disable CS8604 // Possible null reference argument.
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
public readonly struct AsyncFTaskMethodBuilder
|
||||
{
|
||||
public FTask Task
|
||||
{
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static AsyncFTaskMethodBuilder Create()
|
||||
{
|
||||
return new AsyncFTaskMethodBuilder(FTask.Create());
|
||||
}
|
||||
|
||||
public AsyncFTaskMethodBuilder(FTask fTask)
|
||||
{
|
||||
Task = fTask;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
stateMachine.MoveNext();
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetResult()
|
||||
{
|
||||
Task.SetResult();
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetException(Exception exception)
|
||||
{
|
||||
Task.SetException(exception);
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
// 通常在异步方法中遇到 await 关键字时调用,并且需要将执行恢复到调用 await 之前的同步上下文。
|
||||
awaiter.OnCompleted(stateMachine.MoveNext);
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
// 通常在你不需要恢复到原始同步上下文时调用,这意味着你不关心在什么线程上恢复执行。
|
||||
awaiter.UnsafeOnCompleted(stateMachine.MoveNext);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetStateMachine(IAsyncStateMachine stateMachine)
|
||||
{
|
||||
// 用于设置和保存异步方法的状态机实例。
|
||||
// 编译器在生成异步方法时要求其存在。
|
||||
// 编译器生成的代码已经足够处理状态机的管理,所以这里没有什么特殊要求所以保持空实现。
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
public readonly struct AsyncFTaskMethodBuilder<T>
|
||||
{
|
||||
public FTask<T> Task
|
||||
{
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static AsyncFTaskMethodBuilder<T> Create()
|
||||
{
|
||||
return new AsyncFTaskMethodBuilder<T>(FTask<T>.Create());
|
||||
}
|
||||
|
||||
public AsyncFTaskMethodBuilder(FTask<T> fTask)
|
||||
{
|
||||
Task = fTask;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
stateMachine.MoveNext();
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetResult(T value)
|
||||
{
|
||||
Task.SetResult(value);
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetException(Exception exception)
|
||||
{
|
||||
Task.SetException(exception);
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
awaiter.OnCompleted(stateMachine.MoveNext);
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
awaiter.UnsafeOnCompleted(stateMachine.MoveNext);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetStateMachine(IAsyncStateMachine stateMachine) { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fd60976f4009a44fd9171c750809cbf1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using System.Runtime.InteropServices;
|
||||
#pragma warning disable CS8603 // Possible null reference return.
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
internal struct AsyncFVoidMethodBuilder
|
||||
{
|
||||
public FVoid Task
|
||||
{
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => default;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static AsyncFVoidMethodBuilder Create()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
stateMachine.MoveNext();
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetResult() { }
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetException(Exception exception)
|
||||
{
|
||||
ExceptionDispatchInfo.Capture(exception).Throw();
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
awaiter.OnCompleted(stateMachine.MoveNext);
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
|
||||
{
|
||||
awaiter.UnsafeOnCompleted(stateMachine.MoveNext);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetStateMachine(IAsyncStateMachine stateMachine) { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 79d8b6cd8361948c8a19fe4722544c53
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed149491fa2ce43ef9ed42f42d24a71e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
/// <summary>
|
||||
/// 用于FTask取消的CancellationToken
|
||||
/// </summary>
|
||||
public sealed class FCancellationToken : IDisposable
|
||||
{
|
||||
private bool _isDispose;
|
||||
private bool _isCancel;
|
||||
private readonly HashSet<Action> _actions = new HashSet<Action>();
|
||||
/// <summary>
|
||||
/// 当前CancellationToken是否已经取消过了
|
||||
/// </summary>
|
||||
public bool IsCancel => _isDispose || _isCancel;
|
||||
/// <summary>
|
||||
/// 添加一个取消要执行的Action
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
public void Add(Action action)
|
||||
{
|
||||
if (_isDispose)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_actions.Add(action);
|
||||
}
|
||||
/// <summary>
|
||||
/// 移除一个取消要执行的Action
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
public void Remove(Action action)
|
||||
{
|
||||
if (_isDispose)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_actions.Remove(action);
|
||||
}
|
||||
/// <summary>
|
||||
/// 取消CancellationToken
|
||||
/// </summary>
|
||||
public void Cancel()
|
||||
{
|
||||
if (IsCancel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isCancel = true;
|
||||
|
||||
foreach (var action in _actions)
|
||||
{
|
||||
try
|
||||
{
|
||||
action.Invoke();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
_actions.Clear();
|
||||
}
|
||||
/// <summary>
|
||||
/// 销毁掉CancellationToken,会执行Cancel方法。
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_isDispose)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!IsCancel)
|
||||
{
|
||||
Cancel();
|
||||
_isCancel = true;
|
||||
}
|
||||
|
||||
_isDispose = true;
|
||||
|
||||
if (Caches.Count > 2000)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Caches.Enqueue(this);
|
||||
}
|
||||
|
||||
#region Static
|
||||
|
||||
private static readonly ConcurrentQueue<FCancellationToken> Caches = new ConcurrentQueue<FCancellationToken>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个新的CancellationToken
|
||||
/// </summary>
|
||||
public static FCancellationToken ToKen
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!Caches.TryDequeue(out var fCancellationToken))
|
||||
{
|
||||
fCancellationToken = new FCancellationToken();
|
||||
}
|
||||
|
||||
fCancellationToken._isCancel = false;
|
||||
fCancellationToken._isDispose = false;
|
||||
return fCancellationToken;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 44bc4f64d35cb4ead9af1b57d36250d5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8cb2da0c1fd8f4b66912dd23ae2c0b62
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,115 @@
|
||||
#if !FANTASY_WEBGL
|
||||
using System.Collections.Concurrent;
|
||||
#endif
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
/// <summary>
|
||||
/// 一个异步任务
|
||||
/// </summary>
|
||||
public partial class FTask
|
||||
{
|
||||
private bool _isPool;
|
||||
#if FANTASY_WEBGL
|
||||
private static readonly Queue<FTask> Caches = new Queue<FTask>();
|
||||
#else
|
||||
private static readonly ConcurrentQueue<FTask> Caches = new ConcurrentQueue<FTask>();
|
||||
#endif
|
||||
/// <summary>
|
||||
/// 创建一个空的任务
|
||||
/// </summary>
|
||||
public static FTaskCompleted CompletedTask => new FTaskCompleted();
|
||||
|
||||
private FTask() { }
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个任务
|
||||
/// </summary>
|
||||
/// <param name="isPool">是否从对象池中创建</param>
|
||||
/// <returns></returns>
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static FTask Create(bool isPool = true)
|
||||
{
|
||||
if (!isPool)
|
||||
{
|
||||
return new FTask();
|
||||
}
|
||||
|
||||
if (!Caches.TryDequeue(out var fTask))
|
||||
{
|
||||
fTask = new FTask();
|
||||
}
|
||||
|
||||
fTask._isPool = true;
|
||||
return fTask;
|
||||
}
|
||||
|
||||
private void Return()
|
||||
{
|
||||
if (!_isPool || Caches.Count > 2000)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_callBack = null;
|
||||
_status = STaskStatus.Pending;
|
||||
Caches.Enqueue(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 一个异步任务
|
||||
/// </summary>
|
||||
/// <typeparam name="T">任务的泛型类型</typeparam>
|
||||
public partial class FTask<T>
|
||||
{
|
||||
private bool _isPool;
|
||||
#if FANTASY_WEBGL
|
||||
private static readonly Queue<FTask<T>> Caches = new Queue<FTask<T>>();
|
||||
#else
|
||||
private static readonly ConcurrentQueue<FTask<T>> Caches = new ConcurrentQueue<FTask<T>>();
|
||||
#endif
|
||||
/// <summary>
|
||||
/// 创建一个任务
|
||||
/// </summary>
|
||||
/// <param name="isPool">是否从对象池中创建</param>
|
||||
/// <returns></returns>
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static FTask<T> Create(bool isPool = true)
|
||||
{
|
||||
if (!isPool)
|
||||
{
|
||||
return new FTask<T>();
|
||||
}
|
||||
|
||||
if (!Caches.TryDequeue(out var fTask))
|
||||
{
|
||||
fTask = new FTask<T>();
|
||||
}
|
||||
|
||||
fTask._isPool = true;
|
||||
return fTask;
|
||||
}
|
||||
|
||||
private FTask() { }
|
||||
|
||||
private void Return()
|
||||
{
|
||||
if (!_isPool || Caches.Count > 2000)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_callBack = null;
|
||||
_status = STaskStatus.Pending;
|
||||
Caches.Enqueue(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 206d4fc3ee71b4059a08781f251ceb46
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,344 @@
|
||||
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
namespace NBC
|
||||
{
|
||||
public partial class FTask
|
||||
{
|
||||
#region NetTimer
|
||||
|
||||
/// <summary>
|
||||
/// 异步等待指定时间
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public static FTask<bool> Wait(Scene scene, long time, FCancellationToken cancellationToken = null)
|
||||
{
|
||||
return scene.TimerComponent.Net.WaitAsync(time, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步等待直到指定时间
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public static FTask<bool> WaitTill(Scene scene, long time, FCancellationToken cancellationToken = null)
|
||||
{
|
||||
return scene.TimerComponent.Net.WaitTillAsync(time, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步等待一帧时间
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <returns></returns>
|
||||
public static FTask WaitFrame(Scene scene)
|
||||
{
|
||||
return scene.TimerComponent.Net.WaitFrameAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个只执行一次的计时器,直到指定时间
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
public static long OnceTimer(Scene scene, long time, Action action)
|
||||
{
|
||||
return scene.TimerComponent.Net.OnceTimer(time, action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个只执行一次的计时器,直到指定时间。
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
public static long OnceTillTimer(Scene scene, long time, Action action)
|
||||
{
|
||||
return scene.TimerComponent.Net.OnceTillTimer(time, action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个只执行一次的计时器,用于发布指定类型的事件。
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="timerHandlerType"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static long OnceTimer<T>(Scene scene, long time, T timerHandlerType) where T : struct
|
||||
{
|
||||
return scene.TimerComponent.Net.OnceTimer<T>(time, timerHandlerType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个只执行一次的计时器,直到指定时间,用于发布指定类型的事件。
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="tillTime"></param>
|
||||
/// <param name="timerHandlerType"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static long OnceTillTimer<T>(Scene scene, long tillTime, T timerHandlerType) where T : struct
|
||||
{
|
||||
return scene.TimerComponent.Net.OnceTillTimer<T>(tillTime, timerHandlerType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个重复执行的计时器。
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
public static long RepeatedTimer(Scene scene, long time, Action action)
|
||||
{
|
||||
return scene.TimerComponent.Net.RepeatedTimer(time, action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个重复执行的计时器,用于发布指定类型的事件。
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="timerHandlerType"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static long RepeatedTimer<T>(Scene scene, long time, T timerHandlerType) where T : struct
|
||||
{
|
||||
return scene.TimerComponent.Net.RepeatedTimer<T>(time, timerHandlerType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除指定 ID 的计时器。
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="timerId"></param>
|
||||
/// <returns></returns>
|
||||
public static bool RemoveTimer(Scene scene, ref long timerId)
|
||||
{
|
||||
return scene.TimerComponent.Net.Remove(ref timerId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity
|
||||
|
||||
/// <summary>
|
||||
/// 异步等待指定时间。(使用Unity的Time时间)
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public static FTask<bool> UnityWait(Scene scene, long time, FCancellationToken cancellationToken = null)
|
||||
{
|
||||
return scene.TimerComponent.Unity.WaitAsync(time, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步等待直到指定时间。(使用Unity的Time时间)
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public static FTask<bool> UnityWaitTill(Scene scene, long time, FCancellationToken cancellationToken = null)
|
||||
{
|
||||
return scene.TimerComponent.Unity.WaitTillAsync(time, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步等待一帧时间。(使用Unity的Time时间)
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <returns></returns>
|
||||
public static FTask UnityWaitFrame(Scene scene)
|
||||
{
|
||||
return scene.TimerComponent.Unity.WaitFrameAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个只执行一次的计时器,直到指定时间。(使用Unity的Time时间)
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
public static long UnityOnceTimer(Scene scene, long time, Action action)
|
||||
{
|
||||
return scene.TimerComponent.Unity.OnceTimer(time, action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个只执行一次的计时器,直到指定时间。(使用Unity的Time时间)
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
public static long UnityOnceTillTimer(Scene scene, long time, Action action)
|
||||
{
|
||||
return scene.TimerComponent.Unity.OnceTillTimer(time, action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个只执行一次的计时器,用于发布指定类型的事件。(使用Unity的Time时间)
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="timerHandlerType"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static long UnityOnceTimer<T>(Scene scene, long time, T timerHandlerType) where T : struct
|
||||
{
|
||||
return scene.TimerComponent.Unity.OnceTimer<T>(time, timerHandlerType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个只执行一次的计时器,直到指定时间,用于发布指定类型的事件。(使用Unity的Time时间)
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="tillTime"></param>
|
||||
/// <param name="timerHandlerType"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static long UnityOnceTillTimer<T>(Scene scene, long tillTime, T timerHandlerType) where T : struct
|
||||
{
|
||||
return scene.TimerComponent.Unity.OnceTillTimer<T>(tillTime, timerHandlerType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个重复执行的计时器。(使用Unity的Time时间)
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
public static long UnityRepeatedTimer(Scene scene, long time, Action action)
|
||||
{
|
||||
return scene.TimerComponent.Unity.RepeatedTimer(time, action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个重复执行的计时器,用于发布指定类型的事件。(使用Unity的Time时间)
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="timerHandlerType"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static long UnityRepeatedTimer<T>(Scene scene, long time, T timerHandlerType) where T : struct
|
||||
{
|
||||
return scene.TimerComponent.Unity.RepeatedTimer<T>(time, timerHandlerType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除指定 ID 的计时器。(使用Unity的Time时间)
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="timerId"></param>
|
||||
/// <returns></returns>
|
||||
public static bool UnityRemoveTimer(Scene scene, ref long timerId)
|
||||
{
|
||||
return scene.TimerComponent.Unity.Remove(ref timerId);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 创建并运行一个异步任务
|
||||
/// </summary>
|
||||
/// <param name="factory"></param>
|
||||
/// <returns></returns>
|
||||
public static FTask Run(Func<FTask> factory)
|
||||
{
|
||||
return factory();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建并运行一个带有结果的异步任务
|
||||
/// </summary>
|
||||
/// <param name="factory"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static FTask<T> Run<T>(Func<FTask<T>> factory)
|
||||
{
|
||||
return factory();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待所有任务完成
|
||||
/// </summary>
|
||||
/// <param name="tasks"></param>
|
||||
public static async FTask WaitAll(List<FTask> tasks)
|
||||
{
|
||||
if (tasks.Count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var count = tasks.Count;
|
||||
var sTaskCompletionSource = Create();
|
||||
|
||||
foreach (var task in tasks)
|
||||
{
|
||||
RunSTask(task).Coroutine();
|
||||
}
|
||||
|
||||
await sTaskCompletionSource;
|
||||
|
||||
async FVoid RunSTask(FTask task)
|
||||
{
|
||||
await task;
|
||||
count--;
|
||||
if (count <= 0)
|
||||
{
|
||||
sTaskCompletionSource.SetResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 等待其中一个任务完成
|
||||
/// </summary>
|
||||
/// <param name="tasks"></param>
|
||||
public static async FTask WaitAny(List<FTask> tasks)
|
||||
{
|
||||
if (tasks.Count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var count = 1;
|
||||
var sTaskCompletionSource = Create();
|
||||
|
||||
foreach (var task in tasks)
|
||||
{
|
||||
RunSTask(task).Coroutine();
|
||||
}
|
||||
|
||||
await sTaskCompletionSource;
|
||||
|
||||
async FVoid RunSTask(FTask task)
|
||||
{
|
||||
await task;
|
||||
count--;
|
||||
if (count == 0)
|
||||
{
|
||||
sTaskCompletionSource.SetResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 049fb897477f44b90ab54db418990e90
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
3
Assets/Scripts/NBC/Runtime/Core/FTask/NTask.meta
Normal file
3
Assets/Scripts/NBC/Runtime/Core/FTask/NTask.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 611da49ee53645f5baed0faff24cbe33
|
||||
timeCreated: 1751421490
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d069cf2e375b8a74ebab6f3df1f74a91
|
||||
timeCreated: 1656996254
|
||||
@@ -0,0 +1,111 @@
|
||||
namespace NBC
|
||||
{
|
||||
public class ParallelTaskCollection : TaskCollection
|
||||
{
|
||||
private int _currentIndex = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 最大并行数量 (默认为9)
|
||||
/// </summary>
|
||||
public int ParallelNum = 9;
|
||||
|
||||
protected override NTaskStatus RunTasksAndCheckIfDone()
|
||||
{
|
||||
var st = NTaskStatus.Running;
|
||||
|
||||
if (CurRunTask.Count < ParallelNum && _currentIndex < RawList.Count)
|
||||
{
|
||||
var num = ParallelNum - CurRunTask.Count;
|
||||
for (var index = 0; index < num; index++)
|
||||
{
|
||||
if (_currentIndex < RawList.Count)
|
||||
{
|
||||
CurRunTask.Add(RawList[_currentIndex]);
|
||||
_currentIndex += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var index = 0; index < CurrentTask.Count; index++)
|
||||
{
|
||||
var element = CurrentTask[index];
|
||||
var childSt = element.Process();
|
||||
|
||||
if (FailBreak && childSt == NTaskStatus.Fail)
|
||||
{
|
||||
_errorMsg = element.ErrorMsg;
|
||||
st = NTaskStatus.Fail;
|
||||
break;
|
||||
}
|
||||
|
||||
if (childSt == NTaskStatus.Success || childSt == NTaskStatus.Fail)
|
||||
{
|
||||
CurrentTask.RemoveAt(index);
|
||||
index--;
|
||||
FinishList.Add(element);
|
||||
}
|
||||
}
|
||||
|
||||
if (FinishList.Count >= RawList.Count)
|
||||
{
|
||||
st = NTaskStatus.Success;
|
||||
}
|
||||
|
||||
// for (var index = 0; index < CurrentTask.Count; index++)
|
||||
// {
|
||||
// var element = CurrentTask[index];
|
||||
// var childSt = element.Process();
|
||||
//
|
||||
// if (childSt >= Status.Success)
|
||||
// {
|
||||
// if (FailBreak && childSt == Status.Fail)
|
||||
// {
|
||||
// _errorMsg = element.ErrorMsg;
|
||||
// st = Status.Fail;
|
||||
// }
|
||||
//
|
||||
// CurrentTask.RemoveAt(index);
|
||||
// index--;
|
||||
// FinishList.Add(element);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (FinishList.Count >= RawList.Count)
|
||||
// {
|
||||
// st = Status.Success;
|
||||
// }
|
||||
// else if (CurRunTask.Count < ParallelNum && _currentIndex < RawList.Count)
|
||||
// {
|
||||
// var num = ParallelNum - CurRunTask.Count;
|
||||
// for (var index = 0; index < num; index++)
|
||||
// {
|
||||
// if (_currentIndex < RawList.Count)
|
||||
// {
|
||||
// CurRunTask.Add(RawList[_currentIndex]);
|
||||
// _currentIndex += 1;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
return st;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
_currentIndex = 0;
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
{
|
||||
base.Stop();
|
||||
_currentIndex = 0;
|
||||
}
|
||||
|
||||
public override void Clear()
|
||||
{
|
||||
base.Clear();
|
||||
_currentIndex = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 33f78136d6a7de14688a6d3ed6903557
|
||||
timeCreated: 1657002644
|
||||
@@ -0,0 +1,119 @@
|
||||
namespace NBC
|
||||
{
|
||||
public class SequenceTaskCollection : TaskCollection
|
||||
{
|
||||
private int _currentIndex;
|
||||
|
||||
public override string Info
|
||||
{
|
||||
get
|
||||
{
|
||||
if (CurrentTask != null && CurrentTask.Count > 0)
|
||||
{
|
||||
return CurrentTask[0].Info;
|
||||
}
|
||||
|
||||
return TaskInfo;
|
||||
}
|
||||
}
|
||||
|
||||
protected override NTaskStatus RunTasksAndCheckIfDone()
|
||||
{
|
||||
var st = NTaskStatus.Running;
|
||||
|
||||
if (RawList.Count > 0)
|
||||
{
|
||||
if (CurRunTask.Count == 0 && _currentIndex < RawList.Count)
|
||||
{
|
||||
CurRunTask.Add(RawList[_currentIndex]);
|
||||
}
|
||||
|
||||
NTaskStatus curSt;
|
||||
do
|
||||
{
|
||||
var childTask = CurRunTask[0];
|
||||
curSt = childTask.Process();
|
||||
if (curSt >= NTaskStatus.Success)
|
||||
{
|
||||
if (FailBreak && curSt == NTaskStatus.Fail)
|
||||
{
|
||||
_errorMsg = childTask.ErrorMsg;
|
||||
st = NTaskStatus.Fail;
|
||||
break;
|
||||
}
|
||||
|
||||
FinishList.Add(childTask);
|
||||
CurRunTask.RemoveAt(0);
|
||||
_currentIndex++;
|
||||
if (_currentIndex < RawList.Count)
|
||||
CurRunTask.Add(RawList[_currentIndex]);
|
||||
}
|
||||
} while (curSt >= NTaskStatus.Success && CurRunTask.Count > 0);
|
||||
|
||||
if (FinishList.Count == RawList.Count)
|
||||
st = NTaskStatus.Success;
|
||||
}
|
||||
else
|
||||
{
|
||||
st = NTaskStatus.Success;
|
||||
}
|
||||
|
||||
|
||||
// if (RawList.Count > 0)
|
||||
// {
|
||||
// if (FinishList.Count == RawList.Count)
|
||||
// {
|
||||
// st = Status.Success;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (CurRunTask.Count == 0)
|
||||
// {
|
||||
// CurRunTask.Add(RawList[_currentIndex]);
|
||||
// }
|
||||
//
|
||||
// var childTask = CurRunTask[0];
|
||||
// var curSt = childTask.Process();
|
||||
//
|
||||
//
|
||||
// if (curSt >= Status.Success)
|
||||
// {
|
||||
// if (FailBreak && curSt == Status.Fail)
|
||||
// {
|
||||
// _errorMsg = childTask.ErrorMsg;
|
||||
// st = Status.Fail;
|
||||
// }
|
||||
//
|
||||
// FinishList.Add(childTask);
|
||||
// CurRunTask.RemoveAt(0);
|
||||
// _currentIndex++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// st = Status.Success;
|
||||
// }
|
||||
|
||||
return st;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
_currentIndex = 0;
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
{
|
||||
base.Stop();
|
||||
_currentIndex = 0;
|
||||
}
|
||||
|
||||
public override void Clear()
|
||||
{
|
||||
base.Clear();
|
||||
_currentIndex = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7440203326fc09a479080181660af525
|
||||
timeCreated: 1657003965
|
||||
@@ -0,0 +1,95 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
public abstract class TaskCollection : NTask, ITaskCollection
|
||||
{
|
||||
protected readonly List<ITask> RawList;
|
||||
protected readonly List<ITask> FinishList;
|
||||
protected readonly List<ITask> CurRunTask;
|
||||
|
||||
|
||||
public TaskCollection(string taskInfo = "")
|
||||
{
|
||||
RawList = new List<ITask>();
|
||||
FinishList = new List<ITask>();
|
||||
CurRunTask = new List<ITask>();
|
||||
TaskInfo = taskInfo;
|
||||
Status = NTaskStatus.None;
|
||||
}
|
||||
|
||||
public List<ITask> CurrentTask => CurRunTask;
|
||||
|
||||
public virtual int Count => RawList.Count + FinishList.Count;
|
||||
|
||||
/// <summary>
|
||||
/// 任务失败中断任务链
|
||||
/// </summary>
|
||||
public virtual bool FailBreak
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public override float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Status == NTaskStatus.Success) return 1;
|
||||
if (Status == NTaskStatus.None) return 0;
|
||||
var finishCount = FinishList.Count;
|
||||
for (var index = 0; index < CurRunTask.Count; index++)
|
||||
{
|
||||
var element = CurRunTask[index];
|
||||
finishCount += (int)element.Progress;
|
||||
}
|
||||
|
||||
return finishCount * 1f / RawList.Count;
|
||||
}
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
return this.RunTasksAndCheckIfDone();
|
||||
}
|
||||
|
||||
public ITaskCollection AddTask(ITask task)
|
||||
{
|
||||
RawList.Add(task);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
FinishList.Clear();
|
||||
CurrentTask.Clear();
|
||||
Status = NTaskStatus.None;
|
||||
for (int i = 0,len = RawList.Count; i < len; i++)
|
||||
{
|
||||
RawList[i].Reset();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
{
|
||||
FinishList.Clear();
|
||||
Status = NTaskStatus.None;
|
||||
for (var i = 0; i < CurRunTask.Count; i++)
|
||||
{
|
||||
var task = CurrentTask[i];
|
||||
task.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public virtual void Clear()
|
||||
{
|
||||
FinishList.Clear();
|
||||
RawList.Clear();
|
||||
Status = NTaskStatus.None;
|
||||
}
|
||||
|
||||
protected abstract NTaskStatus RunTasksAndCheckIfDone();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e84dd447d4e477a478c1cefaf7a14bc3
|
||||
timeCreated: 1656996883
|
||||
@@ -0,0 +1,85 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
public class TimelineTaskCollection : TaskCollection
|
||||
{
|
||||
private float _startTime = -1;
|
||||
|
||||
|
||||
protected override NTaskStatus RunTasksAndCheckIfDone()
|
||||
{
|
||||
if (_startTime < 0) _startTime = Time.time;
|
||||
var st = NTaskStatus.Running;
|
||||
|
||||
if (RawList.Count > 0)
|
||||
{
|
||||
for (var index = 0; index < RawList.Count; index++)
|
||||
{
|
||||
var raw = RawList[index];
|
||||
if (raw == null) continue;
|
||||
var t = raw["time"];
|
||||
if (t != null)
|
||||
{
|
||||
float time = 0;
|
||||
if (t is int i)
|
||||
{
|
||||
time = i;
|
||||
}
|
||||
else if (t is float f)
|
||||
{
|
||||
time = f;
|
||||
}
|
||||
else if (t is string s)
|
||||
{
|
||||
float.TryParse(s, out time);
|
||||
}
|
||||
|
||||
if (Time.time >= (_startTime + time))
|
||||
{
|
||||
CurrentTask.Add(RawList[index]);
|
||||
RawList.RemoveAt(index);
|
||||
index--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (RawList.Count <= 0 && CurrentTask.Count <= 0)
|
||||
{
|
||||
st = NTaskStatus.Success;
|
||||
}
|
||||
|
||||
for (var index = 0; index < CurrentTask.Count; index++)
|
||||
{
|
||||
var element = CurrentTask[index];
|
||||
var childSt = element.Process();
|
||||
if (childSt >= NTaskStatus.Success)
|
||||
{
|
||||
CurrentTask.RemoveAt(index);
|
||||
index--;
|
||||
FinishList.Add(element);
|
||||
}
|
||||
}
|
||||
|
||||
return st;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
_startTime = -1;
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
{
|
||||
base.Stop();
|
||||
_startTime = -1;
|
||||
}
|
||||
|
||||
public override void Clear()
|
||||
{
|
||||
base.Clear();
|
||||
_startTime = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2532701b307e7d64386ae6a0ccedbf3c
|
||||
timeCreated: 1657434862
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea8861aa2e7ea884ea4ee74daeda8caa
|
||||
timeCreated: 1657007208
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Extensions
|
||||
{
|
||||
public static partial class TaskChainExtension
|
||||
{
|
||||
public static SequenceTaskCollection Sequence<T>(this T selfbehaviour) where T : MonoBehaviour
|
||||
{
|
||||
var retNodeChain = new SequenceTaskCollection();
|
||||
return retNodeChain;
|
||||
}
|
||||
|
||||
public static ParallelTaskCollection Parallel<T>(this T selfbehaviour) where T : MonoBehaviour
|
||||
{
|
||||
var retNodeChain = new ParallelTaskCollection();
|
||||
return retNodeChain;
|
||||
}
|
||||
|
||||
// public static TimelineList Timeline<T>(this T selfbehaviour) where T : MonoBehaviour
|
||||
// {
|
||||
// var retNodeChain = new TimelineList();
|
||||
// return retNodeChain;
|
||||
// }
|
||||
|
||||
// public static ITaskCollection OnComplete(this ITaskCollection selfChain, Action<bool> callback)
|
||||
// {
|
||||
// selfChain.OnComplete(callback);
|
||||
// return selfChain;
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bb9626fffe2a12242999e5c295218da1
|
||||
timeCreated: 1657007222
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3fdfec8acff76af4daef6cf6c55c78f0
|
||||
timeCreated: 1656996246
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace NBC
|
||||
{
|
||||
/// <summary>
|
||||
/// 进度
|
||||
/// </summary>
|
||||
public interface IProcess
|
||||
{
|
||||
NTaskStatus Process();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a5556e4b457d51d49bf2d9ed0e024899
|
||||
timeCreated: 1656996282
|
||||
@@ -0,0 +1,49 @@
|
||||
namespace NBC
|
||||
{
|
||||
public interface IRunner
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否暂停
|
||||
/// </summary>
|
||||
bool IsPaused { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经终止了
|
||||
/// </summary>
|
||||
bool IsKilled { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前运行的任务数量
|
||||
/// </summary>
|
||||
int RunningTaskNum { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 准备执行的任务数量
|
||||
/// </summary>
|
||||
int NeedRunTaskNum { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 执行一个任务
|
||||
/// </summary>
|
||||
/// <param name="task">任务对象</param>
|
||||
void Run(ITask task);
|
||||
|
||||
void Process();
|
||||
|
||||
/// <summary>
|
||||
/// 停止任务
|
||||
/// </summary>
|
||||
/// <param name="task">任务对象</param>
|
||||
void StopTask(ITask task);
|
||||
|
||||
/// <summary>
|
||||
/// 停止所有任务
|
||||
/// </summary>
|
||||
void StopAllTask();
|
||||
|
||||
/// <summary>
|
||||
/// 终止任务
|
||||
/// </summary>
|
||||
void ShutDown();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed95fa20b281e8640a154c48d1780c70
|
||||
timeCreated: 1656996361
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
public interface ITask : IProcess, IEnumerator
|
||||
{
|
||||
NTaskStatus Status { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前任务的信息
|
||||
/// </summary>
|
||||
string Info { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 错误信息
|
||||
/// </summary>
|
||||
string ErrorMsg { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前任务的进度
|
||||
/// </summary>
|
||||
float Progress { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 任务正在执行
|
||||
/// </summary>
|
||||
bool IsRunning { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 任务是否执行完成
|
||||
/// </summary>
|
||||
bool IsDone { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 停止任务
|
||||
/// </summary>
|
||||
void Stop();
|
||||
|
||||
/// <summary>
|
||||
/// 任务开始回调
|
||||
/// </summary>
|
||||
/// <param name="callback"></param>
|
||||
/// <param name="cover"></param>
|
||||
/// <returns></returns>
|
||||
ITask OnStarted(Action<ITask> callback, bool cover = false);
|
||||
|
||||
/// <summary>
|
||||
/// 任务执行回调
|
||||
/// </summary>
|
||||
/// <param name="callback"></param>
|
||||
/// <param name="cover"></param>
|
||||
/// <returns></returns>
|
||||
ITask OnUpdated(Action<ITask> callback, bool cover = false);
|
||||
|
||||
/// <summary>
|
||||
/// 任务完成回调
|
||||
/// </summary>
|
||||
/// <param name="callback"></param>
|
||||
/// <param name="cover"></param>
|
||||
/// <returns></returns>
|
||||
ITask OnCompleted(Action<ITask> callback, bool cover = false);
|
||||
|
||||
/// <summary>
|
||||
/// 运行任务
|
||||
/// </summary>
|
||||
/// <param name="runner">任务运行器</param>
|
||||
void Run(IRunner runner);
|
||||
|
||||
/// <summary>
|
||||
/// 任务参数
|
||||
/// </summary>
|
||||
/// <param name="argsName"></param>
|
||||
object this[string argsName] { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f025551734ce37a479414f55be095790
|
||||
timeCreated: 1656996448
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
public interface ITaskCollection : ITask
|
||||
{
|
||||
/// <summary>
|
||||
/// 当前运行的任务堆栈
|
||||
/// </summary>
|
||||
List<ITask> CurrentTask { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 添加一个任务
|
||||
/// </summary>
|
||||
/// <param name="task"></param>
|
||||
/// <returns></returns>
|
||||
ITaskCollection AddTask(ITask task);
|
||||
|
||||
/// <summary>
|
||||
/// 清理任务列表
|
||||
/// </summary>
|
||||
void Clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2ae6582e140d07049accaf0b5db4b2f4
|
||||
timeCreated: 1656996765
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace NBC
|
||||
{
|
||||
public interface ITaskRun
|
||||
{
|
||||
void Run(IRunner runner);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3b784189f1caf1488eb81fb903ff63e
|
||||
timeCreated: 1656996836
|
||||
224
Assets/Scripts/NBC/Runtime/Core/FTask/NTask/NTask.cs
Normal file
224
Assets/Scripts/NBC/Runtime/Core/FTask/NTask/NTask.cs
Normal file
@@ -0,0 +1,224 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
public abstract class NTask : ITask
|
||||
{
|
||||
protected float _progress = 0;
|
||||
protected string _errorMsg;
|
||||
protected string TaskInfo;
|
||||
|
||||
protected readonly Dictionary<string, object> _argsDic = new Dictionary<string, object>();
|
||||
|
||||
public virtual string Info
|
||||
{
|
||||
get => TaskInfo;
|
||||
set => TaskInfo = value;
|
||||
}
|
||||
|
||||
public NTaskStatus Status { get; protected set; } = NTaskStatus.None;
|
||||
public virtual string ErrorMsg => _errorMsg;
|
||||
public virtual float Progress => _progress;
|
||||
|
||||
public virtual bool IsRunning => Status == NTaskStatus.Running;
|
||||
public virtual bool IsDone => Status == NTaskStatus.Success || Status == NTaskStatus.Fail;
|
||||
|
||||
public object this[string argsName]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_argsDic.TryGetValue(argsName, out object args))
|
||||
{
|
||||
return args;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
set => _argsDic[argsName] = value;
|
||||
}
|
||||
|
||||
|
||||
public virtual void Stop()
|
||||
{
|
||||
Status = NTaskStatus.None;
|
||||
}
|
||||
|
||||
|
||||
public virtual void Run(IRunner runner)
|
||||
{
|
||||
Reset();
|
||||
runner?.Run(this);
|
||||
}
|
||||
|
||||
public NTaskStatus Process()
|
||||
{
|
||||
if (Status == NTaskStatus.None)
|
||||
{
|
||||
Start();
|
||||
}
|
||||
|
||||
Status = OnProcess();
|
||||
CallUpdateListener();
|
||||
if (Status == NTaskStatus.Success)
|
||||
{
|
||||
_progress = 1;
|
||||
CallCompleteListener(Status);
|
||||
}
|
||||
else if (Status == NTaskStatus.Fail)
|
||||
{
|
||||
_progress = 1;
|
||||
CallCompleteListener(Status);
|
||||
}
|
||||
|
||||
return Status;
|
||||
}
|
||||
|
||||
protected virtual void OnStart()
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void OnComplete()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected virtual NTaskStatus OnProcess()
|
||||
{
|
||||
return this.Status;
|
||||
}
|
||||
|
||||
protected void Finish()
|
||||
{
|
||||
_progress = 1;
|
||||
Status = NTaskStatus.Success;
|
||||
}
|
||||
|
||||
protected void Fail(string message)
|
||||
{
|
||||
_progress = 1;
|
||||
Status = NTaskStatus.Fail;
|
||||
_errorMsg = message;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Reset();
|
||||
Status = NTaskStatus.Running;
|
||||
_progress = 0;
|
||||
OnStart();
|
||||
CallStartListener();
|
||||
}
|
||||
|
||||
|
||||
#region 事件
|
||||
|
||||
protected event Action<ITask> OnStartListener;
|
||||
protected event Action<ITask> OnCompleteListener;
|
||||
protected event Action<ITask> OnUpdateListener;
|
||||
|
||||
public ITask OnStarted(Action<ITask> callback, bool cover = false)
|
||||
{
|
||||
if (cover)
|
||||
{
|
||||
OnStartListener = callback;
|
||||
}
|
||||
else
|
||||
{
|
||||
OnStartListener += callback;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public ITask OnUpdated(Action<ITask> callback, bool cover = false)
|
||||
{
|
||||
if (cover)
|
||||
{
|
||||
OnUpdateListener = callback;
|
||||
}
|
||||
else
|
||||
{
|
||||
OnUpdateListener += callback;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public ITask OnCompleted(Action<ITask> callback, bool cover = false)
|
||||
{
|
||||
if (cover)
|
||||
{
|
||||
OnCompleteListener = callback;
|
||||
}
|
||||
else
|
||||
{
|
||||
OnCompleteListener += callback;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
protected void CallStartListener()
|
||||
{
|
||||
OnStartListener?.Invoke(this);
|
||||
}
|
||||
|
||||
protected void CallCompleteListener(NTaskStatus taskStatus)
|
||||
{
|
||||
OnComplete();
|
||||
OnCompleteListener?.Invoke(this);
|
||||
|
||||
_taskCompletionSource?.TrySetResult(null);
|
||||
}
|
||||
|
||||
protected void CallUpdateListener()
|
||||
{
|
||||
OnUpdateListener?.Invoke(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 异步编程相关
|
||||
|
||||
private TaskCompletionSource<object> _taskCompletionSource;
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作任务
|
||||
/// </summary>
|
||||
public Task Task
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_taskCompletionSource == null)
|
||||
{
|
||||
_taskCompletionSource = new TaskCompletionSource<object>();
|
||||
if (IsDone)
|
||||
_taskCompletionSource.SetResult(null);
|
||||
}
|
||||
|
||||
return _taskCompletionSource.Task;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerator
|
||||
|
||||
bool IEnumerator.MoveNext()
|
||||
{
|
||||
return !IsDone;
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
Status = NTaskStatus.None;
|
||||
}
|
||||
|
||||
object IEnumerator.Current => null;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/NBC/Runtime/Core/FTask/NTask/NTask.cs.meta
Normal file
11
Assets/Scripts/NBC/Runtime/Core/FTask/NTask/NTask.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8035be535392c22489664156d78df1ef
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
25
Assets/Scripts/NBC/Runtime/Core/FTask/NTask/NTaskStatus.cs
Normal file
25
Assets/Scripts/NBC/Runtime/Core/FTask/NTask/NTaskStatus.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace NBC
|
||||
{
|
||||
public enum NTaskStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// 任务还未执行
|
||||
/// </summary>
|
||||
None = 0,
|
||||
|
||||
/// <summary>
|
||||
/// 任务运行
|
||||
/// </summary>
|
||||
Running,
|
||||
|
||||
/// <summary>
|
||||
/// 任务执行成功
|
||||
/// </summary>
|
||||
Success,
|
||||
|
||||
/// <summary>
|
||||
/// 任务执行失败
|
||||
/// </summary>
|
||||
Fail
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8d69db579faa30f4db7ae1b2c27cb10f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/NBC/Runtime/Core/FTask/NTask/Runner.meta
Normal file
8
Assets/Scripts/NBC/Runtime/Core/FTask/NTask/Runner.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 63bed6143f265e3449b00af0ef891a5a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace NBC
|
||||
{
|
||||
/// <summary>
|
||||
/// 操作信息类
|
||||
/// </summary>
|
||||
public class FlushingOperation
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否暂停
|
||||
/// </summary>
|
||||
public bool Paused;
|
||||
|
||||
/// <summary>
|
||||
/// 是否被终结
|
||||
/// </summary>
|
||||
public bool Kill;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dc64e5709b547264b90f53b13ee8aaa9
|
||||
timeCreated: 1657004343
|
||||
96
Assets/Scripts/NBC/Runtime/Core/FTask/NTask/Runner/Runner.cs
Normal file
96
Assets/Scripts/NBC/Runtime/Core/FTask/NTask/Runner/Runner.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
public class Runner : IRunner
|
||||
{
|
||||
public bool IsPaused
|
||||
{
|
||||
get => FlushingOperation.Paused;
|
||||
set => FlushingOperation.Paused = value;
|
||||
}
|
||||
|
||||
public bool IsKilled => FlushingOperation.Kill;
|
||||
public int RunningTaskNum => Coroutines.Count;
|
||||
public int NeedRunTaskNum => ReadyTask.Count;
|
||||
|
||||
/// <summary>
|
||||
/// 当前运行的任务
|
||||
/// </summary>
|
||||
protected readonly List<ITask> Coroutines = new List<ITask>();
|
||||
|
||||
/// <summary>
|
||||
/// 准备要运行的任务
|
||||
/// </summary>
|
||||
protected readonly Queue<ITask> ReadyTask = new Queue<ITask>();
|
||||
|
||||
/// <summary>
|
||||
/// 当前操作的信息
|
||||
/// </summary>
|
||||
protected readonly FlushingOperation FlushingOperation = new FlushingOperation();
|
||||
|
||||
public virtual void Run(ITask task)
|
||||
{
|
||||
ReadyTask.Enqueue(task);
|
||||
}
|
||||
|
||||
public virtual void Process()
|
||||
{
|
||||
var count = ReadyTask.Count;
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var task = ReadyTask.Dequeue();
|
||||
Coroutines.Add(task);
|
||||
}
|
||||
|
||||
if (Coroutines.Count < 1) return;
|
||||
|
||||
var index = 0;
|
||||
bool mustExit;
|
||||
do
|
||||
{
|
||||
var childTask = Coroutines[index];
|
||||
var st = childTask.Process();
|
||||
if (st >= NTaskStatus.Success)
|
||||
{
|
||||
Coroutines.Remove(childTask);
|
||||
}
|
||||
else
|
||||
{
|
||||
index++;
|
||||
}
|
||||
|
||||
mustExit = Coroutines.Count == 0 || index >= Coroutines.Count;
|
||||
} while (!mustExit);
|
||||
}
|
||||
|
||||
public virtual void StopTask(ITask task)
|
||||
{
|
||||
var index = Coroutines.IndexOf(task);
|
||||
if (index != -1)
|
||||
{
|
||||
var t = Coroutines[index];
|
||||
t.Stop();
|
||||
Coroutines.RemoveAt(index);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void StopAllTask()
|
||||
{
|
||||
ReadyTask.Clear();
|
||||
for (int i = 0; i < Coroutines.Count; i++)
|
||||
{
|
||||
Coroutines[i].Stop();
|
||||
}
|
||||
|
||||
Coroutines.Clear();
|
||||
}
|
||||
|
||||
public virtual void ShutDown()
|
||||
{
|
||||
IsPaused = false;
|
||||
FlushingOperation.Kill = true;
|
||||
StopAllTask();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3bd5593733a84d74f83248aa0170da03
|
||||
timeCreated: 1657005250
|
||||
@@ -0,0 +1,78 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
public class RunnerProcess : IProcess
|
||||
{
|
||||
/// <summary>
|
||||
/// 当前运行的任务
|
||||
/// </summary>
|
||||
protected readonly List<ITask> Coroutines;
|
||||
|
||||
/// <summary>
|
||||
/// 准备要运行的任务
|
||||
/// </summary>
|
||||
protected readonly Queue<ITask> ReadyTask;
|
||||
|
||||
/// <summary>
|
||||
/// 当前操作的信息
|
||||
/// </summary>
|
||||
protected readonly FlushingOperation FlushingOperation;
|
||||
|
||||
/// <summary>
|
||||
/// 进程名称
|
||||
/// </summary>
|
||||
protected string Name;
|
||||
|
||||
public RunnerProcess(string name, List<ITask> coroutines, Queue<ITask> readyTask, FlushingOperation op)
|
||||
{
|
||||
this.Name = name;
|
||||
Coroutines = coroutines;
|
||||
ReadyTask = readyTask;
|
||||
FlushingOperation = op;
|
||||
}
|
||||
|
||||
public NTaskStatus Process()
|
||||
{
|
||||
var flag = false;
|
||||
if (this.FlushingOperation.Kill) flag = true;
|
||||
else
|
||||
{
|
||||
for (var index = 0; index < this.ReadyTask.Count; index++)
|
||||
{
|
||||
// var task = ReadyTask[0];
|
||||
var task = ReadyTask.Dequeue();
|
||||
this.Coroutines.Add(task);
|
||||
// ReadyTask.RemoveAt(0);
|
||||
}
|
||||
|
||||
if (this.Coroutines.Count == 0 || this.FlushingOperation.Paused)
|
||||
{
|
||||
flag = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
var index = 0;
|
||||
var mustExit = false;
|
||||
do
|
||||
{
|
||||
var childTask = this.Coroutines[index];
|
||||
var st = childTask.Process();
|
||||
if (st >= NTaskStatus.Success)
|
||||
{
|
||||
this.Coroutines.RemoveAt(index); //.splice(index, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
index++;
|
||||
}
|
||||
|
||||
mustExit = this.Coroutines.Count == 0 || index >= this.Coroutines.Count;
|
||||
} while (!mustExit);
|
||||
}
|
||||
}
|
||||
|
||||
return flag ? NTaskStatus.Success : NTaskStatus.Running;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3647c1233c788034e91db438d16c23a5
|
||||
timeCreated: 1657004572
|
||||
8
Assets/Scripts/NBC/Runtime/Core/FTask/Task.meta
Normal file
8
Assets/Scripts/NBC/Runtime/Core/FTask/Task.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b49f1eac772b94ed68a9b19184d4ce80
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
263
Assets/Scripts/NBC/Runtime/Core/FTask/Task/FTask.cs
Normal file
263
Assets/Scripts/NBC/Runtime/Core/FTask/Task/FTask.cs
Normal file
@@ -0,0 +1,263 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.ExceptionServices;
|
||||
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
// ReSharper disable ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
|
||||
// ReSharper disable CheckNamespace
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
#pragma warning disable CS8602 // Dereference of a possibly null reference.
|
||||
#pragma warning disable CS8603 // Possible null reference return.
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
public enum STaskStatus : byte
|
||||
{
|
||||
Pending = 0, // The operation has not yet completed.
|
||||
Succeeded = 1, // The operation completed successfully.
|
||||
Faulted = 2 // The operation completed with an error.
|
||||
}
|
||||
|
||||
[AsyncMethodBuilder(typeof(AsyncFTaskMethodBuilder))]
|
||||
public sealed partial class FTask : ICriticalNotifyCompletion
|
||||
{
|
||||
private Action _callBack;
|
||||
private ExceptionDispatchInfo _exception;
|
||||
private STaskStatus _status = STaskStatus.Pending;
|
||||
public bool IsCompleted
|
||||
{
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _status != STaskStatus.Pending;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public FTask GetAwaiter() => this;
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private async FVoid InnerCoroutine()
|
||||
{
|
||||
await this;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Coroutine()
|
||||
{
|
||||
InnerCoroutine().Coroutine();
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void GetResult()
|
||||
{
|
||||
switch (_status)
|
||||
{
|
||||
case STaskStatus.Succeeded:
|
||||
{
|
||||
Return();
|
||||
return;
|
||||
}
|
||||
case STaskStatus.Faulted:
|
||||
{
|
||||
Return();
|
||||
|
||||
if (_exception == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var exception = _exception;
|
||||
_exception = null;
|
||||
exception.Throw();
|
||||
return;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new NotSupportedException("Direct call to getResult is not allowed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetResult()
|
||||
{
|
||||
if (_status != STaskStatus.Pending)
|
||||
{
|
||||
throw new InvalidOperationException("The task has been completed");
|
||||
}
|
||||
|
||||
_status = STaskStatus.Succeeded;
|
||||
|
||||
if (_callBack == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var callBack = _callBack;
|
||||
_callBack = null;
|
||||
callBack.Invoke();
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void OnCompleted(Action action)
|
||||
{
|
||||
UnsafeOnCompleted(action);
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void UnsafeOnCompleted(Action action)
|
||||
{
|
||||
if (_status != STaskStatus.Pending)
|
||||
{
|
||||
action?.Invoke();
|
||||
return;
|
||||
}
|
||||
|
||||
_callBack = action;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetException(Exception exception)
|
||||
{
|
||||
if (_status != STaskStatus.Pending)
|
||||
{
|
||||
throw new InvalidOperationException("The task has been completed");
|
||||
}
|
||||
|
||||
_status = STaskStatus.Faulted;
|
||||
_exception = ExceptionDispatchInfo.Capture(exception);
|
||||
_callBack?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
[AsyncMethodBuilder(typeof(AsyncFTaskMethodBuilder<>))]
|
||||
public sealed partial class FTask<T> : ICriticalNotifyCompletion
|
||||
{
|
||||
private T _value;
|
||||
private Action _callBack;
|
||||
private ExceptionDispatchInfo _exception;
|
||||
private STaskStatus _status = STaskStatus.Pending;
|
||||
public bool IsCompleted
|
||||
{
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _status != STaskStatus.Pending;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public FTask<T> GetAwaiter() => this;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[DebuggerHidden]
|
||||
private async FVoid InnerCoroutine()
|
||||
{
|
||||
await this;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Coroutine()
|
||||
{
|
||||
InnerCoroutine().Coroutine();
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public T GetResult()
|
||||
{
|
||||
switch (_status)
|
||||
{
|
||||
case STaskStatus.Succeeded:
|
||||
{
|
||||
var value = _value;
|
||||
Return();
|
||||
return value;
|
||||
}
|
||||
case STaskStatus.Faulted:
|
||||
{
|
||||
Return();
|
||||
|
||||
if (_exception == null)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
var exception = _exception;
|
||||
_exception = null;
|
||||
exception.Throw();
|
||||
return default;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new NotSupportedException("Direct call to getResult is not allowed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetResult(T value)
|
||||
{
|
||||
if (_status != STaskStatus.Pending)
|
||||
{
|
||||
throw new InvalidOperationException("The task has been completed");
|
||||
}
|
||||
|
||||
_value = value;
|
||||
_status = STaskStatus.Succeeded;
|
||||
|
||||
if (_callBack == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var callBack = _callBack;
|
||||
_callBack = null;
|
||||
callBack.Invoke();
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void OnCompleted(Action action)
|
||||
{
|
||||
UnsafeOnCompleted(action);
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void UnsafeOnCompleted(Action action)
|
||||
{
|
||||
if (_status != STaskStatus.Pending)
|
||||
{
|
||||
action?.Invoke();
|
||||
return;
|
||||
}
|
||||
|
||||
_callBack = action;
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetException(Exception exception)
|
||||
{
|
||||
if (_status != STaskStatus.Pending)
|
||||
{
|
||||
throw new InvalidOperationException("The task has been completed");
|
||||
}
|
||||
|
||||
_status = STaskStatus.Faulted;
|
||||
_exception = ExceptionDispatchInfo.Capture(exception);
|
||||
_callBack?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/NBC/Runtime/Core/FTask/Task/FTask.cs.meta
Normal file
11
Assets/Scripts/NBC/Runtime/Core/FTask/Task/FTask.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e8d0072a6c1c45a4982529ab0d6385d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
31
Assets/Scripts/NBC/Runtime/Core/FTask/Task/FTaskCompleted.cs
Normal file
31
Assets/Scripts/NBC/Runtime/Core/FTask/Task/FTaskCompleted.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
[AsyncMethodBuilder(typeof(AsyncFTaskCompletedMethodBuilder))]
|
||||
public struct FTaskCompleted : ICriticalNotifyCompletion
|
||||
{
|
||||
[DebuggerHidden]
|
||||
public bool IsCompleted => true;
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public FTaskCompleted GetAwaiter()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void GetResult() { }
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void OnCompleted(Action continuation) { }
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void UnsafeOnCompleted(Action continuation) { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d28012d411e14000ab2ecf1c0fb5b4d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
29
Assets/Scripts/NBC/Runtime/Core/FTask/Task/FVoid.cs
Normal file
29
Assets/Scripts/NBC/Runtime/Core/FTask/Task/FVoid.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
[AsyncMethodBuilder(typeof(AsyncFVoidMethodBuilder))]
|
||||
internal struct FVoid : ICriticalNotifyCompletion
|
||||
{
|
||||
public bool IsCompleted
|
||||
{
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => true;
|
||||
}
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Coroutine() { }
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void OnCompleted(Action continuation) { }
|
||||
|
||||
[DebuggerHidden]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void UnsafeOnCompleted(Action continuation) { }
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/NBC/Runtime/Core/FTask/Task/FVoid.cs.meta
Normal file
11
Assets/Scripts/NBC/Runtime/Core/FTask/Task/FVoid.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b065583f9d663494e973c49cf75623b5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user