Inheritance: GalaSoft.MvvmLight.ObservableObject
        public ClipboardBuffer GetSelectionBuffer()
        {
            World world = _wvm.CurrentWorld;
            XNA.Rectangle area = _wvm.Selection.SelectionArea;
            var buffer = new ClipboardBuffer(new Vector2Int32(area.Width, area.Height));

            for (int x = 0; x < area.Width; x++)
            {
                for (int y = 0; y < area.Height; y++)
                {
                    Tile curTile = (Tile)world.Tiles[x + area.X, y + area.Y].Clone();

                    if (curTile.Type == 21) // 21 Chest , 55 sign
                    {
                        if (buffer.GetChestAtTile(x, y) == null)
                        {
                            var data = world.GetChestAtTile(x + area.X, y + area.Y);
                            if (data != null)
                            {
                                var newChest = data.Copy();
                                newChest.X = x;
                                newChest.Y = y;
                                buffer.Chests.Add(newChest);
                            }
                        }
                    }
                    if (curTile.Type == 55 || curTile.Type == 85) // 21 Chest , 55 sign
                    {
                        if (buffer.GetSignAtTile(x, y) == null)
                        {
                            var data = world.GetSignAtTile(x + area.X, y + area.Y);
                            if (data != null)
                            {
                                var newSign = data.Copy();
                                newSign.X = x;
                                newSign.Y = y;
                                buffer.Signs.Add(newSign);
                            }
                        }
                    }
                    buffer.Tiles[x, y] = curTile;
                }
            }

            buffer.RenderBuffer();
            return buffer;
        }
 private void ActivateBuffer(ClipboardBuffer item)
 {
     _clipboard.Buffer = item;
     EditPaste();
 }
 public void Remove(ClipboardBuffer item)
 {
     if (LoadedBuffers.Contains(item))
         LoadedBuffers.Remove(item);
 }
 public void FlipX(ClipboardBuffer buffer)
 {
     Flip(buffer, true);
 }
        public static ClipboardBuffer Load4(string filename)
        {
            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var b = new BinaryReader(stream))
                {

                    string name = b.ReadString();
                    int version = b.ReadInt32();

                    int sizeX = b.ReadInt32();
                    int sizeY = b.ReadInt32();
                    var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
                    buffer.Name = name;

                    for (int x = 0; x < sizeX; ++x)
                    {
                        for (int y = 0; y < sizeY; y++)
                        {
                            var tile = new Tile();

                            tile.IsActive = b.ReadBoolean();

                            TileProperty tileProperty = null;
                            if (tile.IsActive)
                            {
                                tile.Type = b.ReadByte();
                                tileProperty = World.TileProperties[tile.Type];

                                if (tile.Type == 127)
                                    tile.IsActive = false;

                                if (tileProperty.IsFramed)
                                {
                                    tile.U = b.ReadInt16();
                                    tile.V = b.ReadInt16();

                                    if (tile.Type == 144) //timer
                                        tile.V = 0;
                                }
                                else
                                {
                                    tile.U = -1;
                                    tile.V = -1;
                                }

                                if (b.ReadBoolean())
                                {
                                    tile.Color = b.ReadByte();
                                }
                            }

                            if (b.ReadBoolean())
                            {
                                tile.Wall = b.ReadByte();
                                if (b.ReadBoolean())
                                    tile.WallColor = b.ReadByte();
                            }

                            if (b.ReadBoolean())
                            {
                                tile.Liquid = b.ReadByte();
                                tile.IsLava = b.ReadBoolean();
                                tile.IsHoney = b.ReadBoolean();
                            }

                            tile.HasWire = b.ReadBoolean();
                            tile.HasWire2 = b.ReadBoolean();
                            tile.HasWire3 = b.ReadBoolean();
                            tile.HalfBrick = b.ReadBoolean();

                            if (tileProperty == null || !tileProperty.IsSolid)
                                tile.HalfBrick = false;

                            tile.Slope = b.ReadByte();
                            if (tileProperty == null || !tileProperty.IsSolid)
                                tile.Slope = 0;

                            tile.Actuator = b.ReadBoolean();
                            tile.InActive = b.ReadBoolean();

                            // read complete, start compression
                            buffer.Tiles[x, y] = tile;

                            int rle = b.ReadInt16();
                            if (rle < 0)
                                throw new ApplicationException("Invalid Tile Data!");

                            if (rle > 0)
                            {
                                for (int k = y + 1; k < y + rle + 1; k++)
                                {
                                    var tcopy = (Tile)tile.Clone();
                                    buffer.Tiles[x, k] = tcopy;
                                }
                                y = y + rle;
                            }
                        }
                    }
                    for (int i = 0; i < 1000; i++)
                    {
                        if (b.ReadBoolean())
                        {
                            var chest = new Chest(b.ReadInt32(), b.ReadInt32());
                            for (int slot = 0; slot < Chest.MaxItems; slot++)
                            {
                                if (slot < Chest.MaxItems)
                                {
                                    int stackSize = (int)b.ReadInt16();
                                    chest.Items[slot].StackSize = stackSize;

                                    if (chest.Items[slot].StackSize > 0)
                                    {
                                        chest.Items[slot].NetId = b.ReadInt32();
                                        chest.Items[slot].StackSize = stackSize;
                                        chest.Items[slot].Prefix = b.ReadByte();
                                    }
                                }
                            }
                            buffer.Chests.Add(chest);
                        }
                    }

                    for (int i = 0; i < 1000; i++)
                    {
                        if (b.ReadBoolean())
                        {
                            Sign sign = new Sign();
                            sign.Text = b.ReadString();
                            sign.X = b.ReadInt32();
                            sign.Y = b.ReadInt32();

                            if (buffer.Tiles[sign.X, sign.Y].IsActive && (int)buffer.Tiles[sign.X, sign.Y].Type == 55 && (int)buffer.Tiles[sign.X, sign.Y].Type == 85)
                                buffer.Signs.Add(sign);
                        }
                    }

                    string verifyName = b.ReadString();
                    int verifyVersion = b.ReadInt32();
                    int verifyX = b.ReadInt32();
                    int verifyY = b.ReadInt32();
                    if (buffer.Name == verifyName &&
                        version == verifyVersion &&
                        buffer.Size.X == verifyX &&
                        buffer.Size.Y == verifyY)
                    {
                        // valid;
                        return buffer;
                    }
                    b.Close();
                    return null;
                }
            }
        }
        public static ClipboardBuffer LoadOld(string filename)
        {
            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream))
                {
                    string name = reader.ReadString();
                    int version = reader.ReadInt32();
                    int maxx = reader.ReadInt32();
                    int maxy = reader.ReadInt32();

                    var buffer = new ClipboardBuffer(new Vector2Int32(maxx, maxy));

                    buffer.Name = string.IsNullOrWhiteSpace(name) ? Path.GetFileNameWithoutExtension(filename) : name;

                    try
                    {
                        for (int x = 0; x < buffer.Size.X; x++)
                        {
                            for (int y = 0; y < buffer.Size.Y; y++)
                            {
                                var tile = new Tile();

                                tile.IsActive = reader.ReadBoolean();

                                if (tile.IsActive)
                                {
                                    tile.Type = reader.ReadByte();

                                    if (tile.Type == 19)
                                    {
                                        tile.U = 0;
                                        tile.V = 0;
                                    }
                                    else if (World.TileProperties[tile.Type].IsFramed)
                                    {
                                        tile.U = reader.ReadInt16();
                                        tile.V = reader.ReadInt16();
                                    }
                                    else
                                    {
                                        tile.U = -1;
                                        tile.V = -1;
                                    }
                                }

                                // trash old lighted value
                                reader.ReadBoolean();

                                if (reader.ReadBoolean())
                                {
                                    tile.Wall = reader.ReadByte();
                                }

                                if (reader.ReadBoolean())
                                {
                                    tile.Liquid = reader.ReadByte();
                                    tile.IsLava = reader.ReadBoolean();
                                }

                                buffer.Tiles[x, y] = tile;
                            }
                        }

                    }
                    catch (Exception)
                    {
                        for (int x = 0; x < buffer.Size.X; x++)
                        {
                            for (int y = 0; y < buffer.Size.Y; y++)
                            {
                                if (buffer.Tiles[x, y] == null)
                                    buffer.Tiles[x, y] = new Tile();
                            }
                        }
                        return buffer;
                    }

                    for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            var chest = new Chest();
                            chest.X = reader.ReadInt32();
                            chest.Y = reader.ReadInt32();

                            for (int slot = 0; slot < 20; slot++)
                            {
                                byte stackSize = reader.ReadByte();
                                if (stackSize > 0)
                                {
                                    string itemName = reader.ReadString();
                                    chest.Items[slot].SetFromName(itemName);
                                    chest.Items[slot].StackSize = stackSize;
                                }
                            }

                            //Chests[chestIndex] = chest;
                            buffer.Chests.Add(chest);
                        }
                    }
                    for (int signIndex = 0; signIndex < 1000; signIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            string signText = reader.ReadString();
                            int x = reader.ReadInt32();
                            int y = reader.ReadInt32();
                            if (buffer.Tiles[x, y].IsActive && (buffer.Tiles[x, y].Type == 55 || buffer.Tiles[x, y].Type == 85))
                            // validate tile location
                            {
                                var sign = new Sign(x, y, signText);
                                //Signs[signIndex] = sign;
                                buffer.Signs.Add(sign);
                            }
                        }
                    }

                    int checkx = reader.ReadInt32();
                    int checky = reader.ReadInt32();

                    if (checkx == maxx && checky == maxy)
                        return buffer;

                }
            }

            return null;
        }
