/// <summary> /// Register serialization & deserialization mechanisms for transmitting the given class by ID. /// The given class does not need to implements its own serialization logic. /// </summary> public static void RegisterSerializerAndDeserializer(Type type) { int id = NetworkableId <Type> .ToId(type); Debug.Log("Using id " + id + " for tagging of class " + type.Name + " in network messages when sending/receiving NetworkableById types"); // Construct SerializerAndDeserializer<type> Type serializerAndDeserializerType = typeof(SerializerAndDeserializer <>); Type serializerAndDeserializerTypeSpecialized = serializerAndDeserializerType.MakeGenericType(type); // Find a method Serialize(StreamBuffer, object) in SerializerAndDeserializer<type> MethodInfo serializeMethodInfo = serializerAndDeserializerTypeSpecialized.GetMethod("Serialize", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public, null, new Type[] { typeof(StreamBuffer), typeof(object) }, null); Assert.IsNotNull(serializeMethodInfo, "Unable to find virtual Serialize(StreamBuffer, object) method in " + serializerAndDeserializerTypeSpecialized.Name); SerializeStreamMethod serializer = (SerializeStreamMethod)Delegate.CreateDelegate(typeof(SerializeStreamMethod), serializeMethodInfo); // Find a method Deserialize(StreamBuffer, short) in SerializerAndDeserializer<type> MethodInfo deserializeMethodInfo = serializerAndDeserializerTypeSpecialized.GetMethod("Deserialize", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public, null, new Type[] { typeof(StreamBuffer), typeof(short) }, null); Assert.IsNotNull(serializeMethodInfo, "Unable to find static Deserialize(StreamBuffer, short) method in " + serializerAndDeserializerTypeSpecialized.Name); DeserializeStreamMethod deserializer = (DeserializeStreamMethod)Delegate.CreateDelegate(typeof(DeserializeStreamMethod), deserializeMethodInfo); // Register serializer and deserializer methods for class bool success = PhotonPeer.RegisterType(type, (byte)id, serializer, deserializer); Assert.IsTrue(success, "Failed registering new serialization type for " + type.Name + " with code " + id); }
public void DeregisterSerializer(Type type) { // Photon 1.80 lacks a Photon.DeregisterType() function. This undoes the actions of Photon.RegisterType() as implemented in Photon 1.80. byte typeId = (byte)NetworkableId <Type> .ToId(type); // Invoke Protocol.CodeDict.Remove(typeId) FieldInfo codeDictField = typeof(Protocol).GetField("CodeDict", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); Assert.IsNotNull(codeDictField, "Photon internals have changed; Protocol.CodeDict no longer exists"); object codeDict = codeDictField.GetValue(null); Assert.IsNotNull(codeDict, "Photon internals have changed; Protocol.CodeDict no longer exists"); MethodInfo codeDictRemoveMethodInfo = codeDict.GetType().GetMethod("Remove"); Assert.IsNotNull(codeDictRemoveMethodInfo, "Photon internals have changed; Protocol.CodeDict is no longer a Dictionary"); codeDictRemoveMethodInfo.Invoke(codeDict, new object[] { (byte)typeId }); // Invoke Protocol.TypeDict.Remove(typeId) FieldInfo typeDictField = typeof(Protocol).GetField("TypeDict", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); Assert.IsNotNull(typeDictField, "Photon internals have changed; Protocol.TypeDict no longer exists"); object typeDict = typeDictField.GetValue(null); Assert.IsNotNull(typeDict, "Photon internals have changed; Protocol.TypeDict no longer exists"); MethodInfo typeDictRemoveMethodInfo = typeDict.GetType().GetMethod("Remove"); Assert.IsNotNull(typeDictRemoveMethodInfo, "Photon internals have changed; Protocol.TypeDict is no longer a Dictionary"); typeDictRemoveMethodInfo.Invoke(typeDict, new object[] { type }); }
/// <summary> /// Register serialization & deserialization mechanisms for transmitting an abstract base class by value. /// The given class does not need to implements its own serialization logic. /// </summary> public static void RegisterSerializerAndDeserializer(Type type) { int id = NetworkableId <Type> .ToId(type); Debug.Log("Using id " + id + " for tagging of class " + type.Name + " in network messages when sending/receiving NetworkableByValue types"); bool success = PhotonPeer.RegisterType(type, (byte)id, SerializerAndDeserializer.Serialize, SerializerAndDeserializer.Deserialize); Assert.IsTrue(success, "Failed registering new serialization type for " + type.Name + " with code " + id); }
public static short Serialize(StreamBuffer outBuffer, object customObject) { int length = 0; T obj = (T)customObject; int id = NetworkableId <T> .ToId(obj); Protocol.Serialize(id, serializationBuffer, ref length); outBuffer.Write(serializationBuffer, 0, length); Debug.Log("Serializing object of type " + typeof(T).Name + " with id " + id); return((short)length); }