diff options
Diffstat (limited to 'crates/fparkan-runtime/src/robot_designer.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/robot_designer.rs | 70 |
1 files changed, 68 insertions, 2 deletions
diff --git a/crates/fparkan-runtime/src/robot_designer.rs b/crates/fparkan-runtime/src/robot_designer.rs index 27e899ef..629efa3d 100644 --- a/crates/fparkan-runtime/src/robot_designer.rs +++ b/crates/fparkan-runtime/src/robot_designer.rs @@ -9,7 +9,7 @@ use fparkan_path::NormalizedPath; use fparkan_prototype::{decode_unit_dat, unit_component_tree, UnitComponentRecord, UnitDat}; use fparkan_vfs::{MemoryVfs, OverlayVfs, Vfs}; use fparkan_world::OwnerId; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, VecDeque}; use std::sync::Arc; /// Loads the gameplay world and prepares the player's optional Unit DAT design. @@ -66,6 +66,45 @@ const RECORD_BYTES: usize = 112; const RESOURCE_KEY: std::ops::Range<usize> = 32..64; const DESCRIPTION: std::ops::Range<usize> = 72..104; pub(super) const STARTER_SCHEME_PREFIX: &str = "fparkan/starter/"; +const RECENT_PROJECT_LIMIT: usize = 5; + +/// Mission prebuilds enter the shared factory and designer history newest first. +pub(super) fn recent_mission_projects(config: &[u8]) -> Result<Vec<NormalizedPath>, EngineError> { + let mut projects = VecDeque::with_capacity(RECENT_PROJECT_LIMIT); + let mut in_prebuild = false; + for line in config.split(|&byte| byte == b'\n') { + let line = line.trim_ascii(); + let mut words = line.split(u8::is_ascii_whitespace); + match words.next() { + Some(word) if word.eq_ignore_ascii_case(b"object") => { + in_prebuild = words + .find(|word| !word.is_empty()) + .is_some_and(|name| name.eq_ignore_ascii_case(b"prebuild")); + } + Some(word) if word.eq_ignore_ascii_case(b"end") => in_prebuild = false, + Some(word) if in_prebuild && !word.starts_with(b"#") && !word.starts_with(b"//") => { + let mut fields = line.splitn(3, |&byte| byte == b'"'); + let key = fields.next().unwrap_or_default(); + let name = fields.next().filter(|name| !name.is_empty()); + if let Some(name) = + name.filter(|_| key.trim_ascii_end().ends_with(b"=") && fields.next().is_some()) + { + let name = std::str::from_utf8(name).map_err(invalid)?; + let path = normalize_engine_path( + "mission prebuild", + &format!("UNITS/UNITS/PREBLD/{name}"), + )?; + if projects.len() == RECENT_PROJECT_LIMIT { + projects.pop_back(); + } + projects.push_front(path); + } + } + _ => {} + } + } + Ok(projects.into()) +} pub(super) fn prepare( vfs: Arc<dyn Vfs>, @@ -293,7 +332,34 @@ fn invalid(error: impl std::fmt::Display) -> EngineError { #[cfg(test)] mod tests { - use super::validate_change; + use super::{recent_mission_projects, validate_change, NormalizedPath}; + + #[test] + fn prebuilds_fill_five_recent_slots_newest_first() { + let config = b"object other\nmodel1 = \"ignored.dat\"\nend\r\n\ + OBJECT PreBuild\r\n# model0 = \"comment.dat\"\r\n\ + model6=\"evicted.dat\"\r\nmodel2=\"old.dat\"\r\nmodel1 = \"first.dat\"\r\n\ + model3 = \"second.dat\"\r\nmodel5 = \"FIRST.DAT\"\r\nmodel4 = \"new.dat\"\r\nend"; + let projects = recent_mission_projects(config).unwrap(); + assert_eq!( + projects + .iter() + .map(NormalizedPath::as_str) + .collect::<Vec<_>>(), + [ + "UNITS/UNITS/PREBLD/new.dat", + "UNITS/UNITS/PREBLD/FIRST.DAT", + "UNITS/UNITS/PREBLD/second.dat", + "UNITS/UNITS/PREBLD/first.dat", + "UNITS/UNITS/PREBLD/old.dat", + ] + ); + assert!(recent_mission_projects( + b"object prebuild\nend\nobject other\nmodel1 = \"ignored.dat\"\nend" + ) + .unwrap() + .is_empty()); + } #[test] fn design_changes_keep_the_component_family_and_chassis_size() { |
