Skip to content

Commit

Permalink
Merge pull request #1352 from nkrapivin/fix-yyswf-2022-1
Browse files Browse the repository at this point in the history
Fix YYSWF parsing on 2022.1 and beyond
  • Loading branch information
colinator27 authored Oct 31, 2023
2 parents 0e57128 + b963753 commit 48ada50
Showing 1 changed file with 54 additions and 20 deletions.
74 changes: 54 additions & 20 deletions UndertaleModLib/Models/UndertaleSprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,11 +1174,20 @@ public class UndertaleYYSWFGradientFillData : UndertaleObject
public UndertaleYYSWFGradientFillType GradientFillType { get; set; }
public UndertaleYYSWFMatrix33 TransformationMatrix { get; set; }
public UndertaleSimpleList<UndertaleYYSWFGradientRecord> Records { get; set; }
/// <summary>
/// Unknown purpose. Probably to accomodate for new texture formats.
/// </summary>
/// <remarks>
/// Presumably present in GM 2022.1+.
/// </remarks>
public int? TPEIndex { get; set; }

/// <inheritdoc />
public void Serialize(UndertaleWriter writer)
{
writer.Write((int)GradientFillType);
if (TPEIndex is not null)
writer.Write(TPEIndex.Value);
writer.WriteUndertaleObject(TransformationMatrix);
writer.WriteUndertaleObject(Records);
}
Expand All @@ -1187,6 +1196,10 @@ public void Serialize(UndertaleWriter writer)
public void Unserialize(UndertaleReader reader)
{
GradientFillType = (UndertaleYYSWFGradientFillType)reader.ReadInt32();
if (reader.undertaleData.IsVersionAtLeast(2022, 1))
{
TPEIndex = reader.ReadInt32();
}
TransformationMatrix = reader.ReadUndertaleObject<UndertaleYYSWFMatrix33>();
Records = new UndertaleSimpleList<UndertaleYYSWFGradientRecord>();
int count = reader.ReadInt32();
Expand Down Expand Up @@ -1651,6 +1664,13 @@ public class UndertaleYYSWFBitmapData : UndertaleObject
public UndertaleYYSWFBitmapType Type { get; set; }
public int Width { get; set; }
public int Height { get; set; }
/// <summary>
/// Unknown purpose. Probably to accomodate for new texture formats.
/// </summary>
/// <remarks>
/// Presumably present in GM 2022.1+.
/// </remarks>
public int? TPEIndex { get; set; }
public byte[] ImageData { get; set; }
public byte[] AlphaData { get; set; }
public byte[] ColorPaletteData { get; set; }
Expand All @@ -1663,18 +1683,25 @@ public void Serialize(UndertaleWriter writer)
writer.Write(Width);
writer.Write(Height);

writer.Write(ImageData is null ? 0 : ImageData.Length);
writer.Write(AlphaData is null ? 0 : AlphaData.Length);
writer.Write(ColorPaletteData is null ? 0 : ColorPaletteData.Length);
if (TPEIndex is null)
{
writer.Write(ImageData is null ? 0 : ImageData.Length);
writer.Write(AlphaData is null ? 0 : AlphaData.Length);
writer.Write(ColorPaletteData is null ? 0 : ColorPaletteData.Length);

if (ImageData != null)
writer.Write(ImageData);
if (AlphaData != null)
writer.Write(AlphaData);
if (ColorPaletteData != null)
writer.Write(ColorPaletteData);
if (ImageData != null)
writer.Write(ImageData);
if (AlphaData != null)
writer.Write(AlphaData);
if (ColorPaletteData != null)
writer.Write(ColorPaletteData);

writer.Align(4);
writer.Align(4);
}
else
{
writer.Write(TPEIndex.Value);
}
}

/// <inheritdoc />
Expand All @@ -1684,18 +1711,25 @@ public void Unserialize(UndertaleReader reader)
Width = reader.ReadInt32();
Height = reader.ReadInt32();

int iL = reader.ReadInt32();
int aL = reader.ReadInt32();
int cL = reader.ReadInt32();
if (reader.undertaleData.IsVersionAtLeast(2022, 1))
{
TPEIndex = reader.ReadInt32();
}
else
{
int iL = reader.ReadInt32();
int aL = reader.ReadInt32();
int cL = reader.ReadInt32();

if (iL > 0)
ImageData = reader.ReadBytes(iL);
if (aL > 0)
AlphaData = reader.ReadBytes(aL);
if (cL > 0)
ColorPaletteData = reader.ReadBytes(cL);
if (iL > 0)
ImageData = reader.ReadBytes(iL);
if (aL > 0)
AlphaData = reader.ReadBytes(aL);
if (cL > 0)
ColorPaletteData = reader.ReadBytes(cL);

reader.Align(4);
reader.Align(4);
}
}
}

Expand Down

0 comments on commit 48ada50

Please sign in to comment.