diff --git a/Il2CppInspector/Il2CppReflector.cs b/Il2CppInspector/Il2CppReflector.cs index aeb22b1..1a38ff1 100644 --- a/Il2CppInspector/Il2CppReflector.cs +++ b/Il2CppInspector/Il2CppReflector.cs @@ -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); } } diff --git a/Il2CppInspector/Reflection/Assembly.cs b/Il2CppInspector/Reflection/Assembly.cs index 554f0a1..bbdb70e 100644 --- a/Il2CppInspector/Reflection/Assembly.cs +++ b/Il2CppInspector/Reflection/Assembly.cs @@ -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 DefinedTypes { get; } = new List(); - // 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)); } } } \ No newline at end of file diff --git a/Il2CppInspector/Reflection/MemberInfo.cs b/Il2CppInspector/Reflection/MemberInfo.cs new file mode 100644 index 0000000..29c82d1 --- /dev/null +++ b/Il2CppInspector/Reflection/MemberInfo.cs @@ -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 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. + } +} \ No newline at end of file diff --git a/Il2CppInspector/Reflection/ReflectionClasses.cs b/Il2CppInspector/Reflection/ReflectionClasses.cs index f5b30ff..f6d0446 100644 --- a/Il2CppInspector/Reflection/ReflectionClasses.cs +++ b/Il2CppInspector/Reflection/ReflectionClasses.cs @@ -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 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 Constructors { get; set; } - - public List Inerfaces { get; set; } - - public List Members { get; set; } - - public List Methods { get; set; } - - public List Fields { get; set; } - - public List NestedTypes { get; set; } - - public List 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) diff --git a/Il2CppInspector/Reflection/Type.cs b/Il2CppInspector/Reflection/Type.cs new file mode 100644 index 0000000..703f0cd --- /dev/null +++ b/Il2CppInspector/Reflection/Type.cs @@ -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 Constructors { get; } // TODO + + public List Inerfaces { get; } // TODO + + public List Members { get; } // TODO + + public List Methods { get; } // TODO + + public List Fields { get; } // TODO + + public List NestedTypes { get; } // TODO + + public List 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; + } + } +} \ No newline at end of file