fix: Reset save transfer state and streamline loading logic in SaveTransferPacket and StartGame

This commit is contained in:
2025-12-14 23:56:41 +01:00
parent 4afcaccf75
commit 12a207989e
2 changed files with 16 additions and 75 deletions

View File

@@ -29,32 +29,25 @@ namespace KCM.Packets.Lobby
public override void HandlePacketClient() public override void HandlePacketClient()
{ {
// Initialize saveData and chunksReceived on the first packet received if (chunkId == 0)
// And reset static variables for a new transfer
if (chunkId == 0) // This is the first chunk of a new transfer
{ {
Main.helper.Log("Save Transfer started! Resetting static transfer state."); 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]; saveData = new byte[saveSize];
chunksReceived = new bool[totalChunks]; chunksReceived = new bool[totalChunks];
received = 0; // Reset received bytes counter received = 0;
ServerLobbyScript.LoadingSave.SetActive(true); ServerLobbyScript.LoadingSave.SetActive(true);
} }
// Copy the chunk data into the correct position in saveData
Array.Copy(saveDataChunk, 0, saveData, saveDataIndex, saveDataChunk.Length); Array.Copy(saveDataChunk, 0, saveData, saveDataIndex, saveDataChunk.Length);
// Mark this chunk as received
chunksReceived[chunkId] = true; chunksReceived[chunkId] = true;
// Seek to the next position to write to
received += chunkSize; received += chunkSize;
// Recalculate savePercent AFTER 'received' is updated
if (saveSize > 0) if (saveSize > 0)
{ {
float savePercent = (float)received / (float)saveSize; float savePercent = (float)received / (float)saveSize;
@@ -81,10 +74,8 @@ namespace KCM.Packets.Lobby
Main.helper.Log(WhichIsNotComplete()); Main.helper.Log(WhichIsNotComplete());
} }
// Check if all chunks have been received
if (IsTransferComplete()) if (IsTransferComplete())
{ {
// Handle completed transfer here
Main.helper.Log("Save Transfer complete!"); Main.helper.Log("Save Transfer complete!");
LoadSaveLoadHook.saveBytes = saveData; LoadSaveLoadHook.saveBytes = saveData;
@@ -92,8 +83,9 @@ namespace KCM.Packets.Lobby
LoadSave.Load(); LoadSave.Load();
GameState.inst.SetNewMode(GameState.inst.playingMode);
LobbyManager.loadingSave = false;
LoadSaveLoadHook.saveContainer.Unpack(null);
Broadcast.OnLoadedEvent.Broadcast(new OnLoadedEvent()); Broadcast.OnLoadedEvent.Broadcast(new OnLoadedEvent());
ServerLobbyScript.LoadingSave.SetActive(false); ServerLobbyScript.LoadingSave.SetActive(false);

View File

@@ -18,38 +18,26 @@ namespace KCM.Packets.Lobby
{ {
Main.helper.Log(GameState.inst.mainMenuMode.ToString()); Main.helper.Log(GameState.inst.mainMenuMode.ToString());
// Hide server lobby
Main.TransitionTo((MenuState)200); Main.TransitionTo((MenuState)200);
// This is run when user clicks "accept" on choose your map screeen
try try
{ {
if (!LobbyManager.loadingSave) SpeedControlUI.inst.SetSpeed(0);
{
SpeedControlUI.inst.SetSpeed(0);
try 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
{ {
LobbyManager.loadingSave = false; typeof(MainMenuMode).GetMethod("StartGame", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(GameState.inst.mainMenuMode, null);
GameState.inst.SetNewMode(GameState.inst.playingMode);
} }
catch (Exception ex)
{
Main.helper.Log(ex.Message.ToString());
Main.helper.Log(ex.ToString());
}
SpeedControlUI.inst.SetSpeed(0);
} }
catch (Exception ex) catch (Exception ex)
{ {
// Handle exception here
Main.helper.Log(ex.Message.ToString()); Main.helper.Log(ex.Message.ToString());
Main.helper.Log(ex.ToString()); Main.helper.Log(ex.ToString());
} }
@@ -63,51 +51,12 @@ namespace KCM.Packets.Lobby
} }
else else
{ {
// If loading a save, just ensure the game is in the correct UI state for loading. ServerLobbyScript.LoadingSave.SetActive(true);
// 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);
} }
} }
public override void HandlePacketServer() 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();
}*/
} }
} }
} }