Skip to content

Instantly share code, notes, and snippets.

@SharkyRawr
Created April 10, 2011 16:15
Show Gist options
  • Save SharkyRawr/912472 to your computer and use it in GitHub Desktop.
Save SharkyRawr/912472 to your computer and use it in GitHub Desktop.
Patch for libnbt issue #2
# HG changeset patch
# User schumann2k <schumann2k ]at[ googlemail [dot] com>
# Date 1302451499 -7200
# Node ID 8b9db3a4df0184c66c40c11b20a500a39391f802
# Parent 86ae6c959e53ec7ad6756d60fbe3e39d9b15da76
Added support for specifying the type of a NbtList (crucial for exporting .schematic files).
diff -r 86ae6c959e53 -r 8b9db3a4df01 LibNbt.Test/NbtFileTest.cs
--- a/LibNbt.Test/NbtFileTest.cs Sat Sep 18 17:07:44 2010 -0500
+++ b/LibNbt.Test/NbtFileTest.cs Sun Apr 10 18:04:59 2011 +0200
@@ -275,5 +275,30 @@
FileAssert.AreEqual(testFileStream, nbtStream);
}
+
+ [Test]
+ public void TestNbtListType()
+ {
+ var file = new NbtFile();
+ file.RootTag = new NbtCompound("ListTypeTest");
+
+ NbtTagType mytype = NbtTagType.TAG_Compound;
+
+ NbtList list = new NbtList("Entities", null, mytype);
+ file.RootTag.Tags.Add(list);
+
+ file.SaveFile("TestFiles/NbtListType.nbt");
+
+
+ NbtFile read = new NbtFile();
+ read.LoadFile("TestFiles/NbtListType.nbt");
+ Assert.NotNull(read);
+ Console.WriteLine(read.RootTag.ToString());
+
+ NbtList readlist = (NbtList)read.RootTag.Tags.ToArray()[0];
+ Assert.NotNull(readlist);
+
+ Assert.AreEqual(mytype, readlist.listType);
+ }
}
}
diff -r 86ae6c959e53 -r 8b9db3a4df01 LibNbt/Tags/NbtList.cs
--- a/LibNbt/Tags/NbtList.cs Sat Sep 18 17:07:44 2010 -0500
+++ b/LibNbt/Tags/NbtList.cs Sun Apr 10 18:04:59 2011 +0200
@@ -9,8 +9,10 @@
{
public class NbtList : NbtTag, INbtTagList
{
- public List<NbtTag> Tags { get; protected set; }
- public NbtTagType Type { get; protected set; }
+ public List<NbtTag> Tags { get; protected set; }
+ public NbtTagType Type { get; protected set; }
+
+ public NbtTagType listType { get; protected set; }
public NbtTag this[int tagIdx]
{
@@ -19,11 +21,13 @@
}
public NbtList() : this("") { }
- public NbtList(string tagName) : this(tagName, new NbtTag[] { }) { }
- public NbtList(string tagName, IEnumerable<NbtTag> tags)
+ public NbtList(string tagName) : this(tagName, null) { }
+ public NbtList(string tagName, IEnumerable<NbtTag> tags) : this(tagName, null, NbtTagType.TAG_Unknown) { }
+ public NbtList(string tagName, IEnumerable<NbtTag> tags, NbtTagType type)
{
Name = tagName;
- Tags = new List<NbtTag>();
+ Tags = new List<NbtTag>();
+ listType = type;
if (tags != null)
{
@@ -95,6 +99,11 @@
}
return (T) ((NbtTag) this);
+ }
+
+ public void SetListType(NbtTagType newtype)
+ {
+ this.listType = newtype;
}
#region Reading Tag
@@ -113,7 +122,8 @@
var tagId = new NbtByte();
tagId.ReadTag(readStream, false);
- Type = (NbtTagType)tagId.Value;
+ Type = (NbtTagType)tagId.Value;
+ listType = Type;
var length = new NbtInt();
length.ReadTag(readStream, false);
@@ -198,7 +208,14 @@
// to make sure all elements are that type.
if (Tags.Count > 0)
{
- NbtTagType listType = Tags[0].GetTagType();
+ //NbtTagType listType = Tags[0].GetTagType();
+ if (listType <= 0x00)
+ {
+ listType = Tags[0].GetTagType();
+ if (listType == 0x0)
+ listType = NbtTagType.TAG_Unknown;
+ }
+
foreach(NbtTag tag in Tags)
{
if (tag.GetTagType() != listType)
@@ -206,8 +223,8 @@
throw new Exception("All list items must be the same tag type.");
}
}
- Type = listType;
- }
+ }
+ Type = listType;
var tagType = new NbtByte("", (byte)Type);
tagType.WriteData(writeStream);
@@ -235,7 +252,7 @@
{
sb.AppendFormat("(\"{0}\")", Name);
}
- sb.AppendFormat(": {0} entries\n", Tags.Count);
+ sb.AppendFormat(": {0} entries of type {1}\n", Tags.Count, listType);
sb.Append("{\n");
foreach (NbtTag tag in Tags)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment