From 12a207989e1a5188444a9a5b222762f7bd3c1abb Mon Sep 17 00:00:00 2001 From: devbeni Date: Sun, 14 Dec 2025 23:56:41 +0100 Subject: [PATCH] fix: Reset save transfer state and streamline loading logic in SaveTransferPacket and StartGame --- Packets/Lobby/SaveTransferPacket.cs | 18 ++----- Packets/Lobby/StartGame.cs | 73 +++++------------------------ 2 files changed, 16 insertions(+), 75 deletions(-) diff --git a/Packets/Lobby/SaveTransferPacket.cs b/Packets/Lobby/SaveTransferPacket.cs index 94d2c63..c398bda 100644 --- a/Packets/Lobby/SaveTransferPacket.cs +++ b/Packets/Lobby/SaveTransferPacket.cs @@ -29,32 +29,25 @@ namespace KCM.Packets.Lobby public override void HandlePacketClient() { - // Initialize saveData and chunksReceived on the first packet received - // And reset static variables for a new transfer - if (chunkId == 0) // This is the first chunk of a new transfer + if (chunkId == 0) { Main.helper.Log("Save Transfer started! Resetting static transfer state."); - loadingSave = true; // This is SaveTransferPacket.loadingSave + loadingSave = true; - // Reset static transfer variables saveData = new byte[saveSize]; chunksReceived = new bool[totalChunks]; - received = 0; // Reset received bytes counter + received = 0; ServerLobbyScript.LoadingSave.SetActive(true); } - // Copy the chunk data into the correct position in saveData Array.Copy(saveDataChunk, 0, saveData, saveDataIndex, saveDataChunk.Length); - // Mark this chunk as received chunksReceived[chunkId] = true; - // Seek to the next position to write to received += chunkSize; - // Recalculate savePercent AFTER 'received' is updated if (saveSize > 0) { float savePercent = (float)received / (float)saveSize; @@ -81,10 +74,8 @@ namespace KCM.Packets.Lobby Main.helper.Log(WhichIsNotComplete()); } - // Check if all chunks have been received if (IsTransferComplete()) { - // Handle completed transfer here Main.helper.Log("Save Transfer complete!"); LoadSaveLoadHook.saveBytes = saveData; @@ -92,8 +83,9 @@ namespace KCM.Packets.Lobby LoadSave.Load(); + GameState.inst.SetNewMode(GameState.inst.playingMode); + LobbyManager.loadingSave = false; - LoadSaveLoadHook.saveContainer.Unpack(null); Broadcast.OnLoadedEvent.Broadcast(new OnLoadedEvent()); ServerLobbyScript.LoadingSave.SetActive(false); diff --git a/Packets/Lobby/StartGame.cs b/Packets/Lobby/StartGame.cs index 823f9c2..e955d9c 100644 --- a/Packets/Lobby/StartGame.cs +++ b/Packets/Lobby/StartGame.cs @@ -18,38 +18,26 @@ namespace KCM.Packets.Lobby { Main.helper.Log(GameState.inst.mainMenuMode.ToString()); - // Hide server lobby Main.TransitionTo((MenuState)200); - // This is run when user clicks "accept" on choose your map screeen - try { - if (!LobbyManager.loadingSave) - { - SpeedControlUI.inst.SetSpeed(0); + SpeedControlUI.inst.SetSpeed(0); - try - { - typeof(MainMenuMode).GetMethod("StartGame", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(GameState.inst.mainMenuMode, null); - } - catch (Exception ex) - { - Main.helper.Log(ex.Message.ToString()); - Main.helper.Log(ex.ToString()); - } - - SpeedControlUI.inst.SetSpeed(0); - } - else + try { - LobbyManager.loadingSave = false; - GameState.inst.SetNewMode(GameState.inst.playingMode); + typeof(MainMenuMode).GetMethod("StartGame", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(GameState.inst.mainMenuMode, null); } + catch (Exception ex) + { + Main.helper.Log(ex.Message.ToString()); + Main.helper.Log(ex.ToString()); + } + + SpeedControlUI.inst.SetSpeed(0); } catch (Exception ex) { - // Handle exception here Main.helper.Log(ex.Message.ToString()); Main.helper.Log(ex.ToString()); } @@ -63,51 +51,12 @@ namespace KCM.Packets.Lobby } else { - // If loading a save, just ensure the game is in the correct UI state for loading. - // The actual game start will happen after save data is unpacked in SaveTransferPacket.HandlePacketClient(). - // This prevents MainMenuMode.StartGame() from being called prematurely and causing NullReferenceExceptions. - GameState.inst.SetNewMode(GameState.inst.playingMode); + ServerLobbyScript.LoadingSave.SetActive(true); } } public override void HandlePacketServer() { - //Start(); - - - /*AIBrainsContainer.PreStartAIConfig aiConfig = new AIBrainsContainer.PreStartAIConfig(); - int count = 0; - for (int i = 0; i < RivalKingdomSettingsUI.inst.rivalItems.Length; i++) - { - RivalItemUI r = RivalKingdomSettingsUI.inst.rivalItems[i]; - bool flag = r.Enabled && !r.Locked; - if (flag) - { - count++; - } - } - int idx = 0; - aiConfig.startData = new AIBrainsContainer.PreStartAIConfig.AIStartData[count]; - for (int j = 0; j < RivalKingdomSettingsUI.inst.rivalItems.Length; j++) - { - RivalItemUI item = RivalKingdomSettingsUI.inst.rivalItems[j]; - bool flag2 = item.Enabled && !item.Locked; - if (flag2) - { - aiConfig.startData[idx] = new AIBrainsContainer.PreStartAIConfig.AIStartData(); - aiConfig.startData[idx].landmass = item.flag.landmass; - aiConfig.startData[idx].bioCode = item.bannerIdx; - aiConfig.startData[idx].personalityKey = PersonalityCollection.aiPersonalityKeys[0]; - aiConfig.startData[idx].skillLevel = item.GetSkillLevel(); - idx++; - } - } - AIBrainsContainer.inst.aiStartInfo = aiConfig; - bool isControllerActive = GamepadControl.inst.isControllerActive; - if (isControllerActive) - { - ConsoleCursorMenu.inst.PrepForGamepad(); - }*/ } } }