Implemented ICloneable interface for tags
This commit is contained in:
parent
51efe12e54
commit
59b38baa3d
|
@ -14,7 +14,7 @@ namespace SharpNBT
|
|||
/// Abstract base class that all NBT tags inherit from.
|
||||
/// </summary>
|
||||
[PublicAPI][Serializable]
|
||||
public abstract class Tag : IEquatable<Tag>, ISerializable
|
||||
public abstract class Tag : IEquatable<Tag>, ISerializable, ICloneable
|
||||
{
|
||||
private static IEnumerable<Type> GetKnownTypes()
|
||||
{
|
||||
|
@ -154,6 +154,24 @@ namespace SharpNBT
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>Creates a new object that is a copy of the current instance.</summary>
|
||||
/// <returns>A new object that is a copy of this instance.</returns>
|
||||
public object Clone()
|
||||
{
|
||||
// Serialize then deserialize to make a deep-copy
|
||||
using var stream = new MemoryStream();
|
||||
|
||||
// Might as well not worry about swapping bits, just use native endian
|
||||
var opts = BitConverter.IsLittleEndian ? FormatOptions.LittleEndian : FormatOptions.BigEndian;
|
||||
using var writer = new TagWriter(stream, opts, true);
|
||||
using var reader = new TagReader(stream, opts, true);
|
||||
|
||||
writer.WriteTag(this);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
return reader.ReadTag(!(Parent is ListTag));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Required constructor for ISerializable implementation.
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in New Issue