Skip to content

Commit e7ef9fa

Browse files
committed
Lib: Support v35, v38, v39
1 parent 3ef4530 commit e7ef9fa

35 files changed

Lines changed: 631 additions & 431 deletions

Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected void InitCustomAttributeData()
119119

120120
AttributeTypes = Enumerable.Range(AttributeTypeRange.start, AttributeTypeRange.count)
121121
.Select(attrIdx => AppContext.Metadata!.attributeTypes![attrIdx]) //Not null because we've checked we're not on v29
122-
.Select(typeIdx => AppContext.Binary!.GetType(typeIdx))
122+
.Select(typeIdx => AppContext.Binary!.GetType(Il2CppVariableWidthIndex<Il2CppType>.MakeTemporaryForFixedWidthUsage(typeIdx)))
123123
.ToList();
124124
}
125125

@@ -146,8 +146,8 @@ protected void InitCustomAttributeData()
146146
var attributeDataRange = AppContext.Metadata.AttributeDataRanges[caIndex];
147147
var next = AppContext.Metadata.AttributeDataRanges[caIndex + 1];
148148

149-
var blobStart = AppContext.Metadata.metadataHeader.attributeDataOffset + attributeDataRange.startOffset;
150-
var blobEnd = AppContext.Metadata.metadataHeader.attributeDataOffset + next.startOffset;
149+
var blobStart = AppContext.Metadata.metadataHeader.attributeData.Offset + attributeDataRange.startOffset;
150+
var blobEnd = AppContext.Metadata.metadataHeader.attributeData.Offset + next.startOffset;
151151
return (blobStart, blobEnd);
152152
}
153153

Cpp2IL.Core/Model/Contexts/ParameterAnalysisContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public ParameterAnalysisContext(Il2CppParameterDefinition? definition, int param
8282

8383
if (Attributes.HasFlag(ParameterAttributes.HasDefault))
8484
{
85-
DefaultValue = AppContext.Metadata.GetParameterDefaultValueFromIndex(declaringMethod.Definition!.parameterStart + parameterIndex)!;
85+
DefaultValue = AppContext.Metadata.GetParameterDefaultValueFromIndex(declaringMethod.Definition!.parameterStart.Value + parameterIndex)!;
8686
}
8787
}
8888
}

Cpp2IL.Core/Model/CustomAttributes/CustomAttributeArrayParameter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Cpp2IL.Core.Model.Contexts;
66
using Cpp2IL.Core.Utils;
77
using LibCpp2IL.BinaryStructures;
8+
using LibCpp2IL.Metadata;
89

910
namespace Cpp2IL.Core.Model.CustomAttributes;
1011

