好像修好了?

This commit is contained in:
shenjack 2023-08-05 15:03:21 +08:00
parent f05ea5814d
commit de071737cc
Signed by: shenjack
GPG Key ID: 7B1134A979775551
3 changed files with 37 additions and 16 deletions

View File

@ -26,7 +26,7 @@ pub enum NbtList {
IntArray(Rc<RefCell<Vec<i32>>>), IntArray(Rc<RefCell<Vec<i32>>>),
LongArray(Rc<RefCell<Vec<i64>>>), LongArray(Rc<RefCell<Vec<i64>>>),
List(Rc<RefCell<Vec<NbtItem>>>), List(Rc<RefCell<Vec<NbtItem>>>),
Compound(Rc<RefCell<HashMap<Arc<str>, NbtItem>>>), Compound(Arc<str>, Rc<RefCell<HashMap<Arc<str>, NbtItem>>>),
} }
/// 基本 NBT 数据类型 /// 基本 NBT 数据类型
@ -85,9 +85,11 @@ impl From<Vec<NbtItem>> for NbtItem {
fn from(value: Vec<NbtItem>) -> Self { Self::Array(NbtList::from(value)) } fn from(value: Vec<NbtItem>) -> Self { Self::Array(NbtList::from(value)) }
} }
impl From<HashMap<Arc<str>, NbtItem>> for NbtItem { type Compound = (Arc<str>, HashMap<Arc<str>, NbtItem>);
impl From<Compound> for NbtItem {
#[inline] #[inline]
fn from(value: HashMap<Arc<str>, NbtItem>) -> Self { Self::Array(NbtList::from(value)) } fn from(value: Compound) -> Self { Self::Array(NbtList::from(value)) }
} }
impl From<Vec<bool>> for NbtItem { impl From<Vec<bool>> for NbtItem {
@ -110,10 +112,10 @@ impl From<Vec<NbtItem>> for NbtList {
fn from(value: Vec<NbtItem>) -> Self { Self::List(Rc::new(RefCell::new(value))) } fn from(value: Vec<NbtItem>) -> Self { Self::List(Rc::new(RefCell::new(value))) }
} }
impl From<HashMap<Arc<str>, NbtItem>> for NbtList { impl From<Compound> for NbtList {
#[inline] #[inline]
fn from(value: HashMap<Arc<str>, NbtItem>) -> Self { fn from(value: Compound) -> Self {
Self::Compound(Rc::new(RefCell::new(value))) Self::Compound(value.0, Rc::new(RefCell::new(value.1)))
} }
} }

View File

@ -5,19 +5,22 @@ mod read;
fn main() { fn main() {
println!("Hello, world!"); println!("Hello, world!");
small_read_test();
big_read_test(); big_read_test();
} }
// bincode-org/bincode: A binary encoder / decoder implementation in Rust. // bincode-org/bincode: A binary encoder / decoder implementation in Rust.
// https://github.com/bincode-org/bincode // https://github.com/bincode-org/bincode
fn big_read_test() { fn small_read_test() {
let data: [u8; 0x21] = [ let data: [u8; 0x21] = [
0x0A, 0x00, 0x0B, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x08, 0x00, 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, 0x04, 0x6E, 0x61, 0x6D, 0x65, 0x00, 0x09, 0x42, 0x61, 0x6E, 0x61, 0x6E, 0x72, 0x61, 0x6D, 0x61,
0x00 ]; 0x00];
read_test(&data); read_test(&data);
}
fn big_read_test() {
let data: [u8; 0x608] = [ let data: [u8; 0x608] = [
0x0A, 0x00, 0x05, 0x4C, 0x65, 0x76, 0x65, 0x6C, 0x04, 0x00, 0x08, 0x6C, 0x6F, 0x6E, 0x67, 0x54, 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, 0x65, 0x73, 0x74, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0x00, 0x09, 0x73, 0x68,

View File

@ -165,20 +165,20 @@ pub mod read_data {
// loop 读取长度 name len name value value // loop 读取长度 name len name value value
// 直到一个 End // 直到一个 End
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
println!("reading compound {} bytes", value.position()); println!("compound at {} bytes", value.position());
let mut map: HashMap<Arc<str>, NbtItem> = HashMap::new(); let mut map: HashMap<Arc<str>, NbtItem> = HashMap::new();
loop { loop {
// 读取 name
// 直接调之前的方法读
let name = NbtValue::from_string(value).as_string().unwrap();
let mut type_tag = [0_u8; 1]; let mut type_tag = [0_u8; 1];
_ = value.read(&mut type_tag).unwrap(); _ = value.read(&mut type_tag).unwrap();
#[cfg(feature = "debug")]
println!("compound type tag {:?} with name: {:?}", type_tag, name);
if type_tag == [0x00] { if type_tag == [0x00] {
// End // End
break; break;
} }
// 读取 name
// 直接调之前的方法读
let name = NbtValue::from_string(value).as_string().unwrap();
#[cfg(feature = "debug")]
println!("compound type tag {:?} with name: {:?}", type_tag, name);
// 读取 value // 读取 value
let nbt_value: NbtItem = match type_tag { let nbt_value: NbtItem = match type_tag {
[0x01] => NbtItem::Value(NbtValue::from_bool(value)), [0x01] => NbtItem::Value(NbtValue::from_bool(value)),
@ -190,7 +190,16 @@ pub mod read_data {
[0x07] => NbtItem::from(from_bool_array(value)), [0x07] => NbtItem::from(from_bool_array(value)),
[0x08] => NbtItem::Value(NbtValue::from_string(value)), [0x08] => NbtItem::Value(NbtValue::from_string(value)),
[0x09] => NbtItem::from(from_nbt_list(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)), [0x0B] => NbtItem::from(from_i32_array(value)),
[0x0C] => NbtItem::from(from_i64_array(value)), [0x0C] => NbtItem::from(from_i64_array(value)),
_ => { _ => {
@ -210,7 +219,7 @@ pub mod read_data {
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
println!("compound: {:?}", map); println!("compound: {:?}", map);
} }
NbtList::from(map) NbtList::from((Arc::from(""), map))
} }
} }
@ -276,6 +285,13 @@ impl TryFrom<Cursor<&[u8]>> for NbtItem {
break; break;
} }
NbtStatus::Going(item) => { 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); items.push(item);
if !value.has_data_left().unwrap() { if !value.has_data_left().unwrap() {
break; break;