public async Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            var resolved = await _resolver.Resolve(identifier, parameters, token);

            if (resolved.Stream == null && resolved.Decoded == null)
            {
                throw new ArgumentNullException($"{nameof(resolved.Stream)} and {nameof(resolved.Decoded)}");
            }

            if (resolved.Stream != null && !resolved.Stream.CanSeek)
            {
                using (resolved.Stream)
                {
                    var memoryStream = new MemoryStream();
                    await resolved.Stream.CopyToAsync(memoryStream);

                    memoryStream.Position = 0;
                    resolved = new DataResolverResult(memoryStream, resolved.LoadingResult, resolved.ImageInformation);
                }
            }

            if (resolved.Stream != null)
            {
                if (resolved.Stream.Length == 0)
                {
                    throw new InvalidDataException("Zero length stream");
                }

                if (resolved.Stream.Length < 32)
                {
                    throw new InvalidDataException("Invalid stream");
                }

                if (resolved.ImageInformation.Type == ImageInformation.ImageType.Unknown)
                {
                    //READ HEADER
                    const int headerLength = 4;
                    byte[]    header       = new byte[headerLength];
                    int       offset       = 0;
                    while (offset < headerLength)
                    {
                        int read = await resolved.Stream.ReadAsync(header, offset, headerLength - offset);

                        offset += read;
                    }

                    resolved.Stream.Position = 0;
                    resolved.ImageInformation.SetType(FileHeader.GetImageType(header));
                }

                if (resolved.ImageInformation.Type == ImageInformation.ImageType.JPEG)
                {
                    var exif = ExifHelper.Read(resolved.Stream);
                    resolved.Stream.Position = 0;
                    resolved.ImageInformation.SetExif(exif);
                }
            }

            return(resolved);
        }
Esempio n. 2
0
        public async Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            var resolved = await _resolver.Resolve(identifier, parameters, token);

            if (resolved.Item1 == null)
            {
                throw new ArgumentNullException(nameof(parameters.Stream));
            }

            if (!resolved.Item1.CanSeek)
            {
                using (resolved.Item1)
                {
                    var memoryStream = new MemoryStream();
                    await resolved.Item1.CopyToAsync(memoryStream);

                    memoryStream.Position = 0;
                    resolved = new Tuple <Stream, LoadingResult, ImageInformation>(memoryStream, resolved.Item2, resolved.Item3);
                }
            }

            if (resolved.Item1.Length == 0)
            {
                throw new InvalidDataException("Zero length stream");
            }

            if (resolved.Item1.Length < 32)
            {
                throw new InvalidDataException("Invalid stream");
            }

            if (resolved.Item3.Type == ImageInformation.ImageType.Unknown)
            {
                //READ HEADER
                const int headerLength = 4;
                byte[]    header       = new byte[headerLength];
                int       offset       = 0;
                while (offset < headerLength)
                {
                    int read = await resolved.Item1.ReadAsync(header, offset, headerLength - offset);

                    offset += read;
                }

                resolved.Item1.Position = 0;
                resolved.Item3.SetType(FileHeader.GetImageType(header));
            }

            return(resolved);
        }
        public async Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            var resolved = await _resolver.Resolve(identifier, parameters, token).ConfigureAwait(false);

            if (resolved.Stream == null && resolved.Decoded == null)
            {
                throw new ArgumentNullException($"{nameof(resolved.Stream)} and {nameof(resolved.Decoded)}");
            }

            resolved.Stream = await resolved.Stream.AsSeekableStreamAsync().ConfigureAwait(false);

            if (resolved.Stream != null)
            {
                if (resolved.Stream.Length == 0)
                {
                    throw new InvalidDataException("Zero length stream");
                }

                if (resolved.Stream.Length < 32)
                {
                    throw new InvalidDataException("Invalid stream");
                }

                if (resolved.ImageInformation.Type == ImageInformation.ImageType.Unknown)
                {
                    //READ HEADER
                    const int headerLength = 4;
                    var       header       = new byte[headerLength];
                    var       offset       = 0;
                    while (offset < headerLength)
                    {
                        offset += await resolved.Stream.ReadAsync(header, offset, headerLength - offset).ConfigureAwait(false);
                    }

                    resolved.Stream.Position = 0;
                    resolved.ImageInformation.SetType(FileHeader.GetImageType(header));
                }

                if (resolved.ImageInformation.Type == ImageInformation.ImageType.JPEG)
                {
                    var exif = ExifHelper.Read(resolved.Stream);
                    resolved.Stream.Position = 0;
                    resolved.ImageInformation.SetExif(exif);
                }
            }

            return(resolved);
        }
Esempio n. 4
0
        public PlayerPrefsData(string key, IDataResolver dataResolver)
        {
            m_Key          = key;
            m_DataResolver = dataResolver;

            var s = PlayerPrefs.GetString(m_Key, "");

            if (m_DataResolver != null &&
                m_DataResolver.Resolve(s, out string rS))
            {
                Debug.Log($"Resolved from: {s} to: {rS}");
                s = rS;
            }
            if (string.IsNullOrEmpty(s))
            {
                m_Value = new T();
            }
            else
            {
                m_Value = JsonUtility.FromJson <PlayerPrefsData <T> >(s).m_Value;
            }
        }