Skip to content

Commit

Permalink
Fix off-by-one index error
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtribick committed Feb 3, 2025
1 parent eab180c commit f27a887
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ fn output_path(
}

fn run(args: Args) -> Result<(), Box<dyn std::error::Error>> {
let output_obj = output_path(args.output_obj, &args.input_file, ".obj")?;
let output_mtl = output_path(args.output_mtl, &args.input_file, ".mtl")?;
let output_obj = output_path(args.output_obj, &args.input_file, "obj")?;
let output_mtl = output_path(args.output_mtl, &args.input_file, "mtl")?;

cmodconvert::convert_cmod(args.input_file, output_obj, output_mtl)
}
Expand Down
14 changes: 7 additions & 7 deletions src/reader/binaryread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<R: Read> BinaryReader<R> {
fn read_expected_data_type(&mut self, expected_type: DataType) -> Result<(), CmodError> {
let token = self.input.read_u16::<LittleEndian>()?;
let data_type =
DataType::from_u16(token).ok_or_else(|| CmodError::InvalidDataType(token))?;
DataType::from_u16(token).ok_or(CmodError::InvalidDataType(token))?;
if data_type == expected_type {
Ok(())
} else {
Expand All @@ -36,20 +36,20 @@ impl<R: Read> CmodTokenizer for BinaryReader<R> {
match self.input.read_u16::<LittleEndian>() {
Ok(token) => Token::from_u16(token)
.map(Some)
.ok_or_else(|| CmodError::InvalidToken(token)),
.ok_or( CmodError::InvalidToken(token)),
Err(err) if err.kind() == io::ErrorKind::UnexpectedEof => Ok(None),
Err(err) => Err(err.into()),
}
}

fn read_texture_semantic(&mut self) -> Result<TextureSemantic, CmodError> {
let token = self.input.read_u16::<LittleEndian>()?;
TextureSemantic::from_u16(token).ok_or_else(|| CmodError::InvalidTextureSemantic(token))
TextureSemantic::from_u16(token).ok_or( CmodError::InvalidTextureSemantic(token))
}

fn read_blend_mode(&mut self) -> Result<BlendMode, CmodError> {
let token = self.input.read_u16::<LittleEndian>()?;
BlendMode::from_u16(token).ok_or_else(|| CmodError::InvalidBlendMode(token))
BlendMode::from_u16(token).ok_or( CmodError::InvalidBlendMode(token))
}

fn read_attribute_type(&mut self) -> Result<Option<AttributeType>, CmodError> {
Expand All @@ -59,14 +59,14 @@ impl<R: Read> CmodTokenizer for BinaryReader<R> {
}

let attribute_type =
AttributeType::from_u16(token).ok_or_else(|| CmodError::InvalidAttributeType(token))?;
AttributeType::from_u16(token).ok_or( CmodError::InvalidAttributeType(token))?;

Ok(Some(attribute_type))
}

fn read_attribute_format(&mut self) -> Result<AttributeFormat, CmodError> {
let token = self.input.read_u16::<LittleEndian>()?;
AttributeFormat::from_u16(token).ok_or_else(|| CmodError::InvalidAttributeFormat(token))
AttributeFormat::from_u16(token).ok_or( CmodError::InvalidAttributeFormat(token))
}

fn read_primitive_type(&mut self) -> Result<Option<PrimitiveType>, CmodError> {
Expand All @@ -76,7 +76,7 @@ impl<R: Read> CmodTokenizer for BinaryReader<R> {
}

let primitive_type =
PrimitiveType::from_u16(token).ok_or_else(|| CmodError::InvalidPrimitiveType(token))?;
PrimitiveType::from_u16(token).ok_or( CmodError::InvalidPrimitiveType(token))?;

Ok(Some(primitive_type))
}
Expand Down
12 changes: 8 additions & 4 deletions src/wavefront.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl VertexHandler {
.position_lookup
.entry(position)
.or_insert_with_key(|key| {
let position_idx = self.positions.len();
let position_idx = self.positions.len() + 1;
self.positions.push(*key);
position_idx as u32
});
Expand All @@ -118,7 +118,7 @@ impl VertexHandler {
.tex_coord_lookup
.entry(tex_coord)
.or_insert_with_key(|key| {
let tex_coord_idx = self.tex_coords.len();
let tex_coord_idx = self.tex_coords.len() + 1;
self.tex_coords.push(*key);
tex_coord_idx as u32
})
Expand All @@ -131,7 +131,7 @@ impl VertexHandler {
};

*self.normal_lookup.entry(normal).or_insert_with_key(|key| {
let normal_idx = self.normals.len();
let normal_idx = self.normals.len() + 1;
self.normals.push(*key);
normal_idx as u32
})
Expand Down Expand Up @@ -307,7 +307,11 @@ impl From<EquatableF32> for f32 {

impl Display for EquatableF32 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
if self.0 == 0.0 || (self.0.abs() >= 1e-4 && self.0.abs() <= 1e9) {
write!(f, "{}", self.0)
} else {
write!(f, "{:E}", self.0)
}
}
}

Expand Down

0 comments on commit f27a887

Please sign in to comment.