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.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Il2CppInspector.Cpp.UnityHeaders;
using Il2CppInspector.Model;
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 TypeModel model => appModel.TypeModel;
@@ -33,7 +31,7 @@ namespace Il2CppInspector.Cpp
// Different C++ compilers lay out C++ class structures differently,
// meaning that the compiler must be known in order to generate class type structures
// with the correct layout.
public CppCompilerType InheritanceStyle;
public readonly CppCompilerType InheritanceStyle;
public CppDeclarationGenerator(AppModel appModel) {
this.appModel = appModel;
@@ -46,7 +44,8 @@ namespace Il2CppInspector.Cpp
}
// 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",
["Byte"] = "uint8_t",
["SByte"] = "int8_t",
@@ -68,24 +67,28 @@ namespace Il2CppInspector.Cpp
if (ti.IsByRef || ti.IsPointer) {
return AsCType(ti.ElementType).AsPointer(WordSize);
}
if (ti.IsValueType) {
if (ti.IsPrimitive && primitiveTypeMap.ContainsKey(ti.Name)) {
return types.GetType(primitiveTypeMap[ti.Name]);
if (ti.IsPrimitive && primitiveTypeMap.TryGetValue(ti.Name, out var value)) {
return types.GetType(value);
}
return types.GetType(TypeNamer.GetName(ti));
}
if (ti.IsEnum) {
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
public void Reset() {
VisitedFieldStructs.Clear();
VisitedTypes.Clear();
TodoFieldStructs.Clear();
TodoTypeStructs.Clear();
_visitedFieldStructs.Clear();
_visitedTypes.Clear();
_todoFieldStructs.Clear();
_todoTypeStructs.Clear();
}
#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
private readonly HashSet<TypeInfo> VisitedFieldStructs = new HashSet<TypeInfo>();
private readonly HashSet<TypeInfo> _visitedFieldStructs = [];
// 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 void VisitFieldStructs(TypeInfo ti) {
if (VisitedFieldStructs.Contains(ti))
return;
if (ti.IsByRef || ti.ContainsGenericParameters)
return;
VisitedFieldStructs.Add(ti);
private readonly HashSet<TypeInfo> _requiredForwardDefinitionsForFields = [];
if (ti.BaseType != null)
VisitFieldStructs(ti.BaseType);
private readonly HashSet<TypeInfo> _currentVisitedFieldStructs = [];
private readonly HashSet<TypeInfo> _currentTodoFieldStructs = [];
private readonly HashSet<TypeInfo> _currentRequiredForwardDefinitions = [];
private readonly HashSet<TypeInfo> _currentlyVisitingFieldStructs = [];
if (ti.IsArray)
VisitFieldStructs(ti.ElementType);
if (ti.IsEnum)
VisitFieldStructs(ti.GetEnumUnderlyingType());
foreach (var fi in ti.DeclaredFields.Where(fi => !fi.IsStatic && !fi.IsLiteral))
private class CircularReferenceException(TypeInfo circularType, TypeInfo parentType) : Exception("Circular reference detected")
{
if (fi.FieldType.IsEnum || fi.FieldType.IsValueType)
VisitFieldStructs(fi.FieldType);
else if (fi.FieldType.HasElementType)
VisitFieldStructs(fi.FieldType.ElementType);
public TypeInfo CircularReferencedType { get; } = circularType;
public TypeInfo ParentType { get; } = parentType;
}
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)
@@ -261,8 +350,8 @@ namespace Il2CppInspector.Cpp
// "Flush" the list of visited types, generating C structures for each one
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);
foreach (var ti in TodoFieldStructs) {
var structs = new List<(TypeInfo ilType, CppComplexType valueType, CppComplexType referenceType, CppComplexType fieldsType)>(_todoTypeStructs.Count);
foreach (var ti in _todoFieldStructs) {
if (ti.IsEnum || ti.IsValueType) {
var (valueType, boxedType) = GenerateValueFieldStruct(ti);
structs.Add((ti, valueType, boxedType, null));
@@ -272,7 +361,7 @@ namespace Il2CppInspector.Cpp
structs.Add((ti, null, objectOrArrayType, fieldsType));
}
}
TodoFieldStructs.Clear();
_todoFieldStructs.Clear();
return structs;
}
#endregion
@@ -280,7 +369,7 @@ namespace Il2CppInspector.Cpp
#region Class Struct Generation
// 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>
/// VTables for abstract types have "null" in place of abstract functions.
/// This function searches for concrete implementations so that we can properly
@@ -292,8 +381,9 @@ namespace Il2CppInspector.Cpp
continue;
var baseType = ti.BaseType;
while (baseType != null) {
if (baseType.IsAbstract && !ConcreteImplementations.ContainsKey(baseType))
ConcreteImplementations[baseType] = ti;
if (baseType.IsAbstract)
_concreteImplementations.TryAdd(baseType, ti);
baseType = baseType.BaseType;
}
}
@@ -315,37 +405,45 @@ namespace Il2CppInspector.Cpp
* care which concrete implementation we put in this table! The name
* 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();
MethodBase[] impl = ConcreteImplementations[ti].GetVTable();
for (int i = 0; i < res.Length; i++) {
if (res[i] == null)
res[i] = impl[i];
for (int i = 0; i < res.Length; i++)
{
res[i] ??= impl[i];
}
}
return res;
}
private readonly HashSet<TypeInfo> VisitedTypes = new HashSet<TypeInfo>();
private readonly List<TypeInfo> TodoTypeStructs = new List<TypeInfo>();
private readonly HashSet<TypeInfo> _visitedTypes = [];
private readonly List<TypeInfo> _todoTypeStructs = [];
/// <summary>
/// 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.
/// </summary>
/// <param name="ti"></param>
public void IncludeType(TypeInfo ti) {
if (VisitedTypes.Contains(ti))
public void IncludeType(TypeInfo ti)
{
if (ti.Name.Contains("UQueryState"))
Console.WriteLine("meow");
if (_visitedTypes.Contains(ti))
return;
if (ti.ContainsGenericParameters)
return;
VisitedTypes.Add(ti);
if (ti.IsArray) {
_visitedTypes.Add(ti);
if (ti.IsArray || ti.HasElementType)
{
IncludeType(ti.ElementType);
} else if (ti.HasElementType) {
IncludeType(ti.ElementType);
} else if (ti.IsEnum) {
}
else if (ti.IsEnum)
{
IncludeType(ti.GetEnumUnderlyingType());
}
@@ -362,10 +460,12 @@ namespace Il2CppInspector.Cpp
IncludeType(fi.FieldType);
foreach (var mi in GetFilledVTable(ti))
if (mi != null && !mi.ContainsGenericParameters)
{
if (mi is { ContainsGenericParameters: false })
IncludeMethod(mi);
}
TodoTypeStructs.Add(ti);
_todoTypeStructs.Add(ti);
}
// Generate the C structure for virtual function calls in a given type (the VTable)
@@ -452,7 +552,7 @@ namespace Il2CppInspector.Cpp
(CppComplexType)null))
.ToList();
foreach (var ti in TodoTypeStructs)
foreach (var ti in _todoTypeStructs)
{
var (cls, statics, vtable) = GenerateTypeStruct(ti);
decl.Add((ti, null, cls, null, vtable, statics));
@@ -466,10 +566,17 @@ namespace Il2CppInspector.Cpp
}
finally
{
TodoTypeStructs.Clear();
TodoFieldStructs.Clear();
_todoTypeStructs.Clear();
_todoFieldStructs.Clear();
}
}
public List<CppType> GenerateRequiredForwardDefinitions()
=> _requiredForwardDefinitionsForFields
.Select(x => new CppForwardDefinitionType(TypeNamer.GetName(x)))
.Cast<CppType>()
.ToList();
#endregion
#region Method Generation
@@ -635,5 +742,4 @@ namespace Il2CppInspector.Cpp
public CppNamespace GlobalsNamespace { get; private set; }
public CppNamespace.Namer<MethodBase> GlobalNamer { get; private set; }
#endregion
}
}

