diff --git a/shen-nbt5/src/lib.rs b/shen-nbt5/src/lib.rs index 6b9ef0d..58d2b35 100644 --- a/shen-nbt5/src/lib.rs +++ b/shen-nbt5/src/lib.rs @@ -1,5 +1,47 @@ +/// 用于读取 NBT 数据 +pub struct NbtReader<'data> { + data: &'data [u8], + cursor: usize, +} + +impl NbtReader<'_> { + pub fn new(data: &[u8]) -> NbtReader { + NbtReader { + data, + cursor: 0, + } + } + + pub fn read_i8(&mut self) -> i8 { + let value = self.data[self.cursor] as i8; + self.cursor += 1; + value + } + pub fn read_i16(&mut self) -> i16 { + unsafe { + let value = *(self.data[self.cursor..].as_ptr() as *const i16); + self.cursor += 2; + value.to_be() + } + } + pub fn read_i32(&mut self) -> i32 { + unsafe { + let value = *(self.data[self.cursor..].as_ptr() as *const i32); + self.cursor += 4; + value.to_be() + } + } + pub fn read_i64(&mut self) -> i64 { + unsafe { + let value = *(self.data[self.cursor..].as_ptr() as *const i64); + self.cursor += 8; + value.to_be() + } + } +} + #[derive(Debug, Clone)] pub enum NbtValue<'value> { // end: 0 @@ -15,17 +57,18 @@ pub enum NbtValue<'value> { Float(f32), /// 6 Double(f64), - /// 8 - String(&'value str), /// 7 ByteArray(Vec), - /// 11 - IntArray(Vec), - /// 12 - LongArray(Vec), + /// 8 + /// 或者叫 u8 array + String(&'value str), /// 9 List(Vec>), /// 10 Compound(Vec<(String, NbtValue<'value>)>), + /// 11 + IntArray(Vec), + /// 12 + LongArray(Vec), }