完整版BedrockWiki镜像!
This commit is contained in:
@@ -1,84 +1,85 @@
|
||||
---
|
||||
title: Reading NBT Example
|
||||
category: NBT in Depth
|
||||
title: 读取NBT示例
|
||||
category: 基础
|
||||
mentions:
|
||||
- ConsoleTerm
|
||||
tags:
|
||||
- expert
|
||||
- 专家
|
||||
---
|
||||
|
||||
Before going through this example, it is necessary to first familiarize yourself with NBT in its full beauty. See *[NBT in Depth](/nbt/nbt-in-depth)*.
|
||||
Now we will show you how to read NBT, step by step, the format of what we will read will look something like this:
|
||||
# 读取NBT示例
|
||||
|
||||
<!--@include: @/wiki/bedrock-wiki-mirror.md-->
|
||||
|
||||
在进行本示例前,请先系统学习NBT的完整知识体系。参考*[深入理解NBT](/wiki/nbt/nbt-in-depth)*。
|
||||
现在我们将分步骤演示如何读取NBT数据,示例数据的格式如下:
|
||||
```json
|
||||
"":{
|
||||
"myText":"My NBT text",
|
||||
"myText":"我的NBT文本",
|
||||
"my Int32 Number":456,
|
||||
}
|
||||
```
|
||||
When we don't know what to read, then we read the next byte.
|
||||
当我们不知道后续数据类型时,需要读取下一个字节。
|
||||
|
||||

|
||||
|
||||
What did we read? We read number 10 and that means we will read compoud. We also know that we are at the root element property of this file now, so we need to read the name of our root element property. Name is string, so first we have to read the length of the text in bytes, and that is written by Int16 *(Short)*.
|
||||
我们读取到了数字10,表示即将读取复合标签。此时我们处于文件的根元素属性,需要读取根元素属性的名称。名称是字符串类型,因此首先需要读取字符串长度(以Int16形式存储)。
|
||||
|
||||

|
||||
|
||||
The name size of our root element property is zero so we won't read any more bytes. We don't know what to read, so let's read another byte.
|
||||
根元素属性名称的长度为0,因此无需继续读取字节。此时需要读取下一个字节来确定后续数据类型。
|
||||
|
||||

|
||||
|
||||
We already know that the next property in our root compound is type of string, but before we read our property value, we first read its name written such as string. So we read another 2 bytes to get the length of the string name from of our property.
|
||||
已知根复合标签中的下一个属性是字符串类型,但在读取属性值之前,需要先读取属性名称。因此需要再读取2个字节来获取属性名称的长度。
|
||||
|
||||

|
||||
|
||||
We see that length of property name, it is 6 bytes long. So let's read the next 6 bytes.
|
||||
属性名称长度为6字节,继续读取接下来的6个字节。
|
||||
|
||||

|
||||
|
||||
So we have read the Name of our property, which we can get in text form using UTF-8 encoding, that is: `myText`, then remember that the type of our property is string, so we repeat the process.
|
||||
I'll read the next Int16 (2 bytes) again and we'll find out the length of our string value.
|
||||
通过UTF-8编码转换后,得到属性名称:`myText`。接下来重复字符串类型的读取流程,读取下一个Int16(2字节)来获取字符串值的长度。
|
||||
|
||||

|
||||
|
||||
The string length of our property is 0x0B, so 11, so read another 11 bytes.
|
||||
字符串长度为0x0B(十进制11),继续读取11个字节。
|
||||
|
||||

|
||||
|
||||
When we push our read bytes through UTF-8 encoding, it again returns the value: `My NBT text`,
|
||||
what now? You don't know? So read the next byte to find out what to do next.
|
||||
经过UTF-8解码得到属性值:`My NBT text`。此时需要读取下一个字节来确定后续数据类型。
|
||||
|
||||

|
||||
|
||||
So we read type 3, the 3 type is Int16 which contains 4 bytes. But before we read our number We have to find out the name of this property again. So?
|
||||
Read the next two bytes to get the length of the name for this property.
|
||||
读取到类型3(Int32类型),需要先读取属性名称的长度。读取接下来的2个字节。
|
||||
|
||||

|
||||
|
||||
So we know the length of the name 0x0f (15), Let's read the next 15 bytes and push it through UTF-8 encoding.
|
||||
名称长度为0x0F(十进制15),继续读取15个字节并进行UTF-8解码。
|
||||
|
||||

|
||||
|
||||
Now we have the name of this property: `my Int32 Number`. Next let's read that Int32 => 4 bytes.
|
||||
得到属性名称:`my Int32 Number`。接下来读取Int32类型的4个字节。
|
||||
|
||||

|
||||
|
||||
We read an Int32 that has the value `0x01c8` (456).
|
||||
Again You don't know what to do next? Then just read another type of next property, so? 1 byte.
|
||||
读取到的Int32值为`0x01c8`(十进制456)。继续读取下一个字节确认后续数据类型。
|
||||
|
||||

|
||||
|
||||
We read 0x00 (an empty byte), and that marks the end of the root compound. Then the reading of the compound ends, and since it is the ***root*** compound, we can finish reading it completely and have the entire NBT file read.
|
||||
### NBT Example File
|
||||
This is file what we use here for this example.
|
||||
读取到0x00空字节,表示根复合标签结束。由于这是***根***复合标签,此时完成整个NBT文件的读取。
|
||||
|
||||
### NBT示例文件
|
||||
本示例所使用的NBT文件:
|
||||
|
||||
<BButton
|
||||
link="/assets/nbt/nbt_example_file.nbt" download
|
||||
color=green
|
||||
>Download NBT File</BButton>
|
||||
>下载NBT文件</BButton>
|
||||
|
||||
:::tip Important points to keep in mind
|
||||
- The file may contain an NBT Bedrock Header, so be aware that such a situation may occur. See [NBT in Depth](/nbt/nbt-in-depth)>[NBT Bedrock Headers](/nbt/nbt-in-depth#bedrock-nbt-file-header).
|
||||
- The closing null byte does not terminate the reading of the NBT as such, but merely marks the end of the current compound.
|
||||
- All the numbers you read need to be read with little-endian, See [NBT in Depth](/nbt/nbt-in-depth)>[little-endian](/nbt/nbt-in-depth#little-endian).
|
||||
- The first root NBT element in a file can only be a compound or a list. The root element/property in NBT files also has its own name, even though it is mostly empty, but it still needs to be read and avoid complications.
|
||||
:::
|
||||
:::tip 重要注意事项
|
||||
- 文件可能包含NBT基岩版文件头,需注意这种情况。参见[深入理解NBT](/wiki/nbt/nbt-in-depth)>[NBT基岩版文件头](/wiki/nbt/nbt-in-depth#bedrock-nbt-file-header)
|
||||
- 结束空字节并不直接终止NBT读取,仅标记当前复合标签的结束
|
||||
- 所有数值均需按小端序读取,参见[深入理解NBT](/wiki/nbt/nbt-in-depth)>[小端序](/wiki/nbt/nbt-in-depth#little-endian)
|
||||
- NBT文件的首个根元素只能是复合标签或列表。根元素的名称虽然通常为空,但仍需读取并妥善处理
|
||||
:::
|
||||
Reference in New Issue
Block a user