From 59ff7797cfa5cc6e682a5fb9f49e3acd5482ee63 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Wed, 3 Jul 2024 21:15:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=BA=9B=E6=B3=A8?= =?UTF-8?q?=E9=87=8A,=20=E6=94=B9=E6=88=90=E5=A6=82=E6=9E=9C=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=90=8D=E5=AD=97=E5=B0=B1=E4=B8=8D=E5=86=99=E5=85=A5?= =?UTF-8?q?=E5=B0=B1=E5=A5=BD=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Conflicts: # Cargo.toml --- Cargo.toml | 10 ++++----- shen-nbt5/src/lib.rs | 16 +++++++++++--- shen-nbt5/src/reader.rs | 5 ++++- shen-nbt5/src/writer.rs | 46 ++++++++++++++++++++--------------------- 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c60d0a5..6001fb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/shen-nbt5/src/lib.rs b/shen-nbt5/src/lib.rs index 21c2beb..2ef2b9e 100644 --- a/shen-nbt5/src/lib.rs +++ b/shen-nbt5/src/lib.rs @@ -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, data: &[i8]); + /// 写入一个 i32(int) 数组 fn write_i32_array(writer: &mut Vec, data: &[i32]); + /// 写入一个 i64(long) 数组 fn write_i64_array(writer: &mut Vec, data: &[i64]); + /// 写入一个 NBT 字符串 fn write_nbt_string(writer: &mut Vec, data: &str); + /// 向 `writer` 写入一个列表类型(List) fn write_list(writer: &mut Vec, data: &[NbtValue]) -> NbtResult<()>; /// 向 `writer` 写入一个复合标签类型(Compound) - /// - /// * `is_list_element` 用于指示是否为列表元素。当复合标签类型为列表元素时是不用将名称写入的。 + /// + /// 如果 `name` 为 `None` 则不写入名字 fn write_compound( writer: &mut Vec, name: Option<&String>, data: &[(String, NbtValue)], - is_list_element: bool, ) -> NbtResult<()>; fn write_to(value: &NbtValue, buff: &mut Vec) -> NbtResult<()>; @@ -84,11 +88,17 @@ pub mod nbt_version { } pub trait NbtReadTrait { + /// 从 `reader` 读取一个 i8(byte) 数组 fn read_i8_array(reader: &mut NbtReader) -> NbtResult>; + /// 从 `reader` 读取一个 i32(int) 数组 fn read_i32_array(reader: &mut NbtReader) -> NbtResult>; + /// 从 `reader` 读取一个 i64(long) 数组 fn read_i64_array(reader: &mut NbtReader) -> NbtResult>; + /// 从 `reader` 读取一个 NBT 字符串 fn read_nbt_string(reader: &mut NbtReader) -> NbtResult; + /// 从 `reader` 读取一个列表类型(List) fn read_list(reader: &mut NbtReader) -> NbtResult>; + /// 从 `reader` 读取一个复合标签类型(Compound) fn read_compound(reader: &mut NbtReader) -> NbtResult>; fn from_reader(reader: NbtReader) -> NbtResult; diff --git a/shen-nbt5/src/reader.rs b/shen-nbt5/src/reader.rs index 892cdb9..21ab582 100644 --- a/shen-nbt5/src/reader.rs +++ b/shen-nbt5/src/reader.rs @@ -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)), diff --git a/shen-nbt5/src/writer.rs b/shen-nbt5/src/writer.rs index b3c2d7b..871729e 100644 --- a/shen-nbt5/src/writer.rs +++ b/shen-nbt5/src/writer.rs @@ -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, 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, 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) -> 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, 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, 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());