fix(archive): keep project frame positions on their frames after inserts
The project names frames by position (exclusions, hero, virtual-camera keyframes), and an upload, import, scan or sync download of earlier photos merges them by capture time and renumbers the frames after them; nothing moved the project, so a render excluded the wrong photo and the project's hero no longer matched the Library's. project.json keeps its format: when the index takes a sidecar in which frames were inserted before indexed ones, the index's copy of the project has its positions moved from the indexed order to the new one in the same transaction as the frame rows, and project.json mirrors it unless another writer changed it meanwhile. The JSON is edited in place, so members a newer Titano wrote survive; only an insertion is followed (if an indexed frame is gone, the positions stay). The settings version stays, so a synced copy receiving the same frames makes the same move (CD-2). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
7 files changed
+252
-3
No files matched your search
@@ -174,6 +174,9 @@ public sealed record ProjectSettings
|
||||
{
|
||||
public const int CurrentVersion = 1;
|
||||
|
||||
/// <summary>Layout of a project edited as JSON (<see cref="MoveFramePositions"/>): indented like <see cref="ToJson"/>.</summary>
|
||||
private static readonly JsonSerializerOptions IndentedJson = new() { WriteIndented = true };
|
||||
|
||||
public int Version { get; init; } = CurrentVersion;
|
||||
public TimingSettings Timing { get; init; } = new();
|
||||
public ExposureSettings Exposure { get; init; } = new();
|
||||
@@ -182,7 +185,7 @@ public sealed record ProjectSettings
|
||||
public StackSettings Stack { get; init; } = new();
|
||||
public ExportSettings Export { get; init; } = new();
|
||||
|
||||
/// <summary>Frame indices excluded from renders.</summary>
|
||||
/// <summary>Frame indices excluded from renders (positions in capture order; see <see cref="MoveFramePositions"/>).</summary>
|
||||
public IReadOnlyList<int> ExcludedFrames { get; init; } = [];
|
||||
|
||||
/// <summary>Index of the hero frame (sequence thumbnail).</summary>
|
||||
@@ -237,6 +240,52 @@ public sealed record ProjectSettings
|
||||
target[name] = value?.DeepClone();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves the frame positions of a project — <see cref="ExcludedFrames"/>, <see cref="HeroFrame"/> and the
|
||||
/// virtual-camera <see cref="CameraKey.Frame"/>s — after frames were inserted before them: <paramref name="moves"/> maps
|
||||
/// an old position to the new one, and a position it does not list stays. The JSON is edited in place, so members
|
||||
/// this Titano does not know survive. Null when nothing moves or <paramref name="json"/> is not a project object.
|
||||
/// </summary>
|
||||
public static string? MoveFramePositions(string json, IReadOnlyDictionary<int, int> moves)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(moves);
|
||||
if (moves.Count == 0 || string.IsNullOrWhiteSpace(json)) return null;
|
||||
try
|
||||
{
|
||||
if (JsonNode.Parse(json, new JsonNodeOptions { PropertyNameCaseInsensitive = true }) is not JsonObject project) return null;
|
||||
var moved = false;
|
||||
if (project["excludedFrames"] is JsonArray excluded)
|
||||
{
|
||||
for (var i = 0; i < excluded.Count; i++)
|
||||
{
|
||||
var at = i;
|
||||
moved |= Move(excluded[at], value => excluded[at] = value, moves);
|
||||
}
|
||||
}
|
||||
if (project["heroFrame"] is { } hero) moved |= Move(hero, value => project["heroFrame"] = value, moves);
|
||||
if (project["camera"] is JsonObject camera && camera["keyframes"] is JsonArray keyframes)
|
||||
{
|
||||
foreach (var key in keyframes.OfType<JsonObject>())
|
||||
{
|
||||
if (key["frame"] is { } frame) moved |= Move(frame, value => key["frame"] = value, moves);
|
||||
}
|
||||
}
|
||||
return moved ? project.ToJsonString(IndentedJson) : null;
|
||||
}
|
||||
catch (Exception ex) when (ex is JsonException or ArgumentException)
|
||||
{
|
||||
// Not JSON, or a member given twice (the case-insensitive member table refuses it): not a project to edit.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool Move(JsonNode? position, Action<JsonNode> replace, IReadOnlyDictionary<int, int> moves)
|
||||
{
|
||||
if (position is not JsonValue value || !value.TryGetValue<int>(out var old) || !moves.TryGetValue(old, out var now) || now == old) return false;
|
||||
replace(JsonValue.Create(now));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(
|
||||
|
||||
Reference in new issue
Block a user