Outputs: Add MSVC/GCC option to CppScaffolding and CLI

This commit is contained in:
Katy Coe
2020-07-02 15:17:49 +02:00
parent 0731b380fd
commit 24e4c65c4c
2 changed files with 19 additions and 3 deletions

View File

@@ -7,6 +7,7 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using CommandLine; using CommandLine;
using Il2CppInspector.Cpp;
using Il2CppInspector.Reflection; using Il2CppInspector.Reflection;
using Il2CppInspector.Outputs; using Il2CppInspector.Outputs;
using Il2CppInspector.Cpp.UnityHeaders; 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")] [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; } 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\*")] [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; } public string UnityPath { get; set; }
@@ -248,7 +252,8 @@ namespace Il2CppInspector.CLI
// C++ output // C++ output
using (new Benchmark("Generate C++ code")) { using (new Benchmark("Generate C++ code")) {
var cppWriter = new CppScaffolding(model) { var cppWriter = new CppScaffolding(model) {
UnityVersion = options.UnityVersion UnityVersion = options.UnityVersion,
Compiler = options.CppCompiler
}; };
cppWriter.WriteCppToFile(options.CppOutFile); cppWriter.WriteCppToFile(options.CppOutFile);
} }

View File

@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using Il2CppInspector.Reflection; using Il2CppInspector.Reflection;
using Il2CppInspector.Cpp; using Il2CppInspector.Cpp;
using Il2CppInspector.Cpp.UnityHeaders; using Il2CppInspector.Cpp.UnityHeaders;
@@ -15,16 +16,24 @@ namespace Il2CppInspector.Outputs
public class CppScaffolding public class CppScaffolding
{ {
private readonly Il2CppModel model; private readonly Il2CppModel model;
public CppCompiler.Type Compiler = CppCompiler.Type.BinaryFormat;
private StreamWriter writer; private StreamWriter writer;
public UnityVersion UnityVersion; public UnityVersion UnityVersion;
private CppDeclarationGenerator declGenerator; 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 CppScaffolding(Il2CppModel model) => this.model = model;
public void WriteCppToFile(string outputFile) { public void WriteCppToFile(string outputFile) {
declGenerator = new CppDeclarationGenerator(model, UnityVersion); declGenerator = new CppDeclarationGenerator(model, UnityVersion);
UnityVersion = declGenerator.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); using var fs = new FileStream(outputFile, FileMode.Create);
writer = new StreamWriter(fs, Encoding.UTF8); writer = new StreamWriter(fs, Encoding.UTF8);
@@ -86,8 +95,10 @@ namespace Il2CppInspector.Outputs
} }
private void writeCode(string text) { private void writeCode(string text) {
// TODO: Remove if (Compiler == CppCompiler.Type.MSVC)
text = text.Replace("__attribute__((aligned(8)))", "__declspec(align(8))"); 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 lines = text.Replace("\r", "").Split('\n');
var cleanLines = lines.Select(s => s.ToEscapedString()); var cleanLines = lines.Select(s => s.ToEscapedString());