@@ -17,6 +17,10 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
1717 var iil2CppType = appContext . ResolveTypeOrThrow ( typeof ( IIl2CppType ) ) ;
1818 var iil2CppType_get_ObjectClass = iil2CppType . GetMethodByName ( $ "get_{ nameof ( IIl2CppType . ObjectClass ) } ") ;
1919
20+ var iil2CppValueType = appContext . ResolveTypeOrThrow ( typeof ( IIl2CppValueType ) ) ;
21+ var iil2CppValueType_get_Size = iil2CppValueType . GetMethodByName ( $ "get_{ nameof ( IIl2CppValueType . Size ) } ") ;
22+ var iil2CppValueType_WriteToSpan = iil2CppValueType . GetMethodByName ( nameof ( IIl2CppValueType . WriteToSpan ) ) ;
23+
2024 var iil2CppTypeGeneric = appContext . ResolveTypeOrThrow ( typeof ( IIl2CppType < > ) ) ;
2125 var iil2CppTypeGeneric_get_Size = iil2CppTypeGeneric . GetMethodByName ( $ "get_{ nameof ( IIl2CppType < > . Size ) } ") ;
2226 var iil2CppTypeGeneric_get_AssemblyName = iil2CppTypeGeneric . GetMethodByName ( $ "get_{ nameof ( IIl2CppType < > . AssemblyName ) } ") ;
@@ -35,6 +39,7 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
3539 var il2CppTypeHelper_WriteToSpanAtOffset = il2CppTypeHelper . GetMethodByName ( nameof ( Il2CppTypeHelper . WriteToSpanAtOffset ) ) ;
3640 var il2CppTypeHelper_ReadFromSpanBlittable = il2CppTypeHelper . GetMethodByName ( nameof ( Il2CppTypeHelper . ReadFromSpanBlittable ) ) ;
3741 var il2CppTypeHelper_WriteToSpanBlittable = il2CppTypeHelper . GetMethodByName ( nameof ( Il2CppTypeHelper . WriteToSpanBlittable ) ) ;
42+ var il2CppTypeHelper_WriteToSpan = il2CppTypeHelper . GetMethodByName ( nameof ( Il2CppTypeHelper . WriteToSpan ) ) ;
3843
3944 var intPtr_get_Size = appContext . SystemTypes . SystemIntPtrType . GetMethodByName ( $ "get_{ nameof ( IntPtr . Size ) } ") ;
4045
@@ -58,6 +63,11 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
5863 var instantiatedIl2CppTypeGeneric = iil2CppTypeGeneric . MakeGenericInstanceType ( [ instantiatedType ] ) ;
5964 type . InterfaceContexts . Add ( instantiatedIl2CppTypeGeneric ) ;
6065
66+ if ( type . IsValueType )
67+ {
68+ type . InterfaceContexts . Add ( iil2CppValueType ) ;
69+ }
70+
6171 TypeAnalysisContext nameReferenceType ;
6272 TypeAnalysisContext classReferenceType ;
6373 if ( type == il2CppSystemIObject )
@@ -161,6 +171,46 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
161171 IsInjected = true ,
162172 } ;
163173 type . Properties . Add ( property ) ;
174+
175+ // IIl2CppValueType.Size
176+ if ( type . IsValueType )
177+ {
178+ Debug . Assert ( instantiatedSizeStorage is not null ) ;
179+ var valueTypeMethodName = $ "{ iil2CppValueType . FullName } .get_{ nameof ( IIl2CppValueType . Size ) } ";
180+ var valueTypeMethod = new InjectedMethodAnalysisContext (
181+ type ,
182+ valueTypeMethodName ,
183+ iil2CppValueType_get_Size . ReturnType ,
184+ MethodAttributes . Private | MethodAttributes . HideBySig | MethodAttributes . Virtual | MethodAttributes . Final | MethodAttributes . SpecialName | MethodAttributes . NewSlot ,
185+ [ ] )
186+ {
187+ IsInjected = true ,
188+ } ;
189+ type . Methods . Add ( valueTypeMethod ) ;
190+ valueTypeMethod . Overrides . Add ( iil2CppValueType_get_Size ) ;
191+
192+ valueTypeMethod . PutExtraData ( new NativeMethodBody ( )
193+ {
194+ Instructions =
195+ [
196+ new Instruction ( CilOpCodes . Ldsfld , instantiatedSizeStorage ) ,
197+ new Instruction ( CilOpCodes . Ret ) ,
198+ ] ,
199+ } ) ;
200+
201+ var valueTypePropertyName = $ "{ iil2CppValueType . FullName } .{ nameof ( IIl2CppValueType . Size ) } ";
202+ var valueTypeProperty = new InjectedPropertyAnalysisContext (
203+ valueTypePropertyName ,
204+ valueTypeMethod . ReturnType ,
205+ valueTypeMethod ,
206+ null ,
207+ PropertyAttributes . None ,
208+ type )
209+ {
210+ IsInjected = true ,
211+ } ;
212+ type . Properties . Add ( valueTypeProperty ) ;
213+ }
164214 }
165215
166216 // AssemblyName
@@ -457,6 +507,34 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
457507 } ) ;
458508 }
459509 }
510+ // IIl2CppValueType.WriteToSpan
511+ if ( type . IsValueType )
512+ {
513+ var methodName = $ "{ iil2CppValueType . FullName } .{ nameof ( IIl2CppValueType . WriteToSpan ) } ";
514+ var method = new InjectedMethodAnalysisContext (
515+ type ,
516+ methodName ,
517+ iil2CppValueType_WriteToSpan . ReturnType ,
518+ MethodAttributes . Private | MethodAttributes . HideBySig | MethodAttributes . Virtual | MethodAttributes . Final | MethodAttributes . NewSlot ,
519+ iil2CppValueType_WriteToSpan . Parameters . Select ( p => p . ParameterType ) . ToArray ( ) )
520+ {
521+ IsInjected = true ,
522+ } ;
523+ type . Methods . Add ( method ) ;
524+ method . Overrides . Add ( iil2CppValueType_WriteToSpan ) ;
525+
526+ method . PutExtraData ( new NativeMethodBody ( )
527+ {
528+ Instructions =
529+ [
530+ new Instruction ( CilOpCodes . Ldarg_0 ) ,
531+ new Instruction ( CilOpCodes . Ldobj , instantiatedType ) ,
532+ new Instruction ( CilOpCodes . Ldarg_1 ) ,
533+ new Instruction ( CilOpCodes . Call , il2CppTypeHelper_WriteToSpan . MakeGenericInstanceMethod ( instantiatedType ) ) ,
534+ new Instruction ( CilOpCodes . Ret ) ,
535+ ] ,
536+ } ) ;
537+ }
460538 }
461539 }
462540 }
0 commit comments