Sfoglia il codice sorgente

tools: sort arrays of objects by ID

Daniele Bartolini 5 anni fa
parent
commit
26efa82f57
3 ha cambiato i file con 31 aggiunte e 1 eliminazioni
  1. 1 0
      docs/changelog.rst
  2. 14 0
      tools/core/guid.vala
  3. 16 1
      tools/core/json/sjson.vala

+ 1 - 0
docs/changelog.rst

@@ -35,6 +35,7 @@ Changelog
 * Improved Test Level/Start Game button behavior when game failed to launch
 * Level Editor now saves aggregate logs to disk. User can browse logs folder from Help > Browse Logs...
 * New Project dialog no longer allows selecting non-empty folders for new projects
+* Objects inside .level files are now ordered by their ID before serialization
 * Unified Engine and Run menubar items into a single Debug menubar item
 
 0.38.0

+ 14 - 0
tools/core/guid.vala

@@ -85,6 +85,20 @@ public struct Guid
 			&& a.data4 == b.data4
 			;
 	}
+
+	public static int compare_func(Guid a, Guid b)
+	{
+		if (a.data1 != b.data1)
+			return a.data1 < b.data1 ? -1 : 1;
+		if (a.data2 != b.data2)
+			return a.data2 < b.data2 ? -1 : 1;
+		if (a.data3 != b.data3)
+			return a.data3 < b.data3 ? -1 : 1;
+		if (a.data4 != b.data4)
+			return a.data4 < b.data4 ? -1 : 1;
+
+		return 0;
+	}
 }
 
 const Guid GUID_ZERO = { 0, 0, 0, 0 };

+ 16 - 1
tools/core/json/sjson.vala

@@ -151,8 +151,23 @@ public class SJSON
 
 	static void write_array(ArrayList<Value?> a, StringBuilder builder, int indentation)
 	{
+		ArrayList<Value?> a_sorted = a;
+		a_sorted.sort((a, b) => {
+			if (!a.holds(typeof(Hashtable)) || !b.holds(typeof(Hashtable)))
+				return 0;
+
+			Hashtable obj_a = a as Hashtable;
+			Hashtable obj_b = b as Hashtable;
+			if (!obj_a.has_key("id") || !obj_b.has_key("id"))
+				return 0;
+
+			Guid guid_a = Guid.parse(obj_a["id"] as string);
+			Guid guid_b = Guid.parse(obj_b["id"] as string);
+			return Guid.compare_func(guid_a, guid_b);
+		});
+
 		builder.append_c('[');
-		foreach (Value? item in a) {
+		foreach (Value? item in a_sorted) {
 			write_new_line(builder, indentation+1);
 			write(item, builder, indentation+1);
 		}