InvalidProgramException on RPCs when using serialization extensions
See original GitHub issueDescribe the bug
Types serialized via ReadValueSafe and WriteValueSafe cannot be used in RPCs. InvalidProgramException
is thrown.
To Reproduce Steps to reproduce the behavior:
- Create a custom type to serialize (class/struct).
- Create custom type serialization extensions ReadValueSafe and WriteValueSafe for that type, placed in a static class SerializationExtensions as in documentation examples.
- Create Server/Client RPCs that pass our custom type as one of the parameters.
- At runtime, call the RPC.
Actual outcome
When RPC is called Unity throws the following exception (in my case):
InvalidProgramException: Invalid IL code in Code.Eclipse.ItemSystem.Player.PlayerDetails:DropItemServerRpc (Code.Eclipse.ItemSystem.EclipseItemSystem.ItemBase): IL_00dd: call 0x06000b65
RPC is not sent.
Expected outcome The client and server should be able to send RPC containing custom type successfully,
Environment:
- OS: Windows 10
- Unity Version: 2021.2.0b16
- Netcode Version: v1.0.0-pre.2
Additional context I use this serialization method for various purposes like replicating items via factory method pattern (classes), sending player-data (struct), or passing player object context. None of these can be passed via RPCs. Passing them via a named messaging system works as expected. Passing INetworkSerializable types in RPCs works as expected.
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (3 by maintainers)
Top GitHub Comments
@Taenebron Thank you for the sample code. I have managed to reproduce it and am working on a fix.
In the mean time, you can get around the issue by making the first parameter
this ref
instead ofthis
.So any struct that’s
unmanaged
can be serialized automatically via memcpy. This is what I was worried was happening for you… because your struct includes astring
, it shouldn’t be consideredunmanaged
. So the fact that this value is accepted is also a bug. I’ll have to look at the code generation to see why it’s thinking that struct isunmanaged
- I have a strong suspicion the string you get on the other end is probably not valid.