f31ffe910b
Netcode frontend pattern: UITK main menu / pause / settings (MenuUi + controllers), on-demand world lifecycle (WorldLauncher/SessionRunner), GameBootstrap menu branch; Graphics/Audio settings (SettingsService/GameVolume); single-slot save foundation (SaveData/SaveService, born-correct load at director spawn, autosave on Siege->Calm + quit); RuntimePanelSettings + theme; BuildTool menu; 10 EditMode tests. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
62 lines
2.3 KiB
C#
62 lines
2.3 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));
|
|
if (data == null || 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}"); }
|
|
}
|
|
}
|
|
}
|