From 3ef47e4f55aa4fba1ac60b73d0243a07c82557a8 Mon Sep 17 00:00:00 2001 From: starfish <50672801+starfi5h@users.noreply.github.com> Date: Sat, 23 Mar 2024 04:32:20 +0800 Subject: [PATCH] Fix desync by GameData.CreateDysonSphere --- .../Universe/DysonSphereStatusProcessor.cs | 2 ++ .../Patches/Dynamic/GameData_Patch.cs | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/NebulaNetwork/PacketProcessors/Universe/DysonSphereStatusProcessor.cs b/NebulaNetwork/PacketProcessors/Universe/DysonSphereStatusProcessor.cs index 5148f0eb9..917d0ff03 100644 --- a/NebulaNetwork/PacketProcessors/Universe/DysonSphereStatusProcessor.cs +++ b/NebulaNetwork/PacketProcessors/Universe/DysonSphereStatusProcessor.cs @@ -19,8 +19,10 @@ protected override void ProcessPacket(DysonSphereStatusPacket packet, NebulaConn { return; } + // Replace some values set in DysonSphere.BeforeGameTick dysonSphere.grossRadius = packet.GrossRadius; dysonSphere.energyReqCurrentTick = packet.EnergyReqCurrentTick; dysonSphere.energyGenCurrentTick = packet.EnergyGenCurrentTick; + dysonSphere.energyGenOriginalCurrentTick = (long)(dysonSphere.energyGenCurrentTick / dysonSphere.energyDFHivesDebuffCoef); } } diff --git a/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs b/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs index d27676b5d..7b177e020 100644 --- a/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs @@ -464,4 +464,38 @@ private static void RefreshMissingMeshes() Log.Debug("RefreshMissingMeshes"); } } + + [HarmonyPrefix] + [HarmonyPatch(nameof(GameData.CreateDysonSphere))] + public static bool CreateDysonSphere_Prefix(GameData __instance, int starIndex, ref DysonSphere __result) + { + if (!Multiplayer.IsActive || Multiplayer.Session.IsServer) return true; + + + if ((ulong)starIndex >= (ulong)((long)__instance.galaxy.starCount)) + { + __result = null; + return false; + } + if (__instance.dysonSpheres[starIndex] != null) + { + __result = __instance.dysonSpheres[starIndex]; + return false; + } + + // Create a dummy dyson sphere and prevent sending packets + Multiplayer.Session.DysonSpheres.InBlueprint = true; + __instance.dysonSpheres[starIndex] = new DysonSphere(); + __instance.dysonSpheres[starIndex].Init(__instance, __instance.galaxy.stars[starIndex]); + __instance.dysonSpheres[starIndex].ResetNew(); + Multiplayer.Session.DysonSpheres.InBlueprint = false; + + if (Multiplayer.Session.DysonSpheres.RequestingIndex == -1) + { + // If client is not requesting yet, request the target sphere from server + Multiplayer.Session.DysonSpheres.RequestDysonSphere(starIndex, false); + } + __result = __instance.dysonSpheres[starIndex]; + return false; + } }