Output: Minor refactoring of attribute and address output

This commit is contained in:
Katy Coe
2019-11-04 17:34:21 +01:00
parent 6e93d83c8b
commit 9ceb1368e2
3 changed files with 37 additions and 33 deletions

View File

@@ -21,19 +21,14 @@ namespace Il2CppInspector
private StreamWriter writer; private StreamWriter writer;
private string formatAddress(ulong address) => model.Package.BinaryImage.Bits == 32
? string.Format($"0x{(uint) address:X8}")
: string.Format($"0x{address:X16}");
public void WriteFile(string outFile) { public void WriteFile(string outFile) {
using (writer = new StreamWriter(new FileStream(outFile, FileMode.Create), Encoding.UTF8)) { using (writer = new StreamWriter(new FileStream(outFile, FileMode.Create), Encoding.UTF8)) {
foreach (var asm in model.Assemblies) { foreach (var asm in model.Assemblies) {
writer.Write($"// Image {asm.Index}: {asm.FullName} - {asm.Definition.typeStart}\n"); writer.Write($"// Image {asm.Index}: {asm.FullName} - {asm.Definition.typeStart}\n");
// Assembly-level attributes // Assembly-level attributes
var attributes = asm.CustomAttributes; writer.Write(asm.CustomAttributes.ToString(attributePrefix: "assembly: "));
writer.Write(attributeText(attributes, attributePrefix: "assembly: ")); if (asm.CustomAttributes.Any())
if (attributes.Any())
writer.Write("\n"); writer.Write("\n");
} }
writer.Write("\n"); writer.Write("\n");
@@ -66,7 +61,7 @@ namespace Il2CppInspector
writer.Write(prefix + "[Serializable]\n"); writer.Write(prefix + "[Serializable]\n");
// Custom attributes // Custom attributes
writer.Write(attributeText(type.CustomAttributes, prefix)); writer.Write(type.CustomAttributes.ToString(prefix));
writer.Write(prefix); writer.Write(prefix);
if (type.IsPublic || type.IsNestedPublic) if (type.IsPublic || type.IsNestedPublic)
@@ -99,7 +94,7 @@ namespace Il2CppInspector
writer.Write("out "); writer.Write("out ");
writer.Write($"{param.ParameterType.CSharpName} {param.Name}"); writer.Write($"{param.ParameterType.CSharpName} {param.Name}");
} }
writer.Write($"); // TypeDefIndex: {type.Index}; {formatAddress(del.VirtualAddress)}\n"); writer.Write($"); // TypeDefIndex: {type.Index}; {Il2CppModel.FormatAddress(del.VirtualAddress)}\n");
return; return;
} }
@@ -140,7 +135,7 @@ namespace Il2CppInspector
writer.Write(prefix + "\t[NonSerialized]\n"); writer.Write(prefix + "\t[NonSerialized]\n");
// Attributes // Attributes
writer.Write(attributeText(field.CustomAttributes, prefix + "\t")); writer.Write(field.CustomAttributes.ToString(prefix + "\t"));
writer.Write(prefix + "\t"); writer.Write(prefix + "\t");
if (field.IsPrivate) if (field.IsPrivate)
@@ -191,7 +186,7 @@ namespace Il2CppInspector
foreach (var prop in type.DeclaredProperties) { foreach (var prop in type.DeclaredProperties) {
// Attributes // Attributes
writer.Write(attributeText(prop.CustomAttributes, prefix + "\t")); writer.Write(prop.CustomAttributes.ToString(prefix + "\t"));
string modifiers = prop.GetMethod?.GetModifierString() ?? prop.SetMethod.GetModifierString(); string modifiers = prop.GetMethod?.GetModifierString() ?? prop.SetMethod.GetModifierString();
writer.Write($"{prefix}\t{modifiers}{prop.PropertyType.CSharpName} {prop.Name} {{ "); writer.Write($"{prefix}\t{modifiers}{prop.PropertyType.CSharpName} {prop.Name} {{ ");
@@ -199,8 +194,8 @@ namespace Il2CppInspector
writer.Write((prop.GetMethod != null ? "get; " : "") + (prop.SetMethod != null ? "set; " : "") + "}"); writer.Write((prop.GetMethod != null ? "get; " : "") + (prop.SetMethod != null ? "set; " : "") + "}");
if ((prop.GetMethod != null && prop.GetMethod.VirtualAddress != 0) || (prop.SetMethod != null && prop.SetMethod.VirtualAddress != 0)) if ((prop.GetMethod != null && prop.GetMethod.VirtualAddress != 0) || (prop.SetMethod != null && prop.SetMethod.VirtualAddress != 0))
writer.Write(" // "); writer.Write(" // ");
writer.Write((prop.GetMethod != null && prop.GetMethod.VirtualAddress != 0 ? formatAddress(prop.GetMethod.VirtualAddress) + " " : "") writer.Write((prop.GetMethod != null && prop.GetMethod.VirtualAddress != 0 ? Il2CppModel.FormatAddress(prop.GetMethod.VirtualAddress) + " " : "")
+ (prop.SetMethod != null && prop.SetMethod.VirtualAddress != 0 ? formatAddress(prop.SetMethod.VirtualAddress) : "") + "\n"); + (prop.SetMethod != null && prop.SetMethod.VirtualAddress != 0 ? Il2CppModel.FormatAddress(prop.SetMethod.VirtualAddress) : "") + "\n");
usedMethods.Add(prop.GetMethod); usedMethods.Add(prop.GetMethod);
usedMethods.Add(prop.SetMethod); usedMethods.Add(prop.SetMethod);
} }
@@ -213,7 +208,7 @@ namespace Il2CppInspector
foreach (var evt in type.DeclaredEvents) { foreach (var evt in type.DeclaredEvents) {
// Attributes // Attributes
writer.Write(attributeText(evt.CustomAttributes, prefix + "\t")); writer.Write(evt.CustomAttributes.ToString(prefix + "\t"));
string modifiers = evt.AddMethod?.GetModifierString(); string modifiers = evt.AddMethod?.GetModifierString();
writer.Write($"{prefix}\t{modifiers}event {evt.EventHandlerType.CSharpName} {evt.Name} {{\n"); writer.Write($"{prefix}\t{modifiers}event {evt.EventHandlerType.CSharpName} {evt.Name} {{\n");
@@ -221,7 +216,7 @@ namespace Il2CppInspector
if (evt.AddMethod != null) m.Add("add", evt.AddMethod.VirtualAddress); if (evt.AddMethod != null) m.Add("add", evt.AddMethod.VirtualAddress);
if (evt.RemoveMethod != null) m.Add("remove", evt.RemoveMethod.VirtualAddress); if (evt.RemoveMethod != null) m.Add("remove", evt.RemoveMethod.VirtualAddress);
if (evt.RaiseMethod != null) m.Add("raise", evt.RaiseMethod.VirtualAddress); if (evt.RaiseMethod != null) m.Add("raise", evt.RaiseMethod.VirtualAddress);
writer.Write(string.Join("\n", m.Select(x => $"{prefix}\t\t{x.Key}; // {formatAddress(x.Value)}")) + "\n" + prefix + "\t}\n"); writer.Write(string.Join("\n", m.Select(x => $"{prefix}\t\t{x.Key}; // {Il2CppModel.FormatAddress(x.Value)}")) + "\n" + prefix + "\t}\n");
usedMethods.Add(evt.AddMethod); usedMethods.Add(evt.AddMethod);
usedMethods.Add(evt.RemoveMethod); usedMethods.Add(evt.RemoveMethod);
usedMethods.Add(evt.RaiseMethod); usedMethods.Add(evt.RaiseMethod);
@@ -244,11 +239,11 @@ namespace Il2CppInspector
foreach (var method in type.DeclaredConstructors) { foreach (var method in type.DeclaredConstructors) {
// Attributes // Attributes
writer.Write(attributeText(method.CustomAttributes, prefix + "\t")); writer.Write(method.CustomAttributes.ToString(prefix + "\t"));
writer.Write($"{prefix}\t{method.GetModifierString()}{method.DeclaringType.UnmangledBaseName}{method.GetTypeParametersString()}("); writer.Write($"{prefix}\t{method.GetModifierString()}{method.DeclaringType.UnmangledBaseName}{method.GetTypeParametersString()}(");
writer.Write(method.GetParametersString()); writer.Write(method.GetParametersString());
writer.Write(");" + (method.VirtualAddress != 0 ? $" // {formatAddress(method.VirtualAddress)}" : "") + "\n"); writer.Write(");" + (method.VirtualAddress != 0 ? $" // {Il2CppModel.FormatAddress(method.VirtualAddress)}" : "") + "\n");
} }
if (type.DeclaredConstructors.Any()) if (type.DeclaredConstructors.Any())
writer.Write("\n"); writer.Write("\n");
@@ -260,7 +255,7 @@ namespace Il2CppInspector
// Don't re-output methods for constructors, properties, events etc. // Don't re-output methods for constructors, properties, events etc.
foreach (var method in type.DeclaredMethods.Except(usedMethods)) { foreach (var method in type.DeclaredMethods.Except(usedMethods)) {
// Attributes // Attributes
writer.Write(attributeText(method.CustomAttributes, prefix + "\t")); writer.Write(method.CustomAttributes.ToString(prefix + "\t"));
writer.Write($"{prefix}\t{method.GetModifierString()}"); writer.Write($"{prefix}\t{method.GetModifierString()}");
if (method.Name != "op_Implicit" && method.Name != "op_Explicit") if (method.Name != "op_Implicit" && method.Name != "op_Explicit")
@@ -269,23 +264,9 @@ namespace Il2CppInspector
else else
writer.Write($"{method.CSharpName}{method.ReturnType.CSharpName}"); writer.Write($"{method.CSharpName}{method.ReturnType.CSharpName}");
writer.Write("(" + method.GetParametersString()); writer.Write("(" + method.GetParametersString());
writer.Write(");" + (method.VirtualAddress != 0 ? $" // {formatAddress(method.VirtualAddress)}" : "") + "\n"); writer.Write(");" + (method.VirtualAddress != 0 ? $" // {Il2CppModel.FormatAddress(method.VirtualAddress)}" : "") + "\n");
} }
writer.Write(prefix + "}\n"); writer.Write(prefix + "}\n");
} }
private string attributeText(IEnumerable<CustomAttributeData> attributes, string linePrefix = "", string attributePrefix = "") {
var sb = new StringBuilder();
foreach (var cad in attributes) {
var name = cad.AttributeType.CSharpName;
var suffix = name.LastIndexOf("Attribute", StringComparison.Ordinal);
if (suffix != -1)
name = name[..suffix];
sb.Append($"{linePrefix}[{attributePrefix}{name}] // {formatAddress((ulong)cad.VirtualAddress)}\n");
}
return sb.ToString();
}
} }
} }

