Files
Project-M/Assets/_Project/Scripts/Simulation/Persistence/SaveService.cs
T
kronic b34945c2d2 LANTERN purge B3+B5: delete the cycle/core/win-lose spine + onboarding; save epoch v7
Deletes CyclePhaseSystem, GoalReachedSystem, CoreDamage/CoreRestore, ThreatDirector,
CoreIntegrity/GoalProgress/RunPhase/RunOutcome/ThreatState components,
CoreVisualFeedbackSystem, and the whole Client/Onboarding slice (+6 test files).

Keepers reworked: RunDirectorSystem (UpdateBefore attr + launch guard + goal/threat
bank removed; sole SaveRequest raiser now), CycleDirectorSpawnSystem (ledger/meta
host only), WaveSystem UNGATED (waves run wherever a WaveDirector is baked),
EnemyAISystem core-fallback stripped, AmbientAudioSystem reworked (bed + run cues;
no CycleState gate), MusicSystem RunInfo-only, HudSystem big trim (goal meter, core
bar, siege banner, terminal banner, outcome flash, onboarding hook all gone),
MetaShop/ClassPrep/AimReticle siege gates dropped, DebugOverlay/ops re-meant
(SpawnWave=force next wave, EndSiege=quiet arena; SetCalm/AdvanceGoal/SetHeat
retired, bytes reserved), TuningConfig Core knobs retired (ids 20-23 reserved),
StorageMath.DrainFraction deleted, HowToPlay copy rewritten.

Save epoch v7 (fresh epoch, operator-approved): SaveData drops goal/core/outcome +
conveyor/machine-IO fields; MinLoadableVersion=7; PendingSave/PendingStructure
trimmed; RollTerminalCampaignForward deleted; SaveStructureScan signature slimmed.

390 tests green; Play world-creation clean (player + waves live, no exceptions).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 15:27:12 -07:00

75 lines
3.2 KiB
C#

using System;
using System.IO;
using UnityEngine;
namespace ProjectM.Simulation
{
/// <summary>
/// Host-local persistence for the game save slice (<see cref="SaveData"/>) — single slot, versioned JSON at
/// <c>Application.persistentDataPath/save_0.json</c>, atomic writes (temp + <c>File.Replace</c>). Read by the
/// menu (to offer "Continue" + stage a <see cref="PendingSave"/>) and the server SaveWriteSystem (autosave).
/// JsonUtility keeps it dependency-free. Returns null on a missing / corrupt / version-mismatched file —
/// never throws to callers (a bad save degrades to "New Game", it never crashes boot).
/// </summary>
public static class SaveService
{
static string FilePath => Path.Combine(Application.persistentDataPath, "save_0.json");
public static bool HasSave() => File.Exists(FilePath);
public static SaveData Load()
{
try
{
if (!File.Exists(FilePath)) return null;
var data = JsonUtility.FromJson<SaveData>(File.ReadAllText(FilePath));
// EB-1: additive floor [MinLoadableVersion, CurrentVersion] so OLD v2 saves still load (a missing HP
// field 0-defaults and the restore guard maps 0 -> baked Max); v0/v1 garbage is still rejected.
if (data == null || data.Version < SaveData.MinLoadableVersion || data.Version > SaveData.CurrentVersion) return null;
data.Ledger ??= Array.Empty<LedgerRow>();
data.MetaUpgrades ??= Array.Empty<MetaUpgradeSave>(); // v6: null on any v<=5 file
return data;
}
catch (Exception e)
{
Debug.LogWarning($"[SaveService] Load failed ({e.Message}); treating as no save.");
return null;
}
}
/// <summary>
/// A TERMINAL save (Victory/Loss latched) Continues as a FRESH CAMPAIGN that keeps the base + permanent
/// meta: the outcome latch, goal meter, and core integrity reset to 0 (the spawn restore guards re-map
/// 0 -> InProgress / empty meter / baked-full Core), while meta tiers, run counters, the ledger, and
/// placed structures survive untouched. Without this, Continue/PLAY AGAIN after a win re-latches the dead
/// outcome banner on the first replicated snapshot. Pure + idempotent; no-op for in-progress saves.
/// </summary>
public static void Save(SaveData data)
{
if (data == null) return;
data.Version = SaveData.CurrentVersion;
try
{
var json = JsonUtility.ToJson(data, true);
var tmp = FilePath + ".tmp";
File.WriteAllText(tmp, json);
if (File.Exists(FilePath)) File.Replace(tmp, FilePath, null);
else File.Move(tmp, FilePath);
}
catch (Exception e)
{
Debug.LogWarning($"[SaveService] Save failed: {e.Message}");
}
}
public static void Delete()
{
try { if (File.Exists(FilePath)) File.Delete(FilePath); }
catch (Exception e) { Debug.LogWarning($"[SaveService] Delete failed: {e.Message}"); }
}
}
}