fix(projects): one photo merges as one, newer settings are never merged
A workspace copied before the home hashed or converted a photo named it differently from the versions the home made since, so a pull merged the same photo as two: frame edits were silently undone and Keep mine placed the library's value. Before replaying, the workspace media now learn the home's hashes (same frame id, size and capture time) and conversion aliases, so every side names each photo alike. Rebind also gives a renamed entry the occurrence a placement looks for, so positions on burst photos keep applying once hashed, and folds entries of one photo (in Rebind and in the merge's keyed form) instead of keeping one. Settings of a newer Chronos are no longer merged member by member with this one's: ProjectMerge refuses when both sides changed. A workspace pull keeps its local versions and working state untouched and records a conflict of format that only Save both settles (into a new project of this format); with nothing local it takes the newer head read-only. Main's catch-up leaves a newer head and settings written meanwhile as they are. When Main's catch-up merge overrides values another writer stored, that writer's settings are kept as an External version before the Merge one, so nothing is lost from the history. Unknown members inside Auto values, camera keyframes and curve points, and on pending entries once placed, are kept like ProjectSettings' Extra. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
50e28fd63d
commit
eac4a36a7b
12 files changed
+905
-38
No files matched your search
@@ -1,3 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Chronos.Core.Project;
|
||||
|
||||
/// <summary>
|
||||
@@ -24,6 +27,13 @@ public sealed record Auto<T>
|
||||
|
||||
public IReadOnlyList<string>? ReasonArgs { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Members this Chronos does not know (written by a newer one, such as the advisor's confidence): kept as they are, so
|
||||
/// they survive a round trip and every edit of the parameter (owner review 2, R8).
|
||||
/// </summary>
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
|
||||
/// <summary>The value in force: manual when set by the user, otherwise the advisor's, otherwise <paramref name="fallback"/>.</summary>
|
||||
public T Resolve(T fallback) => !IsAuto && Manual is not null ? Manual : HasAdvice && AutoValue is not null ? AutoValue : fallback;
|
||||
|
||||
|
||||
@@ -55,7 +55,8 @@ public sealed class FramePhotos
|
||||
public FramePhotos(IEnumerable<(int Index, FramePhoto Photo)> frames, IEnumerable<PhotoAlias>? aliases)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(frames);
|
||||
_canonical = PhotoAlias.Classes(aliases);
|
||||
Aliases = aliases is null ? [] : [.. aliases.Where(a => a is not null)];
|
||||
_canonical = PhotoAlias.Classes(Aliases);
|
||||
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;
|
||||
@@ -82,6 +83,9 @@ public sealed class FramePhotos
|
||||
|
||||
public int Count => _byIndex.Length;
|
||||
|
||||
/// <summary>The links between encodings of one photo these frames were given (empty without any): another copy's frames can take them.</summary>
|
||||
public IReadOnlyList<PhotoAlias> Aliases { get; }
|
||||
|
||||
/// <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;
|
||||
|
||||
|
||||
@@ -57,9 +57,12 @@ public static class ProjectContent
|
||||
/// <summary>
|
||||
/// The portable text with each pending frame whose photo <paramref name="frames"/> hold named as they hold it now: an
|
||||
/// encoding a conversion replaced by the one the frame holds (<see cref="PhotoAlias"/>, also its undo), a photo parked
|
||||
/// before it was hashed by its SHA-256, size and capture time. The same choices on the same photos then read as the same
|
||||
/// text (<see cref="SameChoices"/>), whichever encoding or hashing state they were saved against; a placement finds the
|
||||
/// same frames either way. Canonical when anything is renamed; the text as it is otherwise, and for a newer format.
|
||||
/// before it was hashed by its SHA-256, size and capture time, and which copy of that photo it is among the frames
|
||||
/// holding it under that name (a photo of a burst told apart by its order while unhashed is unique once hashed). Entries
|
||||
/// that name one photo once renamed become one (<see cref="Fold"/>). The same choices on the same photos then read as
|
||||
/// the same text (<see cref="SameChoices"/>), whichever encoding or hashing state they were saved against; a placement
|
||||
/// finds the same frames either way. Canonical when anything is renamed or folded; the text as it is otherwise, and for
|
||||
/// a newer format.
|
||||
/// </summary>
|
||||
/// <exception cref="JsonException">The text is not a project object.</exception>
|
||||
public static string Rebind(string portableJson, FramePhotos frames)
|
||||
@@ -80,19 +83,90 @@ public static class ProjectContent
|
||||
var occurrence = entry["occurrence"] is JsonValue o && o.TryGetValue<int>(out var n) && n > 0 ? n : 0;
|
||||
// Only a frame that is hashed names a photo better than the entry does.
|
||||
if (frames.IndexOf(new FramePhoto(sha, size, captured), occurrence) is not { } at || frames[at] is not { IsHashed: true } held) continue;
|
||||
// The copy a placement looks for under the new name: among the frames holding that SHA-256, not those of that size and time.
|
||||
var copy = frames.Occurrence(at);
|
||||
var sameTime = held.CapturedAt is { } heldAt
|
||||
? captured is { } entryAt && FramePhoto.Millisecond(heldAt) == FramePhoto.Millisecond(entryAt)
|
||||
: captured is null;
|
||||
if (string.Equals(held.Sha256, sha, StringComparison.OrdinalIgnoreCase) && held.Size == size && sameTime) continue;
|
||||
if (string.Equals(held.Sha256, sha, StringComparison.OrdinalIgnoreCase) && held.Size == size && sameTime && copy == occurrence) continue;
|
||||
entry["sha256"] = held.Sha256!.ToLowerInvariant();
|
||||
entry["size"] = held.Size;
|
||||
if (held.CapturedAt is { } heldTime) entry["capturedAt"] = heldTime;
|
||||
else entry.Remove("capturedAt");
|
||||
if (copy > 0) entry["occurrence"] = copy;
|
||||
else entry.Remove("occurrence");
|
||||
renamed = true;
|
||||
}
|
||||
renamed |= FoldAlike(pending);
|
||||
return renamed ? Canonical(project.ToJsonString()) : portableJson;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The photo a pending entry names and which copy of it: "sha:<sha-256>#<occurrence>", or
|
||||
/// "photo:<size>@<capture ticks>#<occurrence>" for a photo not hashed; null for an entry naming no photo.
|
||||
/// Entries of one key are one photo's positions (<see cref="Fold"/>).
|
||||
/// </summary>
|
||||
public static string? PhotoKey(JsonNode? entry)
|
||||
{
|
||||
if (entry is not JsonObject e) return null;
|
||||
var occurrence = e["occurrence"] is JsonValue o && o.TryGetValue<int>(out var n) ? n : 0;
|
||||
if (e["sha256"] is JsonValue s && s.TryGetValue<string>(out var sha) && sha.Length > 0)
|
||||
return string.Create(CultureInfo.InvariantCulture, $"sha:{sha.ToLowerInvariant()}#{occurrence}");
|
||||
var size = e["size"] is JsonValue z && z.TryGetValue<long>(out var bytes) ? bytes : 0;
|
||||
return e["capturedAt"] is JsonValue c && c.TryGetValue<DateTime>(out var at)
|
||||
? string.Create(CultureInfo.InvariantCulture, $"photo:{size}@{at.Ticks}#{occurrence}")
|
||||
: null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Folds <paramref name="entry"/> into <paramref name="into"/>, two pending entries of one photo (saved under two names,
|
||||
/// alike since): excluded or hero when either is, the keyframes of both (each once), and the members this Chronos does not
|
||||
/// know of both (those of <paramref name="into"/> where both have one). Nothing either held is dropped.
|
||||
/// </summary>
|
||||
public static void Fold(JsonObject into, JsonObject entry)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(into);
|
||||
ArgumentNullException.ThrowIfNull(entry);
|
||||
if (Flag(entry, "excluded")) into["excluded"] = true;
|
||||
if (Flag(entry, "hero")) into["hero"] = true;
|
||||
if (entry["keyframes"] is JsonArray { Count: > 0 } keys)
|
||||
{
|
||||
if (into["keyframes"] is not JsonArray mine) into["keyframes"] = mine = new JsonArray();
|
||||
foreach (var key in keys.ToList())
|
||||
{
|
||||
if (!mine.Any(k => JsonNode.DeepEquals(k, key))) mine.Add(key?.DeepClone());
|
||||
}
|
||||
}
|
||||
foreach (var (name, value) in entry.ToList())
|
||||
{
|
||||
if (!ProjectSettings.PendingMembers.Contains(name) && !into.ContainsKey(name)) into[name] = value?.DeepClone();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Folds the pending entries naming one photo into the first of them (<see cref="Fold"/>); whether any were.</summary>
|
||||
private static bool FoldAlike(JsonArray pending)
|
||||
{
|
||||
var first = new Dictionary<string, JsonObject>(StringComparer.Ordinal);
|
||||
var folded = new List<JsonNode>();
|
||||
foreach (var entry in pending.OfType<JsonObject>())
|
||||
{
|
||||
if (PhotoKey(entry) is not { } key) continue;
|
||||
if (first.TryGetValue(key, out var into))
|
||||
{
|
||||
Fold(into, entry);
|
||||
folded.Add(entry);
|
||||
}
|
||||
else
|
||||
{
|
||||
first[key] = entry;
|
||||
}
|
||||
}
|
||||
foreach (var entry in folded) pending.Remove(entry);
|
||||
return folded.Count > 0;
|
||||
}
|
||||
|
||||
private static bool Flag(JsonObject entry, string name) => entry[name] is JsonValue value && value.TryGetValue<bool>(out var set) && set;
|
||||
|
||||
/// <summary>
|
||||
/// Whether two portable texts hold the same choices on the frames of <paramref name="frames"/>: the same text, or the
|
||||
/// same once their pending frames are named as the frames hold their photos now (<see cref="Rebind"/>).
|
||||
|
||||
@@ -44,10 +44,20 @@ public enum StackSetting
|
||||
}
|
||||
|
||||
/// <summary>A control point of the time-remap curve: output fraction → source fraction (both 0..1).</summary>
|
||||
public sealed record CurvePoint(double X, double Y);
|
||||
public sealed record CurvePoint(double X, double Y)
|
||||
{
|
||||
/// <summary>Members this Chronos does not know (written by a newer one): kept as they are, so they survive a round trip.</summary>
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
}
|
||||
|
||||
/// <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");
|
||||
public sealed record CameraKey(int Frame, double CenterX, double CenterY, double Zoom, double Rotation, string Easing = "EaseInOut")
|
||||
{
|
||||
/// <summary>Members this Chronos does not know (written by a newer one): kept as they are, so they survive a round trip.</summary>
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The frame positions of a project that name a photo this copy of the sequence does not hold: settings taken from a
|
||||
@@ -564,14 +574,21 @@ public sealed record ProjectSettings
|
||||
return changed;
|
||||
}
|
||||
|
||||
/// <summary>Gives the pending frames whose photo <paramref name="to"/> holds their positions back.</summary>
|
||||
/// <summary>
|
||||
/// Gives the pending frames whose photo <paramref name="to"/> holds their positions back. An entry with members this
|
||||
/// Chronos does not know (a newer one's values of the photo) stays, without its positions: those members are not this
|
||||
/// Chronos's to drop, and the next portable form parks the positions in it again.
|
||||
/// </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, parked.Occurrence) is not { } at) continue;
|
||||
parked.Placed = changed = true;
|
||||
var kept = HasUnknownMembers(parked.Entry);
|
||||
if (kept && !HoldsPosition(parked.Entry)) continue;
|
||||
parked.Placed = !kept;
|
||||
changed = true;
|
||||
if (Flag(parked.Entry, "excluded"))
|
||||
{
|
||||
if (project["excludedFrames"] is not JsonArray positions) project["excludedFrames"] = positions = new JsonArray();
|
||||
@@ -592,6 +609,12 @@ public sealed record ProjectSettings
|
||||
keyframes.Add(copy);
|
||||
}
|
||||
}
|
||||
if (kept)
|
||||
{
|
||||
parked.Entry["excluded"] = false;
|
||||
parked.Entry["hero"] = false;
|
||||
parked.Entry["keyframes"] = new JsonArray();
|
||||
}
|
||||
}
|
||||
if (project["heroFrame"] is null) return changed;
|
||||
// A hero is chosen: none waits any more, and an entry left with nothing to place goes.
|
||||
@@ -651,8 +674,20 @@ public sealed record ProjectSettings
|
||||
|
||||
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 };
|
||||
/// <summary>The members of a pending entry this Chronos knows: its photo, which copy of it, and the positions on it.</summary>
|
||||
internal static readonly HashSet<string> PendingMembers = new(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
"sha256", "size", "capturedAt", "occurrence", "excluded", "hero", "keyframes",
|
||||
};
|
||||
|
||||
/// <summary>A pending entry has a position to give its photo.</summary>
|
||||
private static bool HoldsPosition(JsonObject entry) => Flag(entry, "excluded") || Flag(entry, "hero") || entry["keyframes"] is JsonArray { Count: > 0 };
|
||||
|
||||
/// <summary>A pending entry holds members this Chronos does not know (written by a newer one).</summary>
|
||||
private static bool HasUnknownMembers(JsonObject entry) => entry.Any(p => !PendingMembers.Contains(p.Key));
|
||||
|
||||
/// <summary>A pending entry still holds something: a position to give its photo, or members that are not this Chronos's to drop.</summary>
|
||||
private static bool Waits(JsonObject entry) => HoldsPosition(entry) || HasUnknownMembers(entry);
|
||||
|
||||
private static bool IsPosition(JsonNode? node, out int position)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user