/// <summary> /// Open a given lottie file /// </summary> /// <param name="fileStream"></param> /// <returns></returns> public static async Task <DotLottie> OpenAsync(Stream fileStream) { var dotLottie = new DotLottie(); var archive = new ZipArchive(fileStream, ZipArchiveMode.Read, true); var manifestEntry = archive.GetEntry(Consts.Manifest); var animationEntries = archive.Entries.Where(x => x.FullName.StartsWith(Consts.Animations)); var imageEntries = archive.Entries.Where(x => x.FullName.StartsWith(Consts.Images)); if (manifestEntry != null) { using (var stream = manifestEntry.Open()) { var manifest = await JsonSerializer.DeserializeAsync <Manifest>(stream, Options.JsonSerializerOptions); dotLottie.Manifest = manifest; } } //todo: asyncify ExtractAnimations(dotLottie, animationEntries); ExtractImages(dotLottie, imageEntries); return(dotLottie); }
/// <summary> /// Open a given lottie file /// </summary> /// <param name="fileStream"></param> /// <returns></returns> public static DotLottie Open(Stream fileStream) { var dotLottie = new DotLottie(); var archive = new ZipArchive(fileStream, ZipArchiveMode.Read, true); var manifestEntry = archive.GetEntry(Consts.Manifest); var animationEntries = archive.Entries.Where(x => x.FullName.StartsWith(Consts.Animations)); var imageEntries = archive.Entries.Where(x => x.FullName.StartsWith(Consts.Images)); if (manifestEntry != null) { using (var stream = manifestEntry.Open()) using (StreamReader streamReader = new StreamReader(stream)) { var json = streamReader.ReadToEnd(); var manifest = JsonSerializer.Deserialize <Manifest>(json, Options.JsonSerializerOptions); dotLottie.Manifest = manifest; } } ExtractAnimations(dotLottie, animationEntries); ExtractImages(dotLottie, imageEntries); return(dotLottie); }
//todo: expose this as a helper method or override for simplicity public static KeyValuePair <string, string> FirstAnimation(this DotLottie dotLottie) { var animationStream = dotLottie.Animations.First(); var animationText = Encoding.UTF8.GetString(animationStream.Value.ToArray()); return(new KeyValuePair <string, string>(animationStream.Key, animationText)); }
public static List <string> Validate(this DotLottie dotLottie) { var errors = new List <string>(); //todo: Check return(errors); }
private static void ExtractImages(DotLottie dotLottie, IEnumerable <ZipArchiveEntry> imageEntries) { foreach (var imageEntry in imageEntries) { using (var stream = imageEntry.Open()) { var ms = new MemoryStream(); stream.CopyTo(ms); ms.Position = 0; dotLottie.Images.Add(imageEntry.Name, ms); } } }
private static void ExtractAnimations(DotLottie dotLottie, IEnumerable <ZipArchiveEntry> animationEntries) { foreach (var animationEntry in animationEntries) { using (var stream = animationEntry.Open()) { var ms = new MemoryStream(); //todo: just use the stream, don't clone it. stream.CopyTo(ms); ms.Position = 0; dotLottie.Animations.Add(animationEntry.Name, ms); } } }