Пример #1
0
        public static UndoBuffer Read(string filename)
        {
            var buffer = new UndoBuffer();

            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var br = new BinaryReader(stream))
                {
                    var tilecount = br.ReadInt32();
                    for (int i = 0; i < tilecount; i++)
                    {
                        int x = br.ReadInt32();
                        int y = br.ReadInt32();
                        var curTile = World.ReadTileDataFromStream(br, World.CompatibleVersion);
                        buffer.Tiles.Add(new UndoTile(new Vector2Int32(x, y), curTile));
                    }
                    buffer.Chests.Clear();
                    buffer.Chests.AddRange(World.ReadChestDataFromStream(br, World.CompatibleVersion));

                    buffer.Signs.Clear();
                    buffer.Signs.AddRange(World.ReadSignDataFromStream(br));
                }
            }
            return buffer;
        }
Пример #2
0
        public UndoManager(WorldViewModel viewModel)
        {
            if (!Directory.Exists(Dir))
            {
                Directory.CreateDirectory(Dir);
            }

            _wvm = viewModel;
            _buffer = new UndoBuffer(GetUndoFileName());
        }
Пример #3
0
        public void Redo()
        {
            if (_currentIndex > _maxIndex || _currentIndex < 0)
            {
                return;
            }

            var buffer = UndoBuffer.Read(string.Format(RedoFile, _currentIndex));

            foreach (var undoTile in buffer.Tiles)
            {
                var curTile = (Tile)_wvm.CurrentWorld.Tiles[undoTile.Location.X, undoTile.Location.Y].Clone();
                if (curTile.Type == 21)
                {
                    var curchest = _wvm.CurrentWorld.GetChestAtTile(undoTile.Location.X, undoTile.Location.Y);
                    if (curchest != null)
                    {
                        _wvm.CurrentWorld.Chests.Remove(curchest);
                    }
                }
                if (curTile.Type == 55 || curTile.Type == 85)
                {
                    var cursign = _wvm.CurrentWorld.GetSignAtTile(undoTile.Location.X, undoTile.Location.Y);
                    if (cursign != null)
                    {
                        _wvm.CurrentWorld.Signs.Remove(cursign);
                    }
                }

                _wvm.CurrentWorld.Tiles[undoTile.Location.X, undoTile.Location.Y] = (Tile)undoTile.Tile.Clone();
                _wvm.UpdateRenderPixel(undoTile.Location);

                /* Heathtech */
                BlendRules.ResetUVCache(_wvm, undoTile.Location.X, undoTile.Location.Y, 1, 1);
            }
            foreach (var chest in buffer.Chests)
            {
                _wvm.CurrentWorld.Chests.Add(chest.Copy());
            }
            foreach (var sign in buffer.Signs)
            {
                _wvm.CurrentWorld.Signs.Add(sign.Copy());
            }
            _currentIndex++;
        }
Пример #4
0
 protected virtual void Dispose(bool disposing)
 {
     if (!disposed)
     {
         if (disposing)
         {
             // free managed
         }
         // Free your own state (unmanaged objects).
         // Set large fields to null.
         _buffer = null;
         foreach (var file in Directory.GetFiles(Dir))
         {
             File.Delete(file);
         }
         disposed = true;
     }
 }
Пример #5
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (_buffer != null)
                    {
                        _buffer.Dispose();
                    }
                    _buffer = null;
                }
                // Free your own state (unmanaged objects).
                // Set large fields to null.



                CleanupOldUndoFiles(true);
                disposed = true;
            }
        }
