Implement and output enumerations

This commit is contained in:
Katy Coe
2017-11-22 17:38:15 +01:00
parent b3883802bd
commit 1a81e9a0fb
3 changed files with 76 additions and 47 deletions

View File

@@ -4,6 +4,7 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
using Il2CppInspector.Reflection; using Il2CppInspector.Reflection;
@@ -50,60 +51,70 @@ namespace Il2CppInspector
else { else {
if (type.IsAbstract && !type.IsInterface) if (type.IsAbstract && !type.IsInterface)
writer.Write("abstract "); writer.Write("abstract ");
if (type.IsSealed && !type.IsValueType) if (type.IsSealed && !type.IsValueType && !type.IsEnum)
writer.Write("sealed "); writer.Write("sealed ");
} }
if (type.IsInterface) if (type.IsInterface)
writer.Write("interface "); writer.Write("interface ");
else if (type.IsValueType) else if (type.IsValueType)
writer.Write("struct "); writer.Write("struct ");
else if (type.IsEnum)
writer.Write("enum ");
else else
writer.Write("class "); writer.Write("class ");
var @base = type.ImplementedInterfaces.Select(x => x.CSharpName).ToList(); var @base = type.ImplementedInterfaces.Select(x => x.CSharpName).ToList();
if (type.BaseType != null && type.BaseType.FullName != "System.Object" && type.BaseType.FullName != "System.ValueType") if (type.BaseType != null && type.BaseType.FullName != "System.Object" && type.BaseType.FullName != "System.ValueType" && !type.IsEnum)
@base.Insert(0, type.BaseType.CSharpName); @base.Insert(0, type.BaseType.CSharpName);
if (type.IsEnum && type.ElementType.CSharpName != "int") // enums derive from int by default
@base.Insert(0, type.ElementType.CSharpName);
var baseText = @base.Count > 0 ? " : " + string.Join(", ", @base) : string.Empty; var baseText = @base.Count > 0 ? " : " + string.Join(", ", @base) : string.Empty;
writer.Write($"{type.Name}{baseText} // TypeDefIndex: {type.Index}\n{{\n"); writer.Write($"{type.Name}{baseText} // TypeDefIndex: {type.Index}\n{{\n");
// Fields // Fields
if (type.DeclaredFields.Count > 0) if (!type.IsEnum) {
writer.Write("\t// Fields\n"); if (type.DeclaredFields.Count > 0)
writer.Write("\t// Fields\n");
foreach (var field in type.DeclaredFields) { foreach (var field in type.DeclaredFields) {
writer.Write("\t"); writer.Write("\t");
if (field.IsNotSerialized) if (field.IsNotSerialized)
writer.Write("[NonSerialized]\t"); writer.Write("[NonSerialized]\t");
if (field.IsPrivate) if (field.IsPrivate)
writer.Write("private "); writer.Write("private ");
if (field.IsPublic) if (field.IsPublic)
writer.Write("public "); writer.Write("public ");
if (field.IsFamily) if (field.IsFamily)
writer.Write("protected "); writer.Write("protected ");
if (field.IsAssembly) if (field.IsAssembly)
writer.Write("internal "); writer.Write("internal ");
if (field.IsFamilyOrAssembly) if (field.IsFamilyOrAssembly)
writer.Write("protected internal "); writer.Write("protected internal ");
if (field.IsFamilyAndAssembly) if (field.IsFamilyAndAssembly)
writer.Write("[family and assembly] "); writer.Write("[family and assembly] ");
if (field.IsLiteral) if (field.IsLiteral)
writer.Write("const "); writer.Write("const ");
// All const fields are also static by implication // All const fields are also static by implication
else if (field.IsStatic) else if (field.IsStatic)
writer.Write("static "); writer.Write("static ");
if (field.IsInitOnly) if (field.IsInitOnly)
writer.Write("readonly "); writer.Write("readonly ");
if (field.IsPinvokeImpl) if (field.IsPinvokeImpl)
writer.Write("extern "); writer.Write("extern ");
writer.Write($"{field.FieldType.CSharpName} {field.Name}"); writer.Write($"{field.FieldType.CSharpName} {field.Name}");
if (field.HasDefaultValue) if (field.HasDefaultValue)
writer.Write($" = {field.DefaultValueString}"); writer.Write($" = {field.DefaultValueString}");
writer.Write("; // 0x{0:X}\n", field.Offset); writer.Write("; // 0x{0:X}\n", field.Offset);
}
if (type.DeclaredFields.Count > 0)
writer.Write("\n");
}
else {
writer.Write(string.Join(",\n", type.GetEnumNames().Zip(type.GetEnumValues().OfType<object>(),
(k, v) => new { k, v }).OrderBy(x => x.v).Select(x => $"\t{x.k} = {x.v}")) + "\n");
} }
if (type.DeclaredFields.Count > 0)
writer.Write("\n");
// Properties // Properties
if (type.DeclaredProperties.Count > 0) if (type.DeclaredProperties.Count > 0)

View File

@@ -48,12 +48,16 @@ namespace Il2CppInspector.Reflection
return new TypeInfo(this, pType, memberType); return new TypeInfo(this, pType, memberType);
default: default:
// Basic primitive types return GetTypeFromEnum(pType.type);
if ((int) pType.type >= Il2CppConstants.FullNameTypeString.Count)
return null;
return Assemblies.SelectMany(x => x.DefinedTypes).First(x => x.FullName == Il2CppConstants.FullNameTypeString[(int)pType.type]);
} }
} }
// Basic primitive types
public TypeInfo GetTypeFromEnum(Il2CppTypeEnum t) {
if ((int)t >= Il2CppConstants.FullNameTypeString.Count)
return null;
return Assemblies.SelectMany(x => x.DefinedTypes).First(x => x.FullName == Il2CppConstants.FullNameTypeString[(int)t]);
}
} }
} }

