Skip to content

Commit ff3368f

Browse files
authored
Use newer AsmResolver APIs and update the places we import references (SamboyCoding#432)
1 parent bb74a97 commit ff3368f

File tree

5 files changed

+31
-48
lines changed

5 files changed

+31
-48
lines changed

Cpp2IL.Core/Model/Contexts/EventAnalysisContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Reflection;
12
using Cpp2IL.Core.Utils;
23
using LibCpp2IL.Metadata;
34
using StableNameDotNet.Providers;
@@ -18,8 +19,12 @@ public class EventAnalysisContext : HasCustomAttributesAndName, IEventInfoProvid
1819

1920
public override string DefaultName => Definition.Name!;
2021

22+
public EventAttributes EventAttributes => Definition.EventAttributes;
23+
2124
public TypeAnalysisContext EventTypeContext => DeclaringType.DeclaringAssembly.ResolveIl2CppType(Definition.RawType!);
2225

26+
public bool IsStatic => Definition.IsStatic;
27+
2328
public EventAnalysisContext(Il2CppEventDefinition definition, TypeAnalysisContext parent) : base(definition.token, parent.AppContext)
2429
{
2530
Definition = definition;

Cpp2IL.Core/Model/Contexts/PropertyAnalysisContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Reflection;
12
using Cpp2IL.Core.Utils;
23
using LibCpp2IL.Metadata;
34
using StableNameDotNet.Providers;
@@ -18,6 +19,10 @@ public class PropertyAnalysisContext : HasCustomAttributesAndName, IPropertyInfo
1819

1920
public override string DefaultName => Definition.Name!;
2021

22+
public bool IsStatic => Definition.IsStatic;
23+
24+
public PropertyAttributes PropertyAttributes => (PropertyAttributes)Definition.attrs;
25+
2126
public TypeAnalysisContext PropertyTypeContext => DeclaringType.DeclaringAssembly.ResolveIl2CppType(Definition.RawPropertyType!);
2227

2328
public PropertyAnalysisContext(Il2CppPropertyDefinition definition, TypeAnalysisContext parent) : base(definition.token, parent.AppContext)

Cpp2IL.Core/Model/Contexts/SystemTypesContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Cpp2IL.Core.Model.Contexts;
44

55
public class SystemTypesContext
66
{
7-
private ApplicationAnalysisContext _appContext;
7+
private readonly ApplicationAnalysisContext _appContext;
88

99
public TypeAnalysisContext SystemObjectType { get; }
1010
public TypeAnalysisContext SystemVoidType { get; }

Cpp2IL.Core/Utils/AsmResolver/AsmResolverAssemblyPopulator.cs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void ConfigureHierarchy(AssemblyAnalysisContext asmCtx)
3030
var il2CppTypeDef = typeCtx.Definition;
3131
var typeDefinition = typeCtx.GetExtraData<TypeDefinition>("AsmResolverType") ?? throw new($"AsmResolver type not found in type analysis context for {typeCtx.FullName}");
3232

33-
var importer = typeDefinition.Module!.Assembly!.GetImporter();
33+
var importer = typeDefinition.Module!.DefaultImporter;
3434

3535
//Type generic params.
3636
if (il2CppTypeDef != null)
@@ -57,7 +57,7 @@ private static void PopulateGenericParamsForType(Il2CppTypeDefinition cppTypeDef
5757
if (cppTypeDefinition.GenericContainer == null)
5858
return;
5959

60-
var importer = ilTypeDefinition.Module!.Assembly!.GetImporter();
60+
var importer = ilTypeDefinition.Module!.DefaultImporter;
6161

6262
foreach (var param in cppTypeDefinition.GenericContainer.GenericParameters)
6363
{
@@ -234,7 +234,7 @@ private static CustomAttributeNamedArgument FromAnalyzedAttributeProperty(Assemb
234234
}
235235
#endif
236236

237-
var importedCtor = assemblyDefinition.GetImporter().ImportMethod(ctor);
237+
var importedCtor = assemblyDefinition.ManifestModule!.DefaultImporter.ImportMethod(ctor);
238238

239239
var newAttribute = new CustomAttribute((ICustomAttributeType)importedCtor, signature);
240240
return newAttribute;
@@ -340,7 +340,7 @@ public static void CopyDataFromIl2CppToManaged(AssemblyAnalysisContext asmContex
340340

341341
private static void CopyIl2CppDataToManagedType(TypeAnalysisContext typeContext, TypeDefinition ilTypeDefinition)
342342
{
343-
var importer = ilTypeDefinition.Module!.Assembly!.GetImporter();
343+
var importer = ilTypeDefinition.Module!.DefaultImporter;
344344

345345
CopyFieldsInType(importer, typeContext, ilTypeDefinition);
346346

@@ -357,7 +357,7 @@ private static void CopyFieldsInType(ReferenceImporter importer, TypeAnalysisCon
357357
{
358358
var fieldInfo = fieldContext.BackingData;
359359

360-
var fieldTypeSig = importer.ImportTypeSignature(fieldContext.ToTypeSignature(importer.TargetModule));
360+
var fieldTypeSig = fieldContext.ToTypeSignature(importer.TargetModule);
361361

362362
var managedField = new FieldDefinition(fieldContext.FieldName, (FieldAttributes)fieldContext.Attributes, fieldTypeSig);
363363

@@ -388,15 +388,15 @@ private static void CopyMethodsInType(ReferenceImporter importer, TypeAnalysisCo
388388
{
389389
var methodDef = methodCtx.Definition;
390390

391-
var returnType = importer.ImportTypeSignature(methodCtx.ReturnTypeContext.ToTypeSignature(importer.TargetModule));
391+
var returnType = methodCtx.ReturnTypeContext.ToTypeSignature(importer.TargetModule);
392392

393393
var paramData = methodCtx.Parameters;
394394
var parameterTypes = new TypeSignature[paramData.Count];
395395
var parameterDefinitions = new ParameterDefinition[paramData.Count];
396396
foreach (var parameterAnalysisContext in methodCtx.Parameters)
397397
{
398398
var i = parameterAnalysisContext.ParamIndex;
399-
parameterTypes[i] = importer.ImportTypeSignature(parameterAnalysisContext.ParameterTypeContext.ToTypeSignature(importer.TargetModule));
399+
parameterTypes[i] = parameterAnalysisContext.ParameterTypeContext.ToTypeSignature(importer.TargetModule);
400400

401401
var sequence = (ushort)(i + 1); //Add one because sequence 0 is the return type
402402
parameterDefinitions[i] = new(sequence, parameterAnalysisContext.Name, (ParameterAttributes)parameterAnalysisContext.ParameterAttributes);
@@ -471,23 +471,17 @@ private static void CopyPropertiesInType(ReferenceImporter importer, TypeAnalysi
471471
{
472472
foreach (var propertyCtx in typeContext.Properties)
473473
{
474-
var propertyDef = propertyCtx.Definition;
475-
476-
var propertyTypeSig = importer.ImportTypeSignature(AsmResolverUtils.GetTypeSignatureFromIl2CppType(importer.TargetModule, propertyDef.RawPropertyType!));
477-
var propertySignature = propertyDef.IsStatic
474+
var propertyTypeSig = propertyCtx.ToTypeSignature(importer.TargetModule);
475+
var propertySignature = propertyCtx.IsStatic
478476
? PropertySignature.CreateStatic(propertyTypeSig)
479477
: PropertySignature.CreateInstance(propertyTypeSig);
480478

481-
var managedProperty = new PropertyDefinition(propertyCtx.Name, (PropertyAttributes)propertyDef.attrs, propertySignature);
479+
var managedProperty = new PropertyDefinition(propertyCtx.Name, (PropertyAttributes)propertyCtx.PropertyAttributes, propertySignature);
482480

483481
var managedGetter = propertyCtx.Getter?.GetExtraData<MethodDefinition>("AsmResolverMethod");
484482
var managedSetter = propertyCtx.Setter?.GetExtraData<MethodDefinition>("AsmResolverMethod");
485483

486-
if (managedGetter != null)
487-
managedProperty.Semantics.Add(new(managedGetter, MethodSemanticsAttributes.Getter));
488-
489-
if (managedSetter != null)
490-
managedProperty.Semantics.Add(new(managedSetter, MethodSemanticsAttributes.Setter));
484+
managedProperty.SetSemanticMethods(managedGetter, managedSetter);
491485

492486
//Indexer parameters
493487
if (managedGetter != null && managedGetter.Parameters.Count > 0)
@@ -517,24 +511,15 @@ private static void CopyEventsInType(ReferenceImporter importer, TypeAnalysisCon
517511
{
518512
foreach (var eventCtx in cppTypeDefinition.Events)
519513
{
520-
var eventDef = eventCtx.Definition;
521-
522-
var eventType = importer.ImportTypeIfNeeded(AsmResolverUtils.ImportReferenceFromIl2CppType(ilTypeDefinition.Module!, eventDef.RawType!));
514+
var eventType = eventCtx.ToTypeSignature(importer.TargetModule).ToTypeDefOrRef();
523515

524-
var managedEvent = new EventDefinition(eventCtx.Name, (EventAttributes)eventDef.EventAttributes, eventType);
516+
var managedEvent = new EventDefinition(eventCtx.Name, (EventAttributes)eventCtx.EventAttributes, eventType);
525517

526518
var managedAdder = eventCtx.Adder?.GetExtraData<MethodDefinition>("AsmResolverMethod");
527519
var managedRemover = eventCtx.Remover?.GetExtraData<MethodDefinition>("AsmResolverMethod");
528520
var managedInvoker = eventCtx.Invoker?.GetExtraData<MethodDefinition>("AsmResolverMethod");
529521

530-
if (managedAdder != null)
531-
managedEvent.Semantics.Add(new(managedAdder, MethodSemanticsAttributes.AddOn));
532-
533-
if (managedRemover != null)
534-
managedEvent.Semantics.Add(new(managedRemover, MethodSemanticsAttributes.RemoveOn));
535-
536-
if (managedInvoker != null)
537-
managedEvent.Semantics.Add(new(managedInvoker, MethodSemanticsAttributes.Fire));
522+
managedEvent.SetSemanticMethods(managedAdder, managedRemover, managedInvoker);
538523

539524
eventCtx.PutExtraData("AsmResolverEvent", managedEvent);
540525

@@ -546,7 +531,7 @@ public static void AddExplicitInterfaceImplementations(AssemblyAnalysisContext a
546531
{
547532
var managedAssembly = asmContext.GetExtraData<AssemblyDefinition>("AsmResolverAssembly") ?? throw new("AsmResolver assembly not found in assembly analysis context for " + asmContext);
548533

549-
var importer = managedAssembly.GetImporter();
534+
var importer = managedAssembly.ManifestModule!.DefaultImporter;
550535

551536
foreach (var typeContext in asmContext.Types)
552537
{

Cpp2IL.Core/Utils/AsmResolver/AsmResolverUtils.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public static class AsmResolverUtils
1414
{
1515
private static readonly ConcurrentDictionary<string, TypeDefinition?> CachedTypeDefsByName = new();
1616
private static readonly ConcurrentDictionary<string, TypeSignature?> CachedTypeSignaturesByName = new();
17-
private static readonly ConcurrentDictionary<AssemblyDefinition, ReferenceImporter> ImportersByAssembly = new();
1817

1918
public static readonly ConcurrentDictionary<long, TypeDefinition> TypeDefsByIndex = new();
2019
public static readonly ConcurrentDictionary<long, GenericParameter> GenericParamsByIndexNew = new();
@@ -23,7 +22,6 @@ internal static void Reset()
2322
{
2423
CachedTypeDefsByName.Clear();
2524
CachedTypeSignaturesByName.Clear();
26-
ImportersByAssembly.Clear();
2725
TypeDefsByIndex.Clear();
2826
GenericParamsByIndexNew.Clear();
2927
}
@@ -80,12 +78,12 @@ public static TypeSignature GetTypeSignatureFromIl2CppType(ModuleDefinition modu
8078
case Il2CppTypeEnum.IL2CPP_TYPE_R8:
8179
case Il2CppTypeEnum.IL2CPP_TYPE_STRING:
8280
case Il2CppTypeEnum.IL2CPP_TYPE_TYPEDBYREF:
83-
ret = GetPrimitiveTypeDef(il2CppType.Type)
81+
ret = module.DefaultImporter.ImportType(GetPrimitiveTypeDef(il2CppType.Type))
8482
.ToTypeSignature();
8583
break;
8684
case Il2CppTypeEnum.IL2CPP_TYPE_CLASS:
8785
case Il2CppTypeEnum.IL2CPP_TYPE_VALUETYPE:
88-
ret = TypeDefsByIndex[il2CppType.Data.ClassIndex]
86+
ret = module.DefaultImporter.ImportType(TypeDefsByIndex[il2CppType.Data.ClassIndex])
8987
.ToTypeSignature();
9088
break;
9189
case Il2CppTypeEnum.IL2CPP_TYPE_ARRAY:
@@ -118,7 +116,7 @@ public static TypeSignature GetTypeSignatureFromIl2CppType(ModuleDefinition modu
118116
typeDefinition = GetTypeSignatureFromIl2CppType(module, type).Resolve() ?? throw new Exception("Unable to resolve base type for generic inst");
119117
}
120118

121-
var genericInstanceType = new GenericInstanceTypeSignature(typeDefinition!, typeDefinition!.IsValueType);
119+
var genericInstanceType = new GenericInstanceTypeSignature(module.DefaultImporter.ImportType(typeDefinition!), typeDefinition!.IsValueType);
122120

123121
//Get generic arguments
124122
var genericArgumentTypes = genericClass.Context.ClassInst.Types;
@@ -172,10 +170,10 @@ public static ITypeDefOrRef ImportReferenceFromIl2CppType(ModuleDefinition modul
172170
case Il2CppTypeEnum.IL2CPP_TYPE_STRING:
173171
case Il2CppTypeEnum.IL2CPP_TYPE_TYPEDBYREF:
174172
//This case, and the one below, are faster to go this way rather than delegating to type signature creation, because we can go straight from def -> ref.
175-
return GetPrimitiveTypeDef(il2CppType.Type);
173+
return module.DefaultImporter.ImportType(GetPrimitiveTypeDef(il2CppType.Type));
176174
case Il2CppTypeEnum.IL2CPP_TYPE_CLASS:
177175
case Il2CppTypeEnum.IL2CPP_TYPE_VALUETYPE:
178-
return TypeDefsByIndex[il2CppType.Data.ClassIndex];
176+
return module.DefaultImporter.ImportType(TypeDefsByIndex[il2CppType.Data.ClassIndex]);
179177
case Il2CppTypeEnum.IL2CPP_TYPE_ARRAY:
180178
case Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST:
181179
case Il2CppTypeEnum.IL2CPP_TYPE_PTR:
@@ -309,16 +307,6 @@ private static ParsedTypeString Parse(string name)
309307
return new ParsedTypeString(baseType, suffix, genericParams);
310308
}
311309

312-
public static ReferenceImporter GetImporter(this AssemblyDefinition assemblyDefinition)
313-
{
314-
if (ImportersByAssembly.TryGetValue(assemblyDefinition, out var ret))
315-
return ret;
316-
317-
ImportersByAssembly[assemblyDefinition] = ret = new(assemblyDefinition.Modules[0]);
318-
319-
return ret;
320-
}
321-
322310
public static ITypeDefOrRef ImportTypeIfNeeded(this ReferenceImporter importer, ITypeDefOrRef type)
323311
{
324312
if (type is TypeSpecification spec)

0 commit comments

Comments
 (0)