From 635aa7e999c8f1cd05fb6317345e4226583e597e Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Thu, 23 Mar 2023 23:05:27 +1100 Subject: [PATCH] Fix decal serialization determinism (#14805) --- .../DecalGridChunkCollectionTypeSerializer.cs | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/Content.Shared/Decals/DecalGridChunkCollectionTypeSerializer.cs b/Content.Shared/Decals/DecalGridChunkCollectionTypeSerializer.cs index ad6b803e76..bd48a93e92 100644 --- a/Content.Shared/Decals/DecalGridChunkCollectionTypeSerializer.cs +++ b/Content.Shared/Decals/DecalGridChunkCollectionTypeSerializer.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Linq; using Robust.Shared.Map; using Robust.Shared.Serialization; using Robust.Shared.Serialization.Manager; @@ -62,9 +63,16 @@ namespace Content.Shared.Decals var uids = new SortedSet(); var uidChunkMap = new Dictionary(); - foreach (var (indices, decals) in dictionary) + var allIndices = dictionary.Keys.ToList(); + allIndices.Sort((x, y) => x.X == y.X ? x.Y.CompareTo(y.Y) : x.X.CompareTo(y.X)); + + foreach (var indices in allIndices) { - foreach (var uid in decals.Decals.Keys) + var decals = dictionary[indices]; + var decalUids = decals.Decals.Keys.ToList(); + decalUids.Sort(); + + foreach (var uid in decalUids) { uids.Add(uid); uidChunkMap[uid] = indices; @@ -126,9 +134,12 @@ namespace Content.Shared.Decals } var lookupIndex = 0; + var lookupNodes = lookup.Keys.ToList(); + lookupNodes.Sort(); - foreach (var (data, uids) in lookup) + foreach (var data in lookupNodes) { + var uids = lookup[data]; var lookupNode = new MappingDataNode { { "node", serializationManager.WriteValue(data, alwaysWrite, context) } }; var decks = new MappingDataNode(); @@ -152,7 +163,7 @@ namespace Content.Shared.Decals } [DataDefinition] - private readonly struct DecalData : IEquatable + private readonly struct DecalData : IEquatable, IComparable { [DataField("id")] public readonly string Id = string.Empty; @@ -205,6 +216,19 @@ namespace Content.Shared.Decals { return HashCode.Combine(Id, Color, Angle, ZIndex, Cleanable); } + + public int CompareTo(DecalData other) + { + var idComparison = string.Compare(Id, other.Id, StringComparison.Ordinal); + if (idComparison != 0) + return idComparison; + + var zIndexComparison = ZIndex.CompareTo(other.ZIndex); + if (zIndexComparison != 0) + return zIndexComparison; + + return Cleanable.CompareTo(other.Cleanable); + } } } } -- 2.51.2