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

1.修复帧同步demo点击匹配报错的bug
2.只有LSEntity需要保存LongHashCode,记录到LSEntitySystemSingleton中

tanghai 2 жил өмнө
parent
commit
98355e4f3c

+ 1 - 1
Unity/Assets/Scripts/Core/Entity/Entity.cs

@@ -285,7 +285,7 @@ namespace ET
         [BsonDefaultValue(0L)]
         [BsonElement]
         [BsonId]
-        public long Id { get; protected set; }
+        public long Id { get; set; }
 
         [BsonIgnore]
         protected IScene iScene;

+ 1 - 17
Unity/Assets/Scripts/Core/World/Module/Code/CodeTypes.cs

@@ -8,8 +8,7 @@ namespace ET
     {
         private readonly Dictionary<string, Type> allTypes = new();
         private readonly UnOrderMultiMapSet<Type, Type> types = new();
-        private readonly DoubleMap<Type, long> entityTypeHash = new();
-
+        
         public void Awake(Assembly[] assemblies)
         {
             Dictionary<string, Type> addTypes = AssemblyHelper.GetAssemblyTypes(assemblies);
@@ -29,11 +28,6 @@ namespace ET
                 {
                     this.types.Add(o.GetType(), type);
                 }
-
-                if (typeof(Entity).IsAssignableFrom(type))
-                {
-                    this.entityTypeHash.Add(type, type.FullName.GetLongHashCode());
-                }
             }
         }
 
@@ -57,16 +51,6 @@ namespace ET
             return this.allTypes[typeName];
         }
         
-        public Type GetTypeByHash(long hash)
-        {
-            return this.entityTypeHash.GetKeyByValue(hash);
-        }
-        
-        public long GetHashByType(Type type)
-        {
-            return this.entityTypeHash.GetValueByKey(type);
-        }
-        
         public void CreateCode()
         {
             var hashSet = this.GetTypes(typeof (CodeAttribute));

+ 3 - 3
Unity/Assets/Scripts/Hotfix/Client/LockStep/LSSceneChangeHelper.cs

@@ -19,7 +19,7 @@ namespace ET.Client
             // 等待Room2C_EnterMap消息
             WaitType.Wait_Room2C_Start waitRoom2CStart = await root.GetComponent<ObjectWait>().Wait<WaitType.Wait_Room2C_Start>();
 
-            room.LSWorld = new LSWorld(SceneType.LockStepClient);
+            room.LSWorld = room.AddChild<LSWorld, SceneType>(SceneType.LockStepClient);
             room.Init(waitRoom2CStart.Message.UnitInfo, waitRoom2CStart.Message.StartTime);
             
             room.AddComponent<LSClientUpdater>();
@@ -37,7 +37,7 @@ namespace ET.Client
             room.Name = "Map1";
             room.IsReplay = true;
             room.Replay = replay;
-            room.LSWorld = new LSWorld(SceneType.LockStepClient);
+            room.LSWorld = room.AddChild<LSWorld, SceneType>(SceneType.LockStepClient);
             room.Init(replay.UnitInfos, TimeInfo.Instance.ServerFrameTime());
             
             // 等待表现层订阅的事件完成
@@ -57,7 +57,7 @@ namespace ET.Client
             Room room = root.AddComponent<Room>();
             room.Name = "Map1";
             
-            room.LSWorld = new LSWorld(SceneType.LockStepClient);
+            room.LSWorld = room.AddChild<LSWorld, SceneType>(SceneType.LockStepClient);
             room.Init(message.UnitInfos, message.StartTime, message.Frame);
             
             // 等待表现层订阅的事件完成

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/LockStep/Room/RoomManager2Room_InitHandler.cs

@@ -11,7 +11,7 @@ namespace ET.Server
             
             room.AddComponent<RoomServerComponent, List<long>>(request.PlayerIds);
 
-            room.LSWorld = new LSWorld(SceneType.LockStepServer);
+            room.LSWorld = room.AddChild<LSWorld, SceneType>(SceneType.LockStepServer);
             await ETTask.CompletedTask;
         }
     }

