aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-14 18:48:15 +0300
committerValentin Popov <valentin@popov.link>2026-11-14 18:48:15 +0300
commit55c4aa3ad801b0599fab9e4f669898d02f38c627 (patch)
tree1bf0ee4ee308406142815f2e8f738914b1b7e540 /crates/fparkan-assets/src
parent99abb6bf77c259a1afb629f418f5aad64e20de75 (diff)
downloadfparkan-55c4aa3ad801b0599fab9e4f669898d02f38c627.tar.xz
fparkan-55c4aa3ad801b0599fab9e4f669898d02f38c627.zip
fix(player): read flight capability from chassis profile
Diffstat (limited to 'crates/fparkan-assets/src')
-rw-r--r--crates/fparkan-assets/src/chassis_profile.rs52
-rw-r--r--crates/fparkan-assets/src/lib.rs2
2 files changed, 54 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/chassis_profile.rs b/crates/fparkan-assets/src/chassis_profile.rs
new file mode 100644
index 00000000..dfdaa599
--- /dev/null
+++ b/crates/fparkan-assets/src/chassis_profile.rs
@@ -0,0 +1,52 @@
+use fparkan_binary::{checked_count_bytes, read_lp_bytes, Cursor, DecodeError};
+
+/// Reads the bound `CanFly` boolean from a native chassis VAR profile.
+///
+/// # Errors
+/// Returns an error if the profile framing is invalid.
+pub fn decode_chassis_can_fly(bytes: &[u8]) -> Result<Option<bool>, DecodeError> {
+ let mut cursor = Cursor::new(bytes);
+ let count = cursor.read_u32_le()?;
+ checked_count_bytes(u64::from(count), 28, cursor.remaining() as u64)?;
+ let mut can_fly = None;
+ for _ in 0..count {
+ let size = cursor.read_u32_le()?;
+ let kind = cursor.read_u32_le()?;
+ cursor.read_u32_le()?;
+ let name = read_lp_bytes(&mut cursor, 64 * 1024)?;
+ read_lp_bytes(&mut cursor, 64 * 1024)?;
+ cursor.read_exact(size as usize)?;
+ if can_fly.is_none()
+ && name.split(|byte| *byte == 0).next() == Some(b"CanFly")
+ && size == 4
+ && kind == 2
+ {
+ can_fly = Some(cursor.read_u32_le()? == 1);
+ } else {
+ cursor.read_exact(size as usize)?;
+ }
+ cursor.read_exact(8)?;
+ }
+ cursor.require_eof()?;
+ Ok(can_fly)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::decode_chassis_can_fly;
+
+ #[test]
+ fn uses_the_bound_boolean_instead_of_its_default() {
+ let mut profile = Vec::new();
+ for word in [1_u32, 4, 2, 0, 6] {
+ profile.extend(word.to_le_bytes());
+ }
+ profile.extend(b"CanFly");
+ for word in [0_u32, 0, 1, 0, 1] {
+ profile.extend(word.to_le_bytes());
+ }
+ assert_eq!(decode_chassis_can_fly(&profile), Ok(Some(true)));
+ profile.truncate(profile.len() - 1);
+ assert!(decode_chassis_can_fly(&profile).is_err());
+ }
+}
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs
index 6c32bf62..7159eab2 100644
--- a/crates/fparkan-assets/src/lib.rs
+++ b/crates/fparkan-assets/src/lib.rs
@@ -30,6 +30,8 @@ pub use fparkan_terrain_format::{BuildCategory, TerrainFormatError};
use fparkan_texm::{decode_mip_rgba8, decode_texm, TexmDocument, TexmError};
pub use fparkan_texm::{PageRect, RgbaImage};
use fparkan_vfs::DirectoryVfs;
+mod chassis_profile;
+pub use chassis_profile::decode_chassis_can_fly;
mod research;
pub use research::{
calc_full_research_cost, ResearchCandidate, ResearchCandidateSet, ResearchCost, ResearchError,