NbtTest.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using MineCase.Nbt;
  6. using MineCase.Nbt.Tags;
  7. using Xunit;
  8. namespace MineCase.UnitTest
  9. {
  10. public class NbtTest
  11. {
  12. private class OutputVisitor : INbtTagVisitor
  13. {
  14. private readonly TextWriter _tw;
  15. private readonly List<bool> _lastVisitedList = new List<bool>();
  16. private int _curDepth;
  17. public OutputVisitor(TextWriter tw)
  18. {
  19. _tw = tw;
  20. }
  21. public void StartChild()
  22. {
  23. _tw.Write($"{Enumerable.Repeat('\t', _curDepth).Aggregate("", (s, c) => string.Concat(s, c))}");
  24. _tw.WriteLine(_lastVisitedList.Last() ? '[' : '{');
  25. ++_curDepth;
  26. }
  27. public void EndChild()
  28. {
  29. --_curDepth;
  30. _tw.Write($"{Enumerable.Repeat('\t', _curDepth).Aggregate("", (s, c) => string.Concat(s, c))}");
  31. _tw.WriteLine(_lastVisitedList.Last() ? ']' : '}');
  32. _lastVisitedList.RemoveAt(_lastVisitedList.Count - 1);
  33. }
  34. public void VisitTag(NbtTag tag)
  35. {
  36. if (tag.TagType == NbtTagType.List || tag.TagType == NbtTagType.Compound)
  37. {
  38. _lastVisitedList.Add(tag.TagType == NbtTagType.List);
  39. }
  40. _tw.Write($"{Enumerable.Repeat('\t', _curDepth).Aggregate("", (s, c) => string.Concat(s, c))}{tag.Name ?? "(null)"} : ");
  41. switch (tag.TagType)
  42. {
  43. case NbtTagType.End:
  44. break;
  45. case NbtTagType.Byte:
  46. _tw.WriteLine(((NbtByte)tag).Value);
  47. break;
  48. case NbtTagType.Short:
  49. _tw.WriteLine(((NbtShort)tag).Value);
  50. break;
  51. case NbtTagType.Int:
  52. _tw.WriteLine(((NbtInt)tag).Value);
  53. break;
  54. case NbtTagType.Long:
  55. _tw.WriteLine(((NbtLong)tag).Value);
  56. break;
  57. case NbtTagType.Float:
  58. _tw.WriteLine(((NbtFloat)tag).Value);
  59. break;
  60. case NbtTagType.Double:
  61. _tw.WriteLine(((NbtDouble)tag).Value);
  62. break;
  63. case NbtTagType.ByteArray:
  64. _tw.WriteLine("(ByteArray)");
  65. break;
  66. case NbtTagType.String:
  67. _tw.WriteLine(((NbtString)tag).Value);
  68. break;
  69. case NbtTagType.List:
  70. _tw.WriteLine();
  71. break;
  72. case NbtTagType.Compound:
  73. _tw.WriteLine();
  74. break;
  75. case NbtTagType.IntArray:
  76. _tw.WriteLine("(IntArray)");
  77. break;
  78. }
  79. }
  80. }
  81. [Fact]
  82. public void Test1()
  83. {
  84. var nbtFile = new NbtFile();
  85. nbtFile.RootTag.Add(new NbtInt(1, "23333"));
  86. nbtFile.RootTag.Add(new NbtCompound("test"));
  87. var testCompound = nbtFile.RootTag["test"] as NbtCompound;
  88. Assert.NotNull(testCompound);
  89. var testList = new NbtList(NbtTagType.Int, "testList");
  90. testCompound.Add(testList);
  91. testList.Add(new NbtInt(2));
  92. testList.Add(new NbtInt(4));
  93. testCompound.Add(new NbtLong(0x000000FFFFFFFFFF, "testLong"));
  94. using (var sw = new StringWriter())
  95. {
  96. nbtFile.RootTag.Accept(new OutputVisitor(sw));
  97. var str = sw.ToString();
  98. Console.WriteLine(str);
  99. }
  100. var stream = new FileStream("test.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  101. stream.SetLength(0);
  102. nbtFile.WriteTo(stream);
  103. stream.Seek(0, SeekOrigin.Begin);
  104. var nbtFile2 = new NbtFile(stream);
  105. using (var sw = new StringWriter())
  106. {
  107. nbtFile2.RootTag.Accept(new OutputVisitor(sw));
  108. Console.WriteLine(sw.ToString());
  109. }
  110. }
  111. }
  112. }