From cd45231dd2a4fd9f970d9c7e06004cedd5c9add6 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 05:27:02 +0400 Subject: fix(ui): preserve native recent factory project order --- crates/fparkan-runtime/src/lib.rs | 2 +- crates/fparkan-runtime/src/production.rs | 65 +------------------------- crates/fparkan-runtime/src/robot_designer.rs | 70 +++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 67 deletions(-) (limited to 'crates') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 7905a75b..c5794e55 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -2007,7 +2007,7 @@ fn load_mission_with_options( } else if let Some((scheme, _)) = build_scheme { vec![scheme.clone()] } else if options.start_autodemo && request.mode == MissionMode::SingleMission { - production::mission_prebuilds(&config)? + robot_designer::recent_mission_projects(&config)? } else { Vec::new() }; diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index c0db14ab..3d3eb159 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -3,7 +3,7 @@ use super::{ behavior_ore_index_for_handle, factory_release::FactoryDeparture, mission_events::on_communicator, - normalize_engine_path, query_behavior_profile_ore_rate, + query_behavior_profile_ore_rate, research::{load_clan_table, RESEARCHED}, route_obstacles::BuildingFootprints, set_control_device_table_selector, @@ -27,46 +27,6 @@ pub(super) const RESERVE_SLOTS: usize = 8; const PAID_MINIMUM_MS: u32 = 5_000; const RESOURCE_EPSILON: f32 = 0.01; -pub(super) fn mission_prebuilds(config: &[u8]) -> Result, EngineError> { - let mut schemes = Vec::new(); - 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) - .filter(|word| !word.is_empty()); - match words.next() { - Some(word) if word.eq_ignore_ascii_case(b"object") => { - in_prebuild = words - .next() - .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 key.trim_ascii_end().ends_with(b"=") && fields.next().is_some() { - if let Some(name) = name { - let name = std::str::from_utf8(name).map_err(production_error)?; - let path = normalize_engine_path( - "mission prebuild", - &format!("UNITS/UNITS/PREBLD/{name}"), - )?; - if !schemes.iter().any(|entry: &NormalizedPath| { - entry.as_bytes().eq_ignore_ascii_case(path.as_bytes()) - }) { - schemes.push(path); - } - } - } - } - _ => {} - } - } - Ok(schemes) -} - pub(super) struct Production { reserve_start: usize, schemes: Vec, @@ -944,29 +904,6 @@ mod tests { panic!("stock AI did not release a combat design in the opening"); } - #[test] - fn mission_prebuilds_preserve_order_and_skip_duplicates() { - let config = b"object other\n model1 = \"ignored.dat\"\nend\r\n\ - OBJECT PreBuild\r\n # model1 = \"comment.dat\"\r\n\ - model2=\"first.dat\"\r\n model1 = \"second.dat\"\r\n model3=\"FIRST.DAT\"\r\nend\n"; - assert_eq!( - mission_prebuilds(config) - .unwrap() - .iter() - .map(NormalizedPath::as_str) - .collect::>(), - [ - "UNITS/UNITS/PREBLD/first.dat", - "UNITS/UNITS/PREBLD/second.dat" - ] - ); - assert!(mission_prebuilds( - b"object prebuild\nend\nobject other\nmodel1 = \"ignored.dat\"\nend" - ) - .unwrap() - .is_empty()); - } - #[test] #[ignore = "requires licensed corpus"] fn licensed_scripted_factory_builds_a_usable_builder() { 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 = 32..64; const DESCRIPTION: std::ops::Range = 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, 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, @@ -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::>(), + [ + "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() { -- cgit v1.2.3