Fix issues with puzzle loading order and add saving state when using Menu
This commit is contained in:
committed by
Michal Pikulski
parent
c527ba334d
commit
717deee0cd
@@ -180,6 +180,67 @@ namespace Core.SaveLoad
|
||||
return participant;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unlocked Minigames Management
|
||||
|
||||
/// <summary>
|
||||
/// Marks a minigame as unlocked in the global save data.
|
||||
/// This is separate from scene-specific participant states and persists across all saves.
|
||||
/// </summary>
|
||||
/// <param name="minigameName">The name/identifier of the minigame (typically scene name)</param>
|
||||
public void UnlockMinigame(string minigameName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(minigameName))
|
||||
{
|
||||
Logging.Warning("[SaveLoadManager] Attempted to unlock minigame with null or empty name");
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentSaveData == null)
|
||||
{
|
||||
Logging.Warning("[SaveLoadManager] Cannot unlock minigame - no save data loaded");
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentSaveData.unlockedMinigames == null)
|
||||
{
|
||||
currentSaveData.unlockedMinigames = new System.Collections.Generic.List<string>();
|
||||
}
|
||||
|
||||
if (!currentSaveData.unlockedMinigames.Contains(minigameName))
|
||||
{
|
||||
currentSaveData.unlockedMinigames.Add(minigameName);
|
||||
Logging.Debug($"[SaveLoadManager] Unlocked minigame: {minigameName}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a minigame has been unlocked.
|
||||
/// </summary>
|
||||
/// <param name="minigameName">The name/identifier of the minigame</param>
|
||||
/// <returns>True if the minigame is unlocked, false otherwise</returns>
|
||||
public bool IsMinigameUnlocked(string minigameName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(minigameName))
|
||||
return false;
|
||||
|
||||
if (currentSaveData == null || currentSaveData.unlockedMinigames == null)
|
||||
return false;
|
||||
|
||||
return currentSaveData.unlockedMinigames.Contains(minigameName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a read-only list of all unlocked minigames.
|
||||
/// </summary>
|
||||
public System.Collections.Generic.IReadOnlyList<string> GetUnlockedMinigames()
|
||||
{
|
||||
if (currentSaveData == null || currentSaveData.unlockedMinigames == null)
|
||||
return new System.Collections.Generic.List<string>();
|
||||
|
||||
return currentSaveData.unlockedMinigames.AsReadOnly();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -343,29 +404,68 @@ namespace Core.SaveLoad
|
||||
|
||||
Logging.Debug("[SaveLoadManager] Saving scene-specific data...");
|
||||
|
||||
// Collect scene data from LifecycleManager
|
||||
// Build a dictionary of all data to save
|
||||
var allSceneData = new Dictionary<string, string>();
|
||||
|
||||
// Collect scene data from ManagedBehaviours via LifecycleManager
|
||||
if (Lifecycle.LifecycleManager.Instance != null)
|
||||
{
|
||||
var sceneData = Lifecycle.LifecycleManager.Instance.BroadcastSceneSaveRequested();
|
||||
|
||||
// Remove old scene data and add new
|
||||
if (currentSaveData.participantStates != null)
|
||||
foreach (var kvp in sceneData)
|
||||
{
|
||||
// Remove existing entries for these SaveIds (to avoid duplicates)
|
||||
currentSaveData.participantStates.RemoveAll(entry => sceneData.ContainsKey(entry.saveId));
|
||||
|
||||
// Add new scene data
|
||||
foreach (var kvp in sceneData)
|
||||
allSceneData[kvp.Key] = kvp.Value;
|
||||
}
|
||||
Logging.Debug($"[SaveLoadManager] Collected {sceneData.Count} ManagedBehaviour scene states");
|
||||
}
|
||||
|
||||
// Collect data from ISaveParticipants (all currently registered, identified by SaveId)
|
||||
foreach (var kvp in participants.ToList())
|
||||
{
|
||||
string saveId = kvp.Key;
|
||||
ISaveParticipant participant = kvp.Value;
|
||||
|
||||
try
|
||||
{
|
||||
string serializedState = participant.SerializeState();
|
||||
allSceneData[saveId] = serializedState;
|
||||
Logging.Debug($"[SaveLoadManager] Captured state for ISaveParticipant: {saveId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.Warning($"[SaveLoadManager] Exception while serializing ISaveParticipant '{saveId}': {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
// Update existing entries or add new ones (matches SaveAsync() pattern)
|
||||
if (currentSaveData.participantStates != null)
|
||||
{
|
||||
int updatedCount = 0;
|
||||
|
||||
foreach (var kvp in allSceneData)
|
||||
{
|
||||
var existingEntry = currentSaveData.participantStates.Find(e => e.saveId == kvp.Key);
|
||||
if (existingEntry != null)
|
||||
{
|
||||
// Update existing entry in place
|
||||
existingEntry.serializedState = kvp.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add new entry
|
||||
currentSaveData.participantStates.Add(new ParticipantStateEntry
|
||||
{
|
||||
saveId = kvp.Key,
|
||||
serializedState = kvp.Value
|
||||
});
|
||||
}
|
||||
updatedCount++;
|
||||
}
|
||||
|
||||
Logging.Debug($"[SaveLoadManager] Updated {sceneData.Count} scene data entries in memory");
|
||||
Logging.Debug($"[SaveLoadManager] Updated {updatedCount} scene data entries in memory");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning("[SaveLoadManager] participantStates list is null, cannot save scene data");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user