Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions src/crossing_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 18 additions & 2 deletions src/crossing_edit/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion src/ui_crossing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down