mirror of
https://github.com/lifegpc/msg-tool.git
synced 2026-06-08 13:58:50 +08:00
WIP: Entis GLS CSX v2
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
use crate::types::Encoding;
|
||||
use anyhow::Result;
|
||||
use msg_tool_macro::struct_unpack_impl_for_num;
|
||||
use std::any::Any;
|
||||
use std::io::{Read, Seek, Write};
|
||||
|
||||
/// Trait for unpacking a struct from a binary stream.
|
||||
@@ -11,7 +12,13 @@ pub trait StructUnpack: Sized {
|
||||
/// * `reader` - The reader to read the binary data from.
|
||||
/// * `big` - Whether the data is in big-endian format.
|
||||
/// * `encoding` - The encoding to use for string fields.
|
||||
fn unpack<R: Read + Seek>(reader: &mut R, big: bool, encoding: Encoding) -> Result<Self>;
|
||||
/// * `info` - Additional information that may be needed for unpacking.
|
||||
fn unpack<R: Read + Seek>(
|
||||
reader: &mut R,
|
||||
big: bool,
|
||||
encoding: Encoding,
|
||||
info: &Option<Box<dyn Any>>,
|
||||
) -> Result<Self>;
|
||||
}
|
||||
|
||||
/// Trait for packing a struct into a binary stream.
|
||||
@@ -21,13 +28,26 @@ pub trait StructPack: Sized {
|
||||
/// * `writer` - The writer to write the binary data to.
|
||||
/// * `big` - Whether to use big-endian format.
|
||||
/// * `encoding` - The encoding to use for string fields.
|
||||
fn pack<W: Write>(&self, writer: &mut W, big: bool, encoding: Encoding) -> Result<()>;
|
||||
/// * `info` - Additional information that may be needed for packing.
|
||||
fn pack<W: Write>(
|
||||
&self,
|
||||
writer: &mut W,
|
||||
big: bool,
|
||||
encoding: Encoding,
|
||||
info: &Option<Box<dyn Any>>,
|
||||
) -> Result<()>;
|
||||
}
|
||||
|
||||
impl<T: StructPack> StructPack for Vec<T> {
|
||||
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 Any>>,
|
||||
) -> Result<()> {
|
||||
for item in self {
|
||||
item.pack(writer, big, encoding)?;
|
||||
item.pack(writer, big, encoding, info)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -47,7 +67,12 @@ struct_unpack_impl_for_num!(f32);
|
||||
struct_unpack_impl_for_num!(f64);
|
||||
|
||||
impl StructUnpack for bool {
|
||||
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 Any>>,
|
||||
) -> Result<Self> {
|
||||
let mut buf = [0u8; 1];
|
||||
reader.read_exact(&mut buf)?;
|
||||
Ok(buf[0] != 0)
|
||||
@@ -55,37 +80,65 @@ impl StructUnpack for bool {
|
||||
}
|
||||
|
||||
impl StructPack for bool {
|
||||
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 Any>>,
|
||||
) -> Result<()> {
|
||||
writer.write_all(&[if *self { 1 } else { 0 }])?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: StructPack> StructPack for Option<T> {
|
||||
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 Any>>,
|
||||
) -> Result<()> {
|
||||
if let Some(value) = self {
|
||||
value.pack(writer, big, encoding)?;
|
||||
value.pack(writer, big, encoding, info)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: StructUnpack> StructUnpack for Option<T> {
|
||||
fn unpack<R: Read + Seek>(reader: &mut R, big: bool, encoding: Encoding) -> Result<Self> {
|
||||
let value = T::unpack(reader, big, encoding)?;
|
||||
fn unpack<R: Read + Seek>(
|
||||
reader: &mut R,
|
||||
big: bool,
|
||||
encoding: Encoding,
|
||||
info: &Option<Box<dyn Any>>,
|
||||
) -> Result<Self> {
|
||||
let value = T::unpack(reader, big, encoding, info)?;
|
||||
Ok(Some(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl<const T: usize> StructPack for [u8; T] {
|
||||
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 Any>>,
|
||||
) -> Result<()> {
|
||||
writer.write_all(self)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<const T: usize> StructUnpack for [u8; T] {
|
||||
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 Any>>,
|
||||
) -> Result<Self> {
|
||||
let mut buf = [0u8; T];
|
||||
reader.read_exact(&mut buf)?;
|
||||
Ok(buf)
|
||||
|
||||
Reference in New Issue
Block a user