NBTExplorer/NbtClipboardData.cs
2012-11-09 00:14:23 -05:00

59 lines
1.4 KiB
C#

using System;
using System.IO;
using Substrate.Nbt;
namespace NBTExplorer
{
public class NbtClipboardData
{
private string _name;
private TagNode _node;
public string Name
{
get { return _name; }
set { _name = value; }
}
public TagNode Node
{
get { return _node; }
set { _node = value; }
}
public NbtClipboardData (string name, TagNode node)
{
_name = name;
_node = node;
}
public static byte[] SerializeNode (TagNode node)
{
TagNodeCompound root = new TagNodeCompound();
root.Add("root", node);
NbtTree tree = new NbtTree(root);
using (MemoryStream ms = new MemoryStream()) {
tree.WriteTo(ms);
byte[] data = new byte[ms.Length];
Array.Copy(ms.GetBuffer(), data, ms.Length);
return data;
}
}
public static TagNode DeserializeNode (byte[] data)
{
NbtTree tree = new NbtTree();
using (MemoryStream ms = new MemoryStream(data)) {
tree.ReadFrom(ms);
}
TagNodeCompound root = tree.Root;
if (root == null || !root.ContainsKey("root"))
return null;
return root["root"];
}
}
}