示例#7
0
        public void PasteBufferIntoWorld(Vector2Int32 anchor)
        {
            if (Buffer == null)
            {
                return;
            }
            World           world  = _wvm.CurrentWorld;
            ClipboardBuffer buffer = _wvm.Clipboard.Buffer;

            for (int x = 0; x < buffer.Size.X; x++)
            {
                for (int y = 0; y < buffer.Size.Y; y++)
                {
                    int worldX = x + anchor.X;
                    int worldY = y + anchor.Y;

                    if (world.ValidTileLocation(new Vector2Int32(x + anchor.X, y + anchor.Y)))
                    {
                        //HistMan.AddTileToBuffer(x + anchor.X, y + anchor.Y, ref world.UndoTiles[x + anchor.X, y + anchor.Y]);

                        Tile curTile;

                        if (PasteTiles)
                        {
                            curTile           = (Tile)buffer.Tiles[x, y].Clone();
                            curTile.TileColor = buffer.Tiles[x, y].TileColor;
                        }
                        else
                        {
                            // if pasting tiles is disabled, use the existing tile with buffer's wall & extras
                            curTile              = (Tile)world.Tiles[worldX, worldY].Clone();
                            curTile.Wall         = buffer.Tiles[x, y].Wall;
                            curTile.WallColor    = buffer.Tiles[x, y].WallColor;
                            curTile.LiquidAmount = buffer.Tiles[x, y].LiquidAmount;
                            curTile.LiquidType   = buffer.Tiles[x, y].LiquidType;
                            curTile.WireRed      = buffer.Tiles[x, y].WireRed;
                            curTile.WireGreen    = buffer.Tiles[x, y].WireGreen;
                            curTile.WireBlue     = buffer.Tiles[x, y].WireBlue;
                            curTile.WireYellow   = buffer.Tiles[x, y].WireYellow;
                            curTile.Actuator     = buffer.Tiles[x, y].Actuator;
                            curTile.InActive     = buffer.Tiles[x, y].InActive;
                        }

                        if (!PasteEmpty && (curTile.LiquidAmount == 0 && !curTile.IsActive && curTile.Wall == 0 && !curTile.WireRed && !curTile.WireBlue && !curTile.WireGreen && !curTile.WireYellow))
                        {
                            // skip tiles that are empty if paste empty is not true
                            continue;
                        }
                        if (!PasteWalls)
                        {
                            // if pasting walls is disabled, use the existing wall
                            curTile.Wall      = world.Tiles[worldX, worldY].Wall;
                            curTile.WallColor = world.Tiles[worldX, worldY].WallColor;
                        }
                        if (!PasteLiquids)
                        {
                            // if pasting liquids is disabled, use any existing liquid
                            curTile.LiquidAmount = world.Tiles[worldX, worldY].LiquidAmount;
                            curTile.LiquidType   = world.Tiles[worldX, worldY].LiquidType;
                        }
                        if (!PasteWires)
                        {
                            // if pasting wires is disabled, use any existing wire
                            Tile worldTile = world.Tiles[worldX, worldY];
                            curTile.WireRed    = worldTile.WireRed;
                            curTile.WireGreen  = worldTile.WireGreen;
                            curTile.WireBlue   = worldTile.WireBlue;
                            curTile.WireYellow = worldTile.WireYellow;
                            curTile.Actuator   = worldTile.Actuator;
                            curTile.InActive   = worldTile.InActive;
                        }
                        //  Update chest/sign data only if we've pasted tiles
                        if (PasteTiles)
                        {
                            // Remove overwritten chests data
                            if (Tile.IsChest(world.Tiles[x + anchor.X, y + anchor.Y].Type))
                            {
                                var data = world.GetChestAtTile(x + anchor.X, y + anchor.Y);
                                if (data != null)
                                {
                                    _wvm.UndoManager.Buffer.Chests.Add(data);
                                    world.Chests.Remove(data);
                                }
                            }

                            // Remove overwritten sign data
                            if (Tile.IsSign(world.Tiles[x + anchor.X, y + anchor.Y].Type))
                            {
                                var data = world.GetSignAtTile(x + anchor.X, y + anchor.Y);
                                if (data != null)
                                {
                                    _wvm.UndoManager.Buffer.Signs.Add(data);
                                    world.Signs.Remove(data);
                                }
                            }

                            // Remove overwritten tile entity data
                            if (Tile.IsTileEntity(world.Tiles[x + anchor.X, y + anchor.Y].Type))
                            {
                                var data = world.GetTileEntityAtTile(x + anchor.X, y + anchor.Y);
                                if (data != null)
                                {
                                    //    add this function to UndoManager
                                    //    _wvm.UndoManager.Buffer.TileEntities.Add(data);
                                    world.TileEntities.Remove(data);
                                }
                            }


                            // Add new chest data
                            if (Tile.IsChest(curTile.Type))
                            {
                                if (world.GetChestAtTile(x + anchor.X, y + anchor.Y) == null)
                                {
                                    var data = buffer.GetChestAtTile(x, y, curTile.Type);
                                    if (data != null) // allow? chest copying may not work...
                                    {
                                        // Copied chest
                                        var newChest = data.Copy();
                                        newChest.X = x + anchor.X;
                                        newChest.Y = y + anchor.Y;
                                        world.Chests.Add(newChest);
                                    }
                                }
                            }

                            // Add new sign data
                            if (Tile.IsSign(curTile.Type))
                            {
                                if (world.GetSignAtTile(x + anchor.X, y + anchor.Y) == null)
                                {
                                    var data = buffer.GetSignAtTile(x, y);
                                    if (data != null)
                                    {
                                        // Copied sign
                                        var newSign = data.Copy();
                                        newSign.X = x + anchor.X;
                                        newSign.Y = y + anchor.Y;
                                        world.Signs.Add(newSign);
                                    }
                                }
                            }

                            // Add new tile entity data
                            if (Tile.IsTileEntity(curTile.Type))
                            {
                                if (world.GetTileEntityAtTile(x + anchor.X, y + anchor.Y) == null)
                                {
                                    var data = buffer.GetTileEntityAtTile(x, y);
                                    if (data != null)
                                    {
                                        // Copied sign
                                        var newEntity = data.Copy();
                                        newEntity.PosX = (short)(x + anchor.X);
                                        newEntity.PosY = (short)(y + anchor.Y);
                                        world.TileEntities.Add(newEntity);
                                    }
                                }
                            }
                        }
                        _wvm.UndoManager.SaveTile(x + anchor.X, y + anchor.Y);
                        world.Tiles[x + anchor.X, y + anchor.Y] = curTile;
                    }
                }
            }
            _wvm.UndoManager.SaveUndo();

            /* Heathtech */
            BlendRules.ResetUVCache(_wvm, anchor.X, anchor.Y, buffer.Size.X, buffer.Size.Y);
        }
        public static ClipboardBuffer Load2(string filename)
        {
            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream))
                {
                    string name = reader.ReadString();
                    int version = reader.ReadInt32();
                    int maxx = reader.ReadInt32();
                    int maxy = reader.ReadInt32();

                    var buffer = new ClipboardBuffer(new Vector2Int32(maxx, maxy));

                    buffer.Name = string.IsNullOrWhiteSpace(name) ? Path.GetFileNameWithoutExtension(filename) : name;

                    try
                    {
                        for (int x = 0; x < maxx; x++)
                        {
                            for (int y = 0; y < maxy; y++)
                            {
                                var curTile = new Tile();
                                curTile.IsActive = reader.ReadBoolean();

                                if (curTile.IsActive)
                                {
                                    curTile.Type = reader.ReadByte();
                                    if (curTile.Type == 19) // fix for platforms
                                    {
                                        curTile.U = 0;
                                        curTile.V = 0;
                                    }
                                    else if (World.TileProperties[curTile.Type].IsFramed)
                                    {
                                        curTile.U = reader.ReadInt16();
                                        curTile.V = reader.ReadInt16();

                                        if (curTile.Type == 144) //timer
                                            curTile.V = 0;
                                    }
                                    else
                                    {
                                        curTile.U = -1;
                                        curTile.V = -1;
                                    }
                                }

                                if (reader.ReadBoolean())
                                    curTile.Wall = reader.ReadByte();

                                if (reader.ReadBoolean())
                                {
                                    curTile.Liquid = reader.ReadByte();
                                    curTile.IsLava = reader.ReadBoolean();
                                }

                                curTile.HasWire = reader.ReadBoolean();
                                buffer.Tiles[x, y] = curTile;
                            }

                        }

                    }
                    catch (Exception)
                    {
                        for (int x = 0; x < buffer.Size.X; x++)
                        {
                            for (int y = 0; y < buffer.Size.Y; y++)
                            {
                                if (buffer.Tiles[x, y] == null)
                                    buffer.Tiles[x, y] = new Tile();
                            }
                        }
                        return buffer;
                    }

                    for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            var chest = new Chest();
                            chest.X = reader.ReadInt32();
                            chest.Y = reader.ReadInt32();

                            for (int slot = 0; slot < 20; slot++)
                            {
                                byte stackSize = reader.ReadByte();
                                if (stackSize > 0)
                                {
                                    string itemName = reader.ReadString();
                                    chest.Items[slot].SetFromName(itemName);
                                    chest.Items[slot].StackSize = stackSize;
                                }
                            }

                            //Chests[chestIndex] = chest;
                            buffer.Chests.Add(chest);
                        }
                    }
                    for (int signIndex = 0; signIndex < 1000; signIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            string signText = reader.ReadString();
                            int x = reader.ReadInt32();
                            int y = reader.ReadInt32();
                            if (buffer.Tiles[x, y].IsActive && (buffer.Tiles[x, y].Type == 55 || buffer.Tiles[x, y].Type == 85))
                            // validate tile location
                            {
                                var sign = new Sign(x, y, signText);
                                //Signs[signIndex] = sign;
                                buffer.Signs.Add(sign);
                            }
                        }
                    }
                    string checkName = reader.ReadString();
                    int checkversion = reader.ReadInt32();
                    int checkx = reader.ReadInt32();
                    int checky = reader.ReadInt32();

                    if (checkName != buffer.Name || checkversion != version || checkx != maxx || checky != maxy)
                        System.Windows.MessageBox.Show("Verification failed. Some schematic data may be missing.", "Legacy Schematic Version");

                    return buffer;

                }
            }
            #pragma warning disable 162
            return null;
            #pragma warning restore
        }
        private static ClipboardBuffer Load5(BinaryReader b, string name, uint tVersion, int version)
        {
            int sizeX  = b.ReadInt32();
            int sizeY  = b.ReadInt32();
            var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));

            buffer.Name = name;

            for (int x = 0; x < sizeX; ++x)
            {
                for (int y = 0; y < sizeY; y++)
                {
                    var tile = World.ReadTileDataFromStreamV1(b, tVersion);
                    // read complete, start compression
                    buffer.Tiles[x, y] = tile;

                    int rle = b.ReadInt16();
                    if (rle < 0)
                    {
                        throw new ApplicationException("Invalid Tile Data!");
                    }

                    if (rle > 0)
                    {
                        for (int k = y + 1; k < y + rle + 1; k++)
                        {
                            var tcopy = (Tile)tile.Clone();
                            buffer.Tiles[x, k] = tcopy;
                        }
                        y = y + rle;
                    }
                }
            }
            buffer.Chests.Clear();
            buffer.Chests.AddRange(World.ReadChestDataFromStreamV1(b, tVersion));

            buffer.Signs.Clear();
            foreach (Sign sign in World.ReadSignDataFromStreamV1(b))
            {
                if (buffer.Tiles[sign.X, sign.Y].IsActive && (int)buffer.Tiles[sign.X, sign.Y].Type == 55 &&
                    (int)buffer.Tiles[sign.X, sign.Y].Type == 85)
                {
                    buffer.Signs.Add(sign);
                }
            }

            string verifyName    = b.ReadString();
            int    verifyVersion = b.ReadInt32();
            int    verifyX       = b.ReadInt32();
            int    verifyY       = b.ReadInt32();

            if (buffer.Name == verifyName &&
                version == verifyVersion &&
                buffer.Size.X == verifyX &&
                buffer.Size.Y == verifyY)
            {
                // valid;
                return(buffer);
            }
            b.Close();
            return(null);
        }
