From f45402af9a0e0265f55b5fe66e50df0df53e9560 Mon Sep 17 00:00:00 2001 From: devbeni Date: Sat, 13 Dec 2025 19:42:49 +0100 Subject: [PATCH] fix 2 --- Main.cs | 60 +++++++++++++++++++---------- Packets/Lobby/SaveTransferPacket.cs | 11 +++++- 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/Main.cs b/Main.cs index 7bbd423..94525a2 100644 --- a/Main.cs +++ b/Main.cs @@ -57,6 +57,17 @@ namespace KCM private static readonly Dictionary lastTeamIdLookupLogMs = new Dictionary(); private static int resetInProgress = 0; + private static int multiplayerSaveLoadInProgress = 0; + + public static bool IsMultiplayerSaveLoadInProgress + { + get { return Volatile.Read(ref multiplayerSaveLoadInProgress) != 0; } + } + + public static void SetMultiplayerSaveLoadInProgress(bool inProgress) + { + Interlocked.Exchange(ref multiplayerSaveLoadInProgress, inProgress ? 1 : 0); + } public static void ResetMultiplayerState(string reason = null) { @@ -82,6 +93,7 @@ namespace KCM try { LoadSaveLoadAtPathHook.saveData = new byte[0]; } catch { } try { LobbyManager.loadingSave = false; } catch { } + try { SetMultiplayerSaveLoadInProgress(false); } catch { } try { @@ -1294,20 +1306,18 @@ namespace KCM { BinaryFormatter bf = new BinaryFormatter(); bf.Binder = new MultiplayerSaveDeserializationBinder(); - Stream file = new FileStream(path, FileMode.Open); + Stream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + MultiplayerSaveContainer loadData = null; try { object deserialized = bf.Deserialize(file); - MultiplayerSaveContainer loadData = deserialized as MultiplayerSaveContainer; + loadData = deserialized as MultiplayerSaveContainer; if (loadData == null) { Main.helper.Log("Selected save is not a MultiplayerSaveContainer; falling back to vanilla load."); + saveData = new byte[0]; return true; } - - saveData = File.ReadAllBytes(path); - loadData.Unpack(null); - Broadcast.OnLoadedEvent.Broadcast(new OnLoadedEvent()); } catch (Exception e) { @@ -1326,6 +1336,29 @@ namespace KCM file.Dispose(); } } + + try + { + saveData = File.ReadAllBytes(path); + } + catch (Exception e) + { + saveData = new byte[0]; + Main.helper.Log("Failed reading save bytes for transfer; clients will not be able to load this save."); + Main.helper.Log(e.ToString()); + } + + try + { + Main.SetMultiplayerSaveLoadInProgress(true); + loadData.Unpack(null); + } + finally + { + Main.SetMultiplayerSaveLoadInProgress(false); + } + + Broadcast.OnLoadedEvent.Broadcast(new OnLoadedEvent()); } return false; @@ -1524,7 +1557,7 @@ namespace KCM { public static bool Prefix(Building.BuildingSaveData structureData, Player p, ref Building __result) { - if (KCClient.client.IsConnected) + if (KCClient.client.IsConnected && Main.IsMultiplayerSaveLoadInProgress) { Building Building = GameState.inst.GetPlaceableByUniqueName(structureData.uniqueName); @@ -1561,19 +1594,6 @@ namespace KCM p.keep = keep; Main.helper.Log(p.keep.ToString()); } - - try - { - World.inst.PlaceFromLoad(building); - structureData.UnpackStage2(building); - building.SetVisibleForFog(false); - } - catch (Exception e) - { - Main.helper.Log("Error placing building into world during load"); - Main.helper.Log(e.Message); - Main.helper.Log(e.StackTrace); - } __result = building; } else diff --git a/Packets/Lobby/SaveTransferPacket.cs b/Packets/Lobby/SaveTransferPacket.cs index c4707b7..d3e1374 100644 --- a/Packets/Lobby/SaveTransferPacket.cs +++ b/Packets/Lobby/SaveTransferPacket.cs @@ -108,8 +108,15 @@ namespace KCM.Packets.Lobby LoadSave.Load(); - - LoadSaveLoadHook.saveContainer.Unpack(null); + try + { + Main.SetMultiplayerSaveLoadInProgress(true); + LoadSaveLoadHook.saveContainer.Unpack(null); + } + finally + { + Main.SetMultiplayerSaveLoadInProgress(false); + } Broadcast.OnLoadedEvent.Broadcast(new OnLoadedEvent()); try