Model: Groundwork for custom attribute processisng

This commit is contained in:
Katy Coe
2019-11-03 06:44:50 +01:00
parent defd553e0e
commit 7f398f40cb
6 changed files with 116 additions and 4 deletions

View File

@@ -31,8 +31,6 @@ namespace Il2CppInspector.Reflection
// List of all methods ordered by their MethodDefinitionIndex
public MethodBase[] MethodsByDefinitionIndex { get; }
// List of all types
public Il2CppModel(Il2CppInspector package) {
Package = package;
TypesByDefinitionIndex = new TypeInfo[package.TypeDefinitions.Length];
@@ -108,5 +106,19 @@ namespace Il2CppInspector.Reflection
TypesByVirtualAddress.Add(ptr, newUsage);
return newUsage;
}
// Attribute management
private int getCustomAttributeIndex(Assembly asm, uint token, int customAttributeIndex) {
var image = asm.Definition;
throw new NotImplementedException();
}
public int GetCustomAttributeIndex(Assembly asm) => getCustomAttributeIndex(asm, asm.Definition.token, -1);
public int GetCustomAttributeIndex(EventInfo evt) => getCustomAttributeIndex(evt.Assembly, evt.Definition.token, evt.Definition.customAttributeIndex);
public int GetCustomAttributeIndex(FieldInfo field) => getCustomAttributeIndex(field.Assembly, field.Definition.token, field.Definition.customAttributeIndex);
public int GetCustomAttributeIndex(MethodBase method) => getCustomAttributeIndex(method.Assembly, method.Definition.token, method.Definition.customAttributeIndex);
public int GetCustomAttributeIndex(ParameterInfo param) => getCustomAttributeIndex(param.Member.Assembly, param.Definition.token, param.Definition.customAttributeIndex);
public int GetCustomAttributeIndex(PropertyInfo prop) => getCustomAttributeIndex(prop.Assembly, prop.Definition.token, prop.Definition.customAttributeIndex);
}
}

View File

@@ -28,9 +28,11 @@ namespace Il2CppInspector
public Il2CppEventDefinition[] Events { get; }
public Il2CppGenericContainer[] GenericContainers { get; }
public Il2CppGenericParameter[] GenericParameters { get; }
public Il2CppCustomAttributeTypeRange[] AttributeTypeRanges { get; }
public int[] InterfaceUsageIndices { get; }
public int[] NestedTypeIndices { get; }
public int[] AttributeRangeIndices { get; }
public Dictionary<int, string> Strings { get; } = new Dictionary<int, string>();
@@ -102,7 +104,12 @@ namespace Il2CppInspector
NestedTypeIndices = ReadArray<int>(Header.nestedTypesOffset, Header.nestedTypesCount / sizeof(int));
GenericContainers = ReadArray<Il2CppGenericContainer>(Header.genericContainersOffset, Header.genericContainersCount / Sizeof(typeof(Il2CppGenericContainer)));
GenericParameters = ReadArray<Il2CppGenericParameter>(Header.genericParametersOffset, Header.genericParametersCount / Sizeof(typeof(Il2CppGenericParameter)));
// TODO: ParameterDefaultValue, ParameterConstraints, MetadataUsage, CustomAttributes
if (Version >= 21) {
AttributeRangeIndices = ReadArray<int>(Header.attributeTypesOffset, Header.attributeTypesCount / sizeof(int));
AttributeTypeRanges = ReadArray<Il2CppCustomAttributeTypeRange>(Header.attributesInfoOffset, Header.attributesInfoCount / Sizeof(typeof(Il2CppCustomAttributeTypeRange)));
}
// TODO: ParameterDefaultValue, ParameterConstraints, MetadataUsage
// Get all string literals
Position = Header.stringOffset;

View File

@@ -322,4 +322,13 @@ namespace Il2CppInspector
public ushort num;
public ushort flags;
}
public class Il2CppCustomAttributeTypeRange
{
[Version(Min = 24.1)]
public uint token;
public int start;
public int count;
}
}

View File

@@ -6,8 +6,10 @@
namespace Il2CppInspector.Reflection
{
// See: https://docs.microsoft.com/en-us/dotnet/api/system.reflection.customattributedata?view=netframework-4.8
public class CustomAttributeData
{
// TODO
// TODO: CustomAttributeData
}
}

View File

@@ -27,6 +27,7 @@
</ItemGroup>
<ItemGroup>
<Compile Remove="TestSources\CustomAttributeData.cs" />
<Compile Remove="TestSources\GenericTypes.cs" />
<Compile Remove="TestSources\Methods.cs" />
</ItemGroup>
@@ -35,6 +36,7 @@
<Content Include="TestExpectedResults\GameAssembly-Methods-x64.cs" />
<Content Include="TestExpectedResults\GameAssembly-Methods-x86.cs" />
<Content Include="TestExpectedResults\Methods.cs" />
<Content Include="TestSources\CustomAttributeData.cs" />
<Content Include="TestSources\GenericTypes.cs" />
<Content Include="TestSources\Methods.cs" />
</ItemGroup>

View File

@@ -0,0 +1,80 @@
/*
Copyright 2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com
All rights reserved.
*/
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Il2CppTests.TestSources;
// This code is adapted from https://docs.microsoft.com/en-us/dotnet/api/system.reflection.customattributedata?view=netframework-4.8
// The example attribute is applied to the assembly.
[assembly: Example(ExampleKind.ThirdKind, Note = "This is a note on the assembly.")]
namespace Il2CppTests.TestSources
{
// An enumeration used by the ExampleAttribute class.
public enum ExampleKind
{
FirstKind,
SecondKind,
ThirdKind,
FourthKind
};
// An example attribute. The attribute can be applied to all
// targets, from assemblies to parameters.
[AttributeUsage(AttributeTargets.All)]
public class ExampleAttribute : Attribute
{
// Data for properties.
private ExampleKind kindValue;
private string noteValue;
private string[] arrayStrings;
private int[] arrayNumbers;
// Constructors. The parameterless constructor (.ctor) calls
// the constructor that specifies ExampleKind and an array of
// strings, and supplies the default values.
public ExampleAttribute(ExampleKind initKind, string[] initStrings) {
kindValue = initKind;
arrayStrings = initStrings;
}
public ExampleAttribute(ExampleKind initKind) : this(initKind, null) { }
public ExampleAttribute() : this(ExampleKind.FirstKind, null) { }
// Properties. The Note and Numbers properties must be read/write, so they
// can be used as named parameters.
public ExampleKind Kind => kindValue;
public string[] Strings => arrayStrings;
public string Note {
get { return noteValue; }
set { noteValue = value; }
}
public int[] Numbers {
get { return arrayNumbers; }
set { arrayNumbers = value; }
}
}
// The example attribute is applied to the test class.
[Example(ExampleKind.SecondKind,
new[] { "String array argument, line 1",
"String array argument, line 2",
"String array argument, line 3" },
Note = "This is a note on the class.",
Numbers = new[] { 53, 57, 59 })]
public class Test
{
// The example attribute is applied to a method, using the
// parameterless constructor and supplying a named argument.
// The attribute is also applied to the method parameter.
[Example(Note = "This is a note on a method.")]
public void TestMethod([Example] object arg) { }
}
}