示例#10
0
        public ClipboardBuffer GetSelectionBuffer()
        {
            World world = _wvm.CurrentWorld;

            XNA.Rectangle area   = _wvm.Selection.SelectionArea;
            var           buffer = new ClipboardBuffer(new Vector2Int32(area.Width, area.Height));

            for (int x = 0; x < area.Width; x++)
            {
                for (int y = 0; y < area.Height; y++)
                {
                    Tile curTile = (Tile)world.Tiles[x + area.X, y + area.Y].Clone();

                    if (Tile.IsChest(curTile.Type))
                    {
                        if (buffer.GetChestAtTile(x, y, curTile.Type) == null)
                        {
                            var anchor = world.GetAnchor(x + area.X, y + area.Y);
                            if (anchor.X == x + area.X && anchor.Y == y + area.Y)
                            {
                                var data = world.GetChestAtTile(x + area.X, y + area.Y);
                                if (data != null)
                                {
                                    var newChest = data.Copy();
                                    newChest.X = x;
                                    newChest.Y = y;
                                    buffer.Chests.Add(newChest);
                                }
                            }
                        }
                    }
                    if (Tile.IsSign(curTile.Type))
                    {
                        if (buffer.GetSignAtTile(x, y) == null)
                        {
                            var anchor = world.GetAnchor(x + area.X, y + area.Y);
                            if (anchor.X == x + area.X && anchor.Y == y + area.Y)
                            {
                                var data = world.GetSignAtTile(x + area.X, y + area.Y);
                                if (data != null)
                                {
                                    var newSign = data.Copy();
                                    newSign.X = x;
                                    newSign.Y = y;
                                    buffer.Signs.Add(newSign);
                                }
                            }
                        }
                    }
                    if (Tile.IsTileEntity(curTile.Type))
                    {
                        if (buffer.GetTileEntityAtTile(x, y) == null)
                        {
                            var anchor = world.GetAnchor(x + area.X, y + area.Y);
                            if (anchor.X == x + area.X && anchor.Y == y + area.Y)
                            {
                                var data = world.GetTileEntityAtTile(x + area.X, y + area.Y);
                                if (data != null)
                                {
                                    var newEntity = data.Copy();
                                    newEntity.PosX = (short)x;
                                    newEntity.PosY = (short)y;
                                    buffer.TileEntities.Add(newEntity);
                                }
                            }
                        }
                    }
                    buffer.Tiles[x, y] = curTile;
                }
            }

            buffer.RenderBuffer();
            return(buffer);
        }
        public static ClipboardBuffer Load(string filename)
        {
            string ext = Path.GetExtension(filename);

            if (string.Equals(ext, ".jpg", StringComparison.InvariantCultureIgnoreCase) || string.Equals(ext, ".png", StringComparison.InvariantCultureIgnoreCase) || string.Equals(ext, ".bmp", StringComparison.InvariantCultureIgnoreCase))
            {
                return(LoadFromImage(filename));
            }

            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var br = new BinaryReader(stream))
                {
                    string name    = br.ReadString();
                    int    version = br.ReadInt32();

                    if (name != Path.GetFileNameWithoutExtension(filename))
                    {
                        br.Close();
                        stream.Close();
                        return(LoadOld(filename));
                    }

                    int sizeX  = br.ReadInt32();
                    int sizeY  = br.ReadInt32();
                    var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
                    buffer.Name = name;

                    for (int x = 0; x < sizeX; x++)
                    {
                        for (int y = 0; y < sizeY; y++)
                        {
                            var curTile = new Tile();
                            curTile.IsActive = br.ReadBoolean();

                            if (curTile.IsActive)
                            {
                                curTile.Type = br.ReadByte();
                                if (World.TileProperties[curTile.Type].IsFramed)
                                {
                                    curTile.U = br.ReadInt16();
                                    curTile.V = br.ReadInt16();
                                }
                            }

                            if (br.ReadBoolean())
                            {
                                curTile.Wall = br.ReadByte();
                            }

                            if (br.ReadBoolean())
                            {
                                curTile.Liquid = br.ReadByte();
                                curTile.IsLava = br.ReadBoolean();
                            }

                            curTile.HasWire    = br.ReadBoolean();
                            buffer.Tiles[x, y] = curTile;
                        }
                    }
                    for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                    {
                        if (br.ReadBoolean())
                        {
                            var curChest = new Chest(br.ReadInt32(), br.ReadInt32());
                            for (int j = 0; j < Chest.MaxItems; ++j)
                            {
                                curChest.Items[j].StackSize = br.ReadByte();

                                if (curChest.Items[j].StackSize > 0)
                                {
                                    if (version >= 3)
                                    {
                                        curChest.Items[j].NetId = br.ReadInt32();
                                    }
                                    else
                                    {
                                        curChest.Items[j].SetFromName(br.ReadString());
                                    }
                                    curChest.Items[j].Prefix = br.ReadByte();
                                }
                                else
                                {
                                    curChest.Items[j].SetFromName("[empty]");
                                }
                            }
                            buffer.Chests.Add(curChest);
                        }
                    }
                    for (int signIndex = 0; signIndex < 1000; signIndex++)
                    {
                        if (br.ReadBoolean())
                        {
                            string text = br.ReadString();
                            int    x    = br.ReadInt32();
                            int    y    = br.ReadInt32();
                            buffer.Signs.Add(new Sign(x, y, text));
                        }
                    }

                    if (buffer.Name == br.ReadString() &&
                        version == br.ReadInt32() &&
                        buffer.Size.X == br.ReadInt32() &&
                        buffer.Size.Y == br.ReadInt32())
                    {
                        // valid;
                        return(buffer);
                    }
                    br.Close();
                    return(null);
                }
            }
        }
        private static ClipboardBuffer LoadV2(BinaryReader b, string name, uint tVersion, int version)
        {
            int sizeX = b.ReadInt32();
            int sizeY = b.ReadInt32();
            var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
            buffer.Name = name;

            buffer.Tiles = World.LoadTileData(b, sizeX, sizeY);
            buffer.Chests.AddRange(World.LoadChestData(b));
            buffer.Signs.AddRange(World.LoadSignData(b));

            string verifyName = b.ReadString();
            int verifyVersion = b.ReadInt32();
            int verifyX = b.ReadInt32();
            int verifyY = b.ReadInt32();
            if (buffer.Name == verifyName &&
                version == verifyVersion &&
                buffer.Size.X == verifyX &&
                buffer.Size.Y == verifyY)
            {
                // valid;
                return buffer;
            }
            b.Close();

            return buffer;
        }
        private static ClipboardBuffer Load5(BinaryReader b, string name, uint tVersion, int version)
        {
            int sizeX = b.ReadInt32();
            int sizeY = b.ReadInt32();
            var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
            buffer.Name = name;

            for (int x = 0; x < sizeX; ++x)
            {
                for (int y = 0; y < sizeY; y++)
                {
                    var tile = World.ReadTileDataFromStreamV1(b, tVersion);
                    // read complete, start compression
                    buffer.Tiles[x, y] = tile;

                    int rle = b.ReadInt16();
                    if (rle < 0)
                        throw new ApplicationException("Invalid Tile Data!");

                    if (rle > 0)
                    {
                        for (int k = y + 1; k < y + rle + 1; k++)
                        {
                            var tcopy = (Tile) tile.Clone();
                            buffer.Tiles[x, k] = tcopy;
                        }
                        y = y + rle;
                    }
                }
            }
            buffer.Chests.Clear();
            buffer.Chests.AddRange(World.ReadChestDataFromStreamV1(b, tVersion));

            buffer.Signs.Clear();
            foreach (Sign sign in World.ReadSignDataFromStreamV1(b))
            {
                if (buffer.Tiles[sign.X, sign.Y].IsActive && (int) buffer.Tiles[sign.X, sign.Y].Type == 55 &&
                    (int) buffer.Tiles[sign.X, sign.Y].Type == 85)
                    buffer.Signs.Add(sign);
            }

            string verifyName = b.ReadString();
            int verifyVersion = b.ReadInt32();
            int verifyX = b.ReadInt32();
            int verifyY = b.ReadInt32();
            if (buffer.Name == verifyName &&
                version == verifyVersion &&
                buffer.Size.X == verifyX &&
                buffer.Size.Y == verifyY)
            {
                // valid;
                return buffer;
            }
            b.Close();
            return null;
        }