Пример #6
0
        public void Undo()
        {
            if (_currentIndex <= 0)
            {
                return;
            }

            _currentIndex--;

            UndoBuffer redo = new UndoBuffer(string.Format(RedoFile, _currentIndex));

            using (var stream = new FileStream(string.Format(UndoFile, _currentIndex), FileMode.Open))
                using (BinaryReader br = new BinaryReader(stream))
                {
                    foreach (var undoTile in UndoBuffer.ReadUndoTilesFromStream(br))
                    {
                        var curTile = (Tile)_wvm.CurrentWorld.Tiles[undoTile.Location.X, undoTile.Location.Y];
                        redo.Add(undoTile.Location, curTile);

                        if (Tile.IsChest(curTile.Type))
                        {
                            var curchest = _wvm.CurrentWorld.GetChestAtTile(undoTile.Location.X, undoTile.Location.Y);
                            if (curchest != null)
                            {
                                _wvm.CurrentWorld.Chests.Remove(curchest);
                                var chest = curchest.Copy();
                                redo.Chests.Add(chest);
                            }
                        }
                        if (Tile.IsSign(curTile.Type))
                        {
                            var cursign = _wvm.CurrentWorld.GetSignAtTile(undoTile.Location.X, undoTile.Location.Y);
                            if (cursign != null)
                            {
                                _wvm.CurrentWorld.Signs.Remove(cursign);
                                var sign = cursign.Copy();
                                redo.Signs.Add(sign);
                            }
                        }
                        _wvm.CurrentWorld.Tiles[undoTile.Location.X, undoTile.Location.Y] = (Tile)undoTile.Tile;
                        _wvm.UpdateRenderPixel(undoTile.Location);

                        /* Heathtech */
                        BlendRules.ResetUVCache(_wvm, undoTile.Location.X, undoTile.Location.Y, 1, 1);
                    }

                    redo.Close();
                    redo.Dispose();
                    redo = null;

                    foreach (var chest in World.LoadChestData(br))
                    {
                        _wvm.CurrentWorld.Chests.Add(chest);
                    }
                    foreach (var sign in World.LoadSignData(br))
                    {
                        _wvm.CurrentWorld.Signs.Add(sign);
                    }
                }

            OnUndid(this, EventArgs.Empty);
        }
Пример #7
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (_buffer != null)
                    {
                        _buffer.Dispose();
                    }
                    _buffer = null;
                }
                // Free your own state (unmanaged objects).
                // Set large fields to null.

                CleanupOldUndoFiles(true);
                disposed = true;
            }
        }
Пример #8
0
        public void Undo()
        {
            if (_currentIndex <= 0)
                return;

            _currentIndex--;

            UndoBuffer redo = new UndoBuffer(string.Format(RedoFile, _currentIndex));

            using (var stream = new FileStream(string.Format(UndoFile, _currentIndex), FileMode.Open))
            using (BinaryReader br = new BinaryReader(stream))
            {
                foreach (var undoTile in UndoBuffer.ReadUndoTilesFromStream(br))
                {

                    var curTile = (Tile)_wvm.CurrentWorld.Tiles[undoTile.Location.X, undoTile.Location.Y];
                    redo.Add(undoTile.Location, curTile);

                    if (Tile.IsChest(curTile.Type))
                    {
                        var curchest = _wvm.CurrentWorld.GetChestAtTile(undoTile.Location.X, undoTile.Location.Y);
                        if (curchest != null)
                        {
                            _wvm.CurrentWorld.Chests.Remove(curchest);
                            var chest = curchest.Copy();
                            redo.Chests.Add(chest);
                        }
                    }
                    if (Tile.IsSign(curTile.Type))
                    {
                        var cursign = _wvm.CurrentWorld.GetSignAtTile(undoTile.Location.X, undoTile.Location.Y);
                        if (cursign != null)
                        {
                            _wvm.CurrentWorld.Signs.Remove(cursign);
                            var sign = cursign.Copy();
                            redo.Signs.Add(sign);
                        }
                    }
                    _wvm.CurrentWorld.Tiles[undoTile.Location.X, undoTile.Location.Y] = (Tile)undoTile.Tile;
                    _wvm.UpdateRenderPixel(undoTile.Location);

                    /* Heathtech */
                    BlendRules.ResetUVCache(_wvm, undoTile.Location.X, undoTile.Location.Y, 1, 1);
                }

                redo.Close();
                redo.Dispose();
                redo = null;

                foreach (var chest in World.LoadChestData(br))
                {
                    _wvm.CurrentWorld.Chests.Add(chest);
                }
                foreach (var sign in World.LoadSignData(br))
                {
                    _wvm.CurrentWorld.Signs.Add(sign);
                }
            }

            OnUndid(this, EventArgs.Empty);
        }
