升级框架版本

This commit is contained in:
Bob.Song
2025-11-12 17:24:02 +08:00
parent 4059c207c0
commit 5e6119a073
2246 changed files with 6233 additions and 313 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8882af7b2bcd247b79a45e5a7f166996
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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) { }
}
}

View File

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

View File

@@ -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) { }
}
}

View File

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

View File

@@ -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) { }
}
}

View File

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

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ed149491fa2ce43ef9ed42f42d24a71e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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
}
}

View File

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

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8cb2da0c1fd8f4b66912dd23ae2c0b62
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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);
}
}
}

View File

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

View File

@@ -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();
}
}
}
}
}

View File

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

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b49f1eac772b94ed68a9b19184d4ce80
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View 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();
}
}
}

View File

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

View 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) { }
}
}

View File

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

View 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) { }
}
}

View File

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