示例#14
0
        public void PasteBufferIntoWorld(Vector2Int32 anchor)
        {
            if (Buffer == null)
            {
                return;
            }
            World           world  = _wvm.CurrentWorld;
            ClipboardBuffer buffer = _wvm.Clipboard.Buffer;

            for (int x = 0; x < buffer.Size.X; x++)
            {
                for (int y = 0; y < buffer.Size.Y; y++)
                {
                    int worldX = x + anchor.X;
                    int worldY = y + anchor.Y;

                    if (world.ValidTileLocation(new Vector2Int32(x + anchor.X, y + anchor.Y)))
                    {
                        //HistMan.AddTileToBuffer(x + anchor.X, y + anchor.Y, ref world.Tiles[x + anchor.X, y + anchor.Y]);

                        Tile curTile;

                        if (PasteTiles)
                        {
                            curTile       = (Tile)buffer.Tiles[x, y].Clone();
                            curTile.Color = buffer.Tiles[x, y].Color;
                        }
                        else
                        {
                            // if pasting tiles is disabled, use the existing tile with buffer's wall & extras
                            curTile           = (Tile)world.Tiles[worldX, worldY].Clone();
                            curTile.Wall      = buffer.Tiles[x, y].Wall;
                            curTile.WallColor = buffer.Tiles[x, y].WallColor;
                            curTile.Liquid    = buffer.Tiles[x, y].Liquid;
                            curTile.IsLava    = buffer.Tiles[x, y].IsLava;
                            curTile.IsHoney   = buffer.Tiles[x, y].IsHoney;
                            curTile.HasWire   = buffer.Tiles[x, y].HasWire;
                            curTile.HasWire2  = buffer.Tiles[x, y].HasWire2;
                            curTile.HasWire3  = buffer.Tiles[x, y].HasWire3;
                            curTile.Actuator  = buffer.Tiles[x, y].Actuator;
                            curTile.InActive  = buffer.Tiles[x, y].InActive;
                        }

                        if (!PasteEmpty && (curTile.Liquid == 0 && !curTile.IsActive && curTile.Wall == 0 && !curTile.HasWire))
                        {
                            // skip tiles that are empty if paste empty is not true
                            continue;
                        }
                        if (!PasteWalls)
                        {
                            // if pasting walls is disabled, use the existing wall
                            curTile.Wall      = world.Tiles[worldX, worldY].Wall;
                            curTile.WallColor = world.Tiles[worldX, worldY].WallColor;
                        }
                        if (!PasteLiquids)
                        {
                            // if pasting liquids is disabled, use any existing liquid
                            curTile.Liquid  = world.Tiles[worldX, worldY].Liquid;
                            curTile.IsLava  = world.Tiles[worldX, worldY].IsLava;
                            curTile.IsHoney = world.Tiles[worldX, worldY].IsHoney;
                        }
                        if (!PasteWires)
                        {
                            // if pasting wires is disabled, use any existing wire
                            curTile.HasWire  = buffer.Tiles[x, y].HasWire;
                            curTile.HasWire2 = buffer.Tiles[x, y].HasWire2;
                            curTile.HasWire3 = buffer.Tiles[x, y].HasWire3;
                            curTile.Actuator = buffer.Tiles[x, y].Actuator;
                            curTile.InActive = buffer.Tiles[x, y].InActive;
                        }
                        //  Update chest/sign data only if we've pasted tiles
                        if (PasteTiles)
                        {
                            // Remove overwritten chests data
                            if (world.Tiles[x + anchor.X, y + anchor.Y].Type == 21)
                            {
                                var data = world.GetChestAtTile(x + anchor.X, y + anchor.Y);
                                if (data != null)
                                {
                                    _wvm.UndoManager.Buffer.Chests.Add(data);
                                    world.Chests.Remove(data);
                                }
                            }

                            // Remove overwritten sign data
                            if (world.Tiles[x + anchor.X, y + anchor.Y].Type == 55 || world.Tiles[x + anchor.X, y + anchor.Y].Type == 85)
                            {
                                var data = world.GetSignAtTile(x + anchor.X, y + anchor.Y);
                                if (data != null)
                                {
                                    _wvm.UndoManager.Buffer.Signs.Add(data);
                                    world.Signs.Remove(data);
                                }
                            }


                            // Add new chest data
                            if (curTile.Type == 21)
                            {
                                if (world.GetChestAtTile(x + anchor.X, y + anchor.Y) == null)
                                {
                                    var data = buffer.GetChestAtTile(x, y);
                                    if (data != null) // allow? chest copying may not work...
                                    {
                                        // Copied chest
                                        var newChest = data.Copy();
                                        newChest.X = x + anchor.X;
                                        newChest.Y = y + anchor.Y;
                                        world.Chests.Add(newChest);
                                    }
                                    else
                                    {
                                        // Empty chest
                                        world.Chests.Add(new Chest(x + anchor.X, y + anchor.Y));
                                    }
                                }
                            }

                            // Add new sign data
                            if (curTile.Type == 55 || curTile.Type == 85)
                            {
                                if (world.GetSignAtTile(x + anchor.X, y + anchor.Y) == null)
                                {
                                    var data = buffer.GetSignAtTile(x, y);
                                    if (data != null)
                                    {
                                        // Copied sign
                                        var newSign = data.Copy();
                                        newSign.X = x + anchor.X;
                                        newSign.Y = y + anchor.Y;
                                        world.Signs.Add(newSign);
                                    }
                                    else
                                    {
                                        world.Signs.Add(new Sign(x + anchor.X, y + anchor.Y, string.Empty));
                                    }
                                }
                            }
                        }
                        _wvm.UndoManager.SaveTile(x + anchor.X, y + anchor.Y);
                        world.Tiles[x + anchor.X, y + anchor.Y] = curTile;
                    }
                }
            }
            _wvm.UndoManager.SaveUndo();

            /* Heathtech */
            BlendRules.ResetUVCache(_wvm, anchor.X, anchor.Y, buffer.Size.X, buffer.Size.Y);
        }
示例#15
0
        // Reverse the buffer along the x- or y- axis
        public void Flip(ClipboardBuffer buffer, bool flipX)
        {
            ClipboardBuffer flippedBuffer = new ClipboardBuffer(buffer.Size);

            for (int x = 0, maxX = buffer.Size.X - 1; x <= maxX; x++)
            {
                for (int y = 0, maxY = buffer.Size.Y - 1; y <= maxY; y++)
                {
                    int bufferX;
                    int bufferY;

                    if (flipX)
                    {
                        bufferX = maxX - x;
                        bufferY = y;
                    }
                    else
                    {
                        bufferX = x;
                        bufferY = maxY - y;
                    }

                    Tile tile = (Tile)buffer.Tiles[x, y].Clone();

                    Vector2Short tileSize = World.TileProperties[tile.Type].FrameSize;

                    if (flipX)
                    {
                        //  Ignore multi-width objects when flipping on x-axis
                        if (tileSize.X > 1)
                        {
                            ClearTile(tile);
                        }
                        // Flip brick-style
                        switch (tile.BrickStyle)
                        {
                        case BrickStyle.SlopeTopRight:
                            tile.BrickStyle = BrickStyle.SlopeTopLeft;
                            break;

                        case BrickStyle.SlopeTopLeft:
                            tile.BrickStyle = BrickStyle.SlopeTopRight;
                            break;

                        case BrickStyle.SlopeBottomRight:
                            tile.BrickStyle = BrickStyle.SlopeBottomLeft;
                            break;

                        case BrickStyle.SlopeBottomLeft:
                            tile.BrickStyle = BrickStyle.SlopeBottomRight;
                            break;
                        }
                    }

                    else
                    {
                        //  Ignore multi-height tiles when flipping on y-axis
                        if (tileSize.Y > 1)
                        {
                            ClearTile(tile);
                        }
                        // Flip brick-style
                        switch (tile.BrickStyle)
                        {
                        case BrickStyle.SlopeTopRight:
                            tile.BrickStyle = BrickStyle.SlopeBottomRight;
                            break;

                        case BrickStyle.SlopeTopLeft:
                            tile.BrickStyle = BrickStyle.SlopeBottomLeft;
                            break;

                        case BrickStyle.SlopeBottomRight:
                            tile.BrickStyle = BrickStyle.SlopeTopRight;
                            break;

                        case BrickStyle.SlopeBottomLeft:
                            tile.BrickStyle = BrickStyle.SlopeTopLeft;
                            break;
                        }
                    }

                    flippedBuffer.Tiles[bufferX, bufferY] = (Tile)tile;
                }
            }

            // Replace the existing buffer with the new one
            int bufferIndex = LoadedBuffers.IndexOf(buffer);

            if (bufferIndex > -1)
            {
                LoadedBuffers.Insert(bufferIndex, flippedBuffer);
                LoadedBuffers.RemoveAt(bufferIndex + 1);
            }

            flippedBuffer.RenderBuffer();

            if (Buffer == buffer)
            {
                Buffer = flippedBuffer;
                _wvm.PreviewChange();
            }
        }
