Skip to content

Commit ec7f77f

Browse files
committed
serialization: (minor thing) sort labels before serializing
- this makes the output XML easier to deal with when merging in git - before, it would just add the latest labels towards the bottom, this keeps it sorted by SNES address - doesn't affect the save data ver#, just the output - re-saving with this will re-order labels in existing saves
1 parent 57a2a22 commit ec7f77f

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

Diz.Core.Interfaces/LabelInterfaces.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public interface ILabelProvider : IReadOnlyLabelProvider
9595

9696
void SetAll(Dictionary<int, IAnnotationLabel> newLabels);
9797
void AppendLabels(Dictionary<int, IAnnotationLabel> newLabels, bool smartMerge = false);
98+
99+
void SortLabels();
98100
}
99101

100102
public interface IReadOnlyLabels

Diz.Core.Interfaces/ModelInterfaces.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ public interface ILabelService :
175175
ILabelProvider,
176176
IReadOnlyLabelProvider
177177
{
178-
179178
}
180179

181180
public interface ILabelServiceWithTempLabels :

Diz.Core/model/LabelProvider.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ public override int GetHashCode()
267267
return !Equals(left, right);
268268
}
269269
#endregion
270+
271+
public void SortLabels()
272+
{
273+
if (cachedLabels != null)
274+
throw new InvalidOperationException("Cannot modify labels while cache is locked");
275+
276+
NormalProvider.SortLabels();
277+
}
270278
}
271279

272280
#if DIZ_3_BRANCH
@@ -354,7 +362,7 @@ public void AddLabel(int snesAddress, IAnnotationLabel labelToAdd, bool overwrit
354362
public class LabelsCollection : LabelProviderBase, ILabelService, IEquatable<LabelsCollection>
355363
{
356364
// ReSharper disable once MemberCanBePrivate.Global
357-
public Dictionary<int, IAnnotationLabel> Labels { get; } = new();
365+
public Dictionary<int, IAnnotationLabel> Labels { get; private set; } = new();
358366

359367
[XmlIgnore]
360368
IEnumerable<KeyValuePair<int, IAnnotationLabel>> IReadOnlyLabelProvider.Labels => Labels;
@@ -415,6 +423,13 @@ public void AppendLabels(Dictionary<int, IAnnotationLabel> newLabels, bool smart
415423
}
416424
}
417425
}
426+
427+
public void SortLabels()
428+
{
429+
Labels = Labels
430+
.OrderBy(x => x.Key)
431+
.ToDictionary(x => x.Key, x => x.Value);
432+
}
418433

419434
public override IAnnotationLabel GetLabel(int snesAddress) =>
420435
Labels.GetValueOrDefault(snesAddress);

Diz.Core/serialization/xml_serializer/ProjectXMLSerializer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public override byte[] Save(Project project)
118118
Watermark = DizWatermark,
119119
Project = project
120120
};
121+
122+
// one silly thing. we'll re-sort labels so they show up in the output file in order.
123+
project.Data.Labels.SortLabels();
121124

122125
BeforeSerialize?.Invoke(this, rootElement);
123126

0 commit comments

Comments
 (0)