添加一些注释, 改成如果没有名字就不写入就好了

# Conflicts:
#	Cargo.toml
This commit is contained in:
shenjack 2024-07-03 21:15:10 +08:00
parent bf87b48ca6
commit 59ff7797cf
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 44 additions and 33 deletions

View File

@ -1,11 +1,11 @@
[workspace]
members = [
"nbt-test",
"shen-nbt1", # EOL!
"shen-nbt2",
"shen-nbt3",
"shen-nbt4",
"shen-nbt5",
# "shen-nbt1", # EOL!
# "shen-nbt2",
# "shen-nbt3",
# "shen-nbt4",
"shen-nbt5",
"trait-test",
]
resolver = "2"

View File

@ -58,19 +58,23 @@ pub mod nbt_version {
use super::{NbtReader, NbtResult, NbtValue};
pub trait NbtWriteTrait {
/// 写入一个 i8(byte) 数组
fn write_i8_array(writer: &mut Vec<u8>, data: &[i8]);
/// 写入一个 i32(int) 数组
fn write_i32_array(writer: &mut Vec<u8>, data: &[i32]);
/// 写入一个 i64(long) 数组
fn write_i64_array(writer: &mut Vec<u8>, data: &[i64]);
/// 写入一个 NBT 字符串
fn write_nbt_string(writer: &mut Vec<u8>, data: &str);
/// 向 `writer` 写入一个列表类型(List)
fn write_list(writer: &mut Vec<u8>, data: &[NbtValue]) -> NbtResult<()>;
/// 向 `writer` 写入一个复合标签类型(Compound)
///
/// * `is_list_element` 用于指示是否为列表元素。当复合标签类型为列表元素时是不用将名称写入的。
///
/// 如果 `name` 为 `None` 则不写入名字
fn write_compound(
writer: &mut Vec<u8>,
name: Option<&String>,
data: &[(String, NbtValue)],
is_list_element: bool,
) -> NbtResult<()>;
fn write_to(value: &NbtValue, buff: &mut Vec<u8>) -> NbtResult<()>;
@ -84,11 +88,17 @@ pub mod nbt_version {
}
pub trait NbtReadTrait {
/// 从 `reader` 读取一个 i8(byte) 数组
fn read_i8_array(reader: &mut NbtReader) -> NbtResult<Vec<i8>>;
/// 从 `reader` 读取一个 i32(int) 数组
fn read_i32_array(reader: &mut NbtReader) -> NbtResult<Vec<i32>>;
/// 从 `reader` 读取一个 i64(long) 数组
fn read_i64_array(reader: &mut NbtReader) -> NbtResult<Vec<i64>>;
/// 从 `reader` 读取一个 NBT 字符串
fn read_nbt_string(reader: &mut NbtReader) -> NbtResult<String>;
/// 从 `reader` 读取一个列表类型(List)
fn read_list(reader: &mut NbtReader) -> NbtResult<Vec<NbtValue>>;
/// 从 `reader` 读取一个复合标签类型(Compound)
fn read_compound(reader: &mut NbtReader) -> NbtResult<Vec<(String, NbtValue)>>;
fn from_reader(reader: NbtReader) -> NbtResult<NbtValue>;

View File

@ -57,7 +57,10 @@ impl nbt_version::NbtReadTrait for nbt_version::Java {
7 => NbtValue::ByteArray(Java::read_i8_array(reader)?),
8 => NbtValue::String(Java::read_nbt_string(reader)?),
9 => NbtValue::List(Java::read_list(reader)?),
10 => NbtValue::Compound(Some(name.clone()), nbt_version::Java::read_compound(reader)?),
10 => NbtValue::Compound(
Some(name.clone()),
nbt_version::Java::read_compound(reader)?,
),
11 => NbtValue::IntArray(Java::read_i32_array(reader)?),
12 => NbtValue::LongArray(Java::read_i64_array(reader)?),
_ => return Err(NbtError::UnknownType(tag_id)),

View File

@ -71,7 +71,7 @@ impl NbtWriteTrait for Java {
NbtValue::String(x) => Self::write_nbt_string(writer, x),
NbtValue::List(x) => Self::write_list(writer, x)?,
NbtValue::Compound(name, data) => {
Self::write_compound(writer, name.as_ref(), data, true)?
Self::write_compound(writer, name.as_ref(), data)?
}
}
}
@ -82,19 +82,18 @@ impl NbtWriteTrait for Java {
writer: &mut Vec<u8>,
name: Option<&String>,
data: &[(String, NbtValue)],
is_list_element: bool,
) -> NbtResult<()> {
// 如果是列表元素时不用写入名字和key
if !is_list_element {
// 写入自己的名字
Self::write_nbt_string(writer, name.unwrap_or(&"".to_string()));
// 写入自己的名字
if let Some(name) = name {
Self::write_nbt_string(writer, name);
}
for (key, value) in data {
// 写入 tag
writer.push(value.tag());
// 写入 key如果是Compound就不写入因为key就是名字
if let NbtValue::Compound(_, _) = value {}
else {
if let NbtValue::Compound(_, _) = value {
} else {
Self::write_nbt_string(writer, key)
};
// 写入 value
@ -111,7 +110,7 @@ impl NbtWriteTrait for Java {
NbtValue::String(x) => Self::write_nbt_string(writer, x),
NbtValue::List(x) => Self::write_list(writer, x)?,
NbtValue::Compound(name, data) => {
Self::write_compound(writer, name.as_ref(), data, false)?
Self::write_compound(writer, name.as_ref(), data)?
}
}
}
@ -124,7 +123,7 @@ impl NbtWriteTrait for Java {
match value {
NbtValue::Compound(name, data) => {
buff.push(value.tag());
Self::write_compound(buff, name.as_ref(), data, false)?
Self::write_compound(buff, name.as_ref(), data)?
}
x => return Err(NbtError::WrongRootType(x.tag())),
}
@ -159,9 +158,8 @@ impl NbtWriteTrait for JavaNetAfter1_20_2 {
writer: &mut Vec<u8>,
name: Option<&String>,
data: &[(String, NbtValue)],
is_list_element: bool,
) -> NbtResult<()> {
Java::write_compound(writer, name, data, is_list_element)
Java::write_compound(writer, name, data)
}
#[inline]
fn write_to(value: &NbtValue, buff: &mut Vec<u8>) -> NbtResult<()> {
@ -189,7 +187,7 @@ impl NbtWriteTrait for JavaNetAfter1_20_2 {
NbtValue::String(x) => Self::write_nbt_string(buff, x),
NbtValue::List(x) => Self::write_list(buff, x)?,
NbtValue::Compound(name, data) => {
Self::write_compound(buff, name.as_ref(), data, false)?
Self::write_compound(buff, name.as_ref(), data)?
}
}
}
@ -277,9 +275,7 @@ impl NbtWriteTrait for BedrockDisk {
NbtValue::LongArray(x) => Self::write_i64_array(writer, x),
NbtValue::String(x) => Self::write_nbt_string(writer, x),
NbtValue::List(x) => Self::write_list(writer, x)?,
NbtValue::Compound(name, data) => {
Self::write_compound(writer, name.as_ref(), data, true)?
}
NbtValue::Compound(_, data) => Self::write_compound(writer, None, data)?,
}
}
Ok(())
@ -289,10 +285,11 @@ impl NbtWriteTrait for BedrockDisk {
writer: &mut Vec<u8>,
name: Option<&String>,
data: &[(String, NbtValue)],
_is_list_element: bool,
) -> NbtResult<()> {
// 写入自己的名字
Self::write_nbt_string(writer, name.unwrap_or(&"".to_string()));
if let Some(name) = name {
Self::write_nbt_string(writer, name);
}
for (key, value) in data {
// 写入 tag
writer.push(value.tag());
@ -312,7 +309,7 @@ impl NbtWriteTrait for BedrockDisk {
NbtValue::String(x) => Self::write_nbt_string(writer, x),
NbtValue::List(x) => Self::write_list(writer, x)?,
NbtValue::Compound(name, data) => {
Self::write_compound(writer, name.as_ref(), data, false)?
Self::write_compound(writer, name.as_ref(), data)?
}
}
}
@ -326,7 +323,7 @@ impl NbtWriteTrait for BedrockDisk {
match value {
NbtValue::Compound(name, data) => {
buff.push(value.tag());
Self::write_compound(buff, name.as_ref(), data, false)?
Self::write_compound(buff, name.as_ref(), data)?
}
NbtValue::List(data) => {
buff.push(value.tag());
@ -478,7 +475,7 @@ impl NbtWriteTrait for BedrockNetVarInt {
NbtValue::String(x) => Self::write_nbt_string(writer, x),
NbtValue::List(x) => Self::write_list(writer, x)?,
NbtValue::Compound(name, data) => {
Self::write_compound(writer, name.as_ref(), data, true)?
Self::write_compound(writer, name.as_ref(), data)?
}
}
}
@ -488,10 +485,11 @@ impl NbtWriteTrait for BedrockNetVarInt {
writer: &mut Vec<u8>,
name: Option<&String>,
data: &[(String, NbtValue)],
_is_list_element: bool,
) -> NbtResult<()> {
// 写入自己的名字
Self::write_nbt_string(writer, name.unwrap_or(&"".to_string()));
if let Some(name) = name {
Self::write_nbt_string(writer, name);
}
for (key, value) in data {
// 写入 tag
writer.push(value.tag());
@ -511,7 +509,7 @@ impl NbtWriteTrait for BedrockNetVarInt {
NbtValue::String(x) => Self::write_nbt_string(writer, x),
NbtValue::List(x) => Self::write_list(writer, x)?,
NbtValue::Compound(name, data) => {
Self::write_compound(writer, name.as_ref(), data, false)?
Self::write_compound(writer, name.as_ref(), data)?
}
}
}
@ -525,7 +523,7 @@ impl NbtWriteTrait for BedrockNetVarInt {
match value {
NbtValue::Compound(name, data) => {
buff.push(value.tag());
Self::write_compound(buff, name.as_ref(), data, false)?
Self::write_compound(buff, name.as_ref(), data)?
}
NbtValue::List(data) => {
buff.push(value.tag());