Skip to content

Commit

Permalink
Mostly implement attribute restoration for v29
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Byass committed Oct 26, 2021
1 parent bec0467 commit c37aea7
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 22 deletions.
386 changes: 386 additions & 0 deletions Cpp2IL.Core/AttributeRestorerPost29.cs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Cpp2IL.Core/Cpp2IlApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ public static void RunAttributeRestorationForAssembly(AssemblyDefinition assembl
{
CheckLibInitialized();

if (LibCpp2IlMain.MetadataVersion >= 29)
{
//V29: Attributes are stored in metadata. This process becomes a lot simpler.
AttributeRestorerPost29.ApplyCustomAttributesToAllTypesInAssembly(assembly);
return;
}

switch (LibCpp2IlMain.Binary!.InstructionSet)
{
case InstructionSet.X86_32:
Expand Down
6 changes: 3 additions & 3 deletions Cpp2IL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ public static int MainWithArgs(Cpp2IlRuntimeArgs runtimeArgs)
Logger.InfoNewline($"Applying type, method, and field attributes for {Cpp2IlApi.GeneratedAssemblies.Count} assemblies...This may take a couple of seconds");
var start = DateTime.Now;

if(LibCpp2IlMain.MetadataVersion >= 29)
Logger.WarnNewline("Unable to run attribute restoration, because v29 is not fully supported yet.");
else
// if(LibCpp2IlMain.MetadataVersion >= 29)
// Logger.WarnNewline("Unable to run attribute restoration, because v29 is not fully supported yet.");
// else
Cpp2IlApi.RunAttributeRestorationForAllAssemblies(keyFunctionAddresses, parallel: LibCpp2IlMain.Binary!.InstructionSet is InstructionSet.X86_32 or InstructionSet.X86_64);

Logger.InfoNewline($"Finished Applying Attributes in {(DateTime.Now - start).TotalMilliseconds:F0}ms");
Expand Down
3 changes: 2 additions & 1 deletion LibCpp2IL/BinaryStructures/Il2CppTypeEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public enum Il2CppTypeEnum
IL2CPP_TYPE_SENTINEL = 0x41, /* Sentinel for varargs method signature */
IL2CPP_TYPE_PINNED = 0x45, /* Local var that points to pinned object */

IL2CPP_TYPE_ENUM = 0x55 /* an enumeration */
IL2CPP_TYPE_ENUM = 0x55, /* an enumeration */
IL2CPP_TYPE_IL2CPP_TYPE_INDEX = 0xFF, /* an index into IL2CPP type metadata table */
}
}
3 changes: 3 additions & 0 deletions LibCpp2IL/ClassReadingBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public long Position
if (type == typeof(bool))
return ReadBoolean();

if (type == typeof(char))
return ReadChar();

if (type == typeof(int))
return ReadInt32();

Expand Down
9 changes: 9 additions & 0 deletions LibCpp2IL/Metadata/Il2CppCustomAttributeDataRange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace LibCpp2IL.Metadata
{
public class Il2CppCustomAttributeDataRange
{
//Since v29
public uint token;
public uint startOffset;
}
}
28 changes: 16 additions & 12 deletions LibCpp2IL/Metadata/Il2CppGlobalMetadataHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,26 @@ public class Il2CppGlobalMetadataHeader
public int imagesCount;
public int assembliesOffset; // Il2CppAssemblyDefinition
public int assembliesCount;
[Version(Max=24.5f)] //Removed in v27
public int metadataUsageListsOffset; // Il2CppMetadataUsageList
[Version(Max=24.5f)] //Removed in v27
public int metadataUsageListsCount;
[Version(Max=24.5f)] //Removed in v27
public int metadataUsagePairsOffset; // Il2CppMetadataUsagePair
[Version(Max=24.5f)] //Removed in v27
public int metadataUsagePairsCount;
[Version(Max=24.5f)] public int metadataUsageListsOffset; // Il2CppMetadataUsageList, Removed in v27
[Version(Max=24.5f)] public int metadataUsageListsCount; //Removed in v27
[Version(Max=24.5f)] public int metadataUsagePairsOffset; // Il2CppMetadataUsagePair, Removed in v27
[Version(Max=24.5f)] public int metadataUsagePairsCount; //Removed in v27
public int fieldRefsOffset; // Il2CppFieldRef
public int fieldRefsCount;
public int referencedAssembliesOffset; // int32_t
public int referencedAssembliesCount;
public int attributesInfoOffset; // Il2CppCustomAttributeTypeRange
public int attributesInfoCount;
public int attributeTypesOffset; // TypeIndex
public int attributeTypesCount;

//Pre-29 attribute data
[Version(Max=27.1f)]public int attributesInfoOffset; // Il2CppCustomAttributeTypeRange
[Version(Max=27.1f)]public int attributesInfoCount;
[Version(Max=27.1f)] public int attributeTypesOffset; // TypeIndex
[Version(Max=27.1f)] public int attributeTypesCount;

//Post-29 attribute data
[Version(Min = 27.1f)] public int attributeDataOffset; //uint8_t
[Version(Min = 27.1f)] public int attributeDataCount;
[Version(Min = 27.1f)] public int attributeDataRangeOffset; //Il2CppCustomAttributeDataRange
[Version(Min = 27.1f)] public int attributeDataRangeCount;
public int unresolvedVirtualCallParameterTypesOffset; // TypeIndex
public int unresolvedVirtualCallParameterTypesCount;
public int unresolvedVirtualCallParameterRangesOffset; // Il2CppRange
Expand Down
32 changes: 26 additions & 6 deletions LibCpp2IL/Metadata/Il2CppMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Il2CppMetadata : ClassReadingBinaryReader
{
//Disable null check as this stuff is reflected.
#pragma warning disable 8618
private Il2CppGlobalMetadataHeader metadataHeader;
public Il2CppGlobalMetadataHeader metadataHeader;
public Il2CppImageDefinition[] imageDefinitions;
public Il2CppTypeDefinition[] typeDefs;
internal Il2CppInterfaceOffset[] interfaceOffsets;
Expand All @@ -30,8 +30,13 @@ public class Il2CppMetadata : ClassReadingBinaryReader
public Il2CppMetadataUsageList[] metadataUsageLists;
private Il2CppMetadataUsagePair[] metadataUsagePairs;
public Il2CppRGCTXDefinition[] RgctxDefinitions; //Moved to binary in v24.2

//Pre-29
public int[] attributeTypes;
public int[] interfaceIndices;

//Post-29
public List<Il2CppCustomAttributeDataRange> AttributeDataRanges;

//Moved to binary in v27.
public Dictionary<uint, SortedDictionary<uint, uint>> metadataUsageDic;
Expand Down Expand Up @@ -212,11 +217,26 @@ private Il2CppMetadata(MemoryStream stream) : base(stream)
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");

//v21+ fields
LibLogger.Verbose("\tReading attribute types...");
start = DateTime.Now;
attributeTypeRanges = ReadMetadataClassArray<Il2CppCustomAttributeTypeRange>(metadataHeader.attributesInfoOffset, metadataHeader.attributesInfoCount);
attributeTypes = ReadClassArrayAtRawAddr<int>(metadataHeader.attributeTypesOffset, metadataHeader.attributeTypesCount / 4);
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");

if (LibCpp2IlMain.MetadataVersion < 29)
{
//Removed in v29
LibLogger.Verbose("\tReading attribute types...");
start = DateTime.Now;
attributeTypeRanges = ReadMetadataClassArray<Il2CppCustomAttributeTypeRange>(metadataHeader.attributesInfoOffset, metadataHeader.attributesInfoCount);
attributeTypes = ReadClassArrayAtRawAddr<int>(metadataHeader.attributeTypesOffset, metadataHeader.attributeTypesCount / 4);
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
}
else
{
//Since v29
LibLogger.Verbose("\tReading Attribute data...");
start = DateTime.Now;

//Pointer array
AttributeDataRanges = ReadClassArrayAtRawAddr<Il2CppCustomAttributeDataRange>(metadataHeader.attributeDataRangeOffset, metadataHeader.attributeDataRangeCount / 8).ToList();
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
}

LibLogger.Verbose("\tBuilding Lookup Table for field defaults...");
start = DateTime.Now;
Expand Down

0 comments on commit c37aea7

Please sign in to comment.