Skip to content

Commit c7bf074

Browse files
committed
- Colorset length now automatically calculated when writing MTRL files.
- Colorset Dye data may now be imported/exported for all items, rather than only MTRLs that initially had it.
1 parent 6c885a5 commit c7bf074

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

xivModdingFramework/Materials/DataContainers/XivMtrl.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ public class XivMtrl
5858
/// <remarks>
5959
/// Can be 0 if there is no ColorSet Data
6060
/// </remarks>
61-
public ushort ColorSetDataSize { get; set; }
61+
public ushort ColorSetDataSize { get {
62+
var size = ColorSetData.Count * 2;
63+
size += ColorSetExtraData == null ? 0 : ColorSetExtraData.Length;
64+
return (ushort) size;
65+
} }
6266

6367
/// <summary>
6468
/// The size of the Material Data section
@@ -442,7 +446,6 @@ public void SetShaderInfo(ShaderInfo info, bool forced = false)
442446
ColorSetCount = 1;
443447
ColorSetData = new List<Half>();
444448
ColorSetExtraData = null;
445-
ColorSetDataSize = 0;
446449
} else
447450
{
448451
if(ColorSetCount == 0 || ColorSetData == null || ColorSetData.Count != 256)
@@ -454,9 +457,6 @@ public void SetShaderInfo(ShaderInfo info, bool forced = false)
454457
{
455458
ColorSetExtraData = Tex.GetColorsetExtraDataFromDDS(Tex.GetDefaultTexturePath(XivTexType.ColorSet));
456459
}
457-
458-
// Standard Colorset Size. -- This could really be generated automatically later.
459-
ColorSetDataSize = 544;
460460
}
461461

462462

xivModdingFramework/Materials/FileTypes/Mtrl.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,15 @@ await Task.Run((Func<Task>)(async () =>
331331
{
332332
Signature = br.ReadInt32(),
333333
FileSize = br.ReadInt16(),
334-
ColorSetDataSize = br.ReadUInt16(),
335-
MaterialDataSize = br.ReadUInt16(),
336-
TexturePathsDataSize = br.ReadUInt16(),
337-
TextureCount = br.ReadByte(),
338-
MapCount = br.ReadByte(),
339-
ColorSetCount = br.ReadByte(),
340-
UnknownDataSize = br.ReadByte(),
341-
MTRLPath = mtrlPath
342334
};
335+
var colorSetDataSize = br.ReadUInt16();
336+
xivMtrl.MaterialDataSize = br.ReadUInt16();
337+
xivMtrl.TexturePathsDataSize = br.ReadUInt16();
338+
xivMtrl.TextureCount = br.ReadByte();
339+
xivMtrl.MapCount = br.ReadByte();
340+
xivMtrl.ColorSetCount = br.ReadByte();
341+
xivMtrl.UnknownDataSize = br.ReadByte();
342+
xivMtrl.MTRLPath = mtrlPath;
343343

344344
var pathSizeList = new List<int>();
345345

@@ -453,7 +453,7 @@ await Task.Run((Func<Task>)(async () =>
453453

454454
xivMtrl.ColorSetData = new List<Half>();
455455
xivMtrl.ColorSetExtraData = null;
456-
if (xivMtrl.ColorSetDataSize > 0)
456+
if (colorSetDataSize > 0)
457457
{
458458
// Color Data is always 512 (6 x 14 = 64 x 8bpp = 512)
459459
var colorDataSize = 512;
@@ -464,7 +464,7 @@ await Task.Run((Func<Task>)(async () =>
464464
}
465465

466466
// If the color set is 544 in length, it has an extra 32 bytes at the end
467-
if (xivMtrl.ColorSetDataSize == 544)
467+
if (colorSetDataSize == 544)
468468
{
469469
xivMtrl.ColorSetExtraData = br.ReadBytes(32);
470470
}
@@ -594,16 +594,14 @@ public Task<XivTex> MtrlToXivTex(XivMtrl xivMtrl, TexTypePath ttp)
594594
/// <param name="race">The selected race for the item</param>
595595
public void SaveColorSetExtraData(IItem item, XivMtrl xivMtrl, DirectoryInfo saveDirectory, XivRace race)
596596
{
597-
if (xivMtrl.ColorSetExtraData != null)
598-
{
599-
var path = IOUtil.MakeItemSavePath(item, saveDirectory, race);
597+
var toWrite = xivMtrl.ColorSetExtraData != null ? xivMtrl.ColorSetExtraData : new byte[32];
598+
var path = IOUtil.MakeItemSavePath(item, saveDirectory, race);
600599

601-
Directory.CreateDirectory(path);
600+
Directory.CreateDirectory(path);
602601

603-
var savePath = Path.Combine(path, Path.GetFileNameWithoutExtension(xivMtrl.MTRLPath) + ".dat");
602+
var savePath = Path.Combine(path, Path.GetFileNameWithoutExtension(xivMtrl.MTRLPath) + ".dat");
604603

605-
File.WriteAllBytes(savePath, xivMtrl.ColorSetExtraData);
606-
}
604+
File.WriteAllBytes(savePath, toWrite);
607605
}
608606

609607

@@ -757,6 +755,7 @@ public byte[] CreateMtrlFile(XivMtrl xivMtrl, IItem item)
757755
// Write the actual string list (including padding).
758756
mtrlBytes.AddRange(stringListBytes);
759757

758+
// Don't know what these (4) bytes do, but hey, whatever.
760759
mtrlBytes.AddRange(xivMtrl.Unknown2);
761760

762761
foreach (var colorSetHalf in xivMtrl.ColorSetData)

xivModdingFramework/Textures/FileTypes/Tex.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,9 +1098,12 @@ public byte[] DDStoMtrlData(XivMtrl xivMtrl, DirectoryInfo ddsFileDirectory, IIt
10981098

10991099
var colorSetExtraData = new byte[32];
11001100
// If the colorset size is 544, it contains extra data that must be imported
1101-
if (xivMtrl.ColorSetDataSize == 544)
1101+
try
11021102
{
11031103
colorSetExtraData = GetColorsetExtraDataFromDDS(ddsFileDirectory);
1104+
} catch
1105+
{
1106+
colorSetExtraData = new byte[32];
11041107
}
11051108

11061109
// Replace the color set data with the imported data

0 commit comments

Comments
 (0)