merge: sync and project position fixes (V-9, V-10, V-21, V-29, CD-2)
Merge branch fix2/sync-positions into the verification branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
32 files changed
+1843
-153
No files matched your search
@@ -36,7 +36,8 @@ internal static class ParameterBindings
|
||||
{
|
||||
Parameter = AdvisorParameters.Hero, Section = AdvisorSection.Frames,
|
||||
Effective = s => s.HeroFrame,
|
||||
Set = (s, r) => s.HeroFrame is null ? s with { HeroFrame = (int)r.Value! } : s,
|
||||
// A hero waiting for its photo (taken from another copy) is the user's choice too.
|
||||
Set = (s, r) => s.HeroFrame is null && !s.HeroPending ? s with { HeroFrame = (int)r.Value! } : s,
|
||||
},
|
||||
new()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
namespace Titano.Core.Project;
|
||||
|
||||
/// <summary>
|
||||
/// A photo as every copy of a sequence knows it (BRIEF §14.5: originals are content-addressed): its SHA-256 once hashed,
|
||||
/// else its size and capture time (a folder adopted and not verified yet). Frame ids and positions differ between a
|
||||
/// server sequence and a PC's copy of it; the photo does not.
|
||||
/// </summary>
|
||||
/// <param name="Sha256">Hex SHA-256; null or empty while the copy has not hashed the file.</param>
|
||||
public sealed record FramePhoto(string? Sha256, long Size, DateTime? CapturedAt)
|
||||
{
|
||||
public bool IsHashed => !string.IsNullOrEmpty(Sha256);
|
||||
|
||||
/// <summary>
|
||||
/// The same photo: the same SHA-256 when both are hashed, otherwise the same size and capture time (to the
|
||||
/// millisecond, as databases keep it).
|
||||
/// </summary>
|
||||
public bool Matches(FramePhoto other)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(other);
|
||||
if (IsHashed && other.IsHashed) return string.Equals(Sha256, other.Sha256, StringComparison.OrdinalIgnoreCase);
|
||||
return Size == other.Size && CapturedAt is { } mine && other.CapturedAt is { } theirs && Millisecond(mine) == Millisecond(theirs);
|
||||
}
|
||||
|
||||
internal static long Millisecond(DateTime time) => time.Ticks / TimeSpan.TicksPerMillisecond;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The photos of one copy's frames by frame index (positions in capture order, BRIEF §5), and where a photo is among
|
||||
/// them. Immutable once built, so parallel workers may share it.
|
||||
/// </summary>
|
||||
public sealed class FramePhotos
|
||||
{
|
||||
private readonly FramePhoto?[] _byIndex;
|
||||
private readonly Dictionary<string, int> _bySha = new(StringComparer.Ordinal);
|
||||
private readonly Dictionary<(long Size, long Time), List<int>> _bySizeAndTime = [];
|
||||
|
||||
/// <param name="frames">Each frame's index and photo, in any order.</param>
|
||||
public FramePhotos(IEnumerable<(int Index, FramePhoto Photo)> frames)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(frames);
|
||||
var list = frames.Where(f => f.Index >= 0).ToList();
|
||||
_byIndex = new FramePhoto?[list.Count == 0 ? 0 : list.Max(f => f.Index) + 1];
|
||||
foreach (var (index, photo) in list) _byIndex[index] = photo;
|
||||
for (var i = 0; i < _byIndex.Length; i++)
|
||||
{
|
||||
if (_byIndex[i] is not { } photo) continue;
|
||||
if (photo.IsHashed) _bySha.TryAdd(photo.Sha256!.ToLowerInvariant(), i);
|
||||
if (photo.CapturedAt is not { } time) continue;
|
||||
var key = (photo.Size, FramePhoto.Millisecond(time));
|
||||
if (!_bySizeAndTime.TryGetValue(key, out var same)) _bySizeAndTime[key] = same = [];
|
||||
same.Add(i);
|
||||
}
|
||||
}
|
||||
|
||||
public int Count => _byIndex.Length;
|
||||
|
||||
/// <summary>The photo at <paramref name="index"/>; null when no frame has that index.</summary>
|
||||
public FramePhoto? this[int index] => (uint)index < (uint)_byIndex.Length ? _byIndex[index] : null;
|
||||
|
||||
/// <summary>Some frame is not hashed yet: matching it needs capture times.</summary>
|
||||
public bool HasUnhashed => _byIndex.Any(p => p is { IsHashed: false });
|
||||
|
||||
/// <summary>The index of the first frame holding <paramref name="photo"/> (<see cref="FramePhoto.Matches"/>); null when none does.</summary>
|
||||
public int? IndexOf(FramePhoto photo)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(photo);
|
||||
if (photo.IsHashed && _bySha.TryGetValue(photo.Sha256!.ToLowerInvariant(), out var hashed)) return hashed;
|
||||
if (photo.CapturedAt is not { } time || !_bySizeAndTime.TryGetValue((photo.Size, FramePhoto.Millisecond(time)), out var candidates)) return null;
|
||||
foreach (var index in candidates)
|
||||
{
|
||||
if (_byIndex[index]!.Matches(photo)) return index;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Both lists hold the same photos at the same indices: a position names the same photo on both.</summary>
|
||||
public bool SameAs(FramePhotos other)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(other);
|
||||
if (other.Count != Count) return false;
|
||||
for (var i = 0; i < Count; i++)
|
||||
{
|
||||
var (mine, theirs) = (this[i], other[i]);
|
||||
if (mine is null != theirs is null || (mine is not null && !mine.Matches(theirs!))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -130,6 +130,7 @@ public sealed record ProjectBundle(BundleArchive Archive, BundleSequence Sequenc
|
||||
{
|
||||
ExcludedFrames = current.ExcludedFrames,
|
||||
HeroFrame = current.HeroFrame,
|
||||
PendingFrames = current.PendingFrames,
|
||||
Camera = Project.Camera with { Keyframes = current.Camera.Keyframes },
|
||||
};
|
||||
|
||||
|
||||
@@ -48,6 +48,30 @@ public sealed record CurvePoint(double X, double Y);
|
||||
/// <summary>Virtual-camera keyframe in normalised coordinates.</summary>
|
||||
public sealed record CameraKey(int Frame, double CenterX, double CenterY, double Zoom, double Rotation, string Easing = "EaseInOut");
|
||||
|
||||
/// <summary>
|
||||
/// The frame positions of a project that name a photo this copy of the sequence does not hold: settings taken from a
|
||||
/// copy that holds it (BRIEF §14.5). They become positions again when the photo arrives
|
||||
/// (<see cref="ProjectSettings.TranslateFramePositions"/>).
|
||||
/// </summary>
|
||||
public sealed record PendingFrame
|
||||
{
|
||||
public string? Sha256 { get; init; }
|
||||
public long Size { get; init; }
|
||||
public DateTime? CapturedAt { get; init; }
|
||||
|
||||
/// <summary>The photo is excluded from renders.</summary>
|
||||
public bool Excluded { get; init; }
|
||||
|
||||
/// <summary>The photo is the hero frame, unless another one is chosen before it arrives.</summary>
|
||||
public bool Hero { get; init; }
|
||||
|
||||
/// <summary>Virtual-camera keyframes on the photo; their <see cref="CameraKey.Frame"/> is set when it arrives.</summary>
|
||||
public IReadOnlyList<CameraKey> Keyframes { get; init; } = [];
|
||||
|
||||
[JsonIgnore]
|
||||
public FramePhoto Photo => new(Sha256, Size, CapturedAt);
|
||||
}
|
||||
|
||||
public sealed record TimingSettings
|
||||
{
|
||||
public Auto<TimingMode> Mode { get; init; } = new();
|
||||
@@ -177,6 +201,9 @@ public sealed record ProjectSettings
|
||||
/// <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 };
|
||||
|
||||
/// <summary>Member names as a project edited as JSON reads them (an older or hand-written file may use any casing).</summary>
|
||||
private static readonly JsonNodeOptions CaseInsensitive = new() { PropertyNameCaseInsensitive = true };
|
||||
|
||||
public int Version { get; init; } = CurrentVersion;
|
||||
public TimingSettings Timing { get; init; } = new();
|
||||
public ExposureSettings Exposure { get; init; } = new();
|
||||
@@ -191,6 +218,16 @@ public sealed record ProjectSettings
|
||||
/// <summary>Index of the hero frame (sequence thumbnail).</summary>
|
||||
public int? HeroFrame { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Exclusions, hero and keyframes naming photos this copy does not hold yet (settings synced from a copy that holds
|
||||
/// them); null when there are none. They take their place when the photos arrive (<see cref="TranslateFramePositions"/>).
|
||||
/// </summary>
|
||||
public IReadOnlyList<PendingFrame>? PendingFrames { get; init; }
|
||||
|
||||
/// <summary>The hero waits for its photo, so advice does not choose one meanwhile.</summary>
|
||||
[JsonIgnore]
|
||||
public bool HeroPending => PendingFrames?.Any(p => p.Hero) == true;
|
||||
|
||||
public string ToJson() => JsonSerializer.Serialize(this, ProjectJson.Default.ProjectSettings);
|
||||
|
||||
/// <summary>
|
||||
@@ -286,6 +323,256 @@ public sealed record ProjectSettings
|
||||
replace(JsonValue.Create(now));
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether a project names frames (exclusions, a hero, camera keyframes or <see cref="PendingFrames"/>); settings
|
||||
/// naming none cross between copies of a sequence as they are. False for text that is not a project object.
|
||||
/// </summary>
|
||||
public static bool NamesFrames(string? json)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(json)) return false;
|
||||
try
|
||||
{
|
||||
return JsonNode.Parse(json, CaseInsensitive) is JsonObject project
|
||||
&& (project["excludedFrames"] is JsonArray { Count: > 0 } || project["heroFrame"] is JsonValue || project["pendingFrames"] is JsonArray { Count: > 0 }
|
||||
|| (project["camera"] is JsonObject camera && camera["keyframes"] is JsonArray { Count: > 0 }));
|
||||
}
|
||||
catch (Exception ex) when (ex is JsonException or ArgumentException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Translates the frame positions of a project written against the frames of another copy of the sequence
|
||||
/// (<paramref name="from"/>; a server sequence and a PC's copy hold the same photos, though not always all of them,
|
||||
/// BRIEF §14.5) to the frames of <paramref name="to"/>, by photo: each exclusion, the hero and each camera keyframe
|
||||
/// moves to the frame of <paramref name="to"/> holding its photo. One whose photo <paramref name="to"/> lacks becomes
|
||||
/// a <see cref="PendingFrame"/>, and pending frames whose photo <paramref name="to"/> holds become positions again; a
|
||||
/// pending hero only while no other hero is chosen (a hero chosen since one was left pending is newer, so that one no
|
||||
/// longer waits even when the chosen one must wait in turn), a pending keyframe only where no keyframe is. With
|
||||
/// <paramref name="from"/> null the positions already name <paramref name="to"/>'s frames and only pending frames are
|
||||
/// placed (photos that arrived). A position naming no frame of <paramref name="from"/> names nothing and goes. Edited
|
||||
/// in place like <see cref="MoveFramePositions"/>; null when nothing changes or <paramref name="json"/> is not a
|
||||
/// project object.
|
||||
/// </summary>
|
||||
public static string? TranslateFramePositions(string json, FramePhotos? from, FramePhotos to)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(to);
|
||||
if (string.IsNullOrWhiteSpace(json)) return null;
|
||||
try
|
||||
{
|
||||
if (JsonNode.Parse(json, CaseInsensitive) is not JsonObject project) return null;
|
||||
var pending = new List<Parked>();
|
||||
var unreadable = new List<JsonNode?>();
|
||||
if (project["pendingFrames"] is JsonArray stored)
|
||||
{
|
||||
foreach (var node in stored)
|
||||
{
|
||||
// An entry naming no photo is not this Titano's to place: it stays as it is.
|
||||
if (node is JsonObject entry && PhotoOf(entry) is { } photo) pending.Add(new Parked(photo, entry.DeepClone().AsObject()));
|
||||
else unreadable.Add(node?.DeepClone());
|
||||
}
|
||||
}
|
||||
var changed = false;
|
||||
if (project["heroFrame"] is JsonValue && pending.Any(p => Flag(p.Entry, "hero")))
|
||||
{
|
||||
// Checked before the hero itself may be parked below, which leaves no hero chosen.
|
||||
foreach (var parked in pending) parked.Entry["hero"] = false;
|
||||
pending.RemoveAll(p => !Waits(p.Entry));
|
||||
changed = true;
|
||||
}
|
||||
changed |= from is not null && !from.SameAs(to) && TranslatePositions(project, from, to, pending);
|
||||
changed |= PlacePending(project, to, pending);
|
||||
if (!changed) return null;
|
||||
SortPositions(project);
|
||||
var left = pending.Where(p => !p.Placed).Select(p => (JsonNode?)p.Entry).Concat(unreadable).ToArray();
|
||||
if (left.Length == 0) project.Remove("pendingFrames");
|
||||
else project["pendingFrames"] = new JsonArray(left);
|
||||
return project.ToJsonString(IndentedJson);
|
||||
}
|
||||
catch (Exception ex) when (ex is JsonException or ArgumentException or InvalidOperationException)
|
||||
{
|
||||
// Not JSON, a member given twice, or a member of an unexpected kind: not a project to edit.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Moves every position of <paramref name="project"/> from <paramref name="from"/>'s frames to <paramref name="to"/>'s.</summary>
|
||||
private static bool TranslatePositions(JsonObject project, FramePhotos from, FramePhotos to, List<Parked> pending)
|
||||
{
|
||||
var changed = false;
|
||||
if (project["excludedFrames"] is JsonArray excluded)
|
||||
{
|
||||
var kept = new List<JsonNode?>(excluded.Count);
|
||||
foreach (var node in excluded)
|
||||
{
|
||||
if (!IsPosition(node, out var at)) kept.Add(node?.DeepClone());
|
||||
else if (from[at] is not { } photo) changed = true;
|
||||
else if (to.IndexOf(photo) is { } now)
|
||||
{
|
||||
kept.Add(JsonValue.Create(now));
|
||||
changed |= now != at;
|
||||
}
|
||||
else
|
||||
{
|
||||
Park(pending, photo).Entry["excluded"] = true;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
Replace(excluded, kept);
|
||||
}
|
||||
if (project["heroFrame"] is JsonValue hero && hero.TryGetValue<int>(out var heroAt))
|
||||
{
|
||||
var photo = from[heroAt];
|
||||
var now = photo is null ? null : to.IndexOf(photo);
|
||||
if (photo is not null && now is null) Park(pending, photo).Entry["hero"] = true;
|
||||
if (now != heroAt)
|
||||
{
|
||||
project["heroFrame"] = now;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (project["camera"] is JsonObject camera && camera["keyframes"] is JsonArray keyframes)
|
||||
{
|
||||
var kept = new List<JsonNode?>(keyframes.Count);
|
||||
foreach (var node in keyframes)
|
||||
{
|
||||
if (node is not JsonObject key || !IsPosition(key["frame"], out var at))
|
||||
{
|
||||
kept.Add(node?.DeepClone());
|
||||
continue;
|
||||
}
|
||||
var copy = key.DeepClone().AsObject();
|
||||
if (from[at] is not { } photo)
|
||||
{
|
||||
changed = true;
|
||||
}
|
||||
else if (to.IndexOf(photo) is { } now)
|
||||
{
|
||||
copy["frame"] = now;
|
||||
kept.Add(copy);
|
||||
changed |= now != at;
|
||||
}
|
||||
else
|
||||
{
|
||||
var parked = Park(pending, photo).Entry;
|
||||
if (parked["keyframes"] is not JsonArray keys) parked["keyframes"] = keys = new JsonArray();
|
||||
keys.Add(copy);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
Replace(keyframes, kept);
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
/// <summary>Gives the pending frames whose photo <paramref name="to"/> holds their positions back.</summary>
|
||||
private static bool PlacePending(JsonObject project, FramePhotos to, List<Parked> pending)
|
||||
{
|
||||
var changed = false;
|
||||
foreach (var parked in pending)
|
||||
{
|
||||
if (to.IndexOf(parked.Photo) is not { } at) continue;
|
||||
parked.Placed = changed = true;
|
||||
if (Flag(parked.Entry, "excluded"))
|
||||
{
|
||||
if (project["excludedFrames"] is not JsonArray positions) project["excludedFrames"] = positions = new JsonArray();
|
||||
if (!positions.Any(n => IsPosition(n, out var p) && p == at)) positions.Add(at);
|
||||
}
|
||||
// A hero chosen since the settings were taken is newer than the one that waited.
|
||||
if (Flag(parked.Entry, "hero") && project["heroFrame"] is null) project["heroFrame"] = at;
|
||||
if (parked.Entry["keyframes"] is JsonArray { Count: > 0 } keys)
|
||||
{
|
||||
if (project["camera"] is not JsonObject camera) project["camera"] = camera = new JsonObject();
|
||||
if (camera["keyframes"] is not JsonArray keyframes) camera["keyframes"] = keyframes = new JsonArray();
|
||||
foreach (var key in keys.OfType<JsonObject>())
|
||||
{
|
||||
// A keyframe set on that frame since is newer.
|
||||
if (keyframes.Any(n => n is JsonObject k && IsPosition(k["frame"], out var f) && f == at)) continue;
|
||||
var copy = key.DeepClone().AsObject();
|
||||
copy["frame"] = at;
|
||||
keyframes.Add(copy);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (project["heroFrame"] is null) return changed;
|
||||
// A hero is chosen: none waits any more, and an entry left with nothing to place goes.
|
||||
foreach (var parked in pending.Where(p => !p.Placed && Flag(p.Entry, "hero")))
|
||||
{
|
||||
parked.Entry["hero"] = false;
|
||||
parked.Placed = !Waits(parked.Entry);
|
||||
changed = true;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
/// <summary>Exclusions in ascending order without repeats and keyframes by frame (values that are no position last, as they were).</summary>
|
||||
private static void SortPositions(JsonObject project)
|
||||
{
|
||||
if (project["excludedFrames"] is JsonArray excluded)
|
||||
{
|
||||
var positions = excluded.Select(n => IsPosition(n, out var p) ? p : (int?)null).OfType<int>().Distinct().Order().Select(p => (JsonNode?)JsonValue.Create(p));
|
||||
Replace(excluded, [.. positions, .. excluded.Where(n => !IsPosition(n, out _)).Select(n => n?.DeepClone())]);
|
||||
}
|
||||
if (project["camera"] is JsonObject camera && camera["keyframes"] is JsonArray keyframes)
|
||||
{
|
||||
var sorted = keyframes.Select(n => n?.DeepClone())
|
||||
.OrderBy(n => n is JsonObject key && IsPosition(key["frame"], out var f) ? f : int.MaxValue).ToList();
|
||||
Replace(keyframes, sorted);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The pending entry of <paramref name="photo"/>, added when there is none.</summary>
|
||||
private static Parked Park(List<Parked> pending, FramePhoto photo)
|
||||
{
|
||||
if (pending.FirstOrDefault(p => p.Photo.Matches(photo)) is { } known) return known;
|
||||
var entry = new JsonObject();
|
||||
if (photo.IsHashed) entry["sha256"] = photo.Sha256!.ToLowerInvariant();
|
||||
entry["size"] = photo.Size;
|
||||
if (photo.CapturedAt is { } captured) entry["capturedAt"] = captured;
|
||||
entry["excluded"] = false;
|
||||
entry["hero"] = false;
|
||||
entry["keyframes"] = new JsonArray();
|
||||
var parked = new Parked(photo, entry);
|
||||
pending.Add(parked);
|
||||
return parked;
|
||||
}
|
||||
|
||||
/// <summary>The photo a pending entry names; null when it names none.</summary>
|
||||
private static FramePhoto? PhotoOf(JsonObject entry)
|
||||
{
|
||||
var sha = entry["sha256"] is JsonValue s && s.TryGetValue<string>(out var text) ? text : null;
|
||||
var size = entry["size"] is JsonValue z && z.TryGetValue<long>(out var bytes) ? bytes : -1;
|
||||
DateTime? captured = entry["capturedAt"] is JsonValue t && t.TryGetValue<DateTime>(out var time) ? time : null;
|
||||
return string.IsNullOrEmpty(sha) && (captured is null || size < 0) ? null : new FramePhoto(sha, size, captured);
|
||||
}
|
||||
|
||||
private static bool Flag(JsonObject entry, string name) => entry[name] is JsonValue value && value.TryGetValue<bool>(out var set) && set;
|
||||
|
||||
/// <summary>A pending entry still has a position to give its photo.</summary>
|
||||
private static bool Waits(JsonObject entry) => Flag(entry, "excluded") || Flag(entry, "hero") || entry["keyframes"] is JsonArray { Count: > 0 };
|
||||
|
||||
private static bool IsPosition(JsonNode? node, out int position)
|
||||
{
|
||||
position = 0;
|
||||
return node is JsonValue value && value.TryGetValue(out position);
|
||||
}
|
||||
|
||||
private static void Replace(JsonArray array, IEnumerable<JsonNode?> nodes)
|
||||
{
|
||||
var copy = nodes.ToList();
|
||||
array.Clear();
|
||||
foreach (var node in copy) array.Add(node);
|
||||
}
|
||||
|
||||
/// <summary>A pending entry being edited: its photo, its JSON, and whether its photo took its place.</summary>
|
||||
private sealed class Parked(FramePhoto photo, JsonObject entry)
|
||||
{
|
||||
public FramePhoto Photo { get; } = photo;
|
||||
public JsonObject Entry { get; } = entry;
|
||||
public bool Placed { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(
|
||||
|
||||
Reference in new issue
Block a user