MirageNet / Mirage

Easy to use high performance Network library for Unity 3d
https://miragenet.github.io/Mirage/
MIT License
515 stars 65 forks source link

Odin serializer conflicts with the network behavior implementation #1117

Closed lucasmontec closed 1 year ago

lucasmontec commented 1 year ago

Describe the bug When using Odin serialization together with the network behavior, the Odin serializer is causing the method RPC names to be serialized, or saved. If you change the method signature or just the name, the changes are not reflected on builds or on the editor. The RemoteCallCollection is probably being serialized

To Reproduce Steps to reproduce the behavior:

  1. Create a simple project with mirage and the Odin Inspector plugin
  2. Create a SerializedNetworkBehavior class to implement Odin serialization like so:

    [ShowOdinSerializedPropertiesInInspector]
    public class SerializedNetworkBehaviour : NetworkBehaviour, ISerializationCallbackReceiver, ISupportsPrefabSerialization
    {
        [SerializeField, HideInInspector]
        private SerializationData serializationData;
    
        SerializationData ISupportsPrefabSerialization.SerializationData 
        { 
            get => serializationData;
            set => serializationData = value;
        }
    
        void ISerializationCallbackReceiver.OnAfterDeserialize()
        {
            UnitySerializationUtility.DeserializeUnityObject(this, ref serializationData);
        }
    
        void ISerializationCallbackReceiver.OnBeforeSerialize()
        {
            UnitySerializationUtility.SerializeUnityObject(this, ref serializationData);
        }
    }
  3. Create a test class that has an RPC method (I reproduced with server rpc):
    public class TestClass : SerializedNetworkBehaviour
    {
        [ServerRpc]
        public void SRPCTest()
        {
            //something
        }
    }
  4. Run the project once and call that RPC.
  5. Rename the method and rebuild, call the RPC again.
  6. On the second call, an exception will be raised. The RPC is going to be stored with the old name due to Odin's serialization.

Expected behavior The remote method collection should be created again and the method should be there.

Desktop (please complete the following information):

Additional context This problem seems rare but very hard to debug. Since this deals with serialization, other serializers could cause the same issue. From what I have gathered, it is Odin's fault here, still the constructor usage to full that remote call collection, and the absence of a NonSerialized attribute, both contribute to this issue.

lucasmontec commented 1 year ago

The issue persists even if you remove the serialized class, use the normal NetworkBehavior and then return to the serialized class. It seems unity is using the same serialization thus causing the issue to return after you change back to SerializedNetworkBehavior.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 120 days with no activity. Remove stale label or comment or this will be closed in 14 days.