using System.Runtime.CompilerServices; using Fantasy; using Fantasy.Async; using Fantasy.Entitas.Interface; using Fantasy.Helper; namespace Hotfix; public static class EntityTimeOutComponentSystem { public static void SetInterval(this EntityTimeOutComponent self, int interval) { self.Interval = interval; //self.NextTime = TimeHelper.Now + interval; } public static bool CheckInterval(this EntityTimeOutComponent self) { if (TimeHelper.Now < self.NextTime) { Log.Warning("请求过于频繁" + self.NextTime +"TimeOut" + TimeHelper.Now); return false; } self.NextTime = TimeHelper.Now + self.Interval; return true; } public static void TimeOut(this EntityTimeOutComponent self, long time,Func? callback = null) { var scene = self.Scene; var parentRunTimeId = self.Parent.RuntimeId; self.TimeId = scene.TimerComponent.Net.OnceTimer(time, () => { self.Handler(parentRunTimeId,callback).Coroutine(); }); } public static async FTask Handler(this EntityTimeOutComponent self,long parentRunTimeId , Func? callback) { var selfParent = self.Parent; if (self.Parent == null || parentRunTimeId != self.Parent.RuntimeId ) { return; } if (callback != null) { await callback(); } self.TimeId = 0; selfParent.Dispose(); Log.Debug($"session : {selfParent.RuntimeId} Dispose"); } } public class SessionTimeOutComponentAwakeSystem : AwakeSystem { protected override void Awake(EntityTimeOutComponent self) { } } public class SessionTimeOutComponentDestroySystem : DestroySystem { protected override void Destroy(EntityTimeOutComponent self) { if (self.TimeId != 0) { self.Scene.TimerComponent.Net.Remove(self.TimeId); } self.NextTime = 0; self.Interval = 0; } }