@@ -45,7 +46,7 @@ public override void ReadFromV29Blob(BinaryReader reader, ApplicationAnalysisCon
4546
var enumTypeIndex = reader.BaseStream.ReadUnityCompressedInt();
4647

4748
//Save the actual enum type for later.
48-
EnumType = context.Binary.GetType(enumTypeIndex);
49+
EnumType = context.Binary.GetType(Il2CppVariableWidthIndex<Il2CppType>.MakeTemporaryForFixedWidthUsage(enumTypeIndex)); //DynWidth: enumTypeIndex is already compressed, they didn't make it dynamic
4950

5051
//We read as the primitive underlying type.
5152
var enumClass = EnumType.AsClass();

Cpp2IL.Core/Model/CustomAttributes/CustomAttributeTypeParameter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Cpp2IL.Core.Utils;
55
using LibCpp2IL;
66
using LibCpp2IL.BinaryStructures;
7+
using LibCpp2IL.Metadata;
78

89
namespace Cpp2IL.Core.Model.CustomAttributes;
910

@@ -44,7 +45,7 @@ public override void ReadFromV29Blob(BinaryReader reader, ApplicationAnalysisCon
4445
_type = null;
4546
else
4647
{
47-
_type = context.Binary.GetType(typeIndex);
48+
_type = context.Binary.GetType(Il2CppVariableWidthIndex<Il2CppType>.MakeTemporaryForFixedWidthUsage(typeIndex)); //DynWidth: typeIndex is already compressed, they didn't make it dynamic
4849
}
4950
_typeContext = null;
5051
}

Cpp2IL.Core/Utils/AsmResolver/AsmResolverUtils.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using AsmResolver.DotNet;
77
using AsmResolver.DotNet.Signatures;
88
using LibCpp2IL.BinaryStructures;
9+
using LibCpp2IL.Metadata;
910

1011
namespace Cpp2IL.Core.Utils.AsmResolver;
1112

@@ -14,7 +15,7 @@ public static class AsmResolverUtils
1415
private static readonly ConcurrentDictionary<string, TypeDefinition?> CachedTypeDefsByName = new();
1516
private static readonly ConcurrentDictionary<string, TypeSignature?> CachedTypeSignaturesByName = new();
1617

17-
public static readonly ConcurrentDictionary<long, TypeDefinition> TypeDefsByIndex = new();
18+
public static readonly ConcurrentDictionary<Il2CppVariableWidthIndex<Il2CppTypeDefinition>, TypeDefinition> TypeDefsByIndex = new();
1819

1920
internal static void Reset()
2021
{

Cpp2IL.Core/Utils/V29AttributeUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static T ResolveMemberFromIndex<T>(Stream stream, MethodAnalysisContext
8383
memberIndex = -(memberIndex + 1);
8484

8585
//Resolve type
86-
var typeDef = context.Metadata.typeDefs[typeIndex];
86+
var typeDef = context.Metadata.GetTypeDefinitionFromIndex(Il2CppVariableWidthIndex<Il2CppTypeDefinition>.MakeTemporaryForFixedWidthUsage((int)typeIndex)); //DynWidth: typeIndex is already compressed, they didn't make it dynamic
8787
var typeContext = context.ResolveContextForType(typeDef) ?? throw new("Unable to find type " + typeDef);
8888

8989
//Get member
@@ -118,7 +118,7 @@ public static BaseCustomAttributeParameter ConstructParameterForType(BinaryReade
118118
{
119119
case Il2CppTypeEnum.IL2CPP_TYPE_ENUM:
120120
var enumTypeIndex = reader.BaseStream.ReadUnityCompressedInt();
121-
var enumType = context.Binary.GetType(enumTypeIndex);
121+
var enumType = context.Binary.GetType(Il2CppVariableWidthIndex<Il2CppType>.MakeTemporaryForFixedWidthUsage(enumTypeIndex)); //DynWidth: enumTypeIndex is already compressed, they didn't make it dynamic
122122
return new CustomAttributeEnumParameter(enumType, context, owner, kind, index);
123123
case Il2CppTypeEnum.IL2CPP_TYPE_SZARRAY:
124124
return new CustomAttributeArrayParameter(owner, kind, index);

Cpp2IL.Plugin.StrippedCodeRegSupport/StrippedCodeRegSupportPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private void OnReadFail(Il2CppBinary binary, Il2CppMetadata metadata, ref Il2Cpp
3838

3939
//All we NEED to find is pCodegenModules - the rest of the CodeRegistration struct isn't critical to a successful dump.
4040
//We can piggyback off BinarySearcher:
41-
var searcher = new BinarySearcher(binary, metadata.methodDefs.Length, metadata.typeDefs.Length);
41+
var searcher = new BinarySearcher(binary, metadata.methodDefs.Length, metadata.TypeDefinitionCount);
4242

4343
var mscorlibs = searcher.FindAllStrings("mscorlib.dll\0").Select(binary.MapRawAddressToVirtual).ToList();
4444

LibCpp2IL/BinarySearcher.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ public ulong FindMetadataRegistrationPre24_5()
287287

288288
var bytesToSubtract = sizeOfMr - ptrSize * 4;
289289

290-
var potentialMetaRegPointers = MapOffsetsToVirt(FindAllBytes(BitConverter.GetBytes(LibCpp2IlMain.TheMetadata!.typeDefs.Length), 1)).ToList();
290+
var potentialMetaRegPointers = MapOffsetsToVirt(FindAllBytes(BitConverter.GetBytes(LibCpp2IlMain.TheMetadata!.TypeDefinitionCount), 1)).ToList();
291291

292-
LibLogger.VerboseNewline($"\t\t\tFound {potentialMetaRegPointers.Count} instances of the number of type defs, {LibCpp2IlMain.TheMetadata.typeDefs.Length}");
292+
LibLogger.VerboseNewline($"\t\t\tFound {potentialMetaRegPointers.Count} instances of the number of type defs, {LibCpp2IlMain.TheMetadata.TypeDefinitionCount}");
293293

294294
potentialMetaRegPointers = potentialMetaRegPointers.Select(p => p - bytesToSubtract).ToList();
295295

@@ -347,6 +347,7 @@ public ulong FindMetadataRegistrationPost24_5(Il2CppMetadata metadata)
347347
if (mrWords[i] == 0)
348348
{
349349
ok = i >= 14; //Maybe need an investigation here, but metadataUsages can be (always is?) a null ptr on v27
350+
ok |= i == 1; //genericClasses can rarely be null on v39?
350351
if (!ok)
351352
LibLogger.VerboseNewline($"\t\t\tRejecting metadata registration 0x{va:X} because the pointer at index {i} is 0.");
352353
}
@@ -372,22 +373,22 @@ public ulong FindMetadataRegistrationPost24_5(Il2CppMetadata metadata)
372373
// continue;
373374
}
374375

375-
if (metaReg.typeDefinitionsSizesCount != metadata.typeDefs.Length)
376+
if (metaReg.typeDefinitionsSizesCount != metadata.TypeDefinitionCount)
376377
{
377-
LibLogger.VerboseNewline($"\t\t\tRejecting metadata registration 0x{va:X} because it has {metaReg.typeDefinitionsSizesCount} type def sizes, while metadata file defines {metadata.typeDefs.Length} type defs");
378+
LibLogger.VerboseNewline($"\t\t\tRejecting metadata registration 0x{va:X} because it has {metaReg.typeDefinitionsSizesCount} type def sizes, while metadata file defines {metadata.TypeDefinitionCount} type defs");
378379
continue;
379380
}
380381

381-
if (metaReg.numTypes < metadata.typeDefs.Length)
382+
if (metaReg.numTypes < metadata.TypeDefinitionCount)
382383
{
383-
LibLogger.VerboseNewline($"\t\t\tRejecting metadata registration 0x{va:X} because it has {metaReg.numTypes} types, which is less than metadata-file-defined type def count of {metadata.typeDefs.Length}");
384+
LibLogger.VerboseNewline($"\t\t\tRejecting metadata registration 0x{va:X} because it has {metaReg.numTypes} types, which is less than metadata-file-defined type def count of {metadata.TypeDefinitionCount}");
384385
continue;
385386
}
386387

387-
if (metaReg.fieldOffsetsCount != metadata.typeDefs.Length)
388+
if (metaReg.fieldOffsetsCount != metadata.TypeDefinitionCount)
388389
{
389390
//If we see any cases of failing to find meta reg and this line is in verbose log, maybe the assumption (num field offsets == num type defs) is wrong.
390-
LibLogger.VerboseNewline($"\t\t\tRejecting metadata registration 0x{va:X} because it has {metaReg.fieldOffsetsCount} field offsets, while metadata file defines {metadata.typeDefs.Length} type defs");
391+
LibLogger.VerboseNewline($"\t\t\tRejecting metadata registration 0x{va:X} because it has {metaReg.fieldOffsetsCount} field offsets, while metadata file defines {metadata.TypeDefinitionCount} type defs");
391392
continue;
392393
}
393394

LibCpp2IL/BinaryStructures/Il2CppGenericClass.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,26 @@ namespace LibCpp2IL.BinaryStructures;
44

55
public class Il2CppGenericClass : ReadableClass
66
{
7-
public long TypeDefinitionIndex; /* the generic type definition */
7+
[Version(Max = 24.5f)] public long TypeDefinitionIndex; /* the generic type definition */
8+
9+
[Version(Min = 27.0f)] public ulong V27TypePointer;
10+
811
public Il2CppGenericContext Context = null!; /* a context that contains the type instantiation doesn't contain any method instantiation */
912
public ulong CachedClass; /* if present, the Il2CppClass corresponding to the instantiation. */
1013

1114
public Il2CppTypeDefinition TypeDefinition => LibCpp2IlMain.MetadataVersion < 27f
12-
? LibCpp2IlMain.TheMetadata!.typeDefs[(int)TypeDefinitionIndex]
15+
? LibCpp2IlMain.TheMetadata!.GetTypeDefinitionFromIndex(Il2CppVariableWidthIndex<Il2CppTypeDefinition>.MakeTemporaryForFixedWidthUsage((int)TypeDefinitionIndex)) //DynWidth: TypeDefinitionIndex removed in v24.5, never dynamic
1316
: V27BaseType!.AsClass();
1417

15-
public Il2CppType? V27BaseType => LibCpp2IlMain.MetadataVersion < 27f ? null : LibCpp2IlMain.Binary!.ReadReadableAtVirtualAddress<Il2CppType>((ulong)TypeDefinitionIndex);
18+
public Il2CppType? V27BaseType => LibCpp2IlMain.MetadataVersion < 27f ? null : LibCpp2IlMain.Binary!.ReadReadableAtVirtualAddress<Il2CppType>(V27TypePointer);
1619

1720
public override void Read(ClassReadingBinaryReader reader)
1821
{
19-
TypeDefinitionIndex = reader.ReadNInt();
22+
if (IsAtLeast(27f))
23+
V27TypePointer = reader.ReadNUint();
24+
else
25+
TypeDefinitionIndex = reader.ReadNInt();
26+
2027
Context = reader.ReadReadableHereNoLock<Il2CppGenericContext>();
2128
CachedClass = reader.ReadNUint();
2229
}

LibCpp2IL/BinaryStructures/Il2CppRGCTXDefinition.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using LibCpp2IL.Metadata;
12
using LibCpp2IL.Reflection;
23

34
namespace LibCpp2IL.BinaryStructures;
@@ -13,7 +14,7 @@ public class Il2CppRGCTXDefinition : ReadableClass
1314

1415
public Il2CppMethodSpec? MethodSpec => LibCpp2IlMain.Binary?.GetMethodSpec(MethodIndex);
1516

16-
public Il2CppTypeReflectionData? Type => LibCpp2ILUtils.GetTypeReflectionData(LibCpp2IlMain.Binary!.GetType(TypeIndex));
17+
public Il2CppTypeReflectionData? Type => LibCpp2ILUtils.GetTypeReflectionData(LibCpp2IlMain.Binary!.GetType(Il2CppVariableWidthIndex<Il2CppType>.MakeTemporaryForFixedWidthUsage(TypeIndex)));
1718

1819

1920
public class Il2CppRGCTXDefinitionData : ReadableClass

0 commit comments

Comments
 (0)