此包提供了 Minecraft NBT 数据结构的读取/写入过程。
它还初步(未完全测试!)支持使用“NBTEncoder”和“NBTDecoder”将 NBT 结构解码/编码为 Swift 类/结构体。
数据应该是 Data
类型的未压缩流。
// Data is the decompressed data of the NBT file
guard var structure = NBTStructure(decompressed: data) else {
print("Failed to read data")
exit(0)
}
对于压缩数据,只能使用 GZip
格式。
// Data is the compressed data of the NBT file
guard var structure = NBTStructure(compressed: data) else {
print("Failed to read data")
exit(0)
}
structure
标签提供:
tag
属性,它是 NBT 的复合根标签。您可以使用此标签手动遍历 NBT 结构。read
和 write
函数,它们都接受一个字符串数组(文本或数值,分别用于索引和复合标签),并接受要写入的值,或者返回找到的值。有两种读取数据的方法。
try structure.read(["", "Data", "ServerBrands", "0"])
// Get compound of NBT file
var compound = structure.tag
// Read data from compound
var keys = compound.contents.keys
var someIntValue = compound["intValue"]
var someStringValue = compound["stringValue"]
// Read data from list
var list = compound["listValue"] as! NBTList
var someValue = list.elements[0]
Level.dat(以及其他文件)可能在结构的第一个标签中有一个空白键(如上面的读取命令所示)。