Эх сурвалжийг харах

TimerComponent跟CoroutineLockComponent挪到Fiber中,变成Fiber的成员

tanghai 2 жил өмнө
parent
commit
ed0442f4bd
28 өөрчлөгдсөн 308 нэмэгдсэн , 301 устгасан
  1. 7 0
      Unity/Assets/Scripts/Core/Fiber/Fiber.cs
  2. 2 2
      Unity/Assets/Scripts/Core/Fiber/MailBoxComponent.cs
  3. 35 20
      Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLock.cs
  4. 32 22
      Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockComponent.cs
  5. 48 43
      Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockQueue.cs
  6. 29 22
      Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockQueueType.cs
  7. 88 87
      Unity/Assets/Scripts/Core/Fiber/Module/Timer/TimerComponent.cs
  8. 1 1
      Unity/Assets/Scripts/Core/Helper/ETCancelationTokenHelper.cs
  9. 0 2
      Unity/Assets/Scripts/Core/World/Module/Fiber/FiberManager.cs
  10. 4 4
      Unity/Assets/Scripts/Hotfix/Client/Demo/AI/AI_Attack.cs
  11. 1 1
      Unity/Assets/Scripts/Hotfix/Client/Demo/Ping/PingComponentSystem.cs
  12. 1 1
      Unity/Assets/Scripts/Hotfix/Client/Demo/Router/RouterAddressComponentSystem.cs
  13. 3 2
      Unity/Assets/Scripts/Hotfix/Client/Demo/Router/RouterCheckComponentSystem.cs
  14. 3 2
      Unity/Assets/Scripts/Hotfix/Client/Demo/Router/RouterHelper.cs
  15. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Demo/Gate/GateSessionKeyComponentSystem.cs
  16. 1 1
      Unity/Assets/Scripts/Hotfix/Server/LockStep/Map/C2Room_ChangeSceneFinishHandler.cs
  17. 6 11
      Unity/Assets/Scripts/Hotfix/Server/Module/ActorLocation/ActorLocationSenderComponentSystem.cs
  18. 5 5
      Unity/Assets/Scripts/Hotfix/Server/Module/ActorLocation/LocationOneTypeSystem.cs
  19. 14 14
      Unity/Assets/Scripts/Hotfix/Server/Module/DB/DBComponentSystem.cs
  20. 3 8
      Unity/Assets/Scripts/Hotfix/Share/Module/AI/AIComponentSystem.cs
  21. 2 7
      Unity/Assets/Scripts/Hotfix/Share/Module/Actor/ActorSenderComponentSystem.cs
  22. 2 7
      Unity/Assets/Scripts/Hotfix/Share/Module/Message/SessionAcceptTimeoutComponentSystem.cs
  23. 2 7
      Unity/Assets/Scripts/Hotfix/Share/Module/Message/SessionIdleCheckerComponentSystem.cs
  24. 2 7
      Unity/Assets/Scripts/Hotfix/Share/Module/Move/MoveComponentSystem.cs
  25. 2 10
      Unity/Assets/Scripts/Model/Server/Module/ActorLocation/LocationComponent.cs
  26. 1 1
      Unity/Assets/Scripts/Model/Share/Module/ObjectWait/ObjectWait.cs
  27. 7 7
      Unity/Assets/Scripts/ModelView/Client/Module/Resource/ResourcesComponent.cs
  28. 6 6
      Unity/Assets/Scripts/ModelView/Client/Module/Resource/ResourcesLoaderComponent.cs

+ 7 - 0
Unity/Assets/Scripts/Core/Fiber/Fiber.cs

@@ -38,6 +38,8 @@ namespace ET
         public IdGenerater IdGenerater { get; }
         public Mailboxes Mailboxes { get; }
         public ThreadSynchronizationContext ThreadSynchronizationContext { get; }
+        public TimerComponent TimerComponent { get; }
+        public CoroutineLockComponent CoroutineLockComponent { get; }
 
         private readonly Queue<ETTask> frameFinishTasks = new();
 
@@ -52,6 +54,8 @@ namespace ET
             this.TimeInfo = new TimeInfo();
             this.IdGenerater = new IdGenerater(process, this.TimeInfo);
             this.Mailboxes = new Mailboxes();
+            this.TimerComponent = new TimerComponent(this.TimeInfo);
+            this.CoroutineLockComponent = new CoroutineLockComponent(this.TimerComponent);
             this.ThreadSynchronizationContext = new();
             this.Root = new Scene(this, id, 1, sceneType, name);
         }
@@ -62,6 +66,8 @@ namespace ET
             this.ThreadSynchronizationContext.Update();
             this.TimeInfo.Update();
             this.EntitySystem.Update();
+            this.TimerComponent.Update();
+            this.CoroutineLockComponent.Update();
         }
         
         public void LateUpdate()
@@ -94,6 +100,7 @@ namespace ET
                 return;
             }
             this.IsDisposed = true;
+            this.Id = 0;
             this.Root.Dispose();
         }
     }

+ 2 - 2
Unity/Assets/Scripts/Core/Fiber/MailBoxComponent.cs

@@ -10,7 +10,7 @@
             self.MailBoxType = mailBoxType;
             self.ParentInstanceId = self.Parent.InstanceId;
             fiber.Mailboxes.Add(self);
-            self.CoroutineLockComponent = fiber.Root.GetComponent<CoroutineLockComponent>();
+            self.CoroutineLockComponent = fiber.CoroutineLockComponent;
         }
         
         [EntitySystem]
@@ -44,6 +44,6 @@
         // Mailbox的类型
         public MailBoxType MailBoxType { get; set; }
 
-        public EntityRef<CoroutineLockComponent> CoroutineLockComponent { get; set; }
+        public CoroutineLockComponent CoroutineLockComponent { get; set; }
     }
 }

+ 35 - 20
Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLock.cs

@@ -2,31 +2,46 @@ using System;
 
 namespace ET
 {
-    public static partial class CoroutineLockSystem
+    public class CoroutineLock: IDisposable
     {
-        [EntitySystem]
-        private static void Awake(this CoroutineLock self, int type, long k, int count)
+        private CoroutineLockComponent coroutineLockComponent;
+        private int type;
+        private long key;
+        private int level;
+        
+        public static CoroutineLock Create(CoroutineLockComponent coroutineLockComponent, int type, long k, int count)
+        {
+            CoroutineLock coroutineLock = ObjectPool.Instance.Fetch<CoroutineLock>();
+            coroutineLock.coroutineLockComponent = coroutineLockComponent;
+            coroutineLock.type = type;
+            coroutineLock.key = k;
+            coroutineLock.level = count;
+            return coroutineLock;
+        }
+
+        public bool IsDisposed
         {
-            self.type = type;
-            self.key = k;
-            self.level = count;
+            get
+            {
+                return this.coroutineLockComponent == null;
+            }
         }
         
-        [EntitySystem]
-        private static void Destroy(this CoroutineLock self)
+        public void Dispose()
         {
-            self.Scene<CoroutineLockComponent>().RunNextCoroutine(self.type, self.key, self.level + 1);
-            self.type = CoroutineLockType.None;
-            self.key = 0;
-            self.level = 0;
+            if (this.IsDisposed)
+            {
+                return;
+            }
+            
+            coroutineLockComponent.RunNextCoroutine(this.type, this.key, this.level + 1);
+            
+            this.type = CoroutineLockType.None;
+            this.key = 0;
+            this.level = 0;
+            this.coroutineLockComponent = null;
+            
+            ObjectPool.Instance.Recycle(this);
         }
     }
-    
-    [ChildOf(typeof(CoroutineLockQueue))]
-    public class CoroutineLock: Entity, IAwake<int, long, int>, IDestroy
-    {
-        public int type;
-        public long key;
-        public int level;
-    }
 }

