Skip to content

Commit c02e127

Browse files
Minor changes to fix crashes and incomplete loads. MAT3 now checks if the lighting color index fits within the bounds of the loaded data, as does the swap mode table index. TexCoordGenIO now uses the correct count for the loop (the calculated number of TexGens rather than a constant 4), and TevOrder now uses GXColorChannelID rather than J3DColorChannelID.
1 parent 0db6a7c commit c02e127

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

SuperBMD/source/BMD/MAT3.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ private void LoadInitData(EndianBinaryReader reader)
315315
for (int i = 0; i < 8; i++)
316316
{
317317
int lightIndex = reader.ReadInt16();
318-
if (lightIndex == -1)
318+
if ((lightIndex == -1) || (lightIndex > m_LightingColorBlock.Count) || (m_LightingColorBlock.Count == 0))
319319
continue;
320320
else
321321
mat.LightingColors[i] = m_LightingColorBlock[lightIndex];
@@ -424,7 +424,7 @@ private void LoadInitData(EndianBinaryReader reader)
424424
for (int i = 0; i < 16; i++)
425425
{
426426
int tevSwapModeTableIndex = reader.ReadInt16();
427-
if (tevSwapModeTableIndex == -1)
427+
if ((tevSwapModeTableIndex < 0) || (tevSwapModeTableIndex > m_SwapTableBlock.Count))
428428
continue;
429429
else
430430
mat.SwapTables[i] = m_SwapTableBlock[tevSwapModeTableIndex];

SuperBMD/source/Materials/IO/TexCoordGenIO.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static List<TexCoordGen> Load(EndianBinaryReader reader, int offset, int
1515
List<TexCoordGen> gens = new List<TexCoordGen>();
1616
int count = size / 4;
1717

18-
for (int i = 0; i < 4; i++)
18+
for (int i = 0; i < count; i++)
1919
gens.Add(new TexCoordGen(reader));
2020

2121
return gens;

SuperBMD/source/Materials/Material.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public Material()
7272
AlphaSels = new KonstAlphaSel[16];
7373

7474
TevOrders = new TevOrder?[16];
75-
TevOrders[0] = new TevOrder(TexCoordId.TexCoord0, TexMapId.TexMap0, J3DColorChannelId.Color0);
75+
TevOrders[0] = new TevOrder(TexCoordId.TexCoord0, TexMapId.TexMap0, GXColorChannelId.Color0);
7676

7777
TevColors = new Color?[16];
7878
TevColors[0] = new Color(1, 1, 1, 1);
@@ -85,7 +85,7 @@ public Material()
8585
SwapTables = new TevSwapModeTable?[16];
8686
SwapTables[0] = new TevSwapModeTable(0, 1, 2, 3);
8787

88-
AlphCompare = new AlphaCompare(CompareType.Greater, 0, AlphaOp.And, CompareType.Always, 0);
88+
AlphCompare = new AlphaCompare(CompareType.Greater, 127, AlphaOp.And, CompareType.Always, 0);
8989
ZMode = new ZMode(true, CompareType.LEqual, true);
9090
BMode = new BlendMode(Enums.BlendMode.None, BlendModeControl.SrcAlpha, BlendModeControl.InverseSrcAlpha, LogicOp.NoOp);
9191
NBTScale = new NBTScale(0, Vector3.Zero);
@@ -124,7 +124,7 @@ public void SetUpTev(bool hasTexture, bool hasVtxColor, int texIndex)
124124
// Generate texture stuff
125125
AddTexGen(TexGenType.Matrix2x4, TexGenSrc.Tex0, Enums.TexMatrix.Identity);
126126
AddTexMatrix(TexGenType.Matrix3x4, 0, OpenTK.Vector3.Zero, OpenTK.Vector2.One, 0, OpenTK.Vector2.Zero, OpenTK.Matrix4.Identity);
127-
AddTevOrder(TexCoordId.TexCoord0, TexMapId.TexMap0, J3DColorChannelId.Null);
127+
AddTevOrder(TexCoordId.TexCoord0, TexMapId.TexMap0, GXColorChannelId.ColorNull);
128128
AddTexIndex(texIndex);
129129

130130
// Texture + Vertex Color
@@ -238,7 +238,7 @@ public void AddTexIndex(int index)
238238
}
239239
}
240240

241-
public void AddTevOrder(TexCoordId coordId, TexMapId mapId, J3DColorChannelId colorChanId)
241+
public void AddTevOrder(TexCoordId coordId, TexMapId mapId, GXColorChannelId colorChanId)
242242
{
243243
for (int i = 0; i < 8; i++)
244244
{

SuperBMD/source/Materials/TevOrder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public struct TevOrder : IEquatable<TevOrder>
1212
{
1313
public TexCoordId TexCoord;
1414
public TexMapId TexMap;
15-
public J3DColorChannelId ChannelId;
15+
public GXColorChannelId ChannelId;
1616

17-
public TevOrder(TexCoordId texCoord, TexMapId texMap, J3DColorChannelId chanID)
17+
public TevOrder(TexCoordId texCoord, TexMapId texMap, GXColorChannelId chanID)
1818
{
1919
TexCoord = texCoord;
2020
TexMap = texMap;
@@ -25,7 +25,7 @@ public TevOrder(EndianBinaryReader reader)
2525
{
2626
TexCoord = (TexCoordId)reader.ReadByte();
2727
TexMap = (TexMapId)reader.ReadByte();
28-
ChannelId = (J3DColorChannelId)reader.ReadByte();
28+
ChannelId = (GXColorChannelId)reader.ReadByte();
2929
reader.SkipByte();
3030
}
3131

0 commit comments

Comments
 (0)