This commit is contained in:
2025-12-13 20:21:25 +01:00
parent 0f8f3ce818
commit a2d87106ba
4 changed files with 96 additions and 18 deletions

95
Main.cs
View File

@@ -58,6 +58,7 @@ namespace KCM
private static readonly Dictionary<int, long> lastTeamIdLookupLogMs = new Dictionary<int, long>(); private static readonly Dictionary<int, long> lastTeamIdLookupLogMs = new Dictionary<int, long>();
private static int resetInProgress = 0; private static int resetInProgress = 0;
private static int multiplayerSaveLoadInProgress = 0; private static int multiplayerSaveLoadInProgress = 0;
private static int suppressVillagerTeleportPackets = 0;
public static bool IsMultiplayerSaveLoadInProgress public static bool IsMultiplayerSaveLoadInProgress
{ {
@@ -69,6 +70,16 @@ namespace KCM
Interlocked.Exchange(ref multiplayerSaveLoadInProgress, inProgress ? 1 : 0); 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) public static void ResetMultiplayerState(string reason = null)
{ {
if (Interlocked.Exchange(ref resetInProgress, 1) == 1) 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.irrigation.UpdateIrrigation(); } catch (Exception e) { helper?.Log(e.ToString()); }
try { Player.inst.CalcMaxResources(null, -1); } 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(Player.inst, 1);
SetLoadTickDelay(UnitSystem.inst, 1); SetLoadTickDelay(UnitSystem.inst, 1);
SetLoadTickDelay(JobSystem.inst, 1); SetLoadTickDelay(JobSystem.inst, 1);
SetLoadTickDelay(VillagerSystem.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) catch (Exception e)
{ {
@@ -434,6 +486,26 @@ namespace KCM
if ((MenuState)newState == MenuState.Menu && (KCClient.client.IsConnected || KCServer.IsRunning)) if ((MenuState)newState == MenuState.Menu && (KCClient.client.IsConnected || KCServer.IsRunning))
ResetMultiplayerState("Returned to main menu"); 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) if (KCClient.client.IsConnected)
{ {
bool calledFromPacket = false; bool calledFromPacket = false;
try try { calledFromPacket = PacketHandler.IsHandlingPacket; } catch { }
{
calledFromPacket = new StackFrame(3).GetMethod().Name.Contains("HandlePacket");
}
catch
{
}
if (!calledFromPacket) if (!calledFromPacket)
{ {
@@ -1151,14 +1217,6 @@ namespace KCM
if (!__state) if (!__state)
return; 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); Main.helper.Log("SpeedControlUI.SetSpeed (local): " + idx);
new SetSpeed() new SetSpeed()
{ {
@@ -1274,7 +1332,12 @@ namespace KCM
{ {
if (KCClient.client.IsConnected) 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; return;
new VillagerTeleportTo() new VillagerTeleportTo()

View File

@@ -22,7 +22,7 @@ namespace KCM.Packets.Game
public override void HandlePacketServer() public override void HandlePacketServer()
{ {
//throw new NotImplementedException(); // Server relay is handled automatically by PacketHandler unless [NoServerRelay] is used.
} }
} }
} }

View File

@@ -15,6 +15,14 @@ namespace KCM.Packets.Handlers
{ {
public class PacketHandler public class PacketHandler
{ {
[ThreadStatic]
private static bool isHandlingPacket;
public static bool IsHandlingPacket
{
get { return isHandlingPacket; }
}
public static Dictionary<ushort, PacketRef> Packets = new Dictionary<ushort, PacketRef>(); public static Dictionary<ushort, PacketRef> Packets = new Dictionary<ushort, PacketRef>();
public class PacketRef public class PacketRef
{ {
@@ -183,6 +191,7 @@ namespace KCM.Packets.Handlers
{ {
try try
{ {
isHandlingPacket = true;
packet.HandlePacketClient(); packet.HandlePacketClient();
} }
catch (Exception ex) catch (Exception ex)
@@ -205,6 +214,10 @@ namespace KCM.Packets.Handlers
Main.helper.Log(ex.InnerException.StackTrace); Main.helper.Log(ex.InnerException.StackTrace);
} }
} }
finally
{
isHandlingPacket = false;
}
} }
/* if (PacketHandlers.TryGetValue(id, out PacketHandlerDelegate handler)) /* if (PacketHandlers.TryGetValue(id, out PacketHandlerDelegate handler))

View File

@@ -25,6 +25,7 @@ namespace KCM.Packets.Lobby
try try
{ {
int desiredSpeed = 1;
if (!LobbyManager.loadingSave) if (!LobbyManager.loadingSave)
{ {
SpeedControlUI.inst.SetSpeed(0); SpeedControlUI.inst.SetSpeed(0);
@@ -39,12 +40,13 @@ namespace KCM.Packets.Lobby
Main.helper.Log(ex.ToString()); Main.helper.Log(ex.ToString());
} }
SpeedControlUI.inst.SetSpeed(0); SpeedControlUI.inst.SetSpeed(desiredSpeed);
} }
else else
{ {
LobbyManager.loadingSave = false; LobbyManager.loadingSave = false;
GameState.inst.SetNewMode(GameState.inst.playingMode); GameState.inst.SetNewMode(GameState.inst.playingMode);
SpeedControlUI.inst.SetSpeed(desiredSpeed);
} }
} }
catch (Exception ex) catch (Exception ex)