Skip to content

Commit

Permalink
misc: endianness adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulusParssinen committed Jun 19, 2024
1 parent 9fa3737 commit 7cbfdea
Show file tree
Hide file tree
Showing 24 changed files with 208 additions and 192 deletions.
18 changes: 16 additions & 2 deletions Shockky/IO/ShockwaveReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ public Color ReadColor()
Advance(6);
return Color.FromArgb(r, g, b);
}
public Point ReadPoint()
public Point ReadPointLittleEndian()
{
return new(ReadInt16LittleEndian(), ReadInt16LittleEndian());
}
public Rectangle ReadRect()
public Rectangle ReadRectLittleEndian()
{
short top = ReadInt16LittleEndian();
short left = ReadInt16LittleEndian();
Expand All @@ -228,6 +228,20 @@ public Rectangle ReadRect()

return Rectangle.FromLTRB(left, top, right, bottom);
}

public Point ReadPointBigEndian()
{
return new(ReadInt16BigEndian(), ReadInt16BigEndian());
}
public Rectangle ReadRectBigEndian()
{
short top = ReadInt16BigEndian();
short left = ReadInt16BigEndian();
short bottom = ReadInt16BigEndian();
short right = ReadInt16BigEndian();

return Rectangle.FromLTRB(left, top, right, bottom);
}

public unsafe IResource ReadCompressedResource(AfterburnerMapEntry entry, ReaderContext context)
{
Expand Down
7 changes: 6 additions & 1 deletion Shockky/IO/ShockwaveWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,14 @@ public void WriteRect(Rectangle value)
WriteInt16LittleEndian((short)value.Bottom);
WriteInt16LittleEndian((short)value.Right);
}
public void WriteMemberId(CastMemberId memberId)
public void WriteMemberIdLittleEndian(CastMemberId memberId)
{
WriteInt16LittleEndian(memberId.CastLib);
WriteInt16LittleEndian(memberId.MemberNum);
}
public void WriteMemberIdBigEndian(CastMemberId memberId)
{
WriteInt16BigEndian(memberId.CastLib);
WriteInt16BigEndian(memberId.MemberNum);
}
}
4 changes: 2 additions & 2 deletions Shockky/Resources/Cast/CastMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public CastMap()
{ }
public CastMap(ref ShockwaveReader input, ReaderContext context)
{
input.ReverseEndianness = true;
input.ReverseEndianness = false;

Members = new int[input.Length / sizeof(int)];
for (int i = 0; i < Members.Length; i++)
{
Members[i] = input.ReadInt32LittleEndian();
Members[i] = input.ReadInt32BigEndian();
}
}

Expand Down
25 changes: 11 additions & 14 deletions Shockky/Resources/Cast/CastMemberMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public sealed class MetadataHeader

public MetadataHeader(ref ShockwaveReader input, ReaderContext context)
{
int headerSize = input.ReadInt32LittleEndian();
int headerSize = input.ReadInt32BigEndian();
Debug.Assert(headerSize == 16 || headerSize == 20);

int scriptGarbagePtr = input.ReadInt32LittleEndian();
int legacyFlags = input.ReadInt32LittleEndian();
Flags = (CastMemberInfoFlags)input.ReadInt32LittleEndian();
int scriptGarbagePtr = input.ReadInt32BigEndian();
int legacyFlags = input.ReadInt32BigEndian();
Flags = (CastMemberInfoFlags)input.ReadInt32BigEndian();
if (headerSize >= 20)
{
ScriptContextNum = input.ReadInt32LittleEndian();
ScriptContextNum = input.ReadInt32BigEndian();
}
}
}
Expand All @@ -48,10 +48,10 @@ public sealed class MetadataEntries
{
public MetadataEntries(ref ShockwaveReader input, ReaderContext context)
{
int[] propertyOffsets = new int[input.ReadInt16LittleEndian() + 1];
int[] propertyOffsets = new int[input.ReadInt16BigEndian() + 1];
for (int i = 0; i < propertyOffsets.Length; i++)
{
propertyOffsets[i] = input.ReadInt32LittleEndian();
propertyOffsets[i] = input.ReadInt32BigEndian();
}

// TODO: Serialize the values
Expand Down Expand Up @@ -118,18 +118,18 @@ private void ReadProperty(ref ShockwaveReader input, int index, int length)
RegistrationPoints = new int[length / 4];
for (int i = 0; i < RegistrationPoints.Length; i++)
{
RegistrationPoints[i] = input.ReadInt32LittleEndian();
RegistrationPoints[i] = input.ReadInt32BigEndian();
}
break;
//15 - MoA ID?
case 16:
ClipboardFormat = input.ReadString(length);
break;
case 17:
CreationDate = input.ReadInt32LittleEndian() * 1000;
CreationDate = input.ReadInt32BigEndian() * 1000;
break;
case 18:
ModifiedDate = input.ReadInt32LittleEndian() * 1000;
ModifiedDate = input.ReadInt32BigEndian() * 1000;
break;
case 19:
ModifiedBy = input.ReadNullString();
Expand All @@ -145,14 +145,11 @@ private void ReadProperty(ref ShockwaveReader input, int index, int length)
break;
default:
ReadOnlySpan<byte> unknown = input.ReadBytes(length);
string test = Encoding.UTF8.GetString(unknown);
break;
}
}
}




