Skip to content

Commit

Permalink
Fix issues from previous commits
Browse files Browse the repository at this point in the history
  • Loading branch information
SamboyCoding committed Dec 13, 2024
1 parent 9bb6f0d commit c91170a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Cpp2IL.Core/Model/Contexts/ApplicationAnalysisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Cpp2IL.Core.Exceptions;
using Cpp2IL.Core.Il2CppApiFunctions;
using Cpp2IL.Core.Logging;
using Cpp2IL.Core.Utils;
using LibCpp2IL;
using LibCpp2IL.Metadata;

Expand Down Expand Up @@ -101,6 +102,8 @@ public ApplicationAnalysisContext(Il2CppBinary binary, Il2CppMetadata metadata)
}

SystemTypes = new(this);

MiscUtils.InitFunctionStarts(this);

PopulateMethodsByAddressTable();

Expand Down
11 changes: 7 additions & 4 deletions Cpp2IL.Core/Utils/MiscUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cpp2IL.Core.Model.Contexts;
using LibCpp2IL;

namespace Cpp2IL.Core.Utils;
Expand Down Expand Up @@ -168,21 +169,23 @@ internal static byte[] RawBytes(IConvertible original) =>
_ => throw new($"ReinterpretBytes: Cannot get byte array from {original} (type {original.GetType()}")
};

private static void InitFunctionStarts()
//TODO: Refactor this out to a property of ApplicationAnalysisContext
internal static void InitFunctionStarts(ApplicationAnalysisContext appContext)
{
_allKnownFunctionStarts = Cpp2IlApi.CurrentAppContext!.Metadata.methodDefs.Select(m => m.MethodPointer)
.Concat(Cpp2IlApi.CurrentAppContext.Binary.ConcreteGenericImplementationsByAddress.Keys)
_allKnownFunctionStarts = appContext.Metadata.methodDefs.Select(m => m.MethodPointer)
.Concat(appContext.Binary.ConcreteGenericImplementationsByAddress.Keys)
.Concat(SharedState.AttributeGeneratorStarts)
.ToList();

//Sort in ascending order
_allKnownFunctionStarts.Sort();
}
//TODO: End

public static ulong GetAddressOfNextFunctionStart(ulong current)
{
if (_allKnownFunctionStarts == null)
InitFunctionStarts();
throw new("Function starts not initialized!");

//Binary-search-like approach
var lower = 0;
Expand Down
6 changes: 4 additions & 2 deletions LibCpp2IL/ClassReadingBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace LibCpp2IL;

public class ClassReadingBinaryReader : EndianAwareBinaryReader
public abstract class ClassReadingBinaryReader : EndianAwareBinaryReader
{
/// <summary>
/// Set this to true to enable storing of amount of bytes read of each readable structure.
Expand All @@ -26,6 +26,8 @@ public class ClassReadingBinaryReader : EndianAwareBinaryReader
protected bool _hasFinishedInitialRead;
private bool _inReadableRead;
public ConcurrentDictionary<Type, int> BytesReadPerClass = new();

public abstract float MetadataVersion { get; }


public ClassReadingBinaryReader(MemoryStream input) : base(input)
Expand Down Expand Up @@ -173,7 +175,7 @@ protected internal int ReadUnityCompressedIntAtRawAddr(long position, bool doLoc

private T InternalReadReadableClass<T>() where T : ReadableClass, new()
{
var t = new T();
var t = new T { MetadataVersion = MetadataVersion };

if (!_inReadableRead)
{
Expand Down
5 changes: 5 additions & 0 deletions LibCpp2IL/Il2CppBinary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ public abstract class Il2CppBinary(MemoryStream input) : ClassReadingBinaryReade
/// </summary>
public virtual ClassReadingBinaryReader Reader => this;

private float _metadataVersion;
public sealed override float MetadataVersion => _metadataVersion;

public int InBinaryMetadataSize { get; private set; }

public void Init(ulong pCodeRegistration, ulong pMetadataRegistration, Il2CppMetadata metadata)
{
_metadataVersion = metadata.MetadataVersion;

var cr = pCodeRegistration > 0 ? ReadReadableAtVirtualAddress<Il2CppCodeRegistration>(pCodeRegistration) : null;
var mr = pMetadataRegistration > 0 ? ReadReadableAtVirtualAddress<Il2CppMetadataRegistration>(pMetadataRegistration) : null;

Expand Down
2 changes: 1 addition & 1 deletion LibCpp2IL/LibCpp2IlMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class LibCpp2IlSettings
public static readonly LibCpp2IlSettings Settings = new();

public static bool Il2CppTypeHasNumMods5Bits;
public static float MetadataVersion => TheMetadata?.MetadataVersion ?? 0;
public static float MetadataVersion => TheMetadata!.MetadataVersion;

public static Il2CppBinary? Binary;
public static Il2CppMetadata? TheMetadata;
Expand Down
2 changes: 1 addition & 1 deletion LibCpp2IL/Metadata/Il2CppMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace LibCpp2IL.Metadata;
public class Il2CppMetadata : ClassReadingBinaryReader
{
public const uint MetadataMagic = 0xFAB11BAF;
public float MetadataVersion { get; }
public override float MetadataVersion { get; }
public UnityVersion UnityVersion { get; }

//Disable null check as this stuff is reflected.
Expand Down
12 changes: 7 additions & 5 deletions LibCpp2IL/ReadableClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ namespace LibCpp2IL;

public abstract class ReadableClass
{
protected static bool IsAtLeast(float vers) => LibCpp2IlMain.MetadataVersion >= vers;
protected static bool IsLessThan(float vers) => LibCpp2IlMain.MetadataVersion < vers;
protected static bool IsAtMost(float vers) => LibCpp2IlMain.MetadataVersion <= vers;
protected static bool IsNot(float vers) => Math.Abs(LibCpp2IlMain.MetadataVersion - vers) > 0.001f;
protected static bool Is(float vers) => Math.Abs(LibCpp2IlMain.MetadataVersion - vers) < 0.001f;
internal float MetadataVersion { get; set; }

protected bool IsAtLeast(float vers) => MetadataVersion >= vers;
protected bool IsLessThan(float vers) => MetadataVersion < vers;
protected bool IsAtMost(float vers) => MetadataVersion <= vers;
protected bool IsNot(float vers) => Math.Abs(MetadataVersion - vers) > 0.001f;
protected bool Is(float vers) => Math.Abs(MetadataVersion - vers) < 0.001f;

public abstract void Read(ClassReadingBinaryReader reader);
}
3 changes: 3 additions & 0 deletions LibCpp2IL/Wasm/WasmMemoryBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class WasmMemoryBlock : ClassReadingBinaryReader
{
internal byte[] Bytes;

public override float MetadataVersion { get; }

private static MemoryStream BuildStream(WasmFile file)
{
//Find the maximum byte in the data section that has a value
Expand All @@ -32,6 +34,7 @@ private static MemoryStream BuildStream(WasmFile file)

public WasmMemoryBlock(WasmFile file) : base(BuildStream(file))
{
MetadataVersion = file.MetadataVersion;
is32Bit = true;
Bytes = ((MemoryStream)BaseStream).ToArray();
}
Expand Down

0 comments on commit c91170a

Please sign in to comment.