From a2d87106bae9db14661377e95621cd3c3d316e9f Mon Sep 17 00:00:00 2001 From: devbeni Date: Sat, 13 Dec 2025 20:21:25 +0100 Subject: [PATCH] =?UTF-8?q?tal=C3=A1n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main.cs | 95 +++++++++++++++++++++++++------ Packets/Game/SetSpeed.cs | 2 +- Packets/Handlers/PacketHandler.cs | 13 +++++ Packets/Lobby/StartGame.cs | 4 +- 4 files changed, 96 insertions(+), 18 deletions(-) diff --git a/Main.cs b/Main.cs index 1e772a7..e387c8a 100644 --- a/Main.cs +++ b/Main.cs @@ -58,6 +58,7 @@ namespace KCM private static readonly Dictionary lastTeamIdLookupLogMs = new Dictionary(); private static int resetInProgress = 0; private static int multiplayerSaveLoadInProgress = 0; + private static int suppressVillagerTeleportPackets = 0; public static bool IsMultiplayerSaveLoadInProgress { @@ -69,6 +70,16 @@ namespace KCM Interlocked.Exchange(ref multiplayerSaveLoadInProgress, inProgress ? 1 : 0); } + private static bool ShouldSuppressVillagerTeleportPackets + { + get { return Volatile.Read(ref suppressVillagerTeleportPackets) != 0; } + } + + private static void SetSuppressVillagerTeleportPackets(bool suppress) + { + Interlocked.Exchange(ref suppressVillagerTeleportPackets, suppress ? 1 : 0); + } + public static void ResetMultiplayerState(string reason = null) { if (Interlocked.Exchange(ref resetInProgress, 1) == 1) @@ -171,10 +182,51 @@ namespace KCM try { Player.inst.irrigation.UpdateIrrigation(); } catch (Exception e) { helper?.Log(e.ToString()); } try { Player.inst.CalcMaxResources(null, -1); } catch (Exception e) { helper?.Log(e.ToString()); } + try { if (UnitSystem.inst != null) UnitSystem.inst.enabled = true; } catch (Exception e) { helper?.Log(e.ToString()); } + try { if (JobSystem.inst != null) JobSystem.inst.enabled = true; } catch (Exception e) { helper?.Log(e.ToString()); } + try { if (VillagerSystem.inst != null) VillagerSystem.inst.enabled = true; } catch (Exception e) { helper?.Log(e.ToString()); } + SetLoadTickDelay(Player.inst, 1); SetLoadTickDelay(UnitSystem.inst, 1); SetLoadTickDelay(JobSystem.inst, 1); SetLoadTickDelay(VillagerSystem.inst, 1); + + try + { + // A nudge helps recover from cases where villagers have jobs but never begin moving. + SetSuppressVillagerTeleportPackets(true); + foreach (var kcPlayer in kCPlayers.Values) + { + if (kcPlayer == null || kcPlayer.inst == null) + continue; + + var workers = kcPlayer.inst.Workers; + for (int i = 0; i < workers.Count; i++) + { + Villager v = workers.data[i]; + if (v == null) + continue; + + try + { + Vector3 pos = v.transform != null ? v.transform.position : Vector3.zero; + v.TeleportTo(pos); + } + catch + { + } + } + } + } + catch (Exception e) + { + helper?.Log("Post-load villager nudge failed"); + helper?.Log(e.ToString()); + } + finally + { + SetSuppressVillagerTeleportPackets(false); + } } catch (Exception e) { @@ -434,6 +486,26 @@ namespace KCM if ((MenuState)newState == MenuState.Menu && (KCClient.client.IsConnected || KCServer.IsRunning)) ResetMultiplayerState("Returned to main menu"); + + if ((MenuState)newState == (MenuState)200 && KCClient.client.IsConnected) + { + try + { + RunPostLoadRebuild("Entered playing mode"); + } + catch + { + } + + try + { + if (SpeedControlUI.inst != null) + SpeedControlUI.inst.SetSpeed(1); + } + catch + { + } + } } } @@ -1126,13 +1198,7 @@ namespace KCM if (KCClient.client.IsConnected) { bool calledFromPacket = false; - try - { - calledFromPacket = new StackFrame(3).GetMethod().Name.Contains("HandlePacket"); - } - catch - { - } + try { calledFromPacket = PacketHandler.IsHandlingPacket; } catch { } if (!calledFromPacket) { @@ -1151,14 +1217,6 @@ namespace KCM if (!__state) return; - /*Main.helper.Log($"set speed Called by 0: {new StackFrame(0).GetMethod()} {new StackFrame(0).GetMethod().Name.Contains("HandlePacket")}"); - Main.helper.Log($"set speed Called by 1: {new StackFrame(1).GetMethod()} {new StackFrame(1).GetMethod().Name.Contains("HandlePacket")}"); - Main.helper.Log($"set speed Called by 2: {new StackFrame(2).GetMethod()} {new StackFrame(2).GetMethod().Name.Contains("HandlePacket")}"); - Main.helper.Log($"set speed Called by 3: {new StackFrame(3).GetMethod()} {new StackFrame(3).GetMethod().Name.Contains("HandlePacket")}");*/ - - if (new StackFrame(3).GetMethod().Name.Contains("HandlePacket")) - return; - Main.helper.Log("SpeedControlUI.SetSpeed (local): " + idx); new SetSpeed() { @@ -1274,7 +1332,12 @@ namespace KCM { if (KCClient.client.IsConnected) { - if (new StackFrame(3).GetMethod().Name.Contains("HandlePacket")) + if (ShouldSuppressVillagerTeleportPackets) + return; + + bool calledFromPacket = false; + try { calledFromPacket = PacketHandler.IsHandlingPacket; } catch { } + if (calledFromPacket) return; new VillagerTeleportTo() diff --git a/Packets/Game/SetSpeed.cs b/Packets/Game/SetSpeed.cs index 319eb71..4824d1d 100644 --- a/Packets/Game/SetSpeed.cs +++ b/Packets/Game/SetSpeed.cs @@ -22,7 +22,7 @@ namespace KCM.Packets.Game public override void HandlePacketServer() { - //throw new NotImplementedException(); + // Server relay is handled automatically by PacketHandler unless [NoServerRelay] is used. } } } diff --git a/Packets/Handlers/PacketHandler.cs b/Packets/Handlers/PacketHandler.cs index 90a91ef..f2adc25 100644 --- a/Packets/Handlers/PacketHandler.cs +++ b/Packets/Handlers/PacketHandler.cs @@ -15,6 +15,14 @@ namespace KCM.Packets.Handlers { public class PacketHandler { + [ThreadStatic] + private static bool isHandlingPacket; + + public static bool IsHandlingPacket + { + get { return isHandlingPacket; } + } + public static Dictionary Packets = new Dictionary(); public class PacketRef { @@ -183,6 +191,7 @@ namespace KCM.Packets.Handlers { try { + isHandlingPacket = true; packet.HandlePacketClient(); } catch (Exception ex) @@ -205,6 +214,10 @@ namespace KCM.Packets.Handlers Main.helper.Log(ex.InnerException.StackTrace); } } + finally + { + isHandlingPacket = false; + } } /* if (PacketHandlers.TryGetValue(id, out PacketHandlerDelegate handler)) diff --git a/Packets/Lobby/StartGame.cs b/Packets/Lobby/StartGame.cs index eefcfcf..ab2ab39 100644 --- a/Packets/Lobby/StartGame.cs +++ b/Packets/Lobby/StartGame.cs @@ -25,6 +25,7 @@ namespace KCM.Packets.Lobby try { + int desiredSpeed = 1; if (!LobbyManager.loadingSave) { SpeedControlUI.inst.SetSpeed(0); @@ -39,12 +40,13 @@ namespace KCM.Packets.Lobby Main.helper.Log(ex.ToString()); } - SpeedControlUI.inst.SetSpeed(0); + SpeedControlUI.inst.SetSpeed(desiredSpeed); } else { LobbyManager.loadingSave = false; GameState.inst.SetNewMode(GameState.inst.playingMode); + SpeedControlUI.inst.SetSpeed(desiredSpeed); } } catch (Exception ex)