From 9d03b8ae51e2ed8a33002795aab59d912b8335a0 Mon Sep 17 00:00:00 2001 From: Wyatt Herkamp Date: Thu, 23 Jun 2022 08:26:01 -0400 Subject: [PATCH] Different way of parsing packets --- src/api/tests/protocol.rs | 6 ++++- src/models/protocol/mod.rs | 44 ++++++------------------------------ src/models/protocol/types.rs | 18 +++++++-------- 3 files changed, 20 insertions(+), 48 deletions(-) diff --git a/src/api/tests/protocol.rs b/src/api/tests/protocol.rs index 250bd54..6ab309f 100644 --- a/src/api/tests/protocol.rs +++ b/src/api/tests/protocol.rs @@ -18,7 +18,11 @@ pub fn simple_test() { let protocol = Protocol::new(arc.clone()); let protocol1 = protocol.get_protocol(); match protocol1 { - Ok(_) => {} + Ok(data) => { + for x in data.play.to_server.types { + println!("{:#?}", x); + } + } Err(error) => { panic!("Minecraft Version {} could not be parsed into a Protocol object: {}", arc.minecraft_version, error); } diff --git a/src/models/protocol/mod.rs b/src/models/protocol/mod.rs index 120af8d..8f8e508 100644 --- a/src/models/protocol/mod.rs +++ b/src/models/protocol/mod.rs @@ -63,7 +63,7 @@ impl Into for DataTypeReference { #[derive(Debug, Clone)] pub struct Packet { pub name: String, - pub data: Vec<(String, DataTypeReference)>, + pub data: PacketDataType, } #[derive(Debug, Clone)] @@ -106,45 +106,15 @@ impl<'de> Deserialize<'de> for PacketTypes { } else { return Err(de::Error::custom("Invalid Packet Mapper")); } - } else if let Value::Array(mut array) = value { - let last = array.pop().ok_or_else(|| de::Error::missing_field("missing content"))?; - if let Value::Array(array) = last { - let mut packet_content = Vec::new(); - for value in array.into_iter() { - if let Value::Object(mut obj) = value { - let name = obj.remove("name"); - let name = if let Some(name) = name { - name.to_string() - } else { - "anon".to_string() - }; - let value = obj.remove("type").ok_or_else(|| de::Error::custom(format!("Packet ID {} missing type", key)))?; - let value = match value { - Value::String(simple) => { - DataTypeReference::Simple(simple) - } - Value::Array(mut array) => { - let properties = array.pop().ok_or_else(|| de::Error::custom(format!("Packet ID {} missing properties", key)))?; - let name = array.pop().ok_or_else(|| de::Error::custom(format!("Packet ID {} missing name", key)))?.to_string(); - DataTypeReference::Complex { - name, - properties, - } - } - _ => return Err(de::Error::custom(format!("Invalid Packet Invalid Type {}", key))) - }; - packet_content.push((name, value)); - } else { - return Err(de::Error::custom(format!("Invalid Packet Expected Object {}", key))); - } - } + } else if let Value::Array( array) = value { + let value1 = Value::Array(vec![Value::String(key.clone()), Value::Array(array)]); + println!("{:#?}", value1); + let inner_type = types::build_inner_type(value1); packets.push(Packet { name: key, - data: packet_content, + data: *inner_type, }); - } else { - return Err(de::Error::custom(format!("Invalid Packet {}", key))); - } + } else { return Err(de::Error::custom(format!("Invalid Packet Expected Array {}", key))); } diff --git a/src/models/protocol/types.rs b/src/models/protocol/types.rs index 8914128..86214f7 100644 --- a/src/models/protocol/types.rs +++ b/src/models/protocol/types.rs @@ -295,16 +295,14 @@ impl NativeType { obj.remove("countType") .unwrap_or_default() .as_str() - .unwrap(), + .unwrap_or_default(), Cow::Owned(Value::Null), - ); + ).unwrap_or(NativeType::VarInt); let inner_type = build_inner_type(obj.remove("type").unwrap_or_default()); - if let Some(v) = value { - return Some(NativeType::Array { - count_type: Box::new(v), - array_type: inner_type, - }); - } + return Some(NativeType::Array { + count_type: Box::new(value), + array_type: inner_type, + }); } None } @@ -370,7 +368,7 @@ impl NativeType { } #[inline] -fn build_inner_type(value: Value) -> Box { +pub(crate) fn build_inner_type(value: Value) -> Box { match value { Value::String(simple_type) => { return if let Some(simple_type) = NativeType::new(&simple_type, Cow::Owned(Value::Null)) @@ -495,7 +493,7 @@ impl PacketDataType { } } -#[derive(Debug,Clone)] +#[derive(Debug, Clone)] pub struct PacketDataTypes { pub types: Vec, }