Works. However, it is broken on the 21w07a,20w14a,20w13b

pull/5/head
Wyatt Herkamp 2 years ago
parent 1452ced77f
commit d76dc0d344

@ -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);
}
}
}

@ -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<Packet>,
}
impl<'de> Deserialize<'de> for PacketTypes {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
while let Some(key) = map.next_key::<String>()? {
if key.eq("types") {
let mut packets = Vec::new();
let mut packet_mapper = None;
let value = map.next_value::<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)
}
}

@ -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<String, String>,
}
#[derive(Deserialize, Debug)]
pub struct PacketSwitch {
#[serde(rename = "compareTo")]
pub compare_to: String,
pub fields: HashMap<String, String>,
}
#[derive(Debug)]
pub struct PacketMapperSwitch {
pub mapper: PacketMapper,
pub switch: PacketSwitch,
}
impl<'de> Deserialize<'de> for PacketMapperSwitch {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut mapper = None;
let mut switch = None;
while let Some(value) = seq.next_element::<Value>()? {
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)
}
}

@ -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<PacketDataType> {
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<PacketDataType>,
}

Loading…
Cancel
Save