Пример #9
0
        public void SaveUndo()
        {
            // no tiles to undo, skip save
            if (_buffer == null)
            {
                return;
            }

            //ValidateAndRemoveChests();
            _maxIndex = _currentIndex;
            _buffer.Close();
            _currentIndex++;
            _buffer = null;

            OnUndoSaved(this, EventArgs.Empty);
        }
Пример #10
0
        public void SaveTile(int x, int y)
        {
            if (_buffer == null)
                Buffer = new UndoBuffer(GetUndoFileName());

            ValidateAndRemoveChests();
            var curTile = (Tile)_wvm.CurrentWorld.Tiles[x, y].Clone();
            if (Tile.IsChest(curTile.Type) && !Buffer.Chests.Any(c => c.X == x && c.Y == y))
            {

                var curchest = _wvm.CurrentWorld.GetChestAtTile(x, y);
                if (curchest != null)
                {
                    var chest = curchest.Copy();
                    Buffer.Chests.Add(chest);
                }
            }
            else if (Tile.IsSign(curTile.Type) && !Buffer.Signs.Any(c => c.X == x && c.Y == y))
            {
                var cursign = _wvm.CurrentWorld.GetSignAtTile(x, y);
                if (cursign != null)
                {
                    var sign = cursign.Copy();
                    Buffer.Signs.Add(sign);
                }
            }
            Buffer.Add(new Vector2Int32(x, y), curTile);
        }
        public static UndoBuffer Read(string filename)
        {
            var buffer = new UndoBuffer();

            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var br = new BinaryReader(stream))
                {
                    var tilecount = br.ReadInt32();
                    for (int i = 0; i < tilecount; i++)
                    {
                        int x = br.ReadInt32();
                        int y = br.ReadInt32();
                        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.Add(new UndoTile(new Vector2Int32(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)
                                {
                                    curChest.Items[j].NetId = br.ReadInt32();
                                    curChest.Items[j].Prefix = br.ReadByte();
                                }
                                else
                                {
                                    curChest.Items[j].NetId = 0;
                                }
                            }

                            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));
                        }
                    }
                }
            }
            return buffer;
        }
Пример #12
0
        public static UndoBuffer Read(string filename)
        {
            var buffer = new UndoBuffer();

            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var br = new BinaryReader(stream))
                {
                    var tilecount = br.ReadInt32();
                    for (int i = 0; i < tilecount; i++)
                    {
                        int x       = br.ReadInt32();
                        int y       = br.ReadInt32();
                        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.Add(new UndoTile(new Vector2Int32(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)
                                {
                                    curChest.Items[j].ItemName = br.ReadString();
                                    curChest.Items[j].Prefix   = br.ReadByte();
                                }
                                else
                                {
                                    curChest.Items[j].ItemName = "[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));
                        }
                    }
                }
            }
            return(buffer);
        }
 protected virtual void Dispose(bool disposing)
 {
     if (!disposed)
     {
         if (disposing)
         {
             // free managed
         }
         // Free your own state (unmanaged objects).
         // Set large fields to null.
         _buffer = null;
         foreach (var file in Directory.GetFiles(Dir))
         {
             File.Delete(file);
         }
         disposed = true;
     }
 }
        public void Undo()
        {
            if (_currentIndex <= 0)
                return;

            _currentIndex--;
            var buffer = UndoBuffer.Read(string.Format(UndoFile, _currentIndex));
            var redo = new UndoBuffer();

            foreach (var undoTile in buffer.Tiles)
            {
                var curTile = (Tile)_wvm.CurrentWorld.Tiles[undoTile.Location.X, undoTile.Location.Y].Clone();
                if (curTile.Type == 21)
                {
                    var curchest = _wvm.CurrentWorld.GetChestAtTile(undoTile.Location.X, undoTile.Location.Y);
                    if (curchest != null)
                    {
                        _wvm.CurrentWorld.Chests.Remove(curchest);
                        var chest = curchest.Copy();
                        redo.Chests.Add(chest);
                    }
                }
                if (curTile.Type == 55 || curTile.Type == 85)
                {
                    var cursign = _wvm.CurrentWorld.GetSignAtTile(undoTile.Location.X, undoTile.Location.Y);
                    if (cursign != null)
                    {
                        _wvm.CurrentWorld.Signs.Remove(cursign);
                        var sign = cursign.Copy();
                        redo.Signs.Add(sign);
                    }
                }
                redo.Tiles.Add(new UndoTile(undoTile.Location, curTile));

                _wvm.CurrentWorld.Tiles[undoTile.Location.X, undoTile.Location.Y] = (Tile)undoTile.Tile.Clone();
                _wvm.UpdateRenderPixel(undoTile.Location);

                /* Heathtech */
                BlendRules.ResetUVCache(_wvm, undoTile.Location.X, undoTile.Location.Y, 1, 1);
            }
            foreach (var chest in buffer.Chests)
            {
                _wvm.CurrentWorld.Chests.Add(chest.Copy());
            }
            foreach (var sign in buffer.Signs)
            {
                _wvm.CurrentWorld.Signs.Add(sign.Copy());
            }
            redo.Write(string.Format(RedoFile, _currentIndex));
        }
 public void SaveUndo()
 {
     ValidateAndRemoveChests();
     _maxIndex = _currentIndex;
     _buffer.Write(string.Format(UndoFile, _currentIndex));
     _currentIndex++;
     Buffer = new UndoBuffer();
 }
