Skip to content

Commit

Permalink
Add methods for resolving il2cpp definitions to their contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed Oct 25, 2024
1 parent 02545f5 commit 6cb7237
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
53 changes: 53 additions & 0 deletions Cpp2IL.Core.Tests/MemberResolutionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Linq;

namespace Cpp2IL.Core.Tests;

public class MemberResolutionTests
{
[SetUp]
public void Setup()
{
Cpp2IlApi.ResetInternalState();
TestGameLoader.LoadSimple2019Game();
}

[Test]
public void TestMethodResolving()
{
var appContext = Cpp2IlApi.CurrentAppContext;

var methodContext = appContext!.AllTypes.SelectMany(t => t.Methods).First(m => m.Definition is not null);

Assert.That(appContext.ResolveContextForMethod(methodContext.Definition), Is.EqualTo(methodContext));
}

[Test]
public void TestFieldResolving()
{
var appContext = Cpp2IlApi.CurrentAppContext;

var fieldContext = appContext!.AllTypes.SelectMany(t => t.Fields).First(f => f.BackingData?.Field is not null);

Assert.That(appContext.ResolveContextForField(fieldContext.BackingData!.Field), Is.EqualTo(fieldContext));
}

[Test]
public void TestEventResolving()
{
var appContext = Cpp2IlApi.CurrentAppContext;

var eventContext = appContext!.AllTypes.SelectMany(t => t.Events).First(e => e.Definition is not null);

Assert.That(appContext.ResolveContextForEvent(eventContext.Definition), Is.EqualTo(eventContext));
}

[Test]
public void TestPropertyResolving()
{
var appContext = Cpp2IlApi.CurrentAppContext;

var propertyContext = appContext!.AllTypes.SelectMany(t => t.Properties).First(p => p.Definition is not null);

Assert.That(appContext.ResolveContextForProperty(propertyContext.Definition), Is.EqualTo(propertyContext));
}
}
27 changes: 26 additions & 1 deletion Cpp2IL.Core/Model/Contexts/ApplicationAnalysisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,32 @@ private void PopulateMethodsByAddressTable()
return AssembliesByName[name];
}

public TypeAnalysisContext? ResolveContextForType(Il2CppTypeDefinition typeDefinition) => GetAssemblyByName(typeDefinition.DeclaringAssembly!.Name!)?.GetTypeByDefinition(typeDefinition);
public TypeAnalysisContext? ResolveContextForType(Il2CppTypeDefinition? typeDefinition)
{
return typeDefinition is not null
? GetAssemblyByName(typeDefinition.DeclaringAssembly!.Name!)?.GetTypeByDefinition(typeDefinition)
: null;
}

public MethodAnalysisContext? ResolveContextForMethod(Il2CppMethodDefinition? methodDefinition)
{
return ResolveContextForType(methodDefinition?.DeclaringType)?.Methods.FirstOrDefault(m => m.Definition == methodDefinition);
}

public FieldAnalysisContext? ResolveContextForField(Il2CppFieldDefinition? field)
{
return ResolveContextForType(field?.DeclaringType)?.Fields.FirstOrDefault(f => f.BackingData?.Field == field);
}

public EventAnalysisContext? ResolveContextForEvent(Il2CppEventDefinition? eventDefinition)
{
return ResolveContextForType(eventDefinition?.DeclaringType)?.Events.FirstOrDefault(e => e.Definition == eventDefinition);
}

public PropertyAnalysisContext? ResolveContextForProperty(Il2CppPropertyDefinition? propertyDefinition)
{
return ResolveContextForType(propertyDefinition?.DeclaringType)?.Properties.FirstOrDefault(p => p.Definition == propertyDefinition);
}

public BaseKeyFunctionAddresses GetOrCreateKeyFunctionAddresses()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ private ConcreteGenericMethodAnalysisContext(Cpp2IlMethodRef methodRef, Assembly
DeclaringAsm = declaringAssembly;
BaseMethodContext = ResolveBaseMethod(methodRef, declaringAssembly.GetTypeByDefinition(methodRef.DeclaringType)!);

var genericTypeParameters = ResolveTypeArray(methodRef.TypeGenericParams, declaringAssembly);
var genericMethodParameters = ResolveTypeArray(methodRef.MethodGenericParams, declaringAssembly);
var genericTypeParameters = ResolveTypeGenericParameters();
var genericMethodParameters = ResolveMethodGenericParameters();

for (var i = 0; i < BaseMethodContext.Parameters.Count; i++)
{
Expand All @@ -58,6 +58,10 @@ private ConcreteGenericMethodAnalysisContext(Cpp2IlMethodRef methodRef, Assembly
rawMethodBody = AppContext.InstructionSet.GetRawBytesForMethod(this, false);
}

public TypeAnalysisContext[] ResolveTypeGenericParameters() => ResolveTypeArray(MethodRef.TypeGenericParams, DeclaringAsm);

public TypeAnalysisContext[] ResolveMethodGenericParameters() => ResolveTypeArray(MethodRef.MethodGenericParams, DeclaringAsm);

private static AssemblyAnalysisContext ResolveDeclaringAssembly(Cpp2IlMethodRef methodRef, ApplicationAnalysisContext context)
{
return context.GetAssemblyByName(methodRef.DeclaringType.DeclaringAssembly!.Name!)
Expand Down

0 comments on commit 6cb7237

Please sign in to comment.