Skip to content

Commit 661b287

Browse files
committed
Fix bug in delegate conversion processing layer
1 parent e4fe509 commit 661b287

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

Il2CppInterop.Generator/DelegateConversionProcessingLayer.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
3737
for (var typeIndex = 0; typeIndex < assembly.Types.Count; typeIndex++)
3838
{
3939
var type = assembly.Types[typeIndex];
40-
if (type.BaseType is not { Namespace: "Il2CppSystem", Name: "MulticastDelegate" })
40+
if (!type.IsIl2CppDelegate)
4141
continue;
4242

4343
// Remove variance on generic parameters because only interfaces and (real) delegates can have variance.
@@ -137,26 +137,26 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
137137
// We need to create a new delegate type
138138

139139
var name = type.Name is "Delegate" ? "Converted" : "Delegate"; // Name can't be the same as the declaring type
140-
managedDelegateType = type.InjectNestedType(
140+
var injectedType = type.InjectNestedType(
141141
name,
142142
multicastDelegateType);
143143

144-
managedDelegateType.CopyGenericParameters(type, true);
144+
injectedType.CopyGenericParameters(type, true);
145145

146146
TypeAnalysisContext returnType;
147147
List<TypeAnalysisContext> parameterTypes = invokeMethod.Parameters.Select(p => p.ParameterType).ToList();
148148
{
149149
var genericParameterDictionary = Enumerable.Range(0, type.GenericParameters.Count)
150-
.ToDictionary<int, TypeAnalysisContext, TypeAnalysisContext>(i => type.GenericParameters[i], i => managedDelegateType.GenericParameters[i]);
150+
.ToDictionary<int, TypeAnalysisContext, TypeAnalysisContext>(i => type.GenericParameters[i], i => injectedType.GenericParameters[i]);
151151
var replacementVisitor = new TypeReplacementVisitor(genericParameterDictionary);
152152
replacementVisitor.Modify(parameterTypes);
153153
returnType = replacementVisitor.Replace(invokeMethod.ReturnType);
154154
}
155155

156156
// Constructor
157157
{
158-
managedDelegateType.Methods.Add(new InjectedMethodAnalysisContext(
159-
managedDelegateType,
158+
injectedType.Methods.Add(new InjectedMethodAnalysisContext(
159+
injectedType,
160160
".ctor",
161161
appContext.SystemTypes.SystemVoidType,
162162
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
@@ -166,8 +166,8 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
166166

167167
// Invoke
168168
{
169-
managedDelegateType.Methods.Add(new InjectedMethodAnalysisContext(
170-
managedDelegateType,
169+
injectedType.Methods.Add(new InjectedMethodAnalysisContext(
170+
injectedType,
171171
"Invoke",
172172
returnType,
173173
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual,
@@ -177,8 +177,8 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
177177

178178
// BeginInvoke
179179
{
180-
managedDelegateType.Methods.Add(new InjectedMethodAnalysisContext(
181-
managedDelegateType,
180+
injectedType.Methods.Add(new InjectedMethodAnalysisContext(
181+
injectedType,
182182
"BeginInvoke",
183183
iasyncResultType,
184184
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual,
@@ -188,14 +188,23 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
188188

189189
// EndInvoke
190190
{
191-
managedDelegateType.Methods.Add(new InjectedMethodAnalysisContext(
192-
managedDelegateType,
191+
injectedType.Methods.Add(new InjectedMethodAnalysisContext(
192+
injectedType,
193193
"EndInvoke",
194194
returnType,
195195
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual,
196196
[iasyncResultType],
197197
defaultImplAttributes: MethodImplAttributes.Runtime));
198198
}
199+
200+
if (injectedType.GenericParameters.Count > 0)
201+
{
202+
managedDelegateType = injectedType.MakeGenericInstanceType(type.GenericParameters);
203+
}
204+
else
205+
{
206+
managedDelegateType = injectedType;
207+
}
199208
}
200209

201210
var concreteType = type.HasGenericParameters ? type.MakeGenericInstanceType(type.GenericParameters) : type;

Il2CppInterop.Generator/Extensions/TypeAnalysisContextExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public bool IsIl2CppPrimitive
4343
}
4444
}
4545

46+
public bool IsIl2CppDelegate => type.BaseType is { Namespace: "Il2CppSystem", Name: "MulticastDelegate" };
47+
4648
/// <summary>
4749
/// The fields, methods, properties, and events of this type.
4850
/// </summary>

0 commit comments

Comments
 (0)