public void WriteRead() { var tiles = new Tile?[10, 10]; Main.tileFrameImportant[1] = true; var tile = new Tile { IsActive = true, BlockColor = 1, WallColor = 1, BlockId = 1, WallId = 1, Liquid = 255 }; for (var x = 0; x < 10; ++x) { for (var y = 0; y < 10; ++y) { tiles[x, y] = tile; } } var clipboard = new Clipboard(tiles); var items = new Item[40]; items[0] = new Item(1, 2, 3); var chest = new Chest(new Vector(0, 1), "Test", new Item[40]); var sign = new Sign(new Vector(1, 0), "Test2"); clipboard.AddTileEntity(chest); clipboard.AddTileEntity(sign); var schematicFormat = new TeditSchematicFormat(); Clipboard clipboard2; using (var stream = new MemoryStream()) { schematicFormat.Write(clipboard, stream); stream.Position = 0; clipboard2 = schematicFormat.Read(stream); Assert.That(clipboard2, Is.Not.Null); } for (var x = 0; x < 10; ++x) { for (var y = 0; y < 10; ++y) { Assert.That(clipboard2.GetTile(new Vector(x, y)), Is.EqualTo(tile)); } } foreach (var entity in clipboard2.GetTileEntities()) { if (entity is Chest chest2) { Assert.That(chest2.Name, Is.EqualTo(chest.Name)); Assert.That(chest2.Items, Is.EquivalentTo(chest2.Items)); Assert.That(chest2.Position, Is.EqualTo(chest.Position)); } else if (entity is Sign sign2) { Assert.That(sign2.Text, Is.EqualTo(sign.Text)); Assert.That(sign2.Position, Is.EqualTo(sign.Position)); } } }
public override Clipboard Read(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead) { throw new ArgumentException("Stream must support reading.", nameof(stream)); } var reader = new BinaryReader(stream, Encoding.Default, true); try { var name = reader.ReadString(); var version = reader.ReadUInt32(); var width = reader.ReadInt32(); var height = reader.ReadInt32(); var clipboard = new Clipboard(DeserializeTiles(reader, width, height)); foreach (var chest in DeserializeChests(reader)) { clipboard.AddTileEntity(chest); } foreach (var sign in DeserializeSigns(reader)) { clipboard.AddTileEntity(sign); } var name2 = reader.ReadString(); var version2 = reader.ReadUInt32(); var width2 = reader.ReadInt32(); var height2 = reader.ReadInt32(); if (name != name2 || version != version2 || width != width2 || height != height2) { return(null); } return(clipboard); } catch (Exception e) when(e is EndOfStreamException || e is IndexOutOfRangeException || e is IOException) { return(null); } finally { reader.Dispose(); } }
public void AddTileEntity_False(int x, int y) { var clipboard = new Clipboard(new Tile?[10, 10]); var tileEntity = Mock.Of <ITileEntity>(e => e.Position == new Vector(x, y)); Assert.That(!clipboard.AddTileEntity(tileEntity)); }
public void RemoveTileEntityGetTileEntities(int x, int y) { var clipboard = new Clipboard(new Tile?[10, 10]); var tileEntity = Mock.Of <ITileEntity>(e => e.Position == new Vector(x, y)); clipboard.AddTileEntity(tileEntity); Assert.That(clipboard.RemoveTileEntity(tileEntity)); Assert.That(clipboard.GetTileEntities(), Is.Empty); }
public void AddTileEntityGetTileEntities(int x, int y) { var clipboard = new Clipboard(new Tile?[10, 10]); var tileEntity = Mock.Of <ITileEntity>(e => e.Position == new Vector(x, y)); Assert.That(clipboard.AddTileEntity(tileEntity)); var tileEntities = clipboard.GetTileEntities().ToList(); Assert.That(tileEntities, Has.Count.EqualTo(1)); Assert.That(tileEntities[0].Position, Is.EqualTo(new Vector(x, y))); }
public override Clipboard Read(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead) { throw new ArgumentException("Stream must support reading.", nameof(stream)); } Clipboard clipboard; var gzipStream = new GZipStream(stream, CompressionMode.Decompress, true); var reader = new BinaryReader(gzipStream, Encoding.Default, true); try { reader.ReadUInt32(); // Version, currently not used. var width = reader.ReadInt32(); var height = reader.ReadInt32(); clipboard = new Clipboard(new Tile?[width, height]); for (var x = 0; x < width; ++x) { for (var y = 0; y < height; ++y) { clipboard.SetTile(new Vector(x, y), reader.ReadTile()); } } foreach (var tileEntity in ReadTileEntities(reader)) { clipboard.AddTileEntity(tileEntity); } } catch (Exception e) when(e is EndOfStreamException || e is IndexOutOfRangeException || e is IOException) { return(null); } finally { gzipStream.Dispose(); reader.Dispose(); } return(clipboard); }
public void PasteTo() { var tile = new Tile { WallId = 1 }; var tiles = new Tile?[4, 4]; for (var x = 0; x < 3; ++x) { for (var y = 0; y < 3; ++y) { tiles[x, y] = tile; } } var clipboard = new Clipboard(tiles); var tileEntity = Mock.Of <ITileEntity>(e => e.Position == Vector.Zero); Mock.Get(tileEntity) .Setup(e => e.WithPosition(Vector.One)) .Returns((Vector v) => Mock.Of <ITileEntity>(e => e.Position == v)); clipboard.AddTileEntity(tileEntity); var extent = Mock.Of <Extent>(e => e.Dimensions == new Vector(5, 5) && e.AddTileEntity( Match.Create <ITileEntity>(n => n.Position == Vector.One)) && e.SetTile(It.IsAny <Vector>(), It.IsAny <Tile>())); Assert.That(clipboard.PasteTo(extent, Vector.One), Is.EqualTo(10)); for (var x = 1; x < 4; ++x) { for (var y = 1; y < 4; ++y) { var x1 = x; var y1 = y; Mock.Get(extent).Verify(e => e.SetTile(new Vector(x1, y1), tile)); } } Mock.Get(extent).Verify(e => e.AddTileEntity(Match.Create <ITileEntity>(n => n.Position == Vector.One))); }
public void WriteRead() { var tiles = new Tile?[10, 10]; Main.tileFrameImportant[1] = true; var tile = new Tile { IsActive = true, BlockColor = 1, WallColor = 1, BlockId = 1, WallId = 1, Liquid = 255 }; for (var x = 0; x < 10; ++x) { for (var y = 0; y < 10; ++y) { tiles[x, y] = tile; } } var clipboard = new Clipboard(tiles); var items = new Item[40]; items[0] = new Item(1, 2, 3); var chest = new Chest(new Vector(0, 1), "Test", new Item[40]); var itemFrame = new ItemFrame(new Vector(3, 4), new Item(1, 2, 3)); var logicSensor = new LogicSensor(new Vector(2, 5), LogicSensorType.Day, true, 5); var sign = new Sign(new Vector(1, 0), "Test2"); var trainingDummy = new TrainingDummy(new Vector(4, 5), 21); clipboard.AddTileEntity(chest); clipboard.AddTileEntity(itemFrame); clipboard.AddTileEntity(logicSensor); clipboard.AddTileEntity(sign); clipboard.AddTileEntity(trainingDummy); var schematicFormat = new DefaultSchematicFormat(); Clipboard clipboard2; using (var stream = new MemoryStream()) { schematicFormat.Write(clipboard, stream); stream.Position = 0; clipboard2 = schematicFormat.Read(stream); Assert.That(clipboard2, Is.Not.Null); } for (var x = 0; x < 10; ++x) { for (var y = 0; y < 10; ++y) { Assert.That(clipboard2.GetTile(new Vector(x, y)), Is.EqualTo(tile)); } } foreach (var entity in clipboard2.GetTileEntities()) { if (entity is Chest chest2) { Assert.That(chest2.Position, Is.EqualTo(chest.Position)); Assert.That(chest2.Name, Is.EqualTo(chest.Name)); Assert.That(chest2.Items, Is.EquivalentTo(chest2.Items)); } else if (entity is ItemFrame itemFrame2) { Assert.That(itemFrame2.Position, Is.EqualTo(itemFrame.Position)); Assert.That(itemFrame2.Item, Is.EqualTo(itemFrame.Item)); } else if (entity is LogicSensor logicSensor2) { Assert.That(logicSensor2.Position, Is.EqualTo(logicSensor.Position)); Assert.That(logicSensor2.Type, Is.EqualTo(logicSensor.Type)); Assert.That(logicSensor2.IsEnabled, Is.EqualTo(logicSensor.IsEnabled)); Assert.That(logicSensor2.Data, Is.EqualTo(logicSensor.Data)); } else if (entity is Sign sign2) { Assert.That(sign2.Position, Is.EqualTo(sign.Position)); Assert.That(sign2.Text, Is.EqualTo(sign.Text)); } else if (entity is TrainingDummy trainingDummy2) { Assert.That(trainingDummy2.Position, Is.EqualTo(trainingDummy.Position)); Assert.That(trainingDummy2.NpcIndex, Is.EqualTo(trainingDummy.NpcIndex)); } } }