From 33de89bed7771bc48b753f4909e5430f2e4ef0dc Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Tue, 10 Dec 2019 03:13:37 +0100 Subject: [PATCH] Output: Add AttributeTargets.All to AttributeUsage instances when using --must-compile (CS0592) --- Il2CppDumper/Program.cs | 2 +- Il2CppInspector/Reflection/Extensions.cs | 10 +++++++++- README.md | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Il2CppDumper/Program.cs b/Il2CppDumper/Program.cs index 0704ed6..1d72fce 100644 --- a/Il2CppDumper/Program.cs +++ b/Il2CppDumper/Program.cs @@ -49,7 +49,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.")] + [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.")] public bool MustCompile { get; set; } } diff --git a/Il2CppInspector/Reflection/Extensions.cs b/Il2CppInspector/Reflection/Extensions.cs index 17311e2..21471be 100644 --- a/Il2CppInspector/Reflection/Extensions.cs +++ b/Il2CppInspector/Reflection/Extensions.cs @@ -25,12 +25,20 @@ namespace Il2CppInspector.Reflection // IL2CPP doesn't retain attribute arguments so we have to comment out those with non-optional arguments if we want the output to compile var commentStart = mustCompile && !parameterlessConstructor? inline? "/* " : "// " : ""; var commentEnd = commentStart.Length > 0 && inline? " */" : ""; + var arguments = ""; + + // Set AttributeUsage(AttributeTargets.All) if making output that compiles to mitigate CS0592 + if (mustCompile && cad.AttributeType.FullName == "System.AttributeUsageAttribute") { + commentStart = ""; + commentEnd = ""; + arguments = "(AttributeTargets.All)"; + } var name = cad.AttributeType.GetScopedCSharpName(scope); var suffix = name.LastIndexOf("Attribute", StringComparison.Ordinal); if (suffix != -1) name = name[..suffix]; - sb.Append($"{linePrefix}{commentStart}[{attributePrefix}{name}]{commentEnd}"); + sb.Append($"{linePrefix}{commentStart}[{attributePrefix}{name}{arguments}]{commentEnd}"); if (emitPointer) sb.Append($" {(inline? "/*" : "//")} {cad.VirtualAddress.ToAddressString()}{(inline? " */" : "")}"); sb.Append(inline? " ":"\n"); diff --git a/README.md b/README.md index 26ed237..44b4793 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ File format and architecture are automatically detected. -s, --sort (Default: index) Sort order of type definitions in C# output ('index' = by type definition index, 'name' = by type name). No effect when using file-per-class layout -f, --flatten Flatten the namespace hierarchy into a single folder rather than using per-namespace subfolders. Only used when layout is per-namespace or per-class -n, --suppress-metadata 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 - -k, --must-compile 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. + -k, --must-compile 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. ``` Defaults if not specified: