add initial support for required forward references in il2cpp types, also fix issues with type names clashing with il2cpp api types

This commit is contained in:
LukeFZ
2025-07-25 21:20:04 +02:00
parent 771eb8eb52
commit 6ddbf7ecae
4 changed files with 741 additions and 617 deletions

View File

@@ -5,19 +5,17 @@
All rights reserved. All rights reserved.
*/ */
using System; using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Il2CppInspector.Cpp.UnityHeaders; using Il2CppInspector.Cpp.UnityHeaders;
using Il2CppInspector.Model; using Il2CppInspector.Model;
using Il2CppInspector.Reflection; using Il2CppInspector.Reflection;
namespace Il2CppInspector.Cpp namespace Il2CppInspector.Cpp;
// Class for generating C header declarations from Reflection objects (TypeInfo, etc.)
public class CppDeclarationGenerator
{ {
// Class for generating C header declarations from Reflection objects (TypeInfo, etc.)
public class CppDeclarationGenerator
{
private readonly AppModel appModel; private readonly AppModel appModel;
private TypeModel model => appModel.TypeModel; private TypeModel model => appModel.TypeModel;
@@ -33,7 +31,7 @@ namespace Il2CppInspector.Cpp
// Different C++ compilers lay out C++ class structures differently, // Different C++ compilers lay out C++ class structures differently,
// meaning that the compiler must be known in order to generate class type structures // meaning that the compiler must be known in order to generate class type structures
// with the correct layout. // with the correct layout.
public CppCompilerType InheritanceStyle; public readonly CppCompilerType InheritanceStyle;
public CppDeclarationGenerator(AppModel appModel) { public CppDeclarationGenerator(AppModel appModel) {
this.appModel = appModel; this.appModel = appModel;
@@ -46,7 +44,8 @@ namespace Il2CppInspector.Cpp
} }
// C type declaration used to name variables of the given C# type // C type declaration used to name variables of the given C# type
private static Dictionary<string, string> primitiveTypeMap = new Dictionary<string, string> { private static readonly Dictionary<string, string> primitiveTypeMap = new()
{
["Boolean"] = "bool", ["Boolean"] = "bool",
["Byte"] = "uint8_t", ["Byte"] = "uint8_t",
["SByte"] = "int8_t", ["SByte"] = "int8_t",
@@ -68,24 +67,28 @@ namespace Il2CppInspector.Cpp
if (ti.IsByRef || ti.IsPointer) { if (ti.IsByRef || ti.IsPointer) {
return AsCType(ti.ElementType).AsPointer(WordSize); return AsCType(ti.ElementType).AsPointer(WordSize);
} }
if (ti.IsValueType) { if (ti.IsValueType) {
if (ti.IsPrimitive && primitiveTypeMap.ContainsKey(ti.Name)) { if (ti.IsPrimitive && primitiveTypeMap.TryGetValue(ti.Name, out var value)) {
return types.GetType(primitiveTypeMap[ti.Name]); return types.GetType(value);
} }
return types.GetType(TypeNamer.GetName(ti)); return types.GetType(TypeNamer.GetName(ti));
} }
if (ti.IsEnum) { if (ti.IsEnum) {
return types.GetType(TypeNamer.GetName(ti)); return types.GetType(TypeNamer.GetName(ti));
} }
return types.GetType(TypeNamer.GetName(ti) + " *"); return types.GetType(TypeNamer.GetName(ti) + " *");
} }
// Resets the cache of visited types and pending types to output, but preserve any names we have already generated // Resets the cache of visited types and pending types to output, but preserve any names we have already generated
public void Reset() { public void Reset() {
VisitedFieldStructs.Clear(); _visitedFieldStructs.Clear();
VisitedTypes.Clear(); _visitedTypes.Clear();
TodoFieldStructs.Clear(); _todoFieldStructs.Clear();
TodoTypeStructs.Clear(); _todoTypeStructs.Clear();
} }
#region Field Struct Generation #region Field Struct Generation
@@ -96,37 +99,123 @@ namespace Il2CppInspector.Cpp
*/ */
// A cache of field structures that have already been generated, to eliminate duplicate definitions // A cache of field structures that have already been generated, to eliminate duplicate definitions
private readonly HashSet<TypeInfo> VisitedFieldStructs = new HashSet<TypeInfo>(); private readonly HashSet<TypeInfo> _visitedFieldStructs = [];
// A queue of field structures that need to be generated. // A queue of field structures that need to be generated.
private readonly List<TypeInfo> TodoFieldStructs = new List<TypeInfo>(); private readonly List<TypeInfo> _todoFieldStructs = [];
// Walk over dependencies of the given type, to figure out what field structures it depends on private readonly HashSet<TypeInfo> _requiredForwardDefinitionsForFields = [];
private void VisitFieldStructs(TypeInfo ti) {
if (VisitedFieldStructs.Contains(ti))
return;
if (ti.IsByRef || ti.ContainsGenericParameters)
return;
VisitedFieldStructs.Add(ti);
if (ti.BaseType != null) private readonly HashSet<TypeInfo> _currentVisitedFieldStructs = [];
VisitFieldStructs(ti.BaseType); private readonly HashSet<TypeInfo> _currentTodoFieldStructs = [];
private readonly HashSet<TypeInfo> _currentRequiredForwardDefinitions = [];
private readonly HashSet<TypeInfo> _currentlyVisitingFieldStructs = [];
if (ti.IsArray) private class CircularReferenceException(TypeInfo circularType, TypeInfo parentType) : Exception("Circular reference detected")
VisitFieldStructs(ti.ElementType);
if (ti.IsEnum)
VisitFieldStructs(ti.GetEnumUnderlyingType());
foreach (var fi in ti.DeclaredFields.Where(fi => !fi.IsStatic && !fi.IsLiteral))
{ {
if (fi.FieldType.IsEnum || fi.FieldType.IsValueType) public TypeInfo CircularReferencedType { get; } = circularType;
VisitFieldStructs(fi.FieldType); public TypeInfo ParentType { get; } = parentType;
else if (fi.FieldType.HasElementType)
VisitFieldStructs(fi.FieldType.ElementType);
} }
TodoFieldStructs.Add(ti); // Walk over dependencies of the given type, to figure out what field structures it depends on
private void VisitFieldStructsInner(TypeInfo ti)
{
if (_visitedFieldStructs.Contains(ti) || _currentVisitedFieldStructs.Contains(ti))
return;
if (ti.IsByRef || ti.ContainsGenericParameters)
return;
_currentVisitedFieldStructs.Add(ti);
_currentlyVisitingFieldStructs.Add(ti);
if (ti.BaseType != null)
VisitFieldStructsInner(ti.BaseType);
if (ti.IsArray)
VisitFieldStructsInner(ti.ElementType);
if (ti.IsEnum)
VisitFieldStructsInner(ti.GetEnumUnderlyingType());
foreach (var fi in ti.DeclaredFields.Where(fi => !fi.IsStatic && !fi.IsLiteral))
ProcessTypeField(fi);
_currentTodoFieldStructs.Add(ti);
_currentlyVisitingFieldStructs.Remove(ti);
return;
void ProcessTypeField(FieldInfo fi)
{
if (fi.FieldType.IsEnum || fi.FieldType.IsValueType)
{
VisitFieldStructsInner(fi.FieldType);
}
else if (fi.FieldType.HasElementType)
{
var elementType = fi.FieldType.ElementType;
if (!fi.FieldType.IsPointer || !_currentRequiredForwardDefinitions.Contains(elementType))
{
VisitFieldStructsInner(elementType);
if (elementType.IsValueType
&& elementType != ti
&& _currentlyVisitingFieldStructs.Contains(elementType)
&& !_currentRequiredForwardDefinitions.Contains(elementType))
{
// this is now an issue: there is a loop, and we need to resolve it
// if the field type is a pointer, we can make a forward declaration and be done with it
// otherwise, we cannot generate these types
if (!fi.FieldType.IsPointer)
Debugger.Break();
throw new CircularReferenceException(elementType, ti);
}
}
}
}
}
private void ClearCurrentFieldStructVisitState()
{
_currentTodoFieldStructs.Clear();
_currentVisitedFieldStructs.Clear();
_currentlyVisitingFieldStructs.Clear();
}
private void VisitFieldStructs(TypeInfo ti)
{
ClearCurrentFieldStructVisitState();
var requiredTypesToVisit = new Stack<TypeInfo>([ti]);
while (true)
{
try
{
foreach (var typeToVisit in requiredTypesToVisit)
VisitFieldStructsInner(typeToVisit);
}
catch (CircularReferenceException ex)
{
ClearCurrentFieldStructVisitState();
_currentRequiredForwardDefinitions.Add(ex.CircularReferencedType);
requiredTypesToVisit.Push(ex.ParentType);
continue;
}
break;
}
_todoFieldStructs.AddRange(_currentTodoFieldStructs);
foreach (var visitedType in _currentVisitedFieldStructs)
_visitedFieldStructs.Add(visitedType);
foreach (var requiredType in _currentRequiredForwardDefinitions)
_requiredForwardDefinitionsForFields.Add(requiredType);
} }
// Generate the fields for the base class of all objects (Il2CppObject) // Generate the fields for the base class of all objects (Il2CppObject)
@@ -261,8 +350,8 @@ namespace Il2CppInspector.Cpp
// "Flush" the list of visited types, generating C structures for each one // "Flush" the list of visited types, generating C structures for each one
private List<(TypeInfo ilType, CppComplexType valueType, CppComplexType referenceType, CppComplexType fieldsType)> GenerateVisitedFieldStructs() { private List<(TypeInfo ilType, CppComplexType valueType, CppComplexType referenceType, CppComplexType fieldsType)> GenerateVisitedFieldStructs() {
var structs = new List<(TypeInfo ilType, CppComplexType valueType, CppComplexType referenceType, CppComplexType fieldsType)>(TodoTypeStructs.Count); var structs = new List<(TypeInfo ilType, CppComplexType valueType, CppComplexType referenceType, CppComplexType fieldsType)>(_todoTypeStructs.Count);
foreach (var ti in TodoFieldStructs) { foreach (var ti in _todoFieldStructs) {
if (ti.IsEnum || ti.IsValueType) { if (ti.IsEnum || ti.IsValueType) {
var (valueType, boxedType) = GenerateValueFieldStruct(ti); var (valueType, boxedType) = GenerateValueFieldStruct(ti);
structs.Add((ti, valueType, boxedType, null)); structs.Add((ti, valueType, boxedType, null));
@@ -272,7 +361,7 @@ namespace Il2CppInspector.Cpp
structs.Add((ti, null, objectOrArrayType, fieldsType)); structs.Add((ti, null, objectOrArrayType, fieldsType));
} }
} }
TodoFieldStructs.Clear(); _todoFieldStructs.Clear();
return structs; return structs;
} }
#endregion #endregion
@@ -280,7 +369,7 @@ namespace Il2CppInspector.Cpp
#region Class Struct Generation #region Class Struct Generation
// Concrete implementations for abstract classes, for use in looking up VTable signatures and names // Concrete implementations for abstract classes, for use in looking up VTable signatures and names
private readonly Dictionary<TypeInfo, TypeInfo> ConcreteImplementations = new Dictionary<TypeInfo, TypeInfo>(); private readonly Dictionary<TypeInfo, TypeInfo> _concreteImplementations = new();
/// <summary> /// <summary>
/// VTables for abstract types have "null" in place of abstract functions. /// VTables for abstract types have "null" in place of abstract functions.
/// This function searches for concrete implementations so that we can properly /// This function searches for concrete implementations so that we can properly
@@ -292,8 +381,9 @@ namespace Il2CppInspector.Cpp
continue; continue;
var baseType = ti.BaseType; var baseType = ti.BaseType;
while (baseType != null) { while (baseType != null) {
if (baseType.IsAbstract && !ConcreteImplementations.ContainsKey(baseType)) if (baseType.IsAbstract)
ConcreteImplementations[baseType] = ti; _concreteImplementations.TryAdd(baseType, ti);
baseType = baseType.BaseType; baseType = baseType.BaseType;
} }
} }
@@ -315,37 +405,45 @@ namespace Il2CppInspector.Cpp
* care which concrete implementation we put in this table! The name * care which concrete implementation we put in this table! The name
* and signature will always match that of the abstract type. * and signature will always match that of the abstract type.
*/ */
if (ti.IsAbstract && ConcreteImplementations.ContainsKey(ti)) { if (ti.IsAbstract && _concreteImplementations.TryGetValue(ti, out var implementation)) {
var impl = implementation.GetVTable();
res = (MethodBase[])res.Clone(); res = (MethodBase[])res.Clone();
MethodBase[] impl = ConcreteImplementations[ti].GetVTable(); for (int i = 0; i < res.Length; i++)
for (int i = 0; i < res.Length; i++) { {
if (res[i] == null) res[i] ??= impl[i];
res[i] = impl[i];
} }
} }
return res; return res;
} }
private readonly HashSet<TypeInfo> VisitedTypes = new HashSet<TypeInfo>(); private readonly HashSet<TypeInfo> _visitedTypes = [];
private readonly List<TypeInfo> TodoTypeStructs = new List<TypeInfo>(); private readonly List<TypeInfo> _todoTypeStructs = [];
/// <summary> /// <summary>
/// Include the given type into this generator. This will add the given type and all types it depends on. /// Include the given type into this generator. This will add the given type and all types it depends on.
/// Call GenerateRemainingTypeDeclarations to produce the actual type declarations afterwards. /// Call GenerateRemainingTypeDeclarations to produce the actual type declarations afterwards.
/// </summary> /// </summary>
/// <param name="ti"></param> /// <param name="ti"></param>
public void IncludeType(TypeInfo ti) { public void IncludeType(TypeInfo ti)
if (VisitedTypes.Contains(ti)) {
if (ti.Name.Contains("UQueryState"))
Console.WriteLine("meow");
if (_visitedTypes.Contains(ti))
return; return;
if (ti.ContainsGenericParameters) if (ti.ContainsGenericParameters)
return; return;
VisitedTypes.Add(ti);
if (ti.IsArray) { _visitedTypes.Add(ti);
if (ti.IsArray || ti.HasElementType)
{
IncludeType(ti.ElementType); IncludeType(ti.ElementType);
} else if (ti.HasElementType) { }
IncludeType(ti.ElementType); else if (ti.IsEnum)
} else if (ti.IsEnum) { {
IncludeType(ti.GetEnumUnderlyingType()); IncludeType(ti.GetEnumUnderlyingType());
} }
@@ -362,10 +460,12 @@ namespace Il2CppInspector.Cpp
IncludeType(fi.FieldType); IncludeType(fi.FieldType);
foreach (var mi in GetFilledVTable(ti)) foreach (var mi in GetFilledVTable(ti))
if (mi != null && !mi.ContainsGenericParameters) {
if (mi is { ContainsGenericParameters: false })
IncludeMethod(mi); IncludeMethod(mi);
}
TodoTypeStructs.Add(ti); _todoTypeStructs.Add(ti);
} }
// Generate the C structure for virtual function calls in a given type (the VTable) // Generate the C structure for virtual function calls in a given type (the VTable)
@@ -452,7 +552,7 @@ namespace Il2CppInspector.Cpp
(CppComplexType)null)) (CppComplexType)null))
.ToList(); .ToList();
foreach (var ti in TodoTypeStructs) foreach (var ti in _todoTypeStructs)
{ {
var (cls, statics, vtable) = GenerateTypeStruct(ti); var (cls, statics, vtable) = GenerateTypeStruct(ti);
decl.Add((ti, null, cls, null, vtable, statics)); decl.Add((ti, null, cls, null, vtable, statics));
@@ -466,10 +566,17 @@ namespace Il2CppInspector.Cpp
} }
finally finally
{ {
TodoTypeStructs.Clear(); _todoTypeStructs.Clear();
TodoFieldStructs.Clear(); _todoFieldStructs.Clear();
} }
} }
public List<CppType> GenerateRequiredForwardDefinitions()
=> _requiredForwardDefinitionsForFields
.Select(x => new CppForwardDefinitionType(TypeNamer.GetName(x)))
.Cast<CppType>()
.ToList();
#endregion #endregion
#region Method Generation #region Method Generation
@@ -635,5 +742,4 @@ namespace Il2CppInspector.Cpp
public CppNamespace GlobalsNamespace { get; private set; } public CppNamespace GlobalsNamespace { get; private set; }
public CppNamespace.Namer<MethodBase> GlobalNamer { get; private set; } public CppNamespace.Namer<MethodBase> GlobalNamer { get; private set; }
#endregion #endregion
}
} }

