From 5e59e7ec86ac7d1fc438ffad21648ac900c1cc65 Mon Sep 17 00:00:00 2001 From: neuecc Date: Fri, 5 Jun 2020 16:45:40 +0900 Subject: [PATCH] netstandard2.0 support #84 --- .../NetCore/UniTask.AsValueTask.cs | 68 +++++++++++++++++++ src/UniTask.NetCore/UniTask.NetCore.csproj | 2 +- .../Plugins/UniTask/Runtime/IUniTaskSource.cs | 8 +-- .../Assets/Plugins/UniTask/Runtime/UniTask.cs | 8 +++ 4 files changed, 81 insertions(+), 5 deletions(-) diff --git a/src/UniTask.NetCore/NetCore/UniTask.AsValueTask.cs b/src/UniTask.NetCore/NetCore/UniTask.AsValueTask.cs index 97583e7..103d129 100644 --- a/src/UniTask.NetCore/NetCore/UniTask.AsValueTask.cs +++ b/src/UniTask.NetCore/NetCore/UniTask.AsValueTask.cs @@ -1,6 +1,8 @@ #pragma warning disable 0649 +using System; using System.Threading.Tasks; +using System.Threading.Tasks.Sources; namespace Cysharp.Threading.Tasks { @@ -8,12 +10,20 @@ namespace Cysharp.Threading.Tasks { public static ValueTask AsValueTask(this in UniTask task) { +#if NETSTANDARD2_0 + return new ValueTask(new UniTaskValueTaskSource(task), 0); +#else return task; +#endif } public static ValueTask AsValueTask(this in UniTask task) { +#if NETSTANDARD2_0 + return new ValueTask(new UniTaskValueTaskSource(task), 0); +#else return task; +#endif } public static UniTask AsUniTask(this ValueTask task, bool useCurrentSynchronizationContext = true) @@ -26,5 +36,63 @@ namespace Cysharp.Threading.Tasks { return task.AsTask().AsUniTask(useCurrentSynchronizationContext); } + +#if NETSTANDARD2_0 + + class UniTaskValueTaskSource : IValueTaskSource + { + readonly UniTask task; + readonly UniTask.Awaiter awaiter; + + public UniTaskValueTaskSource(UniTask task) + { + this.task = task; + this.awaiter = task.GetAwaiter(); + } + + public void GetResult(short token) + { + awaiter.GetResult(); + } + + public ValueTaskSourceStatus GetStatus(short token) + { + return (ValueTaskSourceStatus)task.Status; + } + + public void OnCompleted(Action continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) + { + awaiter.SourceOnCompleted(continuation, state); + } + } + + class UniTaskValueTaskSource : IValueTaskSource + { + readonly UniTask task; + readonly UniTask.Awaiter awaiter; + + public UniTaskValueTaskSource(UniTask task) + { + this.task = task; + this.awaiter = task.GetAwaiter(); + } + + public T GetResult(short token) + { + return awaiter.GetResult(); + } + + public ValueTaskSourceStatus GetStatus(short token) + { + return (ValueTaskSourceStatus)task.Status; + } + + public void OnCompleted(Action continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) + { + awaiter.SourceOnCompleted(continuation, state); + } + } + +#endif } } diff --git a/src/UniTask.NetCore/UniTask.NetCore.csproj b/src/UniTask.NetCore/UniTask.NetCore.csproj index 0284d76..f7a7a2e 100644 --- a/src/UniTask.NetCore/UniTask.NetCore.csproj +++ b/src/UniTask.NetCore/UniTask.NetCore.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1;netstandard2.1 + netcoreapp3.1;netstandard2.1;netstandard2.0 UniTask 8.0 Cysharp.Threading.Tasks diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/IUniTaskSource.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/IUniTaskSource.cs index 4904d14..8814781 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/IUniTaskSource.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/IUniTaskSource.cs @@ -19,7 +19,7 @@ namespace Cysharp.Threading.Tasks // similar as IValueTaskSource public interface IUniTaskSource -#if !UNITY_2018_3_OR_NEWER +#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0 : System.Threading.Tasks.Sources.IValueTaskSource #pragma warning disable CS0108 #endif @@ -30,7 +30,7 @@ namespace Cysharp.Threading.Tasks UniTaskStatus UnsafeGetStatus(); // only for debug use. -#if !UNITY_2018_3_OR_NEWER +#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0 #pragma warning restore CS0108 System.Threading.Tasks.Sources.ValueTaskSourceStatus System.Threading.Tasks.Sources.IValueTaskSource.GetStatus(short token) @@ -53,13 +53,13 @@ namespace Cysharp.Threading.Tasks } public interface IUniTaskSource : IUniTaskSource -#if !UNITY_2018_3_OR_NEWER +#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0 , System.Threading.Tasks.Sources.IValueTaskSource #endif { new T GetResult(short token); -#if !UNITY_2018_3_OR_NEWER +#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0 new public UniTaskStatus GetStatus(short token) { diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.cs index 7529b9a..26aa78a 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.cs @@ -78,7 +78,11 @@ namespace Cysharp.Threading.Tasks return default; } +#if NETSTANDARD2_0 + return self.AsValueTask(); +#else return new System.Threading.Tasks.ValueTask(self.source, self.token); +#endif } #endif @@ -439,7 +443,11 @@ namespace Cysharp.Threading.Tasks return new System.Threading.Tasks.ValueTask(self.result); } +#if NETSTANDARD2_0 + return self.AsValueTask(); +#else return new System.Threading.Tasks.ValueTask(self.source, self.token); +#endif } #endif