Refactor some string output helper functions into extension methods

This commit is contained in:
Katy Coe
2019-11-05 19:34:12 +01:00
parent 9d4d8da42b
commit 067cea9e5f
5 changed files with 51 additions and 44 deletions

View File

@@ -104,7 +104,7 @@ namespace Il2CppInspector
writer.Write("out ");
writer.Write($"{param.ParameterType.CSharpName} {param.Name}");
}
writer.Write($"); // TypeDefIndex: {type.Index}; {Il2CppModel.FormatAddress(del.VirtualAddress)}\n");
writer.Write($"); // TypeDefIndex: {type.Index}; {del.VirtualAddress.ToAddressString()}\n");
return;
}
@@ -173,7 +173,7 @@ namespace Il2CppInspector
if (field.IsPinvokeImpl)
writer.Write("extern ");
if (field.GetCustomAttributes(FBAttribute).Any())
writer.Write($"fixed /* {Il2CppModel.FormatAddress((ulong) field.CustomAttributes.First(a => a.AttributeType.FullName == FBAttribute).VirtualAddress)} */" +
writer.Write($"fixed /* {((ulong) field.CustomAttributes.First(a => a.AttributeType.FullName == FBAttribute).VirtualAddress).ToAddressString()} */" +
$" {field.FieldType.DeclaredFields.First(f => f.Name == "FixedElementField").FieldType.CSharpName} {field.Name}[0]");
else
writer.Write($"{field.FieldType.CSharpName} {field.Name}");
@@ -211,8 +211,8 @@ namespace Il2CppInspector
+ (prop.SetMethod != null ? prop.SetMethod.CustomAttributes.Where(a => !SuppressGenerated || a.AttributeType.FullName != CGAttribute).ToString(inline: true) + "set; " : "") + "}");
if ((prop.GetMethod != null && prop.GetMethod.VirtualAddress != 0) || (prop.SetMethod != null && prop.SetMethod.VirtualAddress != 0))
writer.Write(" // ");
writer.Write((prop.GetMethod != null && prop.GetMethod.VirtualAddress != 0 ? Il2CppModel.FormatAddress(prop.GetMethod.VirtualAddress) + " " : "")
+ (prop.SetMethod != null && prop.SetMethod.VirtualAddress != 0 ? Il2CppModel.FormatAddress(prop.SetMethod.VirtualAddress) : "") + "\n");
writer.Write((prop.GetMethod != null && prop.GetMethod.VirtualAddress != 0 ? prop.GetMethod.VirtualAddress.ToAddressString() + " " : "")
+ (prop.SetMethod != null && prop.SetMethod.VirtualAddress != 0 ? prop.SetMethod.VirtualAddress.ToAddressString() : "") + "\n");
usedMethods.Add(prop.GetMethod);
usedMethods.Add(prop.SetMethod);
}
@@ -233,7 +233,7 @@ namespace Il2CppInspector
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 => $"{prefix}\t\t{x.Key}; // {Il2CppModel.FormatAddress(x.Value)}")) + "\n" + prefix + "\t}\n");
writer.Write(string.Join("\n", m.Select(x => $"{prefix}\t\t{x.Key}; // {x.Value.ToAddressString()}")) + "\n" + prefix + "\t}\n");
usedMethods.Add(evt.AddMethod);
usedMethods.Add(evt.RemoveMethod);
usedMethods.Add(evt.RaiseMethod);
@@ -260,7 +260,7 @@ namespace Il2CppInspector
writer.Write($"{prefix}\t{method.GetModifierString()}{method.DeclaringType.UnmangledBaseName}{method.GetTypeParametersString()}(");
writer.Write(method.GetParametersString());
writer.Write(");" + (method.VirtualAddress != 0 ? $" // {Il2CppModel.FormatAddress(method.VirtualAddress)}" : "") + "\n");
writer.Write(");" + (method.VirtualAddress != 0 ? $" // {method.VirtualAddress.ToAddressString()}" : "") + "\n");
}
if (type.DeclaredConstructors.Any())
writer.Write("\n");
@@ -284,7 +284,7 @@ namespace Il2CppInspector
else
writer.Write($"{method.CSharpName}{method.ReturnType.CSharpName}");
writer.Write("(" + method.GetParametersString());
writer.Write(");" + (method.VirtualAddress != 0 ? $" // {Il2CppModel.FormatAddress(method.VirtualAddress)}" : "") + "\n");
writer.Write(");" + (method.VirtualAddress != 0 ? $" // {method.VirtualAddress.ToAddressString()}" : "") + "\n");
}
writer.Write(prefix + "}\n");
}

View File

@@ -120,9 +120,5 @@ namespace Il2CppInspector.Reflection
var index = Array.FindIndex(Package.AttributeTypeRanges[imageRange], x => x.token == token);
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

@@ -55,21 +55,4 @@ namespace Il2CppInspector.Reflection
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 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();
}
}
}

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Il2CppInspector.Reflection
{
public static class Extensions
{
// Convert a list of CustomAttributeData objects into C#-friendly attribute usages
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? "/*" : "//")} {((ulong)cad.VirtualAddress).ToAddressString()}{(inline? " */ " : "\n")}");
}
return sb.ToString();
}
// Output a ulong as a 32 or 64-bit hexadecimal address
public static string ToAddressString(this ulong address) => address <= 0xffff_ffff
? string.Format($"0x{(uint)address:X8}")
: string.Format($"0x{address:X16}");
// Output a value in C#-friendly syntax
public static string ToCSharpValue(this object value) {
if (value is bool)
return (bool) value ? "true" : "false";
if (value is string)
return $"\"{value}\"";
if (!(value is char))
return (value?.ToString() ?? "null");
var cValue = (int) (char) value;
if (cValue < 32 || cValue > 126)
return $"'\\x{cValue:x4}'";
return $"'{value}'";
}
}
}

View File

@@ -21,22 +21,7 @@ namespace Il2CppInspector.Reflection {
public bool HasDefaultValue { get; }
public object DefaultValue { get; }
public string DefaultValueString {
get {
if (!HasDefaultValue)
return "";
if (DefaultValue is bool)
return (bool) DefaultValue ? "true" : "false";
if (DefaultValue is string)
return $"\"{DefaultValue}\"";
if (!(DefaultValue is char))
return (DefaultValue?.ToString() ?? "null");
var cValue = (int) (char) DefaultValue;
if (cValue < 32 || cValue > 126)
return $"'\\x{cValue:x4}'";
return $"'{DefaultValue}'";
}
}
public string DefaultValueString => HasDefaultValue ? DefaultValue.ToCSharpValue() : "";
// Information/flags about the field
public FieldAttributes Attributes { get; }