Output: Generate dummy constructors when using MustCompile (CS1729, CS7036)

This commit is contained in:
Katy Coe
2020-02-05 11:55:39 +01:00
parent ed2c0f8ae1
commit 7d88fd8fc4
2 changed files with 8 additions and 2 deletions

View File

@@ -468,6 +468,12 @@ namespace Il2CppInspector
var fields = type.DeclaredFields.Where(f => !f.GetCustomAttributes(CGAttribute).Any());
sb.Clear();
// Crete a parameterless constructor for every relevant type when making code that compiles to mitigate CS1729 and CS7036
if (MustCompile && !type.IsInterface && !(type.IsAbstract && type.IsSealed) && !type.IsValueType
&& type.DeclaredConstructors.All(c => c.IsStatic || c.DeclaredParameters.Any()))
sb.Append($"{prefix}\t{(type.IsAbstract? "protected" : "public")} {type.UnmangledBaseName}() {{}} // Dummy constructor\n");
foreach (var method in type.DeclaredConstructors) {
// Attributes
sb.Append(method.CustomAttributes.OrderBy(a => a.AttributeType.Name)
@@ -649,7 +655,7 @@ namespace Il2CppInspector
// Ref return type requires us to invent a field
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}; // Dummy field\n");
return writer.ToString();
}

View File

@@ -54,7 +54,7 @@ namespace Il2CppInspector
[Option('n', "suppress-metadata", Required = false, HelpText = "Diff tidying: suppress method pointers, field offsets and type indices from C# output. Useful for comparing two versions of a binary for changes with a diff tool")]
public bool SuppressMetadata { get; set; }
[Option('k', "must-compile", Required = false, HelpText = "Compilation tidying: try really hard to make code that compiles. Suppress generation of code for items with CompilerGenerated attribute. Comment out attributes without parameterless constructors or all-optional constructor arguments. Don't emit add/remove/raise on events. Specify AttributeTargets.All on classes with AttributeUsage attribute. Force auto-properties to have get accessors. Force regular properties to have bodies. Suppress global::Locale classes.")]
[Option('k', "must-compile", Required = false, HelpText = "Compilation tidying: try really hard to make code that compiles. Suppress generation of code for items with CompilerGenerated attribute. Comment out attributes without parameterless constructors or all-optional constructor arguments. Don't emit add/remove/raise on events. Specify AttributeTargets.All on classes with AttributeUsage attribute. Force auto-properties to have get accessors. Force regular properties to have bodies. Suppress global::Locale classes. Generate dummy parameterless base constructors and ref return fields.")]
public bool MustCompile { get; set; }
[Option("separate-attributes", Required = false, HelpText = "Place assembly-level attributes in their own AssemblyInfo.cs files. Only used when layout is per-assembly or tree")]