diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 38b3af3d0..27636b0eb 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -109,18 +109,18 @@ jobs: - name: ALSA and udev headers if: runner.os == 'Linux' run: sudo apt-get update && sudo apt-get install -y --no-install-recommends libasound2-dev libudev-dev - - name: Build the client, horizon-ctl and horizon-prepare - run: cargo build --release --locked -p horizon-client --bin horizon-client --bin horizon-ctl -p horizon-prepare --bin horizon-prepare + - name: Build the client, horizon-ctl, horizon-prepare and horizon-server + run: cargo build --release --locked -p horizon-client --bin horizon-client --bin horizon-ctl -p horizon-prepare --bin horizon-prepare -p horizon-server --bin horizon-server - name: Every program the release folder takes was built run: | - for binary in horizon-client horizon-ctl horizon-prepare; do + for binary in horizon-client horizon-ctl horizon-prepare horizon-server; do test -f "target/release/$binary${{ matrix.exe }}" || { echo "::error::target/release/$binary${{ matrix.exe }} is missing"; exit 1; } done - name: Only system libraries # Windows: no non-system DLL (a "DLL not found" dialog on some players' machines). Linux: the standard # loader and libraries every desktop distribution has. macOS: only the system's own libraries. run: | - for binary in horizon-client horizon-ctl horizon-prepare; do + for binary in horizon-client horizon-ctl horizon-prepare horizon-server; do file="target/release/$binary${{ matrix.exe }}" if [[ "$RUNNER_OS" == macOS ]]; then others=$(otool -L "$file" | tail -n +2 | awk '{print $1}' | grep -v -E '^(/usr/lib/|/System/Library/)' || true) @@ -137,6 +137,7 @@ jobs: "$bin/horizon-ctl${{ matrix.exe }}" --help > /dev/null "$bin/horizon-prepare${{ matrix.exe }}" --version "$bin/horizon-prepare${{ matrix.exe }}" --help | grep -q -- '--minecraft-dir' + "$bin/horizon-server${{ matrix.exe }}" --version - name: Tools from the runner if: runner.environment == 'self-hosted' run: bash tools/ci/runner-env.sh diff --git a/Cargo.lock b/Cargo.lock index 6ebc69964..1d5897d11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1398,6 +1398,14 @@ dependencies = [ "serde_json", ] +[[package]] +name = "horizon-bots" +version = "0.1.0" +dependencies = [ + "horizon-protocol", + "serde_json", +] + [[package]] name = "horizon-client" version = "0.1.0" @@ -1417,6 +1425,7 @@ dependencies = [ "horizon-friends", "horizon-mods", "horizon-protocol", + "horizon-server-files", "horizon-shaderpack", "horizon-telemetry", "horizon-theme", @@ -1541,6 +1550,31 @@ dependencies = [ "serde_json", ] +[[package]] +name = "horizon-server" +version = "0.1.0" +dependencies = [ + "horizon-bench", + "horizon-server-files", + "libc", + "serde", + "serde_json", + "toml", + "toml_edit", + "windows-sys 0.61.2", + "zip", +] + +[[package]] +name = "horizon-server-files" +version = "0.1.0" +dependencies = [ + "horizon-core", + "horizon-protocol", + "horizon-world", + "log", +] + [[package]] name = "horizon-shaderpack" version = "0.1.0" @@ -3739,6 +3773,7 @@ dependencies = [ "indexmap", "toml_datetime", "toml_parser", + "toml_writer", "winnow", ] diff --git a/Cargo.toml b/Cargo.toml index 1b4cb92d4..30a30386d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] exclude = ["launcher", "crates/horizon-telemetry-server"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-shaderpack", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] +members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-shaderpack", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] resolver = "2" [workspace.package] diff --git a/crates/horizon-bots/Cargo.toml b/crates/horizon-bots/Cargo.toml new file mode 100644 index 000000000..5a3e5e5c7 --- /dev/null +++ b/crates/horizon-bots/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "horizon-bots" +version = "0.1.0" +edition.workspace = true +license.workspace = true +rust-version.workspace = true +description = "Load-test bots for Minecraft Java Edition 26.3 servers (docs/server.md, section 9)" + +[dependencies] +horizon-protocol = { path = "../horizon-protocol" } +serde_json = "1.0.151" + +[lints] +workspace = true diff --git a/crates/horizon-bots/src/main.rs b/crates/horizon-bots/src/main.rs new file mode 100644 index 000000000..d76da5e84 --- /dev/null +++ b/crates/horizon-bots/src/main.rs @@ -0,0 +1,589 @@ +//! `horizon-bots`: load-test bots for any Minecraft Java Edition 26.3 server, vanilla, Paper or Horizon +//! (docs/server.md, section 9). Each bot logs in offline, goes through configuration as vanilla's client +//! does, answers keep-alives and pings, accepts teleports, acknowledges chunk batches, and moves at 20 Hz. +//! +//! Bots fly a little above the ground, so the server must give them creative mode (`gamemode=creative` +//! and `force-gamemode=true`): they never touch blocks, so they need no world of their own, and no server +//! can refuse their moves for walking into terrain. Every behaviour is deterministic from `--seed`. +//! +//! horizon-bots --server 127.0.0.1:25565 --bots 25 --behaviour mixed --seconds 360 --report bots.json +//! +//! It prints one JSON line a second (bots online, chunks received) and writes a summary with `--report`. + +use horizon_protocol::{ + PROTOCOL_VERSION, + buf::{Reader, Writer}, + connection::{ConnWriter, Connection}, + crypto::offline_uuid, + ids, + packets::{ + Empty, configuration, + handshake::{self, Intention}, + login::{self, ServerHello}, + play, + }, +}; +use serde_json::json; +use std::{ + sync::{ + Arc, Mutex, + atomic::{AtomicBool, AtomicU64, Ordering}, + }, + thread, + time::{Duration, Instant}, +}; + +const HELP: &str = "\ +horizon-bots: load-test bots for a Minecraft Java Edition 26.3 server (offline mode, creative) + + --server HOST:PORT default 127.0.0.1:25565 + --bots N default 10 + --behaviour B idle, walk, explore or mixed (60 % walk, 20 % explore, 20 % idle); default mixed + --join-every SECONDS default 1 + --seconds N how long to run after the first bot joins; default 60 + --view-distance N what the bots ask for; default 10 + --seed N default 12345 + --name PREFIX bot names: PREFIX0, PREFIX1...; default Bot + --report FILE a JSON summary at the end +"; + +#[derive(Clone, Copy, Debug, PartialEq)] +enum Behaviour { + Idle, + Walk, + Explore, +} + +impl Behaviour { + fn name(self) -> &'static str { + match self { + Self::Idle => "idle", + Self::Walk => "walk", + Self::Explore => "explore", + } + } +} + +struct Options { + host: String, + port: u16, + bots: usize, + behaviour: String, + join_every: f64, + seconds: u64, + view_distance: i8, + seed: u64, + name: String, + report: Option, +} + +fn options() -> Result { + let mut o = Options { + host: "127.0.0.1".into(), + port: 25565, + bots: 10, + behaviour: "mixed".into(), + join_every: 1.0, + seconds: 60, + view_distance: 10, + seed: 12345, + name: "Bot".into(), + report: None, + }; + let mut args = std::env::args().skip(1); + while let Some(arg) = args.next() { + if arg == "-h" || arg == "--help" { + print!("{HELP}"); + std::process::exit(0); + } + let value = args.next().ok_or(format!("{arg} needs a value"))?; + let number = |what: &str| format!("{arg} needs {what}, not {value:?}"); + match arg.as_str() { + "--server" => { + let (host, port) = value.rsplit_once(':').unwrap_or((&value, "25565")); + o.host = host.into(); + o.port = port.parse().map_err(|_| number("a port"))?; + } + "--bots" => o.bots = value.parse().map_err(|_| number("a number"))?, + "--behaviour" => o.behaviour = value, + "--join-every" => o.join_every = value.parse().map_err(|_| number("seconds"))?, + "--seconds" => o.seconds = value.parse().map_err(|_| number("seconds"))?, + "--view-distance" => o.view_distance = value.parse().map_err(|_| number("2 to 32"))?, + "--seed" => o.seed = value.parse().map_err(|_| number("a number"))?, + "--name" => o.name = value, + "--report" => o.report = Some(value), + _ => return Err(format!("unknown option {arg}; --help lists them")), + } + } + if !["idle", "walk", "explore", "mixed"].contains(&o.behaviour.as_str()) { + return Err(format!( + "--behaviour {}: use idle, walk, explore or mixed", + o.behaviour + )); + } + Ok(o) +} + +/// What one bot is doing, shared between its reader and its ticks. +#[derive(Default)] +struct Bot { + online: AtomicBool, + chunks: AtomicU64, + /// Where the server last put the bot (a teleport), to start from. + teleport: Mutex>, + error: Mutex>, + joined_after_ms: AtomicU64, +} + +/// A small deterministic generator (SplitMix64), so every run moves the bots the same way. +struct Random(u64); +impl Random { + fn next(&mut self) -> u64 { + self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); + let mut z = self.0; + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + z ^ (z >> 31) + } + fn unit(&mut self) -> f64 { + (self.next() >> 11) as f64 / (1u64 << 53) as f64 + } +} + +fn raw(id: i32, write: impl FnOnce(&mut Writer)) -> Vec { + let mut w = Writer::new(); + w.varint(id); + write(&mut w); + w.data +} + +fn err(e: impl std::fmt::Display) -> String { + e.to_string() +} + +/// Handshake, offline login and configuration; returns the connection in the play state. +fn join(o: &Options, name: &str) -> Result { + let mut c = Connection::connect(&o.host, o.port, Duration::from_secs(10)).map_err(err)?; + c.send( + &handshake::Serverbound::Intention(Intention { + protocol: PROTOCOL_VERSION, + host: o.host.clone(), + port: o.port, + intent: handshake::LOGIN, + }) + .encode(), + ) + .map_err(err)?; + c.send( + &login::Serverbound::Hello(ServerHello { + name: name.into(), + uuid: offline_uuid(name), + }) + .encode(), + ) + .map_err(err)?; + loop { + let (id, body) = c.recv().map_err(err)?; + match login::Clientbound::decode(id, &body).map_err(err)? { + login::Clientbound::Compression(p) => c.set_compression(p.threshold), + login::Clientbound::LoginFinished(_) => { + c.send(&login::Serverbound::LoginAcknowledged(Empty).encode()) + .map_err(err)?; + break; + } + login::Clientbound::Disconnect(p) => return Err(format!("refused: {}", p.reason_json)), + login::Clientbound::Hello(_) => { + return Err("the server is in online mode; bots need online-mode=false".into()); + } + login::Clientbound::CustomQuery(p) => c + .send( + &login::Serverbound::CustomQueryAnswer(login::CustomQueryAnswer { + transaction: p.transaction, + payload: None, + }) + .encode(), + ) + .map_err(err)?, + login::Clientbound::CookieRequest(p) => c + .send( + &login::Serverbound::CookieResponse(login::CookieResponse { + key: p.key, + payload: None, + }) + .encode(), + ) + .map_err(err)?, + login::Clientbound::Unknown(..) => {} + } + } + use ids::configuration::clientbound as cb; + c.send( + &configuration::Serverbound::ClientInformation(configuration::ClientInformation::new( + o.view_distance, + )) + .encode(), + ) + .map_err(err)?; + loop { + let (id, body) = c.recv().map_err(err)?; + let reply = match id { + cb::SELECT_KNOWN_PACKS + | cb::KEEP_ALIVE + | cb::PING + | cb::CODE_OF_CONDUCT + | cb::COOKIE_REQUEST + | cb::DISCONNECT + | cb::FINISH_CONFIGURATION => { + match configuration::Clientbound::decode(id, &body).map_err(err)? { + configuration::Clientbound::SelectKnownPacks(p) => { + configuration::Serverbound::SelectKnownPacks(p) + } + configuration::Clientbound::KeepAlive(p) => { + configuration::Serverbound::KeepAlive(p) + } + configuration::Clientbound::Ping(p) => configuration::Serverbound::Pong(p), + configuration::Clientbound::CodeOfConduct(_) => { + configuration::Serverbound::AcceptCodeOfConduct(Empty) + } + configuration::Clientbound::CookieRequest(p) => { + configuration::Serverbound::CookieResponse(configuration::CookieResponse { + key: p.key, + payload: None, + }) + } + configuration::Clientbound::Disconnect(_) => { + return Err("disconnected during configuration".into()); + } + configuration::Clientbound::FinishConfiguration(_) => { + c.send(&configuration::Serverbound::FinishConfiguration(Empty).encode()) + .map_err(err)?; + return Ok(c); + } + _ => continue, + } + } + _ => continue, + }; + c.send(&reply.encode()).map_err(err)?; + } +} + +/// Reads the play state: answers what a client must answer, counts chunks, records teleports. +fn read( + mut reader: horizon_protocol::connection::ConnReader, + writer: Arc>, + bot: Arc, +) { + use ids::play::clientbound as cb; + use ids::play::serverbound as sb; + let send = |body: Vec| writer.lock().unwrap().send(&body); + loop { + let (id, body) = match reader.recv() { + Ok(packet) => packet, + Err(e) => { + if bot.online.swap(false, Ordering::SeqCst) { + *bot.error.lock().unwrap() = Some(e.to_string()); + } + return; + } + }; + let mut r = Reader::new(&body); + let sent = match id { + cb::KEEP_ALIVE => match r.i64() { + Ok(v) => send(raw(sb::KEEP_ALIVE, |w| w.i64(v))), + Err(_) => Ok(()), + }, + cb::PING => match r.i32() { + Ok(v) => send(raw(sb::PONG, |w| w.i32(v))), + Err(_) => Ok(()), + }, + cb::LEVEL_CHUNK_WITH_LIGHT => { + bot.chunks.fetch_add(1, Ordering::Relaxed); + Ok(()) + } + cb::CHUNK_BATCH_FINISHED => send( + play::Serverbound::ChunkBatchReceived(play::ChunkBatchReceived { + chunks_per_tick: 25.0, + }) + .encode(), + ), + cb::PLAYER_POSITION => match play::Clientbound::decode(id, &body) { + Ok(play::Clientbound::PlayerPosition(p)) => { + let mut at = bot.teleport.lock().unwrap(); + let previous = at.unwrap_or([0.0; 3]); + // Relative flags: X 1, Y 2, Z 4. + let pos: [f64; 3] = std::array::from_fn(|i| { + if p.relatives & (1 << i) != 0 { + previous[i] + p.position[i] + } else { + p.position[i] + } + }); + *at = Some(pos); + send( + play::Serverbound::AcceptTeleportation(play::AcceptTeleportation { + id: p.teleport, + x: pos[0], + y: pos[1], + z: pos[2], + yaw: p.yaw, + pitch: p.pitch, + }) + .encode(), + ) + } + _ => Ok(()), + }, + cb::DISCONNECT | cb::START_CONFIGURATION => { + bot.online.store(false, Ordering::SeqCst); + *bot.error.lock().unwrap() = Some(if id == cb::DISCONNECT { + format!( + "disconnected: {}", + String::from_utf8_lossy(&body) + .chars() + .filter(|c| !c.is_control()) + .collect::() + ) + } else { + "sent back to configuration".into() + }); + return; + } + _ => Ok(()), + }; + if sent.is_err() { + bot.online.store(false, Ordering::SeqCst); + return; + } + } +} + +/// One bot: join, then move every 50 ms until `until`. +fn run_bot( + o: Arc, + index: usize, + behaviour: Behaviour, + bot: Arc, + until: Arc>>, +) { + let name = format!("{}{index}", o.name); + let asked = Instant::now(); + let connection = match join(&o, &name) { + Ok(c) => c, + Err(e) => { + *bot.error.lock().unwrap() = Some(e); + return; + } + }; + let (reader, writer) = connection.split(); + let writer = Arc::new(Mutex::new(writer)); + bot.online.store(true, Ordering::SeqCst); + { + let (writer, bot) = (writer.clone(), bot.clone()); + thread::spawn(move || read(reader, writer, bot)); + } + use ids::play::serverbound as sb; + // Wait for the spawn position. + let spawn = loop { + if let Some(at) = *bot.teleport.lock().unwrap() { + break at; + } + if !bot.online.load(Ordering::SeqCst) || asked.elapsed() > Duration::from_secs(60) { + if bot.error.lock().unwrap().is_none() { + *bot.error.lock().unwrap() = Some("no spawn position within 60 s".into()); + } + return; + } + thread::sleep(Duration::from_millis(20)); + }; + bot.joined_after_ms + .store(asked.elapsed().as_millis() as u64, Ordering::SeqCst); + let send = |body: Vec| writer.lock().unwrap().send(&body).is_ok(); + send(raw(sb::PLAYER_LOADED, |_| {})); + let mut random = Random(o.seed ^ (index as u64).wrapping_mul(0x2545_F491_4F6C_DD1D)); + let mut pos = spawn; + let cruise = spawn[1] + 24.0; + let heading = random.unit() * std::f64::consts::TAU; + let mut target = pos; + let mut next = Instant::now(); + loop { + if until.lock().unwrap().is_some_and(|u| Instant::now() >= u) + || !bot.online.load(Ordering::SeqCst) + { + break; + } + // A teleport from the server wins over where the bot thought it was. + if let Some(at) = bot.teleport.lock().unwrap().take() { + pos = at; + } + if pos[1] < cruise { + pos[1] = (pos[1] + 0.5).min(cruise); + } else { + match behaviour { + Behaviour::Idle => {} + Behaviour::Walk => { + let (dx, dz) = (target[0] - pos[0], target[2] - pos[2]); + let distance = dx.hypot(dz); + if distance < 1.0 { + let angle = random.unit() * std::f64::consts::TAU; + let reach = 8.0 + random.unit() * 40.0; + target = [ + spawn[0] + angle.cos() * reach, + cruise, + spawn[2] + angle.sin() * reach, + ]; + } else { + // Sprinting: 5.6 blocks a second. + let step = 0.28_f64.min(distance); + pos[0] += dx / distance * step; + pos[2] += dz / distance * step; + } + } + Behaviour::Explore => { + // Flying fast in creative: about 22 blocks a second, which keeps new chunks coming. + pos[0] += heading.cos() * 1.1; + pos[2] += heading.sin() * 1.1; + } + } + } + let ok = send( + play::Serverbound::MovePlayerPos(play::MovePlayerPos { + x: pos[0], + y: pos[1], + z: pos[2], + flags: 0, + }) + .encode(), + ) && send(raw(sb::CLIENT_TICK_END, |_| {})); + if !ok { + bot.online.store(false, Ordering::SeqCst); + break; + } + next += Duration::from_millis(50); + let now = Instant::now(); + if next > now { + thread::sleep(next - now); + } else if now - next > Duration::from_secs(1) { + next = now; + } + } + // Offline first, so the reader doesn't count the closed connection as a failure. + bot.online.store(false, Ordering::SeqCst); + writer.lock().unwrap().shutdown(); +} + +fn behaviour_of(mode: &str, index: usize) -> Behaviour { + match mode { + "idle" => Behaviour::Idle, + "walk" => Behaviour::Walk, + "explore" => Behaviour::Explore, + // mixed: of every five bots, three walk, one explores, one stands. + _ => match index % 5 { + 0 | 2 | 4 => Behaviour::Walk, + 1 => Behaviour::Explore, + _ => Behaviour::Idle, + }, + } +} + +fn main() { + let o = match options() { + Ok(o) => Arc::new(o), + Err(e) => { + eprintln!("horizon-bots: {e}"); + std::process::exit(2); + } + }; + let start = Instant::now(); + let until: Arc>> = Arc::new(Mutex::new(None)); + let bots: Vec> = (0..o.bots).map(|_| Arc::new(Bot::default())).collect(); + let launcher = { + let (o, bots, until) = (o.clone(), bots.clone(), until.clone()); + thread::spawn(move || { + let mut handles = Vec::new(); + for (i, bot) in bots.iter().enumerate() { + let (o2, bot, until) = (o.clone(), bot.clone(), until.clone()); + let behaviour = behaviour_of(&o.behaviour, i); + handles.push(thread::spawn(move || run_bot(o2, i, behaviour, bot, until))); + thread::sleep(Duration::from_secs_f64(o.join_every)); + } + handles + }) + }; + // The run lasts --seconds after the first bot is in. + let mut previous_chunks = 0; + let mut first = None; + loop { + thread::sleep(Duration::from_secs(1)); + let online = bots + .iter() + .filter(|b| b.online.load(Ordering::SeqCst)) + .count(); + let chunks: u64 = bots.iter().map(|b| b.chunks.load(Ordering::Relaxed)).sum(); + let failed = bots + .iter() + .filter(|b| b.error.lock().unwrap().is_some()) + .count(); + if first.is_none() && online > 0 { + first = Some(Instant::now()); + *until.lock().unwrap() = Some(Instant::now() + Duration::from_secs(o.seconds)); + } + println!( + "{}", + json!({ + "t": start.elapsed().as_secs(), + "online": online, + "failed": failed, + "chunks": chunks, + "chunks_per_second": chunks - previous_chunks, + }) + ); + previous_chunks = chunks; + let over = until.lock().unwrap().is_some_and(|u| Instant::now() >= u); + let all_failed = failed == o.bots; + if over || all_failed || (first.is_none() && start.elapsed() > Duration::from_secs(120)) { + *until.lock().unwrap() = Some(Instant::now()); + break; + } + } + for handle in launcher.join().unwrap_or_default() { + let _ = handle.join(); + } + let summary = json!({ + "server": format!("{}:{}", o.host, o.port), + "bots": o.bots, + "behaviour": o.behaviour, + "seed": o.seed, + "view_distance": o.view_distance, + "seconds": o.seconds, + "joined": bots.iter().filter(|b| b.joined_after_ms.load(Ordering::SeqCst) > 0).count(), + "chunks": bots.iter().map(|b| b.chunks.load(Ordering::Relaxed)).sum::(), + "per_bot": bots.iter().enumerate().map(|(i, b)| json!({ + "name": format!("{}{i}", o.name), + "behaviour": behaviour_of(&o.behaviour, i).name(), + "joined_after_ms": b.joined_after_ms.load(Ordering::SeqCst), + "chunks": b.chunks.load(Ordering::Relaxed), + "error": *b.error.lock().unwrap(), + })).collect::>(), + }); + if let Some(path) = &o.report { + if let Err(e) = std::fs::write(path, format!("{summary:#}\n")) { + eprintln!("horizon-bots: {path}: {e}"); + } + } + let errors: Vec = bots + .iter() + .enumerate() + .filter_map(|(i, b)| { + b.error + .lock() + .unwrap() + .clone() + .map(|e| format!("{}{i}: {e}", o.name)) + }) + .collect(); + for e in errors.iter().take(10) { + eprintln!("horizon-bots: {e}"); + } + std::process::exit(if errors.is_empty() { 0 } else { 1 }); +} diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index 19b4c81db..29a02553f 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -12,6 +12,8 @@ horizon-assets = { path = "../horizon-assets" } horizon-core = { path = "../horizon-core" } horizon-world = { path = "../horizon-world" } horizon-protocol = { path = "../horizon-protocol" } +# server.properties, eula.txt and the user lists, shared with horizon-server. +horizon-server-files = { path = "../horizon-server-files" } horizon-friends = { path = "../horizon-friends" } # Horizon mods: sandboxed WebAssembly components, off unless installed and turned on (docs/mods.md). horizon-mods = { path = "../horizon-mods" } diff --git a/crates/horizon-client/src/debug_chart.rs b/crates/horizon-client/src/debug_chart.rs index d006a8ab6..35fd40bd4 100644 --- a/crates/horizon-client/src/debug_chart.rs +++ b/crates/horizon-client/src/debug_chart.rs @@ -74,6 +74,10 @@ pub struct DebugCharts { pub bandwidth: SampleLogger, /// `MinecraftServer.smoothedTickTimeMillis`. pub smoothed_tick_ms: f32, + /// Connection's smoothed packets per second, sent then received. + pub packet_rates: [f32; 2], + packet_totals: [i64; 2], + packet_tick: u8, previous_tick: Option, bytes_seen: Option, } @@ -113,6 +117,9 @@ impl DebugCharts { self.previous_tick = None; self.bytes_seen = None; self.smoothed_tick_ms = 0.0; + self.packet_rates = [0.0; 2]; + self.packet_totals = [0; 2]; + self.packet_tick = 0; } /// One integrated server tick that started at `started` and ran its /// tick method for `method` (`MinecraftServer.logFullTickTime` and @@ -136,6 +143,17 @@ impl DebugCharts { } self.bytes_seen = Some(total); } + /// `Connection.tickSecond`: sampled on the first tick, then every 20 ticks. + pub fn log_packets(&mut self, totals: [i64; 2]) { + if self.packet_tick == 0 { + for (i, total) in totals.into_iter().enumerate() { + let count = (total - self.packet_totals[i]).max(0) as f32; + self.packet_rates[i] = count + 0.75 * (self.packet_rates[i] - count); + } + self.packet_totals = totals; + } + self.packet_tick = (self.packet_tick + 1) % 20; + } /// The charts along the bottom of the screen (`extractRenderState`): /// frame times left and tick times right, or bandwidth left (remote /// servers only) and ping right. `mspt` is the level's milliseconds @@ -524,6 +542,27 @@ mod tests { assert!(draws.contains(&fill(361, 174, 365, 178, 0xFF0A_141E))); } + #[test] + fn packet_rates_follow_connection_smoothing_and_reset_between_worlds() { + let mut charts = DebugCharts::default(); + charts.log_packets([80, 400]); + assert_eq!(charts.packet_rates, [20.0, 100.0]); + for _ in 1..20 { + charts.log_packets([180, 800]); + } + assert_eq!(charts.packet_rates, [20.0, 100.0]); + charts.log_packets([180, 800]); + assert_eq!(charts.packet_rates, [40.0, 175.0]); + for _ in 0..20 { + charts.log_packets([180, 800]); + } + assert_eq!(charts.packet_rates, [30.0, 131.25]); + charts.reset(); + assert_eq!(charts.packet_rates, [0.0, 0.0]); + charts.log_packets([4, 8]); + assert_eq!(charts.packet_rates, [1.0, 2.0]); + } + #[test] fn ticks_and_bandwidth_log_their_differences() { let mut c = DebugCharts::default(); diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index 4e1220d91..adeb1da25 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -1046,12 +1046,15 @@ pub fn arrow_from_nbt( } /// `FallingBlockEntity`: BlockState, Time, DropItem, HurtEntities. +#[allow(clippy::too_many_arguments)] pub fn falling_block_to_nbt( state: Tag, position: Vec3, motion: Vec3, time: i32, drop_item: bool, + damage: crate::falling_block::Damage, + fall_distance: f64, uuid: [i32; 4], ) -> Tag { let base = Base { @@ -1061,29 +1064,27 @@ pub fn falling_block_to_nbt( motion, rotation: [0.0, 0.0], on_ground: false, - fall_distance: 0.0, + fall_distance, }; let mut tag = base.tag(None); for (key, value) in [ ("BlockState", state), ("Time", Tag::Int(time)), ("DropItem", Tag::Byte(i8::from(drop_item))), - ("HurtEntities", Tag::Byte(0)), - ("CancelDrop", Tag::Byte(0)), - ("FallHurtAmount", Tag::Float(0)), - ("FallHurtMax", Tag::Int(40)), ] { put(&mut tag, key, value); } + damage.save(&mut tag); tag } -/// (block state tag, position, motion, time) -pub fn falling_block_from_nbt(tag: &Tag) -> Option<(Tag, Vec3, Vec3, i32)> { +/// (block state tag, position, motion, time, fall distance) +pub fn falling_block_from_nbt(tag: &Tag) -> Option<(Tag, Vec3, Vec3, i32, f64)> { Some(( tag.get("BlockState")?.clone(), position(tag)?, motion(tag), number(tag.get("Time")).unwrap_or(0) as i32, + float(tag.get("fall_distance")).unwrap_or(0.0), )) } @@ -1493,6 +1494,36 @@ mod tests { use super::*; use crate::items::ItemStack; + #[test] + fn falling_block_round_trip_keeps_impact_distance_and_settings() { + let state = Tag::compound([("Name", string("minecraft:damaged_anvil"))]); + let position = Vec3::new(3.5, 100.0, -7.5); + let velocity = Vec3::new(0.0, -1.25, 0.0); + let damage = crate::falling_block::Damage { + cancel_drop: true, + ..crate::falling_block::Damage::anvil() + }; + let tag = falling_block_to_nbt( + state.clone(), + position, + velocity, + 30, + false, + damage, + 19.75, + [1, 2, 3, 4], + ); + let (loaded_state, pos, motion, age, distance) = falling_block_from_nbt(&tag).unwrap(); + assert_eq!(loaded_state, state); + assert_eq!( + (pos, motion, age, distance), + (position, velocity, 30, 19.75) + ); + assert_eq!(falling_block_flags(&tag), (false, false)); + assert_eq!(crate::falling_block::Damage::from_nbt(&tag, true), damage); + assert_eq!(uuid(&tag), Some([1, 2, 3, 4])); + } + #[test] fn item_entities_round_trip() { let e = ItemEntity { diff --git a/crates/horizon-client/src/falling_block.rs b/crates/horizon-client/src/falling_block.rs new file mode 100644 index 000000000..86c6ebd6f --- /dev/null +++ b/crates/horizon-client/src/falling_block.rs @@ -0,0 +1,192 @@ +//! FallingBlockEntity's impact damage, anvil wear and saved impact settings. +use crate::{ + mobs::{Damage as MobDamage, DamageKind, Mob}, + save::{float, number, put}, +}; +use horizon_core::geometry::{Aabb, Vec3}; +use horizon_world::{chunk::BlockState, nbt::Tag}; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Damage { + pub hurt_entities: bool, + pub cancel_drop: bool, + pub per_distance: f32, + pub max: i32, +} +impl Default for Damage { + fn default() -> Self { + Self { + hurt_entities: false, + cancel_drop: false, + per_distance: 0.0, + max: 40, + } + } +} +impl Damage { + pub fn anvil() -> Self { + Self { + hurt_entities: true, + per_distance: 2.0, + ..Self::default() + } + } + pub fn from_nbt(tag: &Tag, anvil: bool) -> Self { + Self { + hurt_entities: number(tag.get("HurtEntities")).map_or(anvil, |v| v != 0), + cancel_drop: number(tag.get("CancelDrop")).is_some_and(|v| v != 0), + per_distance: float(tag.get("FallHurtAmount")).unwrap_or(0.0) as f32, + max: number(tag.get("FallHurtMax")).unwrap_or(40) as i32, + } + } + pub fn save(self, tag: &mut Tag) { + for (key, value) in [ + ("HurtEntities", Tag::Byte(i8::from(self.hurt_entities))), + ("CancelDrop", Tag::Byte(i8::from(self.cancel_drop))), + ("FallHurtAmount", Tag::Float(self.per_distance.to_bits())), + ("FallHurtMax", Tag::Int(self.max)), + ] { + put(tag, key, value); + } + } + pub fn impact(self, fall_distance: f64) -> Option { + if !self.hurt_entities { + return None; + } + let steps = (fall_distance - 1.0).ceil() as i32; + if steps < 0 { + return None; + } + let amount = ((steps as f32 * self.per_distance).floor() as i32).min(self.max) as f32; + Some(Impact { + amount, + wear_chance: if amount > 0.0 { + 0.05 + steps as f32 * 0.05 + } else { + 0.0 + }, + }) + } +} + +#[derive(Clone, Copy, Debug)] +pub struct Impact { + pub amount: f32, + pub wear_chance: f32, +} + +/// AnvilBlock.damage: advance wear without changing facing; false means broken. +pub fn wear_anvil(state: &mut BlockState) -> bool { + state.name = match state.name.as_str() { + "minecraft:anvil" => "minecraft:chipped_anvil", + "minecraft:chipped_anvil" => "minecraft:damaged_anvil", + _ => return false, + } + .into(); + true +} + +pub fn bounds(position: Vec3) -> Aabb { + let size = f64::from(0.98_f32); + Aabb::new( + Vec3::new(position.x - size / 2.0, position.y, position.z - size / 2.0), + Vec3::new( + position.x + size / 2.0, + position.y + size, + position.z + size / 2.0, + ), + ) +} +pub fn hurt_mobs(mobs: &mut [Mob], position: Vec3, amount: f32) { + if amount <= 0.0 { + return; + } + let bounds = bounds(position); + let source = MobDamage { + kind: DamageKind::FallingBlock, + attacker: None, + from: Some([position.x, position.y, position.z]), + }; + for mob in mobs + .iter_mut() + .filter(|m| m.alive() && bounds.intersects(m.bounds())) + { + mob.damage(amount, source); + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn impact_uses_ceil_distance_capped_damage_and_positive_damage_wear() { + let damage = Damage::anvil(); + assert!(Damage::default().impact(50.0).is_none()); + assert!(damage.impact(0.0).is_none()); + let short = damage.impact(1.0).unwrap(); + assert_eq!((short.amount, short.wear_chance), (0.0, 0.0)); + assert_eq!(damage.impact(1.01).unwrap().amount, 2.0); + assert_eq!(damage.impact(5.0).unwrap().amount, 8.0); + assert_eq!(damage.impact(100.0).unwrap().amount, 40.0); + assert!(damage.impact(21.0).unwrap().wear_chance > 1.0); + } + #[test] + fn damage_settings_round_trip_and_keep_vanilla_load_defaults() { + let empty = Tag::Compound(Default::default()); + assert_eq!(Damage::from_nbt(&empty, false), Damage::default()); + let loaded_anvil = Damage::from_nbt(&empty, true); + assert!(loaded_anvil.hurt_entities); + assert_eq!(loaded_anvil.per_distance, 0.0); + let damage = Damage { + hurt_entities: true, + cancel_drop: true, + per_distance: 3.5, + max: 17, + }; + let mut tag = empty; + damage.save(&mut tag); + assert_eq!(Damage::from_nbt(&tag, false), damage); + } + #[test] + fn anvil_wears_twice_then_breaks_preserving_facing() { + for facing in ["north", "south", "east", "west"] { + let mut state = BlockState { + name: "minecraft:anvil".into(), + properties: [("facing".into(), facing.into())].into(), + }; + for name in ["minecraft:chipped_anvil", "minecraft:damaged_anvil"] { + assert!(wear_anvil(&mut state)); + assert_eq!(state.name, name); + assert_eq!(state.properties["facing"], facing); + } + assert!(!wear_anvil(&mut state)); + } + } + #[test] + #[ignore = "requires prepared entity models/data in HORIZON_TEST_RESOURCES"] + fn impact_hurts_only_living_intersecting_noninvulnerable_mobs() { + use crate::mobs::{Mob, MobTypes}; + use std::{path::Path, sync::Arc}; + let root = std::env::var_os("HORIZON_TEST_RESOURCES").expect("HORIZON_TEST_RESOURCES"); + let types = MobTypes::load(Path::new(&root)).unwrap(); + let cow = Arc::new(types.types["minecraft:cow"].clone()); + let mut mobs = vec![ + Mob::new(1, cow.clone(), Vec3::ZERO, 0.0), + Mob::new(2, cow.clone(), Vec3::new(3.0, 0.0, 0.0), 0.0), + Mob::new(3, cow.clone(), Vec3::ZERO, 0.0), + Mob::new(4, cow, Vec3::ZERO, 0.0), + ]; + mobs[2].invulnerable_flag = true; + mobs[3].extra.head = Some("minecraft:carved_pumpkin".into()); + let initial = mobs.iter().map(|m| m.health).collect::>(); + hurt_mobs( + &mut mobs, + Vec3::ZERO, + Damage::anvil().impact(5.0).unwrap().amount, + ); + assert_eq!(mobs[0].health, initial[0] - 8.0); + assert_eq!(mobs[1].health, initial[1]); + assert_eq!(mobs[2].health, initial[2]); + assert_eq!(mobs[3].health, initial[3] - 6.0); + } +} diff --git a/crates/horizon-client/src/front.rs b/crates/horizon-client/src/front.rs index 7b1771000..5b0a2c5ab 100644 --- a/crates/horizon-client/src/front.rs +++ b/crates/horizon-client/src/front.rs @@ -318,6 +318,7 @@ impl Title { x: f32, y: f32, ) -> Action { + self.remember_direct_address(); if self.skip_opening() { return Action::None; } @@ -472,7 +473,7 @@ impl Title { _ => None, }; if let Some(id) = id { - match screen.click(Some(id), 0.0, 0.0, (0.0, 0.0), &saves) { + match self.multiplayer_click(Some(id), 0.0, 0.0, (0.0, 0.0)) { Outcome::Back => self.mode = Mode::Main, Outcome::Connect(address) => return Action::Connect { address }, Outcome::None => {} @@ -1432,6 +1433,28 @@ mod tests { } } + #[test] + fn horizon_direct_connection_remembers_the_address_after_leaving() { + let atlas = + HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap(); + let saves = + std::env::temp_dir().join(format!("horizon-front-direct-{}", std::process::id())); + let mut title = Title::new(saves.clone(), &saves); + title.last_server = "localhost:25566".into(); + title.mode = Mode::Multiplayer(Screen::list(&saves)); + press(&mut title, &atlas, DIRECT); + assert_eq!(title.focused_field().unwrap().1, "localhost:25566"); + *title.focused_field().unwrap().1 = "example.org:25567".into(); + press(&mut title, &atlas, BACK); + press(&mut title, &atlas, DIRECT); + assert_eq!(title.focused_field().unwrap().1, "example.org:25567"); + *title.focused_field().unwrap().1 = "localhost:25568".into(); + press(&mut title, &atlas, NAV); + press(&mut title, &atlas, NAV + 2); + press(&mut title, &atlas, DIRECT); + assert_eq!(title.focused_field().unwrap().1, "localhost:25568"); + } + #[test] fn horizon_world_delete_confirms_and_keeps_the_selection_in_place() { let atlas = diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index b63c29c29..0454a0d69 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -41,6 +41,7 @@ mod entity_culling; mod events; mod explosion; mod extras; +mod falling_damage; mod fangs; mod fireworks; mod fishing; @@ -652,6 +653,7 @@ struct FallingBlock { drop_item: bool, /// `Entity.fallDistance` (powder snow holds a block that fell far at 0.9). fall_distance: f64, + damage: horizon_client::falling_block::Damage, /// The UUID and compound it was loaded with, kept while it moves. saved: Option>, } @@ -1970,6 +1972,7 @@ impl Game<'_> { 1009 => Some(("minecraft:block.fire.extinguish", 0.5)), 1502 => Some(("minecraft:block.redstone_torch.burnout", 0.5)), 1030 => Some(("minecraft:block.anvil.use", 1.0)), + 1029 => Some(("minecraft:block.anvil.destroy", 1.0)), 1031 => Some(("minecraft:block.anvil.land", 0.3)), 1505 => Some(("minecraft:item.bone_meal.use", 1.0)), _ => None, @@ -2022,6 +2025,11 @@ impl Game<'_> { no_gravity: false, drop_item: true, fall_distance: 0.0, + damage: if self.res.facts.get(state).name.ends_with("anvil") { + horizon_client::falling_block::Damage::anvil() + } else { + Default::default() + }, saved: None, }); } @@ -2277,14 +2285,19 @@ impl Game<'_> { return false; } if solid { - landed.push(( - f.state, - [ - next.x.floor() as i32, - next.y.floor() as i32 + i32::from(!sunk), - next.z.floor() as i32, - ], - )); + let pos = [ + next.x.floor() as i32, + next.y.floor() as i32 + i32::from(!sunk), + next.z.floor() as i32, + ]; + let landing_y = if sunk { + next.y.floor() + f64::from(0.9_f32) + } else { + f64::from(pos[1]) + }; + f.fall_distance += (f.position.y - landing_y).max(0.0); + f.position = Vec3::new(next.x, landing_y, next.z); + landed.push((f.clone(), pos)); return false; } if next.y < f.position.y { @@ -2318,30 +2331,8 @@ impl Game<'_> { self.spawn_item(stack, [at.x, at.y, at.z], v, 10); } } - for (state, pos) in landed { - let landing = self.with_level(|b, level, ticks| { - b.falling_block_landed( - level, - ticks, - BlockPos::new(pos[0], pos[1], pos[2]), - state, - false, - ) - }); - if matches!(landing, Landing::Broken) { - let item = self.res.facts.get(state).item.clone(); - if &*item != "minecraft:air" { - self.pop_resource( - pos, - ItemStack { - item, - count: 1, - damage: 0, - extra: None, - }, - ); - } - } + for (block, pos) in landed { + self.finish_falling_block(block, pos); } self.process_level_events() } @@ -3659,6 +3650,9 @@ impl Game<'_> { ) || self.book.is_some()) } fn capture(&mut self, capture: bool) { + if !capture { + self.stop_mining(); + } // The real pointer takes over from an agent's. self.agent_pointer = false; let Some(window) = &self.window else { return }; @@ -6648,6 +6642,9 @@ impl Game<'_> { std::env::current_dir()?.join("saves"), &self.options.resources, )); + if let Some(title) = &mut self.title { + title.last_server = self.settings.file.string("lastServer"); + } self.capture(false); Ok(()) } @@ -9301,6 +9298,7 @@ impl ApplicationHandler for Game<'_> { clicked } }); + self.save_title_options(); if sound { self.audio.play_button_click(); } @@ -9539,6 +9537,7 @@ impl ApplicationHandler for Game<'_> { if let Some(t) = self.title.as_mut() { t.escape(); } + self.save_title_options(); } _ => self.title_edit(event.text.as_deref(), false), } @@ -10323,10 +10322,11 @@ pub fn run() -> Result<()> { game.open_world(&folder, None, Some(seed), creative, None)?; } None => { - let title = Title::new( + let mut title = Title::new( std::env::current_dir()?.join("saves"), &game.options.resources, ); + title.last_server = game.settings.file.string("lastServer"); // The launcher's opening, once per session and only in Horizon's style. if horizon_client::ui_theme::horizon() && game.options.frames.is_none() { title diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 460b73708..076c2fbef 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -2985,6 +2985,7 @@ fn damage_type(kind: horizon_client::mobs::DamageKind, player: bool) -> &'static K::Explosion if player => "minecraft:player_explosion", K::Explosion => "minecraft:explosion", K::Fall => "minecraft:fall", + K::FallingBlock => "minecraft:falling_block", K::OnFire => "minecraft:on_fire", K::InFire => "minecraft:in_fire", K::Lava => "minecraft:lava", diff --git a/crates/horizon-client/src/game/debug_keys.rs b/crates/horizon-client/src/game/debug_keys.rs index 94fd59f61..36761a85e 100644 --- a/crates/horizon-client/src/game/debug_keys.rs +++ b/crates/horizon-client/src/game/debug_keys.rs @@ -194,15 +194,17 @@ impl Game<'_> { } return; }; - let received = r - .session - .counters - .lock() - .unwrap() - .as_ref() - .map(|c| c.bytes_in.load(std::sync::atomic::Ordering::Relaxed)); - if let Some(total) = received { - self.debug_charts.log_bandwidth(total); + let received = r.session.counters.lock().unwrap().as_ref().map(|c| { + let order = std::sync::atomic::Ordering::Relaxed; + [ + c.bytes_in.load(order), + c.sent.load(order), + c.received.load(order), + ] + }); + if let Some([bytes, sent, received]) = received { + self.debug_charts.log_bandwidth(bytes); + self.debug_charts.log_packets([sent, received]); } if shown { use horizon_protocol::packets::{play::Serverbound, status::Ping}; @@ -403,8 +405,9 @@ impl Game<'_> { ], ); } - // tps: DebugEntryTps for the integrated server (it counts no packets). - if self.player.is_some() && self.remote.is_none() { + // tps: DebugEntryTps for local and remote connections. + if self.player.is_some() { + let [tx, rx] = self.debug_charts.packet_rates.map(|n| n.round() as i64); let (target, status) = if self.clock.sprinting() { ("-".to_owned(), " (sprinting)") } else if self.clock.stepping() { @@ -417,10 +420,22 @@ impl Game<'_> { } else { (format!("{:.1}", 1000.0 / self.clock.rate()), "") }; - d.line(format!( - "Integrated server @ {:.1}/{target} ms{status}, 0 tx, 0 rx", - self.debug_charts.smoothed_tick_ms - )); + if self.remote.is_none() || self.integrated.is_some() { + let tick_ms = self + .integrated + .as_ref() + .map_or(self.debug_charts.smoothed_tick_ms, |server| { + server.smoothed_tick_ms() + }); + d.line(format!( + "Integrated server @ {tick_ms:.1}/{target} ms{status}, {tx} tx, {rx} rx", + )); + } else if let Some(remote) = &self.remote { + d.line(format!( + "\"{}\" server{status}, {tx} tx, {rx} rx", + remote.brand + )); + } } let (left, right) = d.columns(&self.debug_chart_lines()); let atlas = &self.atlas; diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index 062bb71c8..314ac2428 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -29,6 +29,8 @@ use std::{ time::{Duration, Instant}, }; +mod status; + /// Double.toString for the magnitudes positions have (no exponent form /// between 1e-3 and 1e7). pub(super) fn java_double(v: f64) -> String { @@ -64,6 +66,11 @@ pub(super) fn log(level: &str, message: &str) { _ => ::log::Level::Info, }; horizon_client::logging::write_only(file_level, message); + CAPTURE.with_borrow_mut(|lines| { + if let Some(lines) = lines { + lines.push(message.to_owned()); + } + }); let secs = now_millis().div_euclid(1000).rem_euclid(86_400); println!( "[{:02}:{:02}:{:02}] [Server thread/{level}]: {message}", @@ -73,6 +80,18 @@ pub(super) fn log(level: &str, message: &str) { ); } +thread_local! { + /// The lines logged while a control request runs a console command. + static CAPTURE: std::cell::RefCell>> = const { std::cell::RefCell::new(None) }; +} + +/// Runs `f` and returns the lines it logged. +fn capture_log(f: impl FnOnce()) -> Vec { + CAPTURE.set(Some(Vec::new())); + f(); + CAPTURE.take().unwrap_or_default() +} + /// Main.main up to the server start: arguments, server.properties (stored /// back at once), eula.txt. False when the process should stop here. pub(super) fn preflight(options: &mut Options) -> Result { @@ -289,6 +308,9 @@ struct ServerState { stop: bool, autosave: bool, empty_ticks: i32, + /// When the last passes of the loop ended, for the tick rate horizon-server shows. + ticks: std::collections::VecDeque, + paused: bool, } impl Game<'_> { @@ -471,14 +493,18 @@ impl Game<'_> { stop: false, autosave: true, empty_ticks: 0, + ticks: Default::default(), + paused: false, }; let pause_after = props.pause_when_empty_seconds * 20; let idle_timeout = props.player_idle_timeout; let mut next = Instant::now(); while !state.stop { + let tick_start = Instant::now(); while let Ok(line) = state.console.try_recv() { self.console_command(&mut state, &line); } + self.poll_server_control(&mut state); let players = self.lan.as_ref().map_or(0, |h| h.hub.guests.len()); if pause_after > 0 { if players == 0 { @@ -488,6 +514,7 @@ impl Game<'_> { } } let paused = pause_after > 0 && state.empty_ticks >= pause_after; + state.paused = paused; if paused && state.empty_ticks == pause_after { log( "INFO", @@ -513,6 +540,9 @@ impl Game<'_> { if idle_timeout > 0 { self.kick_idle(idle_timeout); } + // MinecraftServer.tickServer records each tick's time, for /tick query (and the status). + self.clock.record(tick_start.elapsed()); + state.ticked(Instant::now()); let rate = self.clock.rate().max(1.0); next += Duration::from_secs_f64(1.0 / f64::from(rate)); let now = Instant::now(); diff --git a/crates/horizon-client/src/game/dedicated/status.rs b/crates/horizon-client/src/game/dedicated/status.rs new file mode 100644 index 000000000..a2ec4c1ae --- /dev/null +++ b/crates/horizon-client/src/game/dedicated/status.rs @@ -0,0 +1,100 @@ +//! What `horizon-server` asks the dedicated server on its control endpoint (`--control-socket`, docs/server.md +//! section 2): `server-status` answers with the tick rate, the tick times `/tick query` reports, the players +//! and the loaded chunks; `console ` runs a console command and answers with the lines it logged. +use super::{ServerState, capture_log}; +use crate::game::Game; +use serde_json::{Value, json}; +use std::{ + sync::atomic::Ordering, + time::{Duration, Instant}, +}; + +/// The window the tick rate is measured over. +const TPS_WINDOW: Duration = Duration::from_secs(5); + +impl ServerState { + /// One pass of the server loop ended. + pub(super) fn ticked(&mut self, at: Instant) { + self.ticks.push_back(at); + while self + .ticks + .front() + .is_some_and(|t| at.duration_since(*t) > TPS_WINDOW) + { + self.ticks.pop_front(); + } + } + /// Ticks per second over the last few seconds. + fn tps(&self) -> f64 { + match (self.ticks.front(), self.ticks.back()) { + (Some(first), Some(last)) if self.ticks.len() > 1 => { + (self.ticks.len() - 1) as f64 / last.duration_since(*first).as_secs_f64().max(1e-9) + } + _ => 0.0, + } + } +} + +impl Game<'_> { + /// Answers the requests waiting on the control endpoint. + pub(super) fn poll_server_control(&mut self, state: &mut ServerState) { + while let Some(request) = self.control.as_ref().and_then(|c| c.try_recv()) { + let reply = match request.line.trim().split_once(' ') { + _ if request.line.trim() == "server-status" => self.server_status(state), + Some(("console", line)) => { + // Command feedback is logged as system chat; the reply carries the text alone. + let output: Vec = capture_log(|| self.console_command(state, line)) + .into_iter() + .map(|l| { + l.strip_prefix("System chat: ") + .map_or(l.clone(), str::to_owned) + }) + .collect(); + json!({ "output": output }) + } + _ => json!({ "error": format!("unknown request {:?}", request.line) }), + }; + let _ = request.reply.send(reply.to_string()); + } + } + + fn server_status(&self, state: &ServerState) -> Value { + let millis = |nanos: u64| nanos as f64 / 1e6; + let samples = self.clock.sorted_samples(); + // The percentiles `/tick query` prints (TickCommand.percentileToString). + let at = |q: f64| { + let i = ((samples.len() as f64 * q) as usize).min(samples.len().saturating_sub(1)); + samples.get(i).copied().map_or(0.0, millis) + }; + let (players, max_players) = self.lan.as_ref().map_or((Vec::new(), 0), |h| { + let players = h + .hub + .guests + .iter() + .map(|g| { + json!({ + "name": g.name, + "uuid": horizon_protocol::crypto::uuid_string(g.uuid), + "address": g.address.ip().to_string(), + "ping_ms": g.latency.load(Ordering::Relaxed), + }) + }) + .collect(); + (players, h.hub.lan.shared.max_players()) + }); + json!({ + "tps": state.tps(), + "target_tps": self.clock.rate(), + "mspt": { + "average": millis(self.clock.average_nanos()), + "p50": at(0.5), + "p95": at(0.95), + "p99": at(0.99), + }, + "players": players, + "max_players": max_players, + "chunks": self.world.chunks.len(), + "paused": state.paused, + }) + } +} diff --git a/crates/horizon-client/src/game/falling_damage.rs b/crates/horizon-client/src/game/falling_damage.rs new file mode 100644 index 000000000..16a003c04 --- /dev/null +++ b/crates/horizon-client/src/game/falling_damage.rs @@ -0,0 +1,123 @@ +//! Landing impacts for falling blocks, before their placement/drop decision. +use super::{FallingBlock, Game, Landing}; +use horizon_client::{falling_block, items::ItemStack}; +use horizon_core::{geometry::Vec3, position::BlockPos, random::RandomSource}; +use horizon_world::level::LevelEvent; + +impl Game<'_> { + pub(super) fn finish_falling_block(&mut self, mut block: FallingBlock, pos: [i32; 3]) { + let anvil = self.res.facts.get(block.state).name.ends_with("anvil"); + if let Some(impact) = block.damage.impact(block.fall_distance) { + if impact.amount > 0.0 { + falling_block::hurt_mobs(&mut self.mobs, block.position, impact.amount); + let kind = if anvil { + "minecraft:falling_anvil" + } else { + "minecraft:falling_block" + }; + self.falling_hurt_player(block.position, impact.amount, kind); + if let Some(mut host) = self.lan.take() { + let bounds = falling_block::bounds(block.position); + let conns = host + .hub + .guests + .iter() + .filter(|g| { + g.placed + && g.loaded + && !g.dead + && !g.creative() + && !g.spectator() + && bounds.intersects(super::lan_mobs::guest_bounds(g)) + }) + .map(|g| g.conn) + .collect::>(); + for conn in conns { + self.guest_hit_by(&mut host, conn, |g| { + g.falling_hurt_player(block.position, impact.amount, kind) + }); + } + self.lan = Some(host); + } + } + if anvil + && impact.wear_chance > 0.0 + && self.world.random.next_float() < impact.wear_chance + && let Some(mut state) = self.pack.assets.registry.state(block.state).cloned() + { + if falling_block::wear_anvil(&mut state) { + block.state = self + .pack + .assets + .registry + .resolve(&state) + .unwrap_or(block.state); + } else { + block.damage.cancel_drop = true; + } + } + } + if block.damage.cancel_drop { + if anvil { + self.level_events.push(LevelEvent::LevelEvent { + id: 1029, + pos: BlockPos::new(pos[0], pos[1], pos[2]), + data: 0, + }); + } + return; + } + let landing = self.with_level(|b, level, ticks| { + b.falling_block_landed( + level, + ticks, + BlockPos::new(pos[0], pos[1], pos[2]), + block.state, + false, + ) + }); + if matches!(landing, Landing::Broken) + && block.drop_item + && self.ui.rules.bool("entity_drops") + { + let item = self.res.facts.get(block.state).item.clone(); + if &*item != "minecraft:air" { + self.pop_resource( + pos, + ItemStack { + item, + count: 1, + damage: 0, + extra: None, + }, + ); + } + } + } + fn falling_hurt_player(&mut self, position: Vec3, amount: f32, kind: &str) { + if self.creative || self.spectator() { + return; + } + if self.player.as_ref().is_none_or(|p| { + p.health.dead_or_dying() || !falling_block::bounds(position).intersects(p.bounds()) + }) { + return; + } + let amount = if self.armor[0].is_some() { + amount * 0.75 + } else { + amount + }; + let hurt = if let Some(p) = self.player.as_mut() + && let Some(id) = self.motion.health.damage.id(kind) + { + p.hurt(&self.motion.health, self.world.health_rules, id, amount) + .unwrap_or(false) + } else { + false + }; + if hurt { + self.hurt_armor(amount); + } + } +} diff --git a/crates/horizon-client/src/game/hand.rs b/crates/horizon-client/src/game/hand.rs index 76d626716..431e6661a 100644 --- a/crates/horizon-client/src/game/hand.rs +++ b/crates/horizon-client/src/game/hand.rs @@ -30,6 +30,18 @@ fn attack_range_contains(range: &serde_json::Value, distance: f64, creative: boo } impl Game<'_> { + /// Opening a screen stops breaking before the next game tick (which may pause). + pub(super) fn stop_mining(&mut self) { + let mut destroying = std::mem::take(&mut self.clicks.destroying); + destroying.stop(&mut GameHands { + game: self, + error: None, + }); + self.clicks.destroying = destroying; + self.attack_clicks = 0; + self.stop_remote_mining(); + self.mining = None; + } /// `LivingEntity.swing` with `getModifiedSwingDuration`. pub(super) fn swing_hand(&mut self, hand: Hand, animation: SwingAnimation) { let effects = &self.potions.effects; diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rs index ac2121e2e..1ecdbff28 100644 --- a/crates/horizon-client/src/game/integrated.rs +++ b/crates/horizon-client/src/game/integrated.rs @@ -31,7 +31,7 @@ use std::{ path::Path, sync::{ Arc, - atomic::{AtomicBool, AtomicU16, Ordering}, + atomic::{AtomicBool, AtomicU16, AtomicU32, Ordering}, mpsc::{Receiver, Sender, channel, sync_channel}, }, thread::JoinHandle, @@ -47,6 +47,7 @@ struct Flags { /// `IntegratedServer.getPort`: the port it is open to the LAN on, 0 /// while it is not. published: AtomicU16, + smoothed_tick_ms: AtomicU32, } /// Work the window hands its server thread (`MinecraftServer.execute`). @@ -61,6 +62,9 @@ pub(super) struct Integrated { pub world_name: String, } impl Integrated { + pub fn smoothed_tick_ms(&self) -> f32 { + f32::from_bits(self.flags.smoothed_tick_ms.load(Ordering::Relaxed)) + } /// `IntegratedServer.tickServer` skips the world while the game is paused. pub fn set_paused(&self, paused: bool) { self.flags.paused.store(paused, Ordering::Relaxed); @@ -322,7 +326,13 @@ impl Game<'_> { // Only the connections run. self.tick_lan(); } else { + let started = Instant::now(); self.tick()?; + self.debug_charts.log_tick(started, started.elapsed()); + flags.smoothed_tick_ms.store( + self.debug_charts.smoothed_tick_ms.to_bits(), + Ordering::Relaxed, + ); } self.apply_publish(); let rate = self.clock.rate().max(1.0); diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index 25eb6eb96..dcc7bb23b 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -17,6 +17,17 @@ pub(super) const CONTROL_HELP: &[&str] = &[ ]; impl Game<'_> { + pub(super) fn save_title_options(&mut self) { + if let Some(title) = &self.title + && title.last_server != self.settings.file.string("lastServer") + { + self.settings.file.set( + "lastServer", + horizon_client::options_file::V::Str(title.last_server.clone()), + ); + self.settings.save(&Self::options_path()); + } + } /// `options-get` and `options-set`; None for other commands. pub(super) fn options_control(&mut self, words: &[&str], line: &str) -> Result> { match words[0] { diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index cae85bc6c..62d481b11 100644 --- a/crates/horizon-client/src/game/pad.rs +++ b/crates/horizon-client/src/game/pad.rs @@ -354,6 +354,7 @@ impl Game<'_> { } if let Some(title) = self.title.as_mut() { title.escape(); + self.save_title_options(); return Ok(()); } if self.sign_edit.is_some() { diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs index 12ba759b4..96d879f13 100644 --- a/crates/horizon-client/src/game/persist.rs +++ b/crates/horizon-client/src/game/persist.rs @@ -49,16 +49,7 @@ fn with_raw(modelled: Tag, raw: Option<&Tag>) -> Tag { // Defaults Horizon invents for fields it does not simulate give way to the save. let invented = matches!( k.to_string_lossy().as_str(), - "Fire" - | "Air" - | "Invulnerable" - | "PortalCooldown" - | "HasBeenShot" - // FallingBlockEntity fields Horizon does not simulate. - | "HurtEntities" - | "CancelDrop" - | "FallHurtAmount" - | "FallHurtMax" + "Fire" | "Air" | "Invulnerable" | "PortalCooldown" | "HasBeenShot" ); if !(invented && out.contains_key(k)) { out.insert(k.clone(), v.clone()); @@ -199,7 +190,7 @@ impl Game<'_> { } } "minecraft:falling_block" => { - if let Some((state, position, velocity, age)) = + if let Some((state, position, velocity, age, fall_distance)) = entity_nbt::falling_block_from_nbt(&tag) && let Some(state) = self.state_from_nbt(&state) { @@ -214,7 +205,11 @@ impl Game<'_> { age, no_gravity, drop_item, - fall_distance: 0.0, + fall_distance, + damage: horizon_client::falling_block::Damage::from_nbt( + &tag, + self.res.facts.get(state).name.ends_with("anvil"), + ), saved: uuid.map(|u| Box::new((u, tag.clone()))), }); } @@ -620,6 +615,8 @@ impl Game<'_> { f.velocity, f.age, f.drop_item, + f.damage, + f.fall_distance, uuid, ); out.entry(chunk_of(f.position)) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index c0829b43b..37be759dc 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -68,6 +68,7 @@ struct Lerp { pub(super) struct Remote { pub session: Session, + pub brand: String, builtin: Arc, lang: Arc, registries: Arc, @@ -153,7 +154,20 @@ fn number_format(f: &play::NumberFormat) -> horizon_client::scoreboard::NumberFo impl Game<'_> { /// Leave the title for a server (ConnectScreen): an empty world fed by the connection. pub(super) fn connect_server(&mut self, address: &str, name: &str) -> Result<()> { - self.join_server(address, name, None) + if let Err(error) = self.join_server(address, name, None) { + log::warn!("Horizon: could not join server: {error}"); + if self.title.is_none() { + self.title = Some(horizon_client::title::Title::new( + std::env::current_dir()?.join("saves"), + &self.options.resources, + )); + } + if let Some(title) = &mut self.title { + title.failed(error); + } + self.capture(false); + } + Ok(()) } /// Join single player's integrated server through memory. pub(super) fn connect_local( @@ -173,7 +187,7 @@ impl Game<'_> { let root = self.options.resources.clone(); let builtin = Builtin::find(&root) .and_then(|p| Builtin::load(&p).ok()) - .ok_or("registries.json not found next to the resources; run the data generator reports (horizon-prepare)")?; + .ok_or("Resources are incomplete: registries.json is missing or invalid. Click \"Prepare again\" in the launcher (or run horizon-prepare), then try again.")?; let lang = Arc::new(horizon_client::net::load_language(&root)); let view = self.settings.render_distance.clamp(2, 32) as i8; if local.is_none() { @@ -238,6 +252,7 @@ impl Game<'_> { self.apply_settings(); self.remote = Some(Remote { session, + brand: "unknown".into(), builtin: Arc::new(builtin), lang, registries: Arc::new(Dynamic::default()), @@ -330,6 +345,7 @@ impl Game<'_> { } let saves = std::env::current_dir()?.join("saves"); let mut title = horizon_client::title::Title::new(saves.clone(), &self.options.resources); + title.last_server = self.settings.file.string("lastServer"); if single_player && reason.is_none() { self.title = Some(title); self.capture(false); @@ -467,6 +483,11 @@ impl Game<'_> { r.status = format!("Joined as {name}; loading terrain..."); } } + Event::Brand(brand) => { + if let Some(remote) = &mut self.remote { + remote.brand = brand; + } + } Event::Reconfigured(registries) => self.set_registries(registries), Event::DecodeError(e) => { if let Some(r) = self.remote.as_mut() { @@ -2101,6 +2122,17 @@ impl Game<'_> { } } + pub(super) fn stop_remote_mining(&mut self) { + if let Some((pos, _, _)) = self.remote.as_mut().and_then(|r| r.destroying.take()) { + self.send(S::PlayerAction(play::PlayerAction { + action: 2, + pos, + direction: 0, + sequence: 0, + })); + } + } + /// MultiPlayerGameMode.startDestroyBlock / continueDestroyBlock. fn remote_mining(&mut self) { let Some(r) = self.remote.as_mut() else { @@ -2111,14 +2143,7 @@ impl Game<'_> { } let chatting = r.chat_input.is_some(); if !self.left || self.screen != Screen::Playing || chatting { - if let Some((pos, _, _)) = r.destroying.take() { - self.send(S::PlayerAction(play::PlayerAction { - action: 2, - pos, - direction: 0, - sequence: 0, - })); - } + self.stop_remote_mining(); self.mining = None; return; } diff --git a/crates/horizon-client/src/game/screens.rs b/crates/horizon-client/src/game/screens.rs index 0e0d63c23..54012ca03 100644 --- a/crates/horizon-client/src/game/screens.rs +++ b/crates/horizon-client/src/game/screens.rs @@ -146,9 +146,6 @@ impl Game<'_> { .find(|b| b.5 && ui::hovered(self.cursor, b.0, b.1, b.2, 20.0)) .map(|b| b.4) }; - if hit.is_some() && !horizon_client::ui_theme::horizon() { - self.audio.play_button_click(); - } match hit { Some(0) => { self.screen = Screen::Playing; @@ -168,6 +165,9 @@ impl Game<'_> { Some(2) => self.close_world()?, _ => {} } + if hit.is_some() && !horizon_client::ui_theme::horizon() { + self.audio.play_button_click(); + } Ok(()) } /// DeathScreen: gradient, the title at double size, the cause and score. @@ -260,9 +260,6 @@ impl Game<'_> { .into_iter() .find(|d| ui::hovered(self.cursor, d.0, d.1, 200.0, 20.0)) .map(|d| d.3); - if hit.is_some() { - self.audio.play_button_click(); - } match hit { Some(0) if self.remote.is_some() => { self.ui.death_text = None; @@ -292,6 +289,9 @@ impl Game<'_> { Some(1) => self.close_world()?, _ => {} } + if hit.is_some() { + self.audio.play_button_click(); + } Ok(()) } } diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index d6b4dd345..79a8ddd86 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -59,6 +59,7 @@ pub mod entity_nbt; pub mod environment; pub mod extras; pub mod extras_hud; +pub mod falling_block; pub mod fast_hash; pub mod fastutil_order; pub mod fire_render; @@ -147,7 +148,7 @@ pub mod screen_draw; pub mod screen_effects; pub mod screen_layout; pub mod sculk; -pub mod server; +pub use horizon_server_files as server; pub mod settings; pub mod shader_pack; pub mod shadows; diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 791da2a7f..8b51593bc 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -509,6 +509,7 @@ pub enum DamageKind { Fireball, Explosion, Fall, + FallingBlock, OnFire, InFire, Lava, @@ -1614,6 +1615,12 @@ impl Mob { return false; } let mut amount = amount; + if source.kind == DamageKind::FallingBlock + && !species::body_slot(self.species()) + && self.extra.head.is_some() + { + amount *= 0.75; + } let (worn, worn_toughness) = self.worn_armor(); if !source.bypasses_armor() && self.kind.armor + worn > 0.0 { // CombatRules.getDamageAfterAbsorb diff --git a/crates/horizon-client/src/net.rs b/crates/horizon-client/src/net.rs index d260498a0..f9ad5992a 100644 --- a/crates/horizon-client/src/net.rs +++ b/crates/horizon-client/src/net.rs @@ -29,6 +29,8 @@ use std::{ pub enum Event { /// Progress text for the connecting screen. Status(String), + /// The server's `minecraft:brand` custom payload. + Brand(String), /// Configuration finished: the play state begins with these registries. Joined { registries: Arc, @@ -374,7 +376,7 @@ fn run( .map_err(e)?; let (mut reader, writer) = c.split(); *shared_writer.lock().unwrap() = Some(writer); - let registries = configure(&mut reader, shared_writer, lang)?; + let registries = configure(&mut reader, shared_writer, lang, send)?; let _ = send.send(Event::Joined { registries: Arc::new(registries), uuid: profile.uuid, @@ -413,10 +415,13 @@ fn run( } play::Clientbound::StartConfiguration(_) => { reply(play::Serverbound::ConfigurationAcknowledged(Empty)); - let registries = configure(&mut reader, shared_writer, lang)?; + let registries = configure(&mut reader, shared_writer, lang, send)?; let _ = send.send(Event::Reconfigured(Arc::new(registries))); } play::Clientbound::Disconnect(d) => return Ok(plain(&d.reason, lang)), + play::Clientbound::CustomPayload(p) if p.channel == "minecraft:brand" => { + receive_brand(&p, send); + } other => { if send.send(Event::Packet(Box::new(other))).is_err() { return Ok("Disconnected".into()); @@ -432,6 +437,7 @@ fn configure( reader: &mut ConnReader, writer: &Mutex>, lang: &Language, + send: &Sender, ) -> Result { let mut registries = Dynamic::default(); let reply = |p: cfg::Serverbound| -> Result<(), String> { @@ -444,6 +450,7 @@ fn configure( let (id, payload) = reader.recv().map_err(|e| e.to_string())?; match cfg::Clientbound::decode(id, &payload).map_err(|e| e.to_string())? { cfg::Clientbound::KeepAlive(k) => reply(cfg::Serverbound::KeepAlive(k))?, + cfg::Clientbound::CustomPayload(p) => receive_brand(&p, send), cfg::Clientbound::Ping(p) => reply(cfg::Serverbound::Pong(p))?, // Know no packs: the server then sends every registry entry in full. cfg::Clientbound::SelectKnownPacks(_) => { @@ -477,6 +484,14 @@ fn configure( } } +fn receive_brand(payload: &cfg::CustomPayload, send: &Sender) { + if payload.channel == "minecraft:brand" + && let Ok(brand) = horizon_protocol::buf::Reader::new(&payload.data).string(32767) + { + let _ = send.send(Event::Brand(brand)); + } +} + /// The minecraft:brand payload: a string naming the client. fn brand() -> Vec { let mut w = horizon_protocol::buf::Writer::new(); @@ -565,3 +580,28 @@ pub fn ping(address: &str, timeout: Duration) -> Result<(serde_json::Value, u128 let _ = c.recv().map_err(e)?; Ok((json, start.elapsed().as_millis())) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn server_brand_payload_is_decoded_and_invalid_or_unrelated_payloads_are_ignored() { + let (send, events) = channel(); + let mut writer = horizon_protocol::buf::Writer::new(); + writer.string("vanilla"); + let mut payload = cfg::CustomPayload { + channel: "minecraft:brand".into(), + data: writer.data, + }; + receive_brand(&payload, &send); + assert!(matches!(events.try_recv(), Ok(Event::Brand(brand)) if brand == "vanilla")); + payload.channel = "example:other".into(); + receive_brand(&payload, &send); + assert!(events.try_recv().is_err()); + payload.channel = "minecraft:brand".into(); + payload.data = vec![0xff]; + receive_brand(&payload, &send); + assert!(events.try_recv().is_err()); + } +} diff --git a/crates/horizon-client/src/title.rs b/crates/horizon-client/src/title.rs index 2a9f0fdf9..172fb821a 100644 --- a/crates/horizon-client/src/title.rs +++ b/crates/horizon-client/src/title.rs @@ -125,6 +125,8 @@ pub enum Action { } pub struct Title { pub mode: Mode, + /// Direct Connection's address, synchronized with `options.txt`'s `lastServer`. + pub last_server: String, pub saves: PathBuf, pub started: std::time::Instant, search: String, @@ -232,6 +234,7 @@ impl Title { Self { shown: std::cell::Cell::new((std::mem::discriminant(&mode), now)), mode, + last_server: String::new(), saves, started: now, search: String::new(), @@ -397,7 +400,7 @@ impl Title { scroll.wheel(delta, ROW_HEIGHT / 2.0, top, bottom, content); self.scroll.set(scroll); } - fn failed(&mut self, error: impl std::fmt::Display) { + pub fn failed(&mut self, error: impl std::fmt::Display) { let return_to = Box::new(self.mode.clone()); self.mode = Mode::Error { message: error.to_string(), @@ -1162,6 +1165,7 @@ impl Title { /// The action and whether to play `AbstractWidget.playButtonClickSound`. /// Navigation buttons can return `Action::None` while still sounding. pub fn click(&mut self, x: f32, y: f32, size: (f32, f32)) -> (Action, bool) { + self.remember_direct_address(); let (w, h) = (size.0.ceil(), size.1.ceil()); // MenuTabBar: a new tab clears the focus and clicks. if let Mode::Create { focus, options, .. } = &mut self.mode @@ -1195,16 +1199,14 @@ impl Title { self.mode = Mode::Multiplayer(crate::multiplayer::Screen::list(&self.saves)); Action::None } - (Mode::Multiplayer(screen), hit) => { - match screen.click(hit, x, y, (w, h), &self.saves) { - crate::multiplayer::Outcome::Back => { - self.mode = Mode::Main; - Action::None - } - crate::multiplayer::Outcome::Connect(address) => Action::Connect { address }, - crate::multiplayer::Outcome::None => Action::None, + (Mode::Multiplayer(_), hit) => match self.multiplayer_click(hit, x, y, (w, h)) { + crate::multiplayer::Outcome::Back => { + self.mode = Mode::Main; + Action::None } - } + crate::multiplayer::Outcome::Connect(address) => Action::Connect { address }, + crate::multiplayer::Outcome::None => Action::None, + }, (Mode::Main, Some(3)) => Action::Options, (Mode::Main, Some(7)) => Action::Accessibility, (Mode::Main, Some(6)) => Action::Language, @@ -1512,7 +1514,31 @@ impl Title { _ => None, } } + pub(crate) fn remember_direct_address(&mut self) { + if let Mode::Multiplayer(crate::multiplayer::Screen::Direct { address }) = &self.mode { + self.last_server.clone_from(address); + } + } + pub(crate) fn multiplayer_click( + &mut self, + hit: Option, + x: f32, + y: f32, + size: (f32, f32), + ) -> crate::multiplayer::Outcome { + let Mode::Multiplayer(screen) = &mut self.mode else { + return crate::multiplayer::Outcome::None; + }; + let outcome = screen.click(hit, x, y, size, &self.saves); + if hit == Some(41) + && let crate::multiplayer::Screen::Direct { address } = screen + { + address.clone_from(&self.last_server); + } + outcome + } pub fn escape(&mut self) { + self.remember_direct_address(); if let Mode::Multiplayer(screen) = &mut self.mode && !screen.escape(&self.saves) { diff --git a/crates/horizon-client/src/title/tests.rs b/crates/horizon-client/src/title/tests.rs index 4f759f960..572be2ef2 100644 --- a/crates/horizon-client/src/title/tests.rs +++ b/crates/horizon-client/src/title/tests.rs @@ -82,6 +82,41 @@ impl Drop for Fixture { } } +#[test] +fn direct_connection_remembers_the_address_on_cancel_escape_and_join() { + let mut f = Fixture::new(); + f.title.last_server = "localhost:25566".into(); + f.click("Multiplayer"); + f.click("Direct Connection"); + assert_eq!(f.title.focused_field().unwrap().1, "localhost:25566"); + + *f.title.focused_field().unwrap().1 = "example.org:25567".into(); + f.click("Cancel"); + f.click("Direct Connection"); + assert_eq!(f.title.focused_field().unwrap().1, "example.org:25567"); + + *f.title.focused_field().unwrap().1 = "[::1]:25568".into(); + f.title.escape(); + f.title.escape(); + f.click("Multiplayer"); + f.click("Direct Connection"); + assert_eq!(f.title.focused_field().unwrap().1, "[::1]:25568"); + + *f.title.focused_field().unwrap().1 = "localhost:25569".into(); + assert_eq!( + f.click("Join Server"), + Action::Connect { + address: "localhost:25569".into(), + } + ); + assert_eq!(f.title.last_server, "localhost:25569"); + + f.title.focused_field().unwrap().1.clear(); + f.title.escape(); + f.click("Direct Connection"); + assert!(f.title.focused_field().unwrap().1.is_empty()); +} + #[test] fn empty_singleplayer_opens_creation_and_cancel_returns_to_title() { let mut f = Fixture::new(); diff --git a/crates/horizon-prepare/src/lib.rs b/crates/horizon-prepare/src/lib.rs index f997be326..fb9df356f 100644 --- a/crates/horizon-prepare/src/lib.rs +++ b/crates/horizon-prepare/src/lib.rs @@ -762,8 +762,7 @@ fn steps( "behaviour-export.log", )?; - let items = root.join("items.json"); - if !items.exists() || !run.same("server_sha256") { + if datagen_reports_missing(root) || !run.same("server_sha256") { // Vanilla's own data generator reports every item's default components. (run.log)("Running vanilla's data generator reports"); let datagen = run.local.join("datagen"); @@ -785,21 +784,7 @@ fn steps( ) .map_err(|e| format!("{e} in the data generator; see {}", log_path.display()))?; let reports = datagen.join("out").join("reports"); - let mut components = Value::Object(Vec::new()); - for file in json_files(&reports.join("minecraft/components/item"))? { - let stem = file.file_stem().unwrap().to_string_lossy().into_owned(); - let report = read_json(&file)?; - let component = report - .get("components") - .ok_or_else(|| format!("{} has no components", file.display()))?; - components.insert(&format!("minecraft:{stem}"), component.clone()); - } - write_text(&items, &pyjson::dumps(&components, &pyjson::DEFAULT))?; - let biome_parameters = root.join("biome-parameters"); - mkdir(&biome_parameters)?; - for file in json_files(&reports.join("biome_parameters/minecraft"))? { - copy(&file, &biome_parameters.join(file.file_name().unwrap()))?; - } + install_datagen_reports(root, &reports)?; } // Biome parameter tables come from the data generator reports above. @@ -925,6 +910,37 @@ fn steps( ) } +fn datagen_reports_missing(root: &Path) -> bool { + ["items.json", "registries.json", "commands.json"] + .iter() + .any(|name| !root.join(name).is_file()) +} + +fn install_datagen_reports(root: &Path, reports: &Path) -> Result<(), String> { + for name in ["registries.json", "commands.json"] { + copy(&reports.join(name), &root.join(name))?; + } + let mut components = Value::Object(Vec::new()); + for file in json_files(&reports.join("minecraft/components/item"))? { + let stem = file.file_stem().unwrap().to_string_lossy().into_owned(); + let report = read_json(&file)?; + let component = report + .get("components") + .ok_or_else(|| format!("{} has no components", file.display()))?; + components.insert(&format!("minecraft:{stem}"), component.clone()); + } + write_text( + &root.join("items.json"), + &pyjson::dumps(&components, &pyjson::DEFAULT), + )?; + let biome_parameters = root.join("biome-parameters"); + mkdir(&biome_parameters)?; + for file in json_files(&reports.join("biome_parameters/minecraft"))? { + copy(&file, &biome_parameters.join(file.file_name().unwrap()))?; + } + Ok(()) +} + /// `sorted(directory.glob('*.json'))`: glob skips names starting with a dot. fn json_files(directory: &Path) -> Result, String> { let mut files: Vec = fs::read_dir(directory) @@ -1124,6 +1140,54 @@ fn export_block_behavior(run: &Run, export: &Path) -> Result<(), String> { mod tests { use super::*; + #[test] + fn datagen_reports_are_installed_beside_resources_and_missing_reports_need_repair() { + let dir = + std::env::temp_dir().join(format!("horizon-prepare-reports-{}", std::process::id())); + let root = dir.join("resources"); + let reports = dir.join("reports"); + mkdir(&root).unwrap(); + mkdir(&reports.join("minecraft/components/item")).unwrap(); + mkdir(&reports.join("biome_parameters/minecraft")).unwrap(); + write_text(&root.join("items.json"), "{}").unwrap(); + assert!(datagen_reports_missing(&root)); + write_text( + &reports.join("registries.json"), + "{\"minecraft:item\":{\"entries\":{}}}", + ) + .unwrap(); + write_text(&reports.join("commands.json"), "{\"type\":\"root\"}").unwrap(); + write_text( + &reports.join("minecraft/components/item/stone.json"), + "{\"components\":{\"minecraft:max_stack_size\":64}}", + ) + .unwrap(); + write_text( + &reports.join("biome_parameters/minecraft/overworld.json"), + "{}", + ) + .unwrap(); + install_datagen_reports(&root, &reports).unwrap(); + assert!(!datagen_reports_missing(&root)); + for name in ["registries.json", "commands.json"] { + assert_eq!( + read(&root.join(name)).unwrap(), + read(&reports.join(name)).unwrap() + ); + } + assert!(read_json(&root.join("items.json")) + .unwrap() + .get("minecraft:stone") + .is_some()); + assert!(root.join("biome-parameters/overworld.json").is_file()); + for name in ["registries.json", "commands.json", "items.json"] { + fs::remove_file(root.join(name)).unwrap(); + assert!(datagen_reports_missing(&root), "{name}"); + install_datagen_reports(&root, &reports).unwrap(); + } + fs::remove_dir_all(dir).unwrap(); + } + #[test] fn the_source_digest_is_the_one_build_rs_computed() { assert_eq!( diff --git a/crates/horizon-protocol/src/connection.rs b/crates/horizon-protocol/src/connection.rs index 687839416..4deda020c 100644 --- a/crates/horizon-protocol/src/connection.rs +++ b/crates/horizon-protocol/src/connection.rs @@ -359,6 +359,8 @@ mod tests { big.extend(std::iter::repeat_n(3u8, 1000)); c.send(&big).unwrap(); assert_eq!(c.recv().unwrap(), (9, vec![1, 2])); + assert_eq!(c.reader.counters.sent.load(Ordering::Relaxed), 1); + assert_eq!(c.reader.counters.received.load(Ordering::Relaxed), 1); server.join().unwrap(); assert!(c.recv().is_err()); } diff --git a/crates/horizon-server-files/Cargo.toml b/crates/horizon-server-files/Cargo.toml new file mode 100644 index 000000000..52114718d --- /dev/null +++ b/crates/horizon-server-files/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "horizon-server-files" +version = "0.1.0" +edition.workspace = true +license.workspace = true +rust-version = "1.90" +description = "The dedicated server's vanilla files: server.properties, eula.txt, ops, whitelist and bans" + +[dependencies] +horizon-core = { path = "../horizon-core" } +horizon-protocol = { path = "../horizon-protocol" } +horizon-world = { path = "../horizon-world" } +log = "0.4" + +[lints] +workspace = true diff --git a/crates/horizon-client/src/server/eula.rs b/crates/horizon-server-files/src/eula.rs similarity index 86% rename from crates/horizon-client/src/server/eula.rs rename to crates/horizon-server-files/src/eula.rs index b5896cfd7..769af9401 100644 --- a/crates/horizon-client/src/server/eula.rs +++ b/crates/horizon-server-files/src/eula.rs @@ -9,8 +9,13 @@ pub const EULA_LINK: &str = "https://aka.ms/MinecraftEULA"; /// The default file, as Properties.store(OutputStream) writes it. pub fn default_file(millis: i64) -> String { + file(millis, false) +} + +/// The file with `eula` set: what the owner's agreement writes (horizon-server init). +pub fn file(millis: i64, agreed: bool) -> String { let mut p = Properties::default(); - p.put("eula", "false".into()); + p.put("eula", agreed.to_string()); p.store( Some(&format!( "By changing the setting below to TRUE you are indicating your agreement to our EULA ({EULA_LINK})." diff --git a/crates/horizon-client/src/server/java_properties.rs b/crates/horizon-server-files/src/java_properties.rs similarity index 100% rename from crates/horizon-client/src/server/java_properties.rs rename to crates/horizon-server-files/src/java_properties.rs diff --git a/crates/horizon-client/src/server/json.rs b/crates/horizon-server-files/src/json.rs similarity index 100% rename from crates/horizon-client/src/server/json.rs rename to crates/horizon-server-files/src/json.rs diff --git a/crates/horizon-client/src/server/mod.rs b/crates/horizon-server-files/src/lib.rs similarity index 50% rename from crates/horizon-client/src/server/mod.rs rename to crates/horizon-server-files/src/lib.rs index 1bb0d80c1..30c75c869 100644 --- a/crates/horizon-client/src/server/mod.rs +++ b/crates/horizon-server-files/src/lib.rs @@ -1,8 +1,8 @@ //! The dedicated server's files and settings, ported from //! net.minecraft.server.dedicated and net.minecraft.server.players: -//! server.properties, eula.txt and the four user lists. The server itself -//! runs the game's own world, simulation and LAN hub headless -//! (`horizon-client --dedicated`, see the game's `dedicated` module). +//! server.properties, eula.txt and the four user lists. The game's dedicated +//! server (`horizon-client --dedicated`) reads them, and `horizon-server` +//! manages them; this crate needs neither a window nor the game. pub mod eula; pub mod java_properties; pub mod json; diff --git a/crates/horizon-client/src/server/properties.rs b/crates/horizon-server-files/src/properties.rs similarity index 100% rename from crates/horizon-client/src/server/properties.rs rename to crates/horizon-server-files/src/properties.rs diff --git a/crates/horizon-client/src/server/user_lists.rs b/crates/horizon-server-files/src/user_lists.rs similarity index 100% rename from crates/horizon-client/src/server/user_lists.rs rename to crates/horizon-server-files/src/user_lists.rs diff --git a/crates/horizon-server/Cargo.toml b/crates/horizon-server/Cargo.toml new file mode 100644 index 000000000..c3800b80c --- /dev/null +++ b/crates/horizon-server/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "horizon-server" +version = "0.1.0" +edition.workspace = true +license.workspace = true +rust-version = "1.90" +description = "Host a Horizon server: server.toml, the CLI and the supervisor that runs the game headless (docs/server.md)" + +[dependencies] +horizon-server-files = { path = "../horizon-server-files" } +# The game process's RAM and CPU (the same probes the benchmark reports use). +horizon-bench = { path = "../horizon-bench" } +serde = { version = "1", features = ["derive"] } +serde_json = "1" +toml = "1.1" +# Edits server.toml in place, keeping the owner's comments and order. +toml_edit = "0.25" +# Backups: one zip anyone can open, Windows included. +zip = { version = "2", default-features = false, features = ["deflate"] } + +[target.'cfg(unix)'.dependencies] +# SIGINT/SIGTERM/SIGHUP: stop the game cleanly when the terminal or the service manager asks. +libc = "0.2" + +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_System_Console", "Win32_System_Threading"] } + +[lints] +workspace = true diff --git a/crates/horizon-server/panel/index.html b/crates/horizon-server/panel/index.html new file mode 100644 index 000000000..051b7283e --- /dev/null +++ b/crates/horizon-server/panel/index.html @@ -0,0 +1,78 @@ + + + + + + +Horizon server + + + + +
+
+