示例#16
0
        public static ClipboardBuffer LoadOld(string filename)
        {
            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream))
                {
                    string name    = reader.ReadString();
                    int    version = reader.ReadInt32();
                    int    maxx    = reader.ReadInt32();
                    int    maxy    = reader.ReadInt32();

                    var buffer = new ClipboardBuffer(new Vector2Int32(maxx, maxy));

                    buffer.Name = string.IsNullOrWhiteSpace(name) ? Path.GetFileNameWithoutExtension(filename) : name;

                    try
                    {
                        for (int x = 0; x < buffer.Size.X; x++)
                        {
                            for (int y = 0; y < buffer.Size.Y; y++)
                            {
                                var tile = new Tile();

                                tile.IsActive = reader.ReadBoolean();

                                if (tile.IsActive)
                                {
                                    tile.Type = reader.ReadByte();

                                    if (tile.Type == 19)
                                    {
                                        tile.U = 0;
                                        tile.V = 0;
                                    }
                                    else if (World.TileProperties[tile.Type].IsFramed)
                                    {
                                        tile.U = reader.ReadInt16();
                                        tile.V = reader.ReadInt16();
                                    }
                                    else
                                    {
                                        tile.U = -1;
                                        tile.V = -1;
                                    }
                                }

                                // trash old lighted value
                                reader.ReadBoolean();

                                if (reader.ReadBoolean())
                                {
                                    tile.Wall = reader.ReadByte();
                                }

                                if (reader.ReadBoolean())
                                {
                                    tile.Liquid = reader.ReadByte();
                                    tile.IsLava = reader.ReadBoolean();
                                }

                                buffer.Tiles[x, y] = tile;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        for (int x = 0; x < buffer.Size.X; x++)
                        {
                            for (int y = 0; y < buffer.Size.Y; y++)
                            {
                                if (buffer.Tiles[x, y] == null)
                                {
                                    buffer.Tiles[x, y] = new Tile();
                                }
                            }
                        }
                        return(buffer);
                    }

                    for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            var chest = new Chest();
                            chest.X = reader.ReadInt32();
                            chest.Y = reader.ReadInt32();

                            for (int slot = 0; slot < 20; slot++)
                            {
                                byte stackSize = reader.ReadByte();
                                if (stackSize > 0)
                                {
                                    string itemName = reader.ReadString();
                                    chest.Items[slot].SetFromName(itemName);
                                    chest.Items[slot].StackSize = stackSize;
                                }
                            }

                            //Chests[chestIndex] = chest;
                            buffer.Chests.Add(chest);
                        }
                    }
                    for (int signIndex = 0; signIndex < 1000; signIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            string signText = reader.ReadString();
                            int    x        = reader.ReadInt32();
                            int    y        = reader.ReadInt32();
                            if (buffer.Tiles[x, y].IsActive && (buffer.Tiles[x, y].Type == 55 || buffer.Tiles[x, y].Type == 85))
                            // validate tile location
                            {
                                var sign = new Sign(x, y, signText);
                                //Signs[signIndex] = sign;
                                buffer.Signs.Add(sign);
                            }
                        }
                    }

                    int checkx = reader.ReadInt32();
                    int checky = reader.ReadInt32();

                    if (checkx == maxx && checky == maxy)
                    {
                        return(buffer);
                    }
                }
            }

            return(null);
        }
示例#17
0
 public void FlipX(ClipboardBuffer buffer)
 {
     Flip(buffer, true);
 }
        public static ClipboardBuffer Load3(string filename, bool frame19 = false)
        {
            bool failed = false;
            try
            {
                using (var stream = new FileStream(filename, FileMode.Open))
                {
                    using (var br = new BinaryReader(stream))
                    {
                        string name = br.ReadString();
                        int version = br.ReadInt32();

                        int sizeX = br.ReadInt32();
                        int sizeY = br.ReadInt32();
                        var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
                        buffer.Name = name;

                        for (int x = 0; x < sizeX; x++)
                        {
                            for (int y = 0; y < sizeY; y++)
                            {
                                var curTile = new Tile();
                                curTile.IsActive = br.ReadBoolean();

                                if (curTile.IsActive)
                                {
                                    curTile.Type = br.ReadByte();
                                    if (curTile.Type == 19) // fix for platforms
                                    {

                                        curTile.U = 0;
                                        curTile.V = 0;
                                        if (frame19)
                                        {
                                            curTile.U = br.ReadInt16();
                                            curTile.V = br.ReadInt16();
                                        }
                                    }
                                    else if (World.TileProperties[curTile.Type].IsFramed)
                                    {
                                        curTile.U = br.ReadInt16();
                                        curTile.V = br.ReadInt16();

                                        if (curTile.Type == 144) //timer
                                            curTile.V = 0;
                                    }
                                    else
                                    {
                                        curTile.U = -1;
                                        curTile.V = -1;
                                    }
                                }

                                if (br.ReadBoolean())
                                    curTile.Wall = br.ReadByte();

                                if (br.ReadBoolean())
                                {
                                    curTile.Liquid = br.ReadByte();
                                    curTile.IsLava = br.ReadBoolean();
                                }

                                curTile.HasWire = br.ReadBoolean();
                                buffer.Tiles[x, y] = curTile;
                            }
                        }
                        for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                        {
                            if (br.ReadBoolean())
                            {
                                var curChest = new Chest(br.ReadInt32(), br.ReadInt32());
                                for (int j = 0; j < 20; ++j)
                                {
                                    curChest.Items[j].StackSize = br.ReadByte();

                                    if (curChest.Items[j].StackSize > 0)
                                    {
                                        if (version >= 3)
                                            curChest.Items[j].NetId = br.ReadInt32();
                                        else
                                            curChest.Items[j].SetFromName(br.ReadString());
                                        curChest.Items[j].Prefix = br.ReadByte();
                                    }
                                    else
                                    {
                                        curChest.Items[j].SetFromName("[empty]");
                                    }

                                }
                                buffer.Chests.Add(curChest);
                            }
                        }
                        for (int signIndex = 0; signIndex < 1000; signIndex++)
                        {
                            if (br.ReadBoolean())
                            {
                                string text = br.ReadString();
                                int x = br.ReadInt32();
                                int y = br.ReadInt32();
                                buffer.Signs.Add(new Sign(x, y, text));
                            }
                        }

                        if (buffer.Name != br.ReadString() || version != br.ReadInt32() || buffer.Size.X != br.ReadInt32() || buffer.Size.Y != br.ReadInt32())
                        {
                            if (!frame19)
                            {
                                br.Close();
                                return Load3(filename, true);
                            }
                            else
                                System.Windows.MessageBox.Show("Verification failed. Some schematic data may be missing.", "Legacy Schematic Version");
                        }

                        br.Close();
                        return buffer;
                    }
                }
            }
            catch (Exception)
            {
                failed = true;
            }

            if (failed && !frame19)
            {
                return Load3(filename, true);
            }

            return null;
        }
