aboutsummaryrefslogtreecommitdiff
path: root/src/bt.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bt.rs')
-rw-r--r--src/bt.rs36
1 files changed, 19 insertions, 17 deletions
diff --git a/src/bt.rs b/src/bt.rs
index c348fca..97fb8c5 100644
--- a/src/bt.rs
+++ b/src/bt.rs
@@ -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)
}