Output: Organize nested types correctly
This commit is contained in:
@@ -19,15 +19,18 @@ namespace Il2CppInspector
|
|||||||
|
|
||||||
public Il2CppCSharpDumper(Il2CppModel model) => this.model = model;
|
public Il2CppCSharpDumper(Il2CppModel model) => this.model = model;
|
||||||
|
|
||||||
|
private StreamWriter writer;
|
||||||
|
|
||||||
private string formatAddress(ulong address) => model.Package.BinaryImage.Bits == 32
|
private string formatAddress(ulong address) => model.Package.BinaryImage.Bits == 32
|
||||||
? string.Format($"0x{(uint) address:X8}")
|
? string.Format($"0x{(uint) address:X8}")
|
||||||
: string.Format($"0x{address:X16}");
|
: string.Format($"0x{address:X16}");
|
||||||
|
|
||||||
public void WriteFile(string outFile) {
|
public void WriteFile(string outFile) {
|
||||||
using (var 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");
|
||||||
}
|
}
|
||||||
|
writer.Write("\n");
|
||||||
|
|
||||||
foreach (var type in model.Assemblies.SelectMany(x => x.DefinedTypes)) {
|
foreach (var type in model.Assemblies.SelectMany(x => x.DefinedTypes)) {
|
||||||
|
|
||||||
@@ -35,13 +38,28 @@ namespace Il2CppInspector
|
|||||||
if (ExcludedNamespaces?.Any(x => x == type.Namespace || type.Namespace.StartsWith(x + ".")) ?? false)
|
if (ExcludedNamespaces?.Any(x => x == type.Namespace || type.Namespace.StartsWith(x + ".")) ?? false)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Type declaration
|
// Assembly.DefinedTypes returns nested types in the assembly by design - ignore them
|
||||||
writer.Write($"\n// Namespace: {type.Namespace}\n");
|
if (!type.IsNested) {
|
||||||
|
writeType(type);
|
||||||
|
writer.Write("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeType(TypeInfo type, string prefix = "") {
|
||||||
|
|
||||||
|
// Only print namespace if we're not nested
|
||||||
|
if (!type.IsNested)
|
||||||
|
writer.Write($"{prefix}// Namespace: {type.Namespace}\n");
|
||||||
|
|
||||||
|
// Type declaration
|
||||||
if (type.IsImport)
|
if (type.IsImport)
|
||||||
writer.Write("[ComImport]");
|
writer.Write(prefix + "[ComImport]\n");
|
||||||
if (type.IsSerializable)
|
if (type.IsSerializable)
|
||||||
writer.Write("[Serializable]\n");
|
writer.Write(prefix + "[Serializable]\n");
|
||||||
|
|
||||||
|
writer.Write(prefix);
|
||||||
if (type.IsPublic || type.IsNestedPublic)
|
if (type.IsPublic || type.IsNestedPublic)
|
||||||
writer.Write("public ");
|
writer.Write("public ");
|
||||||
if (type.IsNestedPrivate)
|
if (type.IsNestedPrivate)
|
||||||
@@ -72,7 +90,7 @@ namespace Il2CppInspector
|
|||||||
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}; {formatAddress(del.VirtualAddress)}\n");
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// An abstract sealed class is a static class
|
// An abstract sealed class is a static class
|
||||||
@@ -100,17 +118,17 @@ namespace Il2CppInspector
|
|||||||
@base.Insert(0, type.ElementType.CSharpName);
|
@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" + prefix + "{\n");
|
||||||
|
|
||||||
// Fields
|
// Fields
|
||||||
if (!type.IsEnum) {
|
if (!type.IsEnum) {
|
||||||
if (type.DeclaredFields.Count > 0)
|
if (type.DeclaredFields.Any())
|
||||||
writer.Write("\t// Fields\n");
|
writer.Write(prefix + "\t// Fields\n");
|
||||||
|
|
||||||
foreach (var field in type.DeclaredFields) {
|
foreach (var field in type.DeclaredFields) {
|
||||||
writer.Write("\t");
|
writer.Write(prefix + "\t");
|
||||||
if (field.IsNotSerialized)
|
if (field.IsNotSerialized)
|
||||||
writer.Write("[NonSerialized]\t");
|
writer.Write("[NonSerialized]\n" + prefix + "\t");
|
||||||
|
|
||||||
if (field.IsPrivate)
|
if (field.IsPrivate)
|
||||||
writer.Write("private ");
|
writer.Write("private ");
|
||||||
@@ -140,27 +158,27 @@ namespace Il2CppInspector
|
|||||||
// Don't output field indices for const fields (they don't have any storage)
|
// Don't output field indices for const fields (they don't have any storage)
|
||||||
if (!field.IsLiteral)
|
if (!field.IsLiteral)
|
||||||
writer.Write(" // 0x{0:X2}", (uint)field.Offset);
|
writer.Write(" // 0x{0:X2}", (uint)field.Offset);
|
||||||
writer.WriteLine("");
|
writer.Write("\n");
|
||||||
}
|
}
|
||||||
if (type.DeclaredFields.Count > 0)
|
if (type.DeclaredFields.Any())
|
||||||
writer.Write("\n");
|
writer.Write("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enumeration
|
// Enumeration
|
||||||
else {
|
else {
|
||||||
writer.Write(string.Join(",\n", type.GetEnumNames().Zip(type.GetEnumValues().OfType<object>(),
|
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");
|
(k, v) => new { k, v }).OrderBy(x => x.v).Select(x => $"{prefix}\t{x.k} = {x.v}")) + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
var usedMethods = new List<Reflection.MethodInfo>();
|
var usedMethods = new List<MethodInfo>();
|
||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
if (type.DeclaredProperties.Count > 0)
|
if (type.DeclaredProperties.Any())
|
||||||
writer.Write("\t// Properties\n");
|
writer.Write(prefix + "\t// Properties\n");
|
||||||
|
|
||||||
foreach (var prop in type.DeclaredProperties) {
|
foreach (var prop in type.DeclaredProperties) {
|
||||||
string modifiers = prop.GetMethod?.GetModifierString() ?? prop.SetMethod.GetModifierString();
|
string modifiers = prop.GetMethod?.GetModifierString() ?? prop.SetMethod.GetModifierString();
|
||||||
writer.Write($"\t{modifiers}{prop.PropertyType.CSharpName} {prop.Name} {{ ");
|
writer.Write($"{prefix}\t{modifiers}{prop.PropertyType.CSharpName} {prop.Name} {{ ");
|
||||||
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(" // ");
|
||||||
@@ -169,34 +187,43 @@ namespace Il2CppInspector
|
|||||||
usedMethods.Add(prop.GetMethod);
|
usedMethods.Add(prop.GetMethod);
|
||||||
usedMethods.Add(prop.SetMethod);
|
usedMethods.Add(prop.SetMethod);
|
||||||
}
|
}
|
||||||
if (type.DeclaredProperties.Count > 0)
|
if (type.DeclaredProperties.Any())
|
||||||
writer.Write("\n");
|
writer.Write("\n");
|
||||||
|
|
||||||
// Events
|
// Events
|
||||||
if (type.DeclaredEvents.Count > 0)
|
if (type.DeclaredEvents.Any())
|
||||||
writer.Write("\t// Events\n");
|
writer.Write(prefix + "\t// Events\n");
|
||||||
|
|
||||||
foreach (var evt in type.DeclaredEvents) {
|
foreach (var evt in type.DeclaredEvents) {
|
||||||
string modifiers = evt.AddMethod?.GetModifierString();
|
string modifiers = evt.AddMethod?.GetModifierString();
|
||||||
writer.Write($"\t{modifiers}event {evt.EventHandlerType.CSharpName} {evt.Name} {{\n");
|
writer.Write($"{prefix}\t{modifiers}event {evt.EventHandlerType.CSharpName} {evt.Name} {{\n");
|
||||||
var m = new Dictionary<string, ulong>();
|
var m = new Dictionary<string, ulong>();
|
||||||
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 => $"\t\t{x.Key}; // {formatAddress(x.Value)}")) + "\n\t}\n");
|
writer.Write(string.Join("\n", m.Select(x => $"{prefix}\t\t{x.Key}; // {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);
|
||||||
}
|
}
|
||||||
if (type.DeclaredEvents.Count > 0)
|
if (type.DeclaredEvents.Any())
|
||||||
writer.Write("\n");
|
writer.Write("\n");
|
||||||
|
|
||||||
|
// Nested types
|
||||||
|
if (type.DeclaredNestedTypes.Any())
|
||||||
|
writer.Write(prefix + "\t// Nested types\n");
|
||||||
|
|
||||||
|
foreach (var nestedType in type.DeclaredNestedTypes) {
|
||||||
|
writeType(nestedType, prefix + "\t");
|
||||||
|
writer.Write("\n");
|
||||||
|
}
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
if (type.DeclaredConstructors.Any())
|
if (type.DeclaredConstructors.Any())
|
||||||
writer.Write("\t// Constructors\n");
|
writer.Write(prefix + "\t// Constructors\n");
|
||||||
|
|
||||||
foreach (var method in type.DeclaredConstructors) {
|
foreach (var method in type.DeclaredConstructors) {
|
||||||
writer.Write($"\t{method.GetModifierString()}{method.DeclaringType.Name}(");
|
writer.Write($"{prefix}\t{method.GetModifierString()}{method.DeclaringType.Name}(");
|
||||||
writer.Write(getParametersString(method.DeclaredParameters));
|
writer.Write(getParametersString(method.DeclaredParameters));
|
||||||
writer.Write(");" + (method.VirtualAddress != 0 ? $" // {formatAddress(method.VirtualAddress)}" : "") + "\n");
|
writer.Write(");" + (method.VirtualAddress != 0 ? $" // {formatAddress(method.VirtualAddress)}" : "") + "\n");
|
||||||
}
|
}
|
||||||
@@ -205,11 +232,11 @@ namespace Il2CppInspector
|
|||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
if (type.DeclaredMethods.Except(usedMethods).Any())
|
if (type.DeclaredMethods.Except(usedMethods).Any())
|
||||||
writer.Write("\t// Methods\n");
|
writer.Write(prefix + "\t// Methods\n");
|
||||||
|
|
||||||
// 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)) {
|
||||||
writer.Write($"\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")
|
||||||
writer.Write($"{method.ReturnType.CSharpName} {method.CSharpName}");
|
writer.Write($"{method.ReturnType.CSharpName} {method.CSharpName}");
|
||||||
else
|
else
|
||||||
@@ -217,11 +244,10 @@ namespace Il2CppInspector
|
|||||||
writer.Write("(" + getParametersString(method.DeclaredParameters));
|
writer.Write("(" + getParametersString(method.DeclaredParameters));
|
||||||
writer.Write(");" + (method.VirtualAddress != 0 ? $" // {formatAddress(method.VirtualAddress)}" : "") + "\n");
|
writer.Write(");" + (method.VirtualAddress != 0 ? $" // {formatAddress(method.VirtualAddress)}" : "") + "\n");
|
||||||
}
|
}
|
||||||
writer.Write("}\n");
|
writer.Write(prefix + "}\n");
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Move this info ParameterInfo
|
||||||
private string getParametersString(List<ParameterInfo> @params) {
|
private string getParametersString(List<ParameterInfo> @params) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user