WIP: Entis GLS CSX v2

This commit is contained in:
2026-01-20 11:40:09 +08:00
parent 8c9f2d7d52
commit ff886e66d3
33 changed files with 955 additions and 214 deletions

View File

@@ -35,7 +35,7 @@ pub fn struct_unpack_impl_for_num(item: TokenStream) -> TokenStream {
let i = syn::parse_macro_input!(item as syn::Ident);
let output = quote::quote! {
impl StructUnpack for #i {
fn unpack<R: Read + Seek>(reader: &mut R, big: bool, _encoding: Encoding) -> Result<Self> {
fn unpack<R: Read + Seek>(reader: &mut R, big: bool, _encoding: Encoding, _info: &Option<Box<dyn std::any::Any>>) -> Result<Self> {
let mut buf = [0u8; std::mem::size_of::<#i>()];
reader.read_exact(&mut buf)?;
Ok(if big {
@@ -47,7 +47,7 @@ pub fn struct_unpack_impl_for_num(item: TokenStream) -> TokenStream {
}
impl StructPack for #i {
fn pack<W: Write>(&self, writer: &mut W, big: bool, _encoding: Encoding) -> Result<()> {
fn pack<W: Write>(&self, writer: &mut W, big: bool, _encoding: Encoding, _info: &Option<Box<dyn std::any::Any>>) -> Result<()> {
let bytes = if big {
self.to_be_bytes()
} else {
@@ -220,7 +220,7 @@ pub fn struct_pack_derive(input: TokenStream) -> TokenStream {
cur = Some(quote::quote! {
let encoded = crate::utils::encoding::encode_string(encoding, &self.#field_name, true)?;
let len = encoded.len() as #pstring_type;
len.pack(writer, big, encoding)?;
len.pack(writer, big, encoding, info)?;
writer.write_all(&encoded)?;
});
}
@@ -234,15 +234,15 @@ pub fn struct_pack_derive(input: TokenStream) -> TokenStream {
return Err(anyhow::anyhow!("Vector length was not equal to {}", #fixed_vec));
}
for item in &self.#field_name {
item.pack(writer, big, encoding)?;
item.pack(writer, big, encoding, info)?;
}
});
} else if let Some(pvec_type) = pvec_type {
cur = Some(quote::quote! {
let len = self.#field_name.len() as #pvec_type;
len.pack(writer, big, encoding)?;
len.pack(writer, big, encoding, info)?;
for item in &self.#field_name {
item.pack(writer, big, encoding)?;
item.pack(writer, big, encoding, info)?;
}
});
}
@@ -251,7 +251,7 @@ pub fn struct_pack_derive(input: TokenStream) -> TokenStream {
}
let p = cur.unwrap_or_else(|| {
quote::quote! {
self.#field_name.pack(writer, big, encoding)?;
self.#field_name.pack(writer, big, encoding, info)?;
}
});
if let Some(skip_if) = skip_if {
@@ -266,7 +266,7 @@ pub fn struct_pack_derive(input: TokenStream) -> TokenStream {
});
let output = quote::quote! {
impl StructPack for #name {
fn pack<W: Write>(&self, writer: &mut W, big: bool, encoding: Encoding) -> Result<()> {
fn pack<W: Write>(&self, writer: &mut W, big: bool, encoding: Encoding, info: &Option<Box<dyn std::any::Any>>) -> Result<()> {
#(#fields)*
Ok(())
}
@@ -412,7 +412,7 @@ pub fn struct_pack_derive(input: TokenStream) -> TokenStream {
cur = Some(quote::quote! {
let encoded = crate::utils::encoding::encode_string(encoding, &#field_name, true)?;
let len = encoded.len() as #pstring_type;
len.pack(writer, big, encoding)?;
len.pack(writer, big, encoding, info)?;
writer.write_all(&encoded)?;
});
}
@@ -426,15 +426,15 @@ pub fn struct_pack_derive(input: TokenStream) -> TokenStream {
return Err(anyhow::anyhow!("Vector length was not equal to {}", #fixed_vec));
}
for item in &#field_name {
item.pack(writer, big, encoding)?;
item.pack(writer, big, encoding, info)?;
}
});
} else if let Some(pvec_type) = pvec_type {
cur = Some(quote::quote! {
let len = #field_name.len() as #pvec_type;
len.pack(writer, big, encoding)?;
len.pack(writer, big, encoding, info)?;
for item in &#field_name {
item.pack(writer, big, encoding)?;
item.pack(writer, big, encoding, info)?;
}
});
}
@@ -443,7 +443,7 @@ pub fn struct_pack_derive(input: TokenStream) -> TokenStream {
}
let p = cur.unwrap_or_else(|| {
quote::quote! {
#field_name.pack(writer, big, encoding)?;
#field_name.pack(writer, big, encoding, info)?;
}
});
if let Some(skip_if) = skip_if {
@@ -469,7 +469,7 @@ pub fn struct_pack_derive(input: TokenStream) -> TokenStream {
});
let output = quote::quote! {
impl StructPack for #ident {
fn pack<W: Write>(&self, writer: &mut W, big: bool, encoding: Encoding) -> Result<()> {
fn pack<W: Write>(&self, writer: &mut W, big: bool, encoding: Encoding, info: &Option<Box<dyn std::any::Any>>) -> Result<()> {
match self {
#(#variants)*
}
@@ -625,7 +625,7 @@ pub fn struct_unpack_derive(input: TokenStream) -> TokenStream {
});
} else if let Some(pstring_type) = pstring_type {
cur = Some(quote::quote! {
let len = <#pstring_type>::unpack(reader, big, encoding)? as usize;
let len = <#pstring_type>::unpack(reader, big, encoding, info)? as usize;
let #field_name = reader.read_exact_vec(len)?;
let #field_name = crate::utils::encoding::decode_to_string(encoding, &#field_name, true)?;
});
@@ -636,12 +636,12 @@ pub fn struct_unpack_derive(input: TokenStream) -> TokenStream {
if segment.ident == "Vec" {
if let Some(fixed_vec) = fixed_vec {
cur = Some(quote::quote! {
let #field_name = reader.read_struct_vec(#fixed_vec, big, encoding)?;
let #field_name = reader.read_struct_vec(#fixed_vec, big, encoding, info)?;
});
} else if let Some(pvec_type) = pvec_type {
cur = Some(quote::quote! {
let len = <#pvec_type>::unpack(reader, big, encoding)? as usize;
let #field_name = reader.read_struct_vec(len, big, encoding)?;
let len = <#pvec_type>::unpack(reader, big, encoding, info)? as usize;
let #field_name = reader.read_struct_vec(len, big, encoding, info)?;
});
}
}
@@ -649,7 +649,7 @@ pub fn struct_unpack_derive(input: TokenStream) -> TokenStream {
}
let p = cur.unwrap_or_else(|| {
quote::quote! {
let #field_name = <#field_type>::unpack(reader, big, encoding)?;
let #field_name = <#field_type>::unpack(reader, big, encoding, info)?;
}
});
if let Some(skip_if) = skip_if {
@@ -672,7 +672,7 @@ pub fn struct_unpack_derive(input: TokenStream) -> TokenStream {
};
let output = quote::quote! {
impl StructUnpack for #name {
fn unpack<R: Read + Seek>(reader: &mut R, big: bool, encoding: Encoding) -> Result<Self> {
fn unpack<R: Read + Seek>(reader: &mut R, big: bool, encoding: Encoding, info: &Option<Box<dyn std::any::Any>>) -> Result<Self> {
#(#smts)*
Ok(Self #fields)
}