Small adjustments to BinaryRobloxFileWriter

This commit is contained in:
Max G 2019-07-08 12:03:44 -05:00 committed by GitHub
parent b8a5971f18
commit 506502a732
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,6 +40,16 @@ namespace RobloxFiles.BinaryFormat
ClassMap = new Dictionary<string, INST>(); ClassMap = new Dictionary<string, INST>();
} }
public static int SizeOf<T>() where T : struct
{
int result = 1;
if (typeof(T) != typeof(bool))
result = Marshal.SizeOf<T>();
return result;
}
private static byte[] GetBytes<T>(T value, int bufferSize, IntPtr converter) private static byte[] GetBytes<T>(T value, int bufferSize, IntPtr converter)
{ {
byte[] bytes = new byte[bufferSize]; byte[] bytes = new byte[bufferSize];
@ -52,7 +62,7 @@ namespace RobloxFiles.BinaryFormat
public static byte[] GetBytes<T>(T value) where T : struct public static byte[] GetBytes<T>(T value) where T : struct
{ {
int bufferSize = Marshal.SizeOf<T>(); int bufferSize = SizeOf<T>();
IntPtr converter = Marshal.AllocHGlobal(bufferSize); IntPtr converter = Marshal.AllocHGlobal(bufferSize);
var result = GetBytes(value, bufferSize, converter); var result = GetBytes(value, bufferSize, converter);
@ -66,7 +76,7 @@ namespace RobloxFiles.BinaryFormat
public void WriteInterleaved<T>(List<T> values, Func<T, T> encode = null) where T : struct public void WriteInterleaved<T>(List<T> values, Func<T, T> encode = null) where T : struct
{ {
int count = values.Count; int count = values.Count;
int bufferSize = Marshal.SizeOf<T>(); int bufferSize = SizeOf<T>();
byte[][] blocks = new byte[count][]; byte[][] blocks = new byte[count][];
IntPtr converter = Marshal.AllocHGlobal(bufferSize); IntPtr converter = Marshal.AllocHGlobal(bufferSize);
@ -111,7 +121,7 @@ namespace RobloxFiles.BinaryFormat
return BitConverter.ToSingle(buffer, 0); return BitConverter.ToSingle(buffer, 0);
} }
// Writes an interleaved list of ints. // Writes an interleaved list of integers.
public void WriteInts(List<int> values) public void WriteInts(List<int> values)
{ {
WriteInterleaved(values, EncodeInt); WriteInterleaved(values, EncodeInt);
@ -123,7 +133,7 @@ namespace RobloxFiles.BinaryFormat
WriteInterleaved(values, EncodeFloat); WriteInterleaved(values, EncodeFloat);
} }
// Writes an accumlated array of integers. // Accumulatively writes an interleaved array of integers.
public void WriteInstanceIds(List<int> values) public void WriteInstanceIds(List<int> values)
{ {
int numIds = values.Count; int numIds = values.Count;
@ -204,7 +214,6 @@ namespace RobloxFiles.BinaryFormat
for (int i = 0; i < classes.Length; i++, File.NumClasses++) for (int i = 0; i < classes.Length; i++, File.NumClasses++)
{ {
string className = classNames[i]; string className = classNames[i];
INST inst = ClassMap[className]; INST inst = ClassMap[className];
inst.ClassIndex = i; inst.ClassIndex = i;
} }