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

IdGenerater也放到Singleton中

tanghai 2 жил өмнө
parent
commit
a2a94d7c70

+ 6 - 16
Unity/Assets/Scripts/Core/Entity/Entity.cs

@@ -90,19 +90,9 @@ namespace ET
             }
         }
 
-        private EntitySystem GetEntitySystem()
-        {
-            return this.iScene.Fiber.EntitySystem;
-        }
-        
-        private IdGenerater GetIdGenerater()
-        {
-            return this.iScene.Fiber.IdGenerater;
-        }
-
         protected virtual void RegisterSystem()
         {
-            this.GetEntitySystem().RegisterSystem(this);
+            this.iScene.Fiber.EntitySystem.RegisterSystem(this);
         }
 
         protected virtual string ViewName
@@ -325,7 +315,7 @@ namespace ET
                 {
                     if (this.InstanceId == 0)
                     {
-                        this.InstanceId = this.GetIdGenerater().GenerateInstanceId();
+                        this.InstanceId = IdGenerater.Instance.GenerateInstanceId();
                     }
 
                     this.IsRegister = true;
@@ -874,7 +864,7 @@ namespace ET
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
-            component.Id = this.GetIdGenerater().GenerateId();
+            component.Id = IdGenerater.Instance.GenerateId();
             component.Parent = this;
 
             EntitySystemSingleton.Instance.Awake(component);
@@ -885,7 +875,7 @@ namespace ET
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
-            component.Id = this.GetIdGenerater().GenerateId();
+            component.Id = IdGenerater.Instance.GenerateId();
             component.Parent = this;
 
             EntitySystemSingleton.Instance.Awake(component, a);
@@ -896,7 +886,7 @@ namespace ET
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
-            component.Id = this.GetIdGenerater().GenerateId();
+            component.Id = IdGenerater.Instance.GenerateId();
             component.Parent = this;
 
             EntitySystemSingleton.Instance.Awake(component, a, b);
@@ -907,7 +897,7 @@ namespace ET
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
-            component.Id = this.GetIdGenerater().GenerateId();
+            component.Id = IdGenerater.Instance.GenerateId();
             component.Parent = this;
 
             EntitySystemSingleton.Instance.Awake(component, a, b, c);

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

@@ -30,11 +30,16 @@ namespace ET
                 return new Address(this.Process, this.Id);
             }
         }
-        
-        public int Process { get; }
-        
+
+        public int Process
+        {
+            get
+            {
+                return Options.Instance.Process;
+            }
+        }
+
         public EntitySystem EntitySystem { get; }
-        public IdGenerater IdGenerater { get; private set; }
         public Mailboxes Mailboxes { get; private set; }
         public ThreadSynchronizationContext ThreadSynchronizationContext { get; }
         
@@ -79,13 +84,11 @@ namespace ET
 
         private readonly Queue<ETTask> frameFinishTasks = new();
         
