diff --git a/src/data.rs b/src/data.rs index 9e1b8db..e56f67f 100644 --- a/src/data.rs +++ b/src/data.rs @@ -26,7 +26,7 @@ pub enum NbtList { IntArray(Rc>>), LongArray(Rc>>), List(Rc>>), - Compound(Rc, NbtItem>>>), + Compound(Arc, Rc, NbtItem>>>), } /// 基本 NBT 数据类型 @@ -85,9 +85,11 @@ impl From> for NbtItem { fn from(value: Vec) -> Self { Self::Array(NbtList::from(value)) } } -impl From, NbtItem>> for NbtItem { +type Compound = (Arc, HashMap, NbtItem>); + +impl From for NbtItem { #[inline] - fn from(value: HashMap, NbtItem>) -> Self { Self::Array(NbtList::from(value)) } + fn from(value: Compound) -> Self { Self::Array(NbtList::from(value)) } } impl From> for NbtItem { @@ -110,10 +112,10 @@ impl From> for NbtList { fn from(value: Vec) -> Self { Self::List(Rc::new(RefCell::new(value))) } } -impl From, NbtItem>> for NbtList { +impl From for NbtList { #[inline] - fn from(value: HashMap, NbtItem>) -> Self { - Self::Compound(Rc::new(RefCell::new(value))) + fn from(value: Compound) -> Self { + Self::Compound(value.0, Rc::new(RefCell::new(value.1))) } } diff --git a/src/main.rs b/src/main.rs index 42efff5..5dab204 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,19 +5,22 @@ mod read; fn main() { println!("Hello, world!"); + small_read_test(); big_read_test(); } // bincode-org/bincode: A binary encoder / decoder implementation in Rust. // https://github.com/bincode-org/bincode -fn big_read_test() { +fn small_read_test() { let data: [u8; 0x21] = [ 0x0A, 0x00, 0x0B, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x08, 0x00, 0x04, 0x6E, 0x61, 0x6D, 0x65, 0x00, 0x09, 0x42, 0x61, 0x6E, 0x61, 0x6E, 0x72, 0x61, 0x6D, 0x61, - 0x00 ]; + 0x00]; read_test(&data); +} +fn big_read_test() { let data: [u8; 0x608] = [ 0x0A, 0x00, 0x05, 0x4C, 0x65, 0x76, 0x65, 0x6C, 0x04, 0x00, 0x08, 0x6C, 0x6F, 0x6E, 0x67, 0x54, 0x65, 0x73, 0x74, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0x00, 0x09, 0x73, 0x68, diff --git a/src/read.rs b/src/read.rs index 74bd3e2..8d13248 100644 --- a/src/read.rs +++ b/src/read.rs @@ -165,20 +165,20 @@ pub mod read_data { // loop 读取长度 name len name value value // 直到一个 End #[cfg(feature = "debug")] - println!("reading compound {} bytes", value.position()); + println!("compound at {} bytes", value.position()); let mut map: HashMap, NbtItem> = HashMap::new(); loop { - // 读取 name - // 直接调之前的方法读 - let name = NbtValue::from_string(value).as_string().unwrap(); let mut type_tag = [0_u8; 1]; _ = value.read(&mut type_tag).unwrap(); - #[cfg(feature = "debug")] - println!("compound type tag {:?} with name: {:?}", type_tag, name); if type_tag == [0x00] { // End break; } + // 读取 name + // 直接调之前的方法读 + let name = NbtValue::from_string(value).as_string().unwrap(); + #[cfg(feature = "debug")] + println!("compound type tag {:?} with name: {:?}", type_tag, name); // 读取 value let nbt_value: NbtItem = match type_tag { [0x01] => NbtItem::Value(NbtValue::from_bool(value)), @@ -190,7 +190,16 @@ pub mod read_data { [0x07] => NbtItem::from(from_bool_array(value)), [0x08] => NbtItem::Value(NbtValue::from_string(value)), [0x09] => NbtItem::from(from_nbt_list(value)), - [0x0A] => NbtItem::from(from_compound(value)), + [0x0A] => { + let item = match from_compound(value) { + NbtList::Compound(mut get_name, item) => { + get_name = name.clone(); + NbtList::Compound(get_name, item) + }, + _ => panic!("WTF") + }; + NbtItem::from(item) + }, [0x0B] => NbtItem::from(from_i32_array(value)), [0x0C] => NbtItem::from(from_i64_array(value)), _ => { @@ -210,7 +219,7 @@ pub mod read_data { #[cfg(feature = "debug")] println!("compound: {:?}", map); } - NbtList::from(map) + NbtList::from((Arc::from(""), map)) } } @@ -276,6 +285,13 @@ impl TryFrom> for NbtItem { break; } NbtStatus::Going(item) => { + let item = match item { + NbtItem::Array(NbtList::Compound(mut get_name, map)) => { + get_name = name; + NbtItem::Array(NbtList::Compound(get_name, map)) + } + _ => item, + }; items.push(item); if !value.has_data_left().unwrap() { break;