Implement core of Reflection.Type and some helper functions

This commit is contained in:
Katy Coe
2017-10-26 08:00:14 +02:00
parent 4ab71d8594
commit 6ba60a276f
5 changed files with 134 additions and 103 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Il2CppInspector.Reflection
{
@@ -14,8 +15,9 @@ namespace Il2CppInspector.Reflection
}
// Get the assembly in which a type is defined
public Assembly GetAssembly(Type type) {
throw new NotImplementedException();
}
public Assembly GetAssembly(Type type) => Assemblies.FirstOrDefault(x => x.DefinedTypes.Contains(type));
// Get a type from its IL2CPP type index
public Type GetTypeFromIndex(int typeIndex) => Assemblies.SelectMany(x => x.DefinedTypes).FirstOrDefault(x => x.Index == typeIndex);
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Il2CppInspector.Reflection {
public class Assembly
@@ -19,10 +20,8 @@ namespace Il2CppInspector.Reflection {
// List of types defined in the assembly
public List<Type> DefinedTypes { get; } = new List<Type>();
// Get a type from its string name
public Type GetType(string typeName) {
throw new NotImplementedException();
}
// Get a type from its string name (including namespace)
public Type GetType(string typeName) => DefinedTypes.FirstOrDefault(x => x.FullName == typeName);
// Initialize from specified assembly index in package
public Assembly(Il2CppInspector pkg, int imageIndex) {
@@ -34,7 +33,9 @@ namespace Il2CppInspector.Reflection {
// TODO: Generate EntryPoint method from entryPointIndex
}
// TODO: Generate types in DefinedTypes from typeStart to typeStart+typeCount-1
// Generate types in DefinedTypes from typeStart to typeStart+typeCount-1
for (int t = Definition.typeStart; t < Definition.typeStart + Definition.typeCount; t++)
DefinedTypes.Add(new Type(pkg, t, this));
}
}
}

View File

@@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Reflection;
namespace Il2CppInspector.Reflection {
public abstract class MemberInfo
{
// Assembly that this member is defined in
public Assembly Assembly { get; set; }
// Custom attributes for this member
public IEnumerable<CustomAttributeData> CustomAttributes { get; set; } // TODO
// Type that this type is declared in for nested types
public Type DeclaringType { get; set; } // TODO
// What sort of member this is, eg. method, field etc.
public MemberTypes MemberType { get; set; } // TODO
// Name of the member
public string Name { get; set; }
// TODO: GetCustomAttributes etc.
}
}

View File

