From 741a6588ffcaf949552f5a1f98b2748d471fbc12 Mon Sep 17 00:00:00 2001 From: zhangxl <1062808664@qq.com> Date: Fri, 19 Jul 2024 10:53:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0token=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/DemoGame/GameScript/Hotfix/Pool.meta | 8 ++ .../GameScript/Hotfix/Pool/TokenInfo.cs | 84 +++++++++++++++++++ .../GameScript/Hotfix/Pool/TokenInfo.cs.meta | 3 + .../GameScript/Hotfix/Pool/TokenPool.cs | 35 ++++++++ .../GameScript/Hotfix/Pool/TokenPool.cs.meta | 3 + .../GameScript/Hotfix/Pool/UniTaskPool.cs | 29 +++++++ .../Hotfix/Pool/UniTaskPool.cs.meta | 3 + 7 files changed, 165 insertions(+) create mode 100644 Assets/DemoGame/GameScript/Hotfix/Pool.meta create mode 100644 Assets/DemoGame/GameScript/Hotfix/Pool/TokenInfo.cs create mode 100644 Assets/DemoGame/GameScript/Hotfix/Pool/TokenInfo.cs.meta create mode 100644 Assets/DemoGame/GameScript/Hotfix/Pool/TokenPool.cs create mode 100644 Assets/DemoGame/GameScript/Hotfix/Pool/TokenPool.cs.meta create mode 100644 Assets/DemoGame/GameScript/Hotfix/Pool/UniTaskPool.cs create mode 100644 Assets/DemoGame/GameScript/Hotfix/Pool/UniTaskPool.cs.meta diff --git a/Assets/DemoGame/GameScript/Hotfix/Pool.meta b/Assets/DemoGame/GameScript/Hotfix/Pool.meta new file mode 100644 index 0000000..91766d2 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Pool.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2de8906198a5f2d4889795ad418a7418 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/DemoGame/GameScript/Hotfix/Pool/TokenInfo.cs b/Assets/DemoGame/GameScript/Hotfix/Pool/TokenInfo.cs new file mode 100644 index 0000000..b22ef19 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Pool/TokenInfo.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace ZC +{ + public interface ITokenInfo + { + public int id { get; } + public bool isDispose { get; } + public CancellationTokenSource ets { get; } + public CancellationToken token { get; } + public void Add(Action callback); + public void Remove(Action callback); + public void Cancel(); + public void Dispose(); + } + + public class TokenInfo : ITokenInfo + { + private int _id; + public bool _isDispose; + private CancellationTokenSource _ets; + private List callbacks; + + public int id => this._id; + public CancellationTokenSource ets => this._ets; + public CancellationToken token => this._ets.Token; + public bool isDispose => this._isDispose; + + public Action DisposeAction; + + public TokenInfo(int id) + { + this._id = id; + this.callbacks = new List(); + } + + public void Init() + { + this._ets = new CancellationTokenSource(); + this._ets.Token.Register(this.Callback); + this._isDispose = false; + } + + private void Callback() + { + if (this.callbacks.Count > 0) + foreach (var callback in this.callbacks) + { + callback?.Invoke(); + } + } + + public void Add(Action callback) + { + this.callbacks.Add(callback); + } + + public void Remove(Action callback) + { + if (this.callbacks.Contains(callback)) + this.callbacks.Remove(callback); + } + + public void Cancel() + { + this._ets.Cancel(); + this.Dispose(); + } + + public void Dispose() + { + if (this._isDispose) return; + + this._id = 0; + this._ets.Dispose(); + this.callbacks.Clear(); + this._ets = null; + this._isDispose = true; + this.DisposeAction?.Invoke(this); + } + } +} \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/Pool/TokenInfo.cs.meta b/Assets/DemoGame/GameScript/Hotfix/Pool/TokenInfo.cs.meta new file mode 100644 index 0000000..7c31cc3 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Pool/TokenInfo.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3fe3e1c90d1f442c9b5b248b671c9321 +timeCreated: 1718952740 \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/Pool/TokenPool.cs b/Assets/DemoGame/GameScript/Hotfix/Pool/TokenPool.cs new file mode 100644 index 0000000..c22c01a --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Pool/TokenPool.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; + +namespace ZC +{ + public static class TokenPool + { + private static List runningInfos = new List(); + private static List disposeInfos = new List(); + + public static ITokenInfo GetToken() + { + TokenInfo info; + if (disposeInfos.Count <= 0) + { + info = new TokenInfo(1); + info.DisposeAction = Dispose; + } + else + { + info = disposeInfos[0]; + disposeInfos.RemoveAt(0); + } + + info.Init(); + runningInfos.Add(info); + return info; + } + + private static void Dispose(TokenInfo info) + { + runningInfos.Remove(info); + disposeInfos.Add(info); + } + } +} \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/Pool/TokenPool.cs.meta b/Assets/DemoGame/GameScript/Hotfix/Pool/TokenPool.cs.meta new file mode 100644 index 0000000..d0bf084 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Pool/TokenPool.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5ca25565713545c080d1504d68cb7f3f +timeCreated: 1718952695 \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/Pool/UniTaskPool.cs b/Assets/DemoGame/GameScript/Hotfix/Pool/UniTaskPool.cs new file mode 100644 index 0000000..610c04e --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Pool/UniTaskPool.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using System.Threading; +using Cysharp.Threading.Tasks; + +namespace ZC +{ + public static class UniTaskPool + { + private static List runningInfos = new List(); + private static List disposeInfos = new List(); + + public static void Get(UniTask task) + { + UniTaskInfo info = new UniTaskInfo(task); + info._info.Cancel(); + } + } + + public class UniTaskInfo + { + private UniTask _task; + public TokenInfo _info; + + public UniTaskInfo(UniTask task) + { + this._task = task; + } + } +} \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/Pool/UniTaskPool.cs.meta b/Assets/DemoGame/GameScript/Hotfix/Pool/UniTaskPool.cs.meta new file mode 100644 index 0000000..1acea97 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Pool/UniTaskPool.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fd3f2b265794484ba07b73bbc2adde50 +timeCreated: 1719125352 \ No newline at end of file