public IReadOnlyTexture <byte> Serdes(IReadOnlyTexture <byte> existing, AssetInfo info, AssetMapping mapping, ISerializer s, IJsonUtil jsonUtil) { if (info == null) { throw new ArgumentNullException(nameof(info)); } if (s == null) { throw new ArgumentNullException(nameof(s)); } var paletteNum = info.Get(AssetProperty.PaletteId, 0); var paletteId = new PaletteId(AssetType.Palette, paletteNum); var palette = Resolve <IAssetManager>().LoadPalette(paletteId); if (palette == null) { throw new InvalidOperationException($"Could not load palette {paletteId} ({paletteNum}) for asset {info.AssetId} in file {info.File.Filename}"); } var unambiguousPalette = palette.GetUnambiguousPalette(); if (s.IsWriting()) { if (existing == null) { throw new ArgumentNullException(nameof(existing)); } var encoder = new PngEncoder(); PackedChunks.Pack(s, existing.Regions.Count, frameNum => Write(encoder, unambiguousPalette, existing, frameNum)); return(existing); } // Read var decoder = new PngDecoder(); var configuration = new Configuration(); var images = new List <Image <Rgba32> >(); try { foreach (var(bytes, _) in PackedChunks.Unpack(s)) { using var stream = new MemoryStream(bytes); images.Add(decoder.Decode <Rgba32>(configuration, stream)); } return(Read(info.AssetId, unambiguousPalette, images)); } finally { foreach (var image in images) { image.Dispose(); } } }
public BlockList Serdes(BlockList existing, AssetInfo info, AssetMapping mapping, ISerializer s, IJsonUtil jsonUtil) { if (info == null) throw new ArgumentNullException(nameof(info)); if (s == null) throw new ArgumentNullException(nameof(s)); if (jsonUtil == null) throw new ArgumentNullException(nameof(jsonUtil)); if (s.IsWriting()) { if (existing == null) throw new ArgumentNullException(nameof(existing)); var blockListId = (BlockListId)info.AssetId; var tilesetId = blockListId.ToTileset(); var tilesetPattern = info.Get(AssetProperty.TilesetPattern, "../Tilesets/{0}_{2}.tsx"); var tilesetPath = string.Format(CultureInfo.InvariantCulture, tilesetPattern, tilesetId.Id, 0, ConfigUtil.AssetName(tilesetId)); var tileset = new Tileset { Filename = tilesetPath, TileWidth = 16, TileHeight = 16, }; PackedChunks.Pack(s, existing.Count, stampNumber => { if (existing[stampNumber].Width == 0 || existing[stampNumber].Height == 0) return Array.Empty<byte>(); var stamp = new Stamp(stampNumber, existing[stampNumber], tileset); return Encoding.UTF8.GetBytes(stamp.Serialize(jsonUtil)); }); return existing; } var list = new BlockList(); foreach (var (jsonBytes, _) in PackedChunks.Unpack(s)) { var stamp = Stamp.Parse(jsonBytes, jsonUtil); var block = stamp != null ? stamp.ToBlock() : new Block(); list.Add(block); } while (list.Count < BlockList.MaxCount) list.Add(new Block()); return list; }