public int GetBodySize(WriterOptions options)
{
throw new NotImplementedException();
Expand Down
8 changes: 4 additions & 4 deletions Shockky/Resources/Cast/CastMemberProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public sealed class CastMemberProperties : IResource, IShockwaveItem

public CastMemberProperties(ref ShockwaveReader input, ReaderContext context)
{
input.ReverseEndianness = true;
input.ReverseEndianness = false;

Type = (MemberKind)input.ReadInt32LittleEndian();
int metadataLength = input.ReadInt32LittleEndian();
int propetiesLength = input.ReadInt32LittleEndian();
Type = (MemberKind)input.ReadInt32BigEndian();
int metadataLength = input.ReadInt32BigEndian();
int propetiesLength = input.ReadInt32BigEndian();

Metadata = new CastMemberMetadata(ref input, context);
Properties = ReadTypeProperties(ref input, context, propetiesLength);
Expand Down
14 changes: 7 additions & 7 deletions Shockky/Resources/Cast/Properties/BitmapCastProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ public sealed class BitmapCastProperties : IMemberProperties

public BitmapCastProperties(ref ShockwaveReader input, ReaderContext context)
{
Stride = input.ReadUInt16LittleEndian();
Rectangle = input.ReadRect();
Stride = input.ReadUInt16BigEndian();
Rectangle = input.ReadRectBigEndian();
//TODO: if version < 0x700 ? i16 : i8 i8
AlphaThreshold = input.ReadByte();
input.ReadByte();
OLE = input.ReadBytes(6).ToArray();
RegistrationPoint = input.ReadPoint();
RegistrationPoint = input.ReadPointBigEndian();
Flags = (BitmapFlags)input.ReadByte();

if ((Stride & 0x8000u) != 0)
{
Stride &= 0x3FFF;

BitDepth = input.ReadByte(); //if size == 26: i16 to u8 else u8 - src: eq-rs
PaletteRef = new CastMemberId(input.ReadInt16LittleEndian(), input.ReadInt16LittleEndian()); //is_v5 ? new CastMemberId(input.ReadInt16(), input.ReadInt16()); : new CastMemberId(-1, input.ReadInt16());
PaletteRef = new CastMemberId(input.ReadInt16BigEndian(), input.ReadInt16BigEndian()); //is_v5 ? new CastMemberId(input.ReadInt16(), input.ReadInt16()); : new CastMemberId(-1, input.ReadInt16());
}
}

Expand Down Expand Up @@ -61,17 +61,17 @@ public void WriteTo(ShockwaveWriter output, WriterOptions options)
if (BitDepth != 1)
Stride |= 0x8000;

output.WriteUInt16LittleEndian(Stride);
output.WriteUInt16BigEndian(Stride);

output.WriteRect(Rectangle);
output.WriteByte(AlphaThreshold);
output.WriteByte((byte)0);
output.WriteByte(0);
output.WriteBytes(OLE);
output.WritePoint(RegistrationPoint);
output.WriteByte((byte)Flags);

if (BitDepth == 1) return;
output.WriteByte(BitDepth);
output.WriteMemberId(PaletteRef);
output.WriteMemberIdBigEndian(PaletteRef);
}
}
4 changes: 2 additions & 2 deletions Shockky/Resources/Cast/Properties/ButtonCastProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public ButtonCastProperties()
public ButtonCastProperties(ref ShockwaveReader input, ReaderContext context)
: base(ref input, context)
{
ButtonType = (ButtonType)input.ReadInt16LittleEndian();
ButtonType = (ButtonType)input.ReadInt16BigEndian();
}

