NBTExplorer/NbtClipboardData.cs

60 lines
1.4 KiB
C#
Raw Normal View History

2012-08-26 21:33:54 +00:00
using System;
using System.IO;
using Substrate.Nbt;
namespace NBTExplorer
{
public class NbtClipboardData
{
private string _name;
private TagNode _node;
2012-08-26 21:33:54 +00:00
public string Name
{
get { return _name; }
set { _name = value; }
}
2012-08-26 21:33:54 +00:00
public TagNode Node
{
get { return _node; }
set { _node = value; }
}
2012-08-26 21:33:54 +00:00
public NbtClipboardData (string name, TagNode node)
2012-08-26 21:33:54 +00:00
{
_name = name;
_node = node;
}
2012-08-26 21:33:54 +00:00
public static byte[] SerializeNode (TagNode node)
{
2012-08-26 21:33:54 +00:00
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);
2012-08-26 21:33:54 +00:00
return data;
}
2012-08-26 21:33:54 +00:00
}
public static TagNode DeserializeNode (byte[] data)
2012-08-26 21:33:54 +00:00
{
NbtTree tree = new NbtTree();
using (MemoryStream ms = new MemoryStream(data)) {
2012-08-26 21:33:54 +00:00
tree.ReadFrom(ms);
}
TagNodeCompound root = tree.Root;
if (root == null || !root.ContainsKey("root"))
return null;
return root["root"];
2012-08-26 21:33:54 +00:00
}
}
}