55using System . Linq ;
66using AsmResolver . DotNet ;
77using AsmResolver . DotNet . Signatures ;
8- using LibCpp2IL ;
98using LibCpp2IL . BinaryStructures ;
109
1110namespace Cpp2IL . Core . Utils . AsmResolver ;
@@ -49,142 +48,6 @@ public static TypeDefinition GetPrimitiveTypeDef(Il2CppTypeEnum type) =>
4948 _ => throw new ArgumentException ( $ "Type is not a primitive - { type } ", nameof ( type ) )
5049 } ;
5150
52- public static TypeSignature GetTypeSignatureFromIl2CppType ( ModuleDefinition module , Il2CppType il2CppType )
53- {
54- //Module is needed for generic params
55- if ( il2CppType == null )
56- throw new ArgumentNullException ( nameof ( il2CppType ) ) ;
57-
58- TypeSignature ret ;
59- switch ( il2CppType . Type )
60- {
61- case Il2CppTypeEnum . IL2CPP_TYPE_OBJECT :
62- case Il2CppTypeEnum . IL2CPP_TYPE_VOID :
63- case Il2CppTypeEnum . IL2CPP_TYPE_BOOLEAN :
64- case Il2CppTypeEnum . IL2CPP_TYPE_CHAR :
65- case Il2CppTypeEnum . IL2CPP_TYPE_I1 :
66- case Il2CppTypeEnum . IL2CPP_TYPE_U1 :
67- case Il2CppTypeEnum . IL2CPP_TYPE_I2 :
68- case Il2CppTypeEnum . IL2CPP_TYPE_U2 :
69- case Il2CppTypeEnum . IL2CPP_TYPE_I4 :
70- case Il2CppTypeEnum . IL2CPP_TYPE_U4 :
71- case Il2CppTypeEnum . IL2CPP_TYPE_I :
72- case Il2CppTypeEnum . IL2CPP_TYPE_U :
73- case Il2CppTypeEnum . IL2CPP_TYPE_I8 :
74- case Il2CppTypeEnum . IL2CPP_TYPE_U8 :
75- case Il2CppTypeEnum . IL2CPP_TYPE_R4 :
76- case Il2CppTypeEnum . IL2CPP_TYPE_R8 :
77- case Il2CppTypeEnum . IL2CPP_TYPE_STRING :
78- case Il2CppTypeEnum . IL2CPP_TYPE_TYPEDBYREF :
79- ret = module . DefaultImporter . ImportType ( GetPrimitiveTypeDef ( il2CppType . Type ) )
80- . ToTypeSignature ( ) ;
81- break ;
82- case Il2CppTypeEnum . IL2CPP_TYPE_CLASS :
83- case Il2CppTypeEnum . IL2CPP_TYPE_VALUETYPE :
84- ret = module . DefaultImporter . ImportType ( TypeDefsByIndex [ il2CppType . Data . ClassIndex ] )
85- . ToTypeSignature ( ) ;
86- break ;
87- case Il2CppTypeEnum . IL2CPP_TYPE_ARRAY :
88- ret = GetTypeSignatureFromIl2CppType ( module , il2CppType . GetArrayElementType ( ) )
89- . MakeArrayTypeWithLowerBounds ( il2CppType . GetArrayRank ( ) ) ;
90- break ;
91- case Il2CppTypeEnum . IL2CPP_TYPE_SZARRAY :
92- ret = GetTypeSignatureFromIl2CppType ( module , il2CppType . GetEncapsulatedType ( ) )
93- . MakeSzArrayType ( ) ;
94- break ;
95- case Il2CppTypeEnum . IL2CPP_TYPE_PTR :
96- ret = GetTypeSignatureFromIl2CppType ( module , il2CppType . GetEncapsulatedType ( ) )
97- . MakePointerType ( ) ;
98- break ;
99- case Il2CppTypeEnum . IL2CPP_TYPE_VAR : //Generic type parameter
100- case Il2CppTypeEnum . IL2CPP_TYPE_MVAR : //Generic method parameter
101- var method = il2CppType . Type == Il2CppTypeEnum . IL2CPP_TYPE_MVAR ;
102- ret = new GenericParameterSignature ( module , method ? GenericParameterType . Method : GenericParameterType . Type , il2CppType . GetGenericParameterDef ( ) . genericParameterIndexInOwner ) ;
103- break ;
104- case Il2CppTypeEnum . IL2CPP_TYPE_GENERICINST :
105- {
106- var genericClass = il2CppType . GetGenericClass ( ) ;
107-
108- //Get base type
109- TypeDefsByIndex . TryGetValue ( genericClass . TypeDefinitionIndex , out var typeDefinition ) ;
110- if ( Cpp2IlApi . CurrentAppContext ! . MetadataVersion >= 27f ) //TODO: we should pass in the app context to this method
111- {
112- //V27 - type indexes are pointers now.
113- var type = LibCpp2IlMain . Binary ! . ReadReadableAtVirtualAddress < Il2CppType > ( ( ulong ) genericClass . TypeDefinitionIndex ) ;
114- typeDefinition = GetTypeSignatureFromIl2CppType ( module , type ) . Resolve ( ) ?? throw new Exception ( "Unable to resolve base type for generic inst" ) ;
115- }
116-
117- var genericInstanceType = new GenericInstanceTypeSignature ( module . DefaultImporter . ImportType ( typeDefinition ! ) , typeDefinition ! . IsValueType ) ;
118-
119- //Get generic arguments
120- var genericArgumentTypes = genericClass . Context . ClassInst . Types ;
121-
122- //Add arguments to generic instance
123- foreach ( var type in genericArgumentTypes )
124- genericInstanceType . TypeArguments . Add ( GetTypeSignatureFromIl2CppType ( module , type ) ) ;
125-
126- ret = genericInstanceType ;
127- break ;
128- }
129- default :
130- throw new ( "Don't know how to make a type signature from " + il2CppType . Type ) ;
131- }
132-
133- if ( il2CppType . Byref == 1 )
134- ret = ret . MakeByReferenceType ( ) ;
135-
136- return ret ;
137- }
138-
139- /// <summary>
140- /// Imports the managed representation of the given il2cpp type using the given importer, and returns said type.
141- /// <br/><br/>
142- /// Prefer <see cref="GetTypeSignatureFromIl2CppType"/> where possible, only use this where an actual type reference is needed.
143- /// Such cases would include generic parameter constraints, base types/interfaces, and event types.
144- /// </summary>
145- public static ITypeDefOrRef ImportReferenceFromIl2CppType ( ModuleDefinition module , Il2CppType il2CppType )
146- {
147- if ( il2CppType == null )
148- throw new ArgumentNullException ( nameof ( il2CppType ) ) ;
149-
150- switch ( il2CppType . Type )
151- {
152- case Il2CppTypeEnum . IL2CPP_TYPE_OBJECT :
153- case Il2CppTypeEnum . IL2CPP_TYPE_VOID :
154- case Il2CppTypeEnum . IL2CPP_TYPE_BOOLEAN :
155- case Il2CppTypeEnum . IL2CPP_TYPE_CHAR :
156- case Il2CppTypeEnum . IL2CPP_TYPE_I1 :
157- case Il2CppTypeEnum . IL2CPP_TYPE_U1 :
158- case Il2CppTypeEnum . IL2CPP_TYPE_I2 :
159- case Il2CppTypeEnum . IL2CPP_TYPE_U2 :
160- case Il2CppTypeEnum . IL2CPP_TYPE_I4 :
161- case Il2CppTypeEnum . IL2CPP_TYPE_U4 :
162- case Il2CppTypeEnum . IL2CPP_TYPE_I :
163- case Il2CppTypeEnum . IL2CPP_TYPE_U :
164- case Il2CppTypeEnum . IL2CPP_TYPE_I8 :
165- case Il2CppTypeEnum . IL2CPP_TYPE_U8 :
166- case Il2CppTypeEnum . IL2CPP_TYPE_R4 :
167- case Il2CppTypeEnum . IL2CPP_TYPE_R8 :
168- case Il2CppTypeEnum . IL2CPP_TYPE_STRING :
169- case Il2CppTypeEnum . IL2CPP_TYPE_TYPEDBYREF :
170- //This case, and the one below, are faster to go this way rather than delegating to type signature creation, because we can go straight from def -> ref.
171- return module . DefaultImporter . ImportType ( GetPrimitiveTypeDef ( il2CppType . Type ) ) ;
172- case Il2CppTypeEnum . IL2CPP_TYPE_CLASS :
173- case Il2CppTypeEnum . IL2CPP_TYPE_VALUETYPE :
174- return module . DefaultImporter . ImportType ( TypeDefsByIndex [ il2CppType . Data . ClassIndex ] ) ;
175- case Il2CppTypeEnum . IL2CPP_TYPE_ARRAY :
176- case Il2CppTypeEnum . IL2CPP_TYPE_GENERICINST :
177- case Il2CppTypeEnum . IL2CPP_TYPE_PTR :
178- case Il2CppTypeEnum . IL2CPP_TYPE_SZARRAY :
179- case Il2CppTypeEnum . IL2CPP_TYPE_VAR :
180- case Il2CppTypeEnum . IL2CPP_TYPE_MVAR :
181- //For the rest of these, we have to make a type signature first anyway, so just delegate to signature getter
182- return GetTypeSignatureFromIl2CppType ( module , il2CppType ) . ToTypeDefOrRef ( ) ;
183- default :
184- throw new ( "Don't know how to import a type reference from an il2cpp type of type " + il2CppType . Type ) ;
185- }
186- }
187-
18851 public static TypeDefinition ? TryLookupTypeDefKnownNotGeneric ( string ? name )
18952 {
19053 if ( name == null )
@@ -198,7 +61,7 @@ public static ITypeDefOrRef ImportReferenceFromIl2CppType(ModuleDefinition modul
19861 if ( CachedTypeDefsByName . TryGetValue ( key , out var ret ) )
19962 return ret ;
20063
201- var definedType = Cpp2IlApi . CurrentAppContext ! . AllTypes . FirstOrDefault ( t => t . Definition != null && string . Equals ( t . Definition . FullName , name , StringComparison . OrdinalIgnoreCase ) ) ;
64+ var definedType = Cpp2IlApi . CurrentAppContext ! . AllTypes . FirstOrDefault ( t => string . Equals ( t . FullName , name , StringComparison . OrdinalIgnoreCase ) ) ;
20265
20366 //Try subclasses
20467 definedType ??= Cpp2IlApi . CurrentAppContext . AllTypes . FirstOrDefault ( t =>
0 commit comments