From f067b653b152127079070364ec45f48df25c6760 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Tue, 16 Apr 2024 21:03:26 -0400 Subject: [PATCH] Add common functions for converting bool back and forth from u8/16 Sort of unnecessary, but these are also tested. --- src/chardat.rs | 5 +++-- src/common_file_operations.rs | 33 +++++++++++++++++++++++++++++++++ src/dat.rs | 5 +++-- src/lib.rs | 2 ++ src/model.rs | 9 +++++---- src/patch.rs | 5 +++-- 6 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 src/common_file_operations.rs diff --git a/src/chardat.rs b/src/chardat.rs index 4085b2b..1631382 100644 --- a/src/chardat.rs +++ b/src/chardat.rs @@ -6,6 +6,7 @@ use std::io::{BufWriter, Cursor}; use binrw::{BinRead, BinWrite}; use binrw::binrw; use crate::{ByteBuffer, ByteSpan}; +use crate::common_file_operations::{read_bool_from, write_bool_as}; use crate::race::{Gender, Race, Subrace}; @@ -136,8 +137,8 @@ pub struct CharacterData { // version 4 pub hair: u8, /// If hair highlights are enabled for this character. - #[br(map = | x: u8 | x != 0 )] - #[bw(map = | x: &bool | if *x { 1u8 } else { 0u8 } )] + #[br(map = read_bool_from::)] + #[bw(map = write_bool_as::)] pub enable_highlights: bool, /// The character's skin tone. diff --git a/src/common_file_operations.rs b/src/common_file_operations.rs new file mode 100644 index 0000000..135a6e6 --- /dev/null +++ b/src/common_file_operations.rs @@ -0,0 +1,33 @@ +// SPDX-FileCopyrightText: 2024 Joshua Goins +// SPDX-License-Identifier: GPL-3.0-or-later + +pub(crate) fn read_bool_from + std::cmp::PartialEq>(x: T) -> bool { + x == T::from(1u8) +} + +pub(crate) fn write_bool_as>(x: &bool) -> T { + if *x { T::from(1u8) } else { T::from(0u8) } +} + +#[cfg(test)] +mod tests { + use std::io::Cursor; + use crate::common_file_operations::{read_bool_from, write_bool_as}; + use crate::model::MDL; + + const DATA: [u8; 2] = [0u8, 1u8]; + + // TODO: add tests for u16 + + #[test] + fn read_bool_u8() { + assert!(!read_bool_from::(DATA[0])); + assert!(read_bool_from::(DATA[1])); + } + + #[test] + fn write_bool_u8() { + assert_eq!(write_bool_as::(&false), DATA[0]); + assert_eq!(write_bool_as::(&true), DATA[1]); + } +} \ No newline at end of file diff --git a/src/dat.rs b/src/dat.rs index 91b0a28..d382f37 100755 --- a/src/dat.rs +++ b/src/dat.rs @@ -12,6 +12,7 @@ use crate::ByteBuffer; #[cfg(feature = "visual_data")] use crate::model::ModelFileHeader; use crate::sqpack::read_data_block; +use crate::common_file_operations::read_bool_from; #[binrw] #[brw(repr = i32)] @@ -94,10 +95,10 @@ pub struct ModelFileBlock { pub material_num: u16, pub num_lods: u8, - #[br(map = | x: u8 | x != 0)] + #[br(map = read_bool_from::)] pub index_buffer_streaming_enabled: bool, #[brw(pad_after = 1)] - #[br(map = | x: u8 | x != 0)] + #[br(map = read_bool_from::)] pub edge_geometry_enabled: bool, } diff --git a/src/lib.rs b/src/lib.rs index 71c30e0..408083d 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -119,5 +119,7 @@ pub mod lgb; #[cfg(feature = "visual_data")] pub mod tera; +mod common_file_operations; + #[doc(hidden)] pub const PHYSIS_VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/src/model.rs b/src/model.rs index eb3e77e..023526a 100755 --- a/src/model.rs +++ b/src/model.rs @@ -8,6 +8,7 @@ use binrw::{binrw, BinWrite, BinWriterExt}; use binrw::BinRead; use binrw::BinReaderExt; use crate::{ByteBuffer, ByteSpan}; +use crate::common_file_operations::{read_bool_from, write_bool_as}; use crate::model_vertex_declarations::{vertex_element_parser, VERTEX_ELEMENT_SIZE, vertex_element_writer, VertexDeclaration, VertexType, VertexUsage}; pub const NUM_VERTICES: u32 = 17; @@ -31,11 +32,11 @@ pub struct ModelFileHeader { pub lod_count: u8, - #[br(map = | x: u8 | x != 0)] - #[bw(map = | x: & bool | -> u8 { if * x { 1 } else { 0 } })] + #[br(map = read_bool_from::)] + #[bw(map = write_bool_as::)] pub index_buffer_streaming_enabled: bool, - #[br(map = | x: u8 | x != 0)] - #[bw(map = | x: & bool | -> u8 { if * x { 1 } else { 0 } })] + #[br(map = read_bool_from::)] + #[bw(map = write_bool_as::)] #[brw(pad_after = 1)] pub has_edge_geometry: bool, } diff --git a/src/patch.rs b/src/patch.rs index 7dd44d1..6eff907 100755 --- a/src/patch.rs +++ b/src/patch.rs @@ -14,6 +14,7 @@ use tracing::{debug, warn}; use crate::common::{get_platform_string, Platform, Region}; use crate::sqpack::read_data_block_patch; +use crate::common_file_operations::read_bool_from; #[binread] #[derive(Debug)] @@ -269,7 +270,7 @@ struct SqpkTargetInfo { #[br(pad_before = 3)] platform: Platform, region: Region, - #[br(map = | x : u16 | x == 1)] + #[br(map = read_bool_from::)] is_debug: bool, version: u16, #[br(little)] @@ -292,7 +293,7 @@ enum SqpkIndexCommand { #[br(big)] struct SqpkIndex { command: SqpkIndexCommand, - #[br(map = | x : u8 | x == 1)] + #[br(map = read_bool_from::)] is_synonym: bool, #[br(pad_before = 1)]