From 553e1213027dd5a770b5f3f04854ee33f5cc9e03 Mon Sep 17 00:00:00 2001 From: Passive <20432486+PassiveModding@users.noreply.github.com> Date: Sun, 6 Oct 2024 11:15:50 +1100 Subject: [PATCH] Properly serialize ColorTable nodes --- .../Models/Composer/MaterialSet.cs | 11 ++- .../Files/Structs/Material/ColorTableSet.cs | 91 +++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/Meddle/Meddle.Plugin/Models/Composer/MaterialSet.cs b/Meddle/Meddle.Plugin/Models/Composer/MaterialSet.cs index 67fa863..d707ee6 100644 --- a/Meddle/Meddle.Plugin/Models/Composer/MaterialSet.cs +++ b/Meddle/Meddle.Plugin/Models/Composer/MaterialSet.cs @@ -499,7 +499,16 @@ void AddCustomizeParameters() void AddColorTable() { if (colorTable == null) return; - extrasDict["ColorTable"] = JsonNode.Parse(JsonSerializer.Serialize(colorTable, JsonOptions))!; + + switch (colorTable) + { + case ColorTableSet colorTableSet: + extrasDict["ColorTable"] = JsonNode.Parse(JsonSerializer.Serialize(colorTableSet.ToObject(), JsonOptions))!; + break; + case LegacyColorTableSet legacyColorTableSet: + extrasDict["LegacyColorTable"] = JsonNode.Parse(JsonSerializer.Serialize(legacyColorTableSet.ToObject(), JsonOptions))!; + break; + } } void AddStainColor() diff --git a/Meddle/Meddle.Utils/Files/Structs/Material/ColorTableSet.cs b/Meddle/Meddle.Utils/Files/Structs/Material/ColorTableSet.cs index 3014837..f158555 100644 --- a/Meddle/Meddle.Utils/Files/Structs/Material/ColorTableSet.cs +++ b/Meddle/Meddle.Utils/Files/Structs/Material/ColorTableSet.cs @@ -1,4 +1,6 @@ using System.Numerics; +using System.Text.Json; +using System.Text.Json.Nodes; namespace Meddle.Utils.Files.Structs.Material; @@ -8,12 +10,30 @@ public struct ColorTableSet : IColorTableSet { public ColorTable ColorTable; public ColorDyeTable? ColorDyeTable; + + public object ToObject() + { + return new + { + ColorTable = ColorTable.ToObject(), + ColorDyeTable = ColorDyeTable?.ToObject() + }; + } } public struct LegacyColorTableSet : IColorTableSet { public LegacyColorTable ColorTable; public LegacyColorDyeTable? ColorDyeTable; + + public object ToObject() + { + return new + { + ColorTable = ColorTable.ToObject(), + ColorDyeTable = ColorDyeTable?.ToObject() + }; + } } public readonly struct LegacyColorDyeTable @@ -25,6 +45,23 @@ public LegacyColorDyeTable(ref SpanBinaryReader reader) { rows = reader.Read(LegacyColorTable.LegacyNumRows).ToArray(); } + + public object ToObject() + { + return new + { + Rows = Rows.ToArray().Select(r => new + { + Template = r.Template, + Diffuse = r.Diffuse, + Specular = r.Specular, + Emissive = r.Emissive, + Gloss = r.Gloss, + SpecularStrength = r.SpecularStrength, + + }).ToArray() + }; + } } public readonly struct ColorDyeTable @@ -36,6 +73,23 @@ public ColorDyeTable(ref SpanBinaryReader reader) { rows = reader.Read(ColorTable.NumRows).ToArray(); } + + public object ToObject() + { + return new + { + Rows = Rows.ToArray().Select(r => new + { + Template = r.Template, + Diffuse = r.Diffuse, + Specular = r.Specular, + Emissive = r.Emissive, + Gloss = r.Gloss, + SpecularStrength = r.SpecularStrength, + + }).ToArray() + }; + } } @@ -49,6 +103,24 @@ public LegacyColorTable(ref SpanBinaryReader reader) rows = reader.Read(LegacyNumRows).ToArray(); } + public object ToObject() + { + return new + { + Rows = Rows.ToArray().Select(r => new + { + Diffuse = r.Diffuse, + Specular = r.Specular, + SpecularStrength = r.SpecularStrength, + Emissive = r.Emissive, + GlossStrength = r.GlossStrength, + MaterialRepeat = r.MaterialRepeat, + MaterialSkew = r.MaterialSkew, + TileIndex = r.TileIndex + }).ToArray() + }; + } + // normal pixel A channel on legacy normal as a float from 0-1 public LegacyColorTableRow GetBlendedPair(float normalPixelW) { @@ -121,6 +193,25 @@ public ColorTable(ref SpanBinaryReader reader) return (pair0, pair1); } + public object ToObject() + { + return new + { + Rows = Rows.ToArray().Select(r => new + { + Diffuse = r.Diffuse, + Specular = r.Specular, + SpecularStrength = r.SpecularStrength, + Emissive = r.Emissive, + GlossStrength = r.GlossStrength, + MaterialRepeat = r.MaterialRepeat, + MaterialSkew = r.MaterialSkew, + TileIndex = r.TileIndex, + ShaderId = r.ShaderId + }).ToArray() + }; + } + public ColorTableRow GetBlendedPair(int weight, int blend) { var (row0, row1) = GetPair(weight);