Пример #16
0
        public void Undo()
        {
            if (_currentIndex <= 0)
            {
                return;
            }

            _currentIndex--;
            List <Chest> Chests = new List <Chest>();
            List <Sign>  Signs  = new List <Sign>();

            using (var ws = new FileStream(string.Format(RedoFile, _currentIndex), FileMode.Create))
                using (var bw = new BinaryWriter(ws))
                    using (var stream = new FileStream(string.Format(UndoFile, _currentIndex), FileMode.Open))
                        using (BinaryReader br = new BinaryReader(stream))
                        {
                            int count = br.ReadInt32();
                            br.BaseStream.Position -= 4;
                            bw.Write(count);

                            foreach (var undoTile in UndoBuffer.ReadUndoTilesFromStream(br))
                            {
                                var curTile = (Tile)_wvm.CurrentWorld.Tiles[undoTile.Location.X, undoTile.Location.Y];
                                if (curTile.Type == 21)
                                {
                                    var curchest = _wvm.CurrentWorld.GetChestAtTile(undoTile.Location.X, undoTile.Location.Y);
                                    if (curchest != null)
                                    {
                                        _wvm.CurrentWorld.Chests.Remove(curchest);
                                        var chest = curchest.Copy();
                                        Chests.Add(chest);
                                    }
                                }
                                if (curTile.Type == 55 || curTile.Type == 85)
                                {
                                    var cursign = _wvm.CurrentWorld.GetSignAtTile(undoTile.Location.X, undoTile.Location.Y);
                                    if (cursign != null)
                                    {
                                        _wvm.CurrentWorld.Signs.Remove(cursign);
                                        var sign = cursign.Copy();
                                        Signs.Add(sign);
                                    }
                                }

                                bw.Write(undoTile.Location.X);
                                bw.Write(undoTile.Location.Y);
                                World.WriteTileDataToStream(curTile, bw);

                                _wvm.CurrentWorld.Tiles[undoTile.Location.X, undoTile.Location.Y] = (Tile)undoTile.Tile;
                                _wvm.UpdateRenderPixel(undoTile.Location);

                                /* Heathtech */
                                BlendRules.ResetUVCache(_wvm, undoTile.Location.X, undoTile.Location.Y, 1, 1);
                            }
                            World.WriteChestDataToStream(Chests, bw);
                            World.WriteSignDataToStream(Signs, bw);
                            foreach (var chest in World.ReadChestDataFromStream(br, World.CompatibleVersion))
                            {
                                _wvm.CurrentWorld.Chests.Add(chest);
                            }
                            foreach (var sign in World.ReadSignDataFromStream(br))
                            {
                                _wvm.CurrentWorld.Signs.Add(sign);
                            }
                        }



            OnUndid(this, EventArgs.Empty);
        }
Пример #17
0
 public void CreateUndo()
 {
     if (_buffer == null)
         Buffer = new UndoBuffer(GetUndoFileName());
 }
Пример #18
0
        public void SaveUndo()
        {
            //ValidateAndRemoveChests();
            _maxIndex = _currentIndex;
            _buffer.Close();

            _currentIndex++;

            _buffer = null;

            OnUndoSaved(this, EventArgs.Empty);
        }