diff --git a/src/Configs.cs b/src/Configs.cs
new file mode 100644
index 0000000..5916c4a
--- /dev/null
+++ b/src/Configs.cs
@@ -0,0 +1,18 @@
+namespace NodeBeautify
+{
+ internal static class Configs
+ {
+ ///
+ /// Round to the cloest multiples of this value
+ ///
+ public const int GRID_SIZE = 10;
+
+ ///
+ /// NodeType, (Width, Height)
+ ///
+ public static readonly Dictionary FIXED_SIZE = new(){
+ { "CLIPTextEncode", (400.0f, 160.0f) },
+ { "PreviewImage", (420.0f, 420.0f) }
+ };
+ }
+}
diff --git a/src/Functions.cs b/src/Functions.cs
new file mode 100644
index 0000000..fd7af1b
--- /dev/null
+++ b/src/Functions.cs
@@ -0,0 +1,84 @@
+using Newtonsoft.Json.Linq;
+using static NodeBeautify.Configs;
+
+namespace NodeBeautify
+{
+ internal static class Functions
+ {
+ private static float Round(JToken value) => (float)value - ((float)value % GRID_SIZE);
+
+ internal static void FixNodes(JObject data)
+ {
+ var nodes = (JArray)data["nodes"];
+
+ // Doing try-catch cause somehow the value can be either array or dictionary...
+ foreach (JObject node in nodes)
+ {
+ try
+ {
+ JArray pos = (JArray)node["pos"];
+ pos[0] = Round(pos[0]);
+ pos[1] = Round(pos[1]);
+ }
+ catch (InvalidCastException)
+ {
+ JObject pos = (JObject)node["pos"];
+ pos["0"] = Round(pos["0"]);
+ pos["1"] = Round(pos["1"]);
+ }
+
+ string key = (string)node["type"];
+
+ try
+ {
+ JObject size = (JObject)node["size"];
+
+ if (FIXED_SIZE.ContainsKey(key))
+ {
+ size["0"] = FIXED_SIZE[key].Item1;
+ size["1"] = FIXED_SIZE[key].Item2;
+ }
+ else
+ {
+ size["0"] = Round(size["0"]);
+ size["1"] = Round(size["1"]);
+ }
+ }
+ catch (InvalidCastException)
+ {
+ JArray size = (JArray)node["size"];
+
+ if (FIXED_SIZE.ContainsKey(key))
+ {
+ size[0] = FIXED_SIZE[key].Item1;
+ size[1] = FIXED_SIZE[key].Item2;
+ }
+ else
+ {
+ size[0] = Round(size[0]);
+ size[1] = Round(size[0]);
+ }
+ }
+ }
+ }
+
+ internal static void FixGroups(JObject data)
+ {
+ var groups = (JArray)data["groups"];
+
+ foreach (JObject group in groups)
+ {
+ JArray bound = (JArray)group["bounding"];
+ for (int edge = 0; edge < 4; edge++)
+ bound[edge] = Round(bound[edge]);
+ }
+ }
+
+ internal static void SortNode(JObject data)
+ {
+ var nodes = (JArray)data["nodes"];
+ var sortedNodes = nodes.OrderBy(obj => (int)obj["id"]).ToArray();
+ (data["nodes"] as JArray).ReplaceAll(sortedNodes);
+ }
+ }
+}
diff --git a/src/Program.cs b/src/Program.cs
index 39ccdc4..4be7621 100644
--- a/src/Program.cs
+++ b/src/Program.cs
@@ -5,22 +5,9 @@ namespace NodeBeautify
{
internal class Program
{
- ///
- /// Round to the cloest multiples of this value
- ///
- private const int GRID_SIZE = 20;
-
- ///
- /// NodeType, (Width, Height)
- ///
- private static readonly Dictionary FIXED_SIZE = new(){
- {
- "CLIPTextEncode", (400.0f, 150.0f)
- }
- };
-
private static bool hasError;
- static void Main(string[] args)
+
+ internal static void Main(string[] args)
{
hasError = false;
@@ -55,77 +42,6 @@ namespace NodeBeautify
Process(path);
}
- private static float Round(JToken value)
- {
- return (float)value - ((float)value % GRID_SIZE);
- }
-
- private static void FixNodes(JObject data)
- {
- var nodes = (JArray)data["nodes"];
-
- // Doing try-catch cause somehow the value can be either array or dictionary...
- foreach (JObject node in nodes)
- {
- try
- {
- JArray pos = (JArray)node["pos"];
- pos[0] = Round(pos[0]);
- pos[1] = Round(pos[1]);
- }
- catch (InvalidCastException)
- {
- JObject pos = (JObject)node["pos"];
- pos["0"] = Round(pos["0"]);
- pos["1"] = Round(pos["1"]);
- }
-
- string key = (string)node["type"];
-
- try
- {
- JObject size = (JObject)node["size"];
-
- if (FIXED_SIZE.ContainsKey(key))
- {
- size["0"] = FIXED_SIZE[key].Item1;
- size["1"] = FIXED_SIZE[key].Item2;
- }
- else
- {
- size["0"] = Round(size["0"]);
- size["1"] = Round(size["1"]);
- }
- }
- catch (InvalidCastException)
- {
- JArray size = (JArray)node["size"];
- if (FIXED_SIZE.ContainsKey(key))
- {
- size[0] = FIXED_SIZE[key].Item1;
- size[1] = FIXED_SIZE[key].Item2;
- }
- else
- {
- size[0] = Round(size[0]);
- size[1] = Round(size[0]);
- }
- }
- }
- }
-
- private static void FixGroups(JObject data)
- {
- var groups = (JArray)data["groups"];
-
- foreach (JObject group in groups)
- {
- JArray bound = (JArray)group["bounding"];
- for (int edge = 0; edge < 4; edge++)
- bound[edge] = Round(bound[edge]);
- }
- }
-
private static void Process(string path)
{
string jsonContent = File.ReadAllText(path).Trim();
@@ -133,8 +49,9 @@ namespace NodeBeautify
try
{
- FixNodes(data);
- FixGroups(data);
+ Functions.FixNodes(data);
+ Functions.FixGroups(data);
+ Functions.SortNode(data);
File.WriteAllText(path, JsonConvert.SerializeObject(data));
}
catch (NullReferenceException)