Skip to content

Commit

Permalink
Properly serialize ColorTable nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
PassiveModding committed Oct 6, 2024
1 parent 4829fc8 commit 553e121
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Meddle/Meddle.Plugin/Models/Composer/MaterialSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
91 changes: 91 additions & 0 deletions Meddle/Meddle.Utils/Files/Structs/Material/ColorTableSet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Numerics;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace Meddle.Utils.Files.Structs.Material;

Expand All @@ -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
Expand All @@ -25,6 +45,23 @@ public LegacyColorDyeTable(ref SpanBinaryReader reader)
{
rows = reader.Read<LegacyColorDyeTableRow>(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
Expand All @@ -36,6 +73,23 @@ public ColorDyeTable(ref SpanBinaryReader reader)
{
rows = reader.Read<ColorDyeTableRow>(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()
};
}
}


Expand All @@ -49,6 +103,24 @@ public LegacyColorTable(ref SpanBinaryReader reader)
rows = reader.Read<LegacyColorTableRow>(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)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 553e121

Please sign in to comment.