Skip to content

Commit c34f14d

Browse files
andrews-unityShadauxCatnetcode-ci-service
authored
fix: RPC ILPP generating invalid IL code when using this instead of this ref for the FastBufferReader/FastBufferWriter parameter of an extension method. (Unity-Technologies#1393) (Unity-Technologies#1459)
* fix: RPC ILPP generating invalid IL code when using `this` instead of `this ref` for the FastBufferReader/FastBufferWriter parameter of an extension method. Also updated the test so both `this` and `this ref` are exercised - the documentation says to use `this`, but `this ref` was what worked... so for backward compatibility both have to be supported. * Changelog * standards fix Co-authored-by: Jaedyn Draper <284434+ShadauxCat@users.noreply.github.com> Co-authored-by: Unity Netcode CI <74025435+netcode-ci-service@users.noreply.github.com>
1 parent f9db50e commit c34f14d

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

Editor/CodeGen/NetworkBehaviourILPP.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -579,21 +579,28 @@ private MethodReference GetFastBufferWriterWriteMethod(string name, TypeReferenc
579579
{
580580
if (method.GenericParameters[0].HasConstraints)
581581
{
582+
var meetsConstraints = true;
582583
foreach (var constraint in method.GenericParameters[0].Constraints)
583584
{
584585
var resolvedConstraint = constraint.Resolve();
585586

586587
if (
587588
(resolvedConstraint.IsInterface &&
588-
checkType.HasInterface(resolvedConstraint.FullName))
589+
!checkType.HasInterface(resolvedConstraint.FullName))
589590
|| (resolvedConstraint.IsClass &&
590-
checkType.Resolve().IsSubclassOf(resolvedConstraint.FullName)))
591+
!checkType.Resolve().IsSubclassOf(resolvedConstraint.FullName))
592+
|| (resolvedConstraint.Name == "ValueType" && !checkType.IsValueType))
591593
{
592-
var instanceMethod = new GenericInstanceMethod(method);
593-
instanceMethod.GenericArguments.Add(checkType);
594-
return instanceMethod;
594+
meetsConstraints = false;
595+
break;
595596
}
596597
}
598+
if (meetsConstraints)
599+
{
600+
var instanceMethod = new GenericInstanceMethod(method);
601+
instanceMethod.GenericArguments.Add(checkType);
602+
return instanceMethod;
603+
}
597604
}
598605
}
599606
}
@@ -954,17 +961,23 @@ private void InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomA
954961
// writer.WriteValueSafe(param) for value types, OR
955962
// writer.WriteValueSafe(param, -1, 0) for arrays of value types, OR
956963
// writer.WriteValueSafe(param, false) for strings
957-
instructions.Add(processor.Create(OpCodes.Ldloca, serializerLocIdx));
958964
var method = methodRef.Resolve();
959965
var checkParameter = method.Parameters[0];
960966
var isExtensionMethod = false;
961-
if (checkParameter.ParameterType.Resolve() ==
962-
m_FastBufferWriter_TypeRef.MakeByReferenceType().Resolve())
967+
if (methodRef.Resolve().DeclaringType != m_FastBufferWriter_TypeRef.Resolve())
963968
{
964969
isExtensionMethod = true;
965970
checkParameter = method.Parameters[1];
966971
}
967-
if (checkParameter.IsIn)
972+
if (!isExtensionMethod || method.Parameters[0].ParameterType.IsByReference)
973+
{
974+
instructions.Add(processor.Create(OpCodes.Ldloca, serializerLocIdx));
975+
}
976+
else
977+
{
978+
instructions.Add(processor.Create(OpCodes.Ldloc, serializerLocIdx));
979+
}
980+
if (checkParameter.IsIn || checkParameter.IsOut || checkParameter.ParameterType.IsByReference)
968981
{
969982
instructions.Add(processor.Create(OpCodes.Ldarga, paramIndex + 1));
970983
}
@@ -1275,7 +1288,18 @@ private MethodDefinition GenerateStaticHandler(MethodDefinition methodDefinition
12751288
if (foundMethodRef)
12761289
{
12771290
// reader.ReadValueSafe(out localVar);
1278-
processor.Emit(OpCodes.Ldarga, 1);
1291+
1292+
var checkParameter = methodRef.Resolve().Parameters[0];
1293+
1294+
var isExtensionMethod = methodRef.Resolve().DeclaringType != m_FastBufferReader_TypeRef.Resolve();
1295+
if (!isExtensionMethod || checkParameter.ParameterType.IsByReference)
1296+
{
1297+
processor.Emit(OpCodes.Ldarga, 1);
1298+
}
1299+
else
1300+
{
1301+
processor.Emit(OpCodes.Ldarg, 1);
1302+
}
12791303
processor.Emit(OpCodes.Ldloca, localIndex);
12801304
if (paramType == typeSystem.String)
12811305
{

0 commit comments

Comments
 (0)