feat(progression): json stores on a real filesystem (slice 8)

new progression.adapters project, no engine reference. Stores take a
directory path; Godot resolves user:// with GlobalizePath and passes it
in, which is what makes them testable at all - originally I'd written
this slice off as untestable.

version lives in a file envelope, not the snapshot, so the domain only
knows the current shape. A newer-build save reports Corrupt with a
reason instead of masquerading as "no save" and getting overwritten.

atomic write via temp + rename. One file per aggregate: saving a run
leaves the profile file byte-identical, asserted.

guard rail fixed - it was matching prose in doc comments, now anchored
to using-directives plus a project.assets.json check. 63 tests.
This commit is contained in:
2026-07-28 19:35:43 +02:00
parent f991bb99fc
commit 7a69292066
13 changed files with 385 additions and 4 deletions

View File

@@ -0,0 +1,30 @@
namespace Mr.BrickAdventures.Progression.Adapters;
/// <summary>
/// Keeps each Playthrough in its own JSON file inside a directory.
/// </summary>
/// <remarks>
/// Takes a directory path rather than using Godot's FileAccess. Godot resolves <c>user://</c> to an
/// absolute path with <c>ProjectSettings.GlobalizePath</c> and passes it in, so this whole class has
/// no engine dependency and can be tested against a real temp directory.
/// </remarks>
public sealed class JsonPlaythroughStore : IPlaythroughStore
{
/// <summary>Bump only alongside a migration for the version being replaced.</summary>
private const int CurrentVersion = 1;
private readonly string _directory;
public JsonPlaythroughStore(string directory) => _directory = directory;
/// <summary>One file per slot, so saving a run never rewrites the Profile or another slot.</summary>
private string PathFor(PlaythroughId id) =>
Path.Combine(_directory, $"playthrough_{id.Value:N}.json");
public void Save(Playthrough playthrough) =>
JsonFiles.WriteAtomic(PathFor(playthrough.Id), CurrentVersion, playthrough.Export());
public LoadResult<Playthrough> Load(PlaythroughId id) =>
JsonFiles.Read<PlaythroughSnapshot, Playthrough>(
PathFor(id), CurrentVersion, Playthrough.Restore);
}