From 7d88fd8fc48e790e54556379ee3c1faadd29b598 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Wed, 5 Feb 2020 11:55:39 +0100 Subject: [PATCH] Output: Generate dummy constructors when using MustCompile (CS1729, CS7036) --- Il2CppDumper/Il2CppCSharpDumper.cs | 8 +++++++- Il2CppDumper/Program.cs | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Il2CppDumper/Il2CppCSharpDumper.cs b/Il2CppDumper/Il2CppCSharpDumper.cs index f107c69..ec27f83 100644 --- a/Il2CppDumper/Il2CppCSharpDumper.cs +++ b/Il2CppDumper/Il2CppCSharpDumper.cs @@ -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(); } diff --git a/Il2CppDumper/Program.cs b/Il2CppDumper/Program.cs index 02c22d5..370d2a3 100644 --- a/Il2CppDumper/Program.cs +++ b/Il2CppDumper/Program.cs @@ -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")]