Example #1
0
    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();
                  }
        }
    }
Example #2
0
        public ISerializer Read(string path, AssetInfo info, IFileSystem disk, IJsonUtil jsonUtil)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }
            if (disk == null)
            {
                throw new ArgumentNullException(nameof(disk));
            }
            var subAssets = new Dictionary <int, (string, string)>(); // path and name
            // Pattern vars: 0=Index 1=SubItem 2=Name 3=Palette
            var pattern = info.Get(AssetProperty.Pattern, "{0}_{1}_{2}.dat");

            foreach (var filePath in disk.EnumerateDirectory(path, $"{info.Index}_*.*"))
            {
                var filename = Path.GetFileName(filePath);
                var(index, subAsset, paletteId, name) = AssetInfo.ParseFilename(pattern, filename);

                if (paletteId.HasValue)
                {
                    info.Set(AssetProperty.PaletteId, paletteId);
                }

                if (index != info.Index)
                {
                    continue;
                }

                subAssets[subAsset] = (filePath, name);
            }

            var ms = new MemoryStream();

            if (subAssets.Count > 0)
            {
                using var bw = new BinaryWriter(ms, Encoding.UTF8, true);
                using var s  = new AlbionWriter(bw);
                PackedChunks.PackNamed(s, subAssets.Keys.Max() + 1, i =>
                                       !subAssets.TryGetValue(i, out var pathAndName)
                        ? (Array.Empty <byte>(), null)
                        : (disk.ReadAllBytes(pathAndName.Item1), pathAndName.Item2));
            }

            ms.Position = 0;
            var br = new BinaryReader(ms);

            return(new AlbionReader(br, ms.Length, () =>
            {
                br.Dispose();
                ms.Dispose();
            }));
        }
Example #3
0
        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;
        }