LSClientHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System.IO;
  2. using ET.Client;
  3. namespace ET
  4. {
  5. public static partial class LSClientHelper
  6. {
  7. public static void RunRollbackSystem(Entity entity)
  8. {
  9. if (entity is LSEntity)
  10. {
  11. return;
  12. }
  13. LSEntitySystemSington.Instance.Rollback(entity);
  14. if (entity.ComponentsCount() > 0)
  15. {
  16. foreach (var kv in entity.Components)
  17. {
  18. RunRollbackSystem(kv.Value);
  19. }
  20. }
  21. if (entity.ChildrenCount() > 0)
  22. {
  23. foreach (var kv in entity.Children)
  24. {
  25. RunRollbackSystem(kv.Value);
  26. }
  27. }
  28. }
  29. // 回滚
  30. public static void Rollback(Room room, int frame)
  31. {
  32. room.LSWorld.Dispose();
  33. FrameBuffer frameBuffer = room.FrameBuffer;
  34. // 回滚
  35. room.LSWorld = room.GetLSWorld(SceneType.LockStepClient, frame);
  36. OneFrameInputs authorityFrameInput = frameBuffer.FrameInputs(frame);
  37. // 执行AuthorityFrame
  38. room.Update(authorityFrameInput);
  39. room.SendHash(frame);
  40. // 重新执行预测的帧
  41. for (int i = room.AuthorityFrame + 1; i <= room.PredictionFrame; ++i)
  42. {
  43. OneFrameInputs oneFrameInputs = frameBuffer.FrameInputs(i);
  44. LSClientHelper.CopyOtherInputsTo(room, authorityFrameInput, oneFrameInputs); // 重新预测消息
  45. room.Update(oneFrameInputs);
  46. }
  47. RunRollbackSystem(room);
  48. }
  49. public static void SendHash(this Room self, int frame)
  50. {
  51. if (frame > self.AuthorityFrame)
  52. {
  53. return;
  54. }
  55. long hash = self.FrameBuffer.GetHash(frame);
  56. C2Room_CheckHash c2RoomCheckHash = NetServices.Instance.FetchMessage<C2Room_CheckHash>();
  57. c2RoomCheckHash.Frame = frame;
  58. c2RoomCheckHash.Hash = hash;
  59. self.GetParent<Scene>().GetComponent<SessionComponent>().Session.Send(c2RoomCheckHash);
  60. }
  61. // 重新调整预测消息,只需要调整其他玩家的输入
  62. public static void CopyOtherInputsTo(Room room, OneFrameInputs from, OneFrameInputs to)
  63. {
  64. long myId = room.GetComponent<LSClientUpdater>().MyId;
  65. foreach (var kv in from.Inputs)
  66. {
  67. if (kv.Key == myId)
  68. {
  69. continue;
  70. }
  71. to.Inputs[kv.Key] = kv.Value;
  72. }
  73. }
  74. public static void SaveReplay(Room room, string path)
  75. {
  76. if (room.IsReplay)
  77. {
  78. return;
  79. }
  80. Log.Debug($"save replay: {path} frame: {room.Replay.FrameInputs.Count}");
  81. byte[] bytes = MemoryPackHelper.Serialize(room.Replay);
  82. File.WriteAllBytes(path, bytes);
  83. }
  84. public static void JumpReplay(Room room, int frame)
  85. {
  86. if (!room.IsReplay)
  87. {
  88. return;
  89. }
  90. if (frame >= room.Replay.FrameInputs.Count)
  91. {
  92. frame = room.Replay.FrameInputs.Count - 1;
  93. }
  94. int snapshotIndex = frame / LSConstValue.SaveLSWorldFrameCount;
  95. Log.Debug($"jump replay start {room.AuthorityFrame} {frame} {snapshotIndex}");
  96. if (snapshotIndex != room.AuthorityFrame / LSConstValue.SaveLSWorldFrameCount || frame < room.AuthorityFrame)
  97. {
  98. room.LSWorld.Dispose();
  99. // 回滚
  100. byte[] memoryBuffer = room.Replay.Snapshots[snapshotIndex];
  101. LSWorld lsWorld = MongoHelper.Deserialize(typeof (LSWorld), memoryBuffer, 0, memoryBuffer.Length) as LSWorld;
  102. room.LSWorld = lsWorld;
  103. room.AuthorityFrame = snapshotIndex * LSConstValue.SaveLSWorldFrameCount;
  104. RunRollbackSystem(room);
  105. }
  106. room.FixedTimeCounter.Reset(TimeHelper.ServerFrameTime() - frame * LSConstValue.UpdateInterval, 0);
  107. Log.Debug($"jump replay finish {frame}");
  108. }
  109. }
  110. }