+ 1 - 1
Unity/Assets/Scripts/Model/Share/LockStep/LSEntity.cs

@@ -51,7 +51,7 @@ namespace ET
 
         protected override long GetLongHashCode(Type type)
         {
-            return CodeTypes.Instance.GetHashByType(type);
+            return LSEntitySystemSingleton.Instance.GetLongHashCode(type);
         }
 
         protected override void RegisterSystem()

+ 16 - 0
Unity/Assets/Scripts/Model/Share/LockStep/LSEntitySystemSingleton.cs

@@ -16,6 +16,8 @@ namespace ET
     {
         public TypeSystems TypeSystems { get; private set; }
         
+        private readonly DoubleMap<Type, long> lsEntityTypeLongHashCode = new();
+        
         public void Awake()
         {
             this.TypeSystems = new(LSQueneUpdateIndex.Max);
@@ -36,6 +38,20 @@ namespace ET
                     oneTypeSystems.QueueFlag[index] = true;
                 }
             }
+            
+            foreach (var kv in CodeTypes.Instance.GetTypes())
+            {
+                Type type = kv.Value;
+                if (typeof(LSEntity).IsAssignableFrom(type))
+                {
+                    this.lsEntityTypeLongHashCode.Add(type, type.FullName.GetLongHashCode());
+                }
+            }
+        }
+        
+        public long GetLongHashCode(Type type)
+        {
+            return this.lsEntityTypeLongHashCode.GetValueByKey(type);
         }
         
         public TypeSystems.OneTypeSystems GetOneTypeSystems(Type type)

+ 20 - 16
Unity/Assets/Scripts/Model/Share/LockStep/LSWorld.cs

@@ -5,8 +5,19 @@ using TrueSync;
 
 namespace ET
 {
-    public static class LSWorldSystem
+    [EntitySystemOf(typeof(LSWorld))]
+    public static partial class LSWorldSystem
     {
+        [EntitySystem]
+        private static void Awake(this LSWorld self, SceneType sceneType)
+        {
+            self.Id = self.GetId();
+
+            self.SceneType = sceneType;
+            
+            self.Fiber().Info($"LSScene create: {self.Id} {self.InstanceId}");
+        }
+        
         public static LSWorld LSWorld(this LSEntity entity)
         {
             return entity.IScene as LSWorld;
@@ -16,31 +27,19 @@ namespace ET
         {
             return entity.LSWorld().GetId();
         }
-        
+
         public static TSRandom GetRandom(this LSEntity entity)
         {
             return entity.LSWorld().Random;
         }
+
     }
 
     [EnableMethod]
     [ChildOf]
     [ComponentOf]
-    public class LSWorld: Entity, IAwake, IScene
+    public class LSWorld: Entity, IAwake<SceneType>, IScene
     {
-        public LSWorld()
-        {
-        }
-        
-        public LSWorld(SceneType sceneType)
-        {
-            this.Id = this.GetId();
-
-            this.SceneType = sceneType;
-            
-            this.Fiber().Info($"LSScene create: {this.Id} {this.InstanceId}");
-        }
-
         private readonly LSUpdater updater = new();
         
         [BsonIgnore]
@@ -111,5 +110,10 @@ namespace ET
         {
             return this.AddChildWithId<T, A, B, C>(this.GetId(), a, b, c, isFromPool);
         }
+        
+        protected override long GetLongHashCode(Type type)
+        {
+            return LSEntitySystemSingleton.Instance.GetLongHashCode(type);
+        }
     }
 }

+ 0 - 1
Unity/Assets/Scripts/Model/Share/LockStep/Room.cs

@@ -41,7 +41,6 @@ namespace ET
             }
             set
             {
-                this.AddChild(value);
                 this.lsWorld = value;
             }
         }