@@ -1,103 +1,8 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Il2CppInspector.Reflection
{
public abstract class MemberInfo
{
// Assembly that this member is defined in
public Assembly Assembly { get; set; }
// Custom attributes for this member
public IEnumerable<CustomAttributeData> CustomAttributes { get; set; }
// Type that this type is declared in for nested types
public Type DeclaringType { get; set; }
// What sort of member this is, eg. method, field etc.
public MemberTypes MemberType { get; set; }
// Name of the member
public string Name { get; set; }
// TODO: GetCustomAttributes etc.
}
public class Type : MemberInfo
{
// (not code attributes)
// Undefined if the Type represents a generic type parameter
public TypeAttributes Attributes { get; set; }
// Type that this type inherits from
public Type BaseType { get; set; }
// TODO: ContainsGenericParameters
// Method that the type is declared in if this is a type parameter of a generic method
public MethodBase DeclaringMethod { get; set; }
// Gets the type of the object encompassed or referred to by the current array, pointer or reference type
public Type ElementType { get; set; }
// Type name including namespace
public string FullName { get; set; }
// TODO: Generic stuff
public bool HasElementType { get; set; }
public bool IsAbstract { get; set; }
public bool IsArray { get; set; }
public bool IsByRef { get; set; }
public bool IsClass { get; set; }
public bool IsEnum { get; set; }
public bool IsGenericParameter { get; set; }
public bool IsGenericType { get; set; }
public bool IsGenericTypeDefinition { get; set; }
public bool IsInterface { get; set; }
public bool IsNested { get; set; }
public bool IsNestedPrivate { get; set; }
public bool IsNestedPublic { get; set; }
public bool IsPointer { get; set; }
public bool IsPrimitive { get; set; }
public bool IsPublic { get; set; }
public bool IsSealed { get; set; }
public bool IsSerializable { get; set; }
public bool IsValueType { get; set; }
public string Namespace { get; set; }
// Number of dimensions of an array
public int GetArrayRank() => throw new NotImplementedException();
public List<ConstructorInfo> Constructors { get; set; }
public List<Type> Inerfaces { get; set; }
public List<MemberInfo> Members { get; set; }
public List<MethodInfo> Methods { get; set; }
public List<FieldInfo> Fields { get; set; }
public List<Type> NestedTypes { get; set; }
public List<PropertyInfo> Properties { get; set; }
// TODO: Custom attribute stuff
public string[] GetEnumNames() => throw new NotImplementedException();
public Type GetEnumUnderlyingType() => throw new NotImplementedException();
public Array GetEnumValues() => throw new NotImplementedException();
// TODO: Event stuff
// TODO: Generic stuff
}
public abstract class MethodBase : MemberInfo
{
// (not code attributes)

View File

@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Il2CppInspector.Reflection {
public class Type : MemberInfo
{
// IL2CPP-specific data
public Il2CppTypeDefinition Definition { get; }
public int Index { get; }
// (not code attributes)
// Undefined if the Type represents a generic type parameter
public TypeAttributes Attributes { get; } // TODO
// Type that this type inherits from
public Type BaseType { get; } // TODO
// TODO: ContainsGenericParameters
// Method that the type is declared in if this is a type parameter of a generic method
public MethodBase DeclaringMethod { get; } // TODO
// Gets the type of the object encompassed or referred to by the current array, pointer or reference type
public Type ElementType { get; } // TODO
// Type name including namespace
public string FullName => Namespace + "." + Name;
// TODO: Generic stuff
public bool HasElementType { get; } // TODO
public bool IsAbstract { get; }
public bool IsArray { get; } // TODO
public bool IsByRef { get; } // TODO
public bool IsClass { get; }
public bool IsEnum { get; } // TODO
public bool IsGenericParameter { get; } // TODO
public bool IsGenericType { get; } // TODO
public bool IsGenericTypeDefinition { get; } // TODO
public bool IsInterface { get; }
public bool IsNested { get; } // TODO
public bool IsNestedPrivate { get; } // TODO
public bool IsNestedPublic { get; } // TODO
public bool IsPointer { get; } // TODO
public bool IsPrimitive { get; } // TODO
public bool IsPublic { get; }
public bool IsSealed { get; }
public bool IsSerializable { get; }
public bool IsValueType { get; } // TODO
public string Namespace { get; }
// Number of dimensions of an array
public int GetArrayRank() => throw new NotImplementedException();
public List<ConstructorInfo> Constructors { get; } // TODO
public List<Type> Inerfaces { get; } // TODO
public List<MemberInfo> Members { get; } // TODO
public List<MethodInfo> Methods { get; } // TODO
public List<FieldInfo> Fields { get; } // TODO
public List<Type> NestedTypes { get; } // TODO
public List<PropertyInfo> Properties { get; } // TODO
// TODO: Custom attribute stuff
public string[] GetEnumNames() => throw new NotImplementedException();
public Type GetEnumUnderlyingType() => throw new NotImplementedException();
public Array GetEnumValues() => throw new NotImplementedException();
// TODO: Event stuff
// TODO: Generic stuff
// Initialize from specified type index in package
public Type(Il2CppInspector pkg, int typeIndex, Assembly owner) {
Assembly = owner;
Definition = pkg.Metadata.Types[typeIndex];
Index = typeIndex;
Name = pkg.Metadata.Strings[Definition.nameIndex];
Namespace = pkg.Metadata.Strings[Definition.namespaceIndex];
IsSerializable = (Definition.flags & DefineConstants.TYPE_ATTRIBUTE_SERIALIZABLE) != 0;
IsPublic = (Definition.flags & DefineConstants.TYPE_ATTRIBUTE_VISIBILITY_MASK) == DefineConstants.TYPE_ATTRIBUTE_PUBLIC;
IsAbstract = (Definition.flags & DefineConstants.TYPE_ATTRIBUTE_ABSTRACT) != 0;
IsSealed = (Definition.flags & DefineConstants.TYPE_ATTRIBUTE_SEALED) != 0;
IsInterface = (Definition.flags & DefineConstants.TYPE_ATTRIBUTE_INTERFACE) != 0;
IsClass = !IsInterface;
}
}
}