Implement ParameterInfo and return parameters

This commit is contained in:
Katy Coe
2017-11-08 06:34:12 +01:00
parent fdeb85aaff
commit f5b6f66f35
3 changed files with 50 additions and 12 deletions

View File

@@ -20,7 +20,7 @@ namespace Il2CppInspector.Reflection
// TODO: Custom attribute stuff
public List<ParameterInfo> DeclaredParameters => throw new NotImplementedException();
public List<ParameterInfo> DeclaredParameters { get; } = new List<ParameterInfo>();
public bool IsAbstract => (Attributes & MethodAttributes.Abstract) == MethodAttributes.Abstract;
public bool IsAssembly => throw new NotImplementedException();
@@ -38,7 +38,6 @@ namespace Il2CppInspector.Reflection
public bool IsVirtual => (Attributes & MethodAttributes.Virtual) == MethodAttributes.Virtual;
// TODO: GetMethodBody()
// TODO: GetParameters()
protected MethodBase(TypeInfo declaringType) : base(declaringType) { }
}

View File

@@ -5,6 +5,7 @@
*/
using System;
using System.Linq;
using System.Reflection;
namespace Il2CppInspector.Reflection
@@ -24,7 +25,6 @@ namespace Il2CppInspector.Reflection
// Return type of the method
private readonly Il2CppType returnType;
public TypeInfo ReturnType => Assembly.Model.GetType(returnType, MemberTypes.TypeInfo);
// TODO: ReturnTypeCustomAttributes
@@ -39,8 +39,6 @@ namespace Il2CppInspector.Reflection
}
Name = pkg.Strings[Definition.nameIndex];
returnType = pkg.TypeUsages[Definition.returnType];
if ((Definition.flags & DefineConstants.METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == DefineConstants.METHOD_ATTRIBUTE_PRIVATE)
Attributes |= MethodAttributes.Private;
if ((Definition.flags & DefineConstants.METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == DefineConstants.METHOD_ATTRIBUTE_PUBLIC)
@@ -49,6 +47,16 @@ namespace Il2CppInspector.Reflection
Attributes |= MethodAttributes.Virtual;
if ((Definition.flags & DefineConstants.METHOD_ATTRIBUTE_STATIC) != 0)
Attributes |= MethodAttributes.Static;
// Add return parameter
returnType = pkg.TypeUsages[Definition.returnType];
ReturnParameter = new ParameterInfo(pkg, -1, this);
// Add arguments
for (var p = Definition.parameterStart; p < Definition.parameterStart + Definition.parameterCount; p++)
DeclaredParameters.Add(new ParameterInfo(pkg, p, this));
}
public override string ToString() => ReturnType.Name + " " + Name + "(" + string.Join(", ", DeclaredParameters.Select(x => x.ParameterType.Name)) + ")";
}
}

View File

@@ -17,14 +17,15 @@ namespace Il2CppInspector.Reflection
// TODO: CustomAttributes
// True if the parameter has a default value
public bool HasDefaultValue { get; }
public bool HasDefaultValue => (Attributes & ParameterAttributes.HasDefault) != 0;
// Default value for the parameter
public object DefaultValue { get; }
public object DefaultValue => throw new NotImplementedException();
public bool IsIn { get; }
public bool IsOptional { get; }
public bool IsOut { get; }
public bool IsIn => (Attributes & ParameterAttributes.In) != 0;
public bool IsOptional => (Attributes & ParameterAttributes.Optional) != 0;
public bool IsOut => (Attributes & ParameterAttributes.Out) != 0;
public bool IsRetval => (Attributes & ParameterAttributes.Retval) != 0;
// The member in which the parameter is defined
public MemberInfo Member { get; }
@@ -33,9 +34,39 @@ namespace Il2CppInspector.Reflection
public string Name { get; }
// Type of this parameter
public TypeInfo ParameterType { get; }
private readonly Il2CppType paramType;
public TypeInfo ParameterType => Member.Assembly.Model.GetType(paramType, MemberTypes.TypeInfo);
// Zero-indexed position of the parameter in parameter list
public int Position { get; }
public int Position { get; private set; }
// Create a parameter. Specify paramIndex == -1 for a return type parameter
public ParameterInfo(Il2CppInspector pkg, int paramIndex, MethodInfo declaringMethod) {
Member = declaringMethod;
if (paramIndex == -1) {
Position = -1;
paramType = pkg.TypeUsages[declaringMethod.Definition.returnType];
Attributes |= ParameterAttributes.Retval;
return;
}
var param = pkg.Metadata.Params[paramIndex];
Name = pkg.Metadata.Strings[param.nameIndex];
Position = paramIndex - declaringMethod.Definition.parameterStart;
paramType = pkg.TypeUsages[param.typeIndex];
if ((paramType.attrs & DefineConstants.PARAM_ATTRIBUTE_OPTIONAL) != 0)
Attributes |= ParameterAttributes.Optional;
if ((paramType.attrs & DefineConstants.PARAM_ATTRIBUTE_OUT) != 0)
Attributes |= ParameterAttributes.Out;
if (Position == -1)
Attributes |= ParameterAttributes.Retval;
else if (!IsOut)
Attributes |= ParameterAttributes.In;
// TODO: DefaultValue/HasDefaultValue
}
}
}