用于 Swift 的 Minecraft NBT 读取器/写入器

此包提供了 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 标签提供:

读取

有两种读取数据的方法。

简单读取

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(以及其他文件)可能在结构的第一个标签中有一个空白键(如上面的读取命令所示)。