diff --git a/Cpp2IL.Core/Model/Contexts/GenericParameterTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/GenericParameterTypeAnalysisContext.cs index a998ed96..ebb86125 100644 --- a/Cpp2IL.Core/Model/Contexts/GenericParameterTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/GenericParameterTypeAnalysisContext.cs @@ -6,7 +6,9 @@ namespace Cpp2IL.Core.Model.Contexts; public class GenericParameterTypeAnalysisContext : ReferencedTypeAnalysisContext { - public override string DefaultName { get; } + public sealed override string DefaultName { get; } + + public sealed override string DefaultNs => ""; public int Index { get; } diff --git a/Cpp2IL.Core/Model/CustomAttributes/CustomAttributeNullParameter.cs b/Cpp2IL.Core/Model/CustomAttributes/CustomAttributeNullParameter.cs new file mode 100644 index 00000000..757f9f76 --- /dev/null +++ b/Cpp2IL.Core/Model/CustomAttributes/CustomAttributeNullParameter.cs @@ -0,0 +1,13 @@ +using System.IO; +using Cpp2IL.Core.Model.Contexts; + +namespace Cpp2IL.Core.Model.CustomAttributes; + +public sealed class CustomAttributeNullParameter : BaseCustomAttributeParameter +{ + public CustomAttributeNullParameter(AnalyzedCustomAttribute owner, CustomAttributeParameterKind kind, int index) : base(owner, kind, index) + { + } + + public override void ReadFromV29Blob(BinaryReader reader, ApplicationAnalysisContext context) => throw new System.NotSupportedException(); +} diff --git a/Cpp2IL.Core/ProcessingLayers/CallAnalysisProcessingLayer.cs b/Cpp2IL.Core/ProcessingLayers/CallAnalysisProcessingLayer.cs index 925585a6..90fb04a7 100644 --- a/Cpp2IL.Core/ProcessingLayers/CallAnalysisProcessingLayer.cs +++ b/Cpp2IL.Core/ProcessingLayers/CallAnalysisProcessingLayer.cs @@ -175,17 +175,17 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn } else { - typeField = (callsAttributeInfo.Item2[1], typeFullName); + typeField = (callsAttributeInfo.Item2[0], typeFullName); } - var memberField = (callsAttributeInfo.Item2[2], targetMethod.Name); + var memberField = (callsAttributeInfo.Item2[1], targetMethod.Name); (FieldAnalysisContext, object)? typeParametersField; if (targetMethod is ConcreteGenericMethodAnalysisContext concreteMethod) { if (concreteMethod.MethodRef.MethodGenericParams.Length > 0) { - var parameters = new TypeAnalysisContext[concreteMethod.MethodRef.MethodGenericParams.Length]; + var parameters = new object?[concreteMethod.MethodRef.MethodGenericParams.Length]; for (var i = 0; i < parameters.Length; i++) { @@ -194,9 +194,13 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn { parameters[i] = parameterType; } + else + { + parameters[i] = parameterType?.FullName; + } } - typeParametersField = (callsAttributeInfo.Item2[3], parameters); + typeParametersField = (callsAttributeInfo.Item2[2], parameters); } else { @@ -211,7 +215,7 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn (FieldAnalysisContext, object)? parametersField; if (targetMethod.ParameterCount > 0) { - var parameters = new TypeAnalysisContext[targetMethod.ParameterCount]; + var parameters = new object?[targetMethod.ParameterCount]; for (var i = 0; i < parameters.Length; i++) { @@ -220,9 +224,13 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn { parameters[i] = parameterType; } + else + { + parameters[i] = parameterType?.FullName; + } } - parametersField = (callsAttributeInfo.Item2[4], parameters); + parametersField = (callsAttributeInfo.Item2[3], parameters); } else { @@ -245,11 +253,10 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn methodName, AttributeTargets.Method, true, - (appContext.SystemTypes.SystemTypeType, "Type"), - (appContext.SystemTypes.SystemStringType, "TypeFullName"), + (appContext.SystemTypes.SystemObjectType, "Type"), (appContext.SystemTypes.SystemStringType, "Member"), - (appContext.SystemTypes.SystemTypeType.MakeSzArrayType(), "MemberTypeParameters"), - (appContext.SystemTypes.SystemTypeType.MakeSzArrayType(), "MemberParameters")); + (appContext.SystemTypes.SystemObjectType.MakeSzArrayType(), "MemberTypeParameters"), + (appContext.SystemTypes.SystemObjectType.MakeSzArrayType(), "MemberParameters")); } private static bool TryGetCommonMethodFromList(List methods, [NotNullWhen(true)] out MethodAnalysisContext? commonMethod) diff --git a/Cpp2IL.Core/Utils/AsmResolver/AsmResolverAssemblyPopulator.cs b/Cpp2IL.Core/Utils/AsmResolver/AsmResolverAssemblyPopulator.cs index d101fb49..d76b7da5 100644 --- a/Cpp2IL.Core/Utils/AsmResolver/AsmResolverAssemblyPopulator.cs +++ b/Cpp2IL.Core/Utils/AsmResolver/AsmResolverAssemblyPopulator.cs @@ -112,6 +112,7 @@ private static CustomAttributeArgument BuildArrayArgument(AssemblyDefinition par CustomAttributePrimitiveParameter primitiveParameter => primitiveParameter.PrimitiveValue, CustomAttributeEnumParameter enumParameter => enumParameter.UnderlyingPrimitiveParameter.PrimitiveValue, BaseCustomAttributeTypeParameter type => (object?)type.TypeContext?.ToTypeSignature(parentAssembly.ManifestModule!), + CustomAttributeNullParameter => null, _ => throw new("Not supported array element type: " + e.GetType().FullName) }; diff --git a/Cpp2IL.Core/Utils/AttributeInjectionUtils.cs b/Cpp2IL.Core/Utils/AttributeInjectionUtils.cs index f4b01c95..c6148382 100644 --- a/Cpp2IL.Core/Utils/AttributeInjectionUtils.cs +++ b/Cpp2IL.Core/Utils/AttributeInjectionUtils.cs @@ -172,16 +172,29 @@ private static Il2CppType GetAttributeTargetsType(AssemblyAnalysisContext mscorl private static BaseCustomAttributeParameter MakeFieldParameter(object fieldValue, AnalyzedCustomAttribute owner, int index) { - return fieldValue switch + return MakeCustomAttributeParameter(fieldValue, owner, index, CustomAttributeParameterKind.Field); + } + + private static BaseCustomAttributeParameter MakeCustomAttributeParameter(object? value, AnalyzedCustomAttribute owner, int index, CustomAttributeParameterKind parameterKind) + { + return value switch { - Il2CppType type => new CustomAttributeTypeParameter(type, owner, CustomAttributeParameterKind.Field, index), - TypeAnalysisContext type => new InjectedCustomAttributeTypeParameter(type, owner, CustomAttributeParameterKind.Field, index), - TypeAnalysisContext[] types => new CustomAttributeArrayParameter(owner, CustomAttributeParameterKind.Field, index) + Il2CppType type => new CustomAttributeTypeParameter(type, owner, parameterKind, index), + TypeAnalysisContext type => new InjectedCustomAttributeTypeParameter(type, owner, parameterKind, index), + TypeAnalysisContext?[] types => new CustomAttributeArrayParameter(owner, parameterKind, index) { ArrType = Il2CppTypeEnum.IL2CPP_TYPE_IL2CPP_TYPE_INDEX, - ArrayElements = types.Select(t => new InjectedCustomAttributeTypeParameter(t, owner, CustomAttributeParameterKind.ArrayElement, index)).Cast().ToList() + ArrayElements = types.Select(t => (BaseCustomAttributeParameter)new InjectedCustomAttributeTypeParameter(t, owner, CustomAttributeParameterKind.ArrayElement, index)).ToList() + }, + object?[] objects => new CustomAttributeArrayParameter(owner, parameterKind, index) + { + ArrType = Il2CppTypeEnum.IL2CPP_TYPE_OBJECT, + ArrayElements = objects.Select(obj => obj is null + ? new CustomAttributeNullParameter(owner, CustomAttributeParameterKind.ArrayElement, index) + : MakeCustomAttributeParameter(obj, owner, index, CustomAttributeParameterKind.ArrayElement)).ToList() }, - IConvertible convertible => new CustomAttributePrimitiveParameter(convertible, owner, CustomAttributeParameterKind.Field, index), + IConvertible convertible => new CustomAttributePrimitiveParameter(convertible, owner, parameterKind, index), + null => new CustomAttributeNullParameter(owner, parameterKind, index), _ => throw new NotSupportedException(), }; }