public new int GetBodySize(WriterOptions options)
Expand All @@ -25,6 +25,6 @@ public ButtonCastProperties(ref ShockwaveReader input, ReaderContext context)
public new void WriteTo(ShockwaveWriter output, WriterOptions options)
{
base.WriteTo(output, options);
output.WriteInt16LittleEndian((short)ButtonType);
output.WriteInt16BigEndian((short)ButtonType);
}
}
8 changes: 4 additions & 4 deletions Shockky/Resources/Cast/Properties/FilmLoopCastProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public FilmLoopCastProperties()
{ }
public FilmLoopCastProperties(ref ShockwaveReader input)
{
Rectangle = input.ReadRect();
Flags = (FilmLoopFlags)input.ReadInt32LittleEndian();
short unk14 = input.ReadInt16LittleEndian();
Rectangle = input.ReadRectBigEndian();
Flags = (FilmLoopFlags)input.ReadInt32BigEndian();
short unk14 = input.ReadInt16BigEndian();
}

public int GetBodySize(WriterOptions options)
Expand All @@ -30,6 +30,6 @@ public int GetBodySize(WriterOptions options)
public void WriteTo(ShockwaveWriter output, WriterOptions options)
{
output.WriteRect(Rectangle);
output.WriteInt32LittleEndian((int)Flags);
output.WriteInt32BigEndian((int)Flags);
}
}
10 changes: 5 additions & 5 deletions Shockky/Resources/Cast/Properties/RichTextCastProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ public class RichTextCastProperties : IMemberProperties

