diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 00:39:12 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 00:39:12 +0300 |
| commit | 6c9fe794cda795b101825091aee4644efd9422f8 (patch) | |
| tree | 809310e22352b8f2ad59413caed89a57ec436aac /crates/fparkan-assets/src/object_descriptions.rs | |
| parent | 72eff35e96ea0c9029248d8403c0a37f63642b79 (diff) | |
| download | fparkan-6c9fe794cda795b101825091aee4644efd9422f8.tar.xz fparkan-6c9fe794cda795b101825091aee4644efd9422f8.zip | |
feat(ui): show installed warbot weapons in the cockpit
Resolve each selected mount through the original objects.dlb short name and render the live weapon bank after boarding. On a stock Fireline start the medium warbot shows the laser and both separate missile racks. Firing the selected rack reduces its ammunition from 18 to 17 while the other still reads 18.
Diffstat (limited to 'crates/fparkan-assets/src/object_descriptions.rs')
| -rw-r--r-- | crates/fparkan-assets/src/object_descriptions.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/object_descriptions.rs b/crates/fparkan-assets/src/object_descriptions.rs new file mode 100644 index 00000000..59c4e88e --- /dev/null +++ b/crates/fparkan-assets/src/object_descriptions.rs @@ -0,0 +1,63 @@ +//! Short names from the original object's description library. + +use crate::ui_configuration::read; +use fparkan_nres::{decode, NresDocument, ReadProfile}; +use fparkan_vfs::Vfs; + +const DESCRIPTION: u32 = u32::from_le_bytes(*b"DSCR"); + +pub struct ObjectDescriptions(NresDocument); + +impl ObjectDescriptions { + pub fn load(vfs: &dyn Vfs) -> Result<Self, String> { + decode(read(vfs, "objects.dlb")?, ReadProfile::Strict) + .map(Self) + .map_err(|error| error.to_string()) + } + + /// The catalogue's `#1` field, kept in the game's text encoding. + #[must_use] + pub fn short_name(&self, prototype: &[u8]) -> Option<&[u8]> { + let id = self.0.find_bytes(prototype)?; + if self.0.entry(id)?.meta().type_id != DESCRIPTION { + return None; + } + short_name(self.0.payload(id).ok()?) + } +} + +fn short_name(description: &[u8]) -> Option<&[u8]> { + description + .split(|&byte| matches!(byte, b'\n' | b'\r' | 0)) + .find_map(|line| line.strip_prefix(b"#1").filter(|name| !name.is_empty())) +} + +#[cfg(test)] +mod tests { + use super::*; + use fparkan_vfs::DirectoryVfs; + + #[test] + fn keeps_the_short_name_bytes_and_ignores_the_long_name() { + assert_eq!( + short_name(b"//catalogue\r\n#1MRL 1\r\n#2long name\0"), + Some(b"MRL 1".as_slice()) + ); + assert_eq!(short_name(b"#2long name only\r\n"), None); + } + + #[test] + #[ignore = "requires licensed corpus"] + fn original_flying_warbot_prototypes_use_the_native_short_names() { + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let descriptions = ObjectDescriptions::load(&DirectoryVfs::new(root)).unwrap(); + assert_eq!( + descriptions.short_name(b"E_GUN_MC_21"), + Some(b"MDRLas".as_slice()) + ); + assert_eq!( + descriptions.short_name(b"e_gun_ml_10"), + Some(b"MML9S".as_slice()) + ); + } +} |
