diff --git a/dcb-bytes/src/derive.rs b/dcb-bytes/src/derive.rs index eaff8f3..c486b98 100644 --- a/dcb-bytes/src/derive.rs +++ b/dcb-bytes/src/derive.rs @@ -3,35 +3,36 @@ /// Derives `Bytes` by splitting the input bytes and parsing them with `BYTEORDER` #[macro_export] macro_rules! derive_bytes_split { - ($T:ty, $BYTEORDER:ident, $($field:ident : $U:ty),* $(,)?) => { + ($T:ty, $($field:ident : $U:ty as $BYTEORDER:ident),* $(,)?) => { const _: () = { - use $crate::ByteOrderExt; - use $crate::byteorder::$BYTEORDER as Order; - use $crate::ByteArray; - use $crate::arrayref; + use $crate::{ + ByteOrderExt, + ByteArray, + arrayref, + }; #[allow(clippy::ptr_offset_with_cast)] // `arrayref` does it impl $crate::Bytes for $T { - type ByteArray = [u8; {0 $( + <<$U as ByteOrderExt>::ByteArray as ByteArray>::SIZE )*}]; + type ByteArray = [u8; {0 $( + <<$U as ByteOrderExt<$crate::byteorder::$BYTEORDER>>::ByteArray as ByteArray>::SIZE )*}]; type FromError = !; type ToError = !; fn from_bytes(bytes: &Self::ByteArray) -> Result { - let ( $($field,)* ) = arrayref::array_refs![bytes, $( <<$U as ByteOrderExt>::ByteArray as ByteArray>::SIZE ),*]; + let ( $($field,)* ) = arrayref::array_refs![bytes, $( <<$U as ByteOrderExt<$crate::byteorder::$BYTEORDER>>::ByteArray as ByteArray>::SIZE ),*]; Ok(Self { $( - $field: <$U as ByteOrderExt::>::read( $field ), + $field: <$U as ByteOrderExt::<$crate::byteorder::$BYTEORDER>>::read( $field ).into(), )* }) } fn to_bytes(&self, bytes: &mut Self::ByteArray) -> Result<(), Self::ToError> { - let ( $($field,)* ) = arrayref::mut_array_refs![bytes, $( <<$U as ByteOrderExt>::ByteArray as ByteArray>::SIZE ),*]; + let ( $($field,)* ) = arrayref::mut_array_refs![bytes, $( <<$U as ByteOrderExt<$crate::byteorder::$BYTEORDER>>::ByteArray as ByteArray>::SIZE ),*]; $( - <$U as ByteOrderExt::>::write(&self.$field, $field); + <$U as ByteOrderExt::<$crate::byteorder::$BYTEORDER>>::write(&self.$field.into(), $field); )* Ok(()) diff --git a/dcb-cdrom-xa/src/sector/header/address.rs b/dcb-cdrom-xa/src/sector/header/address.rs index e8b74d9..c1386d3 100644 --- a/dcb-cdrom-xa/src/sector/header/address.rs +++ b/dcb-cdrom-xa/src/sector/header/address.rs @@ -1,9 +1,5 @@ //! Sector address -// Imports -use dcb_bytes::Bytes; -use dcb_util::{array_split, array_split_mut}; - /// Sector address #[derive(PartialEq, Eq, Clone, Debug)] pub struct Address { @@ -17,36 +13,8 @@ pub struct Address { pub block: u8, } -impl Bytes for Address { - type ByteArray = [u8; 0x3]; - type FromError = !; - type ToError = !; - - fn from_bytes(bytes: &Self::ByteArray) -> Result { - let bytes = array_split!(bytes, - min : 0x1, - sec : 0x1, - block: 0x1, - ); - - Ok(Self { - min: *bytes.min, - sec: *bytes.sec, - block: *bytes.block, - }) - } - - fn to_bytes(&self, bytes: &mut Self::ByteArray) -> Result<(), Self::ToError> { - let bytes = array_split_mut!(bytes, - min : 0x1, - sec : 0x1, - block: 0x1, - ); - - *bytes.min = self.min; - *bytes.sec = self.sec; - *bytes.block = self.block; - - Ok(()) - } +dcb_bytes::derive_bytes_split! {Address, + min : u8 as LittleEndian, + sec : u8 as LittleEndian, + block: u8 as LittleEndian, } diff --git a/dcb-cdrom-xa/src/sector/header/subheader.rs b/dcb-cdrom-xa/src/sector/header/subheader.rs index c7736b4..47d1683 100644 --- a/dcb-cdrom-xa/src/sector/header/subheader.rs +++ b/dcb-cdrom-xa/src/sector/header/subheader.rs @@ -1,10 +1,5 @@ //! Sector subheader -// Imports -use byteorder::{ByteOrder, LittleEndian}; -use dcb_bytes::Bytes; -use dcb_util::{array_split, array_split_mut}; - /// The sector sub-header #[derive(PartialEq, Eq, Clone, Debug)] pub struct SubHeader { @@ -21,40 +16,9 @@ pub struct SubHeader { pub data_type: u16, } -impl Bytes for SubHeader { - type ByteArray = [u8; 0x8]; - type FromError = !; - type ToError = !; - - fn from_bytes(bytes: &Self::ByteArray) -> Result { - let bytes = array_split!(bytes, - file : [0x2], - channel : [0x2], - submode : [0x2], - data_type: [0x2], - ); - - Ok(Self { - file: LittleEndian::read_u16(bytes.file), - channel: LittleEndian::read_u16(bytes.channel), - submode: LittleEndian::read_u16(bytes.submode), - data_type: LittleEndian::read_u16(bytes.data_type), - }) - } - - fn to_bytes(&self, bytes: &mut Self::ByteArray) -> Result<(), Self::ToError> { - let bytes = array_split_mut!(bytes, - file : [0x2], - channel : [0x2], - submode : [0x2], - data_type: [0x2], - ); - - LittleEndian::write_u16(bytes.file, self.file); - LittleEndian::write_u16(bytes.channel, self.channel); - LittleEndian::write_u16(bytes.submode, self.submode); - LittleEndian::write_u16(bytes.data_type, self.data_type); - - Ok(()) - } +dcb_bytes::derive_bytes_split! {SubHeader, + file : u16 as LittleEndian, + channel : u16 as LittleEndian, + submode : u16 as LittleEndian, + data_type: u16 as LittleEndian, } diff --git a/dcb-io/src/pak/entry/animation2d/frame.rs b/dcb-io/src/pak/entry/animation2d/frame.rs index 1c21ee6..9b4b181 100644 --- a/dcb-io/src/pak/entry/animation2d/frame.rs +++ b/dcb-io/src/pak/entry/animation2d/frame.rs @@ -1,10 +1,5 @@ //! 2D Animation frame -// Imports -use byteorder::{ByteOrder, LittleEndian}; -use dcb_bytes::Bytes; -use dcb_util::array_split; - /// 2D Animation frame #[derive(PartialEq, Eq, Clone, Debug)] pub struct Frame { @@ -42,41 +37,16 @@ pub struct Frame { pub unknown3: u16, } -impl Bytes for Frame { - type ByteArray = [u8; 0x14]; - type FromError = !; - type ToError = !; - - fn from_bytes(bytes: &Self::ByteArray) -> Result { - let bytes = array_split!(bytes, - unknown0: [0x4], - x0 : 0x1, - x1 : 0x1, - y0 : 0x1, - y1 : 0x1, - width : [0x2], - height : [0x2], - unknown1: [0x2], - duration: [0x2], - unknown2: [0x2], - unknown3: [0x2], - ); - Ok(Self { - unknown0: LittleEndian::read_u32(bytes.unknown0), - x0: *bytes.x0, - x1: *bytes.x1, - y0: *bytes.y0, - y1: *bytes.y1, - width: LittleEndian::read_u16(bytes.width), - height: LittleEndian::read_u16(bytes.height), - unknown1: LittleEndian::read_u16(bytes.unknown1), - duration: LittleEndian::read_u16(bytes.duration), - unknown2: LittleEndian::read_u16(bytes.unknown2), - unknown3: LittleEndian::read_u16(bytes.unknown3), - }) - } - - fn to_bytes(&self, _bytes: &mut Self::ByteArray) -> Result<(), Self::ToError> { - todo!() - } +dcb_bytes::derive_bytes_split! {Frame, + unknown0: u32 as LittleEndian, + x0 : u8 as LittleEndian, + x1 : u8 as LittleEndian, + y0 : u8 as LittleEndian, + y1 : u8 as LittleEndian, + width : u16 as LittleEndian, + height : u16 as LittleEndian, + unknown1: u16 as LittleEndian, + duration: u16 as LittleEndian, + unknown2: u16 as LittleEndian, + unknown3: u16 as LittleEndian, } diff --git a/dcb-io/src/pak/entry/model3d_set/model/obj.rs b/dcb-io/src/pak/entry/model3d_set/model/obj.rs index 0c9d0cd..a25fa06 100644 --- a/dcb-io/src/pak/entry/model3d_set/model/obj.rs +++ b/dcb-io/src/pak/entry/model3d_set/model/obj.rs @@ -1,10 +1,5 @@ //! Object -// Imports -use byteorder::{ByteOrder, LittleEndian}; -use dcb_bytes::Bytes; -use dcb_util::array_split; - /// A `.TMD` object #[derive(PartialEq, Eq, Clone, Debug)] pub struct Obj { @@ -30,35 +25,12 @@ pub struct Obj { pub scale: i32, } - -impl Bytes for Obj { - type ByteArray = [u8; 0x1c]; - type FromError = !; - type ToError = !; - - fn from_bytes(bytes: &Self::ByteArray) -> Result { - let bytes = array_split!(bytes, - vertices_pos : [0x4], - vertices_len : [0x4], - normal_pos : [0x4], - normal_len : [0x4], - primitive_pos: [0x4], - primitive_len: [0x4], - scale : [0x4], - ); - - Ok(Self { - vertices_pos: LittleEndian::read_u32(bytes.vertices_pos), - vertices_len: LittleEndian::read_u32(bytes.vertices_len), - normal_pos: LittleEndian::read_u32(bytes.normal_pos), - normal_len: LittleEndian::read_u32(bytes.normal_len), - primitive_pos: LittleEndian::read_u32(bytes.primitive_pos), - primitive_len: LittleEndian::read_u32(bytes.primitive_len), - scale: LittleEndian::read_i32(bytes.scale), - }) - } - - fn to_bytes(&self, _bytes: &mut Self::ByteArray) -> Result<(), Self::ToError> { - todo!() - } +dcb_bytes::derive_bytes_split! {Obj, + vertices_pos : u32 as LittleEndian, + vertices_len : u32 as LittleEndian, + normal_pos : u32 as LittleEndian, + normal_len : u32 as LittleEndian, + primitive_pos: u32 as LittleEndian, + primitive_len: u32 as LittleEndian, + scale : i32 as LittleEndian, }