diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 882905617..b3df5aa8e 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -529,6 +529,7 @@ struct Game<'a> { message: Option<(String, Instant)>, save: Arc, level: Level, + session_lock: Option, audio: Audio, music: horizon_client::music::MusicManager, ambience: horizon_client::ambience::Ambience, @@ -4691,6 +4692,8 @@ impl Game<'_> { ) -> Result<()> { let save_root = std::env::current_dir()?.join("saves").join(folder); horizon_client::save::migrate_legacy(&save_root)?; + self.session_lock = None; + self.session_lock = Some(horizon_client::save::SessionLock::acquire(&save_root)?); self.persist = Default::default(); self.score = Default::default(); self.fns = Default::default(); @@ -7760,6 +7763,7 @@ pub fn run() -> Result<()> { message: None, save, level, + session_lock: None, audio, music: Default::default(), ambience: Default::default(), diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 151089018..da08be556 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -113,6 +113,26 @@ pub fn dimension_dir(world: &Path, dimension: &str) -> PathBuf { .unwrap_or(("minecraft", dimension)); world.join("dimensions").join(namespace).join(path) } +/// `DirectoryLock`: `session.lock` holds "\u{2603}" (UTF-16BE) under an exclusive +/// file lock for as long as the world is open, so two instances cannot share it. +pub struct SessionLock(#[allow(dead_code)] fs::File); +impl SessionLock { + pub fn acquire(world: &Path) -> Result { + use std::io::Write; + fs::create_dir_all(world)?; + let mut file = fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(false) + .open(world.join("session.lock"))?; + file.try_lock() + .map_err(|_| format!("{} is already open in another instance", world.display()))?; + file.set_len(0)?; + file.write_all(&[0x26, 0x03])?; + file.flush()?; + Ok(Self(file)) + } +} /// Move a world saved by an older Horizon build (pre-26.3 layout with a /// DataVersion that keeps vanilla from upgrading it) into the 26.3 layout. pub fn migrate_legacy(world: &Path) -> Result<()> { @@ -2167,6 +2187,19 @@ impl ChunkGenerator for Persistent { #[cfg(test)] mod tests { + #[test] + fn session_lock_is_exclusive() { + let dir = std::env::temp_dir().join(format!("horizon-lock-{}", std::process::id())); + let first = super::SessionLock::acquire(&dir).expect("first lock"); + assert!(super::SessionLock::acquire(&dir).is_err()); + assert_eq!( + std::fs::read(dir.join("session.lock")).unwrap(), + [0x26, 0x03] + ); + drop(first); + assert!(super::SessionLock::acquire(&dir).is_ok()); + let _ = std::fs::remove_dir_all(&dir); + } #[test] fn crafter_block_entity_round_trips() { let mut crafter = crate::containers::Crafter::default();