diff --git a/source/durandal/bin.rs b/source/durandal/bin.rs index e5124a5..b2dc0aa 100644 --- a/source/durandal/bin.rs +++ b/source/durandal/bin.rs @@ -476,6 +476,22 @@ impl OptU16 Some(n) => Some(n.get() - 1), } } + + /// Return the memory representation of this integer as a byte array + /// in big-endian (network) byte order. + #[inline] + pub fn to_be_bytes(self) -> [u8; 2] + { + >::into(self).to_be_bytes() + } + + /// Return the memory representation of this integer as a byte array + /// in little-endian byte order. + #[inline] + pub fn to_le_bytes(self) -> [u8; 2] + { + >::into(self).to_le_bytes() + } } impl fmt::Debug for OptU16 diff --git a/source/durandal/cksum.rs b/source/durandal/cksum.rs index 7441ae7..08a359a 100644 --- a/source/durandal/cksum.rs +++ b/source/durandal/cksum.rs @@ -1,4 +1,4 @@ -//! Cyclic redundancy check function. +//! Checksum functions. fn crc_accum(a: u32, _: u32) -> u32 { diff --git a/source/marathon/map.rs b/source/marathon/map.rs index 2da456c..c02babe 100644 --- a/source/marathon/map.rs +++ b/source/marathon/map.rs @@ -20,6 +20,18 @@ pub fn read_lightfunc(b: &[u8]) -> ResultS Ok(LightFunc{ftype, prd_nrm, prd_dta, val_nrm, val_dta}) } +/// Writes a `LightFunc` object. +pub fn write_lightfunc(v: &LightFunc) -> Vec +{ + let mut o = Vec::with_capacity(14); + o.extend(&(v.ftype as u16).to_be_bytes()); + o.extend(&v.prd_nrm.to_be_bytes()); + o.extend(&v.prd_dta.to_be_bytes()); + o.extend(&v.val_nrm.to_be_bytes()); + o.extend(&v.val_dta.to_be_bytes()); + o +} + /// Reads a `SideTex` object. pub fn read_sidetex(b: &[u8]) -> ResultS { @@ -33,6 +45,15 @@ pub fn read_sidetex(b: &[u8]) -> ResultS Ok(SideTex{offs, tex_id}) } +/// Writes a `SideTex` object. +pub fn write_sidetex(v: &SideTex) -> Vec +{ + let mut o = Vec::with_capacity(6); + o.extend(write_point(&v.offs)); + o.extend(&v.tex_id.to_be_bytes()); + o +} + /// Reads a `Point` object. pub fn read_point(b: &[u8]) -> ResultS { @@ -46,6 +67,15 @@ pub fn read_point(b: &[u8]) -> ResultS Ok(Point{x, y}) } +/// Writes a `Point` object. +pub fn write_point(v: &Point) -> Vec +{ + let mut o = Vec::with_capacity(4); + o.extend(&v.x.to_be_bytes()); + o.extend(&v.y.to_be_bytes()); + o +} + /// Reads a `Minf` chunk. pub fn read_minf(b: &[u8]) -> ResultS { @@ -65,6 +95,20 @@ pub fn read_minf(b: &[u8]) -> ResultS entr_flags, level_name}) } +/// Writes a `Minf` chunk. +pub fn write_minf(v: &Minf) -> Vec +{ + let mut o = Vec::with_capacity(4); + o.extend(&v.texture_id.to_be_bytes()); + o.extend(&v.physics_id.to_be_bytes()); + o.extend(&v.skypict_id.to_be_bytes()); + o.extend(&v.miss_flags.bits().to_be_bytes()); + o.extend(&v.envi_flags.bits().to_be_bytes()); + o.extend(&pad_0(to_mac_roman(v.level_name), 66)); + o.extend(&v.entr_flags.bits().to_be_bytes()); + o +} + /// Reads an old `Minf` chunk. pub fn read_old_minf(b: &[u8]) -> ResultS {