From 436fb719ad4bb44eb9f5b2ed27dab422b7ce181c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 29 Jun 2018 10:41:33 +1000 Subject: [PATCH] Don't save backup in /save when map save is cancelled --- MCGalaxy/Commands/World/CmdSave.cs | 5 +- MCGalaxy/Commands/building/CmdLine.cs | 1 - MCGalaxy/Levels/IO/fNbt.cs | 155 ++++++++++---------------- 3 files changed, 63 insertions(+), 98 deletions(-) diff --git a/MCGalaxy/Commands/World/CmdSave.cs b/MCGalaxy/Commands/World/CmdSave.cs index ccc315db8..3d1a4a590 100644 --- a/MCGalaxy/Commands/World/CmdSave.cs +++ b/MCGalaxy/Commands/World/CmdSave.cs @@ -70,9 +70,8 @@ namespace MCGalaxy.Commands.World { } static void Save(Player p, Level lvl, string restoreName) { - if (TrySave(p, lvl, true)) { - Player.Message(p, "Level {0} %Ssaved", lvl.ColoredName); - } + if (!TrySave(p, lvl, true)) return; + Player.Message(p, "Level {0} %Ssaved", lvl.ColoredName); int num = lvl.Backup(true, restoreName); if (num == -1) return; diff --git a/MCGalaxy/Commands/building/CmdLine.cs b/MCGalaxy/Commands/building/CmdLine.cs index 133913c8c..4f389be0b 100644 --- a/MCGalaxy/Commands/building/CmdLine.cs +++ b/MCGalaxy/Commands/building/CmdLine.cs @@ -16,7 +16,6 @@ permissions and limitations under the Licenses. */ using System; -using MCGalaxy.Drawing.Brushes; using MCGalaxy.Drawing.Ops; using MCGalaxy.Maths; diff --git a/MCGalaxy/Levels/IO/fNbt.cs b/MCGalaxy/Levels/IO/fNbt.cs index 592084ef2..0030e21c9 100644 --- a/MCGalaxy/Levels/IO/fNbt.cs +++ b/MCGalaxy/Levels/IO/fNbt.cs @@ -34,34 +34,25 @@ namespace fNbt { /// A tag containing a single byte. public sealed class NbtByte : NbtTag { public override NbtTagType TagType { get { return NbtTagType.Byte; } } + public byte Value; - public byte Value { get; set; } - - internal override void ReadTag(NbtBinaryReader readStream) { - Value = readStream.ReadByte(); + internal override void ReadTag(NbtBinaryReader reader) { + Value = reader.ReadByte(); } } /// A tag containing an array of bytes. public sealed class NbtByteArray : NbtTag { - static readonly byte[] ZeroArray = new byte[0]; + static readonly byte[] empty = new byte[0]; public override NbtTagType TagType { get { return NbtTagType.ByteArray; } } + public byte[] Value = empty; - public byte[] Value { - get { return bytes; } - set { - if (value == null) throw new ArgumentNullException("value"); - bytes = value; - } - } - byte[] bytes = ZeroArray; - - internal override void ReadTag(NbtBinaryReader readStream) { - int length = readStream.ReadInt32(); + internal override void ReadTag(NbtBinaryReader reader) { + int length = reader.ReadInt32(); if (length < 0) - throw new NbtFormatException("Negative length given in TAG_Byte_Array"); + throw new InvalidDataException("Negative length given in TAG_Byte_Array"); - Value = readStream.ReadBytes(length); + Value = reader.ReadBytes(length); if (Value.Length < length) throw new EndOfStreamException(); } } @@ -72,95 +63,75 @@ namespace fNbt { readonly Dictionary tags = new Dictionary(); public NbtCompound() {} - public NbtCompound(string tagName) { - Name = tagName; - } + public NbtCompound(string tagName) { Name = tagName; } public override NbtTag this[string tagName] { get { - if (tagName == null) throw new ArgumentNullException("tagName"); NbtTag result; if (tags.TryGetValue(tagName, out result)) return result; return null; } } - public bool Contains(string tagName) { - if (tagName == null) throw new ArgumentNullException("tagName"); - return tags.ContainsKey(tagName); - } + public bool Contains(string tagName) { return tags.ContainsKey(tagName); } - /// Gets a collection containing all tag names in this NbtCompound. - public IEnumerable Names { get { return tags.Keys; } } - - /// Gets a collection containing all tags in this NbtCompound. - public IEnumerable Tags { get { return tags.Values; } } - - internal override void ReadTag(NbtBinaryReader readStream) { + internal override void ReadTag(NbtBinaryReader reader) { while (true) { - NbtTagType nextTag = readStream.ReadTagType(); + NbtTagType nextTag = reader.ReadTagType(); if (nextTag == NbtTagType.End) return; NbtTag newTag = Construct(nextTag); - newTag.Name = readStream.ReadString(); - newTag.ReadTag(readStream); + newTag.Name = reader.ReadString(); + newTag.ReadTag(reader); tags.Add(newTag.Name, newTag); } } public IEnumerator GetEnumerator() { return tags.Values.GetEnumerator(); } - IEnumerator IEnumerable.GetEnumerator() { return tags.Values.GetEnumerator(); } } - /// A tag containing a double-precision floating point number. public sealed class NbtDouble : NbtTag { public override NbtTagType TagType { get { return NbtTagType.Double; } } - public double Value { get; set; } + public double Value; - internal override void ReadTag(NbtBinaryReader readStream) { - Value = readStream.ReadDouble(); + internal override void ReadTag(NbtBinaryReader reader) { + Value = reader.ReadDouble(); } } - /// A tag containing a single-precision floating point number. public sealed class NbtFloat : NbtTag { public override NbtTagType TagType { get { return NbtTagType.Float; } } - public float Value { get; set; } + public float Value; - internal override void ReadTag(NbtBinaryReader readStream) { - Value = readStream.ReadSingle(); + internal override void ReadTag(NbtBinaryReader reader) { + Value = reader.ReadSingle(); } } /// A tag containing a single signed 32-bit integer. public sealed class NbtInt : NbtTag { public override NbtTagType TagType { get { return NbtTagType.Int; } } - public int Value { get; set; } + public int Value; - internal override void ReadTag(NbtBinaryReader readStream) { - Value = readStream.ReadInt32(); + internal override void ReadTag(NbtBinaryReader reader) { + Value = reader.ReadInt32(); } } /// A tag containing an array of signed 32-bit integers. public sealed class NbtIntArray : NbtTag { public override NbtTagType TagType { get { return NbtTagType.IntArray; } } - public int[] Value { get; set; } + public int[] Value; - public int this[int tagIndex] { - get { return Value[tagIndex]; } - set { Value[tagIndex] = value; } - } - - internal override void ReadTag(NbtBinaryReader readStream) { - int length = readStream.ReadInt32(); + internal override void ReadTag(NbtBinaryReader reader) { + int length = reader.ReadInt32(); if (length < 0) - throw new NbtFormatException("Negative length given in TAG_Int_Array"); + throw new InvalidDataException("Negative length given in TAG_Int_Array"); Value = new int[length]; for (int i = 0; i < length; i++) - Value[i] = readStream.ReadInt32(); + Value[i] = reader.ReadInt32(); } } @@ -168,17 +139,17 @@ namespace fNbt { public sealed class NbtList : NbtTag { public override NbtTagType TagType { get { return NbtTagType.List; } } public readonly List Tags = new List(); - public NbtTagType ListType { get; set; } + public NbtTagType ListType; - internal override void ReadTag(NbtBinaryReader readStream) { - ListType = readStream.ReadTagType(); + internal override void ReadTag(NbtBinaryReader reader) { + ListType = reader.ReadTagType(); - int length = readStream.ReadInt32(); - if (length < 0) throw new NbtFormatException("Negative list size given."); + int length = reader.ReadInt32(); + if (length < 0) throw new InvalidDataException("Negative list size given"); for (int i = 0; i < length; i++) { NbtTag newTag = NbtTag.Construct(ListType); - newTag.ReadTag(readStream); + newTag.ReadTag(reader); Tags.Add(newTag); } } @@ -187,39 +158,39 @@ namespace fNbt { /// A tag containing a single signed 64-bit integer. public sealed class NbtLong : NbtTag { public override NbtTagType TagType { get { return NbtTagType.Long; } } - public long Value { get; set; } + public long Value; - internal override void ReadTag(NbtBinaryReader readStream) { - Value = readStream.ReadInt64(); + internal override void ReadTag(NbtBinaryReader reader) { + Value = reader.ReadInt64(); } } /// A tag containing a single signed 16-bit integer. public sealed class NbtShort : NbtTag { public override NbtTagType TagType { get { return NbtTagType.Short; } } - public short Value { get; set; } + public short Value; - internal override void ReadTag(NbtBinaryReader readStream) { - Value = readStream.ReadInt16(); + internal override void ReadTag(NbtBinaryReader reader) { + Value = reader.ReadInt16(); } } /// A tag containing a single string. String is stored in UTF-8 encoding. public sealed class NbtString : NbtTag { public override NbtTagType TagType { get { return NbtTagType.String; } } - public string Value { get; set; } + public string Value; - internal override void ReadTag(NbtBinaryReader readStream) { - Value = readStream.ReadString(); + internal override void ReadTag(NbtBinaryReader reader) { + Value = reader.ReadString(); } } /// Base class for different kinds of named binary tags. public abstract class NbtTag { public abstract NbtTagType TagType { get; } - public string Name { get; set; } + public string Name; - internal abstract void ReadTag(NbtBinaryReader readStream); + internal abstract void ReadTag(NbtBinaryReader reader); public virtual NbtTag this[string tagName] { get { throw new InvalidOperationException("String indexers only work on NbtCompound tags."); } @@ -300,7 +271,8 @@ namespace fNbt { internal sealed class NbtBinaryReader : BinaryReader { readonly byte[] buffer = new byte[sizeof(double)]; readonly bool swapNeeded; - readonly byte[] stringConversionBuffer = new byte[64]; + // avoid allocation for small strings (which is majority of them) + readonly byte[] strBuffer = new byte[64]; public NbtBinaryReader(Stream input, bool bigEndian) : base(input) { swapNeeded = (BitConverter.IsLittleEndian == bigEndian); @@ -356,24 +328,21 @@ namespace fNbt { public override string ReadString() { short length = ReadInt16(); if (length < 0) { - throw new NbtFormatException("Negative string length given!"); + throw new InvalidDataException("Negative string length given"); } - if (length < stringConversionBuffer.Length) { - int stringBytesRead = 0; - while (stringBytesRead < length) { - int bytesReadThisTime = BaseStream.Read(stringConversionBuffer, stringBytesRead, length); - if (bytesReadThisTime == 0) { - throw new EndOfStreamException(); - } - stringBytesRead += bytesReadThisTime; + + if (length < strBuffer.Length) { + int offset = 0; + while (offset < length) { + int read = BaseStream.Read(strBuffer, offset, length - offset); + if (read == 0) throw new EndOfStreamException(); + offset += read; } - return Encoding.UTF8.GetString(stringConversionBuffer, 0, length); + return Encoding.UTF8.GetString(strBuffer, 0, length); } else { - byte[] stringData = ReadBytes(length); - if (stringData.Length < length) { - throw new EndOfStreamException(); - } - return Encoding.UTF8.GetString(stringData); + byte[] data = ReadBytes(length); + if (data.Length < length) throw new EndOfStreamException(); + return Encoding.UTF8.GetString(data); } } @@ -405,8 +374,7 @@ namespace fNbt { /// Represents a complete NBT file. public sealed class NbtFile { - - public NbtCompound RootTag { get; set; } + public NbtCompound RootTag; public NbtFile() { RootTag = new NbtCompound(""); } public void LoadFromStream(Stream stream) { @@ -430,7 +398,6 @@ namespace fNbt { } } - /// Exception thrown when a format violation is detected while parsing or serializing an NBT file. public sealed class NbtFormatException : Exception { internal NbtFormatException(string message) : base(message) {} }