Add better il2cpp_array_size_t definition for script outputs in versions post 2017.2.1, fix some other cpp gen issues
This commit is contained in:
@@ -575,7 +575,7 @@ namespace Il2CppInspector.Cpp
|
|||||||
}
|
}
|
||||||
/* Reserve commonly defined C++ symbols for MSVC DLL projects */
|
/* Reserve commonly defined C++ symbols for MSVC DLL projects */
|
||||||
/* This is not an exhaustive list! (windows.h etc.) */
|
/* This is not an exhaustive list! (windows.h etc.) */
|
||||||
foreach (var symbol in new[] {"_int32", "DEFAULT_CHARSET", "FILETIME", "NULL", "SYSTEMTIME", "stderr", "stdin", "stdout"}) {
|
foreach (var symbol in new[] {"_int8", "_int16", "_int32", "_int64", "DEFAULT_CHARSET", "FILETIME", "NULL", "SYSTEMTIME", "stderr", "stdin", "stdout"}) {
|
||||||
ns.ReserveName(symbol);
|
ns.ReserveName(symbol);
|
||||||
}
|
}
|
||||||
/* Reserve builtin keywords in IDA */
|
/* Reserve builtin keywords in IDA */
|
||||||
@@ -586,7 +586,7 @@ namespace Il2CppInspector.Cpp
|
|||||||
"__ptr32", "__ptr64", "__pure", "__restrict", "__return_ptr", "__shifted", "__spoils", "__stdcall", "__struct_ptr",
|
"__ptr32", "__ptr64", "__pure", "__restrict", "__return_ptr", "__shifted", "__spoils", "__stdcall", "__struct_ptr",
|
||||||
"__thiscall", "__thread", "__unaligned", "__usercall", "__userpurge",
|
"__thiscall", "__thread", "__unaligned", "__usercall", "__userpurge",
|
||||||
"_cs", "_ds", "_es", "_ss", "far", "flat", "near",
|
"_cs", "_ds", "_es", "_ss", "far", "flat", "near",
|
||||||
"Mask", "Region", "Pointer", "GC" }) {
|
"Mask", "Region", "Pointer", "GC", "Time" /* wtf? */ }) {
|
||||||
ns.ReserveName(keyword);
|
ns.ReserveName(keyword);
|
||||||
}
|
}
|
||||||
/* Reserve builtin keywords for Ghidra */
|
/* Reserve builtin keywords for Ghidra */
|
||||||
|
|||||||
@@ -383,7 +383,7 @@ namespace Il2CppInspector.Cpp
|
|||||||
|
|
||||||
sb.Append(Name + (Name.Length > 0 ? " " : ""));
|
sb.Append(Name + (Name.Length > 0 ? " " : ""));
|
||||||
|
|
||||||
sb.Append("{");
|
sb.Append('{');
|
||||||
foreach (var field in Fields.Values.SelectMany(f => f)) {
|
foreach (var field in Fields.Values.SelectMany(f => f)) {
|
||||||
var fieldString = field.ToString(format);
|
var fieldString = field.ToString(format);
|
||||||
var suffix = ";";
|
var suffix = ";";
|
||||||
@@ -399,18 +399,19 @@ namespace Il2CppInspector.Cpp
|
|||||||
suffix = "";
|
suffix = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.Append("\n ");
|
var parts = fieldString.Split('\n');
|
||||||
foreach (var fieldStr in fieldString.Split('\n'))
|
foreach (var part in parts)
|
||||||
{
|
{
|
||||||
sb.Append(fieldStr);
|
|
||||||
sb.Append("\n ");
|
sb.Append("\n ");
|
||||||
|
sb.Append(part);
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.Append(suffix);
|
sb.Append(suffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.Append($"\n}}{(format == "o"? $" /* Size: 0x{SizeBytes:x2} */" : "")};");
|
sb.Append($"\n}}{(format == "o"? $" /* Size: 0x{SizeBytes:x2} */" : "")};");
|
||||||
|
|
||||||
sb.Append("\n");
|
sb.Append('\n');
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1846,7 +1846,7 @@ typedef struct Il2CppAppContext
|
|||||||
int32_t context_id;
|
int32_t context_id;
|
||||||
void* static_data;
|
void* static_data;
|
||||||
} Il2CppAppContext;
|
} Il2CppAppContext;
|
||||||
struct Il2CppDecimal
|
typedef struct Il2CppDecimal
|
||||||
{
|
{
|
||||||
uint16_t reserved;
|
uint16_t reserved;
|
||||||
union
|
union
|
||||||
@@ -1868,7 +1868,7 @@ struct Il2CppDecimal
|
|||||||
} v;
|
} v;
|
||||||
uint64_t Lo64;
|
uint64_t Lo64;
|
||||||
} v;
|
} v;
|
||||||
} Il2CppClass;
|
} Il2CppDecimal;
|
||||||
typedef struct Il2CppDouble
|
typedef struct Il2CppDouble
|
||||||
{
|
{
|
||||||
uint32_t mantLo : 32;
|
uint32_t mantLo : 32;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2017-2021 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
|
Copyright 2017-2021 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
|
||||||
Copyright 2020 Robert Xiao - https://robertxiao.ca
|
Copyright 2020 Robert Xiao - https://robertxiao.ca
|
||||||
|
Copyright 2023 LukeFZ - https://github.com/LukeFZ
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -10,12 +10,11 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using NoisyCowStudios.Bin2Object;
|
|
||||||
|
|
||||||
namespace Il2CppInspector.Cpp.UnityHeaders
|
namespace Il2CppInspector.Cpp.UnityHeaders
|
||||||
{
|
{
|
||||||
// Parsed representation of a Unity version number, such as 5.3.0f1 or 2019.3.7.
|
// Parsed representation of a Unity version number, such as 5.3.0f1 or 2019.3.7.
|
||||||
public class UnityVersion : IComparable<UnityVersion>, IEquatable<UnityVersion>
|
public partial class UnityVersion : IComparable<UnityVersion>, IEquatable<UnityVersion>
|
||||||
{
|
{
|
||||||
// A sorted enumeration of build types, in order of maturity
|
// A sorted enumeration of build types, in order of maturity
|
||||||
public enum BuildTypeEnum
|
public enum BuildTypeEnum
|
||||||
@@ -58,7 +57,7 @@ namespace Il2CppInspector.Cpp.UnityHeaders
|
|||||||
public int BuildNumber { get; }
|
public int BuildNumber { get; }
|
||||||
|
|
||||||
public UnityVersion(string versionString) {
|
public UnityVersion(string versionString) {
|
||||||
var match = Regex.Match(versionString, @"^(\d+)\.(\d+)(?:\.(\d+))?(?:([a-zA-Z]+)(\d+))?$");
|
var match = VersionRegex().Match(versionString);
|
||||||
if (!match.Success)
|
if (!match.Success)
|
||||||
throw new ArgumentException($"'${versionString}' is not a valid Unity version number.");
|
throw new ArgumentException($"'${versionString}' is not a valid Unity version number.");
|
||||||
Major = int.Parse(match.Groups[1].Value);
|
Major = int.Parse(match.Groups[1].Value);
|
||||||
@@ -88,7 +87,7 @@ namespace Il2CppInspector.Cpp.UnityHeaders
|
|||||||
return new UnityVersion(unityString);
|
return new UnityVersion(unityString);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static implicit operator UnityVersion(string versionString) => new UnityVersion(versionString);
|
public static implicit operator UnityVersion(string versionString) => new(versionString);
|
||||||
|
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
var res = $"{Major}.{Minor}.{Update}";
|
var res = $"{Major}.{Minor}.{Update}";
|
||||||
@@ -143,6 +142,9 @@ namespace Il2CppInspector.Cpp.UnityHeaders
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override int GetHashCode() => HashCode.Combine(Major, Minor, Update, BuildType, BuildNumber);
|
public override int GetHashCode() => HashCode.Combine(Major, Minor, Update, BuildType, BuildNumber);
|
||||||
|
|
||||||
|
[GeneratedRegex(@"^(\d+)\.(\d+)(?:\.(\d+))?(?:([a-zA-Z]+)(\d+))?$")]
|
||||||
|
private static partial Regex VersionRegex();
|
||||||
}
|
}
|
||||||
|
|
||||||
// A range of Unity versions
|
// A range of Unity versions
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
// Copyright 2020 Robert Xiao - https://robertxiao.ca/
|
// Copyright 2020 Robert Xiao - https://robertxiao.ca/
|
||||||
// Copyright (c) 2020-2021 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
|
// Copyright (c) 2020-2021 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
|
||||||
|
// Copyright (c) 2023 LukeFZ https://github.com/LukeFZ
|
||||||
// All rights reserved
|
// All rights reserved
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
@@ -15,26 +16,35 @@ using Il2CppInspector.Properties;
|
|||||||
|
|
||||||
namespace Il2CppInspector.Outputs
|
namespace Il2CppInspector.Outputs
|
||||||
{
|
{
|
||||||
public class CppScaffolding
|
public partial class CppScaffolding(AppModel model, bool useBetterArraySize = false)
|
||||||
{
|
{
|
||||||
private readonly AppModel model;
|
private readonly AppModel _model = model;
|
||||||
private StreamWriter writer;
|
|
||||||
|
|
||||||
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*?\)");
|
* 2017.2.1 changed the type of il2cpp_array_size_t to uintptr_t from int32_t. The code, however, uses static_cast<int32_t>(maxLength) to access this value,
|
||||||
|
* which makes decompilation a bit unpleasant due to it only ever checking the lower 32 bits.
|
||||||
|
* The better array size type is a union of the actual size (int32_t) and the actual value (uintptr_t) which should hopefully improve decompilation.
|
||||||
|
*/
|
||||||
|
private readonly bool _useBetterArraySize =
|
||||||
|
model.UnityVersion.CompareTo("2017.2.1") >= 0
|
||||||
|
&& model.Package.BinaryImage.Bits == 64
|
||||||
|
&& useBetterArraySize;
|
||||||
|
|
||||||
public CppScaffolding(AppModel model) => this.model = model;
|
private StreamWriter _writer;
|
||||||
|
|
||||||
// Write the type header
|
// Write the type header
|
||||||
// This can be used by other output modules
|
// This can be used by other output modules
|
||||||
public void WriteTypes(string typeHeaderFile) {
|
public void WriteTypes(string typeHeaderFile) {
|
||||||
using var fs = new FileStream(typeHeaderFile, FileMode.Create);
|
using var fs = new FileStream(typeHeaderFile, FileMode.Create);
|
||||||
writer = new StreamWriter(fs, Encoding.ASCII);
|
_writer = new StreamWriter(fs, Encoding.ASCII);
|
||||||
|
|
||||||
|
using (_writer)
|
||||||
|
{
|
||||||
writeHeader();
|
writeHeader();
|
||||||
|
|
||||||
// Write primitive type definitions for when we're not including other headers
|
// Write primitive type definitions for when we're not including other headers
|
||||||
writeCode($@"#if defined(_GHIDRA_) || defined(_IDA_)
|
writeCode($$"""
|
||||||
|
#if defined(_GHIDRA_) || defined(_IDA_)
|
||||||
typedef unsigned __int8 uint8_t;
|
typedef unsigned __int8 uint8_t;
|
||||||
typedef unsigned __int16 uint16_t;
|
typedef unsigned __int16 uint16_t;
|
||||||
typedef unsigned __int32 uint32_t;
|
typedef unsigned __int32 uint32_t;
|
||||||
@@ -46,7 +56,7 @@ typedef __int64 int64_t;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(_GHIDRA_)
|
#if defined(_GHIDRA_)
|
||||||
typedef __int{model.Package.BinaryImage.Bits} size_t;
|
typedef __int{{_model.Package.BinaryImage.Bits}} size_t;
|
||||||
typedef size_t intptr_t;
|
typedef size_t intptr_t;
|
||||||
typedef size_t uintptr_t;
|
typedef size_t uintptr_t;
|
||||||
#endif
|
#endif
|
||||||
@@ -54,25 +64,41 @@ typedef size_t uintptr_t;
|
|||||||
#if !defined(_GHIDRA_) && !defined(_IDA_)
|
#if !defined(_GHIDRA_) && !defined(_IDA_)
|
||||||
#define _CPLUSPLUS_
|
#define _CPLUSPLUS_
|
||||||
#endif
|
#endif
|
||||||
");
|
""");
|
||||||
|
|
||||||
|
if (_useBetterArraySize)
|
||||||
|
writeCode("#define il2cpp_array_size_t actual_il2cpp_array_size_t");
|
||||||
|
|
||||||
writeSectionHeader("IL2CPP internal types");
|
writeSectionHeader("IL2CPP internal types");
|
||||||
writeCode(model.UnityHeaders.GetTypeHeaderText(model.WordSizeBits));
|
writeCode(_model.UnityHeaders.GetTypeHeaderText(_model.WordSizeBits));
|
||||||
|
|
||||||
|
if (_useBetterArraySize)
|
||||||
|
writeCode("""
|
||||||
|
#undef il2cpp_array_size_t
|
||||||
|
|
||||||
|
typedef union better_il2cpp_array_size_t
|
||||||
|
{
|
||||||
|
int32_t size;
|
||||||
|
actual_il2cpp_array_size_t value;
|
||||||
|
} better_il2cpp_array_size_t;
|
||||||
|
|
||||||
|
#define il2cpp_array_size_t better_il2cpp_array_size_t
|
||||||
|
""");
|
||||||
|
|
||||||
|
if (_model.TargetCompiler == CppCompilerType.MSVC)
|
||||||
|
{
|
||||||
// Stop MSVC complaining about out-of-bounds enum values
|
// Stop MSVC complaining about out-of-bounds enum values
|
||||||
if (model.TargetCompiler == CppCompilerType.MSVC)
|
|
||||||
writeCode("#pragma warning(disable : 4369)");
|
writeCode("#pragma warning(disable : 4369)");
|
||||||
|
|
||||||
// Stop MSVC complaining about constant truncation of enum values
|
// Stop MSVC complaining about constant truncation of enum values
|
||||||
if (model.TargetCompiler == CppCompilerType.MSVC)
|
|
||||||
writeCode("#pragma warning(disable : 4309)");
|
writeCode("#pragma warning(disable : 4309)");
|
||||||
|
|
||||||
// MSVC will (rightly) throw a compiler warning when compiling for 32-bit architectures
|
// MSVC will (rightly) throw a compiler warning when compiling for 32-bit architectures
|
||||||
// if the specified alignment of a type is smaller than the size of its largest element.
|
// if the specified alignment of a type is smaller than the size of its largest element.
|
||||||
// We keep the alignments in to make them match Il2CppObject wherever possible, but it is
|
// We keep the alignments in to make them match Il2CppObject wherever possible, but it is
|
||||||
// safe to ignore them if they are too small, so we just disable the warning
|
// safe to ignore them if they are too small, so we just disable the warning
|
||||||
if (model.TargetCompiler == CppCompilerType.MSVC)
|
|
||||||
writeCode("#pragma warning(disable : 4359)");
|
writeCode("#pragma warning(disable : 4359)");
|
||||||
|
}
|
||||||
|
|
||||||
// C does not support namespaces
|
// C does not support namespaces
|
||||||
writeCode("#if !defined(_GHIDRA_) && !defined(_IDA_)");
|
writeCode("#if !defined(_GHIDRA_) && !defined(_IDA_)");
|
||||||
@@ -88,8 +114,7 @@ typedef size_t uintptr_t;
|
|||||||
writeCode("#if !defined(_GHIDRA_) && !defined(_IDA_)");
|
writeCode("#if !defined(_GHIDRA_) && !defined(_IDA_)");
|
||||||
writeCode("}");
|
writeCode("}");
|
||||||
writeCode("#endif");
|
writeCode("#endif");
|
||||||
|
}
|
||||||
writer.Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Write(string projectPath) {
|
public void Write(string projectPath) {
|
||||||
@@ -110,96 +135,108 @@ typedef size_t uintptr_t;
|
|||||||
// Write selected Unity API function file to il2cpp-api-functions.h
|
// Write selected Unity API function file to il2cpp-api-functions.h
|
||||||
// (this is a copy of the header file from an actual Unity install)
|
// (this is a copy of the header file from an actual Unity install)
|
||||||
var il2cppApiFile = Path.Combine(srcDataPath, "il2cpp-api-functions.h");
|
var il2cppApiFile = Path.Combine(srcDataPath, "il2cpp-api-functions.h");
|
||||||
var apiHeaderText = model.UnityHeaders.GetAPIHeaderText();
|
var apiHeaderText = _model.UnityHeaders.GetAPIHeaderText();
|
||||||
|
|
||||||
using var fsApi = new FileStream(il2cppApiFile, FileMode.Create);
|
using var fsApi = new FileStream(il2cppApiFile, FileMode.Create);
|
||||||
writer = new StreamWriter(fsApi, Encoding.ASCII);
|
_writer = new StreamWriter(fsApi, Encoding.ASCII);
|
||||||
|
|
||||||
|
using (_writer)
|
||||||
|
{
|
||||||
writeHeader();
|
writeHeader();
|
||||||
|
|
||||||
// Elide APIs that aren't in the binary to avoid compile errors
|
// Elide APIs that aren't in the binary to avoid compile errors
|
||||||
foreach (var line in apiHeaderText.Split('\n')) {
|
foreach (var line in apiHeaderText.Split('\n'))
|
||||||
|
{
|
||||||
var fnName = UnityHeaders.GetFunctionNameFromAPILine(line);
|
var fnName = UnityHeaders.GetFunctionNameFromAPILine(line);
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(fnName))
|
if (string.IsNullOrEmpty(fnName))
|
||||||
writer.WriteLine(line);
|
_writer.WriteLine(line);
|
||||||
else if (model.AvailableAPIs.ContainsKey(fnName))
|
else if (_model.AvailableAPIs.ContainsKey(fnName))
|
||||||
writer.WriteLine(line);
|
_writer.WriteLine(line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
writer.Close();
|
|
||||||
|
|
||||||
// Write API function pointers to il2cpp-api-functions-ptr.h
|
// Write API function pointers to il2cpp-api-functions-ptr.h
|
||||||
var il2cppFnPtrFile = Path.Combine(srcDataPath, "il2cpp-api-functions-ptr.h");
|
var il2cppFnPtrFile = Path.Combine(srcDataPath, "il2cpp-api-functions-ptr.h");
|
||||||
|
|
||||||
using var fs2 = new FileStream(il2cppFnPtrFile, FileMode.Create);
|
using var fs2 = new FileStream(il2cppFnPtrFile, FileMode.Create);
|
||||||
writer = new StreamWriter(fs2, Encoding.ASCII);
|
_writer = new StreamWriter(fs2, Encoding.ASCII);
|
||||||
|
|
||||||
|
using (_writer)
|
||||||
|
{
|
||||||
writeHeader();
|
writeHeader();
|
||||||
writeSectionHeader("IL2CPP API function pointers");
|
writeSectionHeader("IL2CPP API function pointers");
|
||||||
|
|
||||||
// We could use model.AvailableAPIs here but that would exclude outputting the address
|
// We could use _model.AvailableAPIs here but that would exclude outputting the address
|
||||||
// of API exports which for some reason aren't defined in our selected API header,
|
// of API exports which for some reason aren't defined in our selected API header,
|
||||||
// so although it doesn't affect the C++ compilation, we use GetAPIExports() instead for completeness
|
// so although it doesn't affect the C++ compilation, we use GetAPIExports() instead for completeness
|
||||||
var exports = model.Package.Binary.APIExports;
|
var exports = _model.Package.Binary.APIExports;
|
||||||
|
|
||||||
foreach (var export in exports) {
|
foreach (var export in exports)
|
||||||
writeCode($"#define {export.Key}_ptr 0x{model.Package.BinaryImage.MapVATR(export.Value):X8}");
|
{
|
||||||
|
writeCode($"#define {export.Key}_ptr 0x{_model.Package.BinaryImage.MapVATR(export.Value):X8}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.Close();
|
|
||||||
|
|
||||||
// Write application type definition addresses to il2cpp-types-ptr.h
|
// Write application type definition addresses to il2cpp-types-ptr.h
|
||||||
var il2cppTypeInfoFile = Path.Combine(srcDataPath, "il2cpp-types-ptr.h");
|
var il2cppTypeInfoFile = Path.Combine(srcDataPath, "il2cpp-types-ptr.h");
|
||||||
|
|
||||||
using var fs3 = new FileStream(il2cppTypeInfoFile, FileMode.Create);
|
using var fs3 = new FileStream(il2cppTypeInfoFile, FileMode.Create);
|
||||||
writer = new StreamWriter(fs3, Encoding.ASCII);
|
_writer = new StreamWriter(fs3, Encoding.ASCII);
|
||||||
|
|
||||||
|
using (_writer)
|
||||||
|
{
|
||||||
writeHeader();
|
writeHeader();
|
||||||
writeSectionHeader("IL2CPP application-specific type definition addresses");
|
writeSectionHeader("IL2CPP application-specific type definition addresses");
|
||||||
|
|
||||||
foreach (var type in model.Types.Values.Where(t => t.TypeClassAddress != 0xffffffff_ffffffff)) {
|
foreach (var type in _model.Types.Values.Where(t => t.TypeClassAddress != 0xffffffff_ffffffff))
|
||||||
writeCode($"DO_TYPEDEF(0x{type.TypeClassAddress - model.Package.BinaryImage.ImageBase:X8}, {type.Name});");
|
{
|
||||||
|
writeCode($"DO_TYPEDEF(0x{type.TypeClassAddress - _model.Package.BinaryImage.ImageBase:X8}, {type.Name});");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.Close();
|
|
||||||
|
|
||||||
// Write method pointers and signatures to il2cpp-functions.h
|
// Write method pointers and signatures to il2cpp-functions.h
|
||||||
var methodFile = Path.Combine(srcDataPath, "il2cpp-functions.h");
|
var methodFile = Path.Combine(srcDataPath, "il2cpp-functions.h");
|
||||||
|
|
||||||
using var fs4 = new FileStream(methodFile, FileMode.Create);
|
using var fs4 = new FileStream(methodFile, FileMode.Create);
|
||||||
writer = new StreamWriter(fs4, Encoding.ASCII);
|
_writer = new StreamWriter(fs4, Encoding.ASCII);
|
||||||
|
|
||||||
|
using (_writer)
|
||||||
|
{
|
||||||
writeHeader();
|
writeHeader();
|
||||||
writeSectionHeader("IL2CPP application-specific method definition addresses and signatures");
|
writeSectionHeader("IL2CPP application-specific method definition addresses and signatures");
|
||||||
|
|
||||||
writeCode("using namespace app;");
|
writeCode("using namespace app;");
|
||||||
writeLine("");
|
writeLine("");
|
||||||
|
|
||||||
foreach (var method in model.Methods.Values) {
|
foreach (var method in _model.Methods.Values)
|
||||||
if (method.HasCompiledCode) {
|
{
|
||||||
|
if (method.HasCompiledCode)
|
||||||
|
{
|
||||||
var arguments = string.Join(", ", method.CppFnPtrType.Arguments.Select(a => a.Type.Name + " " + (a.Name == "this" ? "__this" : a.Name)));
|
var arguments = string.Join(", ", method.CppFnPtrType.Arguments.Select(a => a.Type.Name + " " + (a.Name == "this" ? "__this" : a.Name)));
|
||||||
|
|
||||||
writeCode($"DO_APP_FUNC(0x{method.MethodCodeAddress - model.Package.BinaryImage.ImageBase:X8}, {method.CppFnPtrType.ReturnType.Name}, "
|
writeCode($"DO_APP_FUNC(0x{method.MethodCodeAddress - _model.Package.BinaryImage.ImageBase:X8}, {method.CppFnPtrType.ReturnType.Name}, "
|
||||||
+ $"{method.CppFnPtrType.Name}, ({arguments}));");
|
+ $"{method.CppFnPtrType.Name}, ({arguments}));");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method.HasMethodInfo) {
|
if (method.HasMethodInfo)
|
||||||
writeCode($"DO_APP_FUNC_METHODINFO(0x{method.MethodInfoPtrAddress - model.Package.BinaryImage.ImageBase:X8}, {method.CppFnPtrType.Name}__MethodInfo);");
|
{
|
||||||
|
writeCode($"DO_APP_FUNC_METHODINFO(0x{method.MethodInfoPtrAddress - _model.Package.BinaryImage.ImageBase:X8}, {method.CppFnPtrType.Name}__MethodInfo);");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.Close();
|
|
||||||
|
|
||||||
// Write metadata version
|
// Write metadata version
|
||||||
var versionFile = Path.Combine(srcDataPath, "il2cpp-metadata-version.h");
|
var versionFile = Path.Combine(srcDataPath, "il2cpp-metadata-version.h");
|
||||||
|
|
||||||
using var fs5 = new FileStream(versionFile, FileMode.Create);
|
using var fs5 = new FileStream(versionFile, FileMode.Create);
|
||||||
writer = new StreamWriter(fs5, Encoding.ASCII);
|
_writer = new StreamWriter(fs5, Encoding.ASCII);
|
||||||
|
|
||||||
|
using (_writer)
|
||||||
|
{
|
||||||
writeHeader();
|
writeHeader();
|
||||||
writeCode($"#define __IL2CPP_METADATA_VERSION {model.Package.Version * 10:F0}");
|
writeCode($"#define __IL2CPP_METADATA_VERSION {_model.Package.Version * 10:F0}");
|
||||||
|
}
|
||||||
writer.Close();
|
|
||||||
|
|
||||||
// Write boilerplate code
|
// Write boilerplate code
|
||||||
File.WriteAllText(Path.Combine(srcFxPath, "dllmain.cpp"), Resources.Cpp_DLLMainCpp);
|
File.WriteAllText(Path.Combine(srcFxPath, "dllmain.cpp"), Resources.Cpp_DLLMainCpp);
|
||||||
@@ -251,13 +288,13 @@ typedef size_t uintptr_t;
|
|||||||
|
|
||||||
private void writeHeader() {
|
private void writeHeader() {
|
||||||
writeLine("// Generated C++ file by Il2CppInspector - http://www.djkaty.com - https://github.com/djkaty");
|
writeLine("// Generated C++ file by Il2CppInspector - http://www.djkaty.com - https://github.com/djkaty");
|
||||||
writeLine("// Target Unity version: " + model.UnityHeaders);
|
writeLine("// Target Unity version: " + _model.UnityHeaders);
|
||||||
writeLine("");
|
writeLine("");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeTypesForGroup(string header, string group) {
|
private void writeTypesForGroup(string header, string group) {
|
||||||
writeSectionHeader(header);
|
writeSectionHeader(header);
|
||||||
foreach (var cppType in model.GetDependencyOrderedCppTypeGroup(group))
|
foreach (var cppType in _model.GetDependencyOrderedCppTypeGroup(group))
|
||||||
if (cppType is CppEnumType) {
|
if (cppType is CppEnumType) {
|
||||||
// Ghidra can't process C++ enum base types
|
// Ghidra can't process C++ enum base types
|
||||||
writeCode("#if defined(_CPLUSPLUS_)");
|
writeCode("#if defined(_CPLUSPLUS_)");
|
||||||
@@ -271,16 +308,16 @@ typedef size_t uintptr_t;
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void writeCode(string text) {
|
private void writeCode(string text) {
|
||||||
if (model.TargetCompiler == CppCompilerType.MSVC)
|
if (_model.TargetCompiler == CppCompilerType.MSVC)
|
||||||
text = rgxGCCalign.Replace(text, @"__declspec(align($1))");
|
text = GccAlignRegex().Replace(text, @"__declspec(align($1))");
|
||||||
if (model.TargetCompiler == CppCompilerType.GCC)
|
else if (_model.TargetCompiler == CppCompilerType.GCC)
|
||||||
text = rgxMSVCalign.Replace(text, @"__attribute__((aligned($1)))");
|
text = MsvcAlignRegex().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()); Not sure if this is necessary? maybe for some obfuscated assemblies, but those would just fail on other steps
|
||||||
var declString = string.Join('\n', cleanLines);
|
|
||||||
if (declString != "")
|
foreach (var line in lines)
|
||||||
writeLine(declString);
|
writeLine(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeSectionHeader(string name) {
|
private void writeSectionHeader(string name) {
|
||||||
@@ -290,6 +327,12 @@ typedef size_t uintptr_t;
|
|||||||
writeLine("");
|
writeLine("");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeLine(string line) => writer.WriteLine(line);
|
private void writeLine(string line) => _writer.WriteLine(line);
|
||||||
|
|
||||||
|
[GeneratedRegex(@"__attribute__\s*?\(\s*?\(\s*?aligned\s*?\(\s*?([0-9]+)\s*?\)\s*?\)\s*?\)")]
|
||||||
|
private static partial Regex GccAlignRegex();
|
||||||
|
|
||||||
|
[GeneratedRegex(@"__declspec\s*?\(\s*?align\s*?\(\s*?([0-9]+)\s*?\)\s*?\)")]
|
||||||
|
private static partial Regex MsvcAlignRegex();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ namespace Il2CppInspector.Outputs
|
|||||||
File.WriteAllText(outputFile, script);
|
File.WriteAllText(outputFile, script);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeTypes(string typeHeaderFile) => new CppScaffolding(model).WriteTypes(typeHeaderFile);
|
private void writeTypes(string typeHeaderFile) => new CppScaffolding(model, useBetterArraySize: true).WriteTypes(typeHeaderFile);
|
||||||
|
|
||||||
private void writeJsonMetadata(string jsonMetadataFile) => new JSONMetadata(model).Write(jsonMetadataFile);
|
private void writeJsonMetadata(string jsonMetadataFile) => new JSONMetadata(model).Write(jsonMetadataFile);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user