private static MapCell DeserializeMapCell(BinaryReader Reader, List <Guid> IdTable, MapTileSet TileSet) { MapCell cell = new MapCell(); MapCellSerializationType cellType = DeserializeMapCellType(Reader); if (cellType == MapCellSerializationType.Null) { return(null); } int idIndexBytesCount = 4; if ((cellType & MapCellSerializationType.IdIndex8) != 0) { idIndexBytesCount = 1; } else if ((cellType & MapCellSerializationType.IdIndex16) != 0) { idIndexBytesCount = 3; } else if ((cellType & MapCellSerializationType.IdIndex32) != 0) { idIndexBytesCount = 4; } if ((cellType & MapCellSerializationType.Place) != 0) { cell.Place = DeserializeMapCellObjectReference(Reader, idIndexBytesCount, IdTable, TileSet) as MapPlace; } if ((cellType & MapCellSerializationType.NorthWall) != 0) { cell.SetWall(MapDirection.North, DeserializeMapCellObjectReference(Reader, idIndexBytesCount, IdTable, TileSet) as MapWall); } if ((cellType & MapCellSerializationType.SouthWall) != 0) { cell.SetWall(MapDirection.South, DeserializeMapCellObjectReference(Reader, idIndexBytesCount, IdTable, TileSet) as MapWall); } if ((cellType & MapCellSerializationType.WestWall) != 0) { cell.SetWall(MapDirection.West, DeserializeMapCellObjectReference(Reader, idIndexBytesCount, IdTable, TileSet) as MapWall); } if ((cellType & MapCellSerializationType.EastWall) != 0) { cell.SetWall(MapDirection.East, DeserializeMapCellObjectReference(Reader, idIndexBytesCount, IdTable, TileSet) as MapWall); } return(cell); }
private static void SerializeMapCell(BinaryWriter Writer, MapCell Cell, List <Guid> IdTable) { if (Cell == null) { SerializeMapCellType(Writer, MapCellSerializationType.Null); return; } int idIndexBytesCount = 1; if (IdTable.Count > byte.MaxValue - 5) { idIndexBytesCount = 2; } if (IdTable.Count > UInt16.MaxValue - 5) { idIndexBytesCount = 4; } MapCellSerializationType type = MapCellSerializationType.Place; MapWall northWall = Cell.GetWall(MapDirection.North); MapWall southWall = Cell.GetWall(MapDirection.South); MapWall westWall = Cell.GetWall(MapDirection.West); MapWall eastWall = Cell.GetWall(MapDirection.East); if (northWall != null) { type = type | MapCellSerializationType.NorthWall; } if (southWall != null) { type = type | MapCellSerializationType.SouthWall; } if (westWall != null) { type = type | MapCellSerializationType.WestWall; } if (eastWall != null) { type = type | MapCellSerializationType.EastWall; } if (idIndexBytesCount == 1) { type = type | MapCellSerializationType.IdIndex8; } else if (idIndexBytesCount == 2) { type = type | MapCellSerializationType.IdIndex16; } else if (idIndexBytesCount == 4) { type = type | MapCellSerializationType.IdIndex32; } SerializeMapCellType(Writer, type); int placeIndex = IdTable.IndexOf(Cell.Place.Id); if (placeIndex < 0) { IdTable.Add(Cell.Place.Id); placeIndex = IdTable.Count - 1; } SerializeMapCellIdIndex(Writer, idIndexBytesCount, placeIndex); if (northWall != null) { int wallIndex = IdTable.IndexOf(northWall.Id); if (wallIndex < 0) { IdTable.Add(northWall.Id); wallIndex = IdTable.Count - 1; } SerializeMapCellIdIndex(Writer, idIndexBytesCount, wallIndex); } if (southWall != null) { int wallIndex = IdTable.IndexOf(southWall.Id); if (wallIndex < 0) { IdTable.Add(southWall.Id); wallIndex = IdTable.Count - 1; } SerializeMapCellIdIndex(Writer, idIndexBytesCount, wallIndex); } if (westWall != null) { int wallIndex = IdTable.IndexOf(westWall.Id); if (wallIndex < 0) { IdTable.Add(westWall.Id); wallIndex = IdTable.Count - 1; } SerializeMapCellIdIndex(Writer, idIndexBytesCount, wallIndex); } if (eastWall != null) { int wallIndex = IdTable.IndexOf(eastWall.Id); if (wallIndex < 0) { IdTable.Add(eastWall.Id); wallIndex = IdTable.Count - 1; } SerializeMapCellIdIndex(Writer, idIndexBytesCount, wallIndex); } }
private static void SerializeMapCellType(BinaryWriter Writer, MapCellSerializationType CellType) { Writer.Write((byte)CellType); }