Output: Only create method and constructor bodies if MustCompile is enabled

This commit is contained in:
Katy Coe
2020-02-05 11:18:02 +01:00
parent 3b7b0d8b9d
commit ed2c0f8ae1

View File

@@ -476,29 +476,32 @@ namespace Il2CppInspector
sb.Append($"{prefix}\t{method.GetModifierString()}{method.DeclaringType.UnmangledBaseName}{method.GetTypeParametersString(scope)}"); sb.Append($"{prefix}\t{method.GetModifierString()}{method.DeclaringType.UnmangledBaseName}{method.GetTypeParametersString(scope)}");
sb.Append($"({method.GetParametersString(scope, !SuppressMetadata)})"); sb.Append($"({method.GetParametersString(scope, !SuppressMetadata)})");
// Class constructor if (MustCompile) {
if (method.IsAbstract) // Class constructor
sb.Append(";"); if (method.IsAbstract)
else if (!type.IsValueType) sb.Append(";");
sb.Append(" {}"); else if (!type.IsValueType)
// Struct constructor
else {
// Parameterized struct constructors must call the parameterless constructor to create the object
// if the object has any auto-implemented properties
if (type.DeclaredProperties.Any() && method.DeclaredParameters.Any())
sb.Append(" : this()");
// Struct construvctors must initialize all fields in the struct
if (fields.Any()) {
var paramNames = method.DeclaredParameters.Select(p => p.Name);
sb.Append(" {\n" + string.Join("\n", fields
.Where(f => !f.IsStatic && !f.IsLiteral)
.Select(f => $"{prefix}\t\t{(paramNames.Contains(f.Name) ? "this." : "")}{f.Name} = default;"))
+ $"\n{prefix}\t}}");
} else
sb.Append(" {}"); sb.Append(" {}");
}
// Struct constructor
else {
// Parameterized struct constructors must call the parameterless constructor to create the object
// if the object has any auto-implemented properties
if (type.DeclaredProperties.Any() && method.DeclaredParameters.Any())
sb.Append(" : this()");
// Struct construvctors must initialize all fields in the struct
if (fields.Any()) {
var paramNames = method.DeclaredParameters.Select(p => p.Name);
sb.Append(" {\n" + string.Join("\n", fields
.Where(f => !f.IsStatic && !f.IsLiteral)
.Select(f => $"{prefix}\t\t{(paramNames.Contains(f.Name) ? "this." : "")}{f.Name} = default;"))
+ $"\n{prefix}\t}}");
} else
sb.Append(" {}");
}
} else
sb.Append(";");
sb.Append((!SuppressMetadata && method.VirtualAddress != null ? $" // {method.VirtualAddress.ToAddressString()}" : "") + "\n"); sb.Append((!SuppressMetadata && method.VirtualAddress != null ? $" // {method.VirtualAddress.ToAddressString()}" : "") + "\n");
} }
@@ -616,33 +619,36 @@ namespace Il2CppInspector
} }
// Body // Body
var methodBody = method switch { var methodBody = MustCompile? method switch {
// Abstract method // Abstract method
{ IsAbstract: true } => ";", { IsAbstract: true } => ";",
// Extern method // Extern method
{ Attributes: var a } when (a & MethodAttributes.PinvokeImpl) == MethodAttributes.PinvokeImpl => ";", { Attributes: var a } when (a & MethodAttributes.PinvokeImpl) == MethodAttributes.PinvokeImpl => ";",
// Method with out parameters // Method with out parameters
{ DeclaredParameters: var d } when d.Any(p => p.IsOut) => { DeclaredParameters: var d } when d.Any(p => p.IsOut) =>
" {\n" + string.Join("\n", d.Where(p => p.IsOut).Select(p => $"{prefix}\t\t{p.Name} = default;")) " {\n" + string.Join("\n", d.Where(p => p.IsOut).Select(p => $"{prefix}\t\t{p.Name} = default;"))
+ (method.ReturnType.FullName != "System.Void"? $"\n{prefix}\t\treturn default;" : "") + (method.ReturnType.FullName != "System.Void"? $"\n{prefix}\t\treturn default;" : "")
+ $"\n{prefix}\t}}", + $"\n{prefix}\t}}",
// No return type // No return type
{ ReturnType: var retType } when retType.FullName == "System.Void" => " {}", { ReturnType: var retType } when retType.FullName == "System.Void" => " {}",
// Ref return type // Ref return type
{ ReturnType: var retType } when retType.IsByRef => " => ref _refReturnTypeFor" + method.CSharpName + ";", { ReturnType: var retType } when retType.IsByRef => " => ref _refReturnTypeFor" + method.CSharpName + ";",
// Regular return type // Regular return type
_ => " => default;" _ => " => default;"
}; }
// Only make a method body if we are trying to compile the output
: ";";
writer.Append(methodBody + (!SuppressMetadata && method.VirtualAddress != null ? $" // {method.VirtualAddress.ToAddressString()}" : "") + "\n"); writer.Append(methodBody + (!SuppressMetadata && method.VirtualAddress != null ? $" // {method.VirtualAddress.ToAddressString()}" : "") + "\n");
// Ref return type requires us to invent a field // Ref return type requires us to invent a field
if (method.ReturnType.IsByRef) if (MustCompile && method.ReturnType.IsByRef)
writer.Append($"{prefix}\tprivate {method.ReturnType.GetScopedCSharpName(scope)} _refReturnTypeFor{method.CSharpName};\n"); writer.Append($"{prefix}\tprivate {method.ReturnType.GetScopedCSharpName(scope)} _refReturnTypeFor{method.CSharpName};\n");
return writer.ToString(); return writer.ToString();