Files
Project-M/Assets/_Project/Scripts/Simulation/Persistence/SaveService.cs
T
kronic 73cfe2943d EB-1: machines can die - structures get HP, Husks raze them, wounded base persists
Structures (Turret/Wall/Pylon) reuse the combat spine: authoring bakes Health(GhostField)+DamageEvent buffer+a Destructible tag (no HitRadius -> no friendly projectile fire; no EffectiveCharacterStats -> clamp-to-0). HealthApplyDamageSystem destroys a Destructible at 0 (occupancy auto-frees). EnemyAISystem fortress-targets the weighted-nearest of players+structures via the shared EnemyAIMath.PickWeightedNearest (StructureAggroWeight TuningConfig knob, <1 prefers structures, squared factor; snapshot above the early-return so an undefended base is razed). Persistence v3: per-structure HP threaded through 5 sites (SaveData/PendingStructure/scan-guarded/BaseRestore same-ECB born-correct/WorldLauncher via SaveApply.ToPending); SaveService floor-gate [2,3] loads old saves. Loss feedback: proximity-gated StructureFeedbackSystem; CombatFeedbackSystem suppressed for structures. Pre-code review caught the DamageEvent-buffer crash blocker + 8 majors; post-code review clean. See DR-032.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-11 23:53:34 -07:00

64 lines
2.5 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>();
return data;
}
catch (Exception e)
{
Debug.LogWarning($"[SaveService] Load failed ({e.Message}); treating as no save.");
return null;
}
}
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}"); }
}
}
}