SamboyCoding / Cpp2IL

Work-in-progress tool to reverse unity's IL2CPP toolchain.
MIT License
1.6k stars 187 forks source link

TypeAnalysisContext for Custom Attribute parameters #183

Closed ds5678 closed 1 year ago

ds5678 commented 1 year ago

Issue

Currently, an Il2CppType is required for specifying a type as a parameter in a Custom Attribute. This has several problems:

Using a TypeAnalysisContext would be cleaner and more versatile.

Design Proposal

Currently, CustomAttributeTypeParameter is only used in a handful of locations. It should be trivial to add a new derived class of BaseCustomAttributeParameter and update the required locations.

public class InjectedCustomAttributeTypeParameter : BaseCustomAttributeParameter
{
    public TypeAnalysisContext? Type { get; }

    public InjectedCustomAttributeTypeParameter(TypeAnalysisContext? type, AnalyzedCustomAttribute owner, CustomAttributeParameterKind kind, int index) : base(owner, kind, index)
    {
        Type = type;
    }
}

Alternative Design

We could make a common base class for Custom Attribute type parameters.

public abstract class BaseCustomAttributeTypeParameter : BaseCustomAttributeParameter
{
    public BaseCustomAttributeTypeParameter(AnalyzedCustomAttribute owner, CustomAttributeParameterKind kind, int index) : base(owner, kind, index)
    {
    }
    public abstract TypeSignature? ToTypeSignature(ModuleDefinition parentModule);
}

This would allow us to abstract over the origin of the attribute parameter and prevent duplicate code.