+ 32 - 22
Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockComponent.cs

@@ -3,20 +3,33 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    public static partial class CoroutineLockComponentSystem
+    public class CoroutineLockComponent: IDisposable
     {
-        [EntitySystem]
-        public static void Update(this CoroutineLockComponent self)
+        public readonly TimerComponent TimerComponent;
+        private readonly Dictionary<int, CoroutineLockQueueType> dictionary = new();
+        private readonly Queue<(int, long, int)> nextFrameRun = new();
+
+        public CoroutineLockComponent(TimerComponent timerComponent)
+        {
+            this.TimerComponent = timerComponent;
+        }
+
+        public void Dispose()
+        {
+            this.nextFrameRun.Clear();
+        }
+
+        public void Update()
         {
             // 循环过程中会有对象继续加入队列
-            while (self.nextFrameRun.Count > 0)
+            while (this.nextFrameRun.Count > 0)
             {
-                (int coroutineLockType, long key, int count) = self.nextFrameRun.Dequeue();
-                self.Notify(coroutineLockType, key, count);
+                (int coroutineLockType, long key, int count) = this.nextFrameRun.Dequeue();
+                this.Notify(coroutineLockType, key, count);
             }
         }
 
-        public static void RunNextCoroutine(this CoroutineLockComponent self, int coroutineLockType, long key, int level)
+        public void RunNextCoroutine(int coroutineLockType, long key, int level)
         {
             // 一个协程队列一帧处理超过100个,说明比较多了,打个warning,检查一下是否够正常
             if (level == 100)
@@ -24,32 +37,29 @@ namespace ET
                 Log.Warning($"too much coroutine level: {coroutineLockType} {key}");
             }
 
-            self.nextFrameRun.Enqueue((coroutineLockType, key, level));
+            this.nextFrameRun.Enqueue((coroutineLockType, key, level));
         }
 
-        public static async ETTask<CoroutineLock> Wait(this CoroutineLockComponent self, int coroutineLockType, long key, int time = 60000)
+        public async ETTask<CoroutineLock> Wait(int coroutineLockType, long key, int time = 60000)
         {
-            CoroutineLockQueueType coroutineLockQueueType = self.GetChild<CoroutineLockQueueType>(coroutineLockType) ?? self.AddChildWithId<CoroutineLockQueueType>(coroutineLockType);
+            CoroutineLockQueueType coroutineLockQueueType;
+            if (!this.dictionary.TryGetValue(coroutineLockType, out coroutineLockQueueType))
+            {
+                coroutineLockQueueType = new CoroutineLockQueueType(this, coroutineLockType);
+                this.dictionary.Add(coroutineLockType, coroutineLockQueueType);
+            }
             return await coroutineLockQueueType.Wait(key, time);
         }
 
-        private static void Notify(this CoroutineLockComponent self, int coroutineLockType, long key, int level)
+        private void Notify(int coroutineLockType, long key, int level)
         {
-            CoroutineLockQueueType coroutineLockQueueType = self.GetChild<CoroutineLockQueueType>(coroutineLockType);
-            if (coroutineLockQueueType == null)
+            CoroutineLockQueueType coroutineLockQueueType;
+            if (!this.dictionary.TryGetValue(coroutineLockType, out coroutineLockQueueType))
             {
                 return;
             }
+            
             coroutineLockQueueType.Notify(key, level);
         }
     }
-    
-    [ComponentOf(typeof(Scene))]
-    public class CoroutineLockComponent: Entity, IAwake, IScene, IUpdate
-    {
-        public Fiber Fiber { get; set; }
-        public SceneType SceneType { get; set; }
-        
-        public readonly Queue<(int, long, int)> nextFrameRun = new();
-    }
 }

+ 48 - 43
Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockQueue.cs

@@ -3,88 +3,93 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    public static partial class CoroutineLockQueueSystem
+    public class CoroutineLockQueue: IDisposable
     {
-        [EntitySystem]
-        private static void Awake(this CoroutineLockQueue self, int type)
+        private CoroutineLockComponent coroutineLockComponent;
+        private int type;
+        private long key;
+        
+        public static CoroutineLockQueue Create(CoroutineLockComponent coroutineLockComponent, int type, long key)
         {
-            self.type = type;
+            CoroutineLockQueue coroutineLockQueue = ObjectPool.Instance.Fetch<CoroutineLockQueue>();
+            coroutineLockQueue.coroutineLockComponent = coroutineLockComponent; 
+            coroutineLockQueue.type = type;
+            coroutineLockQueue.key = key;
+            return coroutineLockQueue;
         }
+
+        private CoroutineLock currentCoroutineLock;
         
-        [EntitySystem]
-        private static void Destroy(this CoroutineLockQueue self)
+        private readonly Queue<WaitCoroutineLock> queue = new Queue<WaitCoroutineLock>();
+
+        public int Count
         {
-            self.queue.Clear();
-            self.type = 0;
-            self.CurrentCoroutineLock = null;
+            get
+            {
+                return this.queue.Count;
+            }
         }
-        
-        public static async ETTask<CoroutineLock> Wait(this CoroutineLockQueue self, int time)
+
+        public async ETTask<CoroutineLock> Wait(int time)
         {
-            if (self.CurrentCoroutineLock == null)
+            if (this.currentCoroutineLock == null || this.currentCoroutineLock.IsDisposed)
             {
-                self.CurrentCoroutineLock = self.AddChild<CoroutineLock, int, long, int>(self.type, self.Id, 1, true);
-                return self.CurrentCoroutineLock;
+                this.currentCoroutineLock = CoroutineLock.Create(this.coroutineLockComponent, type, key, 1);
+                return this.currentCoroutineLock;
             }
 
             WaitCoroutineLock waitCoroutineLock = WaitCoroutineLock.Create();
-            self.queue.Enqueue(waitCoroutineLock);
+            this.queue.Enqueue(waitCoroutineLock);
             if (time > 0)
             {
-                long tillTime = self.Fiber().TimeInfo.ClientFrameTime() + time;
-                self.Root().GetComponent<TimerComponent>().NewOnceTimer(tillTime, TimerCoreInvokeType.CoroutineTimeout, waitCoroutineLock);
+                TimerComponent timerComponent = this.coroutineLockComponent.TimerComponent;
+                long tillTime = timerComponent.TimeInfo.ClientFrameTime() + time;
+                timerComponent.NewOnceTimer(tillTime, TimerCoreInvokeType.CoroutineTimeout, waitCoroutineLock);
             }
-            self.CurrentCoroutineLock = await waitCoroutineLock.Wait();
-            return self.CurrentCoroutineLock;
+            this.currentCoroutineLock = await waitCoroutineLock.Wait();
+            return this.currentCoroutineLock;
         }
 
-        public static void Notify(this CoroutineLockQueue self, int level)
+        public void Notify(int level)
         {
             // 有可能WaitCoroutineLock已经超时抛出异常,所以要找到一个未处理的WaitCoroutineLock
-            while (self.queue.Count > 0)
+            while (this.queue.Count > 0)
             {
-                WaitCoroutineLock waitCoroutineLock = self.queue.Dequeue();
+                WaitCoroutineLock waitCoroutineLock = queue.Dequeue();
 
                 if (waitCoroutineLock.IsDisposed())
                 {
                     continue;
                 }
 
-                CoroutineLock coroutineLock = self.AddChild<CoroutineLock, int, long, int>(self.type, self.Id, level, true);
+                CoroutineLock coroutineLock = CoroutineLock.Create(this.coroutineLockComponent, type, key, level);
 
                 waitCoroutineLock.SetResult(coroutineLock);
                 break;
             }
         }
-    }
-    
-    [ChildOf(typeof(CoroutineLockQueueType))]
-    public class CoroutineLockQueue: Entity, IAwake<int>, IDestroy
-    {
-        public int type;
-
-        private EntityRef<CoroutineLock> currentCoroutineLock;
-
-        public CoroutineLock CurrentCoroutineLock
+        
+        public bool IsDisposed
         {
             get
             {
-                return this.currentCoroutineLock;
-            }
-            set
-            {
-                this.currentCoroutineLock = value;
+                return this.coroutineLockComponent == null;
             }
         }
-        
-        public Queue<WaitCoroutineLock> queue = new();
 
-        public int Count
+        public void Dispose()
         {
-            get
+            if (this.IsDisposed)
             {
-                return this.queue.Count;
+                return;
             }
+            
+            this.queue.Clear();
+            this.key = 0;
+            this.type = 0;
+            this.currentCoroutineLock = null;
+            this.coroutineLockComponent = null;
+            ObjectPool.Instance.Recycle(this);
         }
     }
 }