View File

@@ -52,7 +52,15 @@ namespace Il2CppInspector.Reflection {
public MethodBase DeclaringMethod => throw new NotImplementedException(); public MethodBase DeclaringMethod => throw new NotImplementedException();
// Gets the type of the object encompassed or referred to by the current array, pointer or reference type // Gets the type of the object encompassed or referred to by the current array, pointer or reference type
public TypeInfo ElementType { get; } private Il2CppType enumElementType;
private TypeInfo elementType;
public TypeInfo ElementType {
get {
if (IsEnum && elementType == null)
elementType = Assembly.Model.GetType(enumElementType, MemberTypes.TypeInfo);
return elementType;
}
}
// Type name including namespace // Type name including namespace
public string FullName => (IsPointer? "void *" : "") public string FullName => (IsPointer? "void *" : "")
@@ -75,7 +83,7 @@ namespace Il2CppInspector.Reflection {
public bool IsArray { get; } public bool IsArray { get; }
public bool IsByRef => throw new NotImplementedException(); public bool IsByRef => throw new NotImplementedException();
public bool IsClass => (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Class; public bool IsClass => (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Class;
public bool IsEnum => throw new NotImplementedException(); public bool IsEnum { get; }
public bool IsGenericParameter { get; } public bool IsGenericParameter { get; }
public bool IsGenericType => throw new NotImplementedException(); public bool IsGenericType => throw new NotImplementedException();
public bool IsGenericTypeDefinition => throw new NotImplementedException(); public bool IsGenericTypeDefinition => throw new NotImplementedException();
@@ -115,11 +123,11 @@ namespace Il2CppInspector.Reflection {
// TODO: Custom attribute stuff // TODO: Custom attribute stuff
public string[] GetEnumNames() => throw new NotImplementedException(); public string[] GetEnumNames() => IsEnum? DeclaredFields.Where(x => x.Name != "value__").Select(x => x.Name).ToArray() : throw new InvalidOperationException("Type is not an enumeration");
public TypeInfo GetEnumUnderlyingType() => throw new NotImplementedException(); public TypeInfo GetEnumUnderlyingType() => ElementType;
public Array GetEnumValues() => throw new NotImplementedException(); public Array GetEnumValues() => IsEnum? DeclaredFields.Where(x => x.Name != "value__").Select(x => x.DefaultValue).ToArray() : throw new InvalidOperationException("Type is not an enumeration");
// TODO: Event stuff // TODO: Event stuff
@@ -167,6 +175,12 @@ namespace Il2CppInspector.Reflection {
if ((Definition.flags & Il2CppConstants.TYPE_ATTRIBUTE_INTERFACE) != 0) if ((Definition.flags & Il2CppConstants.TYPE_ATTRIBUTE_INTERFACE) != 0)
Attributes |= TypeAttributes.Interface; Attributes |= TypeAttributes.Interface;
// Enumerations - bit 1 of bitfield indicates this (also the baseType will be System.Enum)
if (((Definition.bitfield >> 1) & 1) == 1) {
IsEnum = true;
enumElementType = pkg.TypeUsages[Definition.elementTypeIndex];
}
// Add all implemented interfaces // Add all implemented interfaces
implementedInterfaces = new Il2CppType[Definition.interfaces_count]; implementedInterfaces = new Il2CppType[Definition.interfaces_count];
for (var i = 0; i < Definition.interfaces_count; i++) for (var i = 0; i < Definition.interfaces_count; i++)
@@ -224,7 +238,7 @@ namespace Il2CppInspector.Reflection {
if (pType.type == Il2CppTypeEnum.IL2CPP_TYPE_ARRAY) { if (pType.type == Il2CppTypeEnum.IL2CPP_TYPE_ARRAY) {
var descriptor = image.ReadMappedObject<Il2CppArrayType>(pType.datapoint); var descriptor = image.ReadMappedObject<Il2CppArrayType>(pType.datapoint);
var elementType = image.ReadMappedObject<Il2CppType>(descriptor.etype); var elementType = image.ReadMappedObject<Il2CppType>(descriptor.etype);
ElementType = model.GetType(elementType); this.elementType = model.GetType(elementType);
Namespace = ElementType.Namespace; Namespace = ElementType.Namespace;
Name = ElementType.Name; Name = ElementType.Name;
@@ -235,7 +249,7 @@ namespace Il2CppInspector.Reflection {
// Dynamically allocated array // Dynamically allocated array
if (pType.type == Il2CppTypeEnum.IL2CPP_TYPE_SZARRAY) { if (pType.type == Il2CppTypeEnum.IL2CPP_TYPE_SZARRAY) {
var elementType = image.ReadMappedObject<Il2CppType>(pType.datapoint); var elementType = image.ReadMappedObject<Il2CppType>(pType.datapoint);
ElementType = model.GetType(elementType); this.elementType = model.GetType(elementType);
Namespace = ElementType.Namespace; Namespace = ElementType.Namespace;
Name = ElementType.Name; Name = ElementType.Name;