private static LottieResult <LottieComposition> FromZipStreamSyncInternal(RenderTarget renderTarget, Ionic.Zip.ZipFile inputStream, string cacheKey)
        {
            LottieComposition composition = null;
            Dictionary <string, SharpDX.Direct2D1.Bitmap> images = new Dictionary <string, SharpDX.Direct2D1.Bitmap>();

            try
            {
                foreach (Ionic.Zip.ZipEntry entry in inputStream.Entries)
                {
                    if (entry.FileName.Contains("__MACOSX"))
                    {
                        continue;
                    }
                    else if (entry.FileName.Contains(".json"))
                    {
                        using var reader = entry.OpenReader();
                        composition      = FromJsonInputStreamSync(reader, cacheKey, false).Value;
                    }
                    else if (entry.FileName.Contains(".png"))
                    {
                        string[] splitName = entry.FileName.Split('/');
                        string   name      = splitName[splitName.Length - 1];

                        using (var decoder = new SharpDX.WIC.BitmapDecoder(imagingFactory, entry.InputStream, DecodeOptions.CacheOnDemand))
                        {
                            var bitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(renderTarget, decoder.Preview);

                            images[name] = bitmap;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            catch (IOException e)
            {
                return(new LottieResult <LottieComposition>(e));
            }

            if (composition == null)
            {
                return(new LottieResult <LottieComposition>(new ArgumentException("Unable to parse composition")));
            }

            foreach (var e in images)
            {
                LottieImageAsset imageAsset = FindImageAssetForFileName(composition, e.Key);
                if (imageAsset != null)
                {
                    imageAsset.Bitmap = e.Value;
                }
            }

            // Ensure that all bitmaps have been set.
            foreach (var entry in composition.Images)
            {
                if (entry.Value.Bitmap == null)
                {
                    return(new LottieResult <LottieComposition>(new ArgumentException("There is no image for " + entry.Value.FileName)));
                }
            }

            LottieCompositionCache.Instance.Put(cacheKey, composition);
            return(new LottieResult <LottieComposition>(composition));
        }
Пример #2
0
        private static LottieResult <LottieComposition> FromZipStreamSyncInternal(ZipArchive inputStream, string cacheKey)
        {
            LottieComposition             composition = null;
            Dictionary <string, SKBitmap> images      = new Dictionary <string, SKBitmap>();

            try
            {
                foreach (ZipArchiveEntry entry in inputStream.Entries)
                {
                    if (entry.FullName.Contains("__MACOSX"))
                    {
                        continue;
                    }
                    else if (entry.FullName.Contains(".json"))
                    {
                        composition = FromJsonInputStreamSync(entry.Open(), cacheKey, false).Value;
                    }
                    else if (entry.FullName.Contains(".png"))
                    {
                        string[] splitName = entry.FullName.Split('/');
                        string   name      = splitName[splitName.Length - 1];
                        using (var stream = entry.Open())
                        {
                            var bitmap = SKBitmap.Decode(stream);
                            images[name] = bitmap;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            catch (IOException e)
            {
                return(new LottieResult <LottieComposition>(e));
            }

            if (composition == null)
            {
                return(new LottieResult <LottieComposition>(new ArgumentException("Unable to parse composition")));
            }

            foreach (var e in images)
            {
                LottieImageAsset imageAsset = FindImageAssetForFileName(composition, e.Key);
                if (imageAsset != null)
                {
                    imageAsset.Bitmap = e.Value;
                }
            }

            // Ensure that all bitmaps have been set.
            foreach (var entry in composition.Images)
            {
                if (entry.Value.Bitmap == null)
                {
                    return(new LottieResult <LottieComposition>(new ArgumentException("There is no image for " + entry.Value.FileName)));
                }
            }

            LottieCompositionCache.Instance.Put(cacheKey, composition);
            return(new LottieResult <LottieComposition>(composition));
        }