public RichTextCastProperties(ref ShockwaveReader input)
{
Rectangle = input.ReadRect();
Rect2 = input.ReadRect();
Rectangle = input.ReadRectBigEndian();
Rect2 = input.ReadRectBigEndian();
AntiAlias = input.ReadBoolean();
BoxType = (RichTextBoxType)input.ReadByte();

Unk12 = input.ReadInt16LittleEndian();
Unk12 = input.ReadInt16BigEndian();
AntiAliasMinFontSize = (SizeType)input.ReadByte();
Height = (SizeType)input.ReadByte();

//TODO: Rgb24
ForegroundColor = Color.FromArgb(input.ReadInt32LittleEndian());
BackgroundColor = Color.FromArgb(input.ReadInt16LittleEndian(), input.ReadInt16LittleEndian(), input.ReadInt16LittleEndian());
ForegroundColor = Color.FromArgb(input.ReadInt32BigEndian());
BackgroundColor = Color.FromArgb(input.ReadInt16BigEndian(), input.ReadInt16BigEndian(), input.ReadInt16BigEndian());
}
public int GetBodySize(WriterOptions options)
{
Expand Down
4 changes: 2 additions & 2 deletions Shockky/Resources/Cast/Properties/ScriptCastProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public ScriptCastProperties()
{ }
public ScriptCastProperties(ref ShockwaveReader input, ReaderContext context)
{
Kind = (ScriptKind)input.ReadInt16LittleEndian();
Kind = (ScriptKind)input.ReadInt16BigEndian();
}

public int GetBodySize(WriterOptions options)
Expand All @@ -22,6 +22,6 @@ public int GetBodySize(WriterOptions options)

public void WriteTo(ShockwaveWriter output, WriterOptions options)
{
output.WriteInt16LittleEndian((short)Kind);
output.WriteInt16BigEndian((short)Kind);
}
}
10 changes: 5 additions & 5 deletions Shockky/Resources/Cast/Properties/ShapeCastProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public ShapeCastProperties()
{ }
public ShapeCastProperties(ref ShockwaveReader input, ReaderContext context)
{
Type = (ShapeType)input.ReadInt16LittleEndian();
Rectangle = input.ReadRect();
Type = (ShapeType)input.ReadInt16BigEndian();
Rectangle = input.ReadRectBigEndian();

Pattern = input.ReadInt16LittleEndian();
Pattern = input.ReadInt16BigEndian();
ForegroundColor = input.ReadByte();
BackgroundColor = input.ReadByte();

Expand Down Expand Up @@ -55,10 +55,10 @@ public int GetBodySize(WriterOptions options)

public void WriteTo(ShockwaveWriter output, WriterOptions options)
{
output.WriteInt16LittleEndian((short)Type);
output.WriteInt16BigEndian((short)Type);
output.WriteRect(Rectangle);

output.WriteInt16LittleEndian(Pattern);
output.WriteInt16BigEndian(Pattern);
output.WriteByte(ForegroundColor);
output.WriteByte(BackgroundColor);

Expand Down
14 changes: 7 additions & 7 deletions Shockky/Resources/Cast/Properties/TextCastProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public TextCastProperties(ref ShockwaveReader input, ReaderContext context)
BoxShadowSize = (SizeType)input.ReadByte();
BoxType = (TextBoxType)input.ReadByte();

Alignment = (TextAlignment)input.ReadInt16LittleEndian();
Alignment = (TextAlignment)input.ReadInt16BigEndian();
BackgroundColor = input.ReadColor();
Font = input.ReadInt16LittleEndian();
Rectangle = input.ReadRect();
LineHeight = input.ReadInt16LittleEndian();
Font = input.ReadInt16BigEndian();
Rectangle = input.ReadRectBigEndian();
LineHeight = input.ReadInt16BigEndian();

TextShadowSize = (SizeType)input.ReadByte();
Flags = (TextFlags)input.ReadByte();
Expand Down Expand Up @@ -66,12 +66,12 @@ public void WriteTo(ShockwaveWriter output, WriterOptions options)
output.WriteByte((byte)BoxShadowSize);
output.WriteByte((byte)BoxType);

output.WriteInt16LittleEndian((short)Alignment);
output.WriteInt16BigEndian((short)Alignment);
output.WriteColor(BackgroundColor);

output.WriteInt16LittleEndian(Font);
output.WriteInt16BigEndian(Font);
output.WriteRect(Rectangle);
output.WriteInt16LittleEndian(LineHeight);
output.WriteInt16BigEndian(LineHeight);

output.WriteByte((byte)TextShadowSize);
output.WriteByte((byte)Flags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public TransitionCastProperties(ref ShockwaveReader input, ReaderContext context
ChunkSize = input.ReadByte();
Type = (TransitionType)input.ReadByte();
Flags = (TransitionFlags)input.ReadByte();
DurationInMilliseconds = input.ReadInt16LittleEndian(); //TODO: Not in < D5
DurationInMilliseconds = input.ReadInt16BigEndian(); //TODO: Not in < D5

if (!Flags.HasFlag(TransitionFlags.Standard))
Xtra = new XtraCastProperties(ref input, context);
Expand Down
2 changes: 1 addition & 1 deletion Shockky/Resources/Cast/Properties/VideoCastProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public VideoCastProperties(ref ShockwaveReader input, ReaderContext context)
input.Advance(3);
Framerate = input.ReadByte();
input.Advance(32);
Rectangle = input.ReadRect();
Rectangle = input.ReadRectLittleEndian();
}

public int GetBodySize(WriterOptions options)
Expand Down
4 changes: 2 additions & 2 deletions Shockky/Resources/Cast/Properties/XtraCastProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public int GetBodySize(WriterOptions options)
public void WriteTo(ShockwaveWriter output, WriterOptions options)
{
throw new NotImplementedException(nameof(XtraCastProperties));
output.WriteInt32LittleEndian(SymbolName.Length);
output.WriteInt32LittleEndian(Data.Length);
output.WriteInt32BigEndian(SymbolName.Length);
output.WriteInt32BigEndian(Data.Length);
output.WriteBytes(Data);
}
}
Loading

0 comments on commit 7cbfdea

Please sign in to comment.