diff options
author | Daniel Schadt <kingdread@gmx.de> | 2019-12-22 16:29:47 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2019-12-25 00:39:29 +0100 |
commit | a442d0b3b8ad68885a59604b168bc45269ded62e (patch) | |
tree | 0bd3957f5fab1b3eedc93dff4cdac6ddf9e53d40 /src | |
parent | a882ff3bd8c7927df029db85a0589e4f3d0fba8a (diff) | |
download | kondou-a442d0b3b8ad68885a59604b168bc45269ded62e.tar.gz kondou-a442d0b3b8ad68885a59604b168bc45269ded62e.tar.bz2 kondou-a442d0b3b8ad68885a59604b168bc45269ded62e.zip |
use byteorder for chatlink parsing
Diffstat (limited to 'src')
-rw-r--r-- | src/bt.rs | 36 | ||||
-rw-r--r-- | src/main.rs | 1 |
2 files changed, 20 insertions, 17 deletions
@@ -1,6 +1,7 @@ use super::api::{Api, ApiError, Profession, Skill, Specialization}; +use byteorder::{ReadBytesExt, WriteBytesExt, LE}; use num_enum::{IntoPrimitive, TryFromPrimitive}; -use std::{convert::TryFrom, error::Error, fmt, str::FromStr}; +use std::{convert::TryFrom, error::Error, fmt, io::Cursor, str::FromStr}; #[derive(Debug)] pub enum ChatlinkError { @@ -15,6 +16,12 @@ error_froms! { ChatlinkError, _err: num_enum::TryFromPrimitiveError<Legend> => ChatlinkError::MalformedInput, } +impl From<std::io::Error> for ChatlinkError { + fn from(_err: std::io::Error) -> Self { + panic!("The reading cursor should never return an error!"); + } +} + impl fmt::Display for ChatlinkError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { @@ -137,7 +144,7 @@ pub const LEGEND_COUNT: usize = 4; pub const CODE_REVENANT: u32 = 9; pub const EMPTY_SKILLS: [Option<Skill>; SKILL_COUNT] = [None, None, None, None, None]; -pub const EMPTY_TRAITLINES: [Option<Traitline>; TRAITLINE_COUNT] = [None,None, None]; +pub const EMPTY_TRAITLINES: [Option<Traitline>; TRAITLINE_COUNT] = [None, None, None]; /// Represents a build template. /// @@ -258,12 +265,8 @@ impl BuildTemplate { bytes.push(0); } Some(s) => { - let palette_id = self - .profession() - .skill_id_to_palette_id(s.id) - .unwrap_or(0); - bytes.push((palette_id & 0xFF) as u8); - bytes.push(((palette_id >> 8) & 0xFF) as u8); + let palette_id = self.profession().skill_id_to_palette_id(s.id).unwrap_or(0); + bytes.write_u16::<LE>(palette_id as u16).unwrap(); } } // Aquatic @@ -307,12 +310,14 @@ impl BuildTemplate { } bytes.remove(0); - let profession = code_to_profession(api, bytes.remove(0) as u32)?; + let mut reader = Cursor::new(bytes); + + let profession = code_to_profession(api, reader.read_u8()? as u32)?; let mut traitlines = EMPTY_TRAITLINES; for i in traitlines.iter_mut() { - let spec_id = bytes.remove(0); - let trait_choices = bytes.remove(0); + let spec_id = reader.read_u8()?; + let trait_choices = reader.read_u8()?; if spec_id == 0 { continue; } @@ -327,9 +332,7 @@ impl BuildTemplate { let mut skills = EMPTY_SKILLS; for i in skills.iter_mut() { // Terrestrial - let byte_1 = bytes.remove(0); - let byte_2 = bytes.remove(0); - let palette_id = byte_1 as u32 | (byte_2 as u32) << 8; + let palette_id = reader.read_u16::<LE>()? as u32; if palette_id != 0 { let skill_id = profession .palette_id_to_skill_id(palette_id) @@ -339,15 +342,14 @@ impl BuildTemplate { } // Aquatic - bytes.remove(0); - bytes.remove(0); + reader.read_u16::<LE>()?; } let extra_data = match profession.code { CODE_REVENANT => { let mut legends = [Legend::None; LEGEND_COUNT]; for i in legends.iter_mut() { - *i = Legend::try_from(bytes.remove(0))?; + *i = Legend::try_from(reader.read_u8()?)?; } ExtraData::Legends(legends) } diff --git a/src/main.rs b/src/main.rs index 47ccabd..eb5ef06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ extern crate base64; +extern crate byteorder; extern crate clap; extern crate image; extern crate imageproc; |