Browse Source

去掉TimerComponent回调式的用法,方便热重载

tanghai 4 năm trước cách đây
mục cha
commit
a614f9bdf7

+ 17 - 1
Server/Hotfix/Module/Actor/ActorMessageSenderComponentSystem.cs

@@ -3,6 +3,22 @@ using System.IO;
 
 namespace ET
 {
+    [Timer(TimerType.ActorMessageSenderChecker)]
+    public class ActorMessageSenderChecker: ATimer<ActorMessageSenderComponent>
+    {
+        public override void Run(ActorMessageSenderComponent self)
+        {
+            try
+            {
+                self.Check();
+            }
+            catch (Exception e)
+            {
+                Log.Error($"move timer error: {self.Id}\n{e}");
+            }
+        }
+    }
+    
     [ObjectSystem]
     public class ActorMessageSenderComponentAwakeSystem: AwakeSystem<ActorMessageSenderComponent>
     {
@@ -10,7 +26,7 @@ namespace ET
         {
             ActorMessageSenderComponent.Instance = self;
 
-            self.TimeoutCheckTimer = TimerComponent.Instance.NewRepeatedTimer(1000, self.Check);
+            self.TimeoutCheckTimer = TimerComponent.Instance.NewRepeatedTimer(1000, TimerType.ActorMessageSenderChecker, self);
         }
     }
 

+ 17 - 1
Server/Hotfix/Module/ActorLocation/ActorLocationSenderComponentSystem.cs

@@ -3,6 +3,22 @@ using System.IO;
 
 namespace ET
 {
+    [Timer(TimerType.ActorLocationSenderChecker)]
+    public class ActorLocationSenderChecker: ATimer<ActorLocationSenderComponent>
+    {
+        public override void Run(ActorLocationSenderComponent self)
+        {
+            try
+            {
+                self.Check();
+            }
+            catch (Exception e)
+            {
+                Log.Error($"move timer error: {self.Id}\n{e}");
+            }
+        }
+    }
+    
     [ObjectSystem]
     public class ActorLocationSenderComponentAwakeSystem: AwakeSystem<ActorLocationSenderComponent>
     {
@@ -12,7 +28,7 @@ namespace ET
 
             // 每10s扫描一次过期的actorproxy进行回收,过期时间是2分钟
             // 可能由于bug或者进程挂掉,导致ActorLocationSender发送的消息没有确认,结果无法自动删除,每一分钟清理一次这种ActorLocationSender
-            self.CheckTimer = TimerComponent.Instance.NewRepeatedTimer(10 * 1000, self.Check);
+            self.CheckTimer = TimerComponent.Instance.NewRepeatedTimer(10 * 1000, TimerType.ActorLocationSenderChecker, self);
         }
     }
 

+ 5 - 0
Server/Model/Server.Model.csproj

@@ -38,6 +38,11 @@
     <Compile Include="..\..\Unity\Codes\Model\Demo\Move\MoveComponent.cs">
       <Link>Demo\Move\MoveComponent.cs</Link>
     </Compile>
+    
+
+    <Compile Include="..\..\Unity\Codes\Model\Demo\TimerType.cs">
+      <Link>Demo\TimerType.cs</Link>
+    </Compile>
 
     <Compile Include="..\..\Unity\Codes\Model\Module\Config\**\*.cs">
       <Link>Module\Config\%(RecursiveDir)%(FileName)%(Extension)</Link>

+ 17 - 61
Unity/Codes/Hotfix/Demo/Move/MoveComponentSystem.cs

@@ -4,6 +4,22 @@ using UnityEngine;
 
 namespace ET
 {
+    [Timer(TimerType.MoveTimer)]
+    public class MoveTimer: ATimer<MoveComponent>
+    {
+        public override void Run(MoveComponent self)
+        {
+            try
+            {
+                self.MoveForward(false);
+            }
+            catch (Exception e)
+            {
+                Log.Error($"move timer error: {self.Id}\n{e}");
+            }
+        }
+    }
+    
     [ObjectSystem]
     public class MoveComponentDestroySystem: DestroySystem<MoveComponent>
     {
@@ -185,18 +201,7 @@ namespace ET
             self.StartTime = self.BeginTime;
             self.SetNextTarget();
 
-            self.MoveTimer = TimerComponent.Instance.NewFrameTimer(() =>
-                    {
-                        try
-                        {
-                            self.MoveForward(false);
-                        }
-                        catch (Exception e)
-                        {
-                            Log.Error($"move timer error: {unit.Id}\n{e}");
-                        }
-                    }
-            );
+            self.MoveTimer = TimerComponent.Instance.NewFrameTimer(TimerType.MoveTimer, self);
         }
 
         private static void SetNextTarget(this MoveComponent self)
@@ -269,55 +274,6 @@ namespace ET
             return true;
         }
 
-        public static bool MoveTo(this MoveComponent self, Vector3 target, float speed, int turnTime = 0, bool isTurnHorizontal = false)
-        {
-            if (speed < 0.001)
-            {
-                Log.Error($"speed is 0 {self.GetParent<Unit>().Config.Id} {self.GetParent<Unit>().Id} {speed}");
-                return false;
-            }
-
-            self.Stop();
-
-            self.IsTurnHorizontal = isTurnHorizontal;
-            self.TurnTime = turnTime;
-            self.Targets.Add(self.GetParent<Unit>().Position);
-            self.Targets.Add(target);
-            self.Speed = speed;
-
-            self.StartMove();
-            return true;
-        }
-
-        public static bool MoveTo(this MoveComponent self, List<Vector3> target, float speed, int turnTime = 0)
-        {
-            if (target.Count == 0)
-            {
-                return true;
-            }
-
-            if (Math.Abs(speed) < 0.001)
-            {
-                Log.Error($"speed is 0 {self.GetParent<Unit>().Config.Id} {self.GetParent<Unit>().Id}");
-                return false;
-            }
-
-            self.Stop();
-
-            foreach (Vector3 v in target)
-            {
-                self.Targets.Add(v);
-            }
-
-            self.IsTurnHorizontal = true;
-            self.TurnTime = turnTime;
-            self.Speed = speed;
-
-            self.StartMove();
-
-            return true;
-        }
-
         public static void Stop(this MoveComponent self)
         {
             if (self.Targets.Count > 0)

+ 17 - 1
Unity/Codes/Hotfix/Module/AI/AIComponentSystem.cs

@@ -3,13 +3,29 @@ using UnityEngine;
 
 namespace ET
 {
+    [Timer(TimerType.AITimer)]
+    public class AITimer: ATimer<AIComponent>
+    {
+        public override void Run(AIComponent self)
+        {
+            try
+            {
+                self.Check();
+            }
+            catch (Exception e)
+            {
+                Log.Error($"move timer error: {self.Id}\n{e}");
+            }
+        }
+    }
+    
     [ObjectSystem]
     public class AIComponentAwakeSystem: AwakeSystem<AIComponent, int>
     {
         public override void Awake(AIComponent self, int aiConfigId)
         {
             self.AIConfigId = aiConfigId;
-            self.Timer = TimerComponent.Instance.NewRepeatedTimer(1000, ()=> { self.Check(); });
+            self.Timer = TimerComponent.Instance.NewRepeatedTimer(1000, TimerType.AITimer, self);
         }
     }
 

+ 19 - 1
Unity/Codes/Hotfix/Module/Message/SessionIdleCheckerComponentSystem.cs

@@ -1,11 +1,29 @@
+using System;
+
 namespace ET
 {
+    [Timer(TimerType.SessionIdleChecker)]
+    public class SessionIdleChecker: ATimer<SessionIdleCheckerComponent>
+    {
+        public override void Run(SessionIdleCheckerComponent self)
+        {
+            try
+            {
+                self.Check();
+            }
+            catch (Exception e)
+            {
+                Log.Error($"move timer error: {self.Id}\n{e}");
+            }
+        }
+    }
+    
     [ObjectSystem]
     public class SessionIdleCheckerComponentAwakeSystem: AwakeSystem<SessionIdleCheckerComponent, int>
     {
         public override void Awake(SessionIdleCheckerComponent self, int checkInteral)
         {
-            self.RepeatedTimer = TimerComponent.Instance.NewRepeatedTimer(checkInteral, () => { self.Check(); });
+            self.RepeatedTimer = TimerComponent.Instance.NewRepeatedTimer(checkInteral, TimerType.SessionIdleChecker, self);
         }
     }
 

+ 21 - 0
Unity/Codes/Model/Core/Object/IAwakeSystem.cs

@@ -110,4 +110,25 @@ namespace ET
 
         public abstract void Awake(T self, A a, B b, C c);
     }
+    
+    [ObjectSystem]
+    public abstract class AwakeSystem<T, A, B, C, D> : IAwakeSystem<A, B, C, D>
+    {
+        public Type Type()
+        {
+            return typeof(T);
+        }
+		
+        public Type SystemType()
+        {
+            return typeof(IAwakeSystem<A, B, C, D>);
+        }
+
+        public void Run(object o, A a, B b, C c, D d)
+        {
+            this.Awake((T)o, a, b, c, d);
+        }
+
+        public abstract void Awake(T self, A a, B b, C c, D d);
+    }
 }

+ 27 - 13
Unity/Codes/Model/Core/ObjectWait.cs

@@ -51,32 +51,35 @@ namespace ET
 
         public class ResultCallback<K>: IDestroyRun where K : struct, IWaitType
         {
-            private readonly ETTask<K> tcs;
-            private readonly long timer;
+            private ETTask<K> tcs;
 
             public ResultCallback()
             {
                 this.tcs = ETTask<K>.Create(true);
             }
-            
-            public ResultCallback(long timer)
+
+            public bool IsDisposed
             {
-                this.timer = timer;
-                this.tcs = ETTask<K>.Create(true);
+                get
+                {
+                    return this.tcs == null;
+                }
             }
 
             public ETTask<K> Task => this.tcs;
 
             public void SetResult(K k)
             {
-                TimerComponent.Instance.Remove(this.timer);
-                this.tcs.SetResult(k);
+                var t = tcs;
+                this.tcs = null;
+                t.SetResult(k);
             }
 
             public void SetResult()
             {
-                TimerComponent.Instance.Remove(this.timer);
-                this.tcs.SetResult(new K() { Error = WaitTypeError.Destroy });
+                var t = tcs;
+                this.tcs = null;
+                t.SetResult(new K() { Error = WaitTypeError.Destroy });
             }
         }
 
@@ -108,12 +111,23 @@ namespace ET
 
         public async ETTask<T> Wait<T>(int timeout, ETCancellationToken cancellationToken = null) where T : struct, IWaitType
         {
-            long timerId = TimerComponent.Instance.NewOnceTimer(TimeHelper.ServerNow() + timeout, () =>
+            ResultCallback<T> tcs = new ResultCallback<T>();
+            async ETTask WaitTimeout()
             {
+                bool retV = await TimerComponent.Instance.WaitAsync(timeout, cancellationToken);
+                if (!retV)
+                {
+                    return;
+                }
+                if (tcs.IsDisposed)
+                {
+                    return;
+                }
                 Notify(new T() { Error = WaitTypeError.Timeout });
-            });
+            }
+            
+            WaitTimeout().Coroutine();
             
-            ResultCallback<T> tcs = new ResultCallback<T>(timerId);
             this.tcss.Add(typeof (T), tcs);
             
             void CancelAction()

+ 17 - 0
Unity/Codes/Model/Core/Timer/ITimer.cs

@@ -0,0 +1,17 @@
+namespace ET
+{
+    public interface ITimer
+    {
+        void Handle(object args);
+    }
+    
+    public abstract class ATimer<T>: ITimer where T: class
+    {
+        public void Handle(object args)
+        {
+            this.Run(args as T);
+        }
+
+        public abstract void Run(T t);
+    }
+}

+ 14 - 0
Unity/Codes/Model/Core/Timer/TimerAttribute.cs

@@ -0,0 +1,14 @@
+using System;
+
+namespace ET
+{
+    public class TimerAttribute: BaseAttribute
+    {
+        public int Type { get; }
+
+        public TimerAttribute(int type)
+        {
+            this.Type = type;
+        }
+    }
+}

+ 84 - 60
Unity/Codes/Model/Core/Entity/TimerComponent.cs → Unity/Codes/Model/Core/Timer/TimerComponent.cs

@@ -6,19 +6,19 @@ namespace ET
     public enum TimerClass
     {
         None,
-        OnceWaitTimer,
         OnceTimer,
         RepeatedTimer,
     }
     
     [ObjectSystem]
-    public class TimerActionAwakeSystem: AwakeSystem<TimerAction, TimerClass, long, object>
+    public class TimerActionAwakeSystem: AwakeSystem<TimerAction, TimerClass, long, int, object>
     {
-        public override void Awake(TimerAction self, TimerClass timerClass, long time, object callback)
+        public override void Awake(TimerAction self, TimerClass timerClass, long time, int type, object obj)
         {
             self.TimerClass = timerClass;
-            self.Callback = callback;
+            self.Object = obj;
             self.Time = time;
+            self.Type = type;
         }
     }
 
@@ -27,9 +27,10 @@ namespace ET
     {
         public override void Destroy(TimerAction self)
         {
-            self.Callback = null;
+            self.Object = null;
             self.Time = 0;
             self.TimerClass = TimerClass.None;
+            self.Type = 0;
         }
     }
     
@@ -37,9 +38,11 @@ namespace ET
     {
         public TimerClass TimerClass;
 
-        public object Callback;
+        public object Object;
 
         public long Time;
+
+        public int Type;
     }
 
     [ObjectSystem]
@@ -48,6 +51,7 @@ namespace ET
         public override void Awake(TimerComponent self)
         {
             TimerComponent.Instance = self;
+            self.Awake();
         }
     }
 
@@ -80,6 +84,37 @@ namespace ET
         // 记录最小时间,不用每次都去MultiMap取第一个值
         private long minTime;
 
+        private readonly Dictionary<int, ITimer> timerActions = new Dictionary<int, ITimer>();
+
+        public void Awake()
+        {
+            this.timerActions.Clear();
+
+            HashSet<Type> types = Game.EventSystem.GetTypes(typeof (TimerAttribute));
+
+            foreach (Type type in types)
+            {
+                ITimer iTimer = Activator.CreateInstance(type) as ITimer;
+                if (iTimer == null)
+                {
+                    Log.Error($"Timer Action {type.Name} 需要继承 ITimer");
+                    continue;
+                }
+                
+                object[] attrs = type.GetCustomAttributes(typeof(TimerAttribute), false);
+                if (attrs.Length == 0)
+                {
+                    continue;
+                }
+
+                foreach (object attr in attrs)
+                {
+                    TimerAttribute timerAttribute = attr as TimerAttribute;
+                    this.timerActions.Add(timerAttribute.Type, iTimer);
+                }
+            }
+        }
+
         public void Update()
         {
             if (this.TimeId.Count == 0)
@@ -134,26 +169,24 @@ namespace ET
         {
             switch (timerAction.TimerClass)
             {
-                case TimerClass.OnceWaitTimer:
-                {
-                    ETTask<bool> tcs = timerAction.Callback as ETTask<bool>;
-                    this.Remove(timerAction.Id);
-                    tcs.SetResult(true);
-                    break;
-                }
                 case TimerClass.OnceTimer:
                 {
-                    Action action = timerAction.Callback as Action;
+                    ETTask<bool> tcs = timerAction.Object as ETTask<bool>;
                     this.Remove(timerAction.Id);
-                    action?.Invoke();
+                    tcs.SetResult(true);
                     break;
                 }
                 case TimerClass.RepeatedTimer:
                 {
-                    Action action = timerAction.Callback as Action;
+                    int type = timerAction.Type;
                     long tillTime = TimeHelper.ServerNow() + timerAction.Time;
                     this.AddTimer(tillTime, timerAction);
-                    action?.Invoke();
+                    
+                    if (!this.timerActions.TryGetValue(type, out ITimer timer))
+                    {
+                        return;
+                    }
+                    timer.Handle(timerAction.Object);
                     break;
                 }
             }
@@ -168,15 +201,39 @@ namespace ET
             }
         }
 
+        public bool Remove(ref long id)
+        {
+            long i = id;
+            id = 0;
+            return this.Remove(i);
+        }
+        
+        private bool Remove(long id)
+        {
+            if (id == 0)
+            {
+                return false;
+            }
+
+            TimerAction timerAction = this.GetChild<TimerAction>(id);
+            if (timerAction == null)
+            {
+                return false;
+            }
+            timerAction.Dispose();
+            return true;
+        }
+
         public async ETTask<bool> WaitTillAsync(long tillTime, ETCancellationToken cancellationToken = null)
         {
-            if (TimeHelper.ServerNow() >= tillTime)
+            long timeNow = TimeHelper.ServerNow();
+            if (timeNow >= tillTime)
             {
                 return true;
             }
 
             ETTask<bool> tcs = ETTask<bool>.Create(true);
-            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, object>(TimerClass.OnceWaitTimer, 0, tcs, true);
+            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.OnceTimer, tillTime - timeNow, 0, tcs, true);
             this.AddTimer(tillTime, timer);
             long timerId = timer.Id;
 
@@ -216,7 +273,7 @@ namespace ET
 
             ETTask<bool> tcs = ETTask<bool>.Create(true);
             
-            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, object>(TimerClass.OnceWaitTimer, 0, tcs, true);
+            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.OnceTimer, time, 0, tcs, true);
             this.AddTimer(tillTime, timer);
             long timerId = timer.Id;
 
