diff --git a/Shockky.Sandbox/Program.cs b/Shockky.Sandbox/Program.cs index d44be1d..e00591b 100644 --- a/Shockky.Sandbox/Program.cs +++ b/Shockky.Sandbox/Program.cs @@ -6,7 +6,6 @@ using Shockky.Resources.Types; using System.CommandLine; - using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; @@ -20,17 +19,19 @@ }.ExistingOnly(); var outputOption = new Option("--output", - getDefaultValue: () => new DirectoryInfo("Output/"), + getDefaultValue: () => new DirectoryInfo("Output"), description: "Directory for the extracted resources") .LegalFilePathsOnly(); +outputOption.AddAlias("-o"); + var rootCommand = new RootCommand() { inputArgument, outputOption }; -rootCommand.SetHandler(HandleExtractCommand, +rootCommand.SetHandler(HandleExtractCommand, inputArgument, outputOption); return rootCommand.Invoke(args); @@ -54,11 +55,10 @@ static System.Drawing.Color[] ReadPalette(string fileName) System.Drawing.Color[] colors = new System.Drawing.Color[input.ReadInt16()]; for (int i = 0; i < colors.Length; i++) { - byte r = input.ReadByte(); - byte g = input.ReadByte(); - byte b = input.ReadByte(); - - colors[i] = System.Drawing.Color.FromArgb(r, g, b); + colors[i] = System.Drawing.Color.FromArgb( + red: input.ReadByte(), + green: input.ReadByte(), + blue: input.ReadByte()); input.ReadByte(); } @@ -95,7 +95,7 @@ static void HandleExtractCommand(IEnumerable input, Director Console.Write($"Disassembling file \"{file.Name}\".."); - var shockwaveFile = ShockwaveFile.Load(file.FullName); + var shockwaveFile = ShockwaveFile.Read(file.FullName); if (shockwaveFile.Resources.Values .FirstOrDefault(c => c.Kind == OsType.KEYPtr) is not KeyMap associationTable) diff --git a/Shockky/IO/ShockwaveReader.cs b/Shockky/IO/ShockwaveReader.cs index c8f96cb..2813c1e 100644 --- a/Shockky/IO/ShockwaveReader.cs +++ b/Shockky/IO/ShockwaveReader.cs @@ -76,7 +76,7 @@ public short ReadInt16BigEndian() Advance(sizeof(short)); return ReverseEndianness ? - value : BinaryPrimitives.ReverseEndianness(value); + BinaryPrimitives.ReverseEndianness(value) : value; } public ushort ReadUInt16LittleEndian() @@ -93,7 +93,7 @@ public ushort ReadUInt16BigEndian() Advance(sizeof(ushort)); return ReverseEndianness ? - value : BinaryPrimitives.ReverseEndianness(value); + BinaryPrimitives.ReverseEndianness(value) : value; } public int ReadInt32LittleEndian() @@ -110,7 +110,7 @@ public int ReadInt32BigEndian() Advance(sizeof(int)); return ReverseEndianness ? - value : BinaryPrimitives.ReverseEndianness(value); + BinaryPrimitives.ReverseEndianness(value) : value; } public uint ReadUInt32LittleEndian() @@ -130,7 +130,7 @@ public uint ReadUInt32BigEndian() value : BinaryPrimitives.ReverseEndianness(value); } - public ulong ReadUInt64() + public ulong ReadUInt64LittleEndian() { ulong value = BinaryPrimitives.ReadUInt64LittleEndian(CurrentSpan); Advance(sizeof(ulong)); @@ -138,13 +138,13 @@ public ulong ReadUInt64() return ReverseEndianness ? BinaryPrimitives.ReverseEndianness(value) : value; } - public ulong ReadBEUInt64() + public ulong ReadUInt64BigEndian() { ulong value = BinaryPrimitives.ReadUInt64BigEndian(CurrentSpan); Advance(sizeof(ulong)); return ReverseEndianness ? - value : BinaryPrimitives.ReverseEndianness(value); + BinaryPrimitives.ReverseEndianness(value) : value; } public double ReadDoubleLittleEndian() { @@ -155,7 +155,7 @@ public double ReadDoubleLittleEndian() Advance(sizeof(double)); return value; } - public double ReadBEDouble() + public double ReadDoubleBigEndian() { double value = ReverseEndianness ? BinaryPrimitives.ReadDoubleLittleEndian(CurrentSpan) : diff --git a/Shockky/IO/ShockwaveWriter.cs b/Shockky/IO/ShockwaveWriter.cs index d883138..2fa5eb1 100644 --- a/Shockky/IO/ShockwaveWriter.cs +++ b/Shockky/IO/ShockwaveWriter.cs @@ -168,6 +168,7 @@ public void Write7BitEncodedUInt(uint value) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetVarIntSize(int value) => GetVarUIntSize((uint)value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetVarUIntSize(uint value) { @@ -179,6 +180,10 @@ public static int GetVarUIntSize(uint value) return (x * 37) >> 8; } + /// + /// Writes length-prefixed UTF-8 string. + /// + /// The UTF-8 string to write. public void WriteString(ReadOnlySpan value) { Write7BitEncodedUInt((uint)value.Length); @@ -186,6 +191,11 @@ public void WriteString(ReadOnlySpan value) int len = Encoding.UTF8.GetBytes(value, _data.Slice(_position)); _position += len; } + + /// + /// Writes a null-terminated UTF-8 string. + /// + /// The UTF-8 string to write. public void WriteCString(ReadOnlySpan value) { int len = Encoding.UTF8.GetBytes(value, _data.Slice(_position)); @@ -210,6 +220,8 @@ public void WriteColor(byte r, byte g, byte b) Advance(6); } + + // TODO: Endianness public void WritePoint(Point value) { WriteInt16LittleEndian((short)value.X); diff --git a/Shockky/Lingo/LingoFunction.cs b/Shockky/Lingo/LingoFunction.cs index e0fafaa..5bd5011 100644 --- a/Shockky/Lingo/LingoFunction.cs +++ b/Shockky/Lingo/LingoFunction.cs @@ -4,13 +4,17 @@ namespace Shockky.Lingo; public class LingoFunction : IShockwaveItem { - public byte[] Bytecode { get; set; } - public List Arguments { get; } - public List Locals { get; } - public byte[] BytesPerLine { get; } - public short EnvironmentIndex { get; set; } - public LingoEventFlags EventKind { get; } + public LingoEventFlags EventFlags { get; set; } + + public byte[] Bytecode { get; set; } + public List Arguments { get; set; } + public List Locals { get; set; } + public List Globals { get; set; } + public byte[] BytesPerLine { get; set; } + + public int ParserBytesRead { get; set; } + public int BodyLineNumber { get; set; } public int StackHeight { get; set; } @@ -19,34 +23,36 @@ public LingoFunction() Bytecode = []; Arguments = new List(); Locals = new List(); + Globals = new List(); BytesPerLine = []; } public LingoFunction(ref ShockwaveReader input) { - EnvironmentIndex = input.ReadInt16LittleEndian(); - EventKind = (LingoEventFlags)input.ReadInt16LittleEndian(); + EnvironmentIndex = input.ReadInt16BigEndian(); + EventFlags = (LingoEventFlags)input.ReadInt16BigEndian(); - Bytecode = new byte[input.ReadInt32LittleEndian()]; - int bytecodeOffset = input.ReadInt32LittleEndian(); + Bytecode = new byte[input.ReadInt32BigEndian()]; + int bytecodeOffset = input.ReadInt32BigEndian(); - Arguments = new List(input.ReadInt16LittleEndian()); - int argumentsOffset = input.ReadInt32LittleEndian(); + Arguments = new List(input.ReadInt16BigEndian()); + int argumentsOffset = input.ReadInt32BigEndian(); - Locals = new List(input.ReadInt16LittleEndian()); - int localsOffset = input.ReadInt32LittleEndian(); + Locals = new List(input.ReadInt16BigEndian()); + int localsOffset = input.ReadInt32BigEndian(); - short globalsCount = input.ReadInt16LittleEndian(); //v5 - int globalsOffset = input.ReadInt32LittleEndian(); //v5 + Globals = new List(input.ReadInt16BigEndian()); //v5 + int globalsOffset = input.ReadInt32BigEndian(); //v5 - int parserBytesRead = input.ReadInt32LittleEndian(); - short bodyLineNumber = input.ReadInt16LittleEndian(); + ParserBytesRead = input.ReadInt32BigEndian(); + BodyLineNumber = input.ReadInt16BigEndian(); - BytesPerLine = new byte[input.ReadInt16LittleEndian()]; - int lineOffset = input.ReadInt32LittleEndian(); + BytesPerLine = new byte[input.ReadInt16BigEndian()]; + int lineOffset = input.ReadInt32BigEndian(); //if version > 0x800 - StackHeight = input.ReadInt32LittleEndian(); + StackHeight = input.ReadInt32BigEndian(); + // TODO: Seperate "data" reading from handler body int bodyEndOffset = input.Position; input.Position = bytecodeOffset; @@ -55,15 +61,21 @@ public LingoFunction(ref ShockwaveReader input) input.Position = argumentsOffset; for (int i = 0; i < Arguments.Capacity; i++) { - Arguments.Add(input.ReadInt16LittleEndian()); + Arguments.Add(input.ReadInt16BigEndian()); } input.Position = localsOffset; for (int i = 0; i < Locals.Capacity; i++) { - Locals.Add(input.ReadInt16LittleEndian()); + Locals.Add(input.ReadInt16BigEndian()); } + input.Position = globalsOffset; + for (int i = 0; i < Globals.Capacity; i++) + { + Globals.Add(input.ReadInt16BigEndian()); + } + input.Position = lineOffset; for (int i = 0; i < BytesPerLine.Length; i++) { @@ -97,8 +109,16 @@ public int GetBodySize(WriterOptions options) public void WriteTo(ShockwaveWriter output, WriterOptions options) { - throw new NotImplementedException(); - output.WriteInt16LittleEndian(EnvironmentIndex); - output.WriteInt16LittleEndian((short)EventKind); + output.WriteInt16BigEndian(EnvironmentIndex); + output.WriteUInt16BigEndian((ushort)EventFlags); + output.WriteUInt16BigEndian((ushort)Bytecode.Length); + output.WriteUInt16BigEndian(0); // Reserve + output.WriteUInt16BigEndian((ushort)Arguments.Count); + output.WriteInt32BigEndian(0); // Reserve + output.WriteUInt16BigEndian((ushort)Locals.Count); + output.WriteInt32BigEndian(0); // Reserve + output.WriteUInt16BigEndian((ushort)Globals.Count); + output.WriteInt32BigEndian(0); // Reserve + output.WriteUInt16BigEndian((ushort)BytesPerLine.Length); } } diff --git a/Shockky/Lingo/LingoLiteral.cs b/Shockky/Lingo/LingoLiteral.cs index 1a549b4..9429f81 100644 --- a/Shockky/Lingo/LingoLiteral.cs +++ b/Shockky/Lingo/LingoLiteral.cs @@ -48,11 +48,11 @@ public static LingoLiteral Read(ref ShockwaveReader input, VariantKind entryKind { input.Position = entryOffset; - int length = input.ReadInt32LittleEndian(); + int length = input.ReadInt32BigEndian(); object value = entryKind switch { VariantKind.String => input.ReadString(length), - VariantKind.Float when length == 8 => input.ReadDoubleLittleEndian(), + VariantKind.Float when length == 8 => input.ReadDoubleBigEndian(), VariantKind.Float when length == 10 => throw new NotSupportedException("The 80-bit double-extended precision is not supported, yet."), VariantKind.CompiledJavascript => input.ReadBytes(length).ToArray(), diff --git a/Shockky/Resources/AfterBurner/AfterburnerMapEntry.cs b/Shockky/Resources/AfterBurner/AfterburnerMapEntry.cs index 1500b77..f3c16a7 100644 --- a/Shockky/Resources/AfterBurner/AfterburnerMapEntry.cs +++ b/Shockky/Resources/AfterBurner/AfterburnerMapEntry.cs @@ -25,7 +25,7 @@ public AfterburnerMapEntry(ZLibShockwaveReader input) Length = input.Read7BitEncodedInt(); DecompressedLength = input.Read7BitEncodedInt(); CompressionTypeIndex = input.Read7BitEncodedInt(); - Kind = (OsType)input.ReadInt32LittleEndian(); + Kind = (OsType)input.ReadInt32BigEndian(); } public int GetBodySize(WriterOptions options) diff --git a/Shockky/Resources/AfterBurner/FileCompressionTypes.cs b/Shockky/Resources/AfterBurner/FileCompressionTypes.cs index 88c4527..c4e4999 100644 --- a/Shockky/Resources/AfterBurner/FileCompressionTypes.cs +++ b/Shockky/Resources/AfterBurner/FileCompressionTypes.cs @@ -14,7 +14,7 @@ public FileCompressionTypes(ref ShockwaveReader input, ReaderContext context) { using ZLibShockwaveReader decompressedInput = ZLib.CreateDeflateReaderUnsafe(ref input); - int compressionTypeCount = decompressedInput.ReadInt16LittleEndian(); + int compressionTypeCount = decompressedInput.ReadInt16BigEndian(); Guid[] ids = new Guid[compressionTypeCount]; for (int i = 0; i < ids.Length; i++) diff --git a/Shockky/Resources/Config.cs b/Shockky/Resources/Config.cs index ff81b3e..f2f93e4 100644 --- a/Shockky/Resources/Config.cs +++ b/Shockky/Resources/Config.cs @@ -94,7 +94,7 @@ public Config(ref ShockwaveReader input, ReaderContext context) OriginalVersion = (DirectorVersion)input.ReadInt16LittleEndian(); MaxCastColorDepth = input.ReadInt16LittleEndian(); Flags = (ConfigFlags)input.ReadInt32LittleEndian(); - ScoreUsedChannelsMask = input.ReadUInt64(); + ScoreUsedChannelsMask = input.ReadUInt64LittleEndian(); Trial = input.ReadBoolean(); Field34 = input.ReadByte(); diff --git a/Shockky/Resources/Enum/OsType.cs b/Shockky/Resources/Enum/OsType.cs index cab0fa3..7d207b6 100644 --- a/Shockky/Resources/Enum/OsType.cs +++ b/Shockky/Resources/Enum/OsType.cs @@ -1,7 +1,7 @@ namespace Shockky.Resources; /// -/// Represents all the resource types that Shockwave movies/casts can contain. +/// Represents the resource types that Director authored Shockwave movies/casts can contain. /// /// See: https://en.wikipedia.org/wiki/FourCC /// diff --git a/Shockky/Resources/FileMetadata.cs b/Shockky/Resources/FileMetadata.cs index 56f485c..fe601bd 100644 --- a/Shockky/Resources/FileMetadata.cs +++ b/Shockky/Resources/FileMetadata.cs @@ -8,12 +8,12 @@ public class FileMetadata public CodecKind Codec { get; set; } public int FileLength { get; } - public bool IsBigEndian => Kind == OsType.XFIR; + public bool IsLittleEndian => Kind == OsType.XFIR; public FileMetadata(ref ShockwaveReader input) { Kind = (OsType)input.ReadInt32BigEndian(); - FileLength = IsBigEndian ? input.ReadInt32LittleEndian() : input.ReadInt32BigEndian(); - Codec = (CodecKind)(IsBigEndian ? input.ReadInt32LittleEndian() : input.ReadInt32BigEndian()); + FileLength = IsLittleEndian ? input.ReadInt32LittleEndian() : input.ReadInt32BigEndian(); + Codec = (CodecKind)(IsLittleEndian ? input.ReadInt32LittleEndian() : input.ReadInt32BigEndian()); } } diff --git a/Shockky/Resources/Lingo/LingoContext.cs b/Shockky/Resources/Lingo/LingoContext.cs index 417d607..d74b158 100644 --- a/Shockky/Resources/Lingo/LingoContext.cs +++ b/Shockky/Resources/Lingo/LingoContext.cs @@ -27,33 +27,33 @@ public LingoContext() } public LingoContext(ref ShockwaveReader input, ReaderContext context) { - input.ReverseEndianness = true; + input.ReverseEndianness = false; - input.ReadInt32LittleEndian(); - input.ReadInt32LittleEndian(); + input.ReadInt32BigEndian(); + input.ReadInt32BigEndian(); - Items = new List(input.ReadInt32LittleEndian()); - input.ReadInt32LittleEndian(); + Items = new List(input.ReadInt32BigEndian()); + input.ReadInt32BigEndian(); - input.ReadInt16LittleEndian(); - input.ReadInt16LittleEndian(); + input.ReadInt16BigEndian(); + input.ReadInt16BigEndian(); - input.ReadInt32LittleEndian(); - Type = input.ReadInt32LittleEndian(); //TODO: ?? + int unk4 = input.ReadInt32BigEndian(); + Type = input.ReadInt32BigEndian(); //TODO: ?? - ValuesChunkIndex = input.ReadInt32LittleEndian(); - NameChunkIndex = input.ReadInt32LittleEndian(); + ValuesChunkIndex = input.ReadInt32BigEndian(); + NameChunkIndex = input.ReadInt32BigEndian(); - ValidCount = input.ReadInt16LittleEndian(); - Flags = (LingoContextFlags)input.ReadInt16LittleEndian(); - FreeChunkIndex = input.ReadInt16LittleEndian(); + ValidCount = input.ReadInt16BigEndian(); + Flags = (LingoContextFlags)input.ReadInt16BigEndian(); + FreeChunkIndex = input.ReadInt16BigEndian(); - input.ReadInt16LittleEndian(); - input.ReadInt16LittleEndian(); //EnvIndex some_parent_maybe_index + input.ReadInt16BigEndian(); + input.ReadInt16BigEndian(); //EnvIndex some_parent_maybe_index for (int i = 0; i < EVENT_COUNT; i++) { - EventHandlerNames[i] = input.ReadInt16LittleEndian(); + EventHandlerNames[i] = input.ReadInt16BigEndian(); } for (int i = 0; i < Items.Capacity; i++) @@ -89,30 +89,30 @@ public void WriteTo(ShockwaveWriter output, WriterOptions options) { const short ENTRY_OFFSET = 96; - output.WriteInt32LittleEndian(0); - output.WriteInt32LittleEndian(0); - output.WriteInt32LittleEndian(Items.Count); - output.WriteInt32LittleEndian(Items.Count); + output.WriteInt32BigEndian(0); + output.WriteInt32BigEndian(0); + output.WriteInt32BigEndian(Items.Count); + output.WriteInt32BigEndian(Items.Count); - output.WriteInt16LittleEndian(ENTRY_OFFSET); - output.WriteInt16LittleEndian(SECTION_SIZE); + output.WriteInt16BigEndian(ENTRY_OFFSET); + output.WriteInt16BigEndian(SECTION_SIZE); - output.WriteInt32LittleEndian(0); - output.WriteInt32LittleEndian(Type); + output.WriteInt32BigEndian(0); + output.WriteInt32BigEndian(Type); - output.WriteInt32LittleEndian(ValuesChunkIndex); - output.WriteInt32LittleEndian(NameChunkIndex); + output.WriteInt32BigEndian(ValuesChunkIndex); + output.WriteInt32BigEndian(NameChunkIndex); - output.WriteInt16LittleEndian(ValidCount); - output.WriteInt16LittleEndian((short)Flags); - output.WriteInt16LittleEndian(FreeChunkIndex); + output.WriteInt16BigEndian(ValidCount); + output.WriteInt16BigEndian((short)Flags); + output.WriteInt16BigEndian(FreeChunkIndex); - output.WriteInt16LittleEndian((short)-1); - output.WriteInt16LittleEndian((short)-1); + output.WriteInt16BigEndian(-1); + output.WriteInt16BigEndian(-1); for (int i = 0; i < EVENT_COUNT; i++) { - output.WriteInt16LittleEndian(EventHandlerNames[i]); + output.WriteInt16BigEndian(EventHandlerNames[i]); } foreach (LingoContextItem section in Items) diff --git a/Shockky/Resources/Lingo/LingoContextItem.cs b/Shockky/Resources/Lingo/LingoContextItem.cs index 304d6e2..4098340 100644 --- a/Shockky/Resources/Lingo/LingoContextItem.cs +++ b/Shockky/Resources/Lingo/LingoContextItem.cs @@ -16,10 +16,10 @@ public LingoContextItem() { } public LingoContextItem(ref ShockwaveReader input) { - input.ReadInt32LittleEndian(); - ChunkIndex = input.ReadInt32LittleEndian(); - Flags = (LingoContextItemFlags)input.ReadInt16LittleEndian(); - Link = input.ReadInt16LittleEndian(); + input.ReadInt32BigEndian(); + ChunkIndex = input.ReadInt32BigEndian(); + Flags = (LingoContextItemFlags)input.ReadInt16BigEndian(); + Link = input.ReadInt16BigEndian(); } public int GetBodySize(WriterOptions options) @@ -34,9 +34,9 @@ public int GetBodySize(WriterOptions options) public void WriteTo(ShockwaveWriter output, WriterOptions options) { - output.WriteInt32LittleEndian(0); - output.WriteInt32LittleEndian(ChunkIndex); - output.WriteInt16LittleEndian((short)Flags); - output.WriteInt16LittleEndian(Link); + output.WriteInt32BigEndian(0); + output.WriteInt32BigEndian(ChunkIndex); + output.WriteInt16BigEndian((short)Flags); + output.WriteInt16BigEndian(Link); } } diff --git a/Shockky/Resources/Lingo/LingoNames.cs b/Shockky/Resources/Lingo/LingoNames.cs index 9908fe6..bdd58aa 100644 --- a/Shockky/Resources/Lingo/LingoNames.cs +++ b/Shockky/Resources/Lingo/LingoNames.cs @@ -12,15 +12,15 @@ public LingoNames() { } public LingoNames(ref ShockwaveReader input, ReaderContext context) { - input.ReverseEndianness = true; + input.ReverseEndianness = false; - input.ReadInt32LittleEndian(); - input.ReadInt32LittleEndian(); - input.ReadInt32LittleEndian(); - input.ReadInt32LittleEndian(); + input.ReadInt32BigEndian(); + input.ReadInt32BigEndian(); + input.ReadInt32BigEndian(); + input.ReadInt32BigEndian(); - short nameOffset = input.ReadInt16LittleEndian(); - Names = new List(input.ReadInt16LittleEndian()); + short nameOffset = input.ReadInt16BigEndian(); + Names = new List(input.ReadInt16BigEndian()); input.Position = nameOffset; for (int i = 0; i < Names.Capacity; i++) @@ -47,12 +47,12 @@ public void WriteTo(ShockwaveWriter output, WriterOptions options) const short NAME_OFFSET = 20; int namesLength = Names?.Sum(static n => sizeof(byte) + n.Length) ?? 0; - output.WriteInt32LittleEndian(0); - output.WriteInt32LittleEndian(0); - output.WriteInt32LittleEndian(namesLength); - output.WriteInt32LittleEndian(namesLength); - output.WriteInt16LittleEndian(NAME_OFFSET); - output.WriteInt16LittleEndian((short)Names.Count); + output.WriteInt32BigEndian(0); + output.WriteInt32BigEndian(0); + output.WriteInt32BigEndian(namesLength); + output.WriteInt32BigEndian(namesLength); + output.WriteInt16BigEndian(NAME_OFFSET); + output.WriteInt16BigEndian((short)Names.Count); foreach (string name in Names) { diff --git a/Shockky/Resources/Lingo/LingoScript.cs b/Shockky/Resources/Lingo/LingoScript.cs index 30d277e..d39a650 100644 --- a/Shockky/Resources/Lingo/LingoScript.cs +++ b/Shockky/Resources/Lingo/LingoScript.cs @@ -56,62 +56,62 @@ public LingoScript() } public LingoScript(ref ShockwaveReader input) { - input.ReverseEndianness = true; + input.ReverseEndianness = false; - input.ReadInt32LittleEndian(); - input.ReadInt32LittleEndian(); + input.ReadInt32BigEndian(); + input.ReadInt32BigEndian(); - input.ReadInt32LittleEndian(); - input.ReadInt32LittleEndian(); + input.ReadInt32BigEndian(); + input.ReadInt32BigEndian(); - input.ReadInt16LittleEndian(); + input.ReadInt16BigEndian(); - ContextIndex = input.ReadInt16LittleEndian(); - EnvironmentIndex = input.ReadInt16LittleEndian(); - ParentContextIndex = input.ReadInt16LittleEndian(); - short environmentFactoryIndexGarbageDebug = input.ReadInt16LittleEndian(); + ContextIndex = input.ReadInt16BigEndian(); + EnvironmentIndex = input.ReadInt16BigEndian(); + ParentContextIndex = input.ReadInt16BigEndian(); + short environmentFactoryIndexGarbageDebug = input.ReadInt16BigEndian(); - input.ReadInt32LittleEndian(); - input.ReadInt32LittleEndian(); - input.ReadInt32LittleEndian(); + input.ReadInt32BigEndian(); + input.ReadInt32BigEndian(); + input.ReadInt32BigEndian(); - Flags = (LingoScriptFlags)input.ReadInt32LittleEndian(); + Flags = (LingoScriptFlags)input.ReadInt32BigEndian(); - input.ReadInt32LittleEndian(); - CastMemberId = input.ReadInt16LittleEndian(); + input.ReadInt32BigEndian(); + CastMemberId = input.ReadInt16BigEndian(); - FactoryNameIndex = input.ReadInt16LittleEndian(); + FactoryNameIndex = input.ReadInt16BigEndian(); - EventHandlerIndices = new List(input.ReadInt16LittleEndian()); - int eventHandlerIndexOffset = input.ReadInt32LittleEndian(); + EventHandlerIndices = new List(input.ReadInt16BigEndian()); + int eventHandlerIndexOffset = input.ReadInt32BigEndian(); - EventFlags = (LingoEventFlags)input.ReadInt32LittleEndian(); + EventFlags = (LingoEventFlags)input.ReadInt32BigEndian(); - Properties = new List(input.ReadInt16LittleEndian()); - int propertiesOffset = input.ReadInt32LittleEndian(); + Properties = new List(input.ReadInt16BigEndian()); + int propertiesOffset = input.ReadInt32BigEndian(); - Globals = new List(input.ReadInt16LittleEndian()); - int globalsOffset = input.ReadInt32LittleEndian(); + Globals = new List(input.ReadInt16BigEndian()); + int globalsOffset = input.ReadInt32BigEndian(); - Functions = new List(input.ReadInt16LittleEndian()); - int functionsOffset = input.ReadInt32LittleEndian(); + Functions = new List(input.ReadInt16BigEndian()); + int functionsOffset = input.ReadInt32BigEndian(); - Literals = new List(input.ReadInt16LittleEndian()); - int literalsOffset = input.ReadInt32LittleEndian(); + Literals = new List(input.ReadInt16BigEndian()); + int literalsOffset = input.ReadInt32BigEndian(); - int literalDataLength = input.ReadInt32LittleEndian(); - int literalDataOffset = input.ReadInt32LittleEndian(); + int literalDataLength = input.ReadInt32BigEndian(); + int literalDataOffset = input.ReadInt32BigEndian(); input.Position = propertiesOffset; for (int i = 0; i < Properties.Capacity; i++) { - Properties.Add(input.ReadInt16LittleEndian()); + Properties.Add(input.ReadInt16BigEndian()); } input.Position = globalsOffset; for (int i = 0; i < Globals.Capacity; i++) { - Globals.Add(input.ReadInt16LittleEndian()); + Globals.Add(input.ReadInt16BigEndian()); } input.Position = functionsOffset; @@ -124,8 +124,8 @@ public LingoScript(ref ShockwaveReader input) var literalEntries = new (VariantKind Kind, int Offset)[Literals.Capacity]; // TODO: Stackalloc/ArrayPool for (int i = 0; i < Literals.Capacity; i++) { - literalEntries[i].Kind = (VariantKind)input.ReadInt32LittleEndian(); - literalEntries[i].Offset = input.ReadInt32LittleEndian(); + literalEntries[i].Kind = (VariantKind)input.ReadInt32BigEndian(); + literalEntries[i].Offset = input.ReadInt32BigEndian(); } input.Position = literalDataOffset; @@ -141,7 +141,7 @@ public LingoScript(ref ShockwaveReader input) input.Position = eventHandlerIndexOffset; for (int i = 0; i < EventHandlerIndices.Capacity; i++) { - EventHandlerIndices.Add(input.ReadInt16LittleEndian()); + EventHandlerIndices.Add(input.ReadInt16BigEndian()); } } @@ -199,29 +199,29 @@ public int GetBodySize(WriterOptions options) public void WriteTo(ShockwaveWriter output, WriterOptions options) { - output.WriteInt32LittleEndian(0); - output.WriteInt32LittleEndian(0); + output.WriteInt32BigEndian(0); + output.WriteInt32BigEndian(0); int bodySize = GetBodySize(options); - output.WriteInt32LittleEndian(bodySize); - output.WriteInt32LittleEndian(bodySize); + output.WriteInt32BigEndian(bodySize); + output.WriteInt32BigEndian(bodySize); - output.WriteInt32LittleEndian(92); //TODO: + output.WriteInt32BigEndian(92); //TODO: - output.WriteInt16LittleEndian(ContextIndex); - output.WriteInt16LittleEndian(EnvironmentIndex); - output.WriteInt16LittleEndian(ParentContextIndex); - output.WriteInt16LittleEndian((short)-1); + output.WriteInt16BigEndian(ContextIndex); + output.WriteInt16BigEndian(EnvironmentIndex); + output.WriteInt16BigEndian(ParentContextIndex); + output.WriteInt16BigEndian((short)-1); - output.WriteInt32LittleEndian(0); - output.WriteInt32LittleEndian(0); - output.WriteInt32LittleEndian(0); + output.WriteInt32BigEndian(0); + output.WriteInt32BigEndian(0); + output.WriteInt32BigEndian(0); - output.WriteInt32LittleEndian((int)Flags); + output.WriteInt32BigEndian((int)Flags); - output.WriteInt16LittleEndian((short)0); - output.WriteInt16LittleEndian(CastMemberId); + output.WriteInt16BigEndian((short)0); + output.WriteInt16BigEndian(CastMemberId); - output.WriteInt16LittleEndian(FactoryNameIndex); + output.WriteInt16BigEndian(FactoryNameIndex); } } diff --git a/Shockky/Resources/Types/ResourceId.cs b/Shockky/Resources/Types/ResourceId.cs index e713e45..4b9449c 100644 --- a/Shockky/Resources/Types/ResourceId.cs +++ b/Shockky/Resources/Types/ResourceId.cs @@ -1,6 +1,6 @@ namespace Shockky.Resources.Types; /// -/// Represents +/// Represents a /// public readonly record struct ResourceId(OsType Kind, int Id); diff --git a/Shockky/ShockwaveFile.cs b/Shockky/ShockwaveFile.cs index 7718987..432ecfc 100644 --- a/Shockky/ShockwaveFile.cs +++ b/Shockky/ShockwaveFile.cs @@ -24,7 +24,7 @@ public static ShockwaveFile Read(ReadOnlySpan data) var input = new ShockwaveReader(data); file.Metadata = new FileMetadata(ref input); - input.ReverseEndianness = file.Metadata.IsBigEndian; + input.ReverseEndianness = file.Metadata.IsLittleEndian; if (file.Metadata.Codec is CodecKind.FGDM or CodecKind.FGDC) {