Skip to content

Commit 8786238

Browse files
committed
Handle generic constraints and method overrides in relinker
1 parent 87074a6 commit 8786238

1 file changed

Lines changed: 39 additions & 30 deletions

File tree

ModFramework/Relinker/TypeRelinker.cs

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,14 @@ bool CheckType<TRef>(TRef type, Action<TRef>? update = null)
101101
}
102102
else
103103
{
104-
// TODO determine if this is needed anymore. causes recursion issues but i dont see evidence its needed anymore
105-
//if (type.HasGenericParameters)
106-
// for (int i = 0; i < type.GenericParameters.Count; i++)
107-
// {
108-
// changed |= CheckType(
109-
// type.GenericParameters[i],
110-
// nr => type.GenericParameters[i] = nr
111-
// );
112-
// }
104+
foreach (var gpm in type.GenericParameters)
105+
{
106+
foreach (var ct in gpm.Constraints)
107+
{
108+
FixAttributes(ct.CustomAttributes);
109+
changed |= CheckType(ct.ConstraintType, ntype => ct.ConstraintType = ntype);
110+
}
111+
}
113112

114113
changed |= RelinkType(ref type);
115114
}
@@ -125,34 +124,39 @@ bool CheckType<TRef>(TRef type, Action<TRef>? update = null)
125124
return changed;
126125
}
127126

128-
public abstract bool RelinkType<TRef>(ref TRef typeReference) where TRef : TypeReference;
129-
130-
public void Relink(Instruction instr)
127+
private void CheckMethodRef(MethodReference mref)
131128
{
132-
if (instr.Operand is MethodReference mref)
129+
if (mref is GenericInstanceMethod gim)
133130
{
134-
if (mref is GenericInstanceMethod gim)
135-
{
136-
CheckType(gim.ElementMethod.DeclaringType, nt => gim.ElementMethod.DeclaringType = nt);
131+
CheckType(gim.ElementMethod.DeclaringType, nt => gim.ElementMethod.DeclaringType = nt);
137132

138-
for (var x = 0; x < gim.GenericArguments.Count; x++)
139-
{
140-
CheckType(gim.GenericArguments[x], nt => gim.GenericArguments[x] = nt);
133+
for (var x = 0; x < gim.GenericArguments.Count; x++)
134+
{
135+
CheckType(gim.GenericArguments[x], nt => gim.GenericArguments[x] = nt);
141136

142-
if (gim.GenericArguments[x].DeclaringType is not null)
143-
CheckType(gim.GenericArguments[x].DeclaringType, nt => gim.GenericArguments[x].DeclaringType = nt);
144-
}
137+
if (gim.GenericArguments[x].DeclaringType is not null)
138+
CheckType(gim.GenericArguments[x].DeclaringType, nt => gim.GenericArguments[x].DeclaringType = nt);
145139
}
146-
else
147-
CheckType(mref.DeclaringType, nt => mref.DeclaringType = nt);
140+
}
141+
else
142+
CheckType(mref.DeclaringType, nt => mref.DeclaringType = nt);
148143

149-
CheckType(mref.ReturnType, nt => mref.ReturnType = nt);
144+
CheckType(mref.ReturnType, nt => mref.ReturnType = nt);
150145

151-
foreach (var prm in mref.Parameters)
152-
{
153-
CheckType(prm.ParameterType, nt => prm.ParameterType = nt);
154-
FixAttributes(prm.CustomAttributes);
155-
}
146+
foreach (var prm in mref.Parameters)
147+
{
148+
CheckType(prm.ParameterType, nt => prm.ParameterType = nt);
149+
FixAttributes(prm.CustomAttributes);
150+
}
151+
}
152+
153+
public abstract bool RelinkType<TRef>(ref TRef typeReference) where TRef : TypeReference;
154+
155+
public void Relink(Instruction instr)
156+
{
157+
if (instr.Operand is MethodReference mref)
158+
{
159+
CheckMethodRef(mref);
156160
}
157161
else if (instr.Operand is FieldReference fref)
158162
{
@@ -298,6 +302,11 @@ public override void Relink(MethodDefinition method)
298302
FixAttributes(prm.CustomAttributes);
299303
}
300304

305+
foreach (var ovrd in method.Overrides)
306+
{
307+
CheckMethodRef(ovrd);
308+
}
309+
301310
FixAttributes(method.CustomAttributes);
302311

303312
CheckType(method.MethodReturnType.ReturnType, nt => method.MethodReturnType.ReturnType = nt);

0 commit comments

Comments
 (0)