Esempio n. 1
0
 public static bool TryParseTilemapFormat(string format, out TilemapFormat tilemapFormat)
 {
     tilemapFormat = default;
     if (!(format.StartsWith("`ucm") && format.EndsWith("`")))
     {
         return(false);
     }
     format = format.Substring(4, format.Length - 5);
     return(LzTilemapRun.TryParseGeneralTilemapFormat(format, out tilemapFormat));
 }
        public ISpriteRun SetPixels(IDataModel model, ModelDelta token, int page, int[,] pixels)
        {
            var tileData       = Tilize(pixels);
            var tiles          = GetUniqueTiles(tileData);
            var tilesetAddress = model.GetAddressFromAnchor(new NoDataChangeDeltaModel(), -1, Format.MatchingTileset);
            var tileset        = model.GetNextRun(tilesetAddress) as LzTilesetRun;

            if (tileset == null)
            {
                tileset = model.GetNextRun(arrayTilesetAddress) as LzTilesetRun;
            }
            tileset.SetPixels(model, token, tiles);
            if (tiles.Length > 0x400)
            {
                // TODO fail: too many unique tiles
                return(this);
            }
            var mapData = Decompress(model, Start);

            var tileWidth  = tileData.GetLength(0);
            var tileHeight = tileData.GetLength(1);

            for (int y = 0; y < tileHeight; y++)
            {
                for (int x = 0; x < tileWidth; x++)
                {
                    var i = y * tileWidth + x;
                    var(tile, paletteIndex)   = tileData[x, y];
                    var(tileIndex, matchType) = FindMatch(tile, tiles);
                    var mapping = PackMapping(paletteIndex, matchType, tileIndex);
                    mapData[i * 2 + 0] = (byte)mapping;
                    mapData[i * 2 + 1] = (byte)(mapping >> 8);
                }
            }

            var newModelData = Compress(mapData, 0, mapData.Length);
            var newRun       = (ISpriteRun)model.RelocateForExpansion(token, this, newModelData.Count);

            for (int i = 0; i < newModelData.Count; i++)
            {
                token.ChangeData(model, newRun.Start + i, newModelData[i]);
            }
            for (int i = newModelData.Count; i < Length; i++)
            {
                token.ChangeData(model, newRun.Start + i, 0xFF);
            }
            newRun = new LzTilemapRun(Format, model, newRun.Start, newRun.PointerSources);
            model.ObserveRunWritten(token, newRun);
            return(newRun);
        }
Esempio n. 3
0
        /// <param name="newRawData">Uncompressed data that we want to compress and insert.</param>
        public ITilemapRun ReplaceData(byte[] newRawData, ModelDelta token)
        {
            var newModelData = Compress(newRawData, 0, newRawData.Length);
            var newRun       = Model.RelocateForExpansion(token, this, newModelData.Count);

            for (int i = 0; i < newModelData.Count; i++)
            {
                token.ChangeData(Model, newRun.Start + i, newModelData[i]);
            }
            for (int i = newModelData.Count; i < Length; i++)
            {
                token.ChangeData(Model, newRun.Start + i, 0xFF);
            }
            newRun = new LzTilemapRun(Format, Model, newRun.Start, newRun.PointerSources);
            Model.ObserveRunWritten(token, newRun);
            return(newRun);
        }
Esempio n. 4
0
        public byte[] GetData()
        {
            var tilesetAddress = Model.GetAddressFromAnchor(new NoDataChangeDeltaModel(), -1, Format.MatchingTileset);
            var tileset        = Model.GetNextRun(tilesetAddress) as ISpriteRun;

            if (tileset == null)
            {
                tileset = Model.GetNextRun(arrayTilesetAddress) as ISpriteRun;
            }

            if (tileset == null)
            {
                return(new byte[Format.TileWidth * 8 * Format.TileHeight * Format.BitsPerPixel]);
            }

            var tiles = tileset.GetData();

            return(LzTilemapRun.GetData(GetTilemapData(), tiles, Format, BytesPerTile));
        }
Esempio n. 5
0
        public int[,] GetPixels(IDataModel model, int page)
        {
            var mapData = GetTilemapData();

            var tilesetAddress = model.GetAddressFromAnchor(new NoDataChangeDeltaModel(), -1, Format.MatchingTileset);
            var tileset        = model.GetNextRun(tilesetAddress) as ISpriteRun;

            if (tileset == null)
            {
                tileset = model.GetNextRun(arrayTilesetAddress) as ISpriteRun;
            }

            if (tileset == null)
            {
                return(new int[Format.TileWidth * 8, Format.TileHeight * 8]);              // relax the conditions slightly: if the run we found is an LZSpriteRun, that's close enough, we can use it as a tileset.
            }
            var tiles = tileset.GetData();

            return(LzTilemapRun.GetPixels(mapData, tiles, Format, BytesPerTile));
        }
Esempio n. 6
0
 public ISpriteRun SetPixels(IDataModel model, ModelDelta token, int page, int[,] pixels)
 {
     return(LzTilemapRun.SetPixels(this, model, token, page, pixels, ref arrayTilesetAddress, ReplaceData));
 }
Esempio n. 7
0
 public int FindMatchingTileset(IDataModel model) => LzTilemapRun.FindMatchingTileset(this, model, ref arrayTilesetAddress);