Implement and output events

This commit is contained in:
Katy Coe
2017-11-28 13:57:16 +01:00
parent 1a81e9a0fb
commit 09fd023a9f
6 changed files with 93 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
// Copyright (c) 2017 Katy Coe - https://www.djkaty.com - https://github.com/djkaty
// All rights reserved
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
@@ -129,6 +130,23 @@ namespace Il2CppInspector
if (type.DeclaredProperties.Count > 0)
writer.Write("\n");
// Events
if (type.DeclaredEvents.Count > 0)
writer.Write("\t// Events\n");
foreach (var evt in type.DeclaredEvents) {
string modifiers = evt.AddMethod?.GetModifierString();
writer.Write($"\t{modifiers} event {evt.EventHandlerType.CSharpName} {evt.Name} {{\n");
var m = new Dictionary<string, uint>();
if (evt.AddMethod != null) m.Add("add", evt.AddMethod.VirtualAddress);
if (evt.RemoveMethod != null) m.Add("remove", evt.RemoveMethod.VirtualAddress);
if (evt.RaiseMethod != null) m.Add("raise", evt.RaiseMethod.VirtualAddress);
writer.Write(string.Join("\n", m.Select(x => $"\t\t{x.Key}; // 0x{x.Value:X8}")) + "\n\t}\n");
}
if (type.DeclaredEvents.Count > 0)
writer.Write("\n");
// Methods
if (type.DeclaredMethods.Count > 0)
writer.Write("\t// Methods\n");
@@ -147,7 +165,7 @@ namespace Il2CppInspector
writer.Write("out ");
writer.Write($"{param.ParameterType.CSharpName} {param.Name}");
}
writer.Write("); // 0x{0:X}\n",
writer.Write("); // 0x{0:X8}\n",
method.VirtualAddress);
}
writer.Write("}\n");

View File

@@ -24,6 +24,7 @@ namespace Il2CppInspector
public Il2CppFieldDefinition[] Fields { get; }
public Il2CppFieldDefaultValue[] FieldDefaultValues { get; }
public Il2CppPropertyDefinition[] Properties { get; }
public Il2CppEventDefinition[] Events { get; }
public int[] InterfaceUsageIndices { get; }
@@ -55,8 +56,9 @@ namespace Il2CppInspector
Fields = ReadArray<Il2CppFieldDefinition>(Header.fieldsOffset, Header.fieldsCount / Sizeof(typeof(Il2CppFieldDefinition)));
FieldDefaultValues = ReadArray<Il2CppFieldDefaultValue>(Header.fieldDefaultValuesOffset, Header.fieldDefaultValuesCount / Sizeof(typeof(Il2CppFieldDefaultValue)));
Properties = ReadArray<Il2CppPropertyDefinition>(Header.propertiesOffset, Header.propertiesOffset / Sizeof(typeof(Il2CppPropertyDefinition)));
Events = ReadArray<Il2CppEventDefinition>(Header.eventsOffset, Header.eventsOffset / Sizeof(typeof(Il2CppEventDefinition)));
InterfaceUsageIndices = ReadArray<int>(Header.interfacesOffset, Header.interfacesCount / sizeof(int));
// TODO: Events, ParameterDefaultValue, GenericParameters, ParameterConstraints, GenericContainers, MetadataUsage, CustomAttributes
// TODO: ParameterDefaultValue, GenericParameters, ParameterConstraints, GenericContainers, MetadataUsage, CustomAttributes
// Get all string literals
Position = Header.stringOffset;

View File

@@ -226,4 +226,15 @@ namespace Il2CppInspector
public int customAttributeIndex;
public uint token;
}
public class Il2CppEventDefinition
{
public int nameIndex;
public int typeIndex;
public int add;
public int remove;
public int raise;
public int customAttributeIndex;
public uint token;
}
}

View File