+ 29 - 22
Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockQueueType.cs

@@ -1,40 +1,52 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 
 namespace ET
 {
-    [FriendOf(typeof(CoroutineLockQueueType))]
-    public static partial class CoroutineLockQueueTypeSystem
+    public class CoroutineLockQueueType
     {
-        [EntitySystem]
-        private static void Awake(this CoroutineLockQueueType self)
+        private readonly CoroutineLockComponent coroutineLockComponent;
+        
+        private readonly int type;
+        
+        private readonly Dictionary<long, CoroutineLockQueue> coroutineLockQueues = new();
+
+        public CoroutineLockQueueType(CoroutineLockComponent coroutineLockComponent, int type)
         {
+            this.coroutineLockComponent = coroutineLockComponent;
+            this.type = type;
         }
-        
-        public static CoroutineLockQueue Get(this CoroutineLockQueueType self, long key)
+
+        private CoroutineLockQueue Get(long key)
         {
-            return self.GetChild<CoroutineLockQueue>(key);
+            this.coroutineLockQueues.TryGetValue(key, out CoroutineLockQueue queue);
+            return queue;
         }
 
-        public static CoroutineLockQueue New(this CoroutineLockQueueType self, long key)
+        private CoroutineLockQueue New(long key)
         {
-            CoroutineLockQueue queue = self.AddChildWithId<CoroutineLockQueue, int>(key, (int)self.Id, true);
+            CoroutineLockQueue queue = CoroutineLockQueue.Create(this.coroutineLockComponent, this.type, key);
+            this.coroutineLockQueues.Add(key, queue);
             return queue;
         }
 
-        public static void Remove(this CoroutineLockQueueType self, long key)
+        private void Remove(long key)
         {
-            self.RemoveChild(key);
+            if (this.coroutineLockQueues.Remove(key, out CoroutineLockQueue queue))
+            {
+                queue.Dispose();
+            }
         }
 
-        public static async ETTask<CoroutineLock> Wait(this CoroutineLockQueueType self, long key, int time)
+        public async ETTask<CoroutineLock> Wait(long key, int time)
         {
-            CoroutineLockQueue queue = self.Get(key) ?? self.New(key);
+            CoroutineLockQueue queue = this.Get(key) ?? this.New(key);
             return await queue.Wait(time);
         }
 
-        public static void Notify(this CoroutineLockQueueType self, long key, int level)
+        public void Notify(long key, int level)
         {
-            CoroutineLockQueue queue = self.Get(key);
+            CoroutineLockQueue queue = this.Get(key);
             if (queue == null)
             {
                 return;
@@ -42,15 +54,10 @@ namespace ET
             
             if (queue.Count == 0)
             {
-                self.Remove(key);
+                this.Remove(key);
             }
 
             queue.Notify(level);
         }
     }
-    
-    [ChildOf(typeof(CoroutineLockComponent))]
-    public class CoroutineLockQueueType: Entity, IAwake
-    {
-    }
 }

+ 88 - 87
Unity/Assets/Scripts/Core/Fiber/Module/Timer/TimerComponent.cs

@@ -53,24 +53,55 @@ namespace ET
         public object Args;
     }
 
-    public static partial class TimerComponentSystem
+    public class TimerComponent
     {
-        [EntitySystem]
-        private static void Update(this TimerComponent self)
+        public readonly TimeInfo TimeInfo;
+        /// <summary>
+        /// key: time, value: timer id
+        /// </summary>
+        private readonly MultiMap<long, long> TimeId = new(1000);
+
+        private readonly Queue<long> timeOutTime = new();
+
+        private readonly Queue<long> timeOutTimerIds = new();
+
+        private readonly Dictionary<long, TimerAction> timerActions = new();
+
+        private long idGenerator;
+
+        // 记录最小时间,不用每次都去MultiMap取第一个值
+        private long minTime = long.MaxValue;
+
+        public TimerComponent(TimeInfo timeInfo)
+        {
+            this.TimeInfo = timeInfo;
+        }
+
+        private long GetId()
+        {
+            return ++this.idGenerator;
+        }
+
+        private long GetNow()
+        {
+            return this.TimeInfo.ClientFrameTime();
+        }
+
+        public void Update()
         {
-            if (self.timeId.Count == 0)
+            if (this.TimeId.Count == 0)
             {
                 return;
             }
 
-            long timeNow = self.GetNow();
+            long timeNow = GetNow();
 
-            if (timeNow < self.minTime)
+            if (timeNow < this.minTime)
             {
                 return;
             }
 
-            using (var enumerator = self.timeId.GetEnumerator())
+            using (var enumerator = this.TimeId.GetEnumerator())
             {
                 while (enumerator.MoveNext())
                 {
@@ -78,55 +109,45 @@ namespace ET
                     long k = kv.Key;
                     if (k > timeNow)
                     {
-                        self.minTime = k;
+                        this.minTime = k;
                         break;
                     }
 
-                    self.timeOutTime.Enqueue(k);
+                    this.timeOutTime.Enqueue(k);
                 }
             }
 
-            while (self.timeOutTime.Count > 0)
+            while (this.timeOutTime.Count > 0)
             {
-                long time = self.timeOutTime.Dequeue();
-                var list = self.timeId[time];
+                long time = this.timeOutTime.Dequeue();
+                var list = this.TimeId[time];
                 for (int i = 0; i < list.Count; ++i)
                 {
                     long timerId = list[i];
-                    self.timeOutTimerIds.Enqueue(timerId);
+                    this.timeOutTimerIds.Enqueue(timerId);
                 }
-                self.timeId.Remove(time);
+                this.TimeId.Remove(time);
             }
 
-            if (self.timeId.Count == 0)
+            if (this.TimeId.Count == 0)
             {
-                self.minTime = long.MaxValue;
+                this.minTime = long.MaxValue;
             }
 
-            while (self.timeOutTimerIds.Count > 0)
+            while (this.timeOutTimerIds.Count > 0)
             {
-                long timerId = self.timeOutTimerIds.Dequeue();
+                long timerId = this.timeOutTimerIds.Dequeue();
 
-                if (!self.timerActions.Remove(timerId, out TimerAction timerAction))
+                if (!this.timerActions.Remove(timerId, out TimerAction timerAction))
                 {
                     continue;
                 }
                 
-                self.Run(timerAction);
+                this.Run(timerAction);
             }
         }
-        
-        private static long GetId(this TimerComponent self)
-        {
-            return ++self.idGenerator;
-        }
 
-        private static long GetNow(this TimerComponent self)
-        {
-            return self.Fiber().TimeInfo.ClientFrameTime();
-        }
-
-        private static void Run(this TimerComponent self, TimerAction timerAction)
+        private void Run(TimerAction timerAction)
         {
             switch (timerAction.TimerClass)
             {
@@ -145,41 +166,41 @@ namespace ET
                 }
                 case TimerClass.RepeatedTimer:
                 {                    
-                    long timeNow = self.GetNow();
+                    long timeNow = GetNow();
                     timerAction.StartTime = timeNow;
-                    self.AddTimer(timerAction);
+                    this.AddTimer(timerAction);
                     EventSystem.Instance.Invoke(timerAction.Type, new TimerCallback() { Args = timerAction.Object });
                     break;
                 }
             }
         }
 
