|
|
@ -1,13 +1,14 @@
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use std::fmt::Formatter;
|
|
|
|
|
|
|
|
use serde::{Deserialize, Deserializer};
|
|
|
|
|
|
|
|
use serde::de::Visitor;
|
|
|
|
use serde::de::Visitor;
|
|
|
|
|
|
|
|
use serde::{Deserialize, Deserializer};
|
|
|
|
use serde_json::Value;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
use std::borrow::{Cow};
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct BitField {
|
|
|
|
pub struct BitField {
|
|
|
|
name: String,
|
|
|
|
pub name: String,
|
|
|
|
size: usize,
|
|
|
|
pub size: i64,
|
|
|
|
signed: bool,
|
|
|
|
pub signed: bool,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// These data types should be available in every version.
|
|
|
|
/// These data types should be available in every version.
|
|
|
@ -17,7 +18,7 @@ pub enum NativeType {
|
|
|
|
/// Please read the following link for information on parsing https://wiki.vg/Protocol#VarInt_and_VarLong
|
|
|
|
/// Please read the following link for information on parsing https://wiki.vg/Protocol#VarInt_and_VarLong
|
|
|
|
VarInt,
|
|
|
|
VarInt,
|
|
|
|
PString {
|
|
|
|
PString {
|
|
|
|
count_type: Box<NativeType>
|
|
|
|
count_type: Box<NativeType>,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Buffer {
|
|
|
|
Buffer {
|
|
|
|
count_type: Box<NativeType>,
|
|
|
|
count_type: Box<NativeType>,
|
|
|
@ -37,10 +38,10 @@ pub enum NativeType {
|
|
|
|
// Optional<MinecraftPacketDataType>
|
|
|
|
// Optional<MinecraftPacketDataType>
|
|
|
|
Option(Box<PacketDataType>),
|
|
|
|
Option(Box<PacketDataType>),
|
|
|
|
EntityMetadataLoop {
|
|
|
|
EntityMetadataLoop {
|
|
|
|
end_val: i32,
|
|
|
|
end_val: i64,
|
|
|
|
metadata_type: Box<NativeType>,
|
|
|
|
metadata_type: Box<PacketDataType>,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TopBitSetTerminatedArray(Box<NativeType>),
|
|
|
|
TopBitSetTerminatedArray(Box<PacketDataType>),
|
|
|
|
BitField(Vec<BitField>),
|
|
|
|
BitField(Vec<BitField>),
|
|
|
|
// A set of Name and The Type
|
|
|
|
// A set of Name and The Type
|
|
|
|
Container(Vec<(String, Box<PacketDataType>)>),
|
|
|
|
Container(Vec<(String, Box<PacketDataType>)>),
|
|
|
@ -61,66 +62,37 @@ pub enum NativeType {
|
|
|
|
|
|
|
|
|
|
|
|
impl NativeType {
|
|
|
|
impl NativeType {
|
|
|
|
pub fn contains_type(name: &str) -> bool {
|
|
|
|
pub fn contains_type(name: &str) -> bool {
|
|
|
|
match name {
|
|
|
|
matches!(name, "varint" | "pstring" | "buffer" | "bool" | "u8" | "u16" | "u32" | "u64" | "i8" | "i16" | "i32" | "i64" | "f32" | "f64" | "uuid" | "option" | "entityMetadataLoop" | "topbitsetterminatedarray" | "bitfield" | "container" | "switch" | "void" | "array" | "restbuffer" | "nbt" | "optionalnbt")
|
|
|
|
"varint" => true,
|
|
|
|
|
|
|
|
"pstring" => true,
|
|
|
|
|
|
|
|
"buffer" => true,
|
|
|
|
|
|
|
|
"bool" => true,
|
|
|
|
|
|
|
|
"u8" => true,
|
|
|
|
|
|
|
|
"u16" => true,
|
|
|
|
|
|
|
|
"u32" => true,
|
|
|
|
|
|
|
|
"u64" => true,
|
|
|
|
|
|
|
|
"i8" => true,
|
|
|
|
|
|
|
|
"i16" => true,
|
|
|
|
|
|
|
|
"i32" => true,
|
|
|
|
|
|
|
|
"i64" => true,
|
|
|
|
|
|
|
|
"f32" => true,
|
|
|
|
|
|
|
|
"f64" => true,
|
|
|
|
|
|
|
|
"uuid" => true,
|
|
|
|
|
|
|
|
"option" => true,
|
|
|
|
|
|
|
|
"entitymetadataloop" => true,
|
|
|
|
|
|
|
|
"topbitsetterminatedarray" => true,
|
|
|
|
|
|
|
|
"bitfield" => true,
|
|
|
|
|
|
|
|
"container" => true,
|
|
|
|
|
|
|
|
"switch" => true,
|
|
|
|
|
|
|
|
"void" => true,
|
|
|
|
|
|
|
|
"array" => true,
|
|
|
|
|
|
|
|
"restbuffer" => true,
|
|
|
|
|
|
|
|
"nbt" => true,
|
|
|
|
|
|
|
|
"optionalnbt" => true,
|
|
|
|
|
|
|
|
_ => false,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn new(name: &str, layout: Option<Value>) -> Option<Self> {
|
|
|
|
pub fn new(name: &str, layout: Cow<'_, Value>) -> Option<Self> {
|
|
|
|
match name {
|
|
|
|
match name {
|
|
|
|
"varint" => Some(NativeType::VarInt),
|
|
|
|
"varint" => Some(NativeType::VarInt),
|
|
|
|
"pstring" => {
|
|
|
|
"pstring" => {
|
|
|
|
if let Some(layout) = layout {
|
|
|
|
if let Value::Object(mut obj) = layout.into_owned() {
|
|
|
|
if let Some(value) = &layout.as_object().unwrap().get("countType") {
|
|
|
|
if let Value::String(count_type) = obj.remove("countType").unwrap_or_default() {
|
|
|
|
if let Some(count_type) = NativeType::new(value.as_str().unwrap(), None) {
|
|
|
|
if let Some(count_type) =
|
|
|
|
Some(NativeType::PString {
|
|
|
|
NativeType::new(&count_type, Cow::Owned(Value::Null)) {
|
|
|
|
|
|
|
|
return Some(NativeType::PString {
|
|
|
|
count_type: Box::new(count_type),
|
|
|
|
count_type: Box::new(count_type),
|
|
|
|
})
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"buffer" => {
|
|
|
|
"buffer" => {
|
|
|
|
if let Some(layout) = layout {
|
|
|
|
if let Value::Object(mut obj) = layout.into_owned() {
|
|
|
|
if let Some(count_type) = NativeType::new(&layout["countType"].as_str().unwrap(), None) {
|
|
|
|
if let Value::String(count_type) = obj.remove("countType").unwrap_or_default() {
|
|
|
|
Some(NativeType::Buffer {
|
|
|
|
if let Some(count_type) =
|
|
|
|
count_type: Box::new(count_type),
|
|
|
|
NativeType::new(&count_type, Cow::Owned(Value::Null))
|
|
|
|
})
|
|
|
|
{
|
|
|
|
} else {
|
|
|
|
return Some(NativeType::PString {
|
|
|
|
None
|
|
|
|
count_type: Box::new(count_type),
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { None }
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"bool" => Some(NativeType::Bool),
|
|
|
|
"bool" => Some(NativeType::Bool),
|
|
|
|
"u8" => Some(NativeType::U8),
|
|
|
|
"u8" => Some(NativeType::U8),
|
|
|
@ -134,137 +106,115 @@ impl NativeType {
|
|
|
|
"f32" => Some(NativeType::F32),
|
|
|
|
"f32" => Some(NativeType::F32),
|
|
|
|
"f64" => Some(NativeType::F64),
|
|
|
|
"f64" => Some(NativeType::F64),
|
|
|
|
"uuid" => Some(NativeType::Uuid),
|
|
|
|
"uuid" => Some(NativeType::Uuid),
|
|
|
|
"option" => {
|
|
|
|
"option" => Some(NativeType::Option(build_inner_type(layout.into_owned()))),
|
|
|
|
if let Some(layout) = layout {
|
|
|
|
"entityMetadataLoop" => {
|
|
|
|
let option = layout.as_array().unwrap().get(1);
|
|
|
|
match layout.into_owned() {
|
|
|
|
|
|
|
|
Value::Object(mut layout) => {
|
|
|
|
if let Some(option_type) = option {
|
|
|
|
let end_val = layout
|
|
|
|
let key = option_type.as_str().unwrap();
|
|
|
|
.remove("endVal")
|
|
|
|
let value = PacketDataType::new(key, None).or(Self::new(key, None).and_then(|x| Some(PacketDataType::Native(x))));
|
|
|
|
.and_then(|v| v.as_i64())
|
|
|
|
Some(NativeType::Option(Box::new(value.unwrap())))
|
|
|
|
.unwrap_or_default();
|
|
|
|
} else {
|
|
|
|
let inner_type = layout.remove("type").unwrap_or_default();
|
|
|
|
None
|
|
|
|
let inner_type = build_inner_type(inner_type);
|
|
|
|
|
|
|
|
Some(NativeType::EntityMetadataLoop {
|
|
|
|
|
|
|
|
end_val,
|
|
|
|
|
|
|
|
metadata_type: inner_type,
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_ => None,
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
"entitymetadataloop" => {
|
|
|
|
|
|
|
|
if let Some(layout) = layout {
|
|
|
|
|
|
|
|
if let Some(end_val) = layout["endVal"].as_i64() {
|
|
|
|
|
|
|
|
let value1 = layout["type"].as_array().unwrap();
|
|
|
|
|
|
|
|
if let Some(metadata_type) = NativeType::new(&value1.get(0).unwrap().to_string(), value1.get(1).cloned()) {
|
|
|
|
|
|
|
|
Some(NativeType::EntityMetadataLoop {
|
|
|
|
|
|
|
|
end_val: end_val as i32,
|
|
|
|
|
|
|
|
metadata_type: Box::new(metadata_type),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"topbitsetterminatedarray" => {
|
|
|
|
"topbitsetterminatedarray" => {
|
|
|
|
if let Some(layout) = layout {
|
|
|
|
if let Value::Object(mut layout) = layout.into_owned() {
|
|
|
|
if let Some(count_type) = NativeType::new(&layout["countType"].as_str().unwrap(), None) {
|
|
|
|
let inner_type = layout.remove("type").unwrap_or_default();
|
|
|
|
Some(NativeType::TopBitSetTerminatedArray(Box::new(count_type)))
|
|
|
|
let inner_type = build_inner_type(inner_type);
|
|
|
|
} else {
|
|
|
|
return Some(NativeType::TopBitSetTerminatedArray(inner_type));
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"bitfield" => {
|
|
|
|
"bitfield" => {
|
|
|
|
if let Some(layout) = layout {
|
|
|
|
if let Value::Array(bit_fields) = layout.into_owned() {
|
|
|
|
let bit_fields = layout.as_array().unwrap();
|
|
|
|
let bit_fields_vec = bit_fields
|
|
|
|
let mut bit_fields_vec = Vec::new();
|
|
|
|
.into_iter()
|
|
|
|
for bit_field in bit_fields {
|
|
|
|
.map(|v| serde_json::from_value(v).unwrap())
|
|
|
|
if let Some(name) = bit_field["name"].as_str() {
|
|
|
|
.collect();
|
|
|
|
if let Some(size) = bit_field["size"].as_i64() {
|
|
|
|
|
|
|
|
if let Some(signed) = bit_field["signed"].as_bool() {
|
|
|
|
|
|
|
|
bit_fields_vec.push(BitField {
|
|
|
|
|
|
|
|
name: name.to_string(),
|
|
|
|
|
|
|
|
size: size as usize,
|
|
|
|
|
|
|
|
signed: signed,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(NativeType::BitField(bit_fields_vec))
|
|
|
|
Some(NativeType::BitField(bit_fields_vec))
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"container" => {
|
|
|
|
"container" => {
|
|
|
|
if let Some(layout) = layout {
|
|
|
|
if let Value::Array(containers) = layout.into_owned() {
|
|
|
|
let containers = layout.as_array().unwrap();
|
|
|
|
let containers_vec = containers
|
|
|
|
let mut containers_vec = Vec::new();
|
|
|
|
.into_iter()
|
|
|
|
for container in containers {
|
|
|
|
.map(|v| {
|
|
|
|
if let Some(name) = container["name"].as_str() {
|
|
|
|
if let Value::Object(mut obj) = v {
|
|
|
|
if let Some(type_) = container["type"].as_str() {
|
|
|
|
if let Some(name) = obj.remove("name") {
|
|
|
|
containers_vec.push((name.to_string(), Box::new(PacketDataType::new(type_, None).or(Self::new(type_, None).
|
|
|
|
let name = name.to_string();
|
|
|
|
and_then(|x| Some(PacketDataType::Native(x)))).unwrap())));
|
|
|
|
let inner_type = obj.remove("type").unwrap_or_default();
|
|
|
|
|
|
|
|
let inner_type = build_inner_type(inner_type);
|
|
|
|
|
|
|
|
(name, inner_type)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let inner_type = obj.remove("type").unwrap_or_default();
|
|
|
|
|
|
|
|
let inner_type = build_inner_type(inner_type);
|
|
|
|
|
|
|
|
(String::new(), inner_type)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
panic!("Container is not an object");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
|
|
Some(NativeType::Container(containers_vec))
|
|
|
|
Some(NativeType::Container(containers_vec))
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"switch" => {
|
|
|
|
"switch" => {
|
|
|
|
if let Some(layout) = layout {
|
|
|
|
if let Value::Object(mut layout) = layout.into_owned() {
|
|
|
|
if let Some(name) = layout["compareTo"].as_str() {
|
|
|
|
return Some(NativeType::Switch {
|
|
|
|
if let Some(fields) = layout["fields"].as_object() {
|
|
|
|
compare_to: layout.remove("compareTo").unwrap().to_string(),
|
|
|
|
let fields = fields.iter().map(|(key, v)| {
|
|
|
|
fields: layout
|
|
|
|
(key.to_string(), v.to_string())
|
|
|
|
.remove("fields")
|
|
|
|
}).collect();
|
|
|
|
.and_then(|v| {
|
|
|
|
return Some(NativeType::Switch {
|
|
|
|
if let Value::Object(fields) = v {
|
|
|
|
compare_to: name.to_string(),
|
|
|
|
Some(
|
|
|
|
fields: fields,
|
|
|
|
fields
|
|
|
|
default: None,
|
|
|
|
.into_iter()
|
|
|
|
});
|
|
|
|
.map(|(k, v)| (k, v.to_string()))
|
|
|
|
}
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.unwrap_or_default(),
|
|
|
|
|
|
|
|
default: layout.remove("default").map(|v| v.to_string()),
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"void" => Some(NativeType::Void),
|
|
|
|
"void" => Some(NativeType::Void),
|
|
|
|
"array" => {
|
|
|
|
"array" => {
|
|
|
|
if let Some(layout) = layout {
|
|
|
|
if let Value::Object(mut obj) = layout.into_owned() {
|
|
|
|
let value = layout.as_object().unwrap();
|
|
|
|
let value = NativeType::new(
|
|
|
|
if let Some(count_type) = NativeType::new(&value.get("countType").unwrap().as_str().unwrap(), None) {
|
|
|
|
obj.remove("countType")
|
|
|
|
let type_ = value.get("type").unwrap();
|
|
|
|
.unwrap_or_default()
|
|
|
|
if let Some(type_) = &type_.as_str() {
|
|
|
|
.as_str()
|
|
|
|
return Some(NativeType::Array {
|
|
|
|
.unwrap(),
|
|
|
|
count_type: Box::new(count_type),
|
|
|
|
Cow::Owned(Value::Null),
|
|
|
|
array_type: Box::new(PacketDataType::new(type_, None).or(Self::new(type_, None).
|
|
|
|
);
|
|
|
|
and_then(|x| Some(PacketDataType::Native(x)))).unwrap()),
|
|
|
|
let inner_type = build_inner_type(obj.remove("type").unwrap_or_default());
|
|
|
|
});
|
|
|
|
if let Some(v) = value {
|
|
|
|
} else if let Some(array) = type_.as_array() {
|
|
|
|
return Some(NativeType::Array {
|
|
|
|
let key = array.get(0).unwrap().as_str().unwrap();
|
|
|
|
count_type: Box::new(v),
|
|
|
|
if let Some(inner_type) = PacketDataType::new(key, array.get(1).cloned()).
|
|
|
|
array_type: inner_type,
|
|
|
|
or(Self::new(key, array.get(1).cloned()).and_then(|x| Some(PacketDataType::Native(x)))) {
|
|
|
|
});
|
|
|
|
return Some(NativeType::Array {
|
|
|
|
|
|
|
|
count_type: Box::new(count_type),
|
|
|
|
|
|
|
|
array_type: Box::new(inner_type),
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
println!("Could not parse array type: {}", key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
return None;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return None;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"restbuffer" => Some(NativeType::RestBuffer),
|
|
|
|
"restbuffer" => Some(NativeType::RestBuffer),
|
|
|
@ -275,6 +225,48 @@ impl NativeType {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
|
|
fn build_inner_type(value: Value) -> Box<PacketDataType> {
|
|
|
|
|
|
|
|
match value {
|
|
|
|
|
|
|
|
Value::String(simple_type) => {
|
|
|
|
|
|
|
|
return if let Some(simple_type) = NativeType::new(&simple_type, Cow::Owned(Value::Null))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Box::new(PacketDataType::Native(simple_type))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Probably a reference to a built type
|
|
|
|
|
|
|
|
Box::new(PacketDataType::Other(simple_type, Value::Null))
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Array(mut array) => {
|
|
|
|
|
|
|
|
if array.len() != 2 {
|
|
|
|
|
|
|
|
return Box::new(PacketDataType::Other(String::new(), Value::Array(array)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let inner_value = Cow::Owned(array.pop().unwrap_or_default());
|
|
|
|
|
|
|
|
let key = array.pop().unwrap();
|
|
|
|
|
|
|
|
if let Value::String(key) = &key {
|
|
|
|
|
|
|
|
let value = PacketDataType::new(key, Cow::clone(&inner_value)).or_else(|| {
|
|
|
|
|
|
|
|
let option = NativeType::new(key, inner_value.clone());
|
|
|
|
|
|
|
|
option.map(PacketDataType::Native)
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(value) = value {
|
|
|
|
|
|
|
|
Box::new(value)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Box::new(PacketDataType::Other(
|
|
|
|
|
|
|
|
key.to_string(),
|
|
|
|
|
|
|
|
inner_value.into_owned(),
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Box::new(PacketDataType::Other(
|
|
|
|
|
|
|
|
key.to_string(),
|
|
|
|
|
|
|
|
inner_value.into_owned(),
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
v => Box::new(PacketDataType::Other(String::new(), v)),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub enum PacketDataType {
|
|
|
|
pub enum PacketDataType {
|
|
|
|
// Just a pure native type
|
|
|
|
// Just a pure native type
|
|
|
|
Native(NativeType),
|
|
|
|
Native(NativeType),
|
|
|
@ -287,29 +279,29 @@ pub enum PacketDataType {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl PacketDataType {
|
|
|
|
impl PacketDataType {
|
|
|
|
pub fn new(key: &str, value: Option<Value>) -> Option<Self> {
|
|
|
|
pub fn new(key: &str, value: Cow<'_, Value>) -> Option<Self> {
|
|
|
|
if !NativeType::contains_type(&key) {
|
|
|
|
if !NativeType::contains_type(key) {
|
|
|
|
let value = value.unwrap_or_default();
|
|
|
|
match value.into_owned() {
|
|
|
|
if value.is_string() {
|
|
|
|
Value::String(string) => Some(PacketDataType::UnknownNativeType(string)),
|
|
|
|
Some(PacketDataType::UnknownNativeType(key.to_string()))
|
|
|
|
Value::Array(mut array) => {
|
|
|
|
} else if let Some(array) = value.as_array() {
|
|
|
|
if array.len() != 2 {
|
|
|
|
if let Some(name) = array.get(0) {
|
|
|
|
return Some(PacketDataType::Other(key.to_string(), Value::Array(array)));
|
|
|
|
if let Some(name) = name.as_str() {
|
|
|
|
}
|
|
|
|
let option = value.get(1).cloned();
|
|
|
|
|
|
|
|
let other_type = NativeType::new(&name, option.clone());
|
|
|
|
let inner_type_values = array.pop().unwrap_or_default();
|
|
|
|
if let Some(type_) = other_type {
|
|
|
|
let inner_type_name = array.pop().unwrap();
|
|
|
|
|
|
|
|
if let Value::String(inner_type_name) = inner_type_name {
|
|
|
|
|
|
|
|
return if let Some(type_) =
|
|
|
|
|
|
|
|
NativeType::new(&inner_type_name, Cow::Borrowed(&inner_type_values))
|
|
|
|
|
|
|
|
{
|
|
|
|
Some(PacketDataType::Built(type_))
|
|
|
|
Some(PacketDataType::Built(type_))
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Some(PacketDataType::Other(name.to_string(), option.unwrap_or_default()))
|
|
|
|
Some(PacketDataType::Other(inner_type_name, inner_type_values))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Some(PacketDataType::Other(key.to_string(), value))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_ => None,
|
|
|
|
Some(PacketDataType::Other(key.to_string(), value))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
None
|
|
|
@ -323,7 +315,7 @@ pub struct PacketDataTypes {
|
|
|
|
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
|
|
|
|
use serde::de::{self, SeqAccess, MapAccess};
|
|
|
|
use serde::de::{MapAccess};
|
|
|
|
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for PacketDataTypes {
|
|
|
|
impl<'de> Deserialize<'de> for PacketDataTypes {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
@ -339,7 +331,6 @@ impl<'de> Deserialize<'de> for PacketDataTypes {
|
|
|
|
formatter.write_str("struct PacketDataTypes")
|
|
|
|
formatter.write_str("struct PacketDataTypes")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn visit_map<V>(self, mut map: V) -> Result<PacketDataTypes, V::Error>
|
|
|
|
fn visit_map<V>(self, mut map: V) -> Result<PacketDataTypes, V::Error>
|
|
|
|
where
|
|
|
|
where
|
|
|
|
V: MapAccess<'de>,
|
|
|
|
V: MapAccess<'de>,
|
|
|
@ -347,7 +338,7 @@ impl<'de> Deserialize<'de> for PacketDataTypes {
|
|
|
|
let mut types = Vec::new();
|
|
|
|
let mut types = Vec::new();
|
|
|
|
while let Some(key) = map.next_key::<String>()? {
|
|
|
|
while let Some(key) = map.next_key::<String>()? {
|
|
|
|
let value = map.next_value::<Value>()?;
|
|
|
|
let value = map.next_value::<Value>()?;
|
|
|
|
if let Some(ty) = PacketDataType::new(&key, Some(value)) {
|
|
|
|
if let Some(ty) = PacketDataType::new(&key, Cow::Owned(value)) {
|
|
|
|
types.push(ty);
|
|
|
|
types.push(ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -357,4 +348,4 @@ impl<'de> Deserialize<'de> for PacketDataTypes {
|
|
|
|
|
|
|
|
|
|
|
|
deserializer.deserialize_map(PacketDataTypesVisitor)
|
|
|
|
deserializer.deserialize_map(PacketDataTypesVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|