diff --git a/Il2CppInspector.CLI/Program.cs b/Il2CppInspector.CLI/Program.cs index 9c5aefa..10f29b7 100644 --- a/Il2CppInspector.CLI/Program.cs +++ b/Il2CppInspector.CLI/Program.cs @@ -7,6 +7,7 @@ using System.Diagnostics; using System.IO; using System.Linq; using CommandLine; +using Il2CppInspector.Cpp; using Il2CppInspector.Reflection; using Il2CppInspector.Outputs; using Il2CppInspector.Cpp.UnityHeaders; @@ -69,6 +70,9 @@ namespace Il2CppInspector.CLI [Option('j', "project", Required = false, HelpText = "Create a Visual Studio solution and projects. Implies --layout tree, --must-compile and --separate-attributes")] public bool CreateSolution { get; set; } + [Option("cpp-compiler", Required = false, HelpText = "Compiler to make C++ output compatible with (MSVC or GCC); selects based on binary executable type by default", Default = Cpp.CppCompiler.Type.BinaryFormat)] + public CppCompiler.Type CppCompiler { get; set; } + [Option("unity-path", Required = false, HelpText = "Path to Unity editor (when using --project). Wildcards select last matching folder in alphanumeric order", Default = @"C:\Program Files\Unity\Hub\Editor\*")] public string UnityPath { get; set; } @@ -248,7 +252,8 @@ namespace Il2CppInspector.CLI // C++ output using (new Benchmark("Generate C++ code")) { var cppWriter = new CppScaffolding(model) { - UnityVersion = options.UnityVersion + UnityVersion = options.UnityVersion, + Compiler = options.CppCompiler }; cppWriter.WriteCppToFile(options.CppOutFile); } diff --git a/Il2CppInspector.Common/Outputs/CppScaffolding.cs b/Il2CppInspector.Common/Outputs/CppScaffolding.cs index af4101b..a32d8aa 100644 --- a/Il2CppInspector.Common/Outputs/CppScaffolding.cs +++ b/Il2CppInspector.Common/Outputs/CppScaffolding.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; +using System.Text.RegularExpressions; using Il2CppInspector.Reflection; using Il2CppInspector.Cpp; using Il2CppInspector.Cpp.UnityHeaders; @@ -15,16 +16,24 @@ namespace Il2CppInspector.Outputs public class CppScaffolding { private readonly Il2CppModel model; + public CppCompiler.Type Compiler = CppCompiler.Type.BinaryFormat; private StreamWriter writer; public UnityVersion UnityVersion; private CppDeclarationGenerator declGenerator; + private readonly Regex rgxGCCalign = new Regex(@"__attribute__\s*?\(\s*?\(\s*?aligned\s*?\(\s*?([0-9]+)\s*?\)\s*?\)\s*?\)"); + private readonly Regex rgxMSVCalign = new Regex(@"__declspec\s*?\(\s*?align\s*?\(\s*?([0-9]+)\s*?\)\s*?\)"); + public CppScaffolding(Il2CppModel model) => this.model = model; public void WriteCppToFile(string outputFile) { declGenerator = new CppDeclarationGenerator(model, UnityVersion); UnityVersion = declGenerator.UnityVersion; + // Can be overridden in the object initializer + if (Compiler == CppCompiler.Type.BinaryFormat) + Compiler = CppCompiler.GuessFromImage(model.Package.BinaryImage); + using var fs = new FileStream(outputFile, FileMode.Create); writer = new StreamWriter(fs, Encoding.UTF8); @@ -86,8 +95,10 @@ namespace Il2CppInspector.Outputs } private void writeCode(string text) { - // TODO: Remove - text = text.Replace("__attribute__((aligned(8)))", "__declspec(align(8))"); + if (Compiler == CppCompiler.Type.MSVC) + text = rgxGCCalign.Replace(text, @"__declspec(align($1))"); + if (Compiler == CppCompiler.Type.GCC) + text = rgxMSVCalign.Replace(text, @"__attribute__((aligned($1)))"); var lines = text.Replace("\r", "").Split('\n'); var cleanLines = lines.Select(s => s.ToEscapedString());