先这样吧(
This commit is contained in:
parent
acb40e2f0f
commit
8a5e7a7bde
@ -9,6 +9,87 @@ pub type NbtLength = i32;
|
||||
#[allow(unused)]
|
||||
pub type StringLength = u16;
|
||||
|
||||
pub struct NbtData {
|
||||
pub head: usize,
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
impl NbtData {
|
||||
pub fn new(data: Vec<u8>) -> Self { Self { head: 0, data } }
|
||||
pub fn get_mut(&mut self) -> &mut [u8] {
|
||||
let (_, data) = self.data.split_at_mut(self.head);
|
||||
data
|
||||
}
|
||||
pub fn push_head(&mut self, length: usize) -> usize {
|
||||
self.head += length;
|
||||
self.head
|
||||
}
|
||||
pub fn read_byte(&mut self) -> i8 {
|
||||
let value = self.data[self.head] as i8;
|
||||
self.head += 1;
|
||||
value
|
||||
}
|
||||
pub fn read_short(&mut self) -> i16 {
|
||||
let value = i16::from_le_bytes([self.data[self.head], self.data[self.head + 1]]);
|
||||
self.head += 2;
|
||||
value
|
||||
}
|
||||
pub fn read_int(&mut self) -> i32 {
|
||||
let value = i32::from_le_bytes([
|
||||
self.data[self.head],
|
||||
self.data[self.head + 1],
|
||||
self.data[self.head + 2],
|
||||
self.data[self.head + 3],
|
||||
]);
|
||||
self.head += 4;
|
||||
value
|
||||
}
|
||||
pub fn read_long(&mut self) -> i64 {
|
||||
let value = i64::from_le_bytes([
|
||||
self.data[self.head],
|
||||
self.data[self.head + 1],
|
||||
self.data[self.head + 2],
|
||||
self.data[self.head + 3],
|
||||
self.data[self.head + 4],
|
||||
self.data[self.head + 5],
|
||||
self.data[self.head + 6],
|
||||
self.data[self.head + 7],
|
||||
]);
|
||||
self.head += 8;
|
||||
value
|
||||
}
|
||||
pub fn read_float(&mut self) -> f32 {
|
||||
let value = f32::from_le_bytes([
|
||||
self.data[self.head],
|
||||
self.data[self.head + 1],
|
||||
self.data[self.head + 2],
|
||||
self.data[self.head + 3],
|
||||
]);
|
||||
self.head += 4;
|
||||
value
|
||||
}
|
||||
pub fn read_double(&mut self) -> f64 {
|
||||
let value = f64::from_le_bytes([
|
||||
self.data[self.head],
|
||||
self.data[self.head + 1],
|
||||
self.data[self.head + 2],
|
||||
self.data[self.head + 3],
|
||||
self.data[self.head + 4],
|
||||
self.data[self.head + 5],
|
||||
self.data[self.head + 6],
|
||||
self.data[self.head + 7],
|
||||
]);
|
||||
self.head += 8;
|
||||
value
|
||||
}
|
||||
pub fn read_bytes(&mut self, length: usize) -> &[u8] {
|
||||
let (_, data) = self.data.split_at_mut(self.head);
|
||||
self.head += length;
|
||||
data
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub struct RawData<'data> {
|
||||
pub raw_data: &'data [u8],
|
||||
@ -65,140 +146,83 @@ pub enum ListContent<'value> {
|
||||
ListList(Vec<ListContent<'value>>),
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
// #[allow(unused)]
|
||||
impl<'value> Value<'value> {
|
||||
#[inline(always)]
|
||||
pub fn read_byte(data: &mut [u8]) -> Self {
|
||||
let (value, data) = data.split_at(1);
|
||||
Self::Byte(value[0] as i8)
|
||||
}
|
||||
pub fn read_byte(data: &mut NbtData) -> Self { Self::Byte(data.read_byte()) }
|
||||
#[inline(always)]
|
||||
pub fn read_short(data: &mut [u8]) -> Self {
|
||||
let (value, data) = data.split_at(2);
|
||||
#[cfg(target_endian = "little")]
|
||||
return Self::Short(i16::from_be_bytes([value[0], value[1]]));
|
||||
#[cfg(target_endian = "big")]
|
||||
return Self::Short(i16::from_le_bytes([value[0], value[1]]));
|
||||
}
|
||||
pub fn read_short(data: &mut NbtData) -> Self { Self::Short(data.read_short()) }
|
||||
#[inline(always)]
|
||||
pub fn read_int(data: &mut [u8]) -> Self {
|
||||
let (value, data) = data.split_at(4);
|
||||
#[cfg(target_endian = "little")]
|
||||
return Self::Int(i32::from_be_bytes([value[0], value[1], value[2], value[3]]));
|
||||
#[cfg(target_endian = "big")]
|
||||
return Self::Int(i32::from_le_bytes([value[0], value[1], value[2], value[3]]));
|
||||
}
|
||||
pub fn read_int(data: &mut NbtData) -> Self { Self::Int(data.read_int()) }
|
||||
#[inline(always)]
|
||||
pub fn read_long(data: &mut [u8]) -> Self {
|
||||
let (value, data) = data.split_at(8);
|
||||
#[cfg(target_endian = "little")]
|
||||
return Self::Long(i64::from_be_bytes([
|
||||
value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7],
|
||||
]));
|
||||
#[cfg(target_endian = "big")]
|
||||
return Self::Long(i64::from_le_bytes([
|
||||
value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7],
|
||||
]));
|
||||
}
|
||||
pub fn read_long(data: &mut NbtData) -> Self { Self::Long(data.read_long()) }
|
||||
#[inline(always)]
|
||||
pub fn read_float(data: &mut [u8]) -> Self {
|
||||
let (value, data) = data.split_at(4);
|
||||
#[cfg(target_endian = "little")]
|
||||
return Self::Float(f32::from_be_bytes([value[0], value[1], value[2], value[3]]));
|
||||
#[cfg(target_endian = "big")]
|
||||
return Self::Float(f32::from_le_bytes([value[0], value[1], value[2], value[3]]));
|
||||
}
|
||||
pub fn read_float(data: &mut NbtData) -> Self { Self::Float(data.read_float()) }
|
||||
#[inline(always)]
|
||||
pub fn read_double(data: &mut [u8]) -> Self {
|
||||
let (value, data) = data.split_at(8);
|
||||
#[cfg(target_endian = "little")]
|
||||
return Self::Double(f64::from_be_bytes([
|
||||
value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7],
|
||||
]));
|
||||
#[cfg(target_endian = "big")]
|
||||
return Self::Double(f64::from_le_bytes([
|
||||
value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7],
|
||||
]));
|
||||
}
|
||||
pub fn read_double(data: &mut NbtData) -> Self { Self::Double(data.read_double()) }
|
||||
#[inline(always)]
|
||||
pub fn read_string(data: &'value mut [u8]) -> Self {
|
||||
let (length, data) = data.split_at(2);
|
||||
let length = u16::from_le_bytes([length[0], length[1]]);
|
||||
let (value, data) = data.split_at(length as usize);
|
||||
pub fn read_string(data: &mut NbtData) -> Self {
|
||||
let length = data.read_short();
|
||||
let value = data.read_bytes(length as usize);
|
||||
Self::String(std::str::from_utf8(value).unwrap())
|
||||
}
|
||||
pub fn read_list(data: &'value mut [u8]) -> Self {
|
||||
pub fn read_list(data: &mut NbtData) -> Self {
|
||||
// 内容类型
|
||||
let (type_id, data) = data.split_at(1);
|
||||
let type_id = type_id[0];
|
||||
let type_id = data.read_byte();
|
||||
// 内容长度
|
||||
let (length, mut data) = data.split_at(4);
|
||||
let length = i32::from_le_bytes([length[0], length[1], length[2], length[3]]);
|
||||
let length = data.read_int();
|
||||
match type_id {
|
||||
0 => panic!("WTF, type_id = 0"),
|
||||
1 => {
|
||||
let mut list = Vec::with_capacity(length as usize);
|
||||
for _ in 0..length {
|
||||
let (value, data) = data.split_at(1);
|
||||
list.push(value[0] as i8);
|
||||
list.push(data.read_byte());
|
||||
}
|
||||
Self::List(ListContent::ByteList(list))
|
||||
}
|
||||
2 => {
|
||||
let mut list = Vec::with_capacity(length as usize);
|
||||
for _ in 0..length {
|
||||
let (value, data) = data.split_at(2);
|
||||
list.push(i16::from_le_bytes([value[0], value[1]]));
|
||||
list.push(data.read_short());
|
||||
}
|
||||
Self::List(ListContent::ShortList(list))
|
||||
}
|
||||
3 => {
|
||||
let mut list = Vec::with_capacity(length as usize);
|
||||
for _ in 0..length {
|
||||
let (value, data) = data.split_at(4);
|
||||
list.push(i32::from_le_bytes([value[0], value[1], value[2], value[3]]));
|
||||
list.push(data.read_int());
|
||||
}
|
||||
Self::List(ListContent::IntList(list))
|
||||
}
|
||||
4 => {
|
||||
let mut list = Vec::with_capacity(length as usize);
|
||||
for _ in 0..length {
|
||||
let (value, data) = data.split_at(8);
|
||||
list.push(i64::from_le_bytes([
|
||||
value[0], value[1], value[2], value[3], value[4], value[5], value[6],
|
||||
value[7],
|
||||
]));
|
||||
list.push(data.read_long());
|
||||
}
|
||||
Self::List(ListContent::LongList(list))
|
||||
}
|
||||
5 => {
|
||||
let mut list = Vec::with_capacity(length as usize);
|
||||
for _ in 0..length {
|
||||
let (value, data) = data.split_at(4);
|
||||
list.push(f32::from_le_bytes([value[0], value[1], value[2], value[3]]));
|
||||
list.push(data.read_float());
|
||||
}
|
||||
Self::List(ListContent::FloatList(list))
|
||||
}
|
||||
6 => {
|
||||
let mut list = Vec::with_capacity(length as usize);
|
||||
for _ in 0..length {
|
||||
let (value, data) = data.split_at(8);
|
||||
list.push(f64::from_le_bytes([
|
||||
value[0], value[1], value[2], value[3], value[4], value[5], value[6],
|
||||
value[7],
|
||||
]))
|
||||
list.push(data.read_double());
|
||||
}
|
||||
Self::List(ListContent::DoubleList(list))
|
||||
}
|
||||
7 => {
|
||||
let mut list = Vec::with_capacity(length as usize);
|
||||
for _ in 0..length {
|
||||
let (len, data) = data.split_at(4);
|
||||
let len = i32::from_le_bytes([len[0], len[1], len[2], len[3]]);
|
||||
let (value, data) = data.split_at(len as usize);
|
||||
let length = data.read_int();
|
||||
let value = data.read_bytes(length as usize);
|
||||
list.push(RawData {
|
||||
raw_data: value,
|
||||
length: len as usize,
|
||||
length: length as usize,
|
||||
});
|
||||
}
|
||||
Self::List(ListContent::ByteArrayList(list))
|
||||
@ -206,9 +230,8 @@ impl<'value> Value<'value> {
|
||||
8 => {
|
||||
let mut list = Vec::with_capacity(length as usize);
|
||||
for _ in 0..length {
|
||||
let (len, data) = data.split_at(2);
|
||||
let len = u16::from_le_bytes([len[0], len[1]]);
|
||||
let (value, data) = data.split_at(len as usize);
|
||||
let length = data.read_int();
|
||||
let value = data.read_bytes(length as usize);
|
||||
let value = std::str::from_utf8(value).unwrap();
|
||||
list.push(value);
|
||||
}
|
||||
@ -218,7 +241,7 @@ impl<'value> Value<'value> {
|
||||
// 好好好, list 嵌套 list 是吧
|
||||
let mut list = Vec::with_capacity(length as usize);
|
||||
for _ in 0..length {
|
||||
let inner_list = Self::read_list(data);
|
||||
// let inner_list = Self::read_list(data);
|
||||
}
|
||||
Self::List(ListContent::ListList(list))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user