Skip to content

Commit

Permalink
Streamlined endian detection in P3DFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Hampo committed Sep 2, 2024
1 parent 34828fb commit 1cfd82a
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions NetP3DLib/NetP3DLib/P3D/P3DFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,22 @@ public P3DFile (string filePath)
fs.Read(signatureBuffer, 0, 4);
uint signature = BitConverter.ToUInt32(signatureBuffer, 0);

var endianness = (signature == SIGNATURE_SWAP || signature == COMPRESSED_SIGNATURE_SWAP) ? BinaryExtensions.SwappedEndian : BinaryExtensions.DefaultEndian;

switch (signature)
{
case SIGNATURE:
br = new(fs, BinaryExtensions.DefaultEndian);
break;
case SIGNATURE_SWAP:
br = new(fs, BinaryExtensions.SwappedEndian);
br = new(fs, endianness);
break;
case COMPRESSED_SIGNATURE:
fs.Position = 0;
using (EndianAwareBinaryReader br2 = new(fs, BinaryExtensions.DefaultEndian))
{
byte[] decryptedBytes = LZR_Compression.DecompressFile(br2);
ms = new(decryptedBytes);
br = new(ms, BinaryExtensions.DefaultEndian);
if (br.ReadUInt32() != SIGNATURE)
throw new InvalidDataException($"Decompressed file signature 0x{signature:X} is invalid.");
}
break;
case COMPRESSED_SIGNATURE_SWAP:
fs.Position = 0;
using (EndianAwareBinaryReader br2 = new(fs, BinaryExtensions.SwappedEndian))
using (EndianAwareBinaryReader br2 = new(fs, endianness))
{
byte[] decryptedBytes = LZR_Compression.DecompressFile(br2);
ms = new(decryptedBytes);
br = new(ms, BinaryExtensions.SwappedEndian);
br = new(ms, endianness);
if (br.ReadUInt32() != SIGNATURE)
throw new InvalidDataException($"Decompressed file signature 0x{signature:X} is invalid.");
}
Expand Down

0 comments on commit 1cfd82a

Please sign in to comment.