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

@@ -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());