Skip to content

Commit

Permalink
Minor changes to fix crashes and incomplete loads. MAT3 now checks if…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
Sage-of-Mirrors committed Mar 31, 2018
1 parent 0db6a7c commit c02e127
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions SuperBMD/source/BMD/MAT3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private void LoadInitData(EndianBinaryReader reader)
for (int i = 0; i < 8; i++)
{
int lightIndex = reader.ReadInt16();
if (lightIndex == -1)
if ((lightIndex == -1) || (lightIndex > m_LightingColorBlock.Count) || (m_LightingColorBlock.Count == 0))
continue;
else
mat.LightingColors[i] = m_LightingColorBlock[lightIndex];
Expand Down Expand Up @@ -424,7 +424,7 @@ private void LoadInitData(EndianBinaryReader reader)
for (int i = 0; i < 16; i++)
{
int tevSwapModeTableIndex = reader.ReadInt16();
if (tevSwapModeTableIndex == -1)
if ((tevSwapModeTableIndex < 0) || (tevSwapModeTableIndex > m_SwapTableBlock.Count))
continue;
else
mat.SwapTables[i] = m_SwapTableBlock[tevSwapModeTableIndex];
Expand Down
2 changes: 1 addition & 1 deletion SuperBMD/source/Materials/IO/TexCoordGenIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static List<TexCoordGen> Load(EndianBinaryReader reader, int offset, int
List<TexCoordGen> gens = new List<TexCoordGen>();
int count = size / 4;

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

return gens;
Expand Down
8 changes: 4 additions & 4 deletions SuperBMD/source/Materials/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Material()
AlphaSels = new KonstAlphaSel[16];

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

TevColors = new Color?[16];
TevColors[0] = new Color(1, 1, 1, 1);
Expand All @@ -85,7 +85,7 @@ public Material()
SwapTables = new TevSwapModeTable?[16];
SwapTables[0] = new TevSwapModeTable(0, 1, 2, 3);

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

// Texture + Vertex Color
Expand Down Expand Up @@ -238,7 +238,7 @@ public void AddTexIndex(int index)
}
}

public void AddTevOrder(TexCoordId coordId, TexMapId mapId, J3DColorChannelId colorChanId)
public void AddTevOrder(TexCoordId coordId, TexMapId mapId, GXColorChannelId colorChanId)
{
for (int i = 0; i < 8; i++)
{
Expand Down
6 changes: 3 additions & 3 deletions SuperBMD/source/Materials/TevOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public struct TevOrder : IEquatable<TevOrder>
{
public TexCoordId TexCoord;
public TexMapId TexMap;
public J3DColorChannelId ChannelId;
public GXColorChannelId ChannelId;

public TevOrder(TexCoordId texCoord, TexMapId texMap, J3DColorChannelId chanID)
public TevOrder(TexCoordId texCoord, TexMapId texMap, GXColorChannelId chanID)
{
TexCoord = texCoord;
TexMap = texMap;
Expand All @@ -25,7 +25,7 @@ public TevOrder(EndianBinaryReader reader)
{
TexCoord = (TexCoordId)reader.ReadByte();
TexMap = (TexMapId)reader.ReadByte();
ChannelId = (J3DColorChannelId)reader.ReadByte();
ChannelId = (GXColorChannelId)reader.ReadByte();
reader.SkipByte();
}

Expand Down

0 comments on commit c02e127

Please sign in to comment.