-        private static void AddTimer(this TimerComponent self, TimerAction timer)
+        private void AddTimer(TimerAction timer)
         {
             long tillTime = timer.StartTime + timer.Time;
-            self.timeId.Add(tillTime, timer.Id);
-            self.timerActions.Add(timer.Id, timer);
-            if (tillTime < self.minTime)
+            this.TimeId.Add(tillTime, timer.Id);
+            this.timerActions.Add(timer.Id, timer);
+            if (tillTime < this.minTime)
             {
-                self.minTime = tillTime;
+                this.minTime = tillTime;
             }
         }
 
-        public static bool Remove(this TimerComponent self, ref long id)
+        public bool Remove(ref long id)
         {
             long i = id;
             id = 0;
-            return self.Remove(i);
+            return this.Remove(i);
         }
 
-        private static bool Remove(this TimerComponent self, long id)
+        private bool Remove(long id)
         {
             if (id == 0)
             {
                 return false;
             }
 
-            if (!self.timerActions.Remove(id, out TimerAction timerAction))
+            if (!this.timerActions.Remove(id, out TimerAction timerAction))
             {
                 return false;
             }
@@ -187,22 +208,22 @@ namespace ET
             return true;
         }
 
-        public static async ETTask WaitTillAsync(this TimerComponent self, long tillTime, ETCancellationToken cancellationToken = null)
+        public async ETTask WaitTillAsync(long tillTime, ETCancellationToken cancellationToken = null)
         {
-            long timeNow = self.GetNow();
+            long timeNow = GetNow();
             if (timeNow >= tillTime)
             {
                 return;
             }
 
             ETTask tcs = ETTask.Create(true);
-            TimerAction timer = TimerAction.Create(self.GetId(), TimerClass.OnceWaitTimer, timeNow, tillTime - timeNow, 0, tcs);
-            self.AddTimer(timer);
+            TimerAction timer = TimerAction.Create(this.GetId(), TimerClass.OnceWaitTimer, timeNow, tillTime - timeNow, 0, tcs);
+            this.AddTimer(timer);
             long timerId = timer.Id;
 
             void CancelAction()
             {
-                if (self.Remove(timerId))
+                if (this.Remove(timerId))
                 {
                     tcs.SetResult();
                 }
@@ -219,28 +240,28 @@ namespace ET
             }
         }
 
-        public static async ETTask WaitFrameAsync(this TimerComponent self, ETCancellationToken cancellationToken = null)
+        public async ETTask WaitFrameAsync(ETCancellationToken cancellationToken = null)
         {
-            await self.WaitAsync(1, cancellationToken);
+            await this.WaitAsync(1, cancellationToken);
         }
 
-        public static async ETTask WaitAsync(this TimerComponent self, long time, ETCancellationToken cancellationToken = null)
+        public async ETTask WaitAsync(long time, ETCancellationToken cancellationToken = null)
         {
             if (time == 0)
             {
                 return;
             }
 
-            long timeNow = self.GetNow();
+            long timeNow = GetNow();
 
             ETTask tcs = ETTask.Create(true);
-            TimerAction timer = TimerAction.Create(self.GetId(), TimerClass.OnceWaitTimer, timeNow, time, 0, tcs);
-            self.AddTimer(timer);
+            TimerAction timer = TimerAction.Create(this.GetId(), TimerClass.OnceWaitTimer, timeNow, time, 0, tcs);
+            this.AddTimer(timer);
             long timerId = timer.Id;
 
             void CancelAction()
             {
-                if (self.Remove(timerId))
+                if (this.Remove(timerId))
                 {
                     tcs.SetResult();
                 }
@@ -260,32 +281,32 @@ namespace ET
         // 用这个优点是可以热更,缺点是回调式的写法,逻辑不连贯。WaitTillAsync不能热更,优点是逻辑连贯。
         // wait时间短并且逻辑需要连贯的建议WaitTillAsync
         // wait时间长不需要逻辑连贯的建议用NewOnceTimer
-        public static long NewOnceTimer(this TimerComponent self, long tillTime, int type, object args)
+        public long NewOnceTimer(long tillTime, int type, object args)
         {
-            long timeNow = self.GetNow();
+            long timeNow = GetNow();
             if (tillTime < timeNow)
             {
                 Log.Error($"new once time too small: {tillTime}");
             }
 
-            TimerAction timer = TimerAction.Create(self.GetId(), TimerClass.OnceTimer, timeNow, tillTime - timeNow, type, args);
-            self.AddTimer(timer);
+            TimerAction timer = TimerAction.Create(this.GetId(), TimerClass.OnceTimer, timeNow, tillTime - timeNow, type, args);
+            this.AddTimer(timer);
             return timer.Id;
         }
 
-        public static long NewFrameTimer(this TimerComponent self, int type, object args)
+        public long NewFrameTimer(int type, object args)
         {
 #if DOTNET
-            return self.NewRepeatedTimerInner(100, type, args);
+            return this.NewRepeatedTimerInner(100, type, args);
 #else
-            return self.NewRepeatedTimerInner(0, type, args);
+            return this.NewRepeatedTimerInner(0, type, args);
 #endif
         }
 
         /// <summary>
         /// 创建一个RepeatedTimer
         /// </summary>
-        private static long NewRepeatedTimerInner(this TimerComponent self, long time, int type, object args)
+        private long NewRepeatedTimerInner(long time, int type, object args)
         {
 #if DOTNET
             if (time < 100)
@@ -294,15 +315,15 @@ namespace ET
             }
 #endif
             
-            long timeNow = self.GetNow();
-            TimerAction timer = TimerAction.Create(self.GetId(), TimerClass.RepeatedTimer, timeNow, time, type, args);
+            long timeNow = GetNow();
+            TimerAction timer = TimerAction.Create(this.GetId(), TimerClass.RepeatedTimer, timeNow, time, type, args);
 
             // 每帧执行的不用加到timerId中,防止遍历
-            self.AddTimer(timer);
+            this.AddTimer(timer);
             return timer.Id;
         }
 
-        public static long NewRepeatedTimer(this TimerComponent self, long time, int type, object args)
+        public long NewRepeatedTimer(long time, int type, object args)
         {
             if (time < 100)
             {
@@ -310,27 +331,7 @@ namespace ET
                 return 0;
             }
 
-            return self.NewRepeatedTimerInner(time, type, args);
+            return this.NewRepeatedTimerInner(time, type, args);
         }
     }
