// when localMethod is null, it means serialization is happening within a custom-serialized class private bool AddDeserializeCode(MethodDefinition method, string paramName, Type paramType, MethodInfo localMethod) { Type elementType = paramType; if (elementType.IsEnum) { elementType = Enum.GetUnderlyingType(elementType); } // get a method out of NetworkWriter that can serialize the parameter type MethodInfo typeDeserializer = NetworkScopeUtility.GetNetworkReaderDeserializer(elementType); // if this type is an array, do some array magic if (elementType.IsArray) { string arrCount = string.Format("{0}_count", paramName); string arrIndex = string.Format("{0}_index", paramName); // read count method.instructions.AddInstruction("Int32 {0} = reader.ReadInt32();", arrCount); // define variable if deserializing local method if (localMethod != null) { method.instructions.AddInstruction("{0}[] {1} = new {0}[{2}];", elementType.GetElementType(), paramName, arrCount); } method.instructions.AddInstruction("for (int {0} = 0; {0} < {1}; {0}++)", arrIndex, arrCount); return(AddDeserializeCode(method, string.Format("{0}[{1}]", paramName, arrIndex), elementType.GetElementType(), null)); } // write out the NetworkWriter serializer call if (typeDeserializer != null) { // create new variable if specified // string createVarModifier = (localMethod != null) ? string.Format("{0} ", paramType) : string.Empty; bool cast = paramType != elementType; // call the reader function and assign the value method.instructions.AddMethodCallWithAssignment(paramName, paramType, "reader", cast, typeDeserializer, localMethod != null); return(true); } // if type can't be serialized by NetworkWriter directly, see if the type has a serializer/deserializer method if (typeDeserializer == null) { typeDeserializer = NetworkScopeUtility.GetCustomTypeDeserializer(elementType); } // define variable if we're in the local scope if (localMethod != null) { method.instructions.AddInstruction("{0} {1} = new {0}();", elementType, paramName); } if (typeDeserializer != null) { // call the static NetworkSerialize method method.instructions.AddMethodCall(elementType.Name, typeDeserializer, paramName, "reader"); return(true); } // attempt to create static serializer if attributed with NetworkSerialization ClassDefinition autoGeneratedSerializer = CreateStaticSerializerClass(paramType); if (autoGeneratedSerializer != null) { method.instructions.AddInstruction("NetworkSerializer.{0}.NetworkDeserialize({1}, reader);", autoGeneratedSerializer.Name, paramName); return(true); } // can't find serializer? exit out UnityEngine.Debug.LogWarningFormat("Could not find a deserializer for the type '{0}'", elementType.FullName); return(false); }
private bool AddSerializeCode(MethodDefinition method, string paramName, Type paramType) { Type elementType = paramType; if (elementType.IsEnum) { elementType = Enum.GetUnderlyingType(elementType); } // get a method out of NetworkWriter that can serialize the parameter type MethodInfo typeSerializer = NetworkScopeUtility.GetNetworkWriterSerializer(elementType); // if this type is an array, do some array magic if (elementType.IsArray) { // write array count method.instructions.AddInstruction("writer.Write({0}.Length);", paramName); string arrCounterName = "_arrCounter"; method.instructions.AddInstruction("for (int {0} = 0; {0} < {1}.Length; {0}++)", arrCounterName, paramName); return(AddSerializeCode(method, string.Format("{0}[{1}]", paramName, arrCounterName), elementType.GetElementType())); } // write out the NetworkWriter serializer call if (typeSerializer != null) { if (elementType != paramType) { method.instructions.AddMethodCall("writer", typeSerializer, string.Format("({0}){1}", elementType, paramName)); } else { method.instructions.AddMethodCall("writer", typeSerializer, paramName); } return(true); } // if type can't be serialized by NetworkWriter directly, see if the type has a serializer/deserializer method if (typeSerializer == null) { typeSerializer = NetworkScopeUtility.GetCustomTypeSerializer(elementType); } if (typeSerializer != null) { // call the static NetworkSerialize method method.instructions.AddMethodCall(elementType.Name, typeSerializer, paramName, "writer"); return(true); } // attempt to create static serializer if attributed with NetworkSerialization ClassDefinition autoGeneratedSerializer = CreateStaticSerializerClass(paramType); if (autoGeneratedSerializer != null) { method.instructions.AddInstruction("NetworkSerializer.{0}.NetworkSerialize({1}, writer);", autoGeneratedSerializer.Name, paramName); return(true); } // can't find serializer? exit out UnityEngine.Debug.LogWarningFormat("Could not find a serializer for the type '{0}'", elementType.FullName); return(false); }