Exemplo n.º 1
0
 /// <summary>
 /// Deserializes a value of a given type from the given byte array in the given format.
 /// </summary>
 /// <typeparam name="T">The type to deserialize.</typeparam>
 /// <param name="bytes">The bytes to deserialize from.</param>
 /// <param name="format">The format to read.</param>
 /// <param name="context">The context to use.</param>
 /// <returns>
 /// The deserialized value.
 /// </returns>
 public static T DeserializeValue <T>(byte[] bytes, DataFormat format, DeserializationContext context = null)
 {
     using (var stream = CachedMemoryStream.Claim(bytes))
     {
         return(DeserializeValue <T>(stream.Value.MemoryStream, format, context));
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Deserializes a value of a given type from the given byte array in the given format, using the given list of Unity objects for external index reference resolution.
 /// </summary>
 /// <typeparam name="T">The type to deserialize.</typeparam>
 /// <param name="bytes">The bytes to deserialize from.</param>
 /// <param name="format">The format to read.</param>
 /// <param name="referencedUnityObjects">The list of Unity objects to use for external index reference resolution.</param>
 /// <param name="context">The context to use.</param>
 /// <returns>
 /// The deserialized value.
 /// </returns>
 public static T DeserializeValue <T>(byte[] bytes, DataFormat format, List <UnityEngine.Object> referencedUnityObjects, DeserializationContext context = null)
 {
     using (var stream = CachedMemoryStream.Claim(bytes))
     {
         return(DeserializeValue <T>(stream.Value.MemoryStream, format, referencedUnityObjects, context));
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Deserializes a value from the given byte array in the given format. This might fail with primitive values, as they don't come with type metadata.
 /// </summary>
 /// <param name="bytes">The bytes to deserialize from.</param>
 /// <param name="format">The format to read.</param>
 /// <returns>
 /// The deserialized value.
 /// </returns>
 public static object DeserializeValueWeak(byte[] bytes, DataFormat format)
 {
     using (var stream = CachedMemoryStream.Claim(bytes))
     {
         return(DeserializeValueWeak(stream.Value.MemoryStream, format));
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Deserializes a value from the given byte array in the given format, using the given list of Unity objects for external index reference resolution. This might fail with primitive values, as they don't come with type metadata.
 /// </summary>
 /// <param name="bytes">The bytes to deserialize from.</param>
 /// <param name="format">The format to read.</param>
 /// <param name="referencedUnityObjects">The list of Unity objects to use for external index reference resolution.</param>
 /// <returns>
 /// The deserialized value.
 /// </returns>
 public static object DeserializeValueWeak(byte[] bytes, DataFormat format, List <UnityEngine.Object> referencedUnityObjects)
 {
     using (var stream = CachedMemoryStream.Claim(bytes))
     {
         return(DeserializeValueWeak(stream.Value.MemoryStream, format, referencedUnityObjects));
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Serializes the given value using the specified format and returns the result as a byte array.
 /// </summary>
 /// <typeparam name="T">The type of the value to serialize.</typeparam>
 /// <param name="value">The value to serialize.</param>
 /// <param name="format">The format to use.</param>
 /// <param name="unityObjects">A list of the Unity objects which were referenced during serialization.</param>
 /// <param name="context">The context to use.</param>
 /// <returns>A byte array containing the serialized value.</returns>
 public static byte[] SerializeValue <T>(T value, DataFormat format, out List <UnityEngine.Object> unityObjects, SerializationContext context = null)
 {
     using (var stream = CachedMemoryStream.Claim())
     {
         SerializeValue(value, stream.Value.MemoryStream, format, out unityObjects, context);
         return(stream.Value.MemoryStream.ToArray());
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Serializes the given value using the specified format, and returns the result as a byte array.
 /// </summary>
 /// <typeparam name="T">The type of the value to serialize.</typeparam>
 /// <param name="value">The value to serialize.</param>
 /// <param name="format">The format to use.</param>
 /// <param name="context">The context to use.</param>
 /// <returns>A byte array containing the serialized value.</returns>
 public static byte[] SerializeValue <T>(T value, DataFormat format, SerializationContext context = null)
 {
     using (var stream = CachedMemoryStream.Claim())
     {
         SerializeValue(value, stream.Value.MemoryStream, format, context);
         return(stream.Value.MemoryStream.ToArray());
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Serializes the given value using the specified format, and returns the result as a byte array.
 /// </summary>
 /// <param name="value">The value to serialize.</param>
 /// <param name="format">The format to use.</param>
 /// <param name="unityObjects">A list of the Unity objects which were referenced during serialization.</param>
 /// <returns>A byte array containing the serialized value.</returns>
 public static byte[] SerializeValueWeak(object value, DataFormat format, out List <UnityEngine.Object> unityObjects)
 {
     using (var stream = CachedMemoryStream.Claim())
     {
         SerializeValueWeak(value, stream.Value.MemoryStream, format, out unityObjects);
         return(stream.Value.MemoryStream.ToArray());
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Serializes the given value using the specified format, and returns the result as a byte array.
 /// </summary>
 /// <param name="value">The value to serialize.</param>
 /// <param name="format">The format to use.</param>
 /// <returns>A byte array containing the serialized value.</returns>
 public static byte[] SerializeValueWeak(object value, DataFormat format)
 {
     using (var stream = CachedMemoryStream.Claim())
     {
         SerializeValueWeak(value, stream.Value.MemoryStream, format);
         return(stream.Value.MemoryStream.ToArray());
     }
 }
Exemplo n.º 9
0
        /// <summary>
        /// Creates a deep copy of an object. Returns null if null. All Unity objects references will remain the same - they will not get copied.
        /// </summary>
        public static object CreateCopy(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            if (obj as string != null)
            {
                return(obj);
            }

            var type = obj.GetType();

            if (type.IsValueType)
            {
                return(obj);
            }

            if (type.InheritsFrom(typeof(UnityEngine.Object)))
            {
                return(obj);
            }

            using (var stream = CachedMemoryStream.Claim())
                using (var serContext = Cache <SerializationContext> .Claim())
                    using (var deserContext = Cache <DeserializationContext> .Claim())
                    {
                        serContext.Value.Config.SerializationPolicy   = SerializationPolicies.Everything;
                        deserContext.Value.Config.SerializationPolicy = SerializationPolicies.Everything;

                        List <UnityEngine.Object> unityReferences;
                        SerializeValue(obj, stream.Value.MemoryStream, DataFormat.Binary, out unityReferences, serContext);
                        stream.Value.MemoryStream.Position = 0;
                        return(DeserializeValue <object>(stream.Value.MemoryStream, DataFormat.Binary, unityReferences, deserContext));
                    }
        }