Esempio n. 1
0
        /// <summary>
        /// Deserializes a value of a given type from the given stream 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="stream">The stream to read 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.</param>
        /// <returns>
        /// The deserialized value.
        /// </returns>
        public static T DeserializeValue <T>(Stream stream, DataFormat format, List <UnityEngine.Object> referencedUnityObjects, DeserializationContext context = null)
        {
            IDisposable cache;
            var         reader = GetCachedReader(out cache, format, stream, context);

            try
            {
                if (context != null)
                {
                    return(DeserializeValue <T>(reader, referencedUnityObjects));
                }
                else
                {
                    using (var con = Cache <DeserializationContext> .Claim())
                    {
                        reader.Context = con;
                        return(DeserializeValue <T>(reader, referencedUnityObjects));
                    }
                }
            }
            finally
            {
                cache.Dispose();
            }
        }
Esempio 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));
     }
 }
Esempio n. 3
0
        private static IDataReader GetCachedReader(out IDisposable cache, DataFormat format, Stream stream, DeserializationContext context)
        {
            IDataReader reader;

            if (format == DataFormat.Binary)
            {
                var binaryCache = Cache <BinaryDataReader> .Claim();

                var binaryReader = binaryCache.Value;

                binaryReader.Stream  = stream;
                binaryReader.Context = context;
                binaryReader.PrepareNewSerializationSession();

                reader = binaryReader;
                cache  = binaryCache;
            }
            else if (format == DataFormat.JSON)
            {
                var jsonCache = Cache <JsonDataReader> .Claim();

                var jsonReader = jsonCache.Value;

                jsonReader.Stream  = stream;
                jsonReader.Context = context;
                jsonReader.PrepareNewSerializationSession();

                reader = jsonReader;
                cache  = jsonCache;
            }
            else if (format == DataFormat.Nodes)
            {
                throw new InvalidOperationException("Cannot automatically create a reader for the format '" + DataFormat.Nodes + "', because it does not use a stream.");
            }
            else
            {
                throw new NotImplementedException(format.ToString());
            }

            return(reader);
        }