Explorar el Código

修复一个不同步的bug

tanghai hace 2 años
padre
commit
b5a21ed102

+ 2 - 24
Unity/Assets/Scripts/Hotfix/Client/LockStep/LSClientUpdaterSystem.cs

@@ -46,15 +46,9 @@ namespace ET.Client
                 ++room.PredictionFrame;
                 OneFrameInputs oneFrameInputs = self.GetOneFrameMessages(room.PredictionFrame);
                 
-                // 保存当前帧场景数据
-                room.SaveLSWorld(room.PredictionFrame);
-
-                if (room.PredictionFrame <= room.AuthorityFrame) // 只有AuthorityFrame帧才保存录像数据
-                {
-                    self.Record(room.PredictionFrame);
-                }
+                room.Update(oneFrameInputs);
+                room.SendHash(room.PredictionFrame);
                 
-                room.Update(oneFrameInputs, room.PredictionFrame);
                 room.SpeedMultiply = ++i;
 
                 FrameMessage frameMessage = NetServices.Instance.FetchMessage<FrameMessage>();
@@ -94,21 +88,5 @@ namespace ET.Client
             
             return predictionFrame;
         }
-
-        public static void Record(this LSClientUpdater self, int frame)
-        {
-            Room room = self.Room();
-            //if (frame < room.AuthorityFrame)
-            //{
-            //    return;
-            //}
-            Log.Debug($"{self.Room().Name} Record: {frame}");
-            long hash = room.FrameBuffer.GetHash(frame);
-            C2Room_CheckHash c2RoomCheckHash = NetServices.Instance.FetchMessage<C2Room_CheckHash>();
-            c2RoomCheckHash.Frame = frame;
-            c2RoomCheckHash.Hash = hash;
-            room.GetParent<Scene>().GetComponent<SessionComponent>().Session.Send(c2RoomCheckHash);
-            room.Record(frame);
-        }
     }
 }

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Client/LockStep/LSReplayUpdaterSystem.cs

@@ -36,7 +36,7 @@ namespace ET.Client
 
                 OneFrameInputs oneFrameInputs = room.Replay.FrameInputs[room.AuthorityFrame];
             
-                room.Update(oneFrameInputs, room.AuthorityFrame);
+                room.Update(oneFrameInputs);
                 room.SpeedMultiply = ++i;
                 
                 long timeNow2 = TimeHelper.ServerNow();

+ 1 - 2
Unity/Assets/Scripts/Hotfix/Client/LockStep/OneFrameInputsHandler.cs

@@ -37,8 +37,7 @@ namespace ET.Client
                 }
                 else
                 {
-                    LSClientUpdater clientUpdater = room.GetComponent<LSClientUpdater>();
-                    clientUpdater.Record(room.AuthorityFrame);
+                    room.SendHash(room.AuthorityFrame);
                 }
             }
 

+ 2 - 1
Unity/Assets/Scripts/Hotfix/Client/LockStep/Room2C_CheckHashFailHandler.cs

@@ -11,7 +11,8 @@ namespace ET.Client
                 Log.Debug($"check hash fail, server: {message.Frame} {serverWorld.ToJson()}");
             }
 
-            LSWorld clientWorld = session.ClientScene().GetComponent<Room>().GetLSWorld(SceneType.LockStepClient, message.Frame);
+            Room room = session.ClientScene().GetComponent<Room>();
+            LSWorld clientWorld = room.GetLSWorld(SceneType.LockStepClient, message.Frame);
             using (session.ClientScene().AddChild(clientWorld))
             {
                 Log.Debug($"check hash fail, client: {message.Frame} {clientWorld.ToJson()}");

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

@@ -35,11 +35,7 @@ namespace ET.Server
 
             RoomMessageHelper.BroadCast(room, sendInput);
             
-            // 保存当前帧场景数据
-            room.SaveLSWorld(room.AuthorityFrame);
-            room.Record(room.AuthorityFrame);
-            
-            room.Update(oneFrameInputs, frame);
+            room.Update(oneFrameInputs);
         }
 
         private static OneFrameInputs GetOneFrameMessage(this LSServerUpdater self, int frame)