View File

@@ -120,5 +120,9 @@ namespace Il2CppInspector.Reflection
var index = Array.FindIndex(Package.AttributeTypeRanges[imageRange], x => x.token == token); var index = Array.FindIndex(Package.AttributeTypeRanges[imageRange], x => x.token == token);
return index == -1 ? -1 : index + image.customAttributeStart; return index == -1 ? -1 : index + image.customAttributeStart;
} }
public static string FormatAddress(ulong address) => address <= 0xffff_ffff
? string.Format($"0x{(uint)address:X8}")
: string.Format($"0x{address:X16}");
} }
} }

View File

@@ -4,8 +4,10 @@
All rights reserved. All rights reserved.
*/ */
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
namespace Il2CppInspector.Reflection namespace Il2CppInspector.Reflection
{ {
@@ -53,4 +55,21 @@ namespace Il2CppInspector.Reflection
public static IList<CustomAttributeData> GetCustomAttributes(PropertyInfo prop) => getCustomAttributes(prop.Assembly, prop.Definition.token, prop.Definition.customAttributeIndex); public static IList<CustomAttributeData> GetCustomAttributes(PropertyInfo prop) => getCustomAttributes(prop.Assembly, prop.Definition.token, prop.Definition.customAttributeIndex);
public static IList<CustomAttributeData> GetCustomAttributes(TypeInfo type) => getCustomAttributes(type.Assembly, type.Definition.token, type.Definition.customAttributeIndex); public static IList<CustomAttributeData> GetCustomAttributes(TypeInfo type) => getCustomAttributes(type.Assembly, type.Definition.token, type.Definition.customAttributeIndex);
} }
public static class IEnumerableCustomAttributeDataExtensions
{
public static string ToString(this IEnumerable<CustomAttributeData> attributes, string linePrefix = "", string attributePrefix = "", bool inline = false) {
var sb = new StringBuilder();
foreach (var cad in attributes) {
var name = cad.AttributeType.CSharpName;
var suffix = name.LastIndexOf("Attribute", StringComparison.Ordinal);
if (suffix != -1)
name = name[..suffix];
sb.Append($"{linePrefix}[{attributePrefix}{name}] {(inline? "/*" : "//")} {Il2CppModel.FormatAddress((ulong)cad.VirtualAddress)}{(inline? " */ " : "\n")}");
}
return sb.ToString();
}
}
} }