Different way of parsing packets

pull/5/head
Wyatt Herkamp 2 years ago
parent 4d1353afd2
commit 9d03b8ae51

@ -18,7 +18,11 @@ pub fn simple_test() {
let protocol = Protocol::new(arc.clone()); let protocol = Protocol::new(arc.clone());
let protocol1 = protocol.get_protocol(); let protocol1 = protocol.get_protocol();
match protocol1 { match protocol1 {
Ok(_) => {} Ok(data) => {
for x in data.play.to_server.types {
println!("{:#?}", x);
}
}
Err(error) => { Err(error) => {
panic!("Minecraft Version {} could not be parsed into a Protocol object: {}", arc.minecraft_version, error); panic!("Minecraft Version {} could not be parsed into a Protocol object: {}", arc.minecraft_version, error);
} }

@ -63,7 +63,7 @@ impl Into<PacketDataType> for DataTypeReference {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Packet { pub struct Packet {
pub name: String, pub name: String,
pub data: Vec<(String, DataTypeReference)>, pub data: PacketDataType,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -106,45 +106,15 @@ impl<'de> Deserialize<'de> for PacketTypes {
} else { } else {
return Err(de::Error::custom("Invalid Packet Mapper")); return Err(de::Error::custom("Invalid Packet Mapper"));
} }
} else if let Value::Array(mut array) = value { } else if let Value::Array( array) = value {
let last = array.pop().ok_or_else(|| de::Error::missing_field("missing content"))?; let value1 = Value::Array(vec![Value::String(key.clone()), Value::Array(array)]);
if let Value::Array(array) = last { println!("{:#?}", value1);
let mut packet_content = Vec::new(); let inner_type = types::build_inner_type(value1);
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)));
}
}
packets.push(Packet { packets.push(Packet {
name: key, name: key,
data: packet_content, data: *inner_type,
}); });
} else {
return Err(de::Error::custom(format!("Invalid Packet {}", key)));
}
} else { } else {
return Err(de::Error::custom(format!("Invalid Packet Expected Array {}", key))); return Err(de::Error::custom(format!("Invalid Packet Expected Array {}", key)));
} }

@ -295,17 +295,15 @@ impl NativeType {
obj.remove("countType") obj.remove("countType")
.unwrap_or_default() .unwrap_or_default()
.as_str() .as_str()
.unwrap(), .unwrap_or_default(),
Cow::Owned(Value::Null), Cow::Owned(Value::Null),
); ).unwrap_or(NativeType::VarInt);
let inner_type = build_inner_type(obj.remove("type").unwrap_or_default()); let inner_type = build_inner_type(obj.remove("type").unwrap_or_default());
if let Some(v) = value {
return Some(NativeType::Array { return Some(NativeType::Array {
count_type: Box::new(v), count_type: Box::new(value),
array_type: inner_type, array_type: inner_type,
}); });
} }
}
None None
} }
@ -370,7 +368,7 @@ impl NativeType {
} }
#[inline] #[inline]
fn build_inner_type(value: Value) -> Box<PacketDataType> { pub(crate) fn build_inner_type(value: Value) -> Box<PacketDataType> {
match value { match value {
Value::String(simple_type) => { Value::String(simple_type) => {
return if let Some(simple_type) = NativeType::new(&simple_type, Cow::Owned(Value::Null)) 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 struct PacketDataTypes {
pub types: Vec<PacketDataType>, pub types: Vec<PacketDataType>,
} }

Loading…
Cancel
Save