2020-05-04 00:59:22 +08:00
|
|
|
|
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
2020-04-26 01:38:16 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using UnityEngine;
|
2020-05-04 00:59:22 +08:00
|
|
|
|
using UniRx.Async.Internal;
|
|
|
|
|
#if ENABLE_UNITYWEBREQUEST
|
2019-05-19 23:14:47 +08:00
|
|
|
|
using UnityEngine.Networking;
|
2020-05-04 00:59:22 +08:00
|
|
|
|
#endif
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
namespace UniRx.Async
|
|
|
|
|
{
|
|
|
|
|
public static partial class UnityAsyncExtensions
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
#region AsyncOperation
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
public static AsyncOperationAwaiter GetAwaiter(this AsyncOperation asyncOperation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
return new AsyncOperationAwaiter(asyncOperation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static UniTask ToUniTask(this AsyncOperation asyncOperation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public static UniTask ConfigureAwait(this AsyncOperation asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 01:38:16 +08:00
|
|
|
|
public struct AsyncOperationAwaiter : ICriticalNotifyCompletion
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
|
|
|
|
AsyncOperation asyncOperation;
|
|
|
|
|
Action<AsyncOperation> continuationAction;
|
|
|
|
|
|
|
|
|
|
public AsyncOperationAwaiter(AsyncOperation asyncOperation)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
this.asyncOperation = asyncOperation;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
this.continuationAction = null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 01:38:16 +08:00
|
|
|
|
public bool IsCompleted => asyncOperation.isDone;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
public void GetResult()
|
|
|
|
|
{
|
|
|
|
|
if (continuationAction != null)
|
|
|
|
|
{
|
|
|
|
|
asyncOperation.completed -= continuationAction;
|
2019-05-30 18:41:23 +08:00
|
|
|
|
asyncOperation = null; // remove reference.
|
2019-05-19 23:14:47 +08:00
|
|
|
|
continuationAction = null;
|
|
|
|
|
}
|
2019-05-30 18:41:23 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
asyncOperation = null; // remove reference.
|
|
|
|
|
}
|
2020-05-04 00:59:22 +08:00
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted(Action continuation)
|
|
|
|
|
{
|
|
|
|
|
UnsafeOnCompleted(continuation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnsafeOnCompleted(Action continuation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
2020-04-26 01:38:16 +08:00
|
|
|
|
continuationAction = continuation.AsFuncOfT<AsyncOperation>(); // allocate delegate.
|
2019-05-19 23:14:47 +08:00
|
|
|
|
asyncOperation.completed += continuationAction;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
class AsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
static readonly PromisePool<AsyncOperationConfiguredSource> pool = new PromisePool<AsyncOperationConfiguredSource>();
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
AsyncOperation asyncOperation;
|
|
|
|
|
IProgress<float> progress;
|
|
|
|
|
CancellationToken cancellationToken;
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
UniTaskCompletionSourceCore<AsyncUnit> core;
|
|
|
|
|
|
|
|
|
|
AsyncOperationConfiguredSource()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
var result = pool.TryRent() ?? new AsyncOperationConfiguredSource();
|
|
|
|
|
|
|
|
|
|
result.asyncOperation = asyncOperation;
|
|
|
|
|
result.progress = progress;
|
|
|
|
|
result.cancellationToken = cancellationToken;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.TrackActiveTask(result, 3);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
PlayerLoopHelper.AddAction(timing, result);
|
|
|
|
|
|
|
|
|
|
token = result.core.Version;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GetResult(short token)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
try
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.RemoveTracking(this);
|
2020-05-04 00:59:22 +08:00
|
|
|
|
|
|
|
|
|
core.GetResult(token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
2020-05-04 00:59:22 +08:00
|
|
|
|
finally
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
pool.TryReturn(this);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
2020-05-04 00:59:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
|
|
|
|
{
|
|
|
|
|
return core.GetStatus(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
|
|
|
|
{
|
|
|
|
|
return core.UnsafeGetStatus();
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
|
|
|
|
core.OnCompleted(continuation, state, token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool MoveNext()
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.TrySetCanceled(cancellationToken);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (progress != null)
|
|
|
|
|
{
|
|
|
|
|
progress.Report(asyncOperation.progress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (asyncOperation.isDone)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.TrySetResult(AsyncUnit.Default);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public void Reset()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.Reset();
|
|
|
|
|
asyncOperation = default;
|
|
|
|
|
progress = default;
|
|
|
|
|
cancellationToken = default;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
~AsyncOperationConfiguredSource()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
if (pool.TryReturn(this))
|
|
|
|
|
{
|
|
|
|
|
GC.ReRegisterForFinalize(this);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
2020-05-04 00:59:22 +08:00
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
# endregion
|
|
|
|
|
|
|
|
|
|
#region ResourceRequest
|
|
|
|
|
|
|
|
|
|
public static ResourceRequestAwaiter GetAwaiter(this ResourceRequest asyncOperation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
return new ResourceRequestAwaiter(asyncOperation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static UniTask<UnityEngine.Object> ToUniTask(this ResourceRequest asyncOperation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
|
|
|
|
|
return new UniTask<UnityEngine.Object>(ResourceRequestConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public static UniTask<UnityEngine.Object> ConfigureAwait(this ResourceRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
|
|
|
|
|
return new UniTask<UnityEngine.Object>(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public struct ResourceRequestAwaiter : ICriticalNotifyCompletion
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
|
|
|
|
ResourceRequest asyncOperation;
|
|
|
|
|
Action<AsyncOperation> continuationAction;
|
|
|
|
|
UnityEngine.Object result;
|
|
|
|
|
|
|
|
|
|
public ResourceRequestAwaiter(ResourceRequest asyncOperation)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
this.asyncOperation = asyncOperation;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
this.continuationAction = null;
|
2020-05-04 00:59:22 +08:00
|
|
|
|
this.result = default;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public bool IsCompleted => asyncOperation.isDone;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
public UnityEngine.Object GetResult()
|
|
|
|
|
{
|
|
|
|
|
if (continuationAction != null)
|
|
|
|
|
{
|
|
|
|
|
asyncOperation.completed -= continuationAction;
|
2019-05-30 18:41:23 +08:00
|
|
|
|
asyncOperation = null; // remove reference.
|
2019-05-19 23:14:47 +08:00
|
|
|
|
continuationAction = null;
|
|
|
|
|
}
|
2019-05-30 18:41:23 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
asyncOperation = null; // remove reference.
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
return this.result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted(Action continuation)
|
|
|
|
|
{
|
|
|
|
|
UnsafeOnCompleted(continuation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnsafeOnCompleted(Action continuation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
2020-05-04 00:59:22 +08:00
|
|
|
|
continuationAction = continuation.AsFuncOfT<AsyncOperation>(); // allocate delegate.
|
2019-05-19 23:14:47 +08:00
|
|
|
|
asyncOperation.completed += continuationAction;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 04:07:59 +08:00
|
|
|
|
class ResourceRequestConfiguredSource : IUniTaskSource<UnityEngine.Object>, IPlayerLoopItem, IPromisePoolItem
|
|
|
|
|
{
|
|
|
|
|
static readonly PromisePool<ResourceRequestConfiguredSource> pool = new PromisePool<ResourceRequestConfiguredSource>();
|
|
|
|
|
|
|
|
|
|
ResourceRequest asyncOperation;
|
|
|
|
|
IProgress<float> progress;
|
|
|
|
|
CancellationToken cancellationToken;
|
|
|
|
|
|
|
|
|
|
UniTaskCompletionSourceCore<UnityEngine.Object> core;
|
|
|
|
|
|
|
|
|
|
ResourceRequestConfiguredSource()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IUniTaskSource<UnityEngine.Object> Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
return AutoResetUniTaskCompletionSource<UnityEngine.Object>.CreateFromCanceled(cancellationToken, out token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = pool.TryRent() ?? new ResourceRequestConfiguredSource();
|
|
|
|
|
|
|
|
|
|
result.asyncOperation = asyncOperation;
|
|
|
|
|
result.progress = progress;
|
|
|
|
|
result.cancellationToken = cancellationToken;
|
|
|
|
|
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.TrackActiveTask(result, 3);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
|
|
|
|
PlayerLoopHelper.AddAction(timing, result);
|
|
|
|
|
|
|
|
|
|
token = result.core.Version;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UnityEngine.Object GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.RemoveTracking(this);
|
2020-05-04 00:59:22 +08:00
|
|
|
|
|
2020-04-18 04:07:59 +08:00
|
|
|
|
return core.GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
pool.TryReturn(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IUniTaskSource.GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
|
|
|
|
return core.GetStatus(token);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
|
|
|
|
return core.UnsafeGetStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
|
|
|
|
core.OnCompleted(continuation, state, token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool MoveNext()
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
core.TrySetCanceled(cancellationToken);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (progress != null)
|
|
|
|
|
{
|
|
|
|
|
progress.Report(asyncOperation.progress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (asyncOperation.isDone)
|
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
core.TrySetResult(asyncOperation.asset);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
core.Reset();
|
|
|
|
|
asyncOperation = default;
|
|
|
|
|
progress = default;
|
|
|
|
|
cancellationToken = default;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
~ResourceRequestConfiguredSource()
|
|
|
|
|
{
|
|
|
|
|
if (pool.TryReturn(this))
|
|
|
|
|
{
|
|
|
|
|
GC.ReRegisterForFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
# endregion
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
#region AssetBundleRequest
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public static AssetBundleRequestAwaiter GetAwaiter(this AssetBundleRequest asyncOperation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
return new AssetBundleRequestAwaiter(asyncOperation);
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public static UniTask<UnityEngine.Object> ToUniTask(this AssetBundleRequest asyncOperation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
return new UniTask<UnityEngine.Object>(AssetBundleRequestConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public static UniTask<UnityEngine.Object> ConfigureAwait(this AssetBundleRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
return new UniTask<UnityEngine.Object>(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public struct AssetBundleRequestAwaiter : ICriticalNotifyCompletion
|
2019-07-05 13:41:42 +08:00
|
|
|
|
{
|
|
|
|
|
AssetBundleRequest asyncOperation;
|
|
|
|
|
Action<AsyncOperation> continuationAction;
|
|
|
|
|
UnityEngine.Object result;
|
|
|
|
|
|
|
|
|
|
public AssetBundleRequestAwaiter(AssetBundleRequest asyncOperation)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
this.asyncOperation = asyncOperation;
|
2019-07-05 13:41:42 +08:00
|
|
|
|
this.continuationAction = null;
|
2020-05-04 00:59:22 +08:00
|
|
|
|
this.result = default;
|
2019-07-05 13:41:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public bool IsCompleted => asyncOperation.isDone;
|
2019-07-05 13:41:42 +08:00
|
|
|
|
|
|
|
|
|
public UnityEngine.Object GetResult()
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
if (continuationAction != null)
|
2019-07-05 13:41:42 +08:00
|
|
|
|
{
|
|
|
|
|
asyncOperation.completed -= continuationAction;
|
|
|
|
|
asyncOperation = null; // remove reference.
|
|
|
|
|
continuationAction = null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
asyncOperation = null; // remove reference.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted(Action continuation)
|
|
|
|
|
{
|
|
|
|
|
UnsafeOnCompleted(continuation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnsafeOnCompleted(Action continuation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
2020-05-04 00:59:22 +08:00
|
|
|
|
continuationAction = continuation.AsFuncOfT<AsyncOperation>(); // allocate delegate.
|
2019-07-05 13:41:42 +08:00
|
|
|
|
asyncOperation.completed += continuationAction;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
class AssetBundleRequestConfiguredSource : IUniTaskSource<UnityEngine.Object>, IPlayerLoopItem, IPromisePoolItem
|
2019-07-05 13:41:42 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
static readonly PromisePool<AssetBundleRequestConfiguredSource> pool = new PromisePool<AssetBundleRequestConfiguredSource>();
|
|
|
|
|
|
2019-07-05 13:41:42 +08:00
|
|
|
|
AssetBundleRequest asyncOperation;
|
|
|
|
|
IProgress<float> progress;
|
|
|
|
|
CancellationToken cancellationToken;
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
UniTaskCompletionSourceCore<UnityEngine.Object> core;
|
|
|
|
|
|
|
|
|
|
AssetBundleRequestConfiguredSource()
|
2019-07-05 13:41:42 +08:00
|
|
|
|
{
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
}
|
2019-07-05 13:41:42 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public static IUniTaskSource<UnityEngine.Object> Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
return AutoResetUniTaskCompletionSource<UnityEngine.Object>.CreateFromCanceled(cancellationToken, out token);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
var result = pool.TryRent() ?? new AssetBundleRequestConfiguredSource();
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
result.asyncOperation = asyncOperation;
|
|
|
|
|
result.progress = progress;
|
|
|
|
|
result.cancellationToken = cancellationToken;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.TrackActiveTask(result, 3);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
PlayerLoopHelper.AddAction(timing, result);
|
|
|
|
|
|
|
|
|
|
token = result.core.Version;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UnityEngine.Object GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.RemoveTracking(this);
|
2020-05-04 00:59:22 +08:00
|
|
|
|
|
|
|
|
|
return core.GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
finally
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
pool.TryReturn(this);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
2020-05-04 00:59:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IUniTaskSource.GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
|
|
|
|
{
|
|
|
|
|
return core.GetStatus(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
|
|
|
|
{
|
|
|
|
|
return core.UnsafeGetStatus();
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
|
|
|
|
core.OnCompleted(continuation, state, token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool MoveNext()
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.TrySetCanceled(cancellationToken);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (progress != null)
|
|
|
|
|
{
|
|
|
|
|
progress.Report(asyncOperation.progress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (asyncOperation.isDone)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.TrySetResult(asyncOperation.asset);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public void Reset()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.Reset();
|
|
|
|
|
asyncOperation = default;
|
|
|
|
|
progress = default;
|
|
|
|
|
cancellationToken = default;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
~AssetBundleRequestConfiguredSource()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
if (pool.TryReturn(this))
|
|
|
|
|
{
|
|
|
|
|
GC.ReRegisterForFinalize(this);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
2020-05-04 00:59:22 +08:00
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
# endregion
|
|
|
|
|
|
|
|
|
|
#region AssetBundleCreateRequest
|
|
|
|
|
|
|
|
|
|
public static AssetBundleCreateRequestAwaiter GetAwaiter(this AssetBundleCreateRequest asyncOperation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
return new AssetBundleCreateRequestAwaiter(asyncOperation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static UniTask<AssetBundle> ToUniTask(this AssetBundleCreateRequest asyncOperation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
|
|
|
|
|
return new UniTask<AssetBundle>(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static UniTask<AssetBundle> ConfigureAwait(this AssetBundleCreateRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
|
|
|
|
|
return new UniTask<AssetBundle>(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public struct AssetBundleCreateRequestAwaiter : ICriticalNotifyCompletion
|
2019-11-16 18:44:01 +08:00
|
|
|
|
{
|
|
|
|
|
AssetBundleCreateRequest asyncOperation;
|
|
|
|
|
Action<AsyncOperation> continuationAction;
|
|
|
|
|
AssetBundle result;
|
|
|
|
|
|
|
|
|
|
public AssetBundleCreateRequestAwaiter(AssetBundleCreateRequest asyncOperation)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
this.asyncOperation = asyncOperation;
|
2019-11-16 18:44:01 +08:00
|
|
|
|
this.continuationAction = null;
|
2020-05-04 00:59:22 +08:00
|
|
|
|
this.result = default;
|
2019-11-16 18:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public bool IsCompleted => asyncOperation.isDone;
|
2019-11-16 18:44:01 +08:00
|
|
|
|
|
|
|
|
|
public AssetBundle GetResult()
|
|
|
|
|
{
|
|
|
|
|
if (continuationAction != null)
|
|
|
|
|
{
|
|
|
|
|
asyncOperation.completed -= continuationAction;
|
|
|
|
|
asyncOperation = null; // remove reference.
|
|
|
|
|
continuationAction = null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
asyncOperation = null; // remove reference.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted(Action continuation)
|
|
|
|
|
{
|
|
|
|
|
UnsafeOnCompleted(continuation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnsafeOnCompleted(Action continuation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
2020-05-04 00:59:22 +08:00
|
|
|
|
continuationAction = continuation.AsFuncOfT<AsyncOperation>(); // allocate delegate.
|
2019-11-16 18:44:01 +08:00
|
|
|
|
asyncOperation.completed += continuationAction;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
class AssetBundleCreateRequestConfiguredSource : IUniTaskSource<AssetBundle>, IPlayerLoopItem, IPromisePoolItem
|
2019-11-16 18:44:01 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
static readonly PromisePool<AssetBundleCreateRequestConfiguredSource> pool = new PromisePool<AssetBundleCreateRequestConfiguredSource>();
|
|
|
|
|
|
2019-11-16 18:44:01 +08:00
|
|
|
|
AssetBundleCreateRequest asyncOperation;
|
|
|
|
|
IProgress<float> progress;
|
|
|
|
|
CancellationToken cancellationToken;
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
UniTaskCompletionSourceCore<AssetBundle> core;
|
2019-11-16 18:44:01 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
AssetBundleCreateRequestConfiguredSource()
|
2019-11-16 18:44:01 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public static IUniTaskSource<AssetBundle> Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
|
2019-11-16 18:44:01 +08:00
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
return AutoResetUniTaskCompletionSource<AssetBundle>.CreateFromCanceled(cancellationToken, out token);
|
2019-11-16 18:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
var result = pool.TryRent() ?? new AssetBundleCreateRequestConfiguredSource();
|
2019-11-16 18:44:01 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
result.asyncOperation = asyncOperation;
|
|
|
|
|
result.progress = progress;
|
|
|
|
|
result.cancellationToken = cancellationToken;
|
2019-11-16 18:44:01 +08:00
|
|
|
|
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.TrackActiveTask(result, 3);
|
2019-11-16 18:44:01 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
PlayerLoopHelper.AddAction(timing, result);
|
2019-11-16 18:44:01 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
token = result.core.Version;
|
|
|
|
|
return result;
|
2019-11-16 18:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public AssetBundle GetResult(short token)
|
2019-11-16 18:44:01 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.RemoveTracking(this);
|
2020-05-04 00:59:22 +08:00
|
|
|
|
|
|
|
|
|
return core.GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
pool.TryReturn(this);
|
|
|
|
|
}
|
2019-11-16 18:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
void IUniTaskSource.GetResult(short token)
|
2019-11-16 18:44:01 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
GetResult(token);
|
2019-11-16 18:44:01 +08:00
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
return core.GetStatus(token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
return core.UnsafeGetStatus();
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
|
|
|
|
core.OnCompleted(continuation, state, token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool MoveNext()
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.TrySetCanceled(cancellationToken);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (progress != null)
|
|
|
|
|
{
|
|
|
|
|
progress.Report(asyncOperation.progress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (asyncOperation.isDone)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.TrySetResult(asyncOperation.assetBundle);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public void Reset()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.Reset();
|
|
|
|
|
asyncOperation = default;
|
|
|
|
|
progress = default;
|
|
|
|
|
cancellationToken = default;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
~AssetBundleCreateRequestConfiguredSource()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
if (pool.TryReturn(this))
|
|
|
|
|
{
|
|
|
|
|
GC.ReRegisterForFinalize(this);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
2020-05-04 00:59:22 +08:00
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
# endregion
|
|
|
|
|
|
|
|
|
|
#if ENABLE_UNITYWEBREQUEST
|
|
|
|
|
#region UnityWebRequestAsyncOperation
|
|
|
|
|
|
|
|
|
|
public static UnityWebRequestAsyncOperationAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOperation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
return new UnityWebRequestAsyncOperationAwaiter(asyncOperation);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public static UniTask<UnityWebRequest> ToUniTask(this UnityWebRequestAsyncOperation asyncOperation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
return new UniTask<UnityWebRequest>(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public static UniTask<UnityWebRequest> ConfigureAwait(this UnityWebRequestAsyncOperation asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
|
|
|
|
|
|
|
|
|
return new UniTask<UnityWebRequest>(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public struct UnityWebRequestAsyncOperationAwaiter : ICriticalNotifyCompletion
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
|
|
|
|
UnityWebRequestAsyncOperation asyncOperation;
|
|
|
|
|
Action<AsyncOperation> continuationAction;
|
|
|
|
|
UnityWebRequest result;
|
|
|
|
|
|
|
|
|
|
public UnityWebRequestAsyncOperationAwaiter(UnityWebRequestAsyncOperation asyncOperation)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
this.asyncOperation = asyncOperation;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
this.continuationAction = null;
|
2020-05-04 00:59:22 +08:00
|
|
|
|
this.result = default;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public bool IsCompleted => asyncOperation.isDone;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
public UnityWebRequest GetResult()
|
|
|
|
|
{
|
|
|
|
|
if (continuationAction != null)
|
|
|
|
|
{
|
|
|
|
|
asyncOperation.completed -= continuationAction;
|
2019-05-30 18:41:23 +08:00
|
|
|
|
asyncOperation = null; // remove reference.
|
2019-05-19 23:14:47 +08:00
|
|
|
|
continuationAction = null;
|
|
|
|
|
}
|
2019-05-30 18:41:23 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
asyncOperation = null; // remove reference.
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return this.result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted(Action continuation)
|
|
|
|
|
{
|
|
|
|
|
UnsafeOnCompleted(continuation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnsafeOnCompleted(Action continuation)
|
|
|
|
|
{
|
|
|
|
|
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
2020-05-04 00:59:22 +08:00
|
|
|
|
continuationAction = continuation.AsFuncOfT<AsyncOperation>(); // allocate delegate.
|
2019-05-19 23:14:47 +08:00
|
|
|
|
asyncOperation.completed += continuationAction;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
class UnityWebRequestAsyncOperationConfiguredSource : IUniTaskSource<UnityWebRequest>, IPlayerLoopItem, IPromisePoolItem
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
static readonly PromisePool<UnityWebRequestAsyncOperationConfiguredSource> pool = new PromisePool<UnityWebRequestAsyncOperationConfiguredSource>();
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
UnityWebRequestAsyncOperation asyncOperation;
|
|
|
|
|
IProgress<float> progress;
|
|
|
|
|
CancellationToken cancellationToken;
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
UniTaskCompletionSourceCore<UnityWebRequest> core;
|
|
|
|
|
|
|
|
|
|
UnityWebRequestAsyncOperationConfiguredSource()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public static IUniTaskSource<UnityWebRequest> Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
return AutoResetUniTaskCompletionSource<UnityWebRequest>.CreateFromCanceled(cancellationToken, out token);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
var result = pool.TryRent() ?? new UnityWebRequestAsyncOperationConfiguredSource();
|
|
|
|
|
|
|
|
|
|
result.asyncOperation = asyncOperation;
|
|
|
|
|
result.progress = progress;
|
|
|
|
|
result.cancellationToken = cancellationToken;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.TrackActiveTask(result, 3);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
PlayerLoopHelper.AddAction(timing, result);
|
|
|
|
|
|
|
|
|
|
token = result.core.Version;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public UnityWebRequest GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.RemoveTracking(this);
|
2020-05-04 00:59:22 +08:00
|
|
|
|
|
|
|
|
|
return core.GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
finally
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
pool.TryReturn(this);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
2020-05-04 00:59:22 +08:00
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
void IUniTaskSource.GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
|
|
|
|
{
|
|
|
|
|
return core.GetStatus(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
|
|
|
|
{
|
|
|
|
|
return core.UnsafeGetStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
|
|
|
|
core.OnCompleted(continuation, state, token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool MoveNext()
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.TrySetCanceled(cancellationToken);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (progress != null)
|
|
|
|
|
{
|
|
|
|
|
progress.Report(asyncOperation.progress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (asyncOperation.isDone)
|
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.TrySetResult(asyncOperation.webRequest);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
public void Reset()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
core.Reset();
|
|
|
|
|
asyncOperation = default;
|
|
|
|
|
progress = default;
|
|
|
|
|
cancellationToken = default;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
~UnityWebRequestAsyncOperationConfiguredSource()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-04 00:59:22 +08:00
|
|
|
|
if (pool.TryReturn(this))
|
|
|
|
|
{
|
|
|
|
|
GC.ReRegisterForFinalize(this);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:59:22 +08:00
|
|
|
|
# endregion
|
2019-05-19 23:14:47 +08:00
|
|
|
|
#endif
|
2020-05-04 00:59:22 +08:00
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
2020-05-04 00:59:22 +08:00
|
|
|
|
}
|