rename serialization methods and add BinaryObjectStreamReader for interop

This commit is contained in:
LukeFZ
2024-08-14 01:00:32 +02:00
parent 2d3b186b4d
commit 6c59434984
11 changed files with 191 additions and 28 deletions

View File

@@ -6,10 +6,12 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Il2CppInspector.Next;
using NoisyCowStudios.Bin2Object;
using VersionedSerialization;
@@ -121,6 +123,27 @@ namespace Il2CppInspector
public void AddPrimitiveMapping(Type objType, Type streamType);
public void CopyTo(Stream stream);
public TType ReadMappedPrimitive<TType>(ulong addr) where TType : unmanaged;
public TType ReadPrimitive<TType>(long addr) where TType : unmanaged;
public TType ReadPrimitive<TType>() where TType : unmanaged;
public ImmutableArray<TType> ReadMappedPrimitiveArray<TType>(ulong addr, long count) where TType : unmanaged;
public ImmutableArray<TType> ReadPrimitiveArray<TType>(long addr, long count) where TType : unmanaged;
public ImmutableArray<TType> ReadPrimitiveArray<TType>(long count) where TType : unmanaged;
public TType ReadMappedVersionedObject<TType>(ulong addr) where TType : IReadable, new();
public TType ReadVersionedObject<TType>(long addr) where TType : IReadable, new();
public TType ReadVersionedObject<TType>() where TType : IReadable, new();
public ImmutableArray<TType> ReadMappedVersionedObjectArray<TType>(ulong addr, long count)
where TType : IReadable, new();
public ImmutableArray<TType> ReadVersionedObjectArray<TType>(long addr, long count)
where TType : IReadable, new();
public ImmutableArray<TType> ReadVersionedObjectArray<TType>(long count)
where TType : IReadable, new();
}
public class FileFormatStream
@@ -161,7 +184,7 @@ namespace Il2CppInspector
}
}
public abstract class FileFormatStream<T> : BinaryObjectStream, IFileFormatStream where T : FileFormatStream<T>
public abstract class FileFormatStream<T> : BinaryObjectStreamReader, IFileFormatStream where T : FileFormatStream<T>
{
public abstract string DefaultFilename { get; }
@@ -323,5 +346,15 @@ namespace Il2CppInspector
array.Add(ReadMappedObject<U>(pointers[i]));
return array;
}
public TType ReadMappedPrimitive<TType>(ulong addr) where TType : unmanaged => ReadPrimitive<TType>(MapVATR(addr));
public ImmutableArray<TType> ReadMappedPrimitiveArray<TType>(ulong addr, long count) where TType : unmanaged
=> ReadPrimitiveArray<TType>(MapVATR(addr), count);
public TType ReadMappedVersionedObject<TType>(ulong addr) where TType : IReadable, new() => ReadVersionedObject<TType>(MapVATR(addr));
public ImmutableArray<TType> ReadMappedVersionedObjectArray<TType>(ulong addr, long count) where TType : IReadable, new()
=> ReadVersionedObjectArray<TType>(MapVATR(addr), count);
}
}

View File

@@ -38,7 +38,7 @@ namespace Il2CppInspector
Position = arch.Offset;
Endianness = Endianness.Little;
using var s = new BinaryObjectStream(ReadBytes((int) arch.Size));
using var s = new BinaryObjectStream(ReadBytes((int) arch.Size).ToArray());
return (IFileFormatStream) MachOReader32.Load(s, LoadOptions, OnStatusUpdate) ?? MachOReader64.Load(s, LoadOptions, OnStatusUpdate);
}
}

View File