示例#19
0
 public void FlipY(ClipboardBuffer buffer)
 {
     Flip(buffer, false);
 }
        public static ClipboardBuffer LoadFromImage(string filename)
        {
            var urifrompath = new Uri(filename);
            var bmp = new BitmapImage(urifrompath);
            if (bmp.Width > 8400)
                return null;
            if (bmp.Height > 2400)
                return null;

            string name = Path.GetFileNameWithoutExtension(filename);
            var buffer = new ClipboardBuffer(new Vector2Int32(bmp.PixelWidth, bmp.PixelHeight));
            buffer.Name = name;

            var wbmp = new WriteableBitmap(bmp);
            if (wbmp.Format.BitsPerPixel < 32)
                return null;
            wbmp.Lock();
            unsafe
            {
                var pixels = (int*)wbmp.BackBuffer;
                for (int y = 0; y < bmp.PixelHeight; y++)
                {
                    int row = y * bmp.PixelWidth;
                    for (int x = 0; x < bmp.PixelWidth; x++)
                    {

                        buffer.Tiles[x, y] = TileFromColor(pixels[x + row]);
                    }
                }

            }
            wbmp.Unlock();

            return buffer;
        }
        public static ClipboardBuffer Load(string filename)
        {
            string ext = Path.GetExtension(filename);
            if (string.Equals(ext, ".jpg", StringComparison.InvariantCultureIgnoreCase) || string.Equals(ext, ".png", StringComparison.InvariantCultureIgnoreCase) || string.Equals(ext, ".bmp", StringComparison.InvariantCultureIgnoreCase))
                return LoadFromImage(filename);

            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var br = new BinaryReader(stream))
                {
                    string name = br.ReadString();
                    int version = br.ReadInt32();

                    if (name != Path.GetFileNameWithoutExtension(filename))
                    {
                        br.Close();
                        stream.Close();
                        return LoadOld(filename);
                    }

                    int sizeX = br.ReadInt32();
                    int sizeY = br.ReadInt32();
                    var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
                    buffer.Name = name;

                    for (int x = 0; x < sizeX; x++)
                    {
                        for (int y = 0; y < sizeY; y++)
                        {
                            var curTile = new Tile();
                            curTile.IsActive = br.ReadBoolean();

                            if (curTile.IsActive)
                            {
                                curTile.Type = br.ReadByte();
                                if (World.TileProperties[curTile.Type].IsFramed)
                                {
                                    curTile.U = br.ReadInt16();
                                    curTile.V = br.ReadInt16();
                                }
                            }

                            if (br.ReadBoolean())
                                curTile.Wall = br.ReadByte();

                            if (br.ReadBoolean())
                            {
                                curTile.Liquid = br.ReadByte();
                                curTile.IsLava = br.ReadBoolean();
                            }

                            curTile.HasWire = br.ReadBoolean();
                            buffer.Tiles[x, y] = curTile;
                        }
                    }
                    for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                    {
                        if (br.ReadBoolean())
                        {
                            var curChest = new Chest(br.ReadInt32(), br.ReadInt32());
                            for (int j = 0; j < Chest.MaxItems; ++j)
                            {
                                curChest.Items[j].StackSize = br.ReadByte();

                                if (curChest.Items[j].StackSize > 0)
                                {
                                    if (version >= 3)
                                        curChest.Items[j].NetId = br.ReadInt32();
                                    else
                                        curChest.Items[j].SetFromName(br.ReadString());
                                    curChest.Items[j].Prefix = br.ReadByte();
                                }
                                else
                                {
                                    curChest.Items[j].SetFromName("[empty]");
                                }

                            }
                            buffer.Chests.Add(curChest);
                        }
                    }
                    for (int signIndex = 0; signIndex < 1000; signIndex++)
                    {
                        if (br.ReadBoolean())
                        {
                            string text = br.ReadString();
                            int x = br.ReadInt32();
                            int y = br.ReadInt32();
                            buffer.Signs.Add(new Sign(x, y, text));
                        }
                    }

                    if (buffer.Name == br.ReadString() &&
                        version == br.ReadInt32() &&
                        buffer.Size.X == br.ReadInt32() &&
                        buffer.Size.Y == br.ReadInt32())
                    {
                        // valid;
                        return buffer;
                    }
                    br.Close();
                    return null;
                }
            }
        }
        public static ClipboardBuffer Load(string filename)
        {
            string ext = Path.GetExtension(filename);
            if (string.Equals(ext, ".jpg", StringComparison.InvariantCultureIgnoreCase) || string.Equals(ext, ".png", StringComparison.InvariantCultureIgnoreCase) || string.Equals(ext, ".bmp", StringComparison.InvariantCultureIgnoreCase))
                return LoadFromImage(filename);

            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var b = new BinaryReader(stream))
                {
                    string name = b.ReadString();
                    int version = b.ReadInt32();
                    uint tVersion = (uint)version;

                    if (version == 4)
                    {
                        b.Close();
                        stream.Close();
                        return Load4(filename);
                    }
                    else if (version == 3)
                    {
                        b.Close();
                        stream.Close();
                        return Load3(filename);
                    }
                    else if (version == 2)
                    {
                        b.Close();
                        stream.Close();
                        return Load2(filename);
                    }
                    else if (version < 2)
                    {
                        b.Close();
                        stream.Close();
                        return LoadOld(filename);
                    }

                    int sizeX = b.ReadInt32();
                    int sizeY = b.ReadInt32();
                    var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
                    buffer.Name = name;

                    for (int x = 0; x < sizeX; ++x)
                    {
                        for (int y = 0; y < sizeY; y++)
                        {
                            var tile = World.ReadTileDataFromStream(b, tVersion);
                            // read complete, start compression
                            buffer.Tiles[x, y] = tile;

                            int rle = b.ReadInt16();
                            if (rle < 0)
                                throw new ApplicationException("Invalid Tile Data!");

                            if (rle > 0)
                            {
                                for (int k = y + 1; k < y + rle + 1; k++)
                                {
                                    var tcopy = (Tile)tile.Clone();
                                    buffer.Tiles[x, k] = tcopy;
                                }
                                y = y + rle;
                            }
                        }
                    }
                    buffer.Chests.Clear();
                    buffer.Chests.AddRange(World.ReadChestDataFromStream(b, tVersion));

                    buffer.Signs.Clear();
                    foreach (Sign sign in World.ReadSignDataFromStream(b))
                    {
                        if (buffer.Tiles[sign.X, sign.Y].IsActive && (int)buffer.Tiles[sign.X, sign.Y].Type == 55 && (int)buffer.Tiles[sign.X, sign.Y].Type == 85)
                            buffer.Signs.Add(sign);
                    }

                    string verifyName = b.ReadString();
                    int verifyVersion = b.ReadInt32();
                    int verifyX = b.ReadInt32();
                    int verifyY = b.ReadInt32();
                    if (buffer.Name == verifyName &&
                        version == verifyVersion &&
                        buffer.Size.X == verifyX &&
                        buffer.Size.Y == verifyY)
                    {
                        // valid;
                        return buffer;
                    }
                    b.Close();
                    return null;
                }
            }
        }
示例#23
0
        public static ClipboardBuffer Load(string filename)
        {
            string ext = Path.GetExtension(filename);

            if (string.Equals(ext, ".jpg", StringComparison.InvariantCultureIgnoreCase) || string.Equals(ext, ".png", StringComparison.InvariantCultureIgnoreCase) || string.Equals(ext, ".bmp", StringComparison.InvariantCultureIgnoreCase))
            {
                return(LoadFromImage(filename));
            }

            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var b = new BinaryReader(stream))
                {
                    string name     = b.ReadString();
                    int    version  = b.ReadInt32();
                    uint   tVersion = (uint)version;

                    if (version == 4)
                    {
                        b.Close();
                        stream.Close();
                        return(Load4(filename));
                    }
                    else if (version == 3)
                    {
                        b.Close();
                        stream.Close();
                        return(Load3(filename));
                    }
                    else if (version == 2)
                    {
                        b.Close();
                        stream.Close();
                        return(Load2(filename));
                    }
                    else if (version < 2)
                    {
                        b.Close();
                        stream.Close();
                        return(LoadOld(filename));
                    }


                    int sizeX  = b.ReadInt32();
                    int sizeY  = b.ReadInt32();
                    var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
                    buffer.Name = name;

                    for (int x = 0; x < sizeX; ++x)
                    {
                        for (int y = 0; y < sizeY; y++)
                        {
                            var tile = World.ReadTileDataFromStream(b, tVersion);
                            // read complete, start compression
                            buffer.Tiles[x, y] = tile;

                            int rle = b.ReadInt16();
                            if (rle < 0)
                            {
                                throw new ApplicationException("Invalid Tile Data!");
                            }

                            if (rle > 0)
                            {
                                for (int k = y + 1; k < y + rle + 1; k++)
                                {
                                    var tcopy = (Tile)tile.Clone();
                                    buffer.Tiles[x, k] = tcopy;
                                }
                                y = y + rle;
                            }
                        }
                    }
                    buffer.Chests.Clear();
                    buffer.Chests.AddRange(World.ReadChestDataFromStream(b, tVersion));

                    buffer.Signs.Clear();
                    foreach (Sign sign in World.ReadSignDataFromStream(b))
                    {
                        if (buffer.Tiles[sign.X, sign.Y].IsActive && (int)buffer.Tiles[sign.X, sign.Y].Type == 55 && (int)buffer.Tiles[sign.X, sign.Y].Type == 85)
                        {
                            buffer.Signs.Add(sign);
                        }
                    }

                    string verifyName    = b.ReadString();
                    int    verifyVersion = b.ReadInt32();
                    int    verifyX       = b.ReadInt32();
                    int    verifyY       = b.ReadInt32();
                    if (buffer.Name == verifyName &&
                        version == verifyVersion &&
                        buffer.Size.X == verifyX &&
                        buffer.Size.Y == verifyY)
                    {
                        // valid;
                        return(buffer);
                    }
                    b.Close();
                    return(null);
                }
            }
        }
