From 0731b380fdff46e13515caabb1cc727d186cdfc4 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Thu, 2 Jul 2020 15:17:12 +0200 Subject: [PATCH] C++: Separate compiler option from CppDeclarationGenerator --- Il2CppInspector.Common/Cpp/CppCompiler.cs | 18 +++++++++++++ .../Cpp/CppDeclarationGenerator.cs | 27 +++++-------------- 2 files changed, 24 insertions(+), 21 deletions(-) create mode 100644 Il2CppInspector.Common/Cpp/CppCompiler.cs diff --git a/Il2CppInspector.Common/Cpp/CppCompiler.cs b/Il2CppInspector.Common/Cpp/CppCompiler.cs new file mode 100644 index 0000000..15331a8 --- /dev/null +++ b/Il2CppInspector.Common/Cpp/CppCompiler.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Il2CppInspector.Cpp +{ + public static class CppCompiler + { + public enum Type + { + BinaryFormat, // Inheritance structs use C syntax, and will automatically choose MSVC or GCC based on inferred compiler. + MSVC, // Inheritance structs are laid out assuming the MSVC compiler, which recursively includes base classes + GCC, // Inheritance structs are laid out assuming the GCC compiler, which packs members from all bases + current class together + } + + public static Type GuessFromImage(IFileFormatReader image) => (image is PEReader? Type.MSVC : Type.GCC); + } +} diff --git a/Il2CppInspector.Common/Cpp/CppDeclarationGenerator.cs b/Il2CppInspector.Common/Cpp/CppDeclarationGenerator.cs index b63b1fc..32fc653 100644 --- a/Il2CppInspector.Common/Cpp/CppDeclarationGenerator.cs +++ b/Il2CppInspector.Common/Cpp/CppDeclarationGenerator.cs @@ -28,13 +28,7 @@ namespace Il2CppInspector.Cpp // Different C++ compilers lay out C++ class structures differently, // meaning that the compiler must be known in order to generate class type structures // with the correct layout. - public enum InheritanceStyleEnum - { - C, // Inheritance structs use C syntax, and will automatically choose MSVC or GCC based on inferred compiler. - MSVC, // Inheritance structs are laid out assuming the MSVC compiler, which recursively includes base classes - GCC, // Inheritance structs are laid out assuming the GCC compiler, which packs members from all bases + current class together - } - public InheritanceStyleEnum InheritanceStyle; + public CppCompiler.Type InheritanceStyle; public CppDeclarationGenerator(Il2CppModel model, UnityVersion version) { this.model = model; @@ -53,17 +47,11 @@ namespace Il2CppInspector.Cpp InitializeNaming(); InitializeConcreteImplementations(); - } - private void GuessInheritanceStyle() { - if (InheritanceStyle == InheritanceStyleEnum.C) { - if (model.Package.BinaryImage is PEReader) - InheritanceStyle = InheritanceStyleEnum.MSVC; - else - InheritanceStyle = InheritanceStyleEnum.GCC; - } + // Configure inheritance style based on binary type; this can be overridden by setting InheritanceStyle in the object initializer + InheritanceStyle = CppCompiler.GuessFromImage(model.Package.BinaryImage); } - + // C type declaration used to name variables of the given C# type public string AsCType(TypeInfo ti) { // IsArray case handled by TypeNamer.GetName @@ -206,9 +194,6 @@ namespace Il2CppInspector.Cpp return; } - if (InheritanceStyle == InheritanceStyleEnum.C) - GuessInheritanceStyle(); - /* Generate a list of all base classes starting from the root */ List baseClasses = new List(); for (var bti = ti; bti != null; bti = bti.BaseType) @@ -217,7 +202,7 @@ namespace Il2CppInspector.Cpp var ns = CreateNamespace(); - if (InheritanceStyle == InheritanceStyleEnum.MSVC) { + if (InheritanceStyle == CppCompiler.Type.MSVC) { /* MSVC style: classes directly contain their base class as the first member. * This causes all classes to be aligned to the alignment of their base class. */ TypeInfo firstNonEmpty = null; @@ -255,7 +240,7 @@ namespace Il2CppInspector.Cpp csrc.Append($" struct {name}__Fields fields;\n"); csrc.Append($"}};\n"); } - } else if (InheritanceStyle == InheritanceStyleEnum.GCC) { + } else if (InheritanceStyle == CppCompiler.Type.GCC) { /* GCC style: after the base class, all fields in the hierarchy are concatenated. * This saves space (fields are "packed") but requires us to repeat fields from * base classes. */