private static void ReadLayer(FileStream stream, Grid grid, int version) { stream.Read(MapBuffer, 0, 4); grid.Width = BitConverter.ToInt32(MapBuffer, 0); stream.Read(MapBuffer, 0, 4); grid.Height = BitConverter.ToInt32(MapBuffer, 0); if (version >= 258) { stream.Read(MapBuffer, 0, 4); } stream.Read(MapBuffer, 0, 25); if (version == 260) { stream.Read(MapBuffer, 0, 2); } else if (version == 261) { stream.Read(MapBuffer, 0, 3); } int size = grid.Width * grid.Height; if (grid.Cells.Count == 0) { for (int i = 0; i < size; i++) { Cell cell = new Cell((short)i); grid.Cells.Add(cell); } } int dataBlocks = stream.ReadByte(); if (dataBlocks < 1 && dataBlocks > 2) { throw new Exception($"Invalid data block count in {grid.FileName}"); } //MAIN stream.Read(MapBuffer, 0, 8); int compressedSize = BitConverter.ToInt32(MapBuffer, 4); int nextPosition = (int)stream.Position + compressedSize; Zip.ResetStream(stream); int read = Zip.Read(MapBuffer, 0, size * 2); //if (read != size * 2) { //throw new Exception($"Failed to read layer stream in {grid.FileName}"); //} read >>= 1; stream.Position = nextPosition; List <Item> items = new List <Item>(); for (int j = 0, k = 0; j < read; j++, k += 2) { Cell cell = grid.Cells[j]; short id = BitConverter.ToInt16(MapBuffer, k); Item item; if (!DefaultsByID.TryGetValue(id, out item)) { //throw new Exception($"Failed to find Object with ID {id} [{Reader.ShortToCoordinate(id)}] in {grid.FileName}"); item = Item.EMPTY.Copy(); id = -1; } else { item = item.Copy(); } item.Position = (short)j; items.Add(item); if (id != -1) { cell.Objects.Add(item); } } if (dataBlocks == 2) { //DATA stream.Read(MapBuffer, 0, 13); compressedSize = BitConverter.ToInt32(MapBuffer, 9); nextPosition = (int)stream.Position + compressedSize; Zip.ResetStream(stream); read = Zip.Read(MapBuffer, 0, size); //if (read != size) { // throw new Exception($"Failed to read layer stream in {grid.FileName}"); //} stream.Position = nextPosition; for (int j = 0; j < read; j++) { Item item = items[j]; item.Direction = MapBuffer[j]; } } }