-        internal Fiber(int id, int process, int zone, SceneType sceneType, string name)
+        internal Fiber(int id, int zone, SceneType sceneType, string name)
         {
             this.Id = id;
-            this.Process = process;
             this.Zone = zone;
             this.EntitySystem = new EntitySystem();
-            this.IdGenerater = new IdGenerater(process);
             this.Mailboxes = new Mailboxes();
             this.ThreadSynchronizationContext = new ThreadSynchronizationContext();
             this.Root = new Scene(this, id, 1, sceneType, name);

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

@@ -65,7 +65,7 @@ namespace ET
         {
             try
             {
-                Fiber fiber = new(fiberId, Options.Instance.Process, zone, sceneType, name);
+                Fiber fiber = new(fiberId, zone, sceneType, name);
                 
                 this.fibers.TryAdd(fiberId, fiber);
                 this.schedulers[(int) schedulerType].Add(fiberId);

+ 17 - 37
Unity/Assets/Scripts/Core/Fiber/IdGenerater.cs → Unity/Assets/Scripts/Core/World/Module/IdGenerater/IdGenerater.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Runtime.InteropServices;
+using System.Threading;
 
 namespace ET
 {
@@ -79,7 +80,7 @@ namespace ET
         }
     }
 
-    public class IdGenerater
+    public class IdGenerater: Singleton<IdGenerater>, ISingletonAwake
     {
         public const int MaxZone = 1024;
         
@@ -87,26 +88,15 @@ namespace ET
         public const int Mask30bit = 0x3fffffff;
         public const int Mask20bit = 0xfffff;
         
-        private readonly long epoch2022;
-        private uint value;
-        private uint lastIdTime;
-
-        private readonly int process;
+        private long epoch2022;
         
-        private uint instanceIdValue;
+        private int value;
+        private int instanceIdValue;
         
-        public IdGenerater(int process)
+        public void Awake()
         {
-            this.process = process;
-            
             long epoch1970tick = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks / 10000;
             this.epoch2022 = new DateTime(2022, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks / 10000 - epoch1970tick;
-            this.lastIdTime = TimeSince2022();
-            if (this.lastIdTime <= 0)
-            {
-                Log.Warning($"lastIdTime less than 0: {this.lastIdTime}");
-                this.lastIdTime = 1;
-            }
         }
 
         private uint TimeSince2022()
@@ -118,35 +108,25 @@ namespace ET
         public long GenerateId()
         {
             uint time = TimeSince2022();
-
-            // 时间不会倒退
-            if (time > this.lastIdTime)
+            int v = 0;
+            // 这里必须加锁
+            lock (this)
             {
-                this.lastIdTime = time;
+                if (++this.value > Mask20bit - 1)
+                {
+                    this.value = 0;
+                }
+                v = this.value;
             }
-            this.value = IdValueGenerater.Instance.Value;
-
-            IdStruct idStruct = new(this.lastIdTime, (short)this.process, value);
+            IdStruct idStruct = new(time, (short)Options.Instance.Process, (uint)v);
             return idStruct.ToLong();
         }
         
         public long GenerateInstanceId()
         {
             uint time = this.TimeSince2022();
-
-            // 时间不会倒退
-            if (time > this.lastIdTime)
-            {
-                this.lastIdTime = time;
-            }
-            ++this.instanceIdValue;
-                
-            if (this.instanceIdValue >= int.MaxValue)
-            {
-                this.instanceIdValue = 0;
-            }
-
-            InstanceIdStruct instanceIdStruct = new(this.lastIdTime, this.instanceIdValue);
+            uint v = (uint)Interlocked.Add(ref this.instanceIdValue, 1);
+            InstanceIdStruct instanceIdStruct = new(time, v);
             return instanceIdStruct.ToLong();
         }
     }

+ 1 - 1
Unity/Assets/Scripts/Core/Fiber/IdGenerater.cs.meta → Unity/Assets/Scripts/Core/World/Module/IdGenerater/IdGenerater.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 01dd1407fbf957f41a818bcfb700c7ed
+guid: 27cb0cdb7f2228547a3afe6fe024dc9b
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 0 - 27
Unity/Assets/Scripts/Core/World/Module/IdGenerater/IdValueGenerater.cs

@@ -1,27 +0,0 @@
-namespace ET
-{
-    public class IdValueGenerater: Singleton<IdValueGenerater>, ISingletonAwake
-    {
-        private uint value;
-
-        public uint Value
-        {
-            get
-            {
-                lock (this)
-                {
-                    if (++this.value > IdGenerater.Mask20bit - 1)
-                    {
-                        this.value = 0;
-                    }
-                    return this.value;
-                }
-            }
-        }
-
-        public void Awake()
-        {
-            
-        }
-    }
-}

+ 0 - 3
Unity/Assets/Scripts/Core/World/Module/IdGenerater/IdValueGenerater.cs.meta

@@ -1,3 +0,0 @@
-fileFormatVersion: 2
-guid: d4510ce4f5e843f9a9efc99d1714845a
-timeCreated: 1687098066

+ 2 - 1
Unity/Assets/Scripts/Core/World/Module/TimeInfo/TimeInfo.cs

@@ -22,9 +22,10 @@ namespace ET
         private DateTime dt1970;
         private DateTime dt;
         
+        // ping消息会设置该值,原子操作
         public long ServerMinusClientTime { private get; set; }
 
-        public long FrameTime;
+        public long FrameTime { get; private set; }
         
         public void Awake()
         {

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Client/Demo/Main/Scene/CurrentSceneFactory.cs

@@ -4,7 +4,7 @@ namespace ET.Client
     {
         public static Scene Create(long id, string name, CurrentScenesComponent currentScenesComponent)
         {
-            Scene currentScene = EntitySceneFactory.CreateScene(currentScenesComponent, id, currentScenesComponent.Fiber().IdGenerater.GenerateInstanceId(), SceneType.Current, name);
+            Scene currentScene = EntitySceneFactory.CreateScene(currentScenesComponent, id, IdGenerater.Instance.GenerateInstanceId(), SceneType.Current, name);
             currentScenesComponent.Scene = currentScene;
             
             EventSystem.Instance.Publish(currentScene, new EventType.AfterCreateCurrentScene());

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

@@ -9,7 +9,7 @@
 
 			// 在Gate上动态创建一个Map Scene,把Unit从DB中加载放进来,然后传送到真正的Map中,这样登陆跟传送的逻辑就完全一样了
 			GateMapComponent gateMapComponent = player.AddComponent<GateMapComponent>();
-			gateMapComponent.Scene = await GateMapFactory.Create(gateMapComponent, player.Id, session.Fiber().IdGenerater.GenerateInstanceId(), "GateMap");
+			gateMapComponent.Scene = await GateMapFactory.Create(gateMapComponent, player.Id, IdGenerater.Instance.GenerateInstanceId(), "GateMap");
 
 			Scene scene = gateMapComponent.Scene;
 			

+ 0 - 9
Unity/Assets/Scripts/Model/Generate/Server/ConfigPartial/StartProcessConfig.cs

@@ -4,19 +4,10 @@ namespace ET
 {
     public partial class StartProcessConfig
     {
-        public long SceneId;
-
         public string InnerIP => this.StartMachineConfig.InnerIP;
 
         public string OuterIP => this.StartMachineConfig.OuterIP;
 
         public StartMachineConfig StartMachineConfig => StartMachineConfigCategory.Instance.Get(this.MachineId);
-
-        public override void EndInit()
-        {
-            InstanceIdStruct instanceIdStruct = new(0, (uint)this.Id);
-            this.SceneId = instanceIdStruct.ToLong();
-            Log.Info($"StartProcess info: {this.MachineId} {this.Id} {this.SceneId}");
-        }
     }
 }

+ 2 - 5
Unity/Assets/Scripts/Model/Share/Entry.cs

@@ -1,7 +1,4 @@
-using System.IO;
-using MemoryPack;
-
-namespace ET
+namespace ET
 {
     namespace EventType
     {
@@ -38,8 +35,8 @@ namespace ET
             MongoHelper.Register();
 
             World.Instance.AddSingleton<TimeInfo>();
+            World.Instance.AddSingleton<IdGenerater>();
             World.Instance.AddSingleton<OpcodeType>();
-            World.Instance.AddSingleton<IdValueGenerater>();
             World.Instance.AddSingleton<ObjectPool>();
             World.Instance.AddSingleton<ActorMessageQueue>();
             World.Instance.AddSingleton<NetServices>();