Skip to content

Commit

Permalink
[MapleLib] Sort WzCanvasNode in WzSubProperty by its name during the …
Browse files Browse the repository at this point in the history
…saving process

see: #173
  • Loading branch information
lastbattle committed Oct 31, 2022
1 parent 3cd4637 commit a5760d4
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions MapleLib/WzLib/WzProperties/WzSubProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
using System.Collections.Generic;
using System.IO;
using MapleLib.WzLib.Util;
using SharpDX.Direct2D1;

namespace MapleLib.WzLib.WzProperties
{
Expand Down Expand Up @@ -130,6 +131,11 @@ public override WzImageProperty GetFromPath(string path)
}
return ret;
}

/// <summary>
/// Write the WzSubProperty
/// </summary>
/// <param name="writer"></param>
public override void WriteValue(WzBinaryWriter writer)
{
bool bIsLuaProperty = false;
Expand All @@ -140,14 +146,44 @@ public override void WriteValue(WzBinaryWriter writer)
if (!bIsLuaProperty)
writer.WriteStringValue("Property", WzImage.WzImageHeaderByte_WithoutOffset, WzImage.WzImageHeaderByte_WithOffset);

// Sort WzCanvasPropertys' in images by their name
// see https://github.com/lastbattle/Harepacker-resurrected/issues/173
properties.Sort((img1, img2) =>
{
if (img1 == null)
return 0;
else if (img1.GetType() == typeof(WzCanvasProperty))
{
int nodeId1, nodeId2;
if (int.TryParse(img1.Name, out nodeId1) && int.TryParse(img2.Name, out nodeId2))
{
if (nodeId1 == nodeId2)
return 0;
if (nodeId1 > nodeId2)
return 1;
return -1;
} else // default to string compare
return img1.Name.CompareTo(img2.Name);
}
else
return img1.Name.CompareTo(img2.Name); // (leave non canvas nodes at the very bottom, i.e "info")
});

WzImageProperty.WritePropertyList(writer, properties);
}

/// <summary>
/// Exports as XML
/// </summary>
/// <param name="writer"></param>
/// <param name="level"></param>
public override void ExportXml(StreamWriter writer, int level)
{
writer.WriteLine(XmlUtil.Indentation(level) + XmlUtil.OpenNamedTag("WzSub", this.Name, true));
WzImageProperty.DumpPropertyList(writer, level, WzProperties);
writer.WriteLine(XmlUtil.Indentation(level) + XmlUtil.CloseTag("WzSub"));
}

/// <summary>
/// Disposes the object
/// </summary>
Expand Down

0 comments on commit a5760d4

Please sign in to comment.