Horizon server

+ … + +
+
+ + +
+
+ +
+
+
TPS –
+
MSPT –
+
RAM –
+
CPU –
+
+ +
+
+

Players

+ + + +
NamePing
+

Nobody is online.

+
+ +
+

Backups

+
+ + +
+ + + +
BackupSize
+

No backups yet.

+
+
+ +
+

Console

+

+    
+ + +
+
+
+ +
+

Sign in with a link from the server: run horizon-server panel in the server's folder and open + the link it prints. Links work once, for 10 minutes.

+
+ +
+

The server isn't answering. It may have stopped: start it again with horizon-server start, + then reload this page.

+
+ + + + diff --git a/crates/horizon-server/panel/panel.css b/crates/horizon-server/panel/panel.css new file mode 100644 index 000000000..5d635d509 --- /dev/null +++ b/crates/horizon-server/panel/panel.css @@ -0,0 +1,128 @@ +/* The Horizon server panel. Colours and spacing are horizon-theme's tokens (website/src/tokens.css). */ +:root { + --page: #fafafc; + --plate-soft: #f3f3f8; + --plate: #ebecf4; + --surface: #ffffff; + --rule: #e2e3ea; + --ink: #16181f; + --ink-soft: #3b3e49; + --muted: #646875; + --accent: #5b3fd6; + --on-accent: #ffffff; + --danger: #b4372a; + --success: #1d7a48; + --font: "Geist", ui-sans-serif, system-ui, sans-serif; + --mono: "Geist Mono", ui-monospace, monospace; + --radius: 8px; + --radius-plate: 12px; + color-scheme: light; +} +@media (prefers-color-scheme: dark) { + :root { + --page: #0d0e13; + --plate-soft: #181a22; + --plate: #20222c; + --surface: #16181f; + --rule: #24262f; + --ink: #ecedf3; + --ink-soft: #c5c7d1; + --muted: #8b8f9c; + --accent: #9a86ff; + --on-accent: #0d0e13; + --danger: #f07a68; + --success: #45c27a; + color-scheme: dark; + } +} + +* { box-sizing: border-box; } +[hidden] { display: none !important; } +body { + margin: 0; + background: var(--page); + color: var(--ink); + font: 14px/1.5 var(--font); +} +header { + display: flex; + flex-wrap: wrap; + gap: 12px; + align-items: center; + justify-content: space-between; + padding: 16px 24px; + border-bottom: 1px solid var(--rule); + background: var(--surface); +} +.title { display: flex; flex-wrap: wrap; gap: 12px; align-items: baseline; } +h1 { font-size: 18px; font-weight: 500; margin: 0; letter-spacing: -0.01em; } +h2 { font-size: 13px; font-weight: 500; margin: 0 0 8px; color: var(--ink-soft); } +main { padding: 24px; max-width: 1200px; margin: 0 auto; display: grid; gap: 24px; } +section { background: var(--surface); border: 1px solid var(--rule); border-radius: var(--radius-plate); padding: 16px; } +.muted { color: var(--muted); } +.pill { + font-size: 12px; + padding: 2px 10px; + border-radius: 999px; + background: var(--plate); + color: var(--ink-soft); +} +.pill.running { background: color-mix(in srgb, var(--success) 18%, transparent); color: var(--success); } +.pill.stopping, .pill.restarting, .pill.starting { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); } +.pill.down { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); } + +.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; } +figure { margin: 0; } +figcaption { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; } +figcaption b { color: var(--ink); font-weight: 500; font-variant-numeric: tabular-nums; } +canvas { width: 100%; height: 96px; display: block; } + +.columns { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 24px; } +table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; } +th { text-align: left; font-weight: 400; font-size: 12px; color: var(--muted); padding: 4px 0; } +td { padding: 6px 0; border-top: 1px solid var(--rule); } +td:last-child { text-align: right; white-space: nowrap; } +td button { margin-left: 4px; } + +button, input { + font: inherit; + height: 32px; + border-radius: var(--radius); + border: 1px solid var(--rule); + background: var(--plate-soft); + color: var(--ink); + padding: 0 12px; +} +button { cursor: pointer; } +button:hover { background: var(--plate); } +button:disabled { opacity: 0.5; cursor: default; } +button[type="submit"] { background: var(--accent); border-color: var(--accent); color: var(--on-accent); } +button.danger { color: var(--danger); } +input { background: var(--surface); flex: 1; min-width: 0; } +input:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; } +.row { display: flex; gap: 8px; margin: 8px 0; } + +pre { + font: 12px/1.5 var(--mono); + background: var(--plate-soft); + border-radius: var(--radius); + padding: 12px; + margin: 0; + height: 320px; + overflow: auto; + white-space: pre-wrap; + word-break: break-word; +} +.message { max-width: 560px; } +code { font-family: var(--mono); font-size: 13px; } +#toast { + position: fixed; + bottom: 24px; + left: 50%; + transform: translateX(-50%); + background: var(--ink); + color: var(--page); + padding: 8px 16px; + border-radius: var(--radius); + max-width: 90vw; +} diff --git a/crates/horizon-server/panel/panel.js b/crates/horizon-server/panel/panel.js new file mode 100644 index 000000000..f74c3e1c2 --- /dev/null +++ b/crates/horizon-server/panel/panel.js @@ -0,0 +1,270 @@ +// The Horizon server panel (docs/server.md, section 6). It polls the control API once a second through +// the panel's session; every change is a POST with the X-Horizon header. Text from the server is only +// ever set with textContent. +"use strict"; + +const $ = (id) => document.getElementById(id); +let logNext = 0; +let metricsSince = 0; +const samples = []; +let lastStatus = null; +let backupsShown = ""; + +class SignedOut extends Error {} + +async function api(method, path, body) { + let response; + try { + response = await fetch(path, { + method, + credentials: "same-origin", + headers: { "X-Horizon": "1", "Content-Type": "application/json" }, + body: body === undefined ? undefined : JSON.stringify(body), + }); + } catch (e) { + throw new Error("the server isn't answering"); + } + if (response.status === 401) throw new SignedOut(); + const value = await response.json().catch(() => ({})); + if (!response.ok) throw new Error(value.error || `error ${response.status}`); + return value; +} + +function show(view) { + for (const id of ["app", "signed-out", "gone"]) $(id).hidden = id !== view; + for (const id of ["restart", "stop"]) $(id).disabled = view !== "app"; +} + +let toastTimer; +function toast(text) { + $("toast").textContent = text; + $("toast").hidden = false; + clearTimeout(toastTimer); + toastTimer = setTimeout(() => ($("toast").hidden = true), 5000); +} + +function duration(seconds) { + if (seconds == null) return ""; + const d = Math.floor(seconds / 86400), h = Math.floor(seconds / 3600) % 24, m = Math.floor(seconds / 60) % 60; + if (d) return `${d} d ${h} h`; + if (h) return `${h} h ${m} min`; + return m ? `${m} min` : `${seconds} s`; +} + +function bytes(n) { + if (n == null) return "–"; + if (n >= 1e9) return `${(n / 1e9).toFixed(2)} GB`; + if (n >= 1e6) return `${(n / 1e6).toFixed(1)} MB`; + return `${Math.ceil(n / 1e3)} kB`; +} + +function cell(text) { + const td = document.createElement("td"); + td.textContent = text; + return td; +} + +function button(label, action, danger) { + const b = document.createElement("button"); + b.type = "button"; + b.textContent = label; + if (danger) b.className = "danger"; + b.addEventListener("click", action); + return b; +} + +async function run(line) { + const reply = await api("POST", "/api/v1/command", { line }); + return (reply.output || []).join("\n"); +} + +function renderStatus(status) { + lastStatus = status; + const phase = $("phase"); + phase.textContent = status.phase; + phase.className = `pill ${status.phase}`; + const game = status.sample && status.sample.game; + const parts = [`up ${duration(status.engine_uptime_seconds)}`, `horizon-server ${status.version}`]; + if (status.crashes) parts.push(`${status.crashes} crash${status.crashes > 1 ? "es" : ""}`); + $("summary").textContent = parts.join(" · "); + + const players = (game && game.players) || []; + $("player-count").textContent = game ? `${players.length} of ${game.max_players}` : ""; + $("no-players").hidden = players.length > 0; + const body = $("players"); + body.replaceChildren( + ...players.map((p) => { + const tr = document.createElement("tr"); + const actions = cell(""); + actions.append( + button("Op", async () => toast(await run(`op ${p.name}`).catch((e) => e.message))), + button("Kick", async () => { + if (confirm(`Kick ${p.name}?`)) toast(await run(`kick ${p.name}`).catch((e) => e.message)); + }, true), + ); + tr.append(cell(p.name), cell(p.ping_ms == null ? "–" : `${p.ping_ms} ms`), actions); + return tr; + }), + ); +} + +function renderBackups(list) { + const key = JSON.stringify(list); + if (key === backupsShown) return; + backupsShown = key; + $("no-backups").hidden = list.length > 0; + $("backups").replaceChildren( + ...list.map((b) => { + const tr = document.createElement("tr"); + const actions = cell(""); + actions.append( + button("Restore", async () => { + if (!confirm(`Restore ${b.name}? The server restarts with that world; the current one is kept in backups/.`)) return; + try { + await api("POST", "/api/v1/restore", { name: b.name }); + toast(`Restoring ${b.name}: the server is restarting.`); + } catch (e) { + toast(e.message); + } + }, true), + ); + tr.append(cell(b.name), cell(bytes(b.bytes)), actions); + return tr; + }), + ); +} + +function appendLog(lines) { + if (!lines.length) return; + const log = $("log"); + const atBottom = log.scrollTop + log.clientHeight >= log.scrollHeight - 8; + log.append(document.createTextNode(lines.join("\n") + "\n")); + // Keep the page light: the last 2000 lines, as the server does. + const text = log.textContent; + const all = text.split("\n"); + if (all.length > 2100) log.textContent = all.slice(-2000).join("\n"); + if (atBottom) log.scrollTop = log.scrollHeight; +} + +function chart(id, values, { max, unit, line, decimals = 1 }) { + const canvas = $(id); + const ratio = window.devicePixelRatio || 1; + const width = canvas.clientWidth, height = canvas.clientHeight; + if (canvas.width !== width * ratio) { + canvas.width = width * ratio; + canvas.height = height * ratio; + } + const g = canvas.getContext("2d"); + g.setTransform(ratio, 0, 0, ratio, 0, 0); + g.clearRect(0, 0, width, height); + const style = getComputedStyle(document.documentElement); + const known = values.filter((v) => v != null); + const top = Math.max(max || 0, ...known, 1e-9) * 1.1; + if (line != null) { + g.strokeStyle = style.getPropertyValue("--rule"); + g.setLineDash([4, 4]); + g.beginPath(); + const y = height - (line / top) * height; + g.moveTo(0, y); + g.lineTo(width, y); + g.stroke(); + g.setLineDash([]); + } + g.strokeStyle = style.getPropertyValue("--accent"); + g.lineWidth = 1.5; + g.beginPath(); + const span = 600; + let drawing = false; + values.forEach((v, i) => { + if (v == null) { + drawing = false; + return; + } + const x = width - ((values.length - 1 - i) / (span - 1)) * width; + const y = height - (v / top) * height; + if (drawing) g.lineTo(x, y); + else g.moveTo(x, y); + drawing = true; + }); + g.stroke(); + const last = values[values.length - 1]; + $(`${id}-now`).textContent = last == null ? "–" : `${last.toFixed(decimals)}${unit}`; +} + +function renderCharts() { + chart("tps", samples.map((s) => s.tps), { max: 20, unit: "", line: 20 }); + chart("mspt", samples.map((s) => s.mspt), { max: 50, unit: " ms", line: 50 }); + chart("ram", samples.map((s) => (s.rss_bytes == null ? null : s.rss_bytes / 1e9)), { unit: " GB", decimals: 2 }); + chart("cpu", samples.map((s) => s.cpu_percent), { max: 100, unit: " %", decimals: 0 }); + $("ram-now").textContent = bytes(samples.length ? samples[samples.length - 1].rss_bytes : null); +} + +let ticks = 0; +async function poll() { + try { + const [status, metrics, logs] = await Promise.all([ + api("GET", "/api/v1/status"), + api("GET", `/api/v1/metrics?since=${metricsSince}`), + api("GET", `/api/v1/logs?since=${logNext}`), + ]); + show("app"); + renderStatus(status); + for (const s of metrics.samples) { + samples.push(s); + metricsSince = s.t; + } + samples.splice(0, Math.max(0, samples.length - 600)); + renderCharts(); + logNext = logs.next; + appendLog(logs.lines); + if (ticks++ % 10 === 0) renderBackups((await api("GET", "/api/v1/backups")).backups); + } catch (e) { + if (e instanceof SignedOut) show("signed-out"); + else if (lastStatus) { + show("gone"); + $("phase").textContent = "down"; + $("phase").className = "pill down"; + } else show("gone"); + } +} + +function setUp() { + $("command-form").addEventListener("submit", async (event) => { + event.preventDefault(); + const line = $("command").value.trim(); + if (!line) return; + $("command").value = ""; + try { + const output = await run(line); + appendLog([`> ${line}`]); + if (!output) toast("Done."); + } catch (e) { + toast(e.message); + } + }); + $("backup-form").addEventListener("submit", async (event) => { + event.preventDefault(); + const name = $("backup-name").value.trim(); + toast("Backing up…"); + try { + const made = await api("POST", "/api/v1/backups", name ? { name } : {}); + toast(`Backed up to backups/${made.name}`); + $("backup-name").value = ""; + renderBackups((await api("GET", "/api/v1/backups")).backups); + } catch (e) { + toast(e.message); + } + }); + $("restart").addEventListener("click", async () => { + if (!confirm("Restart the game? Players are disconnected for a moment.")) return; + await api("POST", "/api/v1/restart").then(() => toast("Restarting…"), (e) => toast(e.message)); + }); + $("stop").addEventListener("click", async () => { + if (!confirm("Stop the server? The panel stops with it; start it again on the server.")) return; + await api("POST", "/api/v1/stop").then(() => toast("Stopping…"), (e) => toast(e.message)); + }); + poll(); + setInterval(poll, 1000); +} + +document.addEventListener("DOMContentLoaded", setUp); diff --git a/crates/horizon-server/src/api.rs b/crates/horizon-server/src/api.rs new file mode 100644 index 000000000..85bc0e2b5 --- /dev/null +++ b/crates/horizon-server/src/api.rs @@ -0,0 +1,497 @@ +//! The control API (docs/server.md, section 8): HTTP and JSON on 127.0.0.1, with a random token in +//! `.horizon/api.json` that only the server's own user can read. The CLI manages a running server through +//! it; the web panel and the MCP server will too. +//! +//! - `GET /api/v1/status`: the supervisor's state, the game's status (TPS, MSPT, players), RAM and CPU. +//! - `GET /api/v1/logs?since=N`: the log lines after the Nth. +//! - `POST /api/v1/command {"line": "..."}`: a console command; the lines it logged. +//! - `POST /api/v1/stop`, `POST /api/v1/restart`. +//! - `GET /api/v1/metrics?since=T`: the samples taken after T (seconds since 1970), up to 10 minutes. +//! - `GET /api/v1/backups`; `POST /api/v1/backups {"name": "..."}` backs up now; `POST /api/v1/restore +//! {"name": "..."}` restarts the game with that backup's world. +//! - `POST /api/v1/panel/login`: a one-time login link for the web panel (with the token only). + +use crate::{ + backup, + config::Config, + engine, + http::{self, Request, Response}, + panel, +}; +use serde_json::{Value, json}; +use std::{ + collections::VecDeque, + io::Write, + net::{SocketAddr, TcpListener}, + path::{Path, PathBuf}, + sync::{ + Arc, Mutex, + atomic::{AtomicBool, Ordering}, + }, + time::{Duration, Instant}, +}; + +const ENDPOINT: &str = ".horizon/api.json"; +/// The log lines kept for `logs` and the panel's console. +const LOG_LINES: usize = 2000; +/// The samples kept for the panel's graphs: 10 minutes, one a second. +const METRICS: usize = 600; + +/// What the supervisor is doing. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Phase { + Starting, + Running, + Stopping, + /// Stopping the game to start it again (`restart`), or waiting to after a crash. + Restarting, +} + +impl Phase { + fn name(self) -> &'static str { + match self { + Self::Starting => "starting", + Self::Running => "running", + Self::Stopping => "stopping", + Self::Restarting => "restarting", + } + } +} + +pub struct State { + pub phase: Phase, + pub engine_pid: Option, + pub engine_started: Option, + pub crashes: u32, + pub last_crash: Option, + /// The latest sample (`sample`): the game's status, RAM and CPU. + pub sample: Value, +} + +/// The log, numbered from the first line since the supervisor started. +#[derive(Default)] +pub struct Log { + lines: VecDeque, + first: u64, +} + +impl Log { + pub fn push(&mut self, line: String) { + if self.lines.len() == LOG_LINES { + self.lines.pop_front(); + self.first += 1; + } + self.lines.push_back(line); + } + /// The lines after the `since`th, and the number to ask from next. + pub fn since(&self, since: u64) -> (u64, Vec) { + let skip = since.saturating_sub(self.first) as usize; + let next = self.first + self.lines.len() as u64; + (next, self.lines.iter().skip(skip).cloned().collect()) + } +} + +/// What the supervisor and the API threads share. +pub struct Shared { + pub dir: PathBuf, + pub state: Mutex, + pub log: Mutex, + pub stop: AtomicBool, + pub restart: AtomicBool, + /// The backup to put in place before the game starts again (`restore`). + pub restore: Mutex>, + /// Held while a backup is written: one at a time. + pub backing_up: Mutex<()>, + pub started: Instant, + /// The last samples, oldest first, for the panel's graphs. + pub metrics: Mutex>, + /// The panel's address, login links and sessions. + pub panel: Mutex, +} + +impl Shared { + pub fn new(dir: &Path) -> Arc { + Arc::new(Self { + dir: dir.to_owned(), + state: Mutex::new(State { + phase: Phase::Starting, + engine_pid: None, + engine_started: None, + crashes: 0, + last_crash: None, + sample: Value::Null, + }), + log: Mutex::new(Log::default()), + stop: AtomicBool::new(false), + restart: AtomicBool::new(false), + restore: Mutex::new(None), + backing_up: Mutex::new(()), + started: Instant::now(), + metrics: Mutex::new(VecDeque::new()), + panel: Mutex::new(panel::Auth::default()), + }) + } + /// Keeps one sample for the graphs. + pub fn record(&self, sample: Value) { + let mut metrics = self.metrics.lock().unwrap(); + if metrics.len() == METRICS { + metrics.pop_front(); + } + metrics.push_back(sample); + } + pub fn set_phase(&self, phase: Phase) { + self.state.lock().unwrap().phase = phase; + } + fn phase(&self) -> Phase { + self.state.lock().unwrap().phase + } + + /// A backup while the server runs: the game saves everything and stops writing while the zip is made. + pub fn back_up(&self, file_name: &str) -> Result { + let _one = self.backing_up.lock().unwrap(); + let config = Config::load(&self.dir)?; + let mut game = match self.phase() { + Phase::Running => Some( + engine::Control::connect(&self.dir) + .map_err(|e| format!("the game didn't answer: {e}"))?, + ), + // Between a crash and the restart nothing writes the world. + Phase::Restarting => None, + Phase::Starting | Phase::Stopping => { + return Err("the game is starting or stopping: try again in a moment".into()); + } + }; + if let Some(game) = game.as_mut() { + for command in ["console save-off", "console save-all flush"] { + game.ask(command) + .map_err(|e| format!("the game didn't save: {e}"))?; + } + } + let result = backup::create(&self.dir, &config, file_name); + if let Some(game) = game.as_mut() { + let _ = game.ask("console save-on"); + } + result + } +} + +fn status(shared: &Shared) -> Value { + let state = shared.state.lock().unwrap(); + json!({ + "phase": state.phase.name(), + "version": env!("CARGO_PKG_VERSION"), + "pid": std::process::id(), + "uptime_seconds": shared.started.elapsed().as_secs(), + "engine_pid": state.engine_pid, + "engine_uptime_seconds": state.engine_started.map(|t| t.elapsed().as_secs()), + "crashes": state.crashes, + "last_crash": state.last_crash, + "sample": state.sample, + }) +} + +/// A random token: two 64-bit hashes keyed by the OS's random hasher seeds, the time and the process. +pub fn random_token() -> String { + use std::hash::{BuildHasher, Hasher}; + (0..2u64) + .map(|i| { + let mut h = std::collections::hash_map::RandomState::new().build_hasher(); + h.write_u64(i); + h.write_u128( + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_nanos()), + ); + h.write_u32(std::process::id()); + format!("{:016x}", h.finish()) + }) + .collect() +} + +pub fn same_token(a: &str, b: &str) -> bool { + a.len() == b.len() && a.bytes().zip(b.bytes()).fold(0u8, |d, (x, y)| d | (x ^ y)) == 0 +} + +/// Writes a file only its owner can read. +pub fn write_private(path: &Path, text: &str) -> std::io::Result<()> { + let mut options = std::fs::OpenOptions::new(); + options.write(true).create(true).truncate(true); + #[cfg(unix)] + std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); + options.open(path)?.write_all(text.as_bytes()) +} + +/// Removes the endpoint file when the supervisor ends. +pub struct Endpoint(PathBuf); +impl Drop for Endpoint { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } +} + +/// Starts the API on a free port of 127.0.0.1 and writes where it is and its token. +pub fn start(shared: Arc) -> Result { + let listener = TcpListener::bind("127.0.0.1:0").map_err(|e| format!("control API: {e}"))?; + let address = listener.local_addr().map_err(|e| e.to_string())?; + let token = random_token(); + let path = shared.dir.join(ENDPOINT); + write_private( + &path, + &json!({ "address": address.to_string(), "token": token, "pid": std::process::id() }) + .to_string(), + ) + .map_err(|e| format!("{}: {e}", path.display()))?; + let hosts = [address.to_string(), format!("localhost:{}", address.port())]; + http::serve(listener, move |request| { + // A page in a browser can't send the token, and a rebound DNS name can't pass as this address. + let authorised = request + .header("authorization") + .and_then(|h| h.strip_prefix("Bearer ")) + .is_some_and(|t| same_token(t.trim(), &token)); + if !authorised { + return Response::error(401, "missing or wrong token (see .horizon/api.json)"); + } + if !request + .header("host") + .is_some_and(|h| hosts.iter().any(|a| a == h)) + { + return Response::error(403, "wrong Host"); + } + if (request.method.as_str(), request.path.as_str()) == ("POST", "/api/v1/panel/login") { + return match shared.panel.lock().unwrap().login_link() { + Some(url) => Response::json(200, &json!({ "url": url })), + None => Response::error( + 409, + "the panel is off: set [panel] bind in server.toml and restart", + ), + }; + } + route(&shared, request) + }) + .map_err(|e| e.to_string())?; + Ok(Endpoint(path)) +} + +/// The API's routes, for the CLI (with the token) and the panel (with a session). +pub fn route(shared: &Shared, request: &Request) -> Response { + match (request.method.as_str(), request.path.as_str()) { + ("GET", "/api/v1/status") => Response::json(200, &status(shared)), + ("GET", "/api/v1/metrics") => { + let since: u64 = request + .param("since") + .and_then(|s| s.parse().ok()) + .unwrap_or(0); + let samples: Vec = shared + .metrics + .lock() + .unwrap() + .iter() + .filter(|m| m["t"].as_u64().is_some_and(|t| t > since)) + .cloned() + .collect(); + Response::json(200, &json!({ "samples": samples })) + } + ("GET", "/api/v1/logs") => { + let since = request + .param("since") + .and_then(|s| s.parse().ok()) + .unwrap_or(0); + let (next, lines) = shared.log.lock().unwrap().since(since); + Response::json(200, &json!({ "next": next, "lines": lines })) + } + ("POST", "/api/v1/command") => { + let line = serde_json::from_slice::(&request.body) + .ok() + .and_then(|v| v["line"].as_str().map(str::to_owned)); + let Some(line) = line.filter(|l| !l.trim().is_empty() && !l.contains('\n')) else { + return Response::error(400, "the body must be {\"line\": \"\"}"); + }; + if shared.state.lock().unwrap().phase != Phase::Running { + return Response::error(409, "the game isn't running right now"); + } + match engine::Control::connect(&shared.dir) + .and_then(|mut c| c.ask(&format!("console {}", line.trim_start_matches('/')))) + { + Ok(reply) => Response::json(200, &reply), + Err(e) => Response::error(409, &format!("the game didn't answer: {e}")), + } + } + ("POST", "/api/v1/stop") => { + shared.stop.store(true, Ordering::SeqCst); + Response::json(200, &json!({ "ok": true })) + } + ("POST", "/api/v1/restart") => { + shared.restart.store(true, Ordering::SeqCst); + Response::json(200, &json!({ "ok": true })) + } + ("GET", "/api/v1/backups") => { + let list: Vec = backup::list(&shared.dir) + .into_iter() + .map(|b| json!({ "name": b.name, "bytes": b.bytes })) + .collect(); + Response::json(200, &json!({ "backups": list })) + } + ("POST", "/api/v1/backups") => { + let body: Value = serde_json::from_slice(&request.body).unwrap_or(Value::Null); + let made = backup::file_name(std::time::SystemTime::now(), body["name"].as_str()) + .and_then(|name| shared.back_up(&name)); + match made { + Ok(path) => Response::json( + 200, + &json!({ "name": path.file_name().map(|n| n.to_string_lossy()) }), + ), + Err(e) => Response::error(409, &e), + } + } + ("POST", "/api/v1/restore") => { + let body: Value = serde_json::from_slice(&request.body).unwrap_or(Value::Null); + match backup::find(&shared.dir, body["name"].as_str().unwrap_or_default()) { + Ok(name) => { + *shared.restore.lock().unwrap() = Some(name.clone()); + shared.restart.store(true, Ordering::SeqCst); + Response::json(200, &json!({ "name": name })) + } + Err(e) => Response::error(404, &e), + } + } + ( + _, + "/api/v1/status" | "/api/v1/logs" | "/api/v1/command" | "/api/v1/stop" + | "/api/v1/restart" | "/api/v1/backups" | "/api/v1/restore" | "/api/v1/metrics", + ) => Response::error(405, "wrong method"), + _ => Response::error(404, "no such endpoint"), + } +} + +/// The API of the server running in a folder, for the CLI. +pub struct Client { + address: SocketAddr, + token: String, +} + +impl Client { + /// The running server's API; `None` when no server runs in `dir`. + pub fn open(dir: &Path) -> Result, String> { + if crate::supervisor::lock(dir).is_ok() { + return Ok(None); + } + // The supervisor writes the endpoint just after taking the lock: give it a moment. + let path = dir.join(ENDPOINT); + let deadline = Instant::now() + Duration::from_secs(5); + let text = loop { + match std::fs::read_to_string(&path) { + Ok(text) => break text, + Err(_) if Instant::now() < deadline => { + std::thread::sleep(Duration::from_millis(100)) + } + Err(e) => return Err(format!("{}: {e}", path.display())), + } + }; + let v: Value = + serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display()))?; + let address = v["address"] + .as_str() + .and_then(|a| a.parse().ok()) + .ok_or_else(|| format!("{}: no address", path.display()))?; + let token = v["token"].as_str().unwrap_or_default().to_owned(); + Ok(Some(Self { address, token })) + } + + pub fn call(&self, method: &str, path: &str, body: Option<&Value>) -> Result { + self.call_within(method, path, body, Duration::from_secs(60)) + } + + /// A call that may take long (a backup of a big world). + pub fn call_within( + &self, + method: &str, + path: &str, + body: Option<&Value>, + timeout: Duration, + ) -> Result { + let (status, text) = http::request(self.address, &self.token, method, path, body, timeout) + .map_err(|e| format!("the server didn't answer: {e}"))?; + let value: Value = serde_json::from_str(&text).unwrap_or(Value::Null); + if status == 200 { + Ok(value) + } else { + Err(value["error"].as_str().unwrap_or(&text).to_owned()) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn log_keeps_the_last_lines_numbered() { + let mut log = Log::default(); + for i in 0..LOG_LINES + 10 { + log.push(i.to_string()); + } + let (next, lines) = log.since(0); + assert_eq!(next, (LOG_LINES + 10) as u64); + assert_eq!(lines.len(), LOG_LINES); + assert_eq!(lines[0], "10"); + let (next2, tail) = log.since(next - 2); + assert_eq!( + (next2, tail), + ( + next, + vec![(LOG_LINES + 8).to_string(), (LOG_LINES + 9).to_string()] + ) + ); + } + + #[test] + fn the_api_wants_its_token() { + let dir = std::env::temp_dir().join(format!("horizon-server-api-{}", std::process::id())); + std::fs::create_dir_all(dir.join(".horizon")).unwrap(); + let shared = Shared::new(&dir); + let _endpoint = start(shared.clone()).unwrap(); + let v: Value = + serde_json::from_str(&std::fs::read_to_string(dir.join(ENDPOINT)).unwrap()).unwrap(); + let address: SocketAddr = v["address"].as_str().unwrap().parse().unwrap(); + let token = v["token"].as_str().unwrap(); + let get = |token: &str| { + http::request( + address, + token, + "GET", + "/api/v1/status", + None, + Duration::from_secs(5), + ) + .unwrap() + }; + assert_eq!(get("nope").0, 401); + let (status, body) = get(token); + assert_eq!(status, 200); + let status: Value = serde_json::from_str(&body).unwrap(); + assert_eq!(status["phase"], "starting"); + let (code, _) = http::request( + address, + token, + "POST", + "/api/v1/stop", + None, + Duration::from_secs(5), + ) + .unwrap(); + assert_eq!(code, 200); + assert!(shared.stop.load(Ordering::SeqCst)); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = std::fs::metadata(dir.join(ENDPOINT)) + .unwrap() + .permissions() + .mode(); + assert_eq!(mode & 0o777, 0o600); + } + drop(_endpoint); + assert!(!dir.join(ENDPOINT).exists()); + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/horizon-server/src/backup.rs b/crates/horizon-server/src/backup.rs new file mode 100644 index 000000000..c446d56ca --- /dev/null +++ b/crates/horizon-server/src/backup.rs @@ -0,0 +1,332 @@ +//! Backups and restore (docs/server.md, section 7). A backup is one zip in `backups/`: the world folder and +//! the server's settings and lists. On a running server the game first saves everything and stops writing +//! (`save-off`, `save-all`), and writes again once the zip is done (`save-on`), so players stay online. +//! A restore puts the backup's world in place of the current one, which is moved aside, never deleted. + +use crate::config::Config; +use std::{ + fs::File, + io::{BufWriter, Write}, + path::{Path, PathBuf}, + time::SystemTime, +}; +use zip::{CompressionMethod, ZipArchive, ZipWriter, write::SimpleFileOptions}; + +pub const DIR: &str = "backups"; +/// The settings and lists a backup holds beside the world. +const FILES: [&str; 6] = [ + "server.toml", + "server.properties", + "ops.json", + "whitelist.json", + "banned-players.json", + "banned-ips.json", +]; +/// Automatic backups are named `