From d76dc0d3445c7fe57e0d5df24c9b2c396d6599f1 Mon Sep 17 00:00:00 2001 From: Wyatt Herkamp Date: Sat, 18 Jun 2022 10:36:52 -0400 Subject: [PATCH] Works. However, it is broken on the 21w07a,20w14a,20w13b --- src/api/tests/protocol.rs | 19 ++-- src/models/protocol/mod.rs | 136 ++++++++++++++++++++++++++- src/models/protocol/packet_mapper.rs | 84 +++++++++++++++++ src/models/protocol/types.rs | 7 +- 4 files changed, 232 insertions(+), 14 deletions(-) create mode 100644 src/models/protocol/packet_mapper.rs diff --git a/src/api/tests/protocol.rs b/src/api/tests/protocol.rs index f6f09f8..4b8fdb2 100644 --- a/src/api/tests/protocol.rs +++ b/src/api/tests/protocol.rs @@ -1,21 +1,22 @@ use crate::api::protocol::Protocol; use crate::api::tests::get_test_versions; use crate::models::protocol::PacketDataType; -use crate::Api; use std::sync::Arc; +use crate::DataResult; #[test] pub fn simple_test() { let versions = get_test_versions(); for x in versions { - let protocol = Protocol::new(Arc::new(x)); - let protocol1 = protocol.get_protocol().unwrap(); - for protocol in protocol1.types.types { - match protocol { - PacketDataType::Other(other, data) => { - println!("{:?} data {:?}", other, data); - } - _ => {} + let arc = Arc::new(x); + let protocol = Protocol::new(arc.clone()); + let protocol1 = protocol.get_protocol(); + match protocol1 { + Ok(_) => { + + } + Err(error) => { + println!("{:?} On Version {}", error, arc.minecraft_version); } } } diff --git a/src/models/protocol/mod.rs b/src/models/protocol/mod.rs index e69c813..cd0d608 100644 --- a/src/models/protocol/mod.rs +++ b/src/models/protocol/mod.rs @@ -1,9 +1,141 @@ pub mod types; +pub mod packet_mapper; - +use std::fmt; +use serde::{de, Deserialize, Deserializer}; +use serde::de::{MapAccess, Visitor}; +use serde_json::Value; +pub use packet_mapper::{PacketMapper, PacketSwitch, PacketMapperSwitch}; pub use types::{BitField, NativeType, PacketDataType, PacketDataTypes}; -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] pub struct Protocol { pub types: PacketDataTypes, + pub handshaking: PacketGrouping, + pub status: PacketGrouping, + pub login: PacketGrouping, + pub play: PacketGrouping, +} + +#[derive(Deserialize, Debug)] +pub struct PacketGrouping { + #[serde(rename = "toServer")] + pub to_server: PacketTypes, + #[serde(rename = "toClient")] + pub to_client: PacketTypes, +} + +#[derive(Debug)] +pub enum DataTypeReference { + Simple(String), + Complex { + name: String, + properties: Value, + }, +} + +#[derive(Debug)] +pub struct Packet { + pub name: String, + pub data: Vec<(String, DataTypeReference)>, +} + +#[derive(Debug)] +pub struct PacketTypes { + pub packet_mapper: PacketMapperSwitch, + pub types: Vec, +} + + +impl<'de> Deserialize<'de> for PacketTypes { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + struct PacketTypesVisitor; + + impl<'de> Visitor<'de> for PacketTypesVisitor { + type Value = PacketTypes; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("Expected a map") + } + + fn visit_map(self, mut map: A) -> Result + where + A: MapAccess<'de>, + { + while let Some(key) = map.next_key::()? { + if key.eq("types") { + let mut packets = Vec::new(); + let mut packet_mapper = None; + let value = map.next_value::()?; + if let Value::Object(obj) = value { + for (key, value) in obj.into_iter() { + if key.eq("packet") { + if let Value::Array(mut array) = value { + let value = array.pop().ok_or_else(|| de::Error::missing_field("missing content"))?; + let value: PacketMapperSwitch = serde_json::from_value(value).map_err(de::Error::custom)?; + packet_mapper = Some(value); + } 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))); + } + } + packets.push(Packet { + name: key, + data: packet_content, + }); + } else { + return Err(de::Error::custom(format!("Invalid Packet {}", key))); + } + } else { + return Err(de::Error::custom(format!("Invalid Packet Expected Array {}", key))); + } + } + } + + let packet_mapper = packet_mapper.ok_or_else(|| de::Error::missing_field("packet_mapper"))?; + return Ok(PacketTypes { + packet_mapper, + types: packets, + }); + } + } + Err(de::Error::custom("Expected a types")) + } + } + + deserializer.deserialize_map(PacketTypesVisitor) + } } diff --git a/src/models/protocol/packet_mapper.rs b/src/models/protocol/packet_mapper.rs new file mode 100644 index 0000000..27bda65 --- /dev/null +++ b/src/models/protocol/packet_mapper.rs @@ -0,0 +1,84 @@ +use std::collections::HashMap; +use std::fmt; + +use serde::de::{SeqAccess, Visitor}; +use serde::{de, Deserialize, Deserializer}; +use serde_json::Value; + +#[derive(Deserialize, Debug)] +pub struct PacketMapper { + /// A Type + #[serde(rename = "type")] + pub map_type: String, + pub mappings: HashMap, +} + +#[derive(Deserialize, Debug)] +pub struct PacketSwitch { + #[serde(rename = "compareTo")] + pub compare_to: String, + pub fields: HashMap, +} + +#[derive(Debug)] +pub struct PacketMapperSwitch { + pub mapper: PacketMapper, + pub switch: PacketSwitch, +} + + +impl<'de> Deserialize<'de> for PacketMapperSwitch { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + struct PacketMapperSwitchVisitor; + + impl<'de> Visitor<'de> for PacketMapperSwitchVisitor { + type Value = PacketMapperSwitch; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("Expected a sequence") + } + fn visit_seq(self, mut seq: A) -> Result + where + A: SeqAccess<'de>, + { + let mut mapper = None; + let mut switch = None; + while let Some(value) = seq.next_element::()? { + if let Value::Object(mut value) = value { + let value = value.remove("type").ok_or_else(|| de::Error::missing_field("type"))?; + if let Value::Array(mut array) = value { + let value = array.pop().ok_or_else(|| de::Error::missing_field("missing content"))?; + let key = array.pop().ok_or_else(|| de::Error::missing_field("missing key"))?; + if let Value::String(key) = key { + if key.eq("mapper") { + let value: PacketMapper = serde_json::from_value(value).map_err(de::Error::custom)?; + mapper = Some(value); + } else if key.eq("switch") { + let value: PacketSwitch = serde_json::from_value(value).map_err(de::Error::custom)?; + switch = Some(value); + }else{ + return Err(de::Error::custom("unknown key")); + } + }else{ + return Err(de::Error::custom("unknown key")); + } + } else { + return Err(de::Error::custom("Expected an array")); + } + } + } + let map_type = mapper.ok_or_else(|| de::Error::missing_field("mapper"))?; + let switch = switch.ok_or_else(|| de::Error::missing_field("switch"))?; + Ok(PacketMapperSwitch { + mapper: map_type, + switch, + }) + } + } + + deserializer.deserialize_seq(PacketMapperSwitchVisitor) + } +} diff --git a/src/models/protocol/types.rs b/src/models/protocol/types.rs index faf4805..35539f3 100644 --- a/src/models/protocol/types.rs +++ b/src/models/protocol/types.rs @@ -4,7 +4,7 @@ use serde_json::Value; use std::borrow::{Cow}; use std::collections::HashMap; -#[derive(Deserialize)] +#[derive(Deserialize,Debug)] pub struct BitField { pub name: String, pub size: i64, @@ -14,6 +14,7 @@ pub struct BitField { /// These data types should be available in every version. /// However, they won't break anything if not present /// This can also be known as the Native Types +#[derive(Debug)] pub enum NativeType { /// Please read the following link for information on parsing https://wiki.vg/Protocol#VarInt_and_VarLong VarInt, @@ -266,7 +267,7 @@ fn build_inner_type(value: Value) -> Box { v => Box::new(PacketDataType::Other(String::new(), v)), } } - +#[derive(Debug)] pub enum PacketDataType { // Just a pure native type Native(NativeType), @@ -308,7 +309,7 @@ impl PacketDataType { } } } - +#[derive(Debug)] pub struct PacketDataTypes { pub types: Vec, }