@@ -241,19 +298,19 @@ namespace ET
             return ret;
         }
 
-        public long NewFrameTimer(Action action)
+        public long NewFrameTimer(int type, object args)
         {
 #if NOT_UNITY
-			return NewRepeatedTimerInner(100, action);
+			return NewRepeatedTimerInner(100, type, args);
 #else
-            return NewRepeatedTimerInner(1, action);
+            return NewRepeatedTimerInner(1, type, args);
 #endif
         }
 
         /// <summary>
         /// 创建一个RepeatedTimer
         /// </summary>
-        private long NewRepeatedTimerInner(long time, Action action)
+        private long NewRepeatedTimerInner(long time, int type, object args)
         {
 #if NOT_UNITY
 			if (time < 100)
@@ -262,47 +319,14 @@ namespace ET
 			}
 #endif
             long tillTime = TimeHelper.ServerNow() + time;
-            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, object>(TimerClass.RepeatedTimer, time, action, true);
+            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.RepeatedTimer, time, type, args, true);
             this.AddTimer(tillTime, timer);
             return timer.Id;
         }
 
-        public long NewRepeatedTimer(long time, Action action)
+        public long NewRepeatedTimer(long time, int type, object args)
         {
-            return NewRepeatedTimerInner(time, action);
-        }
-
-        public void Remove(ref long id)
-        {
-            this.Remove(id);
-            id = 0;
-        }
-
-        public bool Remove(long id)
-        {
-            if (id == 0)
-            {
-                return false;
-            }
-
-            TimerAction timerAction = this.GetChild<TimerAction>(id);
-            if (timerAction == null)
-            {
-                return false;
-            }
-            timerAction.Dispose();
-            return true;
-        }
-
-        public long NewOnceTimer(long tillTime, Action action)
-        {
-            if (tillTime < TimeHelper.ServerNow())
-            {
-                Log.Error($"new once time too small: {tillTime}");
-            }
-            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, object>(TimerClass.OnceTimer, 0, action, true);
-            this.AddTimer(tillTime, timer);
-            return timer.Id;
+            return NewRepeatedTimerInner(time, type, args);
         }
     }
 }

+ 11 - 0
Unity/Codes/Model/Core/Timer/TimerType.cs

@@ -0,0 +1,11 @@
+namespace ET
+{
+    public static partial class TimerType
+    {
+        // 框架层0-1000,逻辑层的timer type从1000起
+        public const int WaitTimer = 0;
+        public const int SessionIdleChecker = 1;
+        public const int ActorLocationSenderChecker = 2;
+        public const int ActorMessageSenderChecker = 3;
+    }
+}

+ 9 - 0
Unity/Codes/Model/Demo/TimerType.cs

@@ -0,0 +1,9 @@
+namespace ET
+{
+    public static partial class TimerType
+    {
+        // 框架层0-1000,逻辑层的timer type从1000起
+        public const int MoveTimer = 1001;
+        public const int AITimer = 1002;
+    }
+}