View File

@@ -5,9 +5,6 @@
All rights reserved. All rights reserved.
*/ */
using System;
using System.Collections.Generic;
namespace Il2CppInspector.Cpp namespace Il2CppInspector.Cpp
{ {
/// <summary> /// <summary>
@@ -65,9 +62,9 @@ namespace Il2CppInspector.Cpp
// Uniquely name an object within the parent namespace // Uniquely name an object within the parent namespace
public string GetName(T t) { public string GetName(T t) {
// If we've named this particular object before, just return that name // If we've named this particular object before, just return that name
string name; if (names.TryGetValue(t, out var name))
if (names.TryGetValue(t, out name))
return name; return name;
// Obtain the mangled name for the object // Obtain the mangled name for the object
name = keyFunc(t); name = keyFunc(t);
// Check if the mangled name has been given to another object - if it has, // Check if the mangled name has been given to another object - if it has,

View File

@@ -456,4 +456,14 @@ namespace Il2CppInspector.Cpp
return sb.ToString(); return sb.ToString();
} }
} }
public class CppForwardDefinitionType : CppType
{
public CppForwardDefinitionType(string name) : base(name)
{
}
public override string ToString(string format = "") => $"struct {Name};";
}
} }

View File

@@ -4,15 +4,11 @@
All rights reserved. All rights reserved.
*/ */
using System; using Il2CppInspector.Cpp.UnityHeaders;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Il2CppInspector.Cpp.UnityHeaders;
namespace Il2CppInspector.Cpp namespace Il2CppInspector.Cpp
{ {
@@ -23,7 +19,7 @@ namespace Il2CppInspector.Cpp
public Dictionary<string, CppType> Types { get; } public Dictionary<string, CppType> Types { get; }
// All of the literal typedef aliases // All of the literal typedef aliases
public Dictionary<string, CppType> TypedefAliases { get; } = new Dictionary<string, CppType>(); public Dictionary<string, CppType> TypedefAliases { get; } = [];
public CppType this[string s] => Types.ContainsKey(s)? Types[s] : public CppType this[string s] => Types.ContainsKey(s)? Types[s] :
TypedefAliases.ContainsKey(s)? TypedefAliases[s].AsAlias(s) : null; TypedefAliases.ContainsKey(s)? TypedefAliases[s].AsAlias(s) : null;
@@ -34,7 +30,8 @@ namespace Il2CppInspector.Cpp
// Architecture width in bits (32/64) - to determine pointer sizes // Architecture width in bits (32/64) - to determine pointer sizes
public int WordSize { get; } public int WordSize { get; }
private Dictionary<string, ComplexValueType> complexTypeMap = new Dictionary<string, ComplexValueType> { private Dictionary<string, ComplexValueType> complexTypeMap = new()
{
["struct"] = ComplexValueType.Struct, ["struct"] = ComplexValueType.Struct,
["union"] = ComplexValueType.Union, ["union"] = ComplexValueType.Union,
["enum"] = ComplexValueType.Enum ["enum"] = ComplexValueType.Enum
@@ -44,22 +41,23 @@ namespace Il2CppInspector.Cpp
private string currentGroup = string.Empty; private string currentGroup = string.Empty;
public void SetGroup(string group) => currentGroup = group; public void SetGroup(string group) => currentGroup = group;
private static readonly List<CppType> primitiveTypes = new List<CppType> { private static readonly List<CppType> primitiveTypes =
new CppType("uint8_t", 8), [
new CppType("uint16_t", 16), new("uint8_t", 8),
new CppType("uint32_t", 32), new("uint16_t", 16),
new CppType("uint64_t", 64), new("uint32_t", 32),
new CppType("int8_t", 8), new("uint64_t", 64),
new CppType("int16_t", 16), new("int8_t", 8),
new CppType("int32_t", 32), new("int16_t", 16),
new CppType("int64_t", 64), new("int32_t", 32),
new CppType("char", 8), new("int64_t", 64),
new CppType("int", 32), new("char", 8),
new CppType("float", 32), new("int", 32),
new CppType("double", 64), new("float", 32),
new CppType("bool", 8), new("double", 64),
new CppType("void", 0) new("bool", 8),
}; new("void", 0)
];
public CppTypeCollection(int wordSize) { public CppTypeCollection(int wordSize) {
if (wordSize != 32 && wordSize != 64) if (wordSize != 32 && wordSize != 64)
@@ -538,15 +536,18 @@ namespace Il2CppInspector.Cpp
public CppComplexType Struct(string name = "", int alignmentBytes = 0) { public CppComplexType Struct(string name = "", int alignmentBytes = 0) {
if (!string.IsNullOrEmpty(name) && Types.TryGetValue(name, out var cppType)) if (!string.IsNullOrEmpty(name) && Types.TryGetValue(name, out var cppType))
return (CppComplexType) cppType; return (CppComplexType) cppType;
var type = new CppComplexType(ComplexValueType.Struct) {Name = name, Group = currentGroup, AlignmentBytes = alignmentBytes}; var type = new CppComplexType(ComplexValueType.Struct) {Name = name, Group = currentGroup, AlignmentBytes = alignmentBytes};
if (!string.IsNullOrEmpty(name)) if (!string.IsNullOrEmpty(name))
Add(type); Add(type);
return type; return type;
} }
public CppComplexType Union(string name = "", int alignmentBytes = 0) { public CppComplexType Union(string name = "", int alignmentBytes = 0) {
if (!string.IsNullOrEmpty(name) && Types.TryGetValue(name, out var cppType)) if (!string.IsNullOrEmpty(name) && Types.TryGetValue(name, out var cppType))
return (CppComplexType) cppType; return (CppComplexType) cppType;
var type = new CppComplexType(ComplexValueType.Union) {Name = name, Group = currentGroup, AlignmentBytes = alignmentBytes}; var type = new CppComplexType(ComplexValueType.Union) {Name = name, Group = currentGroup, AlignmentBytes = alignmentBytes};
if (!string.IsNullOrEmpty(name)) if (!string.IsNullOrEmpty(name))
Add(type); Add(type);
@@ -554,9 +555,13 @@ namespace Il2CppInspector.Cpp
} }
public CppEnumType Enum(CppType underlyingType, string name = "") { public CppEnumType Enum(CppType underlyingType, string name = "") {
if (!string.IsNullOrEmpty(name) && Types.TryGetValue(name, out var cppType))
return (CppEnumType)cppType;
var type = new CppEnumType(underlyingType) {Name = name, Group = currentGroup}; var type = new CppEnumType(underlyingType) {Name = name, Group = currentGroup};
if (!string.IsNullOrEmpty(name)) if (!string.IsNullOrEmpty(name))
Add(type); Add(type);
return type; return type;
} }
@@ -585,11 +590,17 @@ namespace Il2CppInspector.Cpp
cppTypes.AddFromDeclarationText(apis); cppTypes.AddFromDeclarationText(apis);
// Don't allow any of the header type names or primitive type names to be re-used // Don't allow any of the header type names or primitive type names to be re-used
foreach (var type in cppTypes.Types.Values) foreach (var type in cppTypes.Types.Keys)
declGen?.TypeNamespace.TryReserveName(type.Name); {
declGen?.TypeNamespace.TryReserveName(type);
declGen?.GlobalsNamespace.TryReserveName(type);
}
foreach (var typedef in cppTypes.TypedefAliases.Values) foreach (var typedef in cppTypes.TypedefAliases.Keys)
declGen?.GlobalsNamespace.TryReserveName(typedef.Name); {
declGen?.TypeNamespace.TryReserveName(typedef);
declGen?.GlobalsNamespace.TryReserveName(typedef);
}
cppTypes.SetGroup(""); cppTypes.SetGroup("");