示例#24
0
        public void PasteBufferIntoWorld(Vector2Int32 anchor)
        {
            if (Buffer == null)
            {
                return;
            }
            World           world  = _wvm.CurrentWorld;
            ClipboardBuffer buffer = _wvm.Clipboard.Buffer;

            for (int x = 0; x < buffer.Size.X; x++)
            {
                for (int y = 0; y < buffer.Size.Y; y++)
                {
                    if (world.ValidTileLocation(new Vector2Int32(x + anchor.X, y + anchor.Y)))
                    {
                        //HistMan.AddTileToBuffer(x + anchor.X, y + anchor.Y, ref world.Tiles[x + anchor.X, y + anchor.Y]);

                        Tile curTile = (Tile)buffer.Tiles[x, y].Clone();

                        if (!PasteEmpty && (curTile.Liquid == 0 && !curTile.IsActive && curTile.Wall == 0 && !curTile.HasWire))
                        {
                            // skip tiles that are empty if paste empty is not true
                            continue;
                        }

                        // Remove overwritten chests data
                        if (world.Tiles[x + anchor.X, y + anchor.Y].Type == 21)
                        {
                            var data = world.GetChestAtTile(x + anchor.X, y + anchor.Y);
                            if (data != null)
                            {
                                _wvm.UndoManager.Buffer.Chests.Add(data);
                                world.Chests.Remove(data);
                            }
                        }

                        // Remove overwritten sign data
                        if (world.Tiles[x + anchor.X, y + anchor.Y].Type == 55 || world.Tiles[x + anchor.X, y + anchor.Y].Type == 85)
                        {
                            var data = world.GetSignAtTile(x + anchor.X, y + anchor.Y);
                            if (data != null)
                            {
                                _wvm.UndoManager.Buffer.Signs.Add(data);
                                world.Signs.Remove(data);
                            }
                        }


                        // Add new chest data
                        if (curTile.Type == 21)
                        {
                            if (world.GetChestAtTile(x + anchor.X, y + anchor.Y) == null)
                            {
                                var data = buffer.GetChestAtTile(x, y);
                                if (data != null) // allow? chest copying may not work...
                                {
                                    // Copied chest
                                    var newChest = data.Copy();
                                    newChest.X = x + anchor.X;
                                    newChest.Y = y + anchor.Y;
                                    world.Chests.Add(newChest);
                                }
                                else
                                {
                                    // Empty chest
                                    world.Chests.Add(new Chest(x + anchor.X, y + anchor.Y));
                                }
                            }
                        }

                        // Add new sign data
                        if (curTile.Type == 55 || curTile.Type == 85)
                        {
                            if (world.GetSignAtTile(x + anchor.X, y + anchor.Y) == null)
                            {
                                var data = buffer.GetSignAtTile(x, y);
                                if (data != null)
                                {
                                    // Copied sign
                                    var newSign = data.Copy();
                                    newSign.X = x + anchor.X;
                                    newSign.Y = y + anchor.Y;
                                    world.Signs.Add(newSign);
                                }
                                else
                                {
                                    world.Signs.Add(new Sign(x + anchor.X, y + anchor.Y, string.Empty));
                                }
                            }
                        }
                        _wvm.UndoManager.SaveTile(x + anchor.X, y + anchor.Y);
                        world.Tiles[x + anchor.X, y + anchor.Y] = curTile;
                    }
                }
            }
            _wvm.UndoManager.SaveUndo();
        }
示例#25
0
        public static ClipboardBuffer Load4(string filename)
        {
            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var b = new BinaryReader(stream))
                {
                    string name    = b.ReadString();
                    int    version = b.ReadInt32();

                    int sizeX  = b.ReadInt32();
                    int sizeY  = b.ReadInt32();
                    var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
                    buffer.Name = name;

                    for (int x = 0; x < sizeX; ++x)
                    {
                        for (int y = 0; y < sizeY; y++)
                        {
                            var tile = new Tile();

                            tile.IsActive = b.ReadBoolean();

                            TileProperty tileProperty = null;
                            if (tile.IsActive)
                            {
                                tile.Type    = b.ReadByte();
                                tileProperty = World.TileProperties[tile.Type];

                                if (tile.Type == 127)
                                {
                                    tile.IsActive = false;
                                }

                                if (tileProperty.IsFramed)
                                {
                                    tile.U = b.ReadInt16();
                                    tile.V = b.ReadInt16();

                                    if (tile.Type == 144) //timer
                                    {
                                        tile.V = 0;
                                    }
                                }
                                else
                                {
                                    tile.U = -1;
                                    tile.V = -1;
                                }

                                if (b.ReadBoolean())
                                {
                                    tile.Color = b.ReadByte();
                                }
                            }

                            if (b.ReadBoolean())
                            {
                                tile.Wall = b.ReadByte();
                                if (b.ReadBoolean())
                                {
                                    tile.WallColor = b.ReadByte();
                                }
                            }

                            if (b.ReadBoolean())
                            {
                                tile.Liquid  = b.ReadByte();
                                tile.IsLava  = b.ReadBoolean();
                                tile.IsHoney = b.ReadBoolean();
                            }

                            tile.HasWire   = b.ReadBoolean();
                            tile.HasWire2  = b.ReadBoolean();
                            tile.HasWire3  = b.ReadBoolean();
                            tile.HalfBrick = b.ReadBoolean();

                            if (tileProperty == null || !tileProperty.IsSolid)
                            {
                                tile.HalfBrick = false;
                            }

                            tile.Slope = b.ReadByte();
                            if (tileProperty == null || !tileProperty.IsSolid)
                            {
                                tile.Slope = 0;
                            }

                            tile.Actuator = b.ReadBoolean();
                            tile.InActive = b.ReadBoolean();

                            // read complete, start compression
                            buffer.Tiles[x, y] = tile;

                            int rle = b.ReadInt16();
                            if (rle < 0)
                            {
                                throw new ApplicationException("Invalid Tile Data!");
                            }

                            if (rle > 0)
                            {
                                for (int k = y + 1; k < y + rle + 1; k++)
                                {
                                    var tcopy = (Tile)tile.Clone();
                                    buffer.Tiles[x, k] = tcopy;
                                }
                                y = y + rle;
                            }
                        }
                    }
                    for (int i = 0; i < 1000; i++)
                    {
                        if (b.ReadBoolean())
                        {
                            var chest = new Chest(b.ReadInt32(), b.ReadInt32());
                            for (int slot = 0; slot < Chest.MaxItems; slot++)
                            {
                                if (slot < Chest.MaxItems)
                                {
                                    int stackSize = (int)b.ReadInt16();
                                    chest.Items[slot].StackSize = stackSize;

                                    if (chest.Items[slot].StackSize > 0)
                                    {
                                        chest.Items[slot].NetId     = b.ReadInt32();
                                        chest.Items[slot].StackSize = stackSize;
                                        chest.Items[slot].Prefix    = b.ReadByte();
                                    }
                                }
                            }
                            buffer.Chests.Add(chest);
                        }
                    }

                    for (int i = 0; i < 1000; i++)
                    {
                        if (b.ReadBoolean())
                        {
                            Sign sign = new Sign();
                            sign.Text = b.ReadString();
                            sign.X    = b.ReadInt32();
                            sign.Y    = b.ReadInt32();

                            if (buffer.Tiles[sign.X, sign.Y].IsActive && (int)buffer.Tiles[sign.X, sign.Y].Type == 55 && (int)buffer.Tiles[sign.X, sign.Y].Type == 85)
                            {
                                buffer.Signs.Add(sign);
                            }
                        }
                    }

                    string verifyName    = b.ReadString();
                    int    verifyVersion = b.ReadInt32();
                    int    verifyX       = b.ReadInt32();
                    int    verifyY       = b.ReadInt32();
                    if (buffer.Name == verifyName &&
                        version == verifyVersion &&
                        buffer.Size.X == verifyX &&
                        buffer.Size.Y == verifyY)
                    {
                        // valid;
                        return(buffer);
                    }
                    b.Close();
                    return(null);
                }
            }
        }
 public void FlipY(ClipboardBuffer buffer)
 {
     Flip(buffer, false);
 }