@@ -0,0 +1,54 @@
/*
Copyright 2017 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com
All rights reserved.
*/
using System.Reflection;
namespace Il2CppInspector.Reflection
{
public class EventInfo : MemberInfo
{
// IL2CPP-specific data
public Il2CppEventDefinition Definition { get; }
public int Index { get; }
// Information/flags about the event
public EventAttributes Attributes { get; }
// Methods for the event
public MethodInfo AddMethod { get; }
public MethodInfo RemoveMethod { get; }
public MethodInfo RaiseMethod { get; }
// Event handler delegate type
private Il2CppType eventType;
public TypeInfo EventHandlerType => Assembly.Model.GetType(eventType, MemberTypes.TypeInfo);
// True if the event has a special name
public bool IsSpecialName => (Attributes & EventAttributes.SpecialName) == EventAttributes.SpecialName;
public override MemberTypes MemberType => MemberTypes.Event;
public EventInfo(Il2CppInspector pkg, int eventIndex, TypeInfo declaringType) :
base(declaringType) {
Definition = pkg.Metadata.Events[eventIndex];
Index = eventIndex;
Name = pkg.Strings[Definition.nameIndex];
eventType = pkg.TypeUsages[Definition.typeIndex];
if ((eventType.attrs & Il2CppConstants.FIELD_ATTRIBUTE_SPECIAL_NAME) == Il2CppConstants.FIELD_ATTRIBUTE_SPECIAL_NAME)
Attributes |= EventAttributes.SpecialName;
// NOTE: This relies on methods being added to TypeInfo.DeclaredMethods in the same order they are defined in the Il2Cpp metadata
// add, remove and raise are method indices from the first method of the declaring type
if (Definition.add >= 0)
AddMethod = declaringType.DeclaredMethods[Definition.add];
if (Definition.remove >= 0)
RemoveMethod = declaringType.DeclaredMethods[Definition.remove];
if (Definition.raise >= 0)
RaiseMethod = declaringType.DeclaredMethods[Definition.raise];
}
}
}

View File

@@ -8,6 +8,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Il2CppInspector.Reflection {
public class TypeInfo : MemberInfo
@@ -41,7 +42,7 @@ namespace Il2CppInspector.Reflection {
}
public List<ConstructorInfo> DeclaredConstructors => throw new NotImplementedException();
public List<EventInfo> DeclaredEvents => throw new NotImplementedException();
public List<EventInfo> DeclaredEvents { get; } = new List<EventInfo>();
public List<FieldInfo> DeclaredFields { get; } = new List<FieldInfo>();
public List<MemberInfo> DeclaredMembers => throw new NotImplementedException();
public List<MethodInfo> DeclaredMethods { get; } = new List<MethodInfo>();
@@ -129,8 +130,6 @@ namespace Il2CppInspector.Reflection {
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: Generic stuff
// Initialize from specified type index in metadata
@@ -198,6 +197,9 @@ namespace Il2CppInspector.Reflection {
for (var p = Definition.propertyStart; p < Definition.propertyStart + Definition.property_count; p++)
DeclaredProperties.Add(new PropertyInfo(pkg, p, this));
// Add all events
for (var e = Definition.eventStart; e < Definition.eventStart + Definition.event_count; e++)
DeclaredEvents.Add(new EventInfo(pkg, e, this));
MemberType = MemberTypes.TypeInfo;
}

View File

@@ -5,6 +5,7 @@ Extract types, methods, properties and fields from Unity IL2CPP binaries.
* Supports ARMv7, ARMv7 Thumb T1 and x86 architectures regardless of file format
* Supports metadata versions 21, 22, 23 and 24
* No manual reverse-engineering required; all data is calculated automatically
* Special language construct/syntax support for enumerations and events
* **Il2CppInspector** re-usable class library for low-level access to IL2CPP binaries and metadata
* **Il2CppReflector** re-usable class library for high-level .NET Reflection-style access to IL2CPP types and data as a tree model