Skip to content

Commit

Permalink
Hide duplicate properties in MethodAnalysisContext (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 authored Oct 25, 2024
1 parent d9048e9 commit 03b6607
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ public void AnalyzeCustomAttributeData(bool allowAnalysis = true)
var attributeTypeContext = AppContext.ResolveContextForType(typeDef) ?? throw new("Unable to find type " + typeDef.FullName);

AnalyzedCustomAttribute attribute;
if (attributeTypeContext.Methods.FirstOrDefault(c => c.MethodName == ".ctor" && c.Definition!.parameterCount == 0) is { } constructor)
if (attributeTypeContext.Methods.FirstOrDefault(c => c.Name == ".ctor" && c.Definition!.parameterCount == 0) is { } constructor)
{
attribute = new(constructor);
}
else if (attributeTypeContext.Methods.FirstOrDefault(c => c.MethodName == ".ctor") is { } anyConstructor)
else if (attributeTypeContext.Methods.FirstOrDefault(c => c.Name == ".ctor") is { } anyConstructor)
{
//TODO change this to actual constructor w/ params once anaylsis is available
attribute = new(anyConstructor);
Expand Down
16 changes: 9 additions & 7 deletions Cpp2IL.Core/Model/Contexts/MethodAnalysisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class MethodAnalysisContext : HasCustomAttributesAndName, IMethodInfoProv
protected Memory<byte>? rawMethodBody;


private static List<IBlockProcessor> blockProcessors =
private static readonly List<IBlockProcessor> blockProcessors =
[
new MetadataProcessor(),
new CallProcessor()
Expand All @@ -103,7 +103,8 @@ public MethodAnalysisContext(Il2CppMethodDefinition? definition, TypeAnalysisCon
else
rawMethodBody = Array.Empty<byte>();
}


[MemberNotNull(nameof(rawMethodBody))]
public void EnsureRawBytes()
{
rawMethodBody ??= InitRawBytes();
Expand Down Expand Up @@ -173,17 +174,18 @@ public void ReleaseAnalysisData()

#region StableNameDot implementation

public ITypeInfoProvider ReturnType =>
ITypeInfoProvider IMethodInfoProvider.ReturnType =>
Definition!.RawReturnType!.ThisOrElementIsGenericParam()
? new GenericParameterTypeInfoProviderWrapper(Definition.RawReturnType!.GetGenericParamName())
: TypeAnalysisContext.GetSndnProviderForType(AppContext, Definition!.RawReturnType);

public IEnumerable<IParameterInfoProvider> ParameterInfoProviders => Parameters;
public string MethodName => Name;
IEnumerable<IParameterInfoProvider> IMethodInfoProvider.ParameterInfoProviders => Parameters;

string IMethodInfoProvider.MethodName => Name;

public MethodAttributes MethodAttributes => Attributes;
MethodAttributes IMethodInfoProvider.MethodAttributes => Attributes;

public MethodSemantics MethodSemantics
MethodSemantics IMethodInfoProvider.MethodSemantics
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private static void InjectAttribute(ApplicationAnalysisContext appContext)

if (convertedIsil is { Count: 0 })
{
if ((m.MethodAttributes & MethodAttributes.Abstract) == 0)
if ((m.Attributes & MethodAttributes.Abstract) == 0)
{
AttributeInjectionUtils.AddZeroParameterAttribute(m, analysisNotSupportedConstructor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ private static void CopyMethodsInType(ReferenceImporter importer, TypeAnalysisCo

var signature = methodCtx.IsStatic ? MethodSignature.CreateStatic(returnType, parameterTypes) : MethodSignature.CreateInstance(returnType, parameterTypes);

var managedMethod = new MethodDefinition(methodCtx.MethodName, (MethodAttributes)methodCtx.Attributes, signature);
var managedMethod = new MethodDefinition(methodCtx.Name, (MethodAttributes)methodCtx.Attributes, signature);

if (methodCtx.Definition != null)
{
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL.Core/Utils/AttributeInjectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static void ApplyAttributeUsageAttribute(ApplicationAnalysisContext appC
var mscorlibAssembly = appContext.GetAssemblyByName("mscorlib") ?? throw new("Could not find mscorlib");
var targetsEnumType = GetAttributeTargetsType(mscorlibAssembly);
var usageAttribute = mscorlibAssembly.GetTypeByFullName($"System.{nameof(AttributeUsageAttribute)}") ?? throw new("Could not find AttributeUsageAttribute");
var usageConstructor = usageAttribute.Methods.First(m => (m.MethodAttributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public && m.Name == ".ctor");
var usageConstructor = usageAttribute.Methods.First(m => (m.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public && m.Name == ".ctor");
var allowMultipleProperty = usageAttribute.Properties.First(p => p.Name == nameof(AttributeUsageAttribute.AllowMultiple));
foreach (var injectedType in multiAssemblyInjectedType.InjectedTypes)
{
Expand Down

0 comments on commit 03b6607

Please sign in to comment.