示例#27
0
        public static ClipboardBuffer Load3(string filename, bool frame19 = false)
        {
            bool failed = false;

            try
            {
                using (var stream = new FileStream(filename, FileMode.Open))
                {
                    using (var br = new BinaryReader(stream))
                    {
                        string name    = br.ReadString();
                        int    version = br.ReadInt32();

                        int sizeX  = br.ReadInt32();
                        int sizeY  = br.ReadInt32();
                        var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
                        buffer.Name = name;

                        for (int x = 0; x < sizeX; x++)
                        {
                            for (int y = 0; y < sizeY; y++)
                            {
                                var curTile = new Tile();
                                curTile.IsActive = br.ReadBoolean();

                                if (curTile.IsActive)
                                {
                                    curTile.Type = br.ReadByte();
                                    if (curTile.Type == 19) // fix for platforms
                                    {
                                        curTile.U = 0;
                                        curTile.V = 0;
                                        if (frame19)
                                        {
                                            curTile.U = br.ReadInt16();
                                            curTile.V = br.ReadInt16();
                                        }
                                    }
                                    else if (World.TileProperties[curTile.Type].IsFramed)
                                    {
                                        curTile.U = br.ReadInt16();
                                        curTile.V = br.ReadInt16();

                                        if (curTile.Type == 144) //timer
                                        {
                                            curTile.V = 0;
                                        }
                                    }
                                    else
                                    {
                                        curTile.U = -1;
                                        curTile.V = -1;
                                    }
                                }

                                if (br.ReadBoolean())
                                {
                                    curTile.Wall = br.ReadByte();
                                }

                                if (br.ReadBoolean())
                                {
                                    curTile.Liquid = br.ReadByte();
                                    curTile.IsLava = br.ReadBoolean();
                                }

                                curTile.HasWire    = br.ReadBoolean();
                                buffer.Tiles[x, y] = curTile;
                            }
                        }
                        for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                        {
                            if (br.ReadBoolean())
                            {
                                var curChest = new Chest(br.ReadInt32(), br.ReadInt32());
                                for (int j = 0; j < 20; ++j)
                                {
                                    curChest.Items[j].StackSize = br.ReadByte();

                                    if (curChest.Items[j].StackSize > 0)
                                    {
                                        if (version >= 3)
                                        {
                                            curChest.Items[j].NetId = br.ReadInt32();
                                        }
                                        else
                                        {
                                            curChest.Items[j].SetFromName(br.ReadString());
                                        }
                                        curChest.Items[j].Prefix = br.ReadByte();
                                    }
                                    else
                                    {
                                        curChest.Items[j].SetFromName("[empty]");
                                    }
                                }
                                buffer.Chests.Add(curChest);
                            }
                        }
                        for (int signIndex = 0; signIndex < 1000; signIndex++)
                        {
                            if (br.ReadBoolean())
                            {
                                string text = br.ReadString();
                                int    x    = br.ReadInt32();
                                int    y    = br.ReadInt32();
                                buffer.Signs.Add(new Sign(x, y, text));
                            }
                        }

                        if (buffer.Name != br.ReadString() || version != br.ReadInt32() || buffer.Size.X != br.ReadInt32() || buffer.Size.Y != br.ReadInt32())
                        {
                            if (!frame19)
                            {
                                br.Close();
                                return(Load3(filename, true));
                            }
                            else
                            {
                                System.Windows.MessageBox.Show("Verification failed. Some schematic data may be missing.", "Legacy Schematic Version");
                            }
                        }

                        br.Close();
                        return(buffer);
                    }
                }
            }
            catch (Exception)
            {
                failed = true;
            }

            if (failed && !frame19)
            {
                return(Load3(filename, true));
            }

            return(null);
        }
        // Reverse the buffer along the x- or y- axis
        public void Flip(ClipboardBuffer buffer, bool flipX)
        {
            ClipboardBuffer flippedBuffer = new ClipboardBuffer(buffer.Size);

            for (int x = 0, maxX = buffer.Size.X - 1; x <= maxX; x++)
            {
                for (int y = 0, maxY = buffer.Size.Y - 1; y <= maxY; y++)
                {
                    int bufferX;
                    int bufferY;

                    if (flipX)
                    {
                        bufferX = maxX - x;
                        bufferY = y;
                    }
                    else
                    {
                        bufferX = x;
                        bufferY = maxY - y;
                    }

                    Tile tile = (Tile)buffer.Tiles[x, y].Clone();

                    Vector2Short tileSize = World.TileProperties[tile.Type].FrameSize;

                    if (flipX)
                    {
                        //  Ignore multi-width objects when flipping on x-axis
                        if (tileSize.X > 1)
                            ClearTile(tile);
                    }
                    //  Ignore multi-height tiles when flipping on y-axis
                    else if (tileSize.Y > 1)
                    {
                            ClearTile(tile);
                    }

                    flippedBuffer.Tiles[bufferX, bufferY] = (Tile)tile;
                }
            }

            // Replace the existing buffer with the new one
            int bufferIndex = LoadedBuffers.IndexOf(buffer);
            if (bufferIndex > -1)
            {
                LoadedBuffers.Insert(bufferIndex, flippedBuffer);
                LoadedBuffers.RemoveAt(bufferIndex + 1);
            }

            flippedBuffer.RenderBuffer();

            if (Buffer == buffer)
            {
                Buffer = flippedBuffer;
                _wvm.PreviewChange();
            }
        }
示例#29
0
        public static ClipboardBuffer Load2(string filename)
        {
            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream))
                {
                    string name    = reader.ReadString();
                    int    version = reader.ReadInt32();
                    int    maxx    = reader.ReadInt32();
                    int    maxy    = reader.ReadInt32();

                    var buffer = new ClipboardBuffer(new Vector2Int32(maxx, maxy));

                    buffer.Name = string.IsNullOrWhiteSpace(name) ? Path.GetFileNameWithoutExtension(filename) : name;

                    try
                    {
                        for (int x = 0; x < maxx; x++)
                        {
                            for (int y = 0; y < maxy; y++)
                            {
                                var curTile = new Tile();
                                curTile.IsActive = reader.ReadBoolean();

                                if (curTile.IsActive)
                                {
                                    curTile.Type = reader.ReadByte();
                                    if (curTile.Type == 19) // fix for platforms
                                    {
                                        curTile.U = 0;
                                        curTile.V = 0;
                                    }
                                    else if (World.TileProperties[curTile.Type].IsFramed)
                                    {
                                        curTile.U = reader.ReadInt16();
                                        curTile.V = reader.ReadInt16();

                                        if (curTile.Type == 144) //timer
                                        {
                                            curTile.V = 0;
                                        }
                                    }
                                    else
                                    {
                                        curTile.U = -1;
                                        curTile.V = -1;
                                    }
                                }

                                if (reader.ReadBoolean())
                                {
                                    curTile.Wall = reader.ReadByte();
                                }

                                if (reader.ReadBoolean())
                                {
                                    curTile.Liquid = reader.ReadByte();
                                    curTile.IsLava = reader.ReadBoolean();
                                }

                                curTile.HasWire    = reader.ReadBoolean();
                                buffer.Tiles[x, y] = curTile;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        for (int x = 0; x < buffer.Size.X; x++)
                        {
                            for (int y = 0; y < buffer.Size.Y; y++)
                            {
                                if (buffer.Tiles[x, y] == null)
                                {
                                    buffer.Tiles[x, y] = new Tile();
                                }
                            }
                        }
                        return(buffer);
                    }

                    for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            var chest = new Chest();
                            chest.X = reader.ReadInt32();
                            chest.Y = reader.ReadInt32();

                            for (int slot = 0; slot < 20; slot++)
                            {
                                byte stackSize = reader.ReadByte();
                                if (stackSize > 0)
                                {
                                    string itemName = reader.ReadString();
                                    chest.Items[slot].SetFromName(itemName);
                                    chest.Items[slot].StackSize = stackSize;
                                }
                            }

                            //Chests[chestIndex] = chest;
                            buffer.Chests.Add(chest);
                        }
                    }
                    for (int signIndex = 0; signIndex < 1000; signIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            string signText = reader.ReadString();
                            int    x        = reader.ReadInt32();
                            int    y        = reader.ReadInt32();
                            if (buffer.Tiles[x, y].IsActive && (buffer.Tiles[x, y].Type == 55 || buffer.Tiles[x, y].Type == 85))
                            // validate tile location
                            {
                                var sign = new Sign(x, y, signText);
                                //Signs[signIndex] = sign;
                                buffer.Signs.Add(sign);
                            }
                        }
                    }
                    string checkName    = reader.ReadString();
                    int    checkversion = reader.ReadInt32();
                    int    checkx       = reader.ReadInt32();
                    int    checky       = reader.ReadInt32();

                    if (checkName != buffer.Name || checkversion != version || checkx != maxx || checky != maxy)
                    {
                        System.Windows.MessageBox.Show("Verification failed. Some schematic data may be missing.", "Legacy Schematic Version");
                    }

                    return(buffer);
                }
            }

            return(null);
        }
        private void ExportSchematicFile(ClipboardBuffer buffer)
        {
            var sfd = new SaveFileDialog();
            sfd.Filter = "TEdit Schematic File|*.TEditSch|Png Image (Real TileColor)|*.png";
            sfd.Title = "Export Schematic File";
            sfd.InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"My Games\Terraria\Schematics");
            if (!Directory.Exists(sfd.InitialDirectory))
                Directory.CreateDirectory(sfd.InitialDirectory);

            if ((bool)sfd.ShowDialog())
            {
                try
                {
                    buffer.Save(sfd.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error Saving Schematic");
                }

            }
        }
        public ClipboardBuffer GetSelectionBuffer()
        {
            World world = _wvm.CurrentWorld;
            XNA.Rectangle area = _wvm.Selection.SelectionArea;
            var buffer = new ClipboardBuffer(new Vector2Int32(area.Width, area.Height));

            for (int x = 0; x < area.Width; x++)
            {
                for (int y = 0; y < area.Height; y++)
                {
                    Tile curTile = (Tile)world.Tiles[x + area.X, y + area.Y].Clone();

                    if (Tile.IsChest(curTile.Type))
                    {
                        if (buffer.GetChestAtTile(x, y, curTile.Type) == null)
                        {
                            var anchor = world.GetAnchor(x + area.X, y + area.Y);
                            if (anchor.X == x + area.X && anchor.Y == y + area.Y)
                            {
                                var data = world.GetChestAtTile(x + area.X, y + area.Y);
                                if (data != null)
                                {
                                    var newChest = data.Copy();
                                    newChest.X = x;
                                    newChest.Y = y;
                                    buffer.Chests.Add(newChest);
                                }
                            }
                        }
                    }
                    if (Tile.IsSign(curTile.Type))
                    {
                        if (buffer.GetSignAtTile(x, y) == null)
                        {
                            var anchor = world.GetAnchor(x + area.X, y + area.Y);
                            if (anchor.X == x + area.X && anchor.Y == y + area.Y)
                            {
                                var data = world.GetSignAtTile(x + area.X, y + area.Y);
                                if (data != null)
                                {
                                    var newSign = data.Copy();
                                    newSign.X = x;
                                    newSign.Y = y;
                                    buffer.Signs.Add(newSign);
                                }
                            }
                        }
                    }
                    if (Tile.IsTileEntity(curTile.Type))
                    {
                        if (buffer.GetTileEntityAtTile(x, y) == null)
                        {
                            var anchor = world.GetAnchor(x + area.X, y + area.Y);
                            if (anchor.X == x + area.X && anchor.Y == y + area.Y)
                            {
                                var data = world.GetTileEntityAtTile(x + area.X, y + area.Y);
                                if (data != null)
                                {
                                    var newEntity = data.Copy();
                                    newEntity.PosX = (short)x;
                                    newEntity.PosY = (short)y;
                                    buffer.TileEntities.Add(newEntity);
                                }
                            }
                        }
                    }
                    buffer.Tiles[x, y] = curTile;
                }
            }

            buffer.RenderBuffer();
            return buffer;
        }