Skip to content

Commit

Permalink
Changed how fixed size arrays are deserialized
Browse files Browse the repository at this point in the history
Made EoReader only throw errors for trying to use `next_chunk` while not
in chunked reading mode
  • Loading branch information
sorokya committed Jul 26, 2024
1 parent 3e0a1a0 commit 0cc15c4
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eolib"
version = "1.0.0-RC8"
version = "1.0.0-RC9"
authors = ["Richard Leek <[email protected]>"]
description = "A core rust library for writing applications related to Endless Online"
edition = "2021"
Expand Down
125 changes: 105 additions & 20 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ fn write_struct_deserialize(
generate_deserialize_length(code, length);
}
StructElement::Dummy(dummy) => {
code.push_str(&format!(" reader.get_{}()?;\n", dummy.data_type));
code.push_str(&format!(" reader.get_{}();\n", dummy.data_type));
}
StructElement::Field(field) => {
generate_deserialize_field(code, field, enums, structs)
Expand Down Expand Up @@ -820,7 +820,7 @@ fn write_struct_deserialize(
}
}
StructElement::Dummy(dummy) => {
code.push_str(&format!(" reader.get_{}()?;\n", dummy.data_type));
code.push_str(&format!(" reader.get_{}();\n", dummy.data_type));
}
StructElement::Length(length) => {
generate_deserialize_length(code, length);
Expand Down Expand Up @@ -1190,7 +1190,7 @@ fn generate_deserialize_length(code: &mut String, length: &Length) {
let offset = length.offset.unwrap_or(0);

if optional {
code.push_str("if reader.remaining()? > 0 {{\n");
code.push_str("if reader.remaining() > 0 {{\n");
}

let offset_operation = match offset.cmp(&0) {
Expand All @@ -1200,7 +1200,7 @@ fn generate_deserialize_length(code: &mut String, length: &Length) {
};

code.push_str(&format!(
" let {} = (reader.get_{}()?{}) as usize;\n",
" let {} = (reader.get_{}(){}) as usize;\n",
replace_keyword(&length.name),
length.data_type,
offset_operation,
Expand All @@ -1225,7 +1225,7 @@ fn generate_deserialize_field(
};

code.push_str(&format!(
" data.{} = if reader.remaining()? > 0 {{\n",
" data.{} = if reader.remaining() > 0 {{\n",
replace_keyword(name)
));
code.push_str(" Some(");
Expand All @@ -1251,7 +1251,7 @@ fn generate_deserialize_array(
) {
let optional = matches!(array.optional, Some(true));
if optional {
code.push_str(" if reader.remaining()? > 0 {{\n");
code.push_str(" if reader.remaining() > 0 {{\n");
generate_inner_array_deserialize(code, array, enums, structs);
code.push_str(" }\n");
} else {
Expand Down Expand Up @@ -1333,34 +1333,34 @@ fn generate_inner_field_deserialize(
enum_data_type.to_string()
};
code.push_str(&format!(
"{}::from(reader.get_{}()?)",
"{}::from(reader.get_{}())",
get_field_type(data_type),
enum_data_type,
));
} else if structs.iter().any(|s| s.name == data_type) {
code.push_str("EoSerialize::deserialize(reader)?");
} else if let Some(length) = &field.length {
match data_type {
"string" => code.push_str(&format!(" reader.get_fixed_string({})?", length)),
"string" => code.push_str(&format!(" reader.get_fixed_string({})", length)),
"encoded_string" => code.push_str(&format!(
" reader.get_fixed_encoded_string({})?",
" reader.get_fixed_encoded_string({})",
length
)),
_ => panic!("Unexpected length for data type: {}", data_type),
}
} else {
match data_type {
"blob" => code.push_str(" reader.get_bytes(reader.remaining()?)?"),
"blob" => code.push_str(" reader.get_bytes(reader.remaining())"),
"bool" => code.push_str(&format!(
"reader.get_{}()? == 1",
"reader.get_{}() == 1",
if enum_data_type.is_empty() {
"char"
} else {
enum_data_type
}
)),
_ => {
code.push_str(&format!(" reader.get_{}()?", data_type));
code.push_str(&format!(" reader.get_{}()", data_type));
}
}
}
Expand All @@ -1386,8 +1386,14 @@ fn generate_inner_array_deserialize(
},
length
));
} else if let Some(size) = get_fixed_type_size(&array.data_type, structs, enums) {
code.push_str(&format!(
" let num_items = reader.remaining() / {};\n",
size
));
code.push_str(" for _ in 0..num_items {\n");
} else {
code.push_str(" while reader.remaining()? > 0 {\n");
code.push_str(" while reader.remaining() > 0 {\n");
}

if is_static_length {
Expand Down Expand Up @@ -1432,6 +1438,46 @@ fn generate_inner_array_deserialize(
code.push_str(" }\n");
}

fn get_fixed_type_size(data_type: &str, structs: &[Struct], enums: &[Enum]) -> Option<usize> {
if let Some(s) = structs.iter().find(|s| s.name == data_type) {
let mut size: usize = 0;
for e in s.elements.iter() {
match e {
StructElement::Comment(_) => {}
StructElement::Dummy(d) => {
match get_fixed_type_size(&d.data_type, structs, enums) {
Some(s) => size += s,
None => return None,
}
}
StructElement::Field(f) => {
match get_fixed_type_size(&f.data_type, structs, enums) {
Some(s) => size += s,
None => return None,
}
}
_ => return None,
}
}

return Some(size);
}

if let Some(e) = enums.iter().find(|e| e.name == data_type) {
return get_fixed_type_size(&e.data_type, structs, enums);
}

match data_type {
"byte" => Some(1),
"char" => Some(1),
"short" => Some(2),
"three" => Some(3),
"int" => Some(4),
"bool" => Some(1),
_ => None,
}
}

fn write_struct_fields(
code: &mut String,
struct_name: &str,
Expand Down Expand Up @@ -1575,13 +1621,52 @@ fn get_imports(elements: &[StructElement], protocols: &[(Protocol, PathBuf)]) ->
let os_separator = std::path::MAIN_SEPARATOR;

let path_mappings: std::collections::HashMap<String, &str> = std::collections::HashMap::from([
(format!("eo-protocol{}xml{}protocol.xml", os_separator, os_separator), "crate::protocol"),
(format!("eo-protocol{}xml{}map{}protocol.xml", os_separator, os_separator, os_separator), "crate::protocol::map"),
(format!("eo-protocol{}xml{}pub{}protocol.xml", os_separator, os_separator, os_separator), "crate::protocol::r#pub"),
(format!("eo-protocol{}xml{}pub{}server{}protocol.xml", os_separator, os_separator, os_separator, os_separator), "crate::protocol::r#pub::server"),
(format!("eo-protocol{}xml{}net{}protocol.xml", os_separator, os_separator, os_separator), "crate::protocol::net"),
(format!("eo-protocol{}xml{}net{}client{}protocol.xml", os_separator, os_separator, os_separator, os_separator), "crate::protocol::net::client"),
(format!("eo-protocol{}xml{}net{}server{}protocol.xml", os_separator, os_separator, os_separator, os_separator),"crate::protocol::net::server"),
(
format!("eo-protocol{}xml{}protocol.xml", os_separator, os_separator),
"crate::protocol",
),
(
format!(
"eo-protocol{}xml{}map{}protocol.xml",
os_separator, os_separator, os_separator
),
"crate::protocol::map",
),
(
format!(
"eo-protocol{}xml{}pub{}protocol.xml",
os_separator, os_separator, os_separator
),
"crate::protocol::r#pub",
),
(
format!(
"eo-protocol{}xml{}pub{}server{}protocol.xml",
os_separator, os_separator, os_separator, os_separator
),
"crate::protocol::r#pub::server",
),
(
format!(
"eo-protocol{}xml{}net{}protocol.xml",
os_separator, os_separator, os_separator
),
"crate::protocol::net",
),
(
format!(
"eo-protocol{}xml{}net{}client{}protocol.xml",
os_separator, os_separator, os_separator, os_separator
),
"crate::protocol::net::client",
),
(
format!(
"eo-protocol{}xml{}net{}server{}protocol.xml",
os_separator, os_separator, os_separator, os_separator
),
"crate::protocol::net::server",
),
]);

for unique_type in &unique_types {
Expand Down
Loading

0 comments on commit 0cc15c4

Please sign in to comment.