1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-25 13:57:45 +00:00

Add common functions for converting bool back and forth from u8/16

Sort of unnecessary, but these are also tested.
This commit is contained in:
Joshua Goins 2024-04-16 21:03:26 -04:00
parent eeb877288f
commit f067b653b1
6 changed files with 49 additions and 10 deletions

View file

@ -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::<u8>)]
#[bw(map = write_bool_as::<u8>)]
pub enable_highlights: bool,
/// The character's skin tone.

View file

@ -0,0 +1,33 @@
// SPDX-FileCopyrightText: 2024 Joshua Goins <josh@redstrate.com>
// SPDX-License-Identifier: GPL-3.0-or-later
pub(crate) fn read_bool_from<T: std::convert::From<u8> + std::cmp::PartialEq>(x: T) -> bool {
x == T::from(1u8)
}
pub(crate) fn write_bool_as<T: std::convert::From<u8>>(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::<u8>(DATA[0]));
assert!(read_bool_from::<u8>(DATA[1]));
}
#[test]
fn write_bool_u8() {
assert_eq!(write_bool_as::<u8>(&false), DATA[0]);
assert_eq!(write_bool_as::<u8>(&true), DATA[1]);
}
}

View file

@ -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::<u8>)]
pub index_buffer_streaming_enabled: bool,
#[brw(pad_after = 1)]
#[br(map = | x: u8 | x != 0)]
#[br(map = read_bool_from::<u8>)]
pub edge_geometry_enabled: bool,
}

View file

@ -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");

View file

@ -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::<u8>)]
#[bw(map = write_bool_as::<u8>)]
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::<u8>)]
#[bw(map = write_bool_as::<u8>)]
#[brw(pad_after = 1)]
pub has_edge_geometry: bool,
}

View file

@ -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::<u16>)]
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::<u8>)]
is_synonym: bool,
#[br(pad_before = 1)]