From c4ad78d456c968d9c05eebf50c963d3541bed776 Mon Sep 17 00:00:00 2001 From: Ben Chan Date: Wed, 25 Nov 2026 03:05:32 +0800 Subject: [PATCH] Open the crossing editor on an object that has no path yet. --- src/crossing_edit.rs | 15 ++++----------- src/crossing_edit/tests.rs | 20 ++++++++++++++++++-- src/ui_crossing.rs | 4 +++- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/crossing_edit.rs b/src/crossing_edit.rs index 207b5064..75a5ed11 100644 --- a/src/crossing_edit.rs +++ b/src/crossing_edit.rs @@ -974,11 +974,6 @@ impl Sheet { } out } - - /// Whether anything the editor owns was found in the file at all. - pub fn has_paths(&self) -> bool { - !self.crossing.paths.is_empty() - } } /// Encode `text` the way the file it comes from was encoded: its byte-order mark, or the @@ -1020,18 +1015,16 @@ pub fn open(roots: &crate::install::Roots, rel: &str) -> anyhow::Result<(PathBuf Ok((path, bytes, Sheet::parse(&text))) } -/// Why this object cannot be edited here, if it cannot. +/// Why this object cannot be edited here, if it cannot. An object with no path of its own is +/// fine: the editor is where its first one is drawn. pub fn why_not(roots: &crate::install::Roots, rel: &str) -> Option<&'static str> { + // (`[paths]`: the paths live in a file of their own, which openMapper does not read) let path = find(roots, rel)?; let text = omsi_cfg::decode_text(&std::fs::read(path).ok()?); - if Sheet::parse(&text).has_paths() { - return None; - } - // `[paths]`: the paths live in a file of their own, which openMapper does not read if omsi_cfg::split_lines(&text).iter().any(|l| omsi_cfg::keyword_of(l).map(|k| k.eq_ignore_ascii_case("paths")).unwrap_or(false)) { return Some(crate::i18n::tr("its paths are in a file of their own (`[paths]`), which this editor does not read", "его пути в отдельном файле (`[paths]`), который этот редактор не читает")); } - Some(crate::i18n::tr("it has no path", "у него нет путей")) + None } /// `..` taken out of a path by its words alone. diff --git a/src/crossing_edit/tests.rs b/src/crossing_edit/tests.rs index 19d5e50c..54aeaed3 100644 --- a/src/crossing_edit/tests.rs +++ b/src/crossing_edit/tests.rs @@ -362,10 +362,10 @@ fn a_utf8_file_keeps_its_bytes() { } #[test] -fn an_object_without_paths_is_not_offered() { +fn an_object_without_paths_is_read_and_written_back_as_it_was() { let text = "[friendlyname]\nHouse\n[mesh]\nhouse.o3d\n"; let s = Sheet::parse(text); - assert!(!s.has_paths()); + assert!(s.crossing.paths.is_empty()); // and a sheet with nothing owned leaves the file as it was assert_eq!(s.text(), text); } @@ -423,6 +423,22 @@ fn scratch_roots(name: &str) -> crate::install::Roots { crate::install::Roots { omsi: dir.join("omsi"), content: dir.join("content") } } +/// An object with no path of its own opens in the editor all the same: that is where its first +/// one is drawn. One whose paths live in a `[paths]` file of their own is still out of reach. +#[test] +fn an_object_with_no_path_of_its_own_is_still_the_editors() { + let roots = scratch_roots("nopath"); + let dir = roots.content.join("Sceneryobjects").join("Test"); + std::fs::create_dir_all(&dir).unwrap(); + let rel = "Sceneryobjects\\Test\\plain.sco"; + std::fs::write(omsi_cfg::resolve_path(&roots.content, rel), "[friendlyname]\nplain\n\n[model]\nmodel\\plain.x\n").unwrap(); + assert_eq!(why_not(&roots, rel), None, "no path yet, and the editor is where one is drawn"); + let spread = "Sceneryobjects\\Test\\spread.sco"; + std::fs::write(omsi_cfg::resolve_path(&roots.content, spread), "[friendlyname]\nspread\n\n[paths]\npaths\\spread.apf\n").unwrap(); + assert!(why_not(&roots, spread).is_some(), "its paths are in a file this editor does not read"); + let _ = std::fs::remove_dir_all(roots.content.parent().unwrap()); +} + /// Writing into the object's own file keeps the file as it stood beside it as a `.bak`, and /// nothing is written (nor the backup touched) when the text is the file's own. #[test] diff --git a/src/ui_crossing.rs b/src/ui_crossing.rs index 6232b494..e6955829 100644 --- a/src/ui_crossing.rs +++ b/src/ui_crossing.rs @@ -2751,7 +2751,9 @@ impl CrossingPaths { self.open_on(ed, roots, k, id) } - /// Read the object's `.sco` for editing. False (and a message) when it has no paths. + /// Read the object's `.sco` for editing. False (and a message) when it cannot be read, or + /// when its paths are in a file of their own. An object with no path at all opens with an + /// empty list: the first one is drawn or started there. fn open_on(&mut self, ed: &Editor, roots: &Roots, k: (i32, i32), id: i64) -> bool { let Some(doc) = ed.doc.as_ref() else { return false }; let Some(o) = doc.object(k, id) else { return false };