View File

@@ -5,9 +5,6 @@
All rights reserved.
*/
using System;
using System.Collections.Generic;
namespace Il2CppInspector.Cpp
{
/// <summary>
@@ -65,9 +62,9 @@ namespace Il2CppInspector.Cpp
// Uniquely name an object within the parent namespace
public string GetName(T t) {
// If we've named this particular object before, just return that name
string name;
if (names.TryGetValue(t, out name))
if (names.TryGetValue(t, out var name))
return name;
// Obtain the mangled name for the object
name = keyFunc(t);
// 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();
}
}
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.
*/
using System;
using Il2CppInspector.Cpp.UnityHeaders;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Il2CppInspector.Cpp.UnityHeaders;
namespace Il2CppInspector.Cpp
{
@@ -23,7 +19,7 @@ namespace Il2CppInspector.Cpp
public Dictionary<string, CppType> Types { get; }
// 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] :
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
public int WordSize { get; }
private Dictionary<string, ComplexValueType> complexTypeMap = new Dictionary<string, ComplexValueType> {
private Dictionary<string, ComplexValueType> complexTypeMap = new()
{
["struct"] = ComplexValueType.Struct,
["union"] = ComplexValueType.Union,
["enum"] = ComplexValueType.Enum
@@ -44,22 +41,23 @@ namespace Il2CppInspector.Cpp
private string currentGroup = string.Empty;
public void SetGroup(string group) => currentGroup = group;
private static readonly List<CppType> primitiveTypes = new List<CppType> {
new CppType("uint8_t", 8),
new CppType("uint16_t", 16),
new CppType("uint32_t", 32),
new CppType("uint64_t", 64),
new CppType("int8_t", 8),
new CppType("int16_t", 16),
new CppType("int32_t", 32),
new CppType("int64_t", 64),
new CppType("char", 8),
new CppType("int", 32),
new CppType("float", 32),
new CppType("double", 64),
new CppType("bool", 8),
new CppType("void", 0)
};
private static readonly List<CppType> primitiveTypes =
[
new("uint8_t", 8),
new("uint16_t", 16),
new("uint32_t", 32),
new("uint64_t", 64),
new("int8_t", 8),
new("int16_t", 16),
new("int32_t", 32),
new("int64_t", 64),
new("char", 8),
new("int", 32),
new("float", 32),
new("double", 64),
new("bool", 8),
new("void", 0)
];
public CppTypeCollection(int wordSize) {
if (wordSize != 32 && wordSize != 64)
@@ -538,15 +536,18 @@ namespace Il2CppInspector.Cpp
public CppComplexType Struct(string name = "", int alignmentBytes = 0) {
if (!string.IsNullOrEmpty(name) && Types.TryGetValue(name, out var cppType))
return (CppComplexType) cppType;
var type = new CppComplexType(ComplexValueType.Struct) {Name = name, Group = currentGroup, AlignmentBytes = alignmentBytes};
if (!string.IsNullOrEmpty(name))
Add(type);
return type;
}
public CppComplexType Union(string name = "", int alignmentBytes = 0) {
if (!string.IsNullOrEmpty(name) && Types.TryGetValue(name, out var cppType))
return (CppComplexType) cppType;
var type = new CppComplexType(ComplexValueType.Union) {Name = name, Group = currentGroup, AlignmentBytes = alignmentBytes};
if (!string.IsNullOrEmpty(name))
Add(type);
@@ -554,9 +555,13 @@ namespace Il2CppInspector.Cpp
}
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};
if (!string.IsNullOrEmpty(name))
Add(type);
return type;
}
@@ -585,11 +590,17 @@ namespace Il2CppInspector.Cpp
cppTypes.AddFromDeclarationText(apis);
// Don't allow any of the header type names or primitive type names to be re-used
foreach (var type in cppTypes.Types.Values)
declGen?.TypeNamespace.TryReserveName(type.Name);
foreach (var type in cppTypes.Types.Keys)
{
declGen?.TypeNamespace.TryReserveName(type);
declGen?.GlobalsNamespace.TryReserveName(type);
}
foreach (var typedef in cppTypes.TypedefAliases.Values)
declGen?.GlobalsNamespace.TryReserveName(typedef.Name);
foreach (var typedef in cppTypes.TypedefAliases.Keys)
{
declGen?.TypeNamespace.TryReserveName(typedef);
declGen?.GlobalsNamespace.TryReserveName(typedef);
}
cppTypes.SetGroup("");