mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
Init TEngine4.0.0
Init TEngine4.0.0
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Triggers
|
||||
{
|
||||
public static partial class AsyncTriggerExtensions
|
||||
{
|
||||
public static AsyncAwakeTrigger GetAsyncAwakeTrigger(this GameObject gameObject)
|
||||
{
|
||||
return GetOrAddComponent<AsyncAwakeTrigger>(gameObject);
|
||||
}
|
||||
|
||||
public static AsyncAwakeTrigger GetAsyncAwakeTrigger(this Component component)
|
||||
{
|
||||
return component.gameObject.GetAsyncAwakeTrigger();
|
||||
}
|
||||
}
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
public sealed class AsyncAwakeTrigger : AsyncTriggerBase<AsyncUnit>
|
||||
{
|
||||
public UniTask AwakeAsync()
|
||||
{
|
||||
if (calledAwake) return UniTask.CompletedTask;
|
||||
|
||||
return ((IAsyncOneShotTrigger)new AsyncTriggerHandler<AsyncUnit>(this, true)).OneShotAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ef2840a2586894741a0ae211b8fd669b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,95 @@
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Triggers
|
||||
{
|
||||
public static partial class AsyncTriggerExtensions
|
||||
{
|
||||
public static AsyncDestroyTrigger GetAsyncDestroyTrigger(this GameObject gameObject)
|
||||
{
|
||||
return GetOrAddComponent<AsyncDestroyTrigger>(gameObject);
|
||||
}
|
||||
|
||||
public static AsyncDestroyTrigger GetAsyncDestroyTrigger(this Component component)
|
||||
{
|
||||
return component.gameObject.GetAsyncDestroyTrigger();
|
||||
}
|
||||
}
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
public sealed class AsyncDestroyTrigger : MonoBehaviour
|
||||
{
|
||||
bool awakeCalled = false;
|
||||
bool called = false;
|
||||
CancellationTokenSource cancellationTokenSource;
|
||||
|
||||
public CancellationToken CancellationToken
|
||||
{
|
||||
get
|
||||
{
|
||||
if (cancellationTokenSource == null)
|
||||
{
|
||||
cancellationTokenSource = new CancellationTokenSource();
|
||||
if (!awakeCalled)
|
||||
{
|
||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
|
||||
}
|
||||
}
|
||||
return cancellationTokenSource.Token;
|
||||
}
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
awakeCalled = true;
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
called = true;
|
||||
|
||||
cancellationTokenSource?.Cancel();
|
||||
cancellationTokenSource?.Dispose();
|
||||
}
|
||||
|
||||
public UniTask OnDestroyAsync()
|
||||
{
|
||||
if (called) return UniTask.CompletedTask;
|
||||
|
||||
var tcs = new UniTaskCompletionSource();
|
||||
|
||||
// OnDestroy = Called Cancel.
|
||||
CancellationToken.RegisterWithoutCaptureExecutionContext(state =>
|
||||
{
|
||||
var tcs2 = (UniTaskCompletionSource)state;
|
||||
tcs2.TrySetResult();
|
||||
}, tcs);
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
class AwakeMonitor : IPlayerLoopItem
|
||||
{
|
||||
readonly AsyncDestroyTrigger trigger;
|
||||
|
||||
public AwakeMonitor(AsyncDestroyTrigger trigger)
|
||||
{
|
||||
this.trigger = trigger;
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (trigger.called || trigger.awakeCalled) return false;
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger.OnDestroy();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4afdcb1cbadf954ba8b1cf465429e17
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,38 @@
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Triggers
|
||||
{
|
||||
public static partial class AsyncTriggerExtensions
|
||||
{
|
||||
public static AsyncStartTrigger GetAsyncStartTrigger(this GameObject gameObject)
|
||||
{
|
||||
return GetOrAddComponent<AsyncStartTrigger>(gameObject);
|
||||
}
|
||||
|
||||
public static AsyncStartTrigger GetAsyncStartTrigger(this Component component)
|
||||
{
|
||||
return component.gameObject.GetAsyncStartTrigger();
|
||||
}
|
||||
}
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
public sealed class AsyncStartTrigger : AsyncTriggerBase<AsyncUnit>
|
||||
{
|
||||
bool called;
|
||||
|
||||
void Start()
|
||||
{
|
||||
called = true;
|
||||
RaiseEvent(AsyncUnit.Default);
|
||||
}
|
||||
|
||||
public UniTask StartAsync()
|
||||
{
|
||||
if (called) return UniTask.CompletedTask;
|
||||
|
||||
return ((IAsyncOneShotTrigger)new AsyncTriggerHandler<AsyncUnit>(this, true)).OneShotAsync();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b4fd0f75e54ec3d4fbcb7fc65b11646b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,310 @@
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Triggers
|
||||
{
|
||||
public abstract class AsyncTriggerBase<T> : MonoBehaviour, IUniTaskAsyncEnumerable<T>
|
||||
{
|
||||
TriggerEvent<T> triggerEvent;
|
||||
|
||||
internal protected bool calledAwake;
|
||||
internal protected bool calledDestroy;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
calledAwake = true;
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (calledDestroy) return;
|
||||
calledDestroy = true;
|
||||
|
||||
triggerEvent.SetCompleted();
|
||||
}
|
||||
|
||||
internal void AddHandler(ITriggerHandler<T> handler)
|
||||
{
|
||||
if (!calledAwake)
|
||||
{
|
||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
|
||||
}
|
||||
|
||||
triggerEvent.Add(handler);
|
||||
}
|
||||
|
||||
internal void RemoveHandler(ITriggerHandler<T> handler)
|
||||
{
|
||||
if (!calledAwake)
|
||||
{
|
||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
|
||||
}
|
||||
|
||||
triggerEvent.Remove(handler);
|
||||
}
|
||||
|
||||
protected void RaiseEvent(T value)
|
||||
{
|
||||
triggerEvent.SetResult(value);
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new AsyncTriggerEnumerator(this, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class AsyncTriggerEnumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, ITriggerHandler<T>
|
||||
{
|
||||
static Action<object> cancellationCallback = CancellationCallback;
|
||||
|
||||
readonly AsyncTriggerBase<T> parent;
|
||||
CancellationToken cancellationToken;
|
||||
CancellationTokenRegistration registration;
|
||||
bool called;
|
||||
bool isDisposed;
|
||||
|
||||
public AsyncTriggerEnumerator(AsyncTriggerBase<T> parent, CancellationToken cancellationToken)
|
||||
{
|
||||
this.parent = parent;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public void OnCanceled(CancellationToken cancellationToken = default)
|
||||
{
|
||||
completionSource.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
|
||||
public void OnNext(T value)
|
||||
{
|
||||
Current = value;
|
||||
completionSource.TrySetResult(true);
|
||||
}
|
||||
|
||||
public void OnCompleted()
|
||||
{
|
||||
completionSource.TrySetResult(false);
|
||||
}
|
||||
|
||||
public void OnError(Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
}
|
||||
|
||||
static void CancellationCallback(object state)
|
||||
{
|
||||
var self = (AsyncTriggerEnumerator)state;
|
||||
self.DisposeAsync().Forget(); // sync
|
||||
|
||||
self.completionSource.TrySetCanceled(self.cancellationToken);
|
||||
}
|
||||
|
||||
public T Current { get; private set; }
|
||||
ITriggerHandler<T> ITriggerHandler<T>.Prev { get; set; }
|
||||
ITriggerHandler<T> ITriggerHandler<T>.Next { get; set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (!called)
|
||||
{
|
||||
called = true;
|
||||
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
parent.AddHandler(this);
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
{
|
||||
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
|
||||
}
|
||||
}
|
||||
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (!isDisposed)
|
||||
{
|
||||
isDisposed = true;
|
||||
TaskTracker.RemoveTracking(this);
|
||||
registration.Dispose();
|
||||
parent.RemoveHandler(this);
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
class AwakeMonitor : IPlayerLoopItem
|
||||
{
|
||||
readonly AsyncTriggerBase<T> trigger;
|
||||
|
||||
public AwakeMonitor(AsyncTriggerBase<T> trigger)
|
||||
{
|
||||
this.trigger = trigger;
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (trigger.calledAwake) return false;
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger.OnDestroy();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface IAsyncOneShotTrigger
|
||||
{
|
||||
UniTask OneShotAsync();
|
||||
}
|
||||
|
||||
public partial class AsyncTriggerHandler<T> : IAsyncOneShotTrigger
|
||||
{
|
||||
UniTask IAsyncOneShotTrigger.OneShotAsync()
|
||||
{
|
||||
core.Reset();
|
||||
return new UniTask((IUniTaskSource)this, core.Version);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed partial class AsyncTriggerHandler<T> : IUniTaskSource<T>, ITriggerHandler<T>, IDisposable
|
||||
{
|
||||
static Action<object> cancellationCallback = CancellationCallback;
|
||||
|
||||
readonly AsyncTriggerBase<T> trigger;
|
||||
|
||||
CancellationToken cancellationToken;
|
||||
CancellationTokenRegistration registration;
|
||||
bool isDisposed;
|
||||
bool callOnce;
|
||||
|
||||
UniTaskCompletionSourceCore<T> core;
|
||||
|
||||
internal CancellationToken CancellationToken => cancellationToken;
|
||||
|
||||
ITriggerHandler<T> ITriggerHandler<T>.Prev { get; set; }
|
||||
ITriggerHandler<T> ITriggerHandler<T>.Next { get; set; }
|
||||
|
||||
internal AsyncTriggerHandler(AsyncTriggerBase<T> trigger, bool callOnce)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
isDisposed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
this.trigger = trigger;
|
||||
this.cancellationToken = default;
|
||||
this.registration = default;
|
||||
this.callOnce = callOnce;
|
||||
|
||||
trigger.AddHandler(this);
|
||||
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
internal AsyncTriggerHandler(AsyncTriggerBase<T> trigger, CancellationToken cancellationToken, bool callOnce)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
isDisposed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
this.trigger = trigger;
|
||||
this.cancellationToken = cancellationToken;
|
||||
this.callOnce = callOnce;
|
||||
|
||||
trigger.AddHandler(this);
|
||||
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
{
|
||||
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
|
||||
}
|
||||
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
static void CancellationCallback(object state)
|
||||
{
|
||||
var self = (AsyncTriggerHandler<T>)state;
|
||||
self.Dispose();
|
||||
|
||||
self.core.TrySetCanceled(self.cancellationToken);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!isDisposed)
|
||||
{
|
||||
isDisposed = true;
|
||||
TaskTracker.RemoveTracking(this);
|
||||
registration.Dispose();
|
||||
trigger.RemoveHandler(this);
|
||||
}
|
||||
}
|
||||
|
||||
T IUniTaskSource<T>.GetResult(short token)
|
||||
{
|
||||
try
|
||||
{
|
||||
return core.GetResult(token);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (callOnce)
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ITriggerHandler<T>.OnNext(T value)
|
||||
{
|
||||
core.TrySetResult(value);
|
||||
}
|
||||
|
||||
void ITriggerHandler<T>.OnCanceled(CancellationToken cancellationToken)
|
||||
{
|
||||
core.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
|
||||
void ITriggerHandler<T>.OnCompleted()
|
||||
{
|
||||
core.TrySetCanceled(CancellationToken.None);
|
||||
}
|
||||
|
||||
void ITriggerHandler<T>.OnError(Exception ex)
|
||||
{
|
||||
core.TrySetException(ex);
|
||||
}
|
||||
|
||||
void IUniTaskSource.GetResult(short token)
|
||||
{
|
||||
((IUniTaskSource<T>)this).GetResult(token);
|
||||
}
|
||||
|
||||
UniTaskStatus IUniTaskSource.GetStatus(short token)
|
||||
{
|
||||
return core.GetStatus(token);
|
||||
}
|
||||
|
||||
UniTaskStatus IUniTaskSource.UnsafeGetStatus()
|
||||
{
|
||||
return core.UnsafeGetStatus();
|
||||
}
|
||||
|
||||
void IUniTaskSource.OnCompleted(Action<object> continuation, object state, short token)
|
||||
{
|
||||
core.OnCompleted(continuation, state, token);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c0c2bcee832c6641b25949c412f020f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,102 @@
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
using Cysharp.Threading.Tasks.Triggers;
|
||||
|
||||
namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
public static class UniTaskCancellationExtensions
|
||||
{
|
||||
#if UNITY_2022_2_OR_NEWER
|
||||
|
||||
/// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
|
||||
public static CancellationToken GetCancellationTokenOnDestroy(this MonoBehaviour monoBehaviour)
|
||||
{
|
||||
return monoBehaviour.destroyCancellationToken;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
|
||||
public static CancellationToken GetCancellationTokenOnDestroy(this GameObject gameObject)
|
||||
{
|
||||
return gameObject.GetAsyncDestroyTrigger().CancellationToken;
|
||||
}
|
||||
|
||||
/// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
|
||||
public static CancellationToken GetCancellationTokenOnDestroy(this Component component)
|
||||
{
|
||||
#if UNITY_2022_2_OR_NEWER
|
||||
if (component is MonoBehaviour mb)
|
||||
{
|
||||
return mb.destroyCancellationToken;
|
||||
}
|
||||
#endif
|
||||
|
||||
return component.GetAsyncDestroyTrigger().CancellationToken;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Triggers
|
||||
{
|
||||
public static partial class AsyncTriggerExtensions
|
||||
{
|
||||
// Util.
|
||||
|
||||
static T GetOrAddComponent<T>(GameObject gameObject)
|
||||
where T : Component
|
||||
{
|
||||
#if UNITY_2019_2_OR_NEWER
|
||||
if (!gameObject.TryGetComponent<T>(out var component))
|
||||
{
|
||||
component = gameObject.AddComponent<T>();
|
||||
}
|
||||
#else
|
||||
var component = gameObject.GetComponent<T>();
|
||||
if (component == null)
|
||||
{
|
||||
component = gameObject.AddComponent<T>();
|
||||
}
|
||||
#endif
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
// Special for single operation.
|
||||
|
||||
/// <summary>This function is called when the MonoBehaviour will be destroyed.</summary>
|
||||
public static UniTask OnDestroyAsync(this GameObject gameObject)
|
||||
{
|
||||
return gameObject.GetAsyncDestroyTrigger().OnDestroyAsync();
|
||||
}
|
||||
|
||||
/// <summary>This function is called when the MonoBehaviour will be destroyed.</summary>
|
||||
public static UniTask OnDestroyAsync(this Component component)
|
||||
{
|
||||
return component.GetAsyncDestroyTrigger().OnDestroyAsync();
|
||||
}
|
||||
|
||||
public static UniTask StartAsync(this GameObject gameObject)
|
||||
{
|
||||
return gameObject.GetAsyncStartTrigger().StartAsync();
|
||||
}
|
||||
|
||||
public static UniTask StartAsync(this Component component)
|
||||
{
|
||||
return component.GetAsyncStartTrigger().StartAsync();
|
||||
}
|
||||
|
||||
public static UniTask AwakeAsync(this GameObject gameObject)
|
||||
{
|
||||
return gameObject.GetAsyncAwakeTrigger().AwakeAsync();
|
||||
}
|
||||
|
||||
public static UniTask AwakeAsync(this Component component)
|
||||
{
|
||||
return component.GetAsyncAwakeTrigger().AwakeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 59b61dbea1562a84fb7a38ae0a0a0f88
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c30655636c35c3d4da44064af3d2d9a7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user