+ 16 - 3
Unity/Assets/Scripts/Hotfix/Share/LockStep/LSHelper.cs

@@ -42,16 +42,16 @@ namespace ET
             room.LSWorld = room.GetLSWorld(SceneType.LockStepClient, frame);
             OneFrameInputs authorityFrameInput = frameBuffer.FrameInputs(frame);
             // 执行AuthorityFrame
-            room.Update(authorityFrameInput, frame);
+            room.Update(authorityFrameInput);
+            room.SendHash(frame);
 
             
             // 重新执行预测的帧
             for (int i = room.AuthorityFrame + 1; i <= room.PredictionFrame; ++i)
             {
-                Log.Debug($"roll back predict {i}");
                 OneFrameInputs oneFrameInputs = frameBuffer.FrameInputs(i);
                 LSHelper.CopyOtherInputsTo(room, authorityFrameInput, oneFrameInputs); // 重新预测消息
-                room.Update(authorityFrameInput, i);
+                room.Update(oneFrameInputs);
             }
             
             RunRollbackSystem(room);
@@ -59,6 +59,19 @@ namespace ET
             Log.Debug($"roll back finish {frame}");
         }
         
+        public static void SendHash(this Room self, int frame)
+        {
+            if (frame > self.AuthorityFrame)
+            {
+                return;
+            }
+            long hash = self.FrameBuffer.GetHash(frame);
+            C2Room_CheckHash c2RoomCheckHash = NetServices.Instance.FetchMessage<C2Room_CheckHash>();
+            c2RoomCheckHash.Frame = frame;
+            c2RoomCheckHash.Hash = hash;
+            self.GetParent<Scene>().GetComponent<SessionComponent>().Session.Send(c2RoomCheckHash);
+        }
+        
         // 重新调整预测消息,只需要调整其他玩家的输入
         public static void CopyOtherInputsTo(Room room, OneFrameInputs from, OneFrameInputs to)
         {

+ 10 - 11
Unity/Assets/Scripts/Hotfix/Share/LockStep/RoomSystem.cs

@@ -29,8 +29,15 @@ namespace ET
         }
 
 
-        public static void Update(this Room self, OneFrameInputs oneFrameInputs, int frame)
+        public static void Update(this Room self, OneFrameInputs oneFrameInputs)
         {
+            if (!self.IsReplay)
+            {
+                // 保存当前帧场景数据
+                self.SaveLSWorld();
+                self.Record(self.LSWorld.Frame);
+            }
+
             LSWorld lsWorld = self.LSWorld;
 
             // 设置输入到每个LSUnit身上
@@ -55,16 +62,12 @@ namespace ET
             return lsWorld;
         }
 
-        public static void SaveLSWorld(this Room self, int frame)
+        public static void SaveLSWorld(this Room self)
         {
+            int frame = self.LSWorld.Frame;
             MemoryBuffer memoryBuffer = self.FrameBuffer.Snapshot(frame);
             memoryBuffer.Seek(0, SeekOrigin.Begin);
             memoryBuffer.SetLength(0);
-
-            if (frame != self.LSWorld.Frame)
-            {
-                Log.Error($"lsworld frame diff: {frame} {self.LSWorld.Frame}");
-            }
             
             MongoHelper.Serialize(self.LSWorld, memoryBuffer);
             memoryBuffer.Seek(0, SeekOrigin.Begin);
@@ -77,10 +80,6 @@ namespace ET
         // 记录需要存档的数据
         public static void Record(this Room self, int frame)
         {
-            if (self.IsReplay)
-            {
-                return;
-            }
             OneFrameInputs oneFrameInputs = self.FrameBuffer.FrameInputs(frame);
             OneFrameInputs saveInput = new();
             oneFrameInputs.CopyTo(saveInput);

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

@@ -1,10 +1,7 @@
-using MongoDB.Bson.Serialization.Attributes;
-
 namespace ET
 {
     public class LSInputComponent: LSEntity, ILSUpdate, IAwake, ISerializeToEntity
     {
-        [BsonIgnore]
-        public LSInput LSInput { get; set; } = new LSInput();
+        public LSInput LSInput { get; set; }
     }
 }