2019-01-29 09:50:55 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2019-06-08 03:43:28 +00:00
|
|
|
|
using System.Linq;
|
2022-10-13 02:19:43 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-01-29 09:50:55 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2019-02-01 17:19:20 +00:00
|
|
|
|
namespace RobloxFiles.BinaryFormat
|
2019-01-29 09:50:55 +00:00
|
|
|
|
{
|
2019-05-19 04:44:51 +00:00
|
|
|
|
public class BinaryRobloxFileReader : BinaryReader
|
2019-01-29 09:50:55 +00:00
|
|
|
|
{
|
2019-06-08 03:43:28 +00:00
|
|
|
|
public readonly BinaryRobloxFile File;
|
2020-08-17 05:33:59 +00:00
|
|
|
|
private byte[] lastStringBuffer = Array.Empty<byte>();
|
2019-05-17 06:14:04 +00:00
|
|
|
|
|
2019-06-08 03:43:28 +00:00
|
|
|
|
public BinaryRobloxFileReader(BinaryRobloxFile file, Stream stream) : base(stream)
|
|
|
|
|
{
|
|
|
|
|
File = file;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 02:19:43 +00:00
|
|
|
|
// Reads 'count * sizeof(T)' interleaved bytes
|
|
|
|
|
public T[] ReadInterleaved<T>(int count, Func<byte[], int, T> transform) where T : struct
|
2019-01-29 09:50:55 +00:00
|
|
|
|
{
|
2022-10-13 02:19:43 +00:00
|
|
|
|
int sizeof_T = Marshal.SizeOf<T>();
|
|
|
|
|
int blobSize = count * sizeof_T;
|
2019-01-29 09:50:55 +00:00
|
|
|
|
|
2022-10-13 02:19:43 +00:00
|
|
|
|
var blob = ReadBytes(blobSize);
|
|
|
|
|
var work = new byte[sizeof_T];
|
|
|
|
|
var values = new T[count];
|
2019-01-29 09:50:55 +00:00
|
|
|
|
|
2022-10-13 02:19:43 +00:00
|
|
|
|
for (int offset = 0; offset < count; offset++)
|
2019-01-29 09:50:55 +00:00
|
|
|
|
{
|
2022-10-13 02:19:43 +00:00
|
|
|
|
for (int i = 0; i < sizeof_T; i++)
|
2019-01-29 09:50:55 +00:00
|
|
|
|
{
|
2022-10-13 02:19:43 +00:00
|
|
|
|
int index = (i * count) + offset;
|
|
|
|
|
work[sizeof_T - i - 1] = blob[index];
|
2019-01-29 09:50:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 02:19:43 +00:00
|
|
|
|
values[offset] = transform(work, 0);
|
2019-01-29 09:50:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return values;
|
|
|
|
|
}
|
2019-02-27 06:32:16 +00:00
|
|
|
|
|
2022-10-13 02:19:43 +00:00
|
|
|
|
// Rotates the sign bit of an int32 buffer.
|
|
|
|
|
public int RotateInt32(byte[] buffer, int startIndex)
|
2019-01-29 09:50:55 +00:00
|
|
|
|
{
|
|
|
|
|
int value = BitConverter.ToInt32(buffer, startIndex);
|
2022-10-13 02:19:43 +00:00
|
|
|
|
return (int)((uint)value >> 1) ^ (-(value & 1));
|
2019-01-29 09:50:55 +00:00
|
|
|
|
}
|
2022-10-13 02:19:43 +00:00
|
|
|
|
|
|
|
|
|
// Rotates the sign bit of an int64 buffer.
|
|
|
|
|
public long RotateInt64(byte[] buffer, int startIndex)
|
|
|
|
|
{
|
|
|
|
|
long value = BitConverter.ToInt64(buffer, startIndex);
|
|
|
|
|
return (long)((ulong)value >> 1) ^ (-(value & 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rotates the sign bit of a float buffer.
|
|
|
|
|
public float RotateFloat(byte[] buffer, int startIndex)
|
2019-01-29 09:50:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint u = BitConverter.ToUInt32(buffer, startIndex);
|
|
|
|
|
uint i = (u >> 1) | (u << 31);
|
|
|
|
|
|
|
|
|
|
byte[] b = BitConverter.GetBytes(i);
|
|
|
|
|
return BitConverter.ToSingle(b, 0);
|
|
|
|
|
}
|
2019-02-27 06:32:16 +00:00
|
|
|
|
|
2022-10-13 02:19:43 +00:00
|
|
|
|
// Reads and accumulates an interleaved int32 buffer.
|
2019-06-08 03:43:28 +00:00
|
|
|
|
public List<int> ReadInstanceIds(int count)
|
2019-01-29 09:50:55 +00:00
|
|
|
|
{
|
2022-10-13 02:19:43 +00:00
|
|
|
|
int[] values = ReadInterleaved(count, RotateInt32);
|
2019-01-29 09:50:55 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 1; i < count; ++i)
|
|
|
|
|
values[i] += values[i - 1];
|
|
|
|
|
|
2019-06-08 03:43:28 +00:00
|
|
|
|
return values.ToList();
|
2019-01-29 09:50:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ReadString()
|
|
|
|
|
{
|
|
|
|
|
int length = ReadInt32();
|
|
|
|
|
byte[] buffer = ReadBytes(length);
|
|
|
|
|
|
|
|
|
|
lastStringBuffer = buffer;
|
|
|
|
|
return Encoding.UTF8.GetString(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float ReadFloat()
|
|
|
|
|
{
|
|
|
|
|
return ReadSingle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] GetLastStringBuffer()
|
|
|
|
|
{
|
|
|
|
|
return lastStringBuffer;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-27 06:32:16 +00:00
|
|
|
|
}
|