@@ -17,7 +17,7 @@ using VersionedSerialization;
namespace Il2CppInspector
{
public class Metadata : BinaryObjectStream
public class Metadata : BinaryObjectStreamReader
{
public Il2CppGlobalMetadataHeader Header { get; set; }

View File

@@ -76,7 +76,7 @@ public record struct Il2CppType : IReadable
public void Read<TReader>(ref TReader reader, in StructVersion version = default) where TReader : IReader, allows ref struct
{
Data.Read(ref reader, version);
Value = reader.Read<uint>();
Value = reader.ReadPrimitive<uint>();
if (MetadataVersions.V272 > version)
{

View File

@@ -0,0 +1,130 @@
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using NoisyCowStudios.Bin2Object;
using VersionedSerialization;
namespace Il2CppInspector.Next;
public class BinaryObjectStreamReader : BinaryObjectStream, IReader
{
public virtual int Bits { get; set; }
public bool Is32Bit => Bits == 32;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static TTo Cast<TFrom, TTo>(in TFrom from) => Unsafe.As<TFrom, TTo>(ref Unsafe.AsRef(in from));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private T ReadInternal<T>() where T : unmanaged
{
var size = Unsafe.SizeOf<T>();
var value = MemoryMarshal.Read<T>(ReadBytes(size));
return value;
}
public T ReadPrimitive<T>() where T : unmanaged
{
if (typeof(T) == typeof(sbyte))
return Cast<byte, T>(ReadByte());
if (typeof(T) == typeof(short))
return Cast<short, T>(ReadInt16());
if (typeof(T) == typeof(int))
return Cast<int, T>(ReadInt32());
if (typeof(T) == typeof(long))
return Cast<long, T>(ReadInt64());
if (typeof(T) == typeof(byte))
return Cast<byte, T>(ReadByte());
if (typeof(T) == typeof(ushort))
return Cast<ushort, T>(ReadUInt16());
if (typeof(T) == typeof(uint))
return Cast<uint, T>(ReadUInt32());
if (typeof(T) == typeof(ulong))
return Cast<ulong, T>(ReadUInt64());
return ReadInternal<T>();
}
public ImmutableArray<T> ReadPrimitiveArray<T>(long count) where T : unmanaged
{
var array = ImmutableArray.CreateBuilder<T>(checked((int)count));
for (long i = 0; i < count; i++)
array.Add(ReadPrimitive<T>());
return array.MoveToImmutable();
}
public T ReadVersionedObject<T>() where T : IReadable, new() => ReadVersionedObject<T>(Version);
public T ReadVersionedObject<T>(in StructVersion version = default) where T : IReadable, new()
{
var obj = new T();
var a = this;
obj.Read(ref a, in version);
return obj;
}
public ImmutableArray<T> ReadVersionedObjectArray<T>(long count) where T : IReadable, new() => ReadVersionedObjectArray<T>(count, Version);
public ImmutableArray<T> ReadVersionedObjectArray<T>(long count, in StructVersion version = default) where T : IReadable, new()
{
var array = ImmutableArray.CreateBuilder<T>(checked((int)count));
for (long i = 0; i < count; i++)
array.Add(ReadVersionedObject<T>(in version));
return array.MoveToImmutable();
}
public long ReadNInt()
=> Is32Bit ? ReadPrimitive<int>() : ReadPrimitive<long>();
public ulong ReadNUInt()
=> Is32Bit ? ReadPrimitive<uint>() : ReadPrimitive<ulong>();
public string ReadString() => ReadNullTerminatedString();
public new ReadOnlySpan<byte> ReadBytes(int length)
{
return base.ReadBytes(length);
}
public void Align(int alignment = 0)
{
if (alignment == 0)
alignment = Is32Bit ? 4 : 8;
var rem = Position % alignment;
if (rem != 0)
Position += alignment - rem;
}
public TType ReadPrimitive<TType>(long addr) where TType : unmanaged
{
Position = addr;
return ReadPrimitive<TType>();
}
public ImmutableArray<TType> ReadPrimitiveArray<TType>(long addr, long count) where TType : unmanaged
{
Position = addr;
return ReadPrimitiveArray<TType>(count);
}
public TType ReadVersionedObject<TType>(long addr) where TType : IReadable, new()
{
Position = addr;
return ReadVersionedObject<TType>(Version);
}
public ImmutableArray<TType> ReadVersionedObjectArray<TType>(long addr, long count) where TType : IReadable, new()
{
Position = addr;
return ReadVersionedObjectArray<TType>(count, Version);
}
}

View File

@@ -34,7 +34,7 @@ public partial struct Il2CppAssemblyNameDefinition
[FieldOffset(44)]
[VersionCondition(LessThan = "15.0")]
[CustomSerialization("reader.Read<PublicKeyToken>();", "8")]
[CustomSerialization("reader.ReadPrimitive<PublicKeyToken>();", "8")]
private PublicKeyToken _legacyPublicKeyToken;
[FieldOffset(16)]
@@ -59,6 +59,6 @@ public partial struct Il2CppAssemblyNameDefinition
public int Revision;
[FieldOffset(44)]
[CustomSerialization("reader.Read<PublicKeyToken>();", "8")]
[CustomSerialization("reader.ReadPrimitive<PublicKeyToken>();", "8")]
public PublicKeyToken PublicKeyToken;
}

View File

@@ -25,13 +25,13 @@ public struct Pointer<T>(ulong value = 0) : IReadable, IEquatable<Pointer<T>> wh
public readonly T Read(ref SpanReader reader, in StructVersion version)
{
reader.Offset = (int)PointerValue;
return reader.ReadObject<T>(version);
return reader.ReadVersionedObject<T>(version);
}
public readonly ImmutableArray<T> ReadArray(ref SpanReader reader, long count, in StructVersion version)
{
reader.Offset = (int)PointerValue;
return reader.ReadObjectArray<T>(count, version);
return reader.ReadVersionedObjectArray<T>(count, version);
}
public static implicit operator Pointer<T>(ulong value) => new(value);

View File

@@ -213,13 +213,13 @@ namespace VersionedSerialization.Generator
string readMethod;
if (typeInfo.Type == PropertyType.None)
{
readMethod = $"reader.ReadObject<{typeInfo.ComplexTypeName}>(in version);";
readMethod = $"reader.ReadVersionedObject<{typeInfo.ComplexTypeName}>(in version);";
}
else
{
readMethod = typeInfo.Type.IsSeperateMethod()
? $"reader.Read{typeInfo.Type.GetTypeName()}();"
: $"reader.Read<{typeInfo.Type.GetTypeName()}>();";
: $"reader.ReadPrimitive<{typeInfo.Type.GetTypeName()}>();";
if (typeInfo.ComplexTypeName != "")
readMethod = $"({typeInfo.ComplexTypeName}){readMethod}";

View File

@@ -12,11 +12,11 @@ public interface IReader
string ReadString();
ReadOnlySpan<byte> ReadBytes(int length);
T Read<T>() where T : unmanaged;
ImmutableArray<T> ReadArray<T>(long count) where T : unmanaged;
T ReadPrimitive<T>() where T : unmanaged;
ImmutableArray<T> ReadPrimitiveArray<T>(long count) where T : unmanaged;
T ReadObject<T>(in StructVersion version = default) where T : IReadable, new();
ImmutableArray<T> ReadObjectArray<T>(long count, in StructVersion version = default) where T : IReadable, new();
T ReadVersionedObject<T>(in StructVersion version = default) where T : IReadable, new();
ImmutableArray<T> ReadVersionedObjectArray<T>(long count, in StructVersion version = default) where T : IReadable, new();
public void Align(int alignment = 0);
}

View File

@@ -7,20 +7,20 @@ public static class ReaderExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReadCompressedUInt<T>(this ref T reader) where T : struct, IReader, allows ref struct
{
var first = reader.Read<byte>();
var first = reader.ReadPrimitive<byte>();
if ((first & 0b10000000) == 0b00000000)
return first;
if ((first & 0b11000000) == 0b10000000)
return (uint)(((first & ~0b10000000) << 8) | reader.Read<byte>());
return (uint)(((first & ~0b10000000) << 8) | reader.ReadPrimitive<byte>());
if ((first & 0b11100000) == 0b11000000)
return (uint)(((first & ~0b11000000) << 24) | (reader.Read<byte>() << 16) | (reader.Read<byte>() << 8) | reader.Read<byte>());
return (uint)(((first & ~0b11000000) << 24) | (reader.ReadPrimitive<byte>() << 16) | (reader.ReadPrimitive<byte>() << 8) | reader.ReadPrimitive<byte>());
return first switch
{
0b11110000 => reader.Read<uint>(),
0b11110000 => reader.ReadPrimitive<uint>(),
0b11111110 => uint.MaxValue - 1,
0b11111111 => uint.MaxValue,
_ => throw new InvalidDataException("Invalid compressed uint")
@@ -49,7 +49,7 @@ public static class ReaderExtensions
do
{
current = reader.Read<byte>();
current = reader.ReadPrimitive<byte>();
value |= (current & 0x7FuL) << shift;
shift += 7;
} while ((current & 0x80) != 0);

View File

@@ -40,7 +40,7 @@ public ref struct SpanReader(ReadOnlySpan<byte> data, int offset = 0, bool littl
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Read<T>() where T : unmanaged
public T ReadPrimitive<T>() where T : unmanaged
{
if (typeof(T) == typeof(byte))
return Cast<byte, T>(_data[Offset++]);
@@ -84,17 +84,17 @@ public ref struct SpanReader(ReadOnlySpan<byte> data, int offset = 0, bool littl
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImmutableArray<T> ReadArray<T>(long count) where T : unmanaged
public ImmutableArray<T> ReadPrimitiveArray<T>(long count) where T : unmanaged
{
var array = ImmutableArray.CreateBuilder<T>(checked((int)count));
for (long i = 0; i < count; i++)
array.Add(Read<T>());
array.Add(ReadPrimitive<T>());
return array.MoveToImmutable();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T ReadObject<T>(in StructVersion version = default) where T : IReadable, new()
public T ReadVersionedObject<T>(in StructVersion version = default) where T : IReadable, new()
{
var obj = new T();
obj.Read(ref this, in version);
@@ -102,11 +102,11 @@ public ref struct SpanReader(ReadOnlySpan<byte> data, int offset = 0, bool littl
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImmutableArray<T> ReadObjectArray<T>(long count, in StructVersion version = default) where T : IReadable, new()
public ImmutableArray<T> ReadVersionedObjectArray<T>(long count, in StructVersion version = default) where T : IReadable, new()
{
var array = ImmutableArray.CreateBuilder<T>(checked((int)count));
for (long i = 0; i < count; i++)
array.Add(ReadObject<T>(in version));
array.Add(ReadVersionedObject<T>(in version));
return array.MoveToImmutable();
}
@@ -126,13 +126,13 @@ public ref struct SpanReader(ReadOnlySpan<byte> data, int offset = 0, bool littl
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ReadBoolean() => Read<byte>() != 0;
public bool ReadBoolean() => ReadPrimitive<byte>() != 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong ReadNUInt() => _is32Bit ? Read<uint>() : Read<ulong>();
public ulong ReadNUInt() => _is32Bit ? ReadPrimitive<uint>() : ReadPrimitive<ulong>();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long ReadNInt() => _is32Bit ? Read<int>() : Read<long>();
public long ReadNInt() => _is32Bit ? ReadPrimitive<int>() : ReadPrimitive<long>();
public void Align(int alignment = 0)
{