-
-    [ComponentOf(typeof(Scene))]
-    public class TimerComponent: Entity, IAwake, IUpdate
-    {
-        /// <summary>
-        /// key: time, value: timer id
-        /// </summary>
-        public readonly MultiMap<long, long> timeId = new(1000);
-
-        public readonly Queue<long> timeOutTime = new();
-
-        public readonly Queue<long> timeOutTimerIds = new();
-
-        public readonly Dictionary<long, TimerAction> timerActions = new();
-
-        public long idGenerator;
-
-        // 记录最小时间,不用每次都去MultiMap取第一个值
-        public long minTime = long.MaxValue;
-    }
 }

+ 1 - 1
Unity/Assets/Scripts/Core/Helper/ETCancelationTokenHelper.cs

@@ -9,7 +9,7 @@ namespace ET
                 return;
             }
 
-            await fiber.Root.GetComponent<TimerComponent>().WaitAsync(afterTimeCancel);
+            await fiber.TimerComponent.WaitAsync(afterTimeCancel);
             
             if (self.IsCancel())
             {

+ 0 - 2
Unity/Assets/Scripts/Core/World/Module/Fiber/FiberManager.cs

@@ -64,8 +64,6 @@ namespace ET
             {
                 Fiber fiber = new(fiberId, Options.Instance.Process, zone, sceneType, name);
 
-                fiber.Root.AddComponent<TimerComponent>();
-                fiber.Root.AddComponent<CoroutineLockComponent>();
                 fiber.Root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
                 fiber.Root.AddComponent<ActorSenderComponent>();
                 fiber.Root.AddComponent<ActorRecverComponent>();

+ 4 - 4
Unity/Assets/Scripts/Hotfix/Client/Demo/AI/AI_Attack.cs

@@ -14,16 +14,16 @@ namespace ET.Client
 
         public override async ETTask Execute(AIComponent aiComponent, AIConfig aiConfig, ETCancellationToken cancellationToken)
         {
-            Scene root = aiComponent.Root();
+            Fiber fiber = aiComponent.Fiber();
 
-            Unit myUnit = UnitHelper.GetMyUnitFromClientScene(root);
+            Unit myUnit = UnitHelper.GetMyUnitFromClientScene(fiber.Root);
             if (myUnit == null)
             {
                 return;
             }
 
             // 停在当前位置
-            root.GetComponent<SessionComponent>().Session.Send(new C2M_Stop());
+            fiber.Root.GetComponent<SessionComponent>().Session.Send(new C2M_Stop());
             
             Log.Debug("开始攻击");
 
@@ -32,7 +32,7 @@ namespace ET.Client
                 Log.Debug($"攻击: {i}次");
 
                 // 因为协程可能被中断,任何协程都要传入cancellationToken,判断如果是中断则要返回
-                await root.GetComponent<TimerComponent>().WaitAsync(1000, cancellationToken);
+                await fiber.TimerComponent.WaitAsync(1000, cancellationToken);
                 if (cancellationToken.IsCancel())
                 {
                     return;

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Client/Demo/Ping/PingComponentSystem.cs

@@ -45,7 +45,7 @@ namespace ET.Client
                     
                     fiber.TimeInfo.ServerMinusClientTime = response.Time + (time2 - time1) / 2 - time2;
                     
-                    await fiber.Root.GetComponent<TimerComponent>().WaitAsync(2000);
+                    await fiber.TimerComponent.WaitAsync(2000);
                 }
                 catch (RpcException e)
                 {

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Client/Demo/Router/RouterAddressComponentSystem.cs

@@ -40,7 +40,7 @@ namespace ET.Client
         // 等10分钟再获取一次
         public static async ETTask WaitTenMinGetAllRouter(this RouterAddressComponent self)
         {
-            await self.Root().GetComponent<TimerComponent>().WaitAsync(5 * 60 * 1000);
+            await self.Fiber().TimerComponent.WaitAsync(5 * 60 * 1000);
             if (self.IsDisposed)
             {
                 return;

+ 3 - 2
Unity/Assets/Scripts/Hotfix/Client/Demo/Router/RouterCheckComponentSystem.cs

@@ -15,7 +15,8 @@ namespace ET.Client
         {
             Session session = self.GetParent<Session>();
             long instanceId = self.InstanceId;
-            Scene root = self.Root();
+            Fiber fiber = self.Fiber();
+            Scene root = fiber.Root;
             while (true)
             {
                 if (self.InstanceId != instanceId)
@@ -23,7 +24,7 @@ namespace ET.Client
                     return;
                 }
 
-                await root.GetComponent<TimerComponent>().WaitAsync(1000);
+                await fiber.TimerComponent.WaitAsync(1000);
                 
                 if (self.InstanceId != instanceId)
                 {

+ 3 - 2
Unity/Assets/Scripts/Hotfix/Client/Demo/Router/RouterHelper.cs

@@ -63,9 +63,10 @@ namespace ET.Client
 
             long lastSendTimer = 0;
 
+            Fiber fiber = root.Fiber;
             while (true)
             {
-                long timeNow = root.Fiber().TimeInfo.ClientFrameTime();
+                long timeNow = fiber.TimeInfo.ClientFrameTime();
                 if (timeNow - lastSendTimer > 300)
                 {
                     if (--count < 0)
@@ -78,7 +79,7 @@ namespace ET.Client
                     socket.SendTo(sendCache, 0, addressBytes.Length + 13, SocketFlags.None, routerAddress);
                 }
                     
-                await root.GetComponent<TimerComponent>().WaitFrameAsync();
+                await fiber.TimerComponent.WaitFrameAsync();
                     
                 // 接收
                 if (socket.Available > 0)

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/Gate/GateSessionKeyComponentSystem.cs

@@ -23,7 +23,7 @@
 
         private static async ETTask TimeoutRemoveKey(this GateSessionKeyComponent self, long key)
         {
-            await self.Root().GetComponent<TimerComponent>().WaitAsync(20000);
+            await self.Fiber().TimerComponent.WaitAsync(20000);
             self.sessionKey.Remove(key);
         }
     }

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/LockStep/Map/C2Room_ChangeSceneFinishHandler.cs

@@ -19,7 +19,7 @@ namespace ET.Server
                 return;
             }
             
-            await room.Root().GetComponent<TimerComponent>().WaitAsync(1000);
+            await room.Fiber.TimerComponent.WaitAsync(1000);
 
             Room2C_Start room2CStart = new() { StartTime = room.Fiber().TimeInfo.ServerFrameTime() };
             foreach (RoomPlayer rp in roomServerComponent.Children.Values)

+ 6 - 11
Unity/Assets/Scripts/Hotfix/Server/Module/ActorLocation/ActorLocationSenderComponentSystem.cs

@@ -30,18 +30,13 @@ namespace ET.Server
             self.LocationType = locationType;
             // 每10s扫描一次过期的actorproxy进行回收,过期时间是2分钟
             // 可能由于bug或者进程挂掉,导致ActorLocationSender发送的消息没有确认,结果无法自动删除,每一分钟清理一次这种ActorLocationSender
-            self.CheckTimer = self.Root().GetComponent<TimerComponent>().NewRepeatedTimer(10 * 1000, TimerInvokeType.ActorLocationSenderChecker, self);
+            self.CheckTimer = self.Fiber().TimerComponent.NewRepeatedTimer(10 * 1000, TimerInvokeType.ActorLocationSenderChecker, self);
         }
         
         [EntitySystem]
         private static void Destroy(this ActorLocationSenderOneType self)
         {
-            Scene root = self.Root();
-            if (root.InstanceId == 0)
-            {
-                return;
-            }
-            root.GetComponent<TimerComponent>().Remove(ref self.CheckTimer);
+            self.Fiber().TimerComponent.Remove(ref self.CheckTimer);
         }
 
         private static void Check(this ActorLocationSenderOneType self)
@@ -117,7 +112,7 @@ namespace ET.Server
             long instanceId = actorLocationSender.InstanceId;
             
             int coroutineLockType = (self.LocationType << 16) | CoroutineLockType.ActorLocationSender;
-            using (await root.GetComponent<CoroutineLockComponent>().Wait(coroutineLockType, entityId))
+            using (await root.Fiber.CoroutineLockComponent.Wait(coroutineLockType, entityId))
             {
                 if (actorLocationSender.InstanceId != instanceId)
                 {
@@ -155,7 +150,7 @@ namespace ET.Server
             long instanceId = actorLocationSender.InstanceId;
             
             int coroutineLockType = (self.LocationType << 16) | CoroutineLockType.ActorLocationSender;
-            using (await root.GetComponent<CoroutineLockComponent>().Wait(coroutineLockType, entityId))
+            using (await root.Fiber.CoroutineLockComponent.Wait(coroutineLockType, entityId))
             {
                 if (actorLocationSender.InstanceId != instanceId)
                 {
@@ -193,7 +188,7 @@ namespace ET.Server
             
             long actorLocationSenderInstanceId = actorLocationSender.InstanceId;
             int coroutineLockType = (self.LocationType << 16) | CoroutineLockType.ActorLocationSender;
-            using (await root.GetComponent<CoroutineLockComponent>().Wait(coroutineLockType, entityId))
+            using (await root.Fiber.CoroutineLockComponent.Wait(coroutineLockType, entityId))
             {
                 if (actorLocationSender.InstanceId != actorLocationSenderInstanceId)
                 {
@@ -268,7 +263,7 @@ namespace ET.Server
                         }
 
                         // 等待0.5s再发送
-                        await root.GetComponent<TimerComponent>().WaitAsync(500);
+                        await root.Fiber.TimerComponent.WaitAsync(500);
                         if (actorLocationSender.InstanceId != instanceId)
                         {
                             throw new RpcException(ErrorCore.ERR_ActorLocationSenderTimeout4, $"{iActorRequest}");

+ 5 - 5
Unity/Assets/Scripts/Hotfix/Server/Module/ActorLocation/LocationOneTypeSystem.cs

@@ -35,7 +35,7 @@ namespace ET.Server
         public static async ETTask Add(this LocationOneType self, long key, ActorId instanceId)
         {
             int coroutineLockType = (self.LocationType << 16) | CoroutineLockType.Location;
-            using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(coroutineLockType, key))
+            using (await self.Fiber().CoroutineLockComponent.Wait(coroutineLockType, key))
             {
                 self.locations[key] = instanceId;
                 Log.Info($"location add key: {key} instanceId: {instanceId}");
@@ -45,7 +45,7 @@ namespace ET.Server
         public static async ETTask Remove(this LocationOneType self, long key)
         {
             int coroutineLockType = (self.LocationType << 16) | CoroutineLockType.Location;
-            using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(coroutineLockType, key))
+            using (await self.Fiber().CoroutineLockComponent.Wait(coroutineLockType, key))
             {
                 self.locations.Remove(key);
                 Log.Info($"location remove key: {key}");
@@ -55,7 +55,7 @@ namespace ET.Server
         public static async ETTask Lock(this LocationOneType self, long key, ActorId actorId, int time = 0)
         {
             int coroutineLockType = (self.LocationType << 16) | CoroutineLockType.Location;
-            CoroutineLock coroutineLock = await self.Root().GetComponent<CoroutineLockComponent>().Wait(coroutineLockType, key);
+            CoroutineLock coroutineLock = await self.Fiber().CoroutineLockComponent.Wait(coroutineLockType, key);
 
             LockInfo lockInfo = self.AddChild<LockInfo, ActorId, CoroutineLock>(actorId, coroutineLock);
             self.lockInfos.Add(key, lockInfo);
@@ -67,7 +67,7 @@ namespace ET.Server
                 async ETTask TimeWaitAsync()
                 {
                     long lockInfoInstanceId = lockInfo.InstanceId;
-                    await self.Root().GetComponent<TimerComponent>().WaitAsync(time);
+                    await self.Fiber().TimerComponent.WaitAsync(time);
                     if (lockInfo.InstanceId != lockInfoInstanceId)
                     {
                         return;
@@ -106,7 +106,7 @@ namespace ET.Server
         public static async ETTask<ActorId> Get(this LocationOneType self, long key)
         {
             int coroutineLockType = (self.LocationType << 16) | CoroutineLockType.Location;
-            using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(coroutineLockType, key))
+            using (await self.Fiber().CoroutineLockComponent.Wait(coroutineLockType, key))
             {
                 self.locations.TryGetValue(key, out ActorId actorId);
                 Log.Info($"location get key: {key} actorId: {actorId}");

+ 14 - 14
Unity/Assets/Scripts/Hotfix/Server/Module/DB/DBComponentSystem.cs

@@ -29,7 +29,7 @@ namespace ET.Server
 
 	    public static async ETTask<T> Query<T>(this DBComponent self, long id, string collection = null) where T : Entity
 	    {
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
 		    {
 			    IAsyncCursor<T> cursor = await self.GetCollection<T>(collection).FindAsync(d => d.Id == id);
 
@@ -40,7 +40,7 @@ namespace ET.Server
 	    public static async ETTask<List<T>> Query<T>(this DBComponent self, Expression<Func<T, bool>> filter, string collection = null)
 			    where T : Entity
 	    {
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, RandomGenerator.RandInt64() % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, RandomGenerator.RandInt64() % DBComponent.TaskCount))
 		    {
 			    IAsyncCursor<T> cursor = await self.GetCollection<T>(collection).FindAsync(filter);
 
@@ -51,7 +51,7 @@ namespace ET.Server
 	    public static async ETTask<List<T>> Query<T>(this DBComponent self, long taskId, Expression<Func<T, bool>> filter, string collection = null)
 			    where T : Entity
 	    {
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
 		    {
 			    IAsyncCursor<T> cursor = await self.GetCollection<T>(collection).FindAsync(filter);
 
@@ -66,7 +66,7 @@ namespace ET.Server
 			    return;
 		    }
 
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
 		    {
 			    foreach (string collectionName in collectionNames)
 			    {
@@ -86,7 +86,7 @@ namespace ET.Server
 
 	    public static async ETTask<List<T>> QueryJson<T>(this DBComponent self, string json, string collection = null) where T : Entity
 	    {
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, RandomGenerator.RandInt64() % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, RandomGenerator.RandInt64() % DBComponent.TaskCount))
 		    {
 			    FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
 			    IAsyncCursor<T> cursor = await self.GetCollection<T>(collection).FindAsync(filterDefinition);
@@ -96,7 +96,7 @@ namespace ET.Server
 
 	    public static async ETTask<List<T>> QueryJson<T>(this DBComponent self, long taskId, string json, string collection = null) where T : Entity
 	    {
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, RandomGenerator.RandInt64() % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, RandomGenerator.RandInt64() % DBComponent.TaskCount))
 		    {
 			    FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
 			    IAsyncCursor<T> cursor = await self.GetCollection<T>(collection).FindAsync(filterDefinition);
@@ -115,7 +115,7 @@ namespace ET.Server
 			    collection = typeof (T).Name;
 		    }
 		    
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, RandomGenerator.RandInt64() % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, RandomGenerator.RandInt64() % DBComponent.TaskCount))
 		    {
 			    await self.GetCollection(collection).InsertManyAsync(list);
 		    }
@@ -139,7 +139,7 @@ namespace ET.Server
 			    collection = entity.GetType().FullName;
 		    }
 
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, entity.Id % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, entity.Id % DBComponent.TaskCount))
 		    {
 			    await self.GetCollection(collection).ReplaceOneAsync(d => d.Id == entity.Id, entity, new ReplaceOptions { IsUpsert = true });
 		    }
@@ -159,7 +159,7 @@ namespace ET.Server
 			    collection = entity.GetType().FullName;
 		    }
 
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
 		    {
 			    await self.GetCollection(collection).ReplaceOneAsync(d => d.Id == entity.Id, entity, new ReplaceOptions { IsUpsert = true });
 		    }
@@ -173,7 +173,7 @@ namespace ET.Server
 			    return;
 		    }
 
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
 		    {
 			    foreach (Entity entity in entities)
 			    {
@@ -206,7 +206,7 @@ namespace ET.Server
 	    
 	    public static async ETTask<long> Remove<T>(this DBComponent self, long id, string collection = null) where T : Entity
 	    {
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
 		    {
 			    DeleteResult result = await self.GetCollection<T>(collection).DeleteOneAsync(d => d.Id == id);
 
@@ -216,7 +216,7 @@ namespace ET.Server
 
 	    public static async ETTask<long> Remove<T>(this DBComponent self, long taskId, long id, string collection = null) where T : Entity
 	    {
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
 		    {
 			    DeleteResult result = await self.GetCollection<T>(collection).DeleteOneAsync(d => d.Id == id);
 
@@ -226,7 +226,7 @@ namespace ET.Server
 
 	    public static async ETTask<long> Remove<T>(this DBComponent self, Expression<Func<T, bool>> filter, string collection = null) where T : Entity
 	    {
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, RandomGenerator.RandInt64() % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, RandomGenerator.RandInt64() % DBComponent.TaskCount))
 		    {
 			    DeleteResult result = await self.GetCollection<T>(collection).DeleteManyAsync(filter);
 
@@ -237,7 +237,7 @@ namespace ET.Server
 	    public static async ETTask<long> Remove<T>(this DBComponent self, long taskId, Expression<Func<T, bool>> filter, string collection = null)
 			    where T : Entity
 	    {
-		    using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
+		    using (await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
 		    {
 			    DeleteResult result = await self.GetCollection<T>(collection).DeleteManyAsync(filter);
 

+ 3 - 8
Unity/Assets/Scripts/Hotfix/Share/Module/AI/AIComponentSystem.cs

@@ -26,18 +26,13 @@ namespace ET
         private static void Awake(this AIComponent self, int aiConfigId)
         {
             self.AIConfigId = aiConfigId;
-            self.Timer = self.Root().GetComponent<TimerComponent>().NewRepeatedTimer(1000, TimerInvokeType.AITimer, self);
+            self.Timer = self.Fiber().TimerComponent.NewRepeatedTimer(1000, TimerInvokeType.AITimer, self);
         }
 
         [EntitySystem]
         private static void Destroy(this AIComponent self)
         {
-            Scene root = self.Root();
-            if (root.InstanceId == 0)
-            {
-                return;
-            }
-            root.GetComponent<TimerComponent>().Remove(ref self.Timer);
+            self.Fiber().TimerComponent.Remove(ref self.Timer);
             self.CancellationToken?.Cancel();
             self.CancellationToken = null;
             self.Current = 0;
@@ -47,7 +42,7 @@ namespace ET
         {
             if (self.Parent == null)
             {
-                self.Root().GetComponent<TimerComponent>().Remove(ref self.Timer);
+                self.Fiber().TimerComponent.Remove(ref self.Timer);
                 return;
             }
 

+ 2 - 7
Unity/Assets/Scripts/Hotfix/Share/Module/Actor/ActorSenderComponentSystem.cs

@@ -26,18 +26,13 @@ namespace ET
         [EntitySystem]
         private static void Awake(this ActorSenderComponent self)
         {
-            self.TimeoutCheckTimer = self.Root().GetComponent<TimerComponent>().NewRepeatedTimer(1000, TimerInvokeType.ActorMessageSenderChecker, self);
+            self.TimeoutCheckTimer = self.Fiber().TimerComponent.NewRepeatedTimer(1000, TimerInvokeType.ActorMessageSenderChecker, self);
         }
         
         [EntitySystem]
         private static void Destroy(this ActorSenderComponent self)
         {
-            Scene root = self.Root();
-            if (root.InstanceId == 0)
-            {
-                return;
-            }
-            root.GetComponent<TimerComponent>().Remove(ref self.TimeoutCheckTimer);
+            self.Fiber().TimerComponent.Remove(ref self.TimeoutCheckTimer);
             self.TimeoutCheckTimer = 0;
             self.TimeoutActorMessageSenders.Clear();
         }

+ 2 - 7
Unity/Assets/Scripts/Hotfix/Share/Module/Message/SessionAcceptTimeoutComponentSystem.cs

@@ -24,18 +24,13 @@ namespace ET
         [EntitySystem]
         private static void Awake(this SessionAcceptTimeoutComponent self)
         {
-            self.Timer = self.Root().GetComponent<TimerComponent>().NewOnceTimer(self.Fiber().TimeInfo.ServerNow() + 5000, TimerInvokeType.SessionAcceptTimeout, self);
+            self.Timer = self.Fiber().TimerComponent.NewOnceTimer(self.Fiber().TimeInfo.ServerNow() + 5000, TimerInvokeType.SessionAcceptTimeout, self);
         }
         
         [EntitySystem]
         private static void Destroy(this SessionAcceptTimeoutComponent self)
         {
-            Scene root = self.Root();
-            if (root.IsDisposed)
-            {
-                return;
-            }
-            root.GetComponent<TimerComponent>().Remove(ref self.Timer);
+            self.Fiber().TimerComponent.Remove(ref self.Timer);
         }
         
     }

+ 2 - 7
Unity/Assets/Scripts/Hotfix/Share/Module/Message/SessionIdleCheckerComponentSystem.cs

@@ -24,18 +24,13 @@ namespace ET
         [EntitySystem]
         private static void Awake(this SessionIdleCheckerComponent self)
         {
-            self.RepeatedTimer = self.Root().GetComponent<TimerComponent>().NewRepeatedTimer(SessionIdleCheckerComponentSystem.CheckInteral, TimerInvokeType.SessionIdleChecker, self);
+            self.RepeatedTimer = self.Fiber().TimerComponent.NewRepeatedTimer(CheckInteral, TimerInvokeType.SessionIdleChecker, self);
         }
         
         [EntitySystem]
         private static void Destroy(this SessionIdleCheckerComponent self)
         {
-            Scene root = self.Root();
-            if (root.IsDisposed)
-            {
-                return;
-            }
-            root.GetComponent<TimerComponent>().Remove(ref self.RepeatedTimer);
+            self.Fiber().TimerComponent.Remove(ref self.RepeatedTimer);
         }
         
         public const int CheckInteral = 2000;

+ 2 - 7
Unity/Assets/Scripts/Hotfix/Share/Module/Move/MoveComponentSystem.cs

@@ -181,7 +181,7 @@ namespace ET
             self.StartTime = self.BeginTime;
             self.SetNextTarget();
 
-            self.MoveTimer = self.Root().GetComponent<TimerComponent>().NewFrameTimer(TimerInvokeType.MoveTimer, self);
+            self.MoveTimer = self.Fiber().TimerComponent.NewFrameTimer(TimerInvokeType.MoveTimer, self);
         }
 
         private static void SetNextTarget(this MoveComponent self)
@@ -266,11 +266,6 @@ namespace ET
 
         private static void MoveFinish(this MoveComponent self, bool ret)
         {
-            Scene root = self.Root();
-            if (root.IsDisposed)
-            {
-                return;
-            }
             if (self.StartTime == 0)
             {
                 return;
@@ -280,12 +275,12 @@ namespace ET
             self.StartPos = float3.zero;
             self.BeginTime = 0;
             self.NeedTime = 0;
-            root.GetComponent<TimerComponent>().Remove(ref self.MoveTimer);
             self.Targets.Clear();
             self.Speed = 0;
             self.N = 0;
             self.TurnTime = 0;
             self.IsTurnHorizontal = false;
+            self.Fiber().TimerComponent.Remove(ref self.MoveTimer);
 
             if (self.tcs != null)
             {

+ 2 - 10
Unity/Assets/Scripts/Model/Server/Module/ActorLocation/LocationComponent.cs

@@ -18,18 +18,10 @@ namespace ET.Server
     {
         public ActorId LockActorId;
 
-        private EntityRef<CoroutineLock> coroutineLock;
-
         public CoroutineLock CoroutineLock
         {
-            get
-            {
-                return this.coroutineLock;
-            }
-            set
-            {
-                this.coroutineLock = value;
-            }
+            get;
+            set;
         }
     }
 

+ 1 - 1
Unity/Assets/Scripts/Model/Share/Module/ObjectWait/ObjectWait.cs

@@ -107,7 +107,7 @@ namespace ET
             ResultCallback<T> tcs = new ResultCallback<T>();
             async ETTask WaitTimeout()
             {
-                await self.Root().GetComponent<TimerComponent>().WaitAsync(timeout, cancellationToken);
+                await self.Fiber().TimerComponent.WaitAsync(timeout, cancellationToken);
                 if (cancellationToken.IsCancel())
                 {
                     return;

+ 7 - 7
Unity/Assets/Scripts/ModelView/Client/Module/Resource/ResourcesComponent.cs

@@ -229,14 +229,13 @@ namespace ET.Client
             string[] dependencies = self.GetSortedDependencies(assetBundleName);
 
             //Log.Debug($"-----------dep unload start {assetBundleName} dep: {dependencies.ToList().ListToString()}");
-            Scene root = self.Root();
-            TimerComponent timerComponent = root.GetComponent<TimerComponent>();
+            Fiber fiber = self.Fiber();
             foreach (string dependency in dependencies)
             {
-                using (await root.GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.Resources, assetBundleName.GetHashCode()))
+                using (await fiber.CoroutineLockComponent.Wait(CoroutineLockType.Resources, assetBundleName.GetHashCode()))
                 {
                     self.UnloadOneBundle(dependency, unload);
-                    await timerComponent.WaitFrameAsync();
+                    await fiber.TimerComponent.WaitFrameAsync();
                 }
             }
             //Log.Debug($"-----------dep unload finish {assetBundleName} dep: {dependencies.ToList().ListToString()}");
@@ -404,12 +403,13 @@ namespace ET.Client
             string[] dependencies = self.GetSortedDependencies(assetBundleName);
             //Log.Debug($"-----------dep load async start {assetBundleName} dep: {dependencies.ToList().ListToString()}");
 
+            Fiber fiber = self.Fiber();
             using (ListComponent<ABInfo> abInfos = ListComponent<ABInfo>.Create())
             {
                 async ETTask LoadDependency(string dependency, List<ABInfo> abInfosList)
                 {
                     using CoroutineLock coroutineLock =
-                            await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.Resources, dependency.GetHashCode());
+                            await fiber.CoroutineLockComponent.Wait(CoroutineLockType.Resources, dependency.GetHashCode());
 
                     ABInfo abInfo = await self.LoadOneBundleAsync(dependency);
                     if (abInfo == null || abInfo.RefCount > 1)
@@ -480,7 +480,7 @@ namespace ET.Client
                     }
 
                     // 编辑器模式也不能同步加载
-                    await self.Root().GetComponent<TimerComponent>().WaitAsync(100);
+                    await self.Fiber().TimerComponent.WaitAsync(100);
 
                     return abInfo;
                 }
@@ -518,7 +518,7 @@ namespace ET.Client
         // 加载ab包中的all assets
         private static async ETTask LoadOneBundleAllAssets(this ResourcesComponent self, ABInfo abInfo)
         {
-            using CoroutineLock coroutineLock = await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.Resources, abInfo.Name.GetHashCode());
+            using CoroutineLock coroutineLock = await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.Resources, abInfo.Name.GetHashCode());
 
             if (abInfo.IsDisposed || abInfo.AlreadyLoadAssets)
             {

+ 6 - 6
Unity/Assets/Scripts/ModelView/Client/Module/Resource/ResourcesLoaderComponent.cs

@@ -10,10 +10,10 @@ namespace ET.Client
         {
             async ETTask UnLoadAsync()
             {
-                Scene root = self.Root();
+                Fiber fiber = self.Fiber();
                 using ListComponent<string> list = ListComponent<string>.Create();
                 
-                TimerComponent timerComponent = root.GetComponent<TimerComponent>();
+                TimerComponent timerComponent = fiber.TimerComponent;
                 list.AddRange(self.LoadedResource);
                 self.LoadedResource = null;
 
@@ -25,18 +25,18 @@ namespace ET.Client
                 // 延迟5秒卸载包,因为包卸载是引用计数,5秒之内假如重新有逻辑加载了这个包,那么可以避免一次卸载跟加载
                 await timerComponent.WaitAsync(5000);
 
-                CoroutineLockComponent coroutineLockComponent = root.GetComponent<CoroutineLockComponent>();
+                CoroutineLockComponent coroutineLockComponent = fiber.CoroutineLockComponent;
                 foreach (string abName in list)
                 {
                     using CoroutineLock coroutineLock =
                             await coroutineLockComponent.Wait(CoroutineLockType.ResourcesLoader, abName.GetHashCode(), 0);
                     {
-                        if (root.IsDisposed)
+                        if (fiber.IsDisposed)
                         {
                             return;
                         }
 
-                        await root.GetComponent<ResourcesComponent>().UnloadBundleAsync(abName);
+                        await fiber.Root.GetComponent<ResourcesComponent>().UnloadBundleAsync(abName);
                     }
                 }
             }
@@ -46,7 +46,7 @@ namespace ET.Client
 
         public static async ETTask LoadAsync(this ResourcesLoaderComponent self, string ab)
         {
-            using CoroutineLock coroutineLock = await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.ResourcesLoader, ab.GetHashCode(), 0);
+            using CoroutineLock coroutineLock = await self.Fiber().CoroutineLockComponent.Wait(CoroutineLockType.ResourcesLoader, ab.GetHashCode(), 0);
 
             if (self.IsDisposed)
             {