Merge remote-tracking branch 'origin/main' into perf/behaviour-cache-hash-once

This commit is contained in:
Ludovic Marie
2026-10-26 00:33:29 +01:00
47 changed files with 2331 additions and 253 deletions
+32 -13
View File
@@ -16,7 +16,7 @@ Horizon is Minecraft: Java Edition 26.3 rewritten from scratch in Rust, so it ru
- **Single player as a server: the last differences**: Single player now runs a small server in the background, as vanilla does. We are fixing what still behaves differently there; every fix also works for LAN and server players.
- **Horizon's mod registry**: One place for mods, modpacks and resource packs made for Horizon: browse them on the website and install them from the launcher in one click.
**Next**: Microsoft sign-in (coming soon); CurseForge in the mod browser (coming soon); Fresh benchmarks against vanilla; Pistons push players and mobs; Horizon on Intel Macs; Faster world creation and entry on small CPUs; Servers that hold 20 TPS with 50 to 100 players; Low-end computers: Steam Deck, old Macs, 8 GB; Native upscaling; A batch of targeted optimisations; A Convert to Rust button for what isn't rewritten yet; Mods with code: machines, magic, mobs; Data packs in the mod browser; Prepare a version without Java; Server console, bans and a full admin API; Hosting polish: Windows service, safe updates; Guided hosting with Tailscale and an invite button; An official Docker image for servers; One single-player path: remove the in-frame world; Villages and combat, exactly as in vanilla; Choose the audio output device; Mods that read and change the world; Machines and blocks with inventories (API 0.4); Lua and other languages for mods; Well-known shader looks, rewritten for Horizon; A visible beta label; Wayland and Hyprland polish; A versioned agent-control protocol; Port mods with your own AI agent; Horizon on X (coming soon); Pipeline protection and a GitHub App for agents; Signed updates everywhere, installers included.
**Next**: Microsoft sign-in (coming soon); CurseForge in the mod browser (coming soon); Fresh benchmarks against vanilla; Pistons push players and mobs; Horizon on Intel Macs; Faster world creation and entry on small CPUs; Servers that hold 20 TPS with 50 to 100 players; Low-end computers: Steam Deck, old Macs, 8 GB; Native upscaling; A batch of targeted optimisations; A Convert to Rust button for what isn't rewritten yet; Mods with code: machines, magic, mobs; Data packs in the mod browser; Prepare a version without Java; Server console, bans and a full admin API; Hosting polish: Windows service, safe updates; Guided hosting with Tailscale and an invite button; An official Docker image for servers; One single-player path: remove the in-frame world; Villages and combat, exactly as in vanilla; Choose the audio output device; Mods that read and change the world; Machines and blocks with inventories (API 0.4); Lua and other languages for mods; Well-known shader looks, rewritten for Horizon; A visible beta label; Wayland and Hyprland polish; A versioned agent-control protocol; Port mods with your own AI agent; Horizon on X (coming soon); Pipeline protection and a GitHub App for agents; Signed updates everywhere, installers included; A server you install with one command.
**How to help**: Play it and tell us what differs from vanilla (Discord or GitHub), run the benchmark on your computer, vote for what you want next, or pick one of the Help wanted items.
@@ -374,6 +374,16 @@ More: [docs/server.md](docs/server.md)
</details>
#### A server you install with one command
Install and run a Horizon dedicated server from a terminal with a single command, no launcher needed, for VPSes and headless machines.
<details><summary>Technical</summary>
A player's idea (#horizon-ideas). `horizon-server` already runs headless; left: an install script (Linux/macOS shell, Windows PowerShell) that fetches the signed release, checks it, asks for or points at the player's own game files and starts the server, documented in [docs/hosting.md](docs/hosting.md).
</details>
### Mods and API
#### Mods that read and change the world
@@ -790,18 +800,6 @@ Issue [#1709](https://github.com/ludovic111/horizon/issues/1709): aarch64 builds
</details>
#### OpenGL for old computers
A way to play on old PCs and Linux machines that have no Vulkan or DirectX 12, through OpenGL.
<details><summary>Technical</summary>
Players on older hardware ask whether Vulkan is the only way. Horizon draws with wgpu, which has an OpenGL backend; what is needed is to see which features hold up on it, how fast it runs, and a launcher switch like the DirectX 12 fallback that already ships.
More: [docs/graphics.md](docs/graphics.md)
</details>
#### Android, iOS and the browser
Versions of Horizon for Android, iOS and web browsers. Nothing official yet; contributors have started the first pieces.
@@ -812,6 +810,16 @@ No official build. Android was first closed as not planned, with touch controls,
</details>
#### AppImage and a Nix flake
Install Horizon on any Linux distribution as an AppImage, and on NixOS through a Nix flake.
<details><summary>Technical</summary>
A player's idea (#horizon-ideas). An AppImage of the launcher and game from the release workflow, and a flake.nix packaging the release binaries with their runtime libraries (Vulkan loader, X11/Wayland, ALSA), the way horizon-parity's NixOS setup runs them today.
</details>
### Horizon extras
#### A complete video studio inside the game
@@ -824,6 +832,16 @@ Three stages. Recording: capture gameplay in a keypress with an encoder thread a
</details>
#### Game menus in the launcher's style
An optional look for the game's own menus and options that matches the Horizon launcher. Off by default: the vanilla menus stay as they are.
<details><summary>Technical</summary>
A player's idea (#horizon-ideas). A built-in resource pack or a Horizon setting that restyles the title, pause and options screens with the launcher's design tokens (crates/horizon-theme), following [docs/design-system.md](docs/design-system.md); off by default because it changes the vanilla look.
</details>
### AI and agents
#### An optional AI teammate
@@ -936,6 +954,7 @@ Vanilla's `Chicken.aiStep` updates `flap` and `flapSpeed` when the chicken isn't
## Recently shipped
- **OpenGL for old computers** (v0.2.63): Horizon now starts on old PCs whose graphics card has no Vulkan or DirectX 12 driver: it switches to OpenGL by itself, and Options > Graphics can pick OpenGL by hand. [#1917](https://github.com/ludovic111/horizon/issues/1917) · [#1920](https://github.com/ludovic111/horizon/issues/1920) · [#1936](https://github.com/ludovic111/horizon/issues/1936)
- **Server resource packs** (v0.2.48): Servers can send you their resource pack: Horizon asks, downloads it and applies it, like vanilla. [#1562](https://github.com/ludovic111/horizon/issues/1562) · [#1569](https://github.com/ludovic111/horizon/issues/1569) · [#1570](https://github.com/ludovic111/horizon/issues/1570)
- **Single player on its own server thread** (v0.2.39): Single player runs on a server thread, as vanilla does, so the world keeps ticking smoothly while the window draws. [#1514](https://github.com/ludovic111/horizon/issues/1514) · [docs/server-levels.md](docs/server-levels.md)
- **GPU occlusion culling on Macs** (v0.2.17): On Macs the GPU now skips terrain hidden behind hills and caves: 8 to 48% more frames per second at 32 chunks on an M4 Pro. [#1434](https://github.com/ludovic111/horizon/issues/1434)
@@ -0,0 +1,53 @@
//! Lore tooltip companion for tools/test_lore_tooltip_parity.py: the spans
//! `item_tooltip::lore_texts` gives each stack (SNBT, one per line).
use horizon_client::{
item_data::parse_stack,
item_tooltip::{TooltipDisplay, lore_texts},
};
use std::{error::Error, fmt::Write};
pub fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = crate::args().collect();
let input = std::fs::read_to_string(&args[1])?;
let mut output = String::new();
for line in input
.lines()
.filter(|l| !l.is_empty() && !l.starts_with('#'))
{
let stack = parse_stack(line)?.ok_or("empty stack")?;
let display = TooltipDisplay::for_stack(&stack);
if !display.shows("minecraft:lore") {
writeln!(output, "-")?;
continue;
}
let lines = lore_texts(&stack);
if lines.is_empty() {
writeln!(output, "-")?;
continue;
}
let mut rendered = Vec::new();
for text in lines {
let mut spans = Vec::new();
for span in &text.spans {
if span.text.is_empty() {
continue;
}
let style = &span.style;
spans.push(format!(
"{}\t{}\t{}\t{}\t{}\t{}\t{}",
span.text,
style.color.map(|c| c as i32).unwrap_or(-1),
style.bold == Some(true),
style.italic == Some(true),
style.underlined == Some(true),
style.strikethrough == Some(true),
style.obfuscated == Some(true),
));
}
rendered.push(spans.join(" | "));
}
writeln!(output, "{}", rendered.join(" || "))?;
}
std::fs::write(&args[2], output)?;
Ok(())
}
@@ -49,6 +49,7 @@ mod input_probe;
mod interaction_input_fixture;
mod lan_probe;
mod light_parity;
mod lore_tooltip_fixture;
mod marker_particles_probe;
mod menu_fixture;
mod mipmap_probe;
@@ -133,6 +134,7 @@ probes! {
interaction_input_fixture,
lan_probe,
light_parity,
lore_tooltip_fixture,
marker_particles_probe,
menu_fixture,
mipmap_probe,
+50
View File
@@ -63,6 +63,10 @@ enum Animate {
SculkSensor,
/// Potent sulfur that is not dry.
PotentSulfur,
DriedGhast {
waterlogged: bool,
},
EndPortal,
}
fn step(facing: &str) -> [i32; 3] {
match facing {
@@ -137,6 +141,10 @@ fn classify(name: &str, prop: &dyn Fn(&str) -> Option<String>) -> Animate {
drag: prop("drag").as_deref() == Some("true"),
},
"fire" | "soul_fire" => Animate::Fire,
"dried_ghast" => Animate::DriedGhast {
waterlogged: prop("waterlogged").as_deref() == Some("true"),
},
"end_portal" => Animate::EndPortal,
"sculk_sensor" | "calibrated_sculk_sensor"
if prop("sculk_sensor_phase").as_deref() == Some("active") =>
{
@@ -180,6 +188,8 @@ pub struct AnimateTables {
impermeable: Vec<bool>,
power_provider: Vec<bool>,
power_transmitter: Vec<bool>,
/// `#triggers_ambient_dried_ghast_block_sounds` (soul sand, soul soil).
dried_ghast_sounds: Vec<bool>,
/// Biome id -> ambient particles and probabilities (`minecraft:visual/ambient_particles`).
ambient: Vec<Vec<(T, f32)>>,
}
@@ -237,6 +247,7 @@ impl AnimateTables {
impermeable: by_state("minecraft:impermeable"),
power_provider: by_state("minecraft:enchantment_power_provider"),
power_transmitter: by_state("minecraft:enchantment_power_transmitter"),
dried_ghast_sounds: by_state("minecraft:triggers_ambient_dried_ghast_block_sounds"),
ambient,
})
}
@@ -869,6 +880,45 @@ fn block_animate_tick(
];
add(engine, T::Smoke, p, [0.0; 3]);
}
Animate::DriedGhast { waterlogged } => {
// DriedGhastBlock.animateTick.
let c = [fx + 0.5, fy + 0.5, fz + 0.5];
if waterlogged {
if next_int(engine.random(), 40) == 0 {
let name = "minecraft:block.dried_ghast.ambient_water";
sound(engine, name, c, 1.0, 1.0);
}
let r = engine.random();
if next_int(r, 6) == 0 {
let x = c[0] + f64::from((r.next_float() * 2.0 - 1.0) / 3.0);
let y = c[1] + 0.4;
let z = c[2] + f64::from((r.next_float() * 2.0 - 1.0) / 3.0);
let v = [0.0, f64::from(r.next_float()), 0.0];
add(engine, T::HappyVillager, [x, y, z], v);
}
} else {
let below = ctx.state([pos[0], pos[1] - 1, pos[2]]);
let triggers = || {
tables
.dried_ghast_sounds
.get(below.0 as usize)
.copied()
.unwrap_or(false)
};
if next_int(engine.random(), 40) == 0 && triggers() {
sound(engine, "minecraft:block.dried_ghast.ambient", c, 1.0, 1.0);
}
if next_int(engine.random(), 6) == 0 {
add(engine, T::WhiteSmoke, c, [0.0, 0.02, 0.0]);
}
}
}
Animate::EndPortal => {
// EndPortalBlock.animateTick: one smoke particle above the surface.
let r = engine.random();
let p = [fx + r.next_double(), fy + 0.8, fz + r.next_double()];
add(engine, T::Smoke, p, [0.0; 3]);
}
Animate::EnderChest => {
for _ in 0..3 {
let r = engine.random();
+2
View File
@@ -20,6 +20,7 @@ mod bossbars;
mod breezes;
mod cartography;
mod chat;
mod client_ambient;
mod clipboard;
mod combat;
mod command_blocks;
@@ -4481,6 +4482,7 @@ impl Game<'_> {
self.tick_block_entity_anims()
});
horizon_client::spans::time("tick_particles", || self.tick_particles());
self.tick_client_ambient();
}
// ServerBossEvent broadcasts, after this level's entities have ticked.
self.publish_boss_bars();
+6 -1
View File
@@ -519,7 +519,12 @@ impl Game<'_> {
self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
} else if let Some(key) = tooltip {
let text = self.translate(&key, &key);
self.draw_tooltip_lines(batch, &[(text, u32::MAX)], self.cursor.0, self.cursor.1);
self.draw_tooltip_lines(
batch,
&[(horizon_client::text::Text::literal(text), u32::MAX)],
self.cursor.0,
self.cursor.1,
);
}
}
/// `BeaconRenderer`: each section's rotating solid beam and its glow.
+1 -1
View File
@@ -18,7 +18,7 @@ fn chat_tree<'a>(
mods: &'a super::mods::ModsState,
) -> &'a horizon_client::command::Dispatcher {
match remote.as_ref().and_then(|r| r.commands.as_ref()) {
Some(tree) => tree,
Some(tree) => mods.remote_dispatcher().unwrap_or(tree),
None => mods.dispatcher(),
}
}
@@ -0,0 +1,37 @@
//! The window's own ambient effects of entities, which a client makes for
//! itself (`level().isClientSide()` branches): endermen's and endermites'
//! portal particles, blazes' smoke and burn, eyes of ender's trail. They run
//! on the game that has the window, in-frame or following a server, never on
//! a server: nothing of them goes over the network.
use super::Game;
use horizon_client::mobs::{MobEvent, ambient};
use horizon_client::particles::{ParticleOptions, ParticleType};
impl Game<'_> {
pub(super) fn tick_client_ambient(&mut self) {
if self.serving.is_some() || self.options.dedicated.is_some() {
return;
}
let mut events = Vec::new();
for m in self.mobs.iter_mut().filter(|m| !m.removed) {
ambient::client_ai_step(m, &mut events);
}
for event in events {
match event {
MobEvent::ParticleAt { kind, at, velocity } => {
if let Some(ty) = ParticleType::from_name(kind) {
self.particles
.add(&ParticleOptions::simple(ty), at, velocity);
}
}
// Each guest of a LAN host makes its own.
MobEvent::LocalSound(name, at, volume, pitch) => {
self.audio
.play_unshared(&format!("minecraft:{name}"), Some(at), volume, pitch)
}
_ => {}
}
}
self.eye_particles();
}
}
+35 -1
View File
@@ -4,7 +4,12 @@
//! inactive until the dragon dies.
use super::{Dim, Game};
use horizon_client::{
Result, environment::Environment, items::ItemStack, mesher::Vertex, motion::PlayerMotion,
Result,
environment::Environment,
items::ItemStack,
mesher::Vertex,
motion::PlayerMotion,
particles::{ParticleOptions, ParticleType},
vanillagen::VanillaTerrain,
};
use horizon_core::{
@@ -1122,6 +1127,35 @@ impl Game<'_> {
self.end.eyes = eyes;
}
/// `EyeOfEnder.tick` on a client: behind each eye (a quarter of its last
/// move back), four bubbles in water, else a portal particle.
pub(super) fn eye_particles(&mut self) {
for i in 0..self.end.eyes.len() {
let e = &self.end.eyes[i];
let movement = (e.position - e.previous).to_array();
let origin = (e.position - (e.position - e.previous) * 0.25).to_array();
let block = origin.map(|v| v.floor() as i32);
let water = self
.res
.facts()
.get(self.world.block(block[0], block[1], block[2]))
.fluid
== horizon_client::blocks::Fluid::Water;
if water {
let bubble = ParticleOptions::simple(ParticleType::Bubble);
for _ in 0..4 {
self.particles.add(&bubble, origin, movement);
}
} else {
let r = self.particles.random();
let x = origin[0] + r.next_double() * 0.6 - 0.3;
let z = origin[2] + r.next_double() * 0.6 - 0.3;
let portal = ParticleOptions::simple(ParticleType::Portal);
self.particles
.add(&portal, [x, origin[1] - 0.5, z], movement);
}
}
}
/// ThrownItemRenderer: the ender eye item facing the camera.
fn eye_geometry(&self, camera: glam::DVec3, out: &mut Vec<Vertex>) {
let Some(r) = self.gui.atlas.sprites.get("item/ender_eye").copied() else {
+38 -5
View File
@@ -25,9 +25,15 @@ pub(super) struct Rocket {
pub(super) velocity: Vec3,
life: i32,
lifetime: i32,
explosions: Vec<FireworkExplosion>,
pub(super) explosions: Vec<FireworkExplosion>,
/// Boosting the player (attachedToEntity).
pub(super) attached: bool,
/// On a server, the player it boosts when not this game's own: a guest's
/// connection.
pub(super) guest: Option<u32>,
/// `DATA_ATTACHED_TO_TARGET`: the server entity the rocket follows;
/// boosting happens when it is the local player.
pub(super) attached_to: Option<u32>,
/// `DATA_SHOT_AT_ANGLE` (dispensers): constant velocity, exploding on
/// the first block it hits.
pub(super) angled: bool,
@@ -48,6 +54,8 @@ impl Rocket {
lifetime: i32::MAX,
explosions,
attached: false,
guest: None,
attached_to: None,
angled: false,
item,
}
@@ -63,7 +71,7 @@ impl Rocket {
(self.position, self.velocity)
}
}
fn fireworks_of(stack: &ItemStack) -> (u8, Vec<FireworkExplosion>) {
pub(super) fn fireworks_of(stack: &ItemStack) -> (u8, Vec<FireworkExplosion>) {
// The item's default component: flight 1, no explosions.
stack
.data()
@@ -89,6 +97,8 @@ impl Game<'_> {
lifetime,
explosions,
attached,
guest: None,
attached_to: None,
angled: false,
item: stack.clone(),
}
@@ -127,7 +137,8 @@ impl Game<'_> {
return false;
}
let at = self.player.as_ref().map_or(Vec3::ZERO, |p| p.position);
let rocket = self.new_rocket(at, stack, true);
let mut rocket = self.new_rocket(at, stack, true);
rocket.guest = self.lan_acting;
self.rockets.push(rocket);
if !self.creative {
self.consume_held();
@@ -140,7 +151,18 @@ impl Game<'_> {
while i < self.rockets.len() {
let mut r = self.rockets.remove(i);
r.previous = r.position;
if r.attached {
if r.attached
&& let Some(conn) = r.guest
{
// A guest's rocket follows them; their own client boosts them.
let guest = self
.lan
.as_ref()
.and_then(|host| host.hub.guests.iter().find(|g| g.conn == conn));
if let Some(g) = guest {
r.position = Vec3::new(g.position[0], g.position[1], g.position[2]);
}
} else if r.attached {
if let Some(p) = self.player.as_mut() {
if p.fall_flying {
let look = view_vector(self.yaw, self.pitch);
@@ -229,8 +251,19 @@ impl Game<'_> {
let at = [r.position.x, r.position.y, r.position.z];
if !r.explosions.is_empty() {
let damage = 5.0 + r.explosions.len() as f32 * 2.0;
// The player it boosts takes the full damage, a guest as well.
if r.attached {
self.hurt_player_by("minecraft:fireworks", damage);
match r.guest {
None => self.hurt_player_by("minecraft:fireworks", damage),
Some(conn) => {
if let Some(mut host) = self.lan.take() {
self.as_guest(&mut host, conn, |g| {
g.hurt_player_by("minecraft:fireworks", damage);
});
self.lan = Some(host);
}
}
}
}
let from = r.position;
let hits: Vec<(u32, f32)> = {
+22
View File
@@ -162,6 +162,13 @@ fn mob_meta(
meta.push((3, serializer::BOOLEAN, Value::Bool(true)));
}
}
// LivingEntity.DATA_LIVING_ENTITY_FLAGS: using an item 1, in the off
// hand 2 (a skeleton draws its bow, a witch raises her potion).
let using = i8::from(m.extra.using_item > 0) | i8::from(m.extra.using_off_hand) << 1;
if using != 0 {
meta.push((8, serializer::BYTE, Value::Byte(using)));
}
defaults.push((8, serializer::BYTE, Value::Byte(0)));
// LivingEntity.DATA_HEALTH_ID: defined as 1, then set to the max health.
let health = m.health.max(0.0);
if health != 1.0 {
@@ -293,6 +300,21 @@ fn mob_meta(
meta.push((18, serializer::BOOLEAN, Value::Bool(true)));
}
}
// Pillager.IS_CHARGING_CROSSBOW: the charge pose and the held
// item's loading animation.
"minecraft:pillager" => {
if e.flag {
meta.push((17, serializer::BOOLEAN, Value::Bool(true)));
}
defaults.push((17, serializer::BOOLEAN, Value::Bool(false)));
}
// SpellcasterIllager.DATA_SPELL_CASTING_ID: the casting pose.
"minecraft:evoker" | "minecraft:illusioner" => {
if e.variant2 != 0 {
meta.push((17, serializer::BYTE, Value::Byte(e.variant2 as i8)));
}
defaults.push((17, serializer::BYTE, Value::Byte(0)));
}
// Enderman.DATA_CARRY_STATE.
"minecraft:enderman" => {
if let Some(state) = e.carried {
@@ -275,6 +275,8 @@ impl Game<'_> {
motion.sprinting = g.sprinting;
motion.may_fly = g.creative() || g.game_mode == SPECTATOR;
motion.flying = g.flying;
// FireworkRocketItem.use boosts only a gliding player.
motion.fall_flying = g.fall_flying;
motion.invulnerable = motion.may_fly;
let _ = motion.set_health(g.health, false);
motion.health.food.food = g.food;
+10 -2
View File
@@ -404,11 +404,19 @@ impl Game<'_> {
Value::Item(hub.stack(&Some(r.item.clone()))),
));
}
let boosted = match r.guest {
Some(conn) => hub
.guests
.iter()
.find(|g| g.conn == conn)
.map(|g| g.entity_id),
None => host_entity,
};
if r.attached
&& let Some(host) = host_entity
&& let Some(player) = boosted
{
e.meta
.push((9, serializer::OPTIONAL_UNSIGNED_INT, Value::Id(host + 1)));
.push((9, serializer::OPTIONAL_UNSIGNED_INT, Value::Id(player + 1)));
}
if r.angled {
e.meta.push((10, serializer::BOOLEAN, Value::Bool(true)));
+6 -3
View File
@@ -218,7 +218,7 @@ impl Game<'_> {
oy: f32,
draws: &[ScreenDraw],
background: bool,
) -> Option<Vec<(String, u32)>> {
) -> Option<Vec<(horizon_client::text::Text, u32)>> {
let atlas = &self.gui.atlas;
let mut tooltip = None;
let mut sheet: Option<String> = None;
@@ -261,7 +261,10 @@ impl Game<'_> {
..
} => self.draw_banner_result(batch, ox + *x0 as f32, oy + *y0 as f32, base, layers),
ScreenDraw::Tooltip { key } => {
tooltip = Some(vec![(self.translate(key, key), u32::MAX)]);
tooltip = Some(vec![(
horizon_client::text::Text::literal(self.translate(key, key)),
u32::MAX,
)]);
}
ScreenDraw::SpriteOf { id } => sheet = Some(id.clone()),
ScreenDraw::Fill {
@@ -314,7 +317,7 @@ impl Game<'_> {
ox: f32,
oy: f32,
local: (f32, f32),
) -> Option<Vec<(String, u32)>> {
) -> Option<Vec<(horizon_client::text::Text, u32)>> {
let s = self.station.as_ref()?;
let l = &s.loom;
let dye = s.inputs[1]
+2 -2
View File
@@ -259,7 +259,7 @@ impl Game<'_> {
// ItemInHandLayer submits with OverlayTexture.NO_OVERLAY: no hurt tint.
for (item, off_hand) in [(&mob.extra.main_hand, false), (&mob.extra.off_hand, true)] {
let Some(item) = item else { continue };
let left = off_hand != mob.extra.main_hand_left;
let left = off_hand != mob.main_arm_left();
let Some(t) =
render::held_item_transform(mob, alpha as f32, camera, self.res.mob_data(), left)
else {
@@ -305,7 +305,7 @@ impl Game<'_> {
.player_model
.as_ref()
.is_some_and(|p| std::ptr::eq(p, mob));
owner.main_arm_left = mob.extra.main_hand_left;
owner.main_arm_left = mob.main_arm_left();
if off_hand == mob.extra.using_off_hand && mob.extra.using_item > 0 {
owner.using_this_item = true;
owner.use_duration = 72_000;
+25
View File
@@ -42,6 +42,9 @@ pub(super) struct ModsState {
/// The chat's command tree while a running mod owns commands.
#[cfg(feature = "mods")]
tree: Option<Dispatcher>,
/// The server's command tree with the running mods' commands added.
#[cfg(feature = "mods")]
remote_tree: Option<Dispatcher>,
/// A `ui` mod's screen or settings page while it is up.
#[cfg(feature = "mods")]
screen: Option<ModScreen>,
@@ -69,6 +72,14 @@ impl ModsState {
}
vanilla::game_dispatcher()
}
/// The server's tree plus the mods' commands, when a mod owns any.
pub(super) fn remote_dispatcher(&self) -> Option<&Dispatcher> {
#[cfg(feature = "mods")]
if let Some(tree) = &self.remote_tree {
return Some(tree);
}
None
}
/// Whether a mod runs: the chat keeps its lines for them.
pub(super) fn running(&self) -> bool {
#[cfg(feature = "mods")]
@@ -178,6 +189,7 @@ impl Game<'_> {
);
}
}
pub(super) fn merge_remote_tree(&mut self) {}
pub(super) fn sync_mods(&mut self) {}
pub(super) fn tick_mods(&mut self) {}
pub(super) fn mods_frame(&mut self, _elapsed: Duration) {}
@@ -267,7 +279,20 @@ impl Game<'_> {
.peek()
.is_some()
.then(|| vanilla::game_dispatcher().with_mod_commands(names));
self.merge_remote_tree();
}
/// The server's command tree with the mods' commands, for the chat.
pub(super) fn merge_remote_tree(&mut self) {
let names: Vec<String> = self.mods.mods.commands().map(str::to_owned).collect();
self.mods.remote_tree = self
.remote
.as_ref()
.and_then(|r| r.commands.as_ref())
.filter(|_| !names.is_empty())
.map(|t| t.with_mod_commands(names.iter().map(String::as_str)));
}
/// After the options changed: restart the mods if the list of the ones
/// turned on did.
pub(super) fn sync_mods(&mut self) {
+16 -5
View File
@@ -1163,8 +1163,12 @@ impl Game<'_> {
}
/// Paints recipe book calls; returns the first tooltip set (vanilla keeps
/// the first one a frame asks for).
fn paint_book(&mut self, batch: &mut HudBatch, calls: &[Drawn]) -> Option<Vec<(String, u32)>> {
let mut tooltip: Option<Vec<(String, u32)>> = None;
fn paint_book(
&mut self,
batch: &mut HudBatch,
calls: &[Drawn],
) -> Option<Vec<(horizon_client::text::Text, u32)>> {
let mut tooltip: Option<Vec<(horizon_client::text::Text, u32)>> = None;
for d in calls {
let start = batch.vertices.len();
let atlas = &self.gui.atlas;
@@ -1259,7 +1263,14 @@ impl Game<'_> {
tooltip = Some(
lines
.iter()
.map(|l| (l.iter().map(|r| r.text.as_str()).collect(), u32::MAX))
.map(|l| {
(
horizon_client::text::Text::literal(
l.iter().map(|r| r.text.as_str()).collect::<String>(),
),
u32::MAX,
)
})
.collect(),
);
}
@@ -1269,10 +1280,10 @@ impl Game<'_> {
let mut lines = self.stack_tooltip(stack);
if *more {
lines.push((
self.translate(
horizon_client::text::Text::literal(self.translate(
"gui.recipebook.moreRecipes",
"Right Click for More",
),
)),
u32::MAX,
));
}
+26 -34
View File
@@ -1904,25 +1904,12 @@ impl Game<'_> {
}
}
}
// LivingEntity.onSyncedDataUpdated(DATA_LIVING_ENTITY_FLAGS):
// another player starts using the item in a hand (eating, a
// bow drawn, a shield up), or stops.
// LivingEntity.DATA_LIVING_ENTITY_FLAGS: for another player
// too, remote_mob_data's apply reads index 8 above.
let other = self
.remote
.as_ref()
.is_some_and(|r| r.level_players.contains_key(&p.id) && r.entity_id != p.id);
if other && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) {
for (index, _, value) in &p.values {
if let (8, Value::Byte(flags)) = (index, value) {
m.extra.using_off_hand = flags & 2 != 0;
if flags & 1 == 0 {
m.extra.using_item = 0;
} else if m.extra.using_item == 0 {
m.extra.using_item = 1;
}
}
}
}
// LivingEntity.onSyncedDataUpdated(SLEEPING_POS_ID): another
// player lies down in their bed (drawn lying in it), or gets up.
if other {
@@ -2378,6 +2365,7 @@ impl Game<'_> {
if let Some(r) = self.remote.as_mut() {
r.commands = Some(tree);
}
self.merge_remote_tree();
}
C::SystemChat(p) => {
// The component as sent, translated in the client's own
@@ -3072,6 +3060,7 @@ impl Game<'_> {
self.tick_block_entity_anims();
self.tick_hand();
self.tick_particles();
self.tick_client_ambient();
self.tick_client_effects();
// Player.tick's sleep counter (the darkening and the wake-up fade).
self.sleep_timer = super::tick_sleep_timer(self.sleep_timer, self.sleeping.is_some());
@@ -3405,25 +3394,28 @@ impl Game<'_> {
m.head_yaw += wrap_degrees(*yaw - m.head_yaw) / *steps as f32;
*steps -= 1;
}
// LivingEntity.updatingUsingItem for another player: the ticks the
// item has been used before this one.
if player && m.extra.using_item > 0 {
let item = if m.extra.using_off_hand {
m.extra.off_hand.clone()
} else {
m.extra.main_hand.clone()
};
if let Some(item) = item {
let eye = if m.extra.crouching { 1.27 } else { 1.62 };
let at = [m.position.x, m.position.y, m.position.z];
using.push((
item,
m.extra.using_item as u32 - 1,
at,
eye,
m.head_pitch,
m.yaw,
));
// LivingEntity.updatingUsingItem: the ticks the item has been
// used before this one. Another player's use particles; a mob's
// drawn bow only needs the count (its pull follows it).
if m.extra.using_item > 0 {
if player {
let item = if m.extra.using_off_hand {
m.extra.off_hand.clone()
} else {
m.extra.main_hand.clone()
};
if let Some(item) = item {
let eye = if m.extra.crouching { 1.27 } else { 1.62 };
let at = [m.position.x, m.position.y, m.position.z];
using.push((
item,
m.extra.using_item as u32 - 1,
at,
eye,
m.head_pitch,
m.yaw,
));
}
}
m.extra.using_item += 1;
}
@@ -20,6 +20,17 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) {
// client, which draws the flames. A client doesn't count the
// fire down: the flag goes off when the server's fire is out.
(0, Value::Byte(flags)) => m.fire_ticks = i32::from(flags & 1),
// LivingEntity.DATA_LIVING_ENTITY_FLAGS: using an item 1, in the
// off hand 2. The flag starts and stops the use; the client ticks
// the count itself (a bow's pull follows it).
(8, Value::Byte(flags)) => {
m.extra.using_off_hand = flags & 2 != 0;
if flags & 1 == 0 {
m.extra.using_item = 0;
} else if m.extra.using_item == 0 {
m.extra.using_item = 1;
}
}
// LivingEntity.DATA_HEALTH_ID: the boss bars and the death
// animation follow it.
(9, Value::Float(health)) => m.health = *health,
@@ -53,6 +64,13 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) {
(Species::Slime | Species::MagmaCube, 18, Value::Int(size)) => {
m.extra.size = (*size).clamp(1, 127);
}
// Pillager.IS_CHARGING_CROSSBOW: the charge pose (Raider's
// IS_CELEBRATING takes 16).
(Species::Pillager, 17, Value::Bool(charging)) => m.extra.flag = *charging,
// SpellcasterIllager.DATA_SPELL_CASTING_ID: the casting pose.
(Species::Evoker | Species::Illusioner, 17, Value::Byte(spell)) => {
m.extra.variant2 = (*spell).max(0) as u8;
}
// Villager.DATA_VILLAGER_DATA and ZombieVillager's (after
// Zombie's three): what VillagerProfessionLayer draws.
(Species::Villager, 19, Value::Ids(data))
@@ -239,4 +257,42 @@ mod tests {
);
assert_eq!(name(&z), ("desert", "farmer", 3));
}
#[test]
fn the_mob_flags_drive_the_aggressive_poses() {
let Some(mut m) = mob("minecraft:skeleton") else {
return;
};
apply(&mut m, &[(15, s::BYTE, Value::Byte(2 | 4))]);
assert!(m.ai.aggressive && m.extra.left_handed && !m.no_ai);
apply(&mut m, &[(15, s::BYTE, Value::Byte(0))]);
assert!(!m.ai.aggressive && !m.extra.left_handed);
}
#[test]
fn the_living_flags_start_and_stop_a_mob_using_an_item() {
let Some(mut m) = mob("minecraft:skeleton") else {
return;
};
apply(&mut m, &[(8, s::BYTE, Value::Byte(1))]);
assert_eq!(m.extra.using_item, 1);
assert!(!m.extra.using_off_hand);
// Using in the off hand.
apply(&mut m, &[(8, s::BYTE, Value::Byte(3))]);
assert!(m.extra.using_off_hand);
apply(&mut m, &[(8, s::BYTE, Value::Byte(0))]);
assert_eq!(m.extra.using_item, 0);
}
#[test]
fn a_pillager_charges_and_a_spellcaster_casts() {
let Some(mut p) = mob("minecraft:pillager") else {
return;
};
apply(&mut p, &[(17, s::BOOLEAN, Value::Bool(true))]);
assert!(p.extra.flag);
let mut e = mob("minecraft:evoker").unwrap();
apply(&mut e, &[(17, s::BYTE, Value::Byte(2))]);
assert_eq!(e.extra.variant2, 2);
}
}
@@ -2,7 +2,7 @@
//! end crystals on a server's client (`ClientPacketListener.handleAddEntity`
//! and `handleSetEntityData`): the server simulates them, the client draws
//! them where it says and interpolates their moves.
use super::{FallStep, FallingBlock, Game};
use super::{FallStep, FallingBlock, Game, fireworks::fireworks_of, view_vector};
use horizon_client::projectiles::{Projectile, ProjectileKind};
use horizon_core::{geometry::Vec3, position::BlockPos};
use horizon_protocol::entity_data::Value;
@@ -125,6 +125,37 @@ impl Game<'_> {
pub(super) fn remote_projectile_data(&mut self, id: i32, values: &[(u8, i32, Value)]) {
let id = id as u32;
self.remote_hook_data(id, values);
if self.rockets.iter().any(|r| r.id == id) {
for (index, _, value) in values {
match (index, value) {
// FireworkRocketEntity.DATA_ID_FIREWORKS_ITEM: its
// explosions for `createFireworks`.
(8, Value::Item(stack)) => {
if let Some(stack) = self.to_stack(stack)
&& let Some(r) = self.rockets.iter_mut().find(|r| r.id == id)
{
let (_, explosions) = fireworks_of(&stack);
r.explosions = explosions;
r.item = stack;
}
}
// DATA_ATTACHED_TO_TARGET (OptionalInt: the id plus 1).
(9, Value::Id(v)) => {
let v = (*v > 0).then(|| (*v - 1) as u32);
if let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) {
r.attached_to = v;
}
}
// DATA_SHOT_AT_ANGLE.
(10, Value::Bool(a)) => {
if let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) {
r.angled = *a;
}
}
_ => {}
}
}
}
if self.potions.has_thrown(id) {
// ThrowableItemProjectile.DATA_ITEM_STACK: the potion it is.
for (index, _, value) in values {
@@ -213,9 +244,30 @@ impl Game<'_> {
let id = t.id;
t.remote_step(|at| step(id, at));
}
// FireworkRocketEntity.tick: a rocket attached to the gliding local
// player boosts it client-side too (attachedToEntity.isFallFlying).
let me = self
.remote
.as_ref()
.and_then(|r| u32::try_from(r.entity_id).ok());
for r in &mut self.rockets {
let id = r.id;
r.remote_step(|at| step(id, at));
if me.is_some()
&& r.attached_to == me
&& let Some(p) = self.player.as_mut()
&& p.fall_flying
{
let look = view_vector(self.yaw, self.pitch);
let m = p.velocity;
p.velocity = Vec3::new(
m.x + look[0] * 0.1 + (look[0] * 1.5 - m.x) * 0.5,
m.y + look[1] * 0.1 + (look[1] * 1.5 - m.y) * 0.5,
m.z + look[2] * 0.1 + (look[2] * 1.5 - m.z) * 0.5,
);
r.position = p.position;
r.velocity = p.velocity;
}
}
for c in &mut self.crystals {
c.time += 1;
+90 -45
View File
@@ -1092,7 +1092,7 @@ impl Game<'_> {
}
let (ox, oy) = self.inventory_origin(batch);
let local = (self.cursor.0 - ox, self.cursor.1 - oy);
let mut tooltip: Option<Vec<(String, u32)>> = None;
let mut tooltip: Option<Vec<(horizon_client::text::Text, u32)>> = None;
{
let atlas = &self.gui.atlas;
batch.fill(
@@ -1341,16 +1341,19 @@ impl Game<'_> {
let clue = self
.translate("container.enchant.clue", "%s . . . ?")
.replace("%s", &name);
let mut lines = vec![(clue, u32::MAX)];
let mut lines =
vec![(horizon_client::text::Text::literal(clue), u32::MAX)];
if !self.creative {
lines.push((String::new(), u32::MAX));
lines.push((horizon_client::text::Text::literal(""), u32::MAX));
if level < cost {
lines.push((
self.translate(
"container.enchant.level.requirement",
"Level Requirement: %s",
)
.replace("%s", &cost.to_string()),
horizon_client::text::Text::literal(
self.translate(
"container.enchant.level.requirement",
"Level Requirement: %s",
)
.replace("%s", &cost.to_string()),
),
rgba(255, 85, 85, 255),
));
} else {
@@ -1368,7 +1371,7 @@ impl Game<'_> {
.replace("%s", &n.to_string())
};
lines.push((
lapis,
horizon_client::text::Text::literal(lapis),
if gold >= n as i32 {
rgba(170, 170, 170, 255)
} else {
@@ -1387,7 +1390,10 @@ impl Game<'_> {
)
.replace("%s", &n.to_string())
};
lines.push((levels, rgba(170, 170, 170, 255)));
lines.push((
horizon_client::text::Text::literal(levels),
rgba(170, 170, 170, 255),
));
}
}
tooltip = Some(lines);
@@ -1496,10 +1502,10 @@ impl Game<'_> {
);
if inside(local, 65.0, 46.0, 28.0, 21.0) {
tooltip = Some(vec![(
self.translate(
horizon_client::text::Text::literal(self.translate(
"container.upgrade.error_tooltip",
"Item can't be upgraded this way",
),
)),
u32::MAX,
)]);
}
@@ -1626,9 +1632,12 @@ impl Game<'_> {
// ---------------------------------------------------------------- tooltips
/// `ItemStack.getTooltipLines` subset: name in rarity colour, potion
/// effects, enchantments (gray, curses red), stored enchantments, trims.
pub(super) fn stack_tooltip(&self, stack: &ItemStack) -> Vec<(String, u32)> {
/// `ItemStack.getTooltipLines` subset: name in rarity colour, lore,
/// potion effects, enchantments (gray, curses red), stored enchantments, trims.
pub(super) fn stack_tooltip(
&self,
stack: &ItemStack,
) -> Vec<(horizon_client::text::Text, u32)> {
let e: &Enchantments = &self.res.stations().enchantments;
let props = e.props(&stack.item);
let rarity = horizon_client::item_tooltip::rarity(
@@ -1660,24 +1669,27 @@ impl Game<'_> {
let display = horizon_client::item_tooltip::TooltipDisplay::for_stack(stack);
let mut lines = Vec::new();
if display.name_visible(self.screen == Screen::Creative) {
lines.push((name, color));
lines.push((horizon_client::text::Text::legacy(&name), color));
}
let translate = |k: &str| self.pack.names.display_get(k);
let push = |lines: &mut Vec<(horizon_client::text::Text, u32)>, s: String, c: u32| {
lines.push((horizon_client::text::Text::legacy(&s), c));
};
// ItemContainerContents.addToTooltip.
let hover_name = |s: &ItemStack| {
s.custom_name()
.map_or_else(|| self.pack.names.display_item(&s.item), str::to_owned)
};
if display.shows("minecraft:container") {
for line in
for text in
horizon_client::item_tooltip::container_lines(stack, &hover_name, &translate)
{
lines.push((line, u32::MAX));
push(&mut lines, text, u32::MAX);
}
}
// Fireworks / FireworkExplosion.addToTooltip.
for line in horizon_client::item_tooltip::firework_lines(stack, &translate) {
lines.push((line, rgba(170, 170, 170, 255)));
for text in horizon_client::item_tooltip::firework_lines(stack, &translate) {
push(&mut lines, text, rgba(170, 170, 170, 255));
}
if display.shows("minecraft:potion_contents")
&& (stack.item.ends_with("potion") || &*stack.item == "minecraft:tipped_arrow")
@@ -1687,7 +1699,7 @@ impl Game<'_> {
"minecraft:tipped_arrow" => 0.125,
_ => 1.0,
};
for (line, cat) in brewing::tooltip(stack.potion(), scale, &|k| {
for (text, cat) in brewing::tooltip(stack.potion(), scale, &|k| {
e.translate(k).map(str::to_owned)
}) {
let c = match cat {
@@ -1695,18 +1707,19 @@ impl Game<'_> {
brewing::Category::Harmful => rgba(255, 85, 85, 255),
brewing::Category::Neutral => rgba(170, 170, 170, 255),
};
lines.push((line, c));
push(&mut lines, text, c);
}
}
if display.shows("minecraft:trim")
&& let Some((material, pattern)) = stack.data().and_then(|d| d.trim.clone())
{
lines.push((
push(
&mut lines,
e.translate("item.minecraft.smithing_template.upgrade")
.unwrap_or("Upgrade:")
.to_owned(),
rgba(170, 170, 170, 255),
));
);
let short = |s: &str| s.trim_start_matches("minecraft:").to_owned();
let p = e
.translate(&format!("trim_pattern.minecraft.{}", short(&pattern)))
@@ -1714,38 +1727,51 @@ impl Game<'_> {
let m = e
.translate(&format!("trim_material.minecraft.{}", short(&material)))
.map_or_else(|| short(&material), str::to_owned);
lines.push((format!(" {p}"), rgba(170, 170, 170, 255)));
lines.push((format!(" {m}"), rgba(170, 170, 170, 255)));
push(&mut lines, format!(" {p}"), rgba(170, 170, 170, 255));
push(&mut lines, format!(" {m}"), rgba(170, 170, 170, 255));
}
if display.shows("minecraft:stored_enchantments") {
for (text, curse) in e.tooltip(stack.stored_enchantments()) {
lines.push((
push(
&mut lines,
text,
if curse {
rgba(255, 85, 85, 255)
} else {
rgba(170, 170, 170, 255)
},
));
);
}
}
if display.shows("minecraft:enchantments") {
for (text, curse) in e.tooltip(stack.enchantments()) {
lines.push((
push(
&mut lines,
text,
if curse {
rgba(255, 85, 85, 255)
} else {
rgba(170, 170, 170, 255)
},
));
);
}
}
// BannerPatternLayers.addToTooltip: up to six layer names.
if display.shows("minecraft:banner_patterns") {
for layer in horizon_client::banner::tooltip_layers(stack).iter().take(6) {
let key = self.res.stations().banners.layer_key(layer);
lines.push((self.translate(&key, &key), rgba(170, 170, 170, 255)));
push(
&mut lines,
self.translate(&key, &key),
rgba(170, 170, 170, 255),
);
}
}
// ItemLore.addToTooltip: after the enchantments, before the attributes
// (ItemStack.addDetailsToTooltip).
if display.shows("minecraft:lore") {
for text in horizon_client::item_tooltip::lore_texts(stack) {
lines.push((text, u32::MAX));
}
}
// Spawner.appendHoverText: what a spawner or trial spawner item spawns.
@@ -1759,15 +1785,15 @@ impl Game<'_> {
match block_entity_item::spawner_entity(stack) {
Some(id) => {
let key = format!("entity.{}", id.replacen(':', ".", 1));
lines.push((self.translate(&key, &id), gray));
push(&mut lines, self.translate(&key, &id), gray);
}
None => {
lines.push((String::new(), u32::MAX));
push(&mut lines, String::new(), u32::MAX);
let desc1 =
self.translate("block.minecraft.spawner.desc1", "Interact with Spawn Egg:");
lines.push((desc1, gray));
push(&mut lines, desc1, gray);
let desc2 = self.translate("block.minecraft.spawner.desc2", "Sets Mob Type");
lines.push((format!(" {desc2}"), rgba(85, 85, 255, 255)));
push(&mut lines, format!(" {desc2}"), rgba(85, 85, 255, 255));
}
}
}
@@ -1795,11 +1821,13 @@ impl Game<'_> {
horizon_client::attack_strength::BASE_ATTACK_SPEED,
),
];
lines.extend(horizon_client::item_tooltip::attribute_lines(
for (text, c) in horizon_client::item_tooltip::attribute_lines(
&info.modifiers,
Some(player_base),
&translate,
));
) {
push(&mut lines, text, c);
}
}
if display.shows("minecraft:damage")
&& let Some(info) = self.res.items().get(&stack.item)
@@ -1807,14 +1835,15 @@ impl Game<'_> {
&& stack.damage > 0
&& self.options_advanced_tooltips()
{
lines.push((
push(
&mut lines,
format!(
"Durability: {} / {}",
info.max_damage - stack.damage,
info.max_damage
),
u32::MAX,
));
);
}
// ItemStack.OP_NBT_WARNING, for a player with game master permission.
if self.ui.registries.permission >= 2 && block_entity_item::op_warning(stack) {
@@ -1829,7 +1858,11 @@ impl Game<'_> {
"Do not use unless you know the exact contents!",
),
] {
lines.push((self.translate(key, fallback), rgba(255, 85, 85, 255)));
push(
&mut lines,
self.translate(key, fallback),
rgba(255, 85, 85, 255),
);
}
}
lines
@@ -1860,20 +1893,27 @@ impl Game<'_> {
self.settings.file.bool("advancedItemTooltips")
}
/// `TooltipRenderUtil`: dark box with the purple border, first line spaced.
pub(super) fn draw_tooltip_lines(
pub(super) fn draw_tooltip_lines<T: Clone + Into<horizon_client::text::Text>>(
&self,
batch: &mut HudBatch,
lines: &[(String, u32)],
lines: &[(T, u32)],
x: f32,
y: f32,
) {
if lines.is_empty() {
return;
}
let lines: Vec<_> = lines
.iter()
.map(|(text, color)| {
let text: horizon_client::text::Text = text.clone().into();
(text.resolve_legacy(), *color)
})
.collect();
let atlas = &self.gui.atlas;
let w = lines
.iter()
.map(|(t, _)| HudBatch::text_width(atlas, t))
.map(|(t, _)| atlas.font.width_text(t))
.fold(0.0, f32::max);
let h = if lines.len() == 1 {
8.0
@@ -1926,7 +1966,7 @@ impl Game<'_> {
);
let mut ty = y;
for (i, (text, color)) in lines.iter().enumerate() {
batch.text(atlas, text, x, ty, *color, true);
batch.text_styled(atlas, text, x, ty, *color, true);
ty += if i == 0 { 12.0 } else { 10.0 };
}
}
@@ -1943,7 +1983,12 @@ impl Game<'_> {
let lines = self.stack_tooltip(&stack);
self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
} else if let Some(text) = self.crafter_tooltip(slot) {
self.draw_tooltip_lines(batch, &[(text, u32::MAX)], self.cursor.0, self.cursor.1);
self.draw_tooltip_lines(
batch,
&[(horizon_client::text::Text::literal(text), u32::MAX)],
self.cursor.0,
self.cursor.1,
);
}
}
+15 -4
View File
@@ -2182,15 +2182,26 @@ impl Renderer {
)
});
let sorted_indices = arena::Arena::indices(&device, 1 << 16, flat.is_some());
let multi_draw = device
.features()
.contains(wgpu::Features::INDIRECT_FIRST_INSTANCE)
&& adapter
.get_downlevel_capabilities()
.flags
.contains(wgpu::DownlevelFlags::INDIRECT_EXECUTION);
// Without indirect draws (OpenGL before 4.3 without compute shaders)
// even creating an indirect buffer fails; one draw per section reads
// `draws` on the CPU instead.
let indirect = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Section draws"),
size: 1024 * 20,
usage: wgpu::BufferUsages::INDIRECT | wgpu::BufferUsages::COPY_DST,
usage: if multi_draw {
wgpu::BufferUsages::INDIRECT | wgpu::BufferUsages::COPY_DST
} else {
wgpu::BufferUsages::COPY_DST
},
mapped_at_creation: false,
});
let multi_draw = device
.features()
.contains(wgpu::Features::INDIRECT_FIRST_INSTANCE);
// HORIZON_GPU_CULL=0 turns occlusion culling off, `all` culls every
// frame however few its draws and little it hides, `shader` tests in
// the shader as on Metal (A/B checks).
+12 -8
View File
@@ -385,14 +385,18 @@ impl Graphics {
if self.workers > 0 {
return self.workers as usize;
}
// The pool never takes every core. The render thread and the
// integrated server thread (single player runs on its own server
// thread by default) each need one, and a pool that fills every core
// starves them: the frame rate collapses to single digits while the
// workers saturate the CPU and its memory bandwidth.
// Vanilla's count (Util.maxAllowedExecutorThreads: every core but
// one). The pool never takes every core: a pool that fills them all
// starves the render and server threads, and the frame rate collapses
// while the workers saturate the CPU and its memory bandwidth. One
// core left is enough with the workers behind those two threads
// (horizon_threads: niced on Linux; on macOS and Windows the server
// and render threads are raised above four cores, where vanilla
// raises its server thread). Leaving two idled a core of a 4-thread
// laptop while a world loaded: a teleport there took ~15% longer.
let auto = std::thread::available_parallelism()
.map_or(2, |n| n.get())
.saturating_sub(2)
.saturating_sub(1)
.max(1);
if self.battery { auto.div_ceil(2) } else { auto }
}
@@ -555,11 +559,11 @@ mod tests {
}
#[test]
fn automatic_workers_leave_cores_for_the_render_and_server_threads() {
fn automatic_workers_leave_a_core_as_vanilla_does() {
let g = Graphics::default();
assert_eq!(g.workers, 0);
let cores = std::thread::available_parallelism().map_or(2, |n| n.get());
assert_eq!(g.worker_threads(), cores.saturating_sub(2).max(1));
assert_eq!(g.worker_threads(), cores.saturating_sub(1).max(1));
// A manual count is the player's own choice, kept as asked.
let manual = Graphics { workers: 3, ..g };
assert_eq!(manual.worker_threads(), 3);
+31
View File
@@ -87,6 +87,14 @@ fn components_tag(s: &Stack, builtin: &Builtin, synced: &Dynamic) -> Option<Tag>
("pages", Tag::List(b.pages.clone())),
("resolved", Tag::Byte(i8::from(b.resolved))),
]),
Component::Fireworks { flight, explosions } => Tag::compound([
("flight_duration", Tag::Byte(*flight as i8)),
(
"explosions",
Tag::List(explosions.iter().map(fireworks_tag).collect()),
),
]),
Component::FireworkExplosion(e) => fireworks_tag(e),
Component::Enchantments(list) => {
// Enchantments are a synced (data-driven) registry: the
// ids index the server's RegistryData, not the builtin
@@ -178,6 +186,29 @@ fn components_tag(s: &Stack, builtin: &Builtin, synced: &Dynamic) -> Option<Tag>
Some(Tag::compound(entries))
}
/// A wire `FireworkExplosion` as vanilla's NBT codec writes it
/// (`FireworkExplosion.Shape`'s BY_ID order).
fn fireworks_tag(e: &horizon_protocol::item::FireworkExplosion) -> Tag {
const SHAPES: [&str; 5] = ["small_ball", "large_ball", "star", "creeper", "burst"];
let ints = |c: &[i32]| Tag::List(c.iter().map(|c| Tag::Int(*c)).collect());
Tag::compound([
(
"shape",
Tag::String(
SHAPES
.get(e.shape.max(0) as usize)
.copied()
.unwrap_or("small_ball")
.into(),
),
),
("colors", ints(&e.colors)),
("fade_colors", ints(&e.fade)),
("has_trail", Tag::Byte(i8::from(e.trail))),
("has_twinkle", Tag::Byte(i8::from(e.twinkle))),
])
}
/// A `minecraft:profile` component as the item data JSON the model reads.
fn profile_tag(p: &Profile) -> Tag {
let properties = p
+50 -6
View File
@@ -487,23 +487,31 @@ pub fn lines(
) -> Vec<String> {
let mut lines = vec![name_line(stack, translate)];
if TooltipDisplay::for_stack(stack).shows("minecraft:lore") {
lines.extend(lore_lines(stack));
lines.extend(lore_texts(stack).into_iter().map(|t| t.plain()));
}
lines.extend(detail_lines(stack, info, translate));
lines
}
/// `LoreComponent.addToTooltip`: the saved component lines, as plain text.
fn lore_lines(stack: &ItemStack) -> Vec<String> {
/// `ItemLore.addToTooltip`: each lore component under dark purple italic, with
/// the component's own styles (including explicit `false`) winning on children.
pub fn lore_texts(stack: &ItemStack) -> Vec<crate::text::Text> {
let Some(Tag::List(entries)) = stack.data().and_then(|d| d.other("minecraft:lore")) else {
return Vec::new();
};
let base = crate::text::Style {
color: Some(crate::text::DARK_PURPLE),
italic: Some(true),
..Default::default()
};
entries
.iter()
.map(|entry| {
let json = crate::scoreboard::tag_to_json(entry);
let text = crate::component::to_text(&json, &mut |s| crate::text::Text::literal(s));
text.plain()
let mut text = crate::entity_nbt::name_text(entry).resolve_legacy();
for span in &mut text.spans {
span.style.inherit(&base);
}
text
})
.collect()
}
@@ -512,6 +520,42 @@ fn lore_lines(stack: &ItemStack) -> Vec<String> {
mod tests {
use super::*;
#[test]
fn lore_keeps_component_colours_under_dark_purple_italic() {
let mut stack = ItemStack::new("minecraft:stick", 1);
stack.data_mut().set_other(
"minecraft:lore",
Some(Tag::List(vec![
Tag::compound([
("text", Tag::String("Gold line".into())),
("color", Tag::String("gold".into())),
("italic", Tag::Byte(0)),
]),
Tag::String("Plain lore".into()),
])),
);
let lines = lore_texts(&stack);
assert_eq!(lines.len(), 2);
assert_eq!(lines[0].plain(), "Gold line");
assert_eq!(lines[0].spans[0].style.color, Some(crate::text::GOLD));
assert!(!lines[0].spans[0].style.is_italic());
assert_eq!(lines[1].plain(), "Plain lore");
assert_eq!(
lines[1].spans[0].style.color,
Some(crate::text::DARK_PURPLE)
);
assert!(lines[1].spans[0].style.is_italic());
assert!(TooltipDisplay::for_stack(&stack).shows("minecraft:lore"));
stack.data_mut().set_other(
"minecraft:tooltip_display",
Some(Tag::compound([(
"hidden_components",
Tag::List(vec![Tag::String("minecraft:lore".into())]),
)])),
);
assert!(!TooltipDisplay::for_stack(&stack).shows("minecraft:lore"));
}
#[test]
fn tooltip_display_controls_name_and_component_visibility() {
let mut stack = ItemStack::new("minecraft:diamond_sword", 1);
+38
View File
@@ -445,6 +445,43 @@ impl Guest {
}
}
/// `FireworkExplosion.STREAM_CODEC`'s fields (`Shape` by its BY_ID order).
fn network_explosion(
e: &crate::particles::FireworkExplosion,
) -> horizon_protocol::item::FireworkExplosion {
horizon_protocol::item::FireworkExplosion {
shape: e.shape as i32,
colors: e.colors.iter().map(|c| *c as i32).collect(),
fade: e.fade.iter().map(|c| *c as i32).collect(),
trail: e.trail,
twinkle: e.twinkle,
}
}
/// A rocket's `minecraft:fireworks` and a star's `minecraft:firework_explosion`.
pub fn firework_components(
d: &crate::item_data::StackData,
) -> Vec<(i32, horizon_protocol::item::Component)> {
use horizon_protocol::item::{Component, component};
let mut out = Vec::new();
if let Some((flight, explosions)) = &d.fireworks {
out.push((
component::FIREWORKS,
Component::Fireworks {
flight: i32::from(*flight),
explosions: explosions.iter().map(network_explosion).collect(),
},
));
}
if let Some(e) = &d.firework_explosion {
out.push((
component::FIREWORK_EXPLOSION,
Component::FireworkExplosion(network_explosion(e)),
));
}
out
}
/// The off hand slot is conventionally 40 in saves and 45 in the menu.
/// `synced_id` names an entry of a synced registry (enchantments, trim
/// materials and patterns) by its id on this connection.
@@ -535,6 +572,7 @@ fn network_stack(
horizon_protocol::item::Component::Int(i32::from(mode)),
));
}
stack.added.extend(firework_components(d));
// PotionContents: the potion by its `minecraft:potion` id.
if let Some(id) = (d.potion.as_deref()).and_then(|potion| {
synced_id(
+6
View File
@@ -7,6 +7,7 @@
//! behaviour, models, animations and sounds.
pub mod ai;
pub mod allay;
pub mod ambient;
pub mod anim;
pub mod animal_brain;
pub mod aquatic;
@@ -1646,6 +1647,11 @@ impl Mob {
pub fn species(&self) -> Species {
self.kind.species
}
/// `Mob.getMainArm`: the left arm when the mob is left-handed, a player's
/// `mainHand` option for a player mob.
pub fn main_arm_left(&self) -> bool {
self.extra.main_hand_left || self.extra.left_handed
}
/// The player hit this mob (melee): LivingEntity.hurt with knockback.
pub fn hurt(
&mut self,
+55
View File
@@ -0,0 +1,55 @@
//! What a client's copy of a mob makes on its own, in the
//! `level().isClientSide()` branches of `aiStep`: nothing of it reaches the
//! server or goes over the network, so the window's game runs it, whether it
//! simulates the world (in-frame) or follows a server.
use super::{Mob, MobEvent, Species};
use horizon_core::random::RandomSource;
/// `Enderman.aiStep`, `Endermite.aiStep` and `Blaze.aiStep` on a client:
/// two portal particles a tick around endermen and endermites; two large
/// smoke particles a tick around blazes and, one tick in 24, their burn.
pub fn client_ai_step(m: &mut Mob, out: &mut Vec<MobEvent>) {
let species = m.species();
if !matches!(
species,
Species::Enderman | Species::Endermite | Species::Blaze
) {
return;
}
let (w, h) = (f64::from(m.width()), f64::from(m.height()));
let p = m.position;
if species == Species::Blaze && m.random.next_int_bound(24).unwrap_or(1) == 0 && !m.silent {
let volume = 1.0 + m.random.next_float();
let pitch = m.random.next_float() * 0.7 + 0.3;
out.push(MobEvent::LocalSound(
"entity.blaze.burn",
[p.x + 0.5, p.y + 0.5, p.z + 0.5],
volume,
pitch,
));
}
for _ in 0..2 {
// Entity.getRandomX(0.5), getRandomY(), getRandomZ(0.5).
let r = &mut m.random;
let x = p.x + w * (2.0 * r.next_double() - 1.0) * 0.5;
let y = p.y + h * r.next_double();
let z = p.z + w * (2.0 * r.next_double() - 1.0) * 0.5;
let (kind, at, velocity) = if species == Species::Blaze {
("large_smoke", [x, y, z], [0.0; 3])
} else {
// The enderman's are a quarter block lower.
let y = if species == Species::Enderman {
y - 0.25
} else {
y
};
let velocity = [
(r.next_double() - 0.5) * 2.0,
-r.next_double(),
(r.next_double() - 0.5) * 2.0,
];
("portal", [x, y, z], velocity)
};
out.push(MobEvent::ParticleAt { kind, at, velocity });
}
}
+2 -2
View File
@@ -642,7 +642,7 @@ pub fn setup(
} else {
ArmPose::Empty
};
let mut h = if m.extra.main_hand_left {
let mut h = if m.main_arm_left() {
Hum::new(ArmPose::Empty, main)
} else {
Hum::new(main, ArmPose::Empty)
@@ -693,7 +693,7 @@ pub fn setup(
} else {
ArmPose::Empty
};
let mut h = if m.extra.main_hand_left {
let mut h = if m.main_arm_left() {
Hum::new(off, pose)
} else {
Hum::new(pose, off)
+5
View File
@@ -621,8 +621,10 @@ impl BowAttack {
}
}
}
/// `LivingEntity.isHolding(Items.BOW)`: the bow can sit in either hand.
fn holding_bow(m: &Mob) -> bool {
m.extra.main_hand.as_deref() == Some("minecraft:bow")
|| m.extra.off_hand.as_deref() == Some("minecraft:bow")
}
impl Goal for BowAttack {
goal_clone!();
@@ -715,7 +717,10 @@ impl Goal for BowAttack {
} else {
self.attack_time -= 1;
if self.attack_time <= 0 && self.see_time >= -60 {
// mob.startUsingItem(ProjectileUtil.getWeaponHoldingHand):
// the off hand when the main one doesn't hold the bow.
m.extra.using_item = 1;
m.extra.using_off_hand = m.extra.main_hand.as_deref() != Some("minecraft:bow");
}
}
}
+80 -15
View File
@@ -1277,21 +1277,22 @@ impl MotionWorld<'_, '_, '_> {
continue;
}
let powder_snow = name == "minecraft:powder_snow";
let lava_cauldron = name == "minecraft:lava_cauldron";
// getEntityInsideCollisionShape is the whole block but for powder
// snow (its collision shape, the whole block when it is empty),
// filled cauldrons, end portals and tripwires; Horizon doesn't
// follow the player into those last three.
// follow the player into the water and powder snow cauldrons, end
// portals or tripwires yet.
if matches!(
name,
"minecraft:water_cauldron"
| "minecraft:lava_cauldron"
| "minecraft:powder_snow_cauldron"
| "minecraft:end_portal"
| "minecraft:tripwire"
) {
continue;
}
let inside = !powder_snow || {
let inside = !(powder_snow || lava_cauldron) || {
let context = self.context();
let binding = self.collision_binding;
let shape = binding.collision_shape_with_context(
@@ -1300,22 +1301,30 @@ impl MotionWorld<'_, '_, '_> {
p,
&context,
)?;
shape.is_empty() || shape.boxes() == VoxelShape::block().boxes() || {
let boxes: Vec<_> = shape
.moved(Vec3::new(f64::from(p.x), f64::from(p.y), f64::from(p.z)))
.boxes()
.to_vec();
self.player
.dimensions
.bounds(from)
.collided_along_vector(to.subtract(from), &boxes)
}
powder_snow && (shape.is_empty() || shape.boxes() == VoxelShape::block().boxes())
|| {
let offset = Vec3::new(f64::from(p.x), f64::from(p.y), f64::from(p.z));
let mut boxes: Vec<_> = shape.moved(offset).boxes().to_vec();
if lava_cauldron {
// LavaCauldronBlock.FILLED_SHAPE: the cauldron's
// shape or its lava, Block.column(12, 4, 15).
boxes.push(Aabb::new(
Vec3::new(2.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0).add_vector(offset),
Vec3::new(14.0 / 16.0, 15.0 / 16.0, 14.0 / 16.0).add_vector(offset),
));
}
self.player
.dimensions
.bounds(from)
.collided_along_vector(to.subtract(from), &boxes)
}
};
if inside && visited.insert(p.pack()) {
// CactusBlock and CampfireBlock.entityInside (`entity.hurt`),
// and WitherRoseBlock's server-side effect.
// CactusBlock, CampfireBlock and LavaCauldronBlock.entityInside
// (`entity.hurt`), and WitherRoseBlock's server-side effect.
match name {
"minecraft:cactus" => self.environment_hurt("minecraft:cactus", 1.0)?,
"minecraft:lava_cauldron" => self.lava_cauldron_inside()?,
"minecraft:wither_rose" => {
self.player.events.push(MotionEvent::InsideWitherRose);
}
@@ -1378,6 +1387,14 @@ impl MotionWorld<'_, '_, '_> {
}
self.player.impulse.try_reset();
}
/// `LavaCauldronBlock.entityInside`: CLEAR_FREEZE, LAVA_IGNITE
/// (`Entity.lavaIgnite`, 15 seconds) and then `Entity.lavaHurt` (lava,
/// 4), as ordinary lava does to the player.
fn lava_cauldron_inside(&mut self) -> Result<()> {
self.player.ticks_frozen = 0;
self.player.ignite_for_ticks(300)?;
self.environment_hurt("minecraft:lava", 4.0)
}
/// `WebBlock.entityInside`: stuck at a quarter of the speed, half with
/// the Weaving effect.
fn web_inside(&mut self) {
@@ -2392,6 +2409,54 @@ mod tests {
assert_eq!(player.position.y, 4.0);
}
#[test]
fn a_lava_cauldron_burns_the_player_inside_it() {
let root = std::env::var_os("HORIZON_TEST_RESOURCES")
.map(std::path::PathBuf::from)
.unwrap_or_else(|| {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../.local/horizon-client")
.join(horizon_core::VANILLA_VERSION)
});
if !root.join("movement-properties.nbt").exists() {
return;
}
let assets = Assets::load(&root).unwrap();
let render = assets.reload(&root).unwrap();
let binding = crate::collisions::load(&root, assets.registry).unwrap();
let resources = MotionResources::load(&root, &assets).unwrap();
let mut scene = World::test_scene(&assets, &render, 1).unwrap();
let cauldron = assets
.registry
.default_state("minecraft:lava_cauldron")
.unwrap();
scene.set(20, 4, 20, cauldron).unwrap();
let mut tick = |player: &mut PlayerMotion| {
MotionWorld {
entities: &[],
player,
scene: &mut scene,
assets: &assets,
collision_binding: &binding,
resources: &resources,
}
.controlled_simulation_tick(Default::default())
.unwrap();
};
// Standing on the cauldron's floor, in its lava.
let mut player = PlayerMotion::new(&resources, Vec3::new(20.5, 4.25, 20.5)).unwrap();
player.ticks_frozen = 40;
tick(&mut player);
assert!(player.health.health() < 20.0, "the lava must hurt");
assert!(player.fire_ticks > 0, "the lava must ignite");
assert_eq!(player.ticks_frozen, 0);
// Beside it, nothing.
let mut away = PlayerMotion::new(&resources, Vec3::new(22.5, 4.0, 20.5)).unwrap();
tick(&mut away);
assert_eq!(away.health.health(), 20.0);
assert!(away.fire_ticks <= 0);
}
#[test]
fn standing_in_a_wither_rose_asks_for_its_effect() {
let root = std::env::var_os("HORIZON_TEST_RESOURCES")
+21 -8
View File
@@ -106,14 +106,16 @@ enum Palette {
List(Vec<u32>),
Global,
}
/// `n / per_word` for `n < 4096` by multiplication: `MAGIC[bits] = ceil(2^20 /
/// (64 / bits))` is exact while `n * (64 / bits) < 2^20`.
const MAGIC: [u32; 33] = {
let mut table = [0; 33];
/// `n / per_word` for `n < 4096` by multiplication: `MAGIC[bits].0 = ceil(2^20 /
/// (64 / bits))` is exact while `n * (64 / bits) < 2^20`. `.1` is the bits a
/// word's entries fill (`64 / bits * bits`): looked up, not divided for, as a
/// division by a run-time value costs tens of cycles on x86.
const MAGIC: [(u32, u32); 33] = {
let mut table = [(0, 0); 33];
let mut bits = 1;
while bits <= 32 {
let per = 64 / bits as u32;
table[bits] = (1u32 << 20).div_ceil(per);
table[bits] = ((1u32 << 20).div_ceil(per), per * bits as u32);
bits += 1;
}
table
@@ -134,9 +136,9 @@ fn locate(bits: u8, i: usize) -> (usize, u32) {
let word = i >> (6 - log);
(word, ((i & ((64 >> log) - 1)) as u32) << log)
} else {
let per = 64 / u32::from(bits);
let word = (i as u32 * MAGIC[usize::from(bits)]) >> 20;
(word as usize, (i as u32 - word * per) * u32::from(bits))
let (magic, filled) = MAGIC[usize::from(bits)];
let word = (i as u32 * magic) >> 20;
(word as usize, i as u32 * u32::from(bits) - word * filled)
}
}
#[inline]
@@ -720,6 +722,17 @@ mod tests {
use super::*;
const BLOCKS: Strategy = Strategy::blocks(16);
const BIOMES: Strategy = Strategy::biomes(7);
#[test]
fn locate_matches_division_for_every_width() {
for bits in 1..=32u8 {
let per = 64 / usize::from(bits);
for i in 0..4096 {
let expected = (i / per, ((i % per) * usize::from(bits)) as u32);
assert_eq!(locate(bits, i), expected, "{bits} bits, entry {i}");
}
}
}
fn rng(state: &mut u64) -> u64 {
*state ^= *state << 13;
*state ^= *state >> 7;
+6 -1
View File
@@ -7,10 +7,15 @@ rust-version = "1.85"
description = "The DualSense's output over HID: rumble, adaptive triggers, lightbar and player LEDs"
[dependencies]
log = "0.4"
# hidapi has no iOS backend and its build script stops there, so the output's two
# dependencies go only to the targets that have one. On iOS the crate builds without
# output: the Link never opens.
[target.'cfg(not(target_os = "ios"))'.dependencies]
# MIT; bundles the C hidapi under its BSD licence option. hidraw on Linux (libudev, as gilrs),
# IOKit on macOS (opened shared, so gilrs keeps reading the pad), the HID API on Windows.
hidapi = { version = "2.6.7", default-features = false, features = ["linux-static-hidraw", "macos-shared-device"] }
log = "0.4"
# HD haptics through the pad's own audio device (USB).
cpal = "0.16"
+38 -8
View File
@@ -1,14 +1,21 @@
//! The pad over hidapi, and the thread that writes to it: writes block (a Bluetooth report can
//! take milliseconds), so the game only hands over what the pad should do.
use crate::{
DEFAULT_LIGHTBAR, Output, PLAYER_1, TRIGGER_OFF, TriggerEffect, Vibration, is_dualsense,
};
#[cfg(not(target_os = "ios"))]
use crate::{DEFAULT_LIGHTBAR, Output, PLAYER_1, Vibration, is_dualsense};
use crate::{TRIGGER_OFF, TriggerEffect};
#[cfg(not(target_os = "ios"))]
use hidapi::{BusType, HidApi, HidDevice, HidResult};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
use std::sync::mpsc::{self, RecvTimeoutError, Sender};
#[cfg(not(target_os = "ios"))]
use std::sync::atomic::AtomicBool;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::mpsc::Sender;
#[cfg(not(target_os = "ios"))]
use std::sync::mpsc::{self, RecvTimeoutError};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
use std::time::Duration;
#[cfg(not(target_os = "ios"))]
use std::time::Instant;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Transport {
@@ -17,6 +24,7 @@ pub enum Transport {
}
/// An open DualSense.
#[cfg(not(target_os = "ios"))]
pub struct DualSense {
device: HidDevice,
transport: Transport,
@@ -34,6 +42,7 @@ pub fn bluetooth_allowed() -> bool {
cfg!(target_os = "linux") || std::env::var_os("HORIZON_DUALSENSE_BT").is_some_and(|v| v == "1")
}
#[cfg(not(target_os = "ios"))]
impl DualSense {
/// The first DualSense hidapi lists, USB before Bluetooth (as SDL); `None` when there is
/// none, an error when it can't be opened (Linux without access to its hidraw node).
@@ -119,13 +128,17 @@ impl Default for State {
/// A vibration: the motors' levels (left, right), each for a while, in order.
pub type Rumble = Vec<([u8; 2], Duration)>;
// On iOS no thread reads the queue (the Link never opens); the variants stay so the
// Link's API is the same everywhere.
#[cfg_attr(target_os = "ios", allow(dead_code))]
enum Message {
State(State),
Rumble(Rumble),
Scan,
}
/// The writer thread. Dropping it frees the triggers and stops the motors before it ends.
/// The output thread. Dropping it frees the triggers and stops the motors before it ends; on
/// iOS there is none (no hidapi backend) and the Link stays closed.
pub struct Link {
tx: Option<Sender<Message>>,
/// 0: no pad open, else 1 + the transport.
@@ -135,6 +148,7 @@ pub struct Link {
impl Link {
/// Start the thread and look for a DualSense.
#[cfg(not(target_os = "ios"))]
pub fn start() -> Link {
let (tx, rx) = mpsc::channel();
let open = Arc::new(AtomicU8::new(0));
@@ -151,6 +165,16 @@ impl Link {
}
}
/// No output on iOS: hidapi has no backend there. The Link never opens.
#[cfg(target_os = "ios")]
pub fn start() -> Link {
Link {
tx: None,
open: Arc::new(AtomicU8::new(0)),
thread: None,
}
}
/// The pad's transport while one is open and taking reports.
pub fn open(&self) -> Option<Transport> {
match self.open.load(Ordering::Relaxed) {
@@ -194,8 +218,10 @@ impl Drop for Link {
/// Looks for the pad this many more times, a second apart, after a scan finds nothing (its
/// hidraw node may appear after the controller does).
#[cfg(not(target_os = "ios"))]
const SCAN_TRIES: u32 = 5;
#[cfg(not(target_os = "ios"))]
struct Writer {
api: Option<HidApi>,
pad: Option<DualSense>,
@@ -213,6 +239,7 @@ struct Writer {
next_scan: Instant,
}
#[cfg(not(target_os = "ios"))]
#[derive(Clone, Copy, Debug, PartialEq)]
enum Lights {
/// Over Bluetooth, until the pad's clock says its animation is over (or this long after
@@ -225,11 +252,13 @@ enum Lights {
/// The pad's sensor clock once its Bluetooth connection animation is over (SDL_hidapi_ps5.c's
/// `HIDAPI_DriverPS5_CheckPendingLEDReset`).
#[cfg(not(target_os = "ios"))]
const ANIMATION_OVER: u32 = 10_200_000;
/// The sensor clock of a Bluetooth input report 0x31: its state block starts at byte 2 and
/// holds the clock at 27..31, little-endian (SDL's `PS5StatePacketCommon_t`, the kernel's
/// `dualsense_input_report`).
#[cfg(not(target_os = "ios"))]
fn bluetooth_clock(report: &[u8]) -> Option<u32> {
match report {
[0x31, _, state @ ..] if state.len() >= 31 => {
@@ -239,6 +268,7 @@ fn bluetooth_clock(report: &[u8]) -> Option<u32> {
}
}
#[cfg(not(target_os = "ios"))]
impl Writer {
fn new(open: Arc<AtomicU8>) -> Self {
Writer {
@@ -451,7 +481,7 @@ impl Writer {
}
}
#[cfg(test)]
#[cfg(all(test, not(target_os = "ios")))]
mod tests {
use super::*;
@@ -0,0 +1,20 @@
//! No HD haptics on iOS: they play through the pad's own four-channel audio device over USB,
//! which needs cpal, and cpal is left out there with hidapi (see Cargo.toml).
/// The pad's audio output, playing the queued samples (left, right actuator).
pub struct Haptics;
impl Haptics {
/// The DualSense's audio device, if one is there with its four channels (USB). A phone
/// has none.
pub fn open() -> Option<Haptics> {
None
}
/// Play a vibration (heavy and light levels, 0..1, each for a while), replacing the one
/// playing. Nothing plays on iOS.
pub fn play(&self, _steps: &[(f32, f32, std::time::Duration)]) {}
/// Stop at once. Nothing plays on iOS.
pub fn stop(&self) {}
}
+5
View File
@@ -1,12 +1,17 @@
//! The DualSense's output report, built by hand (docs/controllers.md "DualSense output"): the
//! compatible-vibration motors, the adaptive triggers, the lightbar and the player LEDs. Gilrs
//! keeps reading the pad; this crate only writes to it, over hidapi ([`device`]).
//! On iOS the crate builds without hidapi, which has no backend there: [`Link`] never opens.
//!
//! The byte layout is the one documented by the Linux kernel's `hid-playstation.c` and SDL's
//! `SDL_hidapi_ps5.c`: a 47-byte block shared by both transports, sent as report 0x02 over USB
//! and as report 0x31 over Bluetooth, where a sequence tag leads it and a CRC-32 (seeded with
//! the byte 0xA2) closes it.
pub mod device;
#[cfg(not(target_os = "ios"))]
pub mod haptics;
#[cfg(target_os = "ios")]
#[path = "haptics_stub.rs"]
pub mod haptics;
pub mod trigger;
+99
View File
@@ -39,6 +39,8 @@ pub mod component {
pub const WRITABLE_BOOK_CONTENT: i32 = 56;
pub const WRITTEN_BOOK_CONTENT: i32 = 57;
pub const TRIM: i32 = 58;
pub const FIREWORK_EXPLOSION: i32 = 70;
pub const FIREWORKS: i32 = 71;
}
/// A written book's content (`WrittenBookContent.STREAM_CODEC`): the
@@ -89,6 +91,13 @@ pub enum Component {
WrittenBook(WrittenBook),
/// `ChargedProjectiles`: a loaded crossbow's ammunition, as templates.
Stacks(Vec<Stack>),
/// `Fireworks`: flight duration and its explosions.
Fireworks {
flight: i32,
explosions: Vec<FireworkExplosion>,
},
/// `FireworkExplosion` (a firework star).
FireworkExplosion(FireworkExplosion),
/// `ArmorTrim` naming its material and pattern by registry id (`trim_material`,
/// `trim_pattern`); a trim with a direct holder stays [`Component::Raw`].
Trim {
@@ -100,6 +109,41 @@ pub enum Component {
Raw(Vec<u8>),
}
/// `FireworkExplosion.STREAM_CODEC`: shape id, colors, fade colors, trail
/// and twinkle (FireworkExplosion.Shape's BY_ID order).
#[derive(Clone, Debug, PartialEq)]
pub struct FireworkExplosion {
pub shape: i32,
pub colors: Vec<i32>,
pub fade: Vec<i32>,
pub trail: bool,
pub twinkle: bool,
}
impl FireworkExplosion {
fn read(r: &mut Reader) -> Result<Self> {
let ints = |r: &mut Reader| -> Result<Vec<i32>> {
let n = r.count(1024)?;
(0..n).map(|_| r.i32()).collect()
};
Ok(Self {
shape: r.varint()?,
colors: ints(r)?,
fade: ints(r)?,
trail: r.bool()?,
twinkle: r.bool()?,
})
}
fn write(&self, w: &mut Writer) {
w.varint(self.shape);
w.varint(self.colors.len() as i32);
self.colors.iter().for_each(|c| w.i32(*c));
w.varint(self.fade.len() as i32);
self.fade.iter().for_each(|c| w.i32(*c));
w.bool(self.trail);
w.bool(self.twinkle);
}
}
/// `Filterable.streamCodec`: the raw value, then an optional filtered one
/// (dropped).
fn filterable<T>(r: &mut Reader, value: impl Fn(&mut Reader) -> Result<T>) -> Result<T> {
@@ -260,6 +304,17 @@ impl Component {
hidden: (0..n).map(|_| r.varint()).collect::<Result<_>>()?,
}
}
FIREWORKS => {
let flight = r.varint()?;
let n = r.count(256)?;
Self::Fireworks {
flight,
explosions: (0..n)
.map(|_| FireworkExplosion::read(r))
.collect::<Result<_>>()?,
}
}
FIREWORK_EXPLOSION => Self::FireworkExplosion(FireworkExplosion::read(r)?),
// ArmorTrim.STREAM_CODEC: two holders, each a reference's id plus
// one. A direct material or pattern has no registry name: read past.
TRIM => {
@@ -375,6 +430,12 @@ impl Component {
w.varint(stacks.len() as i32);
stacks.iter().for_each(|s| Template::write(s, w));
}
Self::Fireworks { flight, explosions } => {
w.varint(*flight);
w.varint(explosions.len() as i32);
explosions.iter().for_each(|e| e.write(w));
}
Self::FireworkExplosion(e) => e.write(w),
Self::WritableBook(pages) => {
w.varint(pages.len() as i32);
for page in pages {
@@ -670,6 +731,44 @@ mod tests {
round::<Option<Hashed>, HashedSlot>(None);
}
#[test]
fn fireworks_components_round_trip() {
let mut rocket = Stack::new(42, 1);
rocket.added.push((
component::FIREWORKS,
Component::Fireworks {
flight: 3,
explosions: vec![
FireworkExplosion {
shape: 4,
colors: vec![0xff0000, 0x00ff00],
fade: vec![0x0000ff],
trail: true,
twinkle: false,
},
FireworkExplosion {
shape: 0,
colors: vec![],
fade: vec![],
trail: false,
twinkle: true,
},
],
},
));
rocket.added.push((
component::FIREWORK_EXPLOSION,
Component::FireworkExplosion(FireworkExplosion {
shape: 2,
colors: vec![7],
fade: vec![],
trail: false,
twinkle: false,
}),
));
round::<Option<Stack>, Slot>(Some(rocket));
}
#[test]
fn charged_projectiles_are_item_stack_templates() {
// A crossbow (id 7) loaded with one arrow (id 9): ChargedProjectiles
+4
View File
@@ -49,6 +49,10 @@ A harness can declare, at the top level of its script:
- `PARITY_SHARDS = N`: it runs as `NAME#1` to `NAME#N`, in parallel, each told its share in `PARITY_SHARD=i/N` (`test_datafix_parity.py` upgrades every sixth world). `NAME` on the command line or in `--known-failures` means all of them.
- `PARITY_RUST_SCOPE = [...]`: the Rust sources its probes can reach, as path prefixes. Cargo's dep-info names every file of every crate a probe links, so without it a probe of `horizon-world` depends on all 350 of its files. The runner keeps only the scope's files if the scope is closed: nothing in it names a module, crate-root re-export or macro outside it (`crate::`, `super::`, other workspace crates; comments and strings ignored), declares a module it doesn't cover, or includes source, and nothing outside it implements anything for a type it defines. Crate roots then count without their declarations of other modules, so adding a module elsewhere doesn't rerun the harness. If the scope isn't closed the runner says why and keeps every file. `datafix` depends on 41 files this way, not 354.
### Which harnesses a change affects
`--changed` and `--changed-files` narrow each harness's Rust sources to those its probes can reach (`tools/parity_reach.py`), so a change to one module of `horizon-client` no longer reruns every harness whose probe links the crate. The narrowing works on items, not files: from the probe modules a harness ran (or the binary it built), a free item (fn, type, const, static) is reached when a reached item names it in its own module, through a path or an import of a module that is named, or through a glob import; an inherent impl's methods count one by one once their type is reached and their name is mentioned; a trait impl counts once its trait and its type are reached (std's traits and types always are); a macro counts once invoked. Names match by spelling, so the result only over-approximates. A reached file's parent modules count through their `mod` declarations of it, compared with the merge base; Rust files a change adds take part, a dependency that was deleted counts as changed, code under `#[cfg(test)]` never counts, and a harness whose probe can't be found keeps every source. Parses are cached by content in `.local/parity-runner/reach-cache.json`. `--by-crate` turns the narrowing off. On the last 20 pull requests merged on 2026-10-25, the harnesses picked went from 2255 to 1061 (2384 to 1592 harness-minutes; game-binary harnesses, which link all of `game/`, set the floor), and every harness a CI parity run failed in the week before was still picked; every source file a sample of probes executed (LLVM `-C instrument-coverage`) is among those kept. Building the graph takes 15 s cold, under a second warm, and up to a minute and a half on a change that reaches many harnesses.
## The gate
`tools/gate.py` is what a branch must pass before it is integrated: the public-tree check, rustfmt, clippy with warnings denied, the Rust and Python tests, then the harnesses whose inputs changed. It stops at the first failure and writes `.local/gate.json`. `--known-failures a,b` lets harnesses that already fail on the integration branch keep failing. `--quick` checks formatting, lints and Python tests only; `--no-parity` skips the harnesses.
+174 -67
View File
@@ -552,7 +552,7 @@ pub fn install(release: &Release, progress: impl Fn(f32)) -> Result<(), String>
fs::write(target.join(COMPLETE), "").map_err(|e| e.to_string())?;
let _ = fs::remove_dir_all(&partial);
reuse_prepared(&root, tag);
complete_packs(tag);
complete_assets(tag);
progress(1.0);
Ok(())
}
@@ -774,13 +774,13 @@ fn reuse_prepared(versions: &Path, tag: &str) -> Option<String> {
/// `reuse_prepared` for every installed version that is not prepared, oldest first, so a
/// version prepared this way can serve the next. Launchers before 0.1.4 installed new versions
/// without it; this catches those up. Returns each (version, where its resources came from).
/// Prepared versions also get the run-time reports (`complete_reports`) and built-in packs
/// (`complete_packs`) they lack.
/// Prepared versions also get the run-time reports (`complete_reports`) and the built-in packs,
/// panorama and sounds (`complete_assets`) they lack.
pub fn reuse_prepared_everywhere() -> Vec<(String, String)> {
let versions = versions_dir();
let reused = reuse_prepared_in(&versions);
for tag in installed_in(&versions) {
complete_packs(&tag);
complete_assets(&tag);
}
reused
}
@@ -856,19 +856,77 @@ fn complete_reports(versions: &Path, tag: &str) -> bool {
complete
}
/// Adds vanilla's built-in Programmer Art and High Contrast packs to a prepared version that
/// lacks them, as `horizon-prepare packs` does: preparers before #1538 left them out, and a new
/// version reuses the resources of the one before (`reuse_prepared`). They come from the player's
/// own Minecraft asset store; without them there, one line in the log, once per launcher run, and
/// the version plays as before. Nothing is prepared again.
pub fn complete_packs(tag: &str) {
complete_packs_in(&versions_dir(), tag, minecraft_folders, &|line| {
/// What a prepared version may lack that the launcher adds without preparing again, each copied
/// from the player's own Minecraft asset store as `horizon-prepare <kind>` does: what the
/// player misses without it, whether the resources lack it, and the copy.
type Completion = (
&'static str,
fn(&Path) -> bool,
fn(&Path, Option<&Path>, horizon_prepare::Log) -> Result<PathBuf, String>,
);
const COMPLETIONS: [Completion; 5] = [
// Preparers before #1538 left the built-in packs out.
(
"Programmer Art and High Contrast",
lacks_packs,
horizon_prepare::packs,
),
// A preparation that could not read the asset store keeps the client jar's 1x1 grey
// placeholders, its English alone, and no sounds (#1867: Prism Launcher installs prepared
// since 0.2.48).
(
"The title panorama",
lacks_panorama,
horizon_prepare::panorama,
),
("The Unifont glyphs", lacks_unifont, horizon_prepare::fonts),
("The languages", lacks_languages, horizon_prepare::languages),
(
"The sounds",
|resources| !sounds_in(resources),
horizon_prepare::sounds,
),
];
fn lacks_unifont(resources: &Path) -> bool {
!resources
.join("assets/minecraft/font/unifont.zip")
.is_file()
}
/// Whether `en_us`, the one language the client jar has, is the only one.
fn lacks_languages(resources: &Path) -> bool {
fs::read_dir(resources.join("assets/minecraft/lang")).map_or(true, |dir| dir.count() <= 1)
}
fn lacks_packs(resources: &Path) -> bool {
horizon_prepare::BUILT_IN_PACKS
.iter()
.any(|pack| !resources.join("assets").join(pack).is_file())
}
/// Whether the first panorama face is missing or the client jar's 1x1 placeholder (a PNG's
/// width is the big-endian word at byte 16, in the IHDR chunk).
fn lacks_panorama(resources: &Path) -> bool {
let face = resources.join("assets/minecraft/textures/gui/title/background/panorama_0.png");
fs::read(face).map_or(true, |png| {
png.get(16..20).is_none_or(|width| width == [0, 0, 0, 1])
})
}
/// Adds what a prepared version lacks of `COMPLETIONS`: a new version reuses the resources of the
/// one before (`reuse_prepared`), which may come from a preparer that left them out. Without them
/// in any Minecraft folder, one line each in the log, once per launcher run, and the version plays
/// as before. Nothing is prepared again.
pub fn complete_assets(tag: &str) {
complete_assets_in(&versions_dir(), tag, minecraft_folders, &|line| {
eprintln!("{line}")
});
}
/// `folders`: where to look, asked only when the version lacks a pack.
fn complete_packs_in(
/// `folders`: where to look, asked only when the version lacks something.
fn complete_assets_in(
versions: &Path,
tag: &str,
folders: impl Fn() -> Vec<PathBuf>,
@@ -879,37 +937,41 @@ fn complete_packs_in(
static TRIED: Mutex<Vec<PathBuf>> = Mutex::new(Vec::new());
let root = versions.join(tag);
let resources = resources_at(&root);
let lacking = || {
resources.join(RESOURCES_MANIFEST).exists()
&& horizon_prepare::BUILT_IN_PACKS
.iter()
.any(|pack| !resources.join("assets").join(pack).is_file())
let lacking = || -> Vec<&Completion> {
if !resources.join(RESOURCES_MANIFEST).exists() {
return Vec::new();
}
COMPLETIONS
.iter()
.filter(|(_, lacks, _)| lacks(&resources))
.collect()
};
if !lacking() {
if lacking().is_empty() {
return;
}
let mut tried = TRIED.lock().unwrap_or_else(|e| e.into_inner());
if tried.contains(&root) || !lacking() {
let lacking = lacking();
if tried.contains(&root) || lacking.is_empty() {
return;
}
tried.push(root.clone());
let mut first_error = None;
for folder in folders() {
let copied = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
horizon_prepare::packs(&root, Some(&folder), &|line| log(line.to_string()))
}))
.unwrap_or_else(|_| Err("the copy stopped unexpectedly".into()));
match copied {
Ok(_) => return,
Err(e) => {
first_error.get_or_insert(e);
}
let folders = folders();
for (what, _, copy) in lacking {
let mut first_error = None;
let added = folders.iter().any(|folder| {
let copied = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
copy(&root, Some(folder), &|line| log(line.to_string()))
}))
.unwrap_or_else(|_| Err("the copy stopped unexpectedly".into()));
copied.map_err(|e| first_error.get_or_insert(e)).is_ok()
});
if !added {
log(format!(
"{tag}: {what} not added: {}",
first_error.unwrap_or_else(|| "no Minecraft folder found".into())
));
}
}
log(format!(
"{tag}: Programmer Art and High Contrast not added: {}",
first_error.unwrap_or_else(|| "no Minecraft folder found".into())
));
}
/// The Minecraft folders whose asset store can hold what a prepared version lacks: the one
@@ -1667,7 +1729,7 @@ pub fn need_prepared(tag: &str) -> Result<(), String> {
/// The client's command, once its resources are prepared.
pub fn client(tag: &str) -> Result<Command, String> {
need_prepared(tag)?;
complete_packs(tag);
complete_assets(tag);
crate::browser::horizon::before_play(&game_home());
let mut cmd = client_command(tag);
// A sign-in that can't be renewed (no network, expired) still lets the game start, offline.
@@ -3148,7 +3210,7 @@ mod tests {
}
#[test]
fn prepared_versions_get_the_built_in_packs_without_preparing_again() {
fn prepared_versions_get_what_the_asset_store_adds_without_preparing_again() {
let dir = std::env::temp_dir().join(format!(
"horizon-launcher-packs-test-{}",
std::process::id()
@@ -3159,17 +3221,42 @@ mod tests {
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, data).unwrap();
};
// An official launcher folder whose asset store holds the two packs.
let packs = horizon_prepare::BUILT_IN_PACKS.map(|name| (name, name.as_bytes()));
// An official launcher folder whose asset store holds the two packs, a panorama (fake
// 2x2 PNG headers), Unifont, a language and the sound list.
let background = "minecraft/textures/gui/title/background";
let png = |n: u8| {
let mut data = b"\x89PNG\r\n\x1a\n\0\0\0\x0dIHDR\0\0\0\x02\0\0\0\x02".to_vec();
data.push(n);
data
};
let mut assets: Vec<(String, Vec<u8>)> = horizon_prepare::BUILT_IN_PACKS
.iter()
.map(|name| (name.to_string(), name.as_bytes().to_vec()))
.collect();
for face in ["0", "1", "2", "3", "4", "5", "overlay"] {
assets.push((
format!("{background}/panorama_{face}.png"),
png(face.as_bytes()[0]),
));
}
for name in [
"minecraft/font/include/unifont.json",
"minecraft/font/unifont.zip",
"pack.mcmeta",
"minecraft/lang/fr_fr.json",
"minecraft/sounds.json",
] {
assets.push((name.into(), format!("{{\"{name}\": 1}}").into_bytes()));
}
let mut index = serde_json::Map::new();
for (name, data) in packs {
for (name, data) in &assets {
let hash = sources::sha1_hex(data);
write(
store.join("assets/objects").join(&hash[..2]).join(&hash),
data,
);
index.insert(
name.into(),
name.clone(),
serde_json::json!({"hash": hash, "size": data.len()}),
);
}
@@ -3184,6 +3271,8 @@ mod tests {
.to_string();
write(store.join("versions/9.9/9.9.json"), manifest.as_bytes());
fs::create_dir_all(&empty).unwrap();
// The client jar's 1x1 placeholder.
let placeholder = b"\x89PNG\r\n\x1a\n\0\0\0\x0dIHDR\0\0\0\x01\0\0\0\x01";
let release = |tag: &str, prepared: bool| {
let root = versions.join(tag);
write(root.join(COMPLETE), b"");
@@ -3191,27 +3280,27 @@ mod tests {
"client_sha1": "c", "server_sha1": "s", "manifest_sha1": sources::sha1_hex(manifest.as_bytes())}});
write(root.join("research-lock.json"), lock.to_string().as_bytes());
if prepared {
write(resources_at(&root).join(RESOURCES_MANIFEST), b"{}");
let resources = resources_at(&root);
write(resources.join(RESOURCES_MANIFEST), b"{}");
write(resources.join("assets/minecraft/lang/en_us.json"), b"{}");
write(
resources_at(&root).join("assets/minecraft/lang/en_us.json"),
b"{}",
resources
.join("assets")
.join(background)
.join("panorama_0.png"),
placeholder,
);
}
};
let lines = std::cell::RefCell::new(Vec::new());
let log = |line: String| lines.borrow_mut().push(line);
let pack = |tag: &str, i: usize| {
fs::read(
resources_at(&versions.join(tag))
.join("assets")
.join(packs[i].0),
)
.ok()
let asset = |tag: &str, name: &str| {
fs::read(resources_at(&versions.join(tag)).join("assets").join(name)).ok()
};
// Resources prepared before the packs were: a folder without an asset store is skipped.
// Resources prepared without any of them: a folder without an asset store is skipped.
release("v0.2.1", true);
complete_packs_in(
complete_assets_in(
&versions,
"v0.2.1",
|| vec![empty.clone(), store.clone()],
@@ -3219,37 +3308,55 @@ mod tests {
);
assert_eq!(
*lines.borrow(),
["Built-in pack assets ready: 2 files, 0.0 MB"]
[
"Built-in pack assets ready: 2 files, 0.0 MB",
"Panorama assets ready: 7 files, 0.0 MB",
"Font assets ready: 2 files, 0.0 MB",
"Language assets ready: 2 files, 0.0 MB",
"Sound assets ready: 1 files, 0.0 MB",
]
);
for (i, (_, data)) in packs.iter().enumerate() {
assert_eq!(pack("v0.2.1", i).as_deref(), Some(*data));
for (name, data) in &assets {
assert_eq!(asset("v0.2.1", name).as_ref(), Some(data), "{name}");
}
// Nothing else is prepared again.
let resources = resources_at(&versions.join("v0.2.1"));
assert_eq!(fs::read(resources.join(RESOURCES_MANIFEST)).unwrap(), b"{}");
assert!(!resources.join("assets/minecraft/sounds.json").exists());
// No asset store anywhere: one line, the version stays prepared, and no second try.
// No asset store anywhere: one line each, the version stays prepared, and no second try.
lines.borrow_mut().clear();
release("v0.2.2", true);
complete_packs_in(&versions, "v0.2.2", || vec![empty.clone()], &log);
complete_packs_in(&versions, "v0.2.2", || vec![empty.clone()], &log);
assert_eq!(lines.borrow().len(), 1);
assert!(
lines.borrow()[0].starts_with("v0.2.2: Programmer Art and High Contrast not added: "),
"{lines:?}"
complete_assets_in(&versions, "v0.2.2", || vec![empty.clone()], &log);
complete_assets_in(&versions, "v0.2.2", || vec![empty.clone()], &log);
let what = lines
.borrow()
.iter()
.map(|line| line.split(" not added: ").next().unwrap().to_string())
.collect::<Vec<_>>();
assert_eq!(
what,
[
"v0.2.2: Programmer Art and High Contrast",
"v0.2.2: The title panorama",
"v0.2.2: The Unifont glyphs",
"v0.2.2: The languages",
"v0.2.2: The sounds",
]
);
assert!(resources_at(&versions.join("v0.2.2"))
.join(RESOURCES_MANIFEST)
.exists());
assert_eq!(pack("v0.2.2", 0), None);
assert_eq!(
asset("v0.2.2", &format!("{background}/panorama_0.png")).as_deref(),
Some(&placeholder[..])
);
// Not prepared, or complete: nothing to do, without even looking for a Minecraft folder.
lines.borrow_mut().clear();
release("v0.2.3", false);
let unasked = || -> Vec<PathBuf> { unreachable!("no folder is needed") };
complete_packs_in(&versions, "v0.2.3", unasked, &log);
complete_packs_in(&versions, "v0.2.1", unasked, &log);
complete_assets_in(&versions, "v0.2.3", unasked, &log);
complete_assets_in(&versions, "v0.2.1", unasked, &log);
assert!(lines.borrow().is_empty());
assert!(!resources_at(&versions.join("v0.2.3")).exists());
let _ = fs::remove_dir_all(&dir);
+4 -3
View File
@@ -831,13 +831,14 @@ impl Launcher {
// A little earlier than the spawn: file times can be coarse.
self.client_started =
Some(std::time::SystemTime::now() - std::time::Duration::from_secs(2));
// The built-in packs the resources lack and the session (a network round trip when the
// token needs renewing) come first; `drain` starts the game when the session arrives.
// What the resources lack (built-in packs, panorama, sounds) and the session (a network
// round trip when the token needs renewing) come first; `drain` starts the game when the
// session arrives.
self.starting_client = Some(tag.clone());
self.play_pressed = Some(Instant::now());
let tx = self.tx.clone();
std::thread::spawn(move || {
backend::complete_packs(&tag);
backend::complete_assets(&tag);
let session = (|| {
let mut accounts = Accounts::load();
let Some((a, secrets)) = account::session_for_launch(
@@ -0,0 +1,58 @@
package dev.horizonlab.parity;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Optional;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.component.ItemLore;
import net.minecraft.world.item.component.TooltipDisplay;
/**
* The styled lines {@code ItemLore.addToTooltip} adds, one stack (SNBT) per input
* line. Each line is its visited spans ({@code text, colour, bold, italic,
* underlined, strikethrough, obfuscated}); lines are separated by {@code ||}.
*/
public final class VanillaLoreTooltipOracle {
public static void main(String[] args) throws Exception {
ParityData.init();
var context = Item.TooltipContext.of(ParityData.lookup);
var output = new ArrayList<String>();
for (String line : Files.readAllLines(Path.of(args[0]))) {
if (line.isBlank() || line.startsWith("#")) continue;
var stack = ParityData.stack(line);
var display = stack.getOrDefault(DataComponents.TOOLTIP_DISPLAY, TooltipDisplay.DEFAULT);
ItemLore lore = stack.get(DataComponents.LORE);
if (lore == null || !display.shows(DataComponents.LORE)) {
output.add("-");
continue;
}
var lines = new ArrayList<String>();
lore.addToTooltip(context, component -> lines.add(spans(component)), TooltipFlag.NORMAL, stack);
output.add(lines.isEmpty() ? "-" : String.join(" || ", lines));
}
Files.write(Path.of(args[1]), output);
}
/** {@code Component.visit} from {@code Style.EMPTY}: the style a tooltip draws. */
static String spans(Component component) {
var out = new StringBuilder();
component.visit((style, text) -> {
if (text.isEmpty()) return Optional.empty();
if (out.length() > 0) out.append(" | ");
int color = style.getColor() == null ? -1 : style.getColor().getValue();
out.append(text).append('\t').append(color).append('\t')
.append(style.isBold()).append('\t')
.append(style.isItalic()).append('\t')
.append(style.isUnderlined()).append('\t')
.append(style.isStrikethrough()).append('\t')
.append(style.isObfuscated());
return Optional.empty();
}, Style.EMPTY);
return out.toString();
}
}
+43 -6
View File
@@ -968,13 +968,15 @@ links = ["#1709"]
[[item]]
id = "opengl-fallback"
title = "OpenGL for old computers"
status = "later"
status = "shipped"
shipped_in = "v0.2.63"
area = "platforms"
plain = "A way to play on old PCs and Linux machines that have no Vulkan or DirectX 12, through OpenGL."
dev = """Players on older hardware ask whether Vulkan is the only way. Horizon draws with wgpu, which has an OpenGL \
backend; what is needed is to see which features hold up on it, how fast it runs, and a launcher switch like \
the DirectX 12 fallback that already ships."""
links = ["docs/graphics.md"]
plain = """Horizon now starts on old PCs whose graphics card has no Vulkan or DirectX 12 driver: it switches to OpenGL \
by itself, and Options > Graphics can pick OpenGL by hand."""
dev = """wgpu's GL backend (WGL on Windows, EGL on Linux) as the last fallback after Vulkan and DirectX 12 (#1917), \
sprite arrays and CPU sprite animation for drivers without compute (#1920), no indirect draws below GL 4.3 \
(#1936). Minimum OpenGL 3.3 core. Shader packs don't run on it yet."""
links = ["#1917", "#1920", "#1936"]
[[item]]
id = "phones-and-browser"
@@ -1187,3 +1189,38 @@ plain = """The launcher's Servers page creates, starts, backs up and updates a H
invites friends."""
dev = """The launcher's Servers page (#1026) over `horizon-server` (docs/hosting.md)."""
links = ["#1026", "docs/hosting.md"]
[[item]]
id = "cli-server-install"
title = "A server you install with one command"
status = "next"
area = "servers"
plain = """Install and run a Horizon dedicated server from a terminal with a single command, no launcher needed, for \
VPSes and headless machines."""
dev = """A player's idea (#horizon-ideas). `horizon-server` already runs headless; left: an install script \
(Linux/macOS shell, Windows PowerShell) that fetches the signed release, checks it, asks for or points at the \
player's own game files and starts the server, documented in docs/hosting.md."""
links = ["docs/hosting.md"]
[[item]]
id = "launcher-look-menus"
title = "Game menus in the launcher's style"
status = "later"
area = "extras"
plain = """An optional look for the game's own menus and options that matches the Horizon launcher. Off by default: \
the vanilla menus stay as they are."""
dev = """A player's idea (#horizon-ideas). A built-in resource pack or a Horizon setting that restyles the title, \
pause and options screens with the launcher's design tokens (crates/horizon-theme), following \
docs/design-system.md; off by default because it changes the vanilla look."""
links = ["docs/design-system.md"]
[[item]]
id = "linux-packages"
title = "AppImage and a Nix flake"
status = "later"
area = "platforms"
plain = """Install Horizon on any Linux distribution as an AppImage, and on NixOS through a Nix flake."""
dev = """A player's idea (#horizon-ideas). An AppImage of the launcher and game from the release workflow, and a \
flake.nix packaging the release binaries with their runtime libraries (Vulkan loader, X11/Wayland, ALSA), \
the way horizon-parity's NixOS setup runs them today."""
links = []
+111
View File
@@ -0,0 +1,111 @@
"""tools/parity_reach.py: which workspace sources a harness's probe can reach."""
from pathlib import Path
import tempfile
import textwrap
import unittest
from tools import parity, parity_reach
PROBE = [["example", "world_probe", "release", ["-p", "a"], True]]
def workspace(root, files):
for path, text in files.items():
(root / path).parent.mkdir(parents=True, exist_ok=True)
(root / path).write_text(textwrap.dedent(text), encoding="utf-8")
class ReachTests(unittest.TestCase):
def setUp(self):
self.directory = tempfile.TemporaryDirectory()
self.root = Path(self.directory.name)
workspace(self.root, {
"crates/a/Cargo.toml": '[package]\nname = "a"\n\n[[example]]\nname = "world_probe"\npath = "examples/probe/main.rs"\n',
"crates/a/src/lib.rs": """
pub mod used;
pub mod unused;
mod methods;
mod display;
mod table;
#[cfg(test)]
mod tests;
""",
"crates/a/src/used.rs": """
pub struct Thing;
pub fn helper() -> u32 { 1 }
""",
# Same names as used.rs, but nothing names this module.
"crates/a/src/unused.rs": "pub struct Thing;\npub fn helper() -> u32 { 2 }\n",
"crates/a/src/methods.rs": """
use crate::used::Thing;
impl Thing {
pub fn method(&self) -> u32 { 3 }
pub fn never_called(&self) -> u32 { 4 }
}
""",
"crates/a/src/display.rs": """
impl std::fmt::Display for crate::used::Thing {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "thing") }
}
""",
"crates/a/src/table.rs": """
macro_rules! table { () => { pub struct Table; }; }
table!();
""",
"crates/a/src/tests.rs": "use crate::used::Thing;\nimpl Thing { pub fn method_in_tests(&self) {} }\n",
"crates/a/examples/probe/main.rs": "mod dispatch;\nmod p;\n",
"crates/a/examples/probe/dispatch.rs": "pub fn args() {}\n",
"crates/a/examples/probe/p.rs": """
use a::used::{helper, Thing};
pub fn main() { println!("{} {} {}", helper(), Thing.method(), Thing); }
""",
})
self.dependencies = sorted(str(path.relative_to(self.root)) for path in self.root.rglob("*") if path.is_file())
self.dependencies.remove("crates/a/examples/probe/main.rs") # parity.only_probes_run leaves it out
def tearDown(self):
self.directory.cleanup()
def narrowed(self, changed=(), artifacts=PROBE):
graph = parity_reach.SourceGraph(self.root, parity.rust_code)
return parity_reach.narrow(self.dependencies, artifacts, graph, set(changed))
def test_keeps_what_the_probe_reaches(self):
kept = {path for path, (whole, _) in self.narrowed().items() if whole}
self.assertIn("crates/a/src/used.rs", kept)
self.assertIn("crates/a/src/methods.rs", kept) # Thing.method()
self.assertIn("crates/a/src/display.rs", kept) # std's Display for a reached type
self.assertIn("crates/a/Cargo.toml", kept)
self.assertNotIn("crates/a/src/unused.rs", kept) # its names match, but nothing names its module
self.assertNotIn("crates/a/src/table.rs", kept)
self.assertNotIn("crates/a/src/tests.rs", kept) # compiled into tests only
def test_module_parents_count_through_their_declarations(self):
self.assertEqual(self.narrowed()["crates/a/src/lib.rs"], (False, {"used", "methods", "display"}))
text = (self.root / "crates/a/src/lib.rs").read_text()
self.assertEqual(parity_reach.declarations(text, {"used"}, False), "pub mod used;")
self.assertEqual(parity_reach.declarations(text, {"tests"}, False), "#[cfg(test)]\nmod tests;")
# Adding or dropping a module beside the reached ones changes neither view.
other = text.replace("mod table;", "mod table2;\nmod more;")
self.assertEqual(parity_reach.declarations(other, {"used"}, False), "pub mod used;")
self.assertEqual(parity_reach.declarations(other, {"used"}, True), parity_reach.declarations(text, {"used"}, True))
self.assertNotEqual(parity_reach.declarations(text + "pub fn f() {}\n", {"used"}, True),
parity_reach.declarations(text, {"used"}, True))
def test_a_new_file_counts_once_it_is_reached(self):
(self.root / "crates/a/src/lib.rs").write_text("pub mod used;\nmod extra;\n")
workspace(self.root, {"crates/a/src/extra.rs": "impl crate::used::Thing { pub fn method(&self) -> u32 { 5 } }\n"})
self.assertIn("crates/a/src/extra.rs", self.narrowed(changed={"crates/a/src/extra.rs"}))
def test_a_deleted_dependency_counts(self):
(self.root / "crates/a/src/unused.rs").unlink()
self.assertIn("crates/a/src/unused.rs", self.narrowed())
def test_unknown_probes_keep_everything(self):
self.assertEqual(set(self.narrowed(artifacts=[])), set(self.dependencies))
self.assertEqual(set(self.narrowed(artifacts=[["example", "missing", "release", ["-p", "a"], True]])),
set(self.dependencies))
if __name__ == "__main__":
unittest.main()
+42 -7
View File
@@ -45,6 +45,11 @@ import subprocess
import sys
import time
try:
from . import parity_reach
except ImportError:
import parity_reach
ROOT = Path(__file__).resolve().parents[1]
LOCAL = ROOT / ".local"
STATE_DIR = LOCAL / "parity-runner"
@@ -1224,20 +1229,44 @@ def changed_harnesses(found, records, ref):
return found_changed
def affected_harnesses(found, records, changed):
def affected_harnesses(found, records, changed, reach=True, base=None):
"""Harnesses the changed files can affect: their script or a recorded dependency (here, or in the shared
dependency lists) changed, a file every harness depends on changed, or nothing is recorded about them yet."""
dependency lists) changed, a file every harness depends on changed, or nothing is recorded about them yet.
With reach, a harness's workspace Rust sources count only if its probes can reach them (tools/parity_reach.py);
a harness whose probes can't be placed keeps them all. A changed file counts unless, compared with base (a
commit), only declarations of modules the harness doesn't reach changed (tools/parity_reach.py)."""
if changed & SHARED_INPUTS:
return set(found)
graph = None
if reach and any(path.startswith("crates/") for path in changed):
cache_path = STATE_DIR / "reach-cache.json"
try:
cache = json.loads(cache_path.read_text(encoding="utf-8"))
except (OSError, ValueError):
cache = {}
graph = parity_reach.SourceGraph(ROOT, rust_code, cache)
affected = set()
for name, script in found.items():
dependencies = records.get(name, {}).get("dependencies")
record = records.get(name) or {}
dependencies = record.get("dependencies")
template = SHARED / "dependencies" / f"{name}.json"
if dependencies is None and template.exists():
dependencies = json.loads(template.read_text(encoding="utf-8")).get("dependencies")
if dependencies is None or Path(script).relative_to(ROOT).as_posix() in changed or \
any(path.split(KEEP)[0] in changed for path in dependencies):
record = json.loads(template.read_text(encoding="utf-8"))
dependencies = record.get("dependencies")
if dependencies is None or Path(script).relative_to(ROOT).as_posix() in changed:
affected.add(name)
continue
plain = {path.split(KEEP)[0] for path in dependencies}
if graph is not None and plain & changed:
narrowed = parity_reach.narrow(dependencies, recorded_artifacts(record), graph, changed)
if any(path.split(KEEP)[0] in changed and (children is None or parity_reach.declarations_changed(
ROOT, path.split(KEEP)[0], children, base, whole)) for path, (whole, children) in narrowed.items()):
affected.add(name)
elif plain & changed:
affected.add(name)
if graph is not None:
STATE_DIR.mkdir(parents=True, exist_ok=True)
cache_path.write_text(json.dumps(graph.saved_cache()), encoding="utf-8")
return affected
@@ -1280,6 +1309,8 @@ def main():
parser.add_argument("--flake-rounds", type=int, default=3, help="runs required by --flake-check (default 3)")
parser.add_argument("--changed", metavar="REF", help="only harnesses the changes since the merge base with REF can "
"affect (and that have not passed with their current inputs): the pull-request check")
parser.add_argument("--by-crate", action="store_true", help="with --changed: count every Rust source a probe "
"links, not only those it can reach (tools/parity_reach.py)")
parser.add_argument("--changed-files", metavar="FILE", help="like --changed, for the changed paths listed in FILE, "
"one per line: for a copy without Git history (tools/pr_ready.py on another machine)")
args = parser.parse_args()
@@ -1316,7 +1347,11 @@ def main():
changed, since = changed_files(args.changed), f" since the merge base with {args.changed}"
if changed is None:
parser.error(f"no merge base with {args.changed}")
affected = affected_harnesses(found, records, changed)
base = None
if args.changed:
merge_base = subprocess.run(["git", "merge-base", "HEAD", args.changed], cwd=ROOT, capture_output=True, text=True)
base = merge_base.stdout.strip() or None
affected = affected_harnesses(found, records, changed, reach=not args.by_crate, base=base)
chosen = [name for name in found if name in affected and stale(name, state, hasher, environment)]
print(f"{len(changed)} files changed{since}: {len(affected)} harnesses "
f"depend on them, {len(chosen)} without a pass for their current inputs.", flush=True)
+768
View File
@@ -0,0 +1,768 @@
"""Which of a harness's Rust sources its probes can reach, for `parity.py --changed`.
Cargo's dep-info names every source file of every crate a probe links, so a change anywhere in horizon-client
selected every harness whose probe links horizon-client. This module narrows a harness's recorded dependencies to
the files holding an item its entry points (the probe modules it ran, the binaries it built) can reach, over a graph
of the workspace's current sources whose nodes are items:
- everything in an entry is reached, and so is everything in a Rust file the module tree doesn't place (an
`include!`d source);
- a module is open when it is a crate root, holds reached code, or is named in a path (`name::`, `use ...name`) by
reached code while its parent is open;
- a free item (fn, struct, enum, union, trait, type, const, static, an item-level macro call's items) is reached when
reached code in its own module or below it mentions its name, or when its module is open and reached code names
it in a path or an import, or when its module is glob-imported (`use m::*`) and its name is mentioned;
- an inherent impl's methods and constants are reached one by one, by name, once their type is; a trait impl once
its trait and its type are (a std or outside name always is, a generic parameter needs nothing); a macro once
invoked; a `pub use` like a free item named by everything it imports;
- "a crate's reached code" covers the crates it names (`horizon_world::`), directly or through one that does, and
code under `#[cfg(test)]` or `#[test]` is left out (probes are never test builds).
Names match by spelling, so a file can be kept for a name it never meant: the graph only over-approximates. A
reached file's parent modules count through their `mod` declarations of it (parity.py compares those with the merge
base). Everything that isn't a workspace Rust source stays a dependency, except a crate's non-Rust files (shaders,
assets, fonts) that only unreached sources name; a dependency that no longer exists counts as changed, and Rust files
a change adds take part.
"""
import hashlib
from pathlib import Path, PurePosixPath
import re
import subprocess
IDENT = r"[A-Za-z_]\w*"
WORD = re.compile(rf"\b({IDENT})\b")
MOD_DECLARATION = re.compile(rf"\bmod\s+({IDENT})\s*;")
INLINE_MOD = re.compile(rf"\bmod\s+({IDENT})\s*\{{")
PATH_ATTRIBUTE = re.compile(r'#\[\s*path\s*=\s*"([^"]+)"\s*\]\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+\w+\s*;$')
BRACE = re.compile(r"[{}]")
TEST_ONLY = re.compile(r"#\[\s*(?:cfg\s*\(\s*test\s*\)|test)\s*\]")
TEST_MODULE = re.compile(r"#\[\s*cfg\s*\(\s*test\s*\)\s*\]\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+\w+\s*;$")
SPLIT = re.compile(r"[{}()\[\];]")
ITEM = re.compile(rf"(?:pub(?:\s*\([^)]*\))?\s+)?(?:(?:unsafe|async|const|extern|default|auto)\s+)*"
rf"(fn|struct|enum|union|trait|type|const|static|impl|mod|use|macro_rules\s*!|extern\s+crate)"
rf"(?!\w)\s*(?:mut\s+)?({IDENT})?")
PATH_BEFORE = re.compile(rf"\b({IDENT})\s*::")
PATH_AFTER = re.compile(rf"::\s*({IDENT})")
MACRO_USE = re.compile(rf"\b({IDENT})\s*!\s*[(\[{{]")
MACRO_CALL = re.compile(rf"({IDENT}(?:\s*::\s*{IDENT})*)\s*!")
DEFINED_INSIDE = re.compile(rf"\b(?:fn|struct|enum|union|trait|type|const|static)\s+({IDENT})")
NESTED_IMPL = re.compile(r"^[ \t]*(?:unsafe\s+)?impl\b([^{;]*)\{", re.MULTILINE)
QUALIFIED = re.compile(rf"{IDENT}(?:\s*::\s*{IDENT})*")
GLOB_IMPORT = re.compile(r"\buse\s[^;]*::\s*\*")
# File names a source may name in a literal (include_str!, include_bytes!, a path it reads). A file of another kind
# is never "named", so it always counts.
FILE_NAME = re.compile(r"[\w-]+\.(?:wgsl|glsl|json|png|ttf|otf|svg|txt|toml|ogg|wav|nbt|bin|dat|csv|mcmeta|wit|wasm)\b")
TYPE_KINDS = {"struct", "enum", "union", "trait", "type", "macro_call"}
# Names an impl header can take from outside the workspace: std's prelude and primitive types, and the std and serde
# traits a glob import may bring. A glob-imported name outside this list is taken to be the workspace's.
STD_NAMES = {"Display", "Error", "Read", "Write", "Seek", "BufRead", "Deref", "DerefMut", "Index", "IndexMut", "Add",
"Sub", "Mul", "Div", "Rem", "Neg", "Not", "BitAnd", "BitOr", "BitXor", "Shl", "Shr", "AddAssign",
"SubAssign", "MulAssign", "DivAssign", "RemAssign", "BitAndAssign", "BitOrAssign", "BitXorAssign",
"Borrow", "BorrowMut", "Future", "Any", "FromStr", "Hasher", "BuildHasher", "Formatter", "Ordering",
"Serialize", "Deserialize", "Serializer", "Deserializer", "Visitor", "Termination", "Sum", "Product",
"Wake", "Cow", "Rc", "Arc", "Mutex", "RwLock", "Cell", "RefCell", "HashMap", "HashSet", "BTreeMap",
"BTreeSet", "VecDeque"}
PRELUDE = {"Option", "Some", "None", "Result", "Ok", "Err", "Vec", "String", "Box", "ToString", "ToOwned", "Clone",
"Copy", "Send", "Sync", "Sized", "Unpin", "Drop", "Fn", "FnMut", "FnOnce", "From", "Into", "TryFrom",
"TryInto", "AsRef", "AsMut", "Default", "Eq", "PartialEq", "Ord", "PartialOrd", "Iterator", "Extend",
"IntoIterator", "DoubleEndedIterator", "ExactSizeIterator", "FromIterator", "Debug", "Hash",
"bool", "char", "str", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128",
"isize", "f32", "f64"}
STD_NAMES |= PRELUDE
KEYWORDS = {"crate", "self", "super", "Self", "pub", "in", "as", "use", "for", "where", "dyn", "mut", "const", "fn",
"unsafe", "impl", "std", "core", "alloc", "let", "if", "else", "match", "return", "struct", "enum", "mod",
"type", "static", "trait", "true", "false", "loop", "while", "move", "ref", "break", "continue", "async",
"await", "extern", "union"}
def segments(code, start, end):
"""The item-level pieces of code[start:end]: each ends at a `;` or a `}` outside any bracket."""
found, begin, depth = [], start, 0
for match in SPLIT.finditer(code, start, end):
character = match.group()
if character in "{([":
depth += 1
elif character in "})]":
depth -= 1
if character == "}" and depth == 0:
found.append((begin, match.end()))
begin = match.end()
elif depth == 0:
found.append((begin, match.end()))
begin = match.end()
if code[begin:end].strip():
found.append((begin, end))
return found
def skip_attributes(code, index, end):
"""The offset of the first non-space character after the attributes (`#[...]`, `#![...]`) starting at index."""
while True:
while index < end and code[index].isspace():
index += 1
if not code.startswith("#", index) or not re.match(r"#!?\[", code[index:index + 3]):
return index
depth = 0
for position in range(code.index("[", index), end):
depth += {"[": 1, "]": -1}.get(code[position], 0)
if depth == 0:
index = position + 1
break
else:
return end
def body_of(code, start, end):
"""The range inside an item's outermost `{ }` block, or None."""
opening = code.find("{", start, end)
closing = code.rfind("}", start, end)
return (opening + 1, closing) if 0 <= opening < closing else None
def words(text):
return set(WORD.findall(text)) - KEYWORDS
def qualified_words(text):
"""Names a piece of code reaches through a path (`a::b`: b), the way items of other modules are named."""
return set(PATH_AFTER.findall(text)) - KEYWORDS
def path_words(text):
"""Names a piece of code uses as path segments (`a::`, `::b`), where module names appear."""
return set(PATH_BEFORE.findall(text)) | set(PATH_AFTER.findall(text))
def parse_items(code, start, end, items):
"""Append the items of code[start:end] (a file or an inline module's body) to items, as dicts."""
for begin, finish in segments(code, start, end):
head = skip_attributes(code, begin, finish)
text = code[head:finish]
if not text.strip(" \n\t;") or TEST_ONLY.search(code[begin:head]):
continue # compiled into tests only, never into a probe
match = ITEM.match(text)
kind = re.sub(r"\s+", "", match.group(1)) if match else None
name = match.group(2) if match else None
if kind == "mod":
body = body_of(code, head, finish)
if body:
parse_items(code, body[0], body[1], items)
continue
if kind in ("externcrate", None) and not MACRO_CALL.match(text):
if kind is None:
items.append({"kind": "glue", "mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))})
continue
if kind is None: # an item-level macro call: it may define items
items.append({"kind": "macro_call", "names": sorted(set(DEFINED_INSIDE.findall(text))),
"macro": MACRO_CALL.match(text).group(1).split("::")[-1].strip(),
"mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))})
elif kind == "use":
head_word = re.match(rf"\s*(?:::\s*)?({IDENT})", text[match.end(1):])
items.append({"kind": "use", "public": text.startswith("pub"), "names": sorted(words(text)),
"head": head_word.group(1) if head_word else "", "glob": bool(re.search(r"::\s*\*", text)),
"mentions": sorted(words(text)), "paths": sorted(words(text)), "qualified": sorted(words(text)),
"invokes": []})
elif kind == "impl":
body = body_of(code, head, finish)
subject = code[head + match.end(1) - match.start(): body[0] - 1 if body else finish]
inherent = not re.search(r"\bfor\b", impl_subject(subject)[1])
if inherent and body:
members = []
for member_begin, member_end in segments(code, body[0], body[1]):
member = code[skip_attributes(code, member_begin, member_end):member_end]
found = ITEM.match(member)
members.append({"name": found.group(2) if found else None, "mentions": sorted(words(member)),
"paths": sorted(path_words(member)), "qualified": sorted(qualified_words(member)), "invokes": sorted(set(MACRO_USE.findall(member)))})
items.append({"kind": "impl", "subject": subject, "inherent": True, "members": members,
"mentions": sorted(words(subject)), "paths": sorted(path_words(subject)), "qualified": sorted(qualified_words(subject)), "invokes": sorted(set(MACRO_USE.findall(subject)))})
else:
items.append({"kind": "impl", "subject": subject, "inherent": False, "mentions": sorted(words(text)),
"paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))})
else:
kind = "macro" if kind.startswith("macro_rules") else kind
items.append({"kind": kind, "names": [name] if name else [], "mentions": sorted(words(text)),
"defines": sorted(set(DEFINED_INSIDE.findall(text))) if kind == "macro" else [],
"paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))})
if kind in ("fn", "const", "static"):
# An impl inside a function body still holds for the whole program.
for header in NESTED_IMPL.findall(text):
items.append({"kind": "impl", "subject": header, "inherent": False,
"mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))})
def crate_roots(crate_dir):
"""A crate's root files: its library, binaries and examples."""
roots = [crate_dir / "src" / "lib.rs", crate_dir / "src" / "main.rs"]
for folder in (crate_dir / "src" / "bin", crate_dir / "examples"):
if folder.is_dir():
roots += sorted(folder.glob("*.rs")) + sorted(folder.glob("*/main.rs"))
return [root for root in roots if root.is_file()]
class SourceGraph:
"""The workspace's Rust sources, parsed once: what each file names, defines and declares."""
def __init__(self, root, rust_code, cache=None):
"""cache: a dict of earlier parses by content digest, which the graph reuses and adds to (parity.py keeps it
in .local/parity-runner/reach-cache.json)."""
self.root = Path(root)
self.rust_code = rust_code
self.cache = {} if cache is None else cache
self.used = set()
self.files = {}
self.parent = {} # module file -> the file declaring it
self.module_name = {}
self.roots = set()
self.target_cache = {}
self.workspace_heads = None
self.test_only = set() # modules compiled into tests only
self.tables = None
self.root_of = {} # source -> the root of its target (lib.rs, a bin's or an example's main file)
self.results = {} # (entries, universe) -> reachable files
for manifest in sorted((self.root / "crates").glob("*/Cargo.toml")):
for crate_root in crate_roots(manifest.parent):
path = self.relative(crate_root)
self.roots.add(path)
self.root_of[path] = path
self.place(path, crate_root.parent, set())
def entries(self, artifacts, rust):
"""The root files of the probes and binaries a harness built (its record's artifacts), among its Rust
dependencies: for a crate's probe binary, the probe modules it ran (parity.only_probes_run kept only those).
None when an artifact can't be placed."""
found = set()
for kind, name, *rest in artifacts:
selection = rest[1] if len(rest) > 1 else []
packages = [value for flag, value in zip(selection, selection[1:]) if flag == "-p"] or \
[manifest.parent.name for manifest in sorted((self.root / "crates").glob("*/Cargo.toml"))]
roots = set()
for package in packages:
crate = self.root / "crates" / package
targets = self.targets(crate)
if kind in ("example", "examples"):
candidates = [path for target, (target_kind, path) in targets.items()
if target_kind == "example" and (not name or target == name)]
else:
candidates = [path for target, (target_kind, path) in targets.items()
if target_kind == "bin" and (not name or target == name)]
for path in candidates:
if path.endswith("/examples/probe/main.rs"):
# The probes it ran, and the dispatch and support modules they use (only_probes_run kept
# those); the binary's root, which only declares them, so their own submodules are placed.
folder = path.removesuffix("main.rs")
roots |= {dependency for dependency in rust if dependency.startswith(folder)
and "/" not in dependency.removeprefix(folder).removeprefix("support/")}
if roots:
roots.add(path)
elif path in rust:
roots.add(path)
if not roots:
return None
found |= roots
return found or None
def targets(self, crate):
"""A crate's bins and examples: {target name: (kind, root file)}, from its Cargo.toml and Cargo's defaults."""
if crate in self.target_cache:
return self.target_cache[crate]
found = {}
manifest = (crate / "Cargo.toml").read_text(encoding="utf-8") if (crate / "Cargo.toml").is_file() else ""
package = re.search(r'^\[package\][^\[]*?^name\s*=\s*"([^"]+)"', manifest, re.MULTILINE | re.DOTALL)
if (crate / "src" / "main.rs").is_file() and package:
found[package.group(1)] = ("bin", self.relative(crate / "src" / "main.rs"))
for kind, folder in (("bin", crate / "src" / "bin"), ("example", crate / "examples")):
for path in sorted(folder.glob("*.rs")) + sorted(folder.glob("*/main.rs")):
found[path.stem if path.name != "main.rs" else path.parent.name] = (kind, self.relative(path))
for kind, table in re.findall(r"^\[\[(bin|example)\]\]([^\[]*)", manifest, re.MULTILINE):
name = re.search(r'^name\s*=\s*"([^"]+)"', table, re.MULTILINE)
path = re.search(r'^path\s*=\s*"([^"]+)"', table, re.MULTILINE)
if name and path and (crate / path.group(1)).is_file():
found[name.group(1)] = (kind, self.relative(crate / path.group(1)))
self.target_cache[crate] = found
return found
def target(self, path):
"""The Cargo target a source belongs to: the root file of the library, bin or example whose module tree
holds it (a file nothing places is its own)."""
return self.root_of.get(path, path)
def is_library(self, path):
return self.target(path).endswith("/src/lib.rs")
def relative(self, path):
return Path(path).resolve().relative_to(self.root.resolve()).as_posix()
def place(self, path, module_dir, seen):
"""Walk the module tree from a file: record each declared module's file, parent and name."""
if path in seen:
return
seen.add(path)
parsed = self.parse(path)
if parsed is None:
return
for name, nesting, attribute, test in parsed["declarations"]:
if attribute is not None:
if nesting:
continue # a #[path] inside an inline module: unplaced, so always kept
candidates = [(self.root / path).parent / attribute]
else:
folder = module_dir.joinpath(*nesting)
candidates = [folder / f"{name}.rs", folder / name / "mod.rs"]
for candidate in candidates:
if candidate.is_file():
child = self.relative(candidate)
self.parent.setdefault(child, path)
self.module_name.setdefault(child, name)
self.root_of.setdefault(child, self.root_of[path])
if test or path in self.test_only:
self.test_only.add(child)
child_dir = candidate.parent if candidate.name == "mod.rs" or attribute is not None \
else candidate.parent / candidate.stem
self.place(child, child_dir, seen)
break
def parse(self, path):
if path in self.files:
return self.files[path]
try:
data = (self.root / path).read_bytes()
except OSError:
self.files[path] = None
return None
digest = hashlib.sha256(data).hexdigest()
self.used.add(digest)
if digest in self.cache:
self.files[path] = parsed = self.cache[digest]
return parsed
raw = data.decode("utf-8", errors="replace")
code = self.rust_code(raw) # same length as raw: literals and comments blanked
opening = {match.end() - 1: match.group(1) for match in INLINE_MOD.finditer(code)}
events = sorted([(match.start(), match) for match in BRACE.finditer(code)]
+ [(match.start(), match) for match in MOD_DECLARATION.finditer(code)], key=lambda event: event[0])
declarations, stack, depth = [], [], 0
for index, match in events:
if match.group() == "{":
depth += 1
if index in opening:
stack.append((opening[index], depth))
elif match.group() == "}":
if stack and stack[-1][1] == depth:
stack.pop()
depth -= 1
else:
window = " ".join(raw[max(0, index - 300):match.end()].split())
attribute = PATH_ATTRIBUTE.search(window)
declarations.append([match.group(1), [name for name, _ in stack], attribute.group(1) if attribute else None,
bool(TEST_MODULE.search(window))])
items = []
parse_items(code, 0, len(code), items)
self.files[path] = parsed = {
"declarations": declarations,
"items": items,
"glob": bool(GLOB_IMPORT.search(code)),
"file_names": sorted(set(FILE_NAME.findall(raw))),
}
self.cache[digest] = parsed
return parsed
def saved_cache(self):
"""The parses of the files this graph read, to keep for the next run."""
return {digest: self.cache[digest] for digest in self.used if digest in self.cache}
def requirement(self, path, item):
"""For an impl: for its trait and for its type, [names the file defines, other workspace names], one of
which must be a reached type or trait (of this file, of a crate it sees) before the impl can matter; both
empty: nothing of the workspace is needed. A name is the workspace's when the path naming it starts in the
workspace, or the file defines it, imports it from the workspace or may glob-import it; otherwise (std's,
another crate's, the prelude's) it needs nothing."""
if "requires" in item:
return item["requires"]
if self.workspace_heads is None:
self.workspace_heads = {"crate", "self", "super"} | set(self.module_name.values()) | {
manifest.parent.name.replace("-", "_") for manifest in (self.root / "crates").glob("*/Cargo.toml")}
parsed = self.parse(path)
defined = {name for other in parsed["items"] for name in other.get("names", ()) if other["kind"] != "use"}
workspace_imports = {name for other in parsed["items"] if other["kind"] == "use"
and other["head"] in self.workspace_heads for name in other["names"]}
parameters, subject = impl_subject(item["subject"])
parts = []
for part in re.split(r"\bfor\b", subject, maxsplit=1):
names, own = set(), set()
for match in QUALIFIED.finditer(part):
segments_ = [segment.strip() for segment in match.group().split("::")]
name = segments_[-1]
if name in parameters or name in KEYWORDS:
continue
if len(segments_) > 1:
if segments_[0] in self.workspace_heads:
names.add(name)
elif name in defined:
own.add(name)
elif name in workspace_imports or (parsed["glob"] and name not in STD_NAMES):
names.add(name)
parts.append([sorted(own), sorted(names)])
item["requires"] = parts
return parts
def index(self):
"""Lookup tables over every placed source, built once: which crates each crate sees, the nodes (items, an
inherent impl's members) by the names they define and the types they need, the modules by name."""
if self.tables is not None:
return self.tables
idents = {manifest.parent.name.replace("-", "_"): manifest.parent.name
for manifest in (self.root / "crates").glob("*/Cargo.toml")}
paths = [path for path in self.files if self.files[path] is not None and path not in self.test_only]
sees = {}
for path in paths:
named = {word for item in self.files[path]["items"] for word in item["mentions"]} & idents.keys()
sees.setdefault(crate_of(path), {crate_of(path)}).update(idents[name] for name in named)
changed = True
while changed:
changed = False
for crate, seen in sees.items():
wider = set().union(*(sees.get(other, {other}) for other in seen))
if not wider <= seen:
seen |= wider
changed = True
seen_by = {} # crate -> the crates that see it
for crate, seen in sees.items():
for other in seen:
seen_by.setdefault(other, set()).add(crate)
# What a call of a workspace macro defines: what its definition's body defines.
expands = {}
for path in paths:
for item in self.files[path]["items"]:
if item["kind"] == "macro":
for name in item["names"]:
expands.setdefault(name, set()).update(item["defines"])
names_of, by_name, by_file_name, by_type, by_module, children, seeds = {}, {}, {}, {}, {}, {}, []
for path in paths:
crate = crate_of(path)
if path in self.module_name:
by_module.setdefault((crate, self.module_name[path]), []).append(path)
children.setdefault(self.parent[path], []).append(path)
for index, item in enumerate(self.files[path]["items"]):
node = (path, index, None)
names = set(item.get("names", ()))
if item["kind"] == "macro_call":
names |= expands.get(item["macro"], set())
names_of[node] = names
if item["kind"] == "glue":
seeds.append(node)
elif item["kind"] == "macro_call" and not names:
# A macro call defining nothing by name may still expand to impls: it counts once a type or
# trait it mentions is reached.
for name in item["mentions"]:
by_type.setdefault(name, []).append(node)
elif item["kind"] == "impl":
required = {name for own, foreign in self.requirement(path, item) for name in own + foreign}
members = [(path, index, member) for member in range(len(item["members"]))] if item["inherent"] else []
for member_node in members:
name = item["members"][member_node[2]]["name"]
names_of[member_node] = set()
if name:
by_name.setdefault(name, []).append(member_node)
for name in required:
for other in [node] + members:
by_type.setdefault(name, []).append(other)
if not required:
seeds.append(node)
seeds += [member_node for member_node in members if not item["members"][member_node[2]]["name"]]
else:
for name in names:
by_name.setdefault(name, []).append(node)
by_file_name.setdefault((path, name), []).append(node)
self.tables = dict(idents=idents, sees=sees, seen_by=seen_by, names_of=names_of, by_name=by_name,
by_file_name=by_file_name, by_type=by_type, by_module=by_module, children=children,
seeds=seeds, node_sets={})
return self.tables
def reachable(self, entries, universe, reasons=None):
"""The files of universe (workspace Rust sources) holding an item reachable from entries (by the rules in
this module's doc), and the entries. reasons, a dict, gets why each file was reached (for debugging)."""
# Searching every source of the crates involved (not only those Cargo listed) only adds reach, and lets
# harnesses with the same entries (the game binary's) share one search.
crates = frozenset(crate_of(path) for path in universe)
key = (frozenset(entries), crates)
if reasons is None and key in self.results:
return self.results[key] & set(universe)
everything = {path for path in self.files if crate_of(path) in crates} | set(universe)
files = self.search(entries, everything, reasons)
self.results[key] = files
return files & set(universe)
def search(self, entries, universe, reasons):
targets = {self.target(path) for path in entries}
universe = {path for path in universe if (self.is_library(path) or self.target(path) in targets)
and path not in self.test_only}
tables = self.index()
idents, sees, seen_by, names_of = tables["idents"], tables["sees"], tables["seen_by"], tables["names_of"]
node_sets = tables["node_sets"]
by_name, by_file_name, by_type = tables["by_name"], tables["by_file_name"], tables["by_type"]
by_module, children = tables["by_module"], tables["children"]
seeds = [node for node in tables["seeds"] if node[0] in universe]
for path in universe:
if (path in entries or (path not in self.parent and path not in self.roots)) and self.parse(path):
seeds += [(path, index, None) for index in range(len(self.parse(path)["items"]))]
def names(node):
return names_of.get(node, set())
def sets_of(node, item):
"""A node's mentions, path words, qualified names and macro calls, as sets (parses keep lists)."""
if node not in node_sets:
source = item["members"][node[2]] if node[2] is not None else item
node_sets[node] = {key: set(source.get(key, ())) for key in ("mentions", "paths", "qualified", "invokes")}
return node_sets[node]
def item_of(node):
return self.parse(node[0])["items"][node[1]]
reached, opened, globbed, files, queue = set(), set(), set(), set(), []
# name -> the crates whose reached code mentions it plainly, names it in a path or an import, invokes it.
mentioned, qualified, invoked = {}, {}, {}
done = {"mentions": {}, "qualified": {}, "invokes": {}, "paths": {}} # crate -> names already handled
path_names, types = {}, {} # by crate
local = {} # file -> plain mentions by reached code in it or below it (child modules may `use super::*`)
local_types = {} # file -> its reached types and traits
def heard(table, name, crate):
"""Whether a crate that can see crate's items has name in table."""
sources = table.get(name)
return bool(sources) and not sources.isdisjoint(seen_by.get(crate, {crate}))
def ready(node):
path, index, member = node
item = item_of(node)
crate = crate_of(path)
if item["kind"] == "impl":
visible = visible_types(crate)
here = local_types.get(path, set())
if not all((not own and not foreign) or here.intersection(own) or visible.intersection(foreign)
for own, foreign in self.requirement(path, item)):
return False
name = item["members"][member]["name"] if member is not None else None
return name is None or heard(mentioned, name, crate) # a method: called by its name
if item["kind"] == "macro_call" and not names(node):
return not visible_types(crate).isdisjoint(item["mentions"])
names_ = names(node)
if item["kind"] == "macro":
return any(heard(invoked, name, crate) for name in names_)
if item["kind"] == "use" and item["glob"]:
return path in opened
nearby = local.get(path, ())
if any(name in nearby for name in names_):
return True # named in its own module, or below it
if item["kind"] == "use" and not item["public"]:
return False
if path in opened and any(heard(qualified, name, crate) for name in names_):
return True # named through a path, or imported
return path in globbed and any(heard(mentioned, name, crate) for name in names_)
visible_cache = {}
def visible_types(crate):
"""The reached types and traits of the crates a crate sees (cached until a type is reached)."""
if crate not in visible_cache:
visible_cache[crate] = set().union(*(types.get(other, set()) for other in sees.get(crate, {crate})))
return visible_cache[crate]
def wake(nodes, why):
for other in nodes:
if other not in reached and other[0] in universe and ready(other):
reach(other, why)
def open_module(path, why):
pending = [path]
while pending:
current = pending.pop()
if current in opened:
continue
opened.add(current)
crate = crate_of(current)
pending += [child for child in children.get(current, ())
if child in universe and self.module_name[child] in path_names.get(crate, ())]
parsed = self.parse(current)
wake([(current, index, None) for index, item in enumerate(parsed["items"] if parsed else ())
if item["kind"] != "impl"], f"{current} opened ({why})")
def reach(node, why):
if node in reached:
return
reached.add(node)
queue.append(node)
path, _, member = node
item = item_of(node)
if (item["kind"] == "use" and not item["public"]) or (item["kind"] == "impl" and item["inherent"] and member is None):
return
if path not in files:
files.add(path)
if reasons is not None:
reasons[path] = why
for path in universe:
if path.endswith("/src/lib.rs") or path in entries or path not in self.parent:
open_module(path, "crate root or entry")
for node in seeds:
reach(node, "entry" if node[0] in entries else "always counted")
for path in entries & universe:
files.add(path)
if reasons is not None:
reasons.setdefault(path, "entry")
while queue:
node = queue.pop()
path, index, member = node
item = item_of(node)
crate = crate_of(path)
label = (item.get("names") or [item["kind"]])[0] if member is None else item["members"][member]["name"]
source = sets_of(node, item)
why = f"{path} ({label})"
# Plain mentions reach items of this file and of the modules above it.
ancestor, new = path, source["mentions"]
while ancestor is not None and new:
known = local.setdefault(ancestor, set())
new = new - known
known |= new
for name in new:
wake(by_file_name.get((ancestor, name), ()), f"{why} mentions {name}")
ancestor = self.parent.get(ancestor)
if item["kind"] == "use" and item["glob"]:
for name in item["names"]:
for target in sees.get(crate, {crate}):
for module in list(by_module.get((target, name), ())) + \
([f"crates/{idents[name]}/src/lib.rs"] if name in idents else []):
if module not in globbed and module in universe:
globbed.add(module)
wake([(module, index, None) for index in range(len(self.parse(module)["items"]))]
if self.parse(module) else [], f"{why} glob-imports {name}")
visible_to = sees.get(crate, {crate})
new = source["paths"] - done["paths"].setdefault(crate, set())
done["paths"][crate] |= new
for name in new:
for target in visible_to:
path_names.setdefault(target, set()).add(name)
for module in by_module.get((target, name), ()):
if module in universe and self.parent[module] in opened:
open_module(module, f"{why} names {name}")
for key, table, wanted in (("qualified", qualified, lambda other: True),
("invokes", invoked, lambda other: item_of(other)["kind"] == "macro"),
("mentions", mentioned, lambda other: other[2] is not None or other[0] in globbed)):
new = source[key] - done[key].setdefault(crate, set())
done[key][crate] |= new
for name in new:
table.setdefault(name, set()).add(crate)
wake([other for other in by_name.get(name, ()) if crate_of(other[0]) in visible_to and wanted(other)],
f"{why} {key} {name}")
if item["kind"] in TYPE_KINDS and member is None:
own = types.setdefault(crate, set())
local_types.setdefault(path, set()).update(names(node))
if not names(node) <= own:
own |= names(node)
visible_cache.clear()
seeing = seen_by.get(crate, {crate})
for name in names(node): # also when the crate has one by that name already: impls of this one
wake([other for other in by_type.get(name, ()) if crate_of(other[0]) in seeing],
f"implements for {name}, defined in {path}")
return files
def impl_subject(header):
"""An impl header (the text after `impl`): its generic parameters' names, and the rest without its where clause
(the trait and the type it is for)."""
header, parameters = header.strip(), set()
if header.startswith("<"):
depth = 0
for index, character in enumerate(header):
depth += {"<": 1, ">": -1}.get(character, 0)
if depth == 0:
generics, header = header[1:index], header[index + 1:]
parameters = {match.group(1) for match in re.finditer(rf"(?:^|,)\s*(?:const\s+)?({IDENT})", generics)}
break
return parameters, re.split(r"\bwhere\b", header)[0]
def crate_of(path):
return path.split("/", 2)[1]
def is_workspace_rust(path):
parts = PurePosixPath(path).parts
return len(parts) > 2 and parts[0] == "crates" and path.endswith(".rs")
DECLARATION_LINE = re.compile(rf"\s*(?:pub(?:\([^)]*\))?\s+)?mod\s+({IDENT})\s*;")
def declarations(text, names, keep_rest):
"""A Rust file's `mod` declarations of names, each with the attribute lines just above it; with keep_rest, the
whole text except the declarations of other modules instead."""
lines, found, attributes = text.splitlines(), [], []
for line in lines:
if line.strip().startswith("#[") and not line.strip().startswith("#!["):
attributes.append(line.strip())
continue
match = DECLARATION_LINE.match(line)
if match and match.group(1) in names:
found += attributes + [line.strip()]
elif keep_rest and not match:
found += attributes + [line]
attributes = []
return "\n".join(found + attributes)
def declarations_changed(root, path, children, base, whole):
"""Whether a changed file differs from the commit base in what a harness depends on: for a module parent, its
`mod` declarations of the reached children; for a whole file (whole), everything but its declarations of other
modules (adding a module beside reached ones changes nothing). True without a base, or if either side is
missing."""
if base is None:
return True
try:
head = (Path(root) / path).read_text(encoding="utf-8", errors="replace")
before = subprocess.run(["git", "show", f"{base}:{path}"], cwd=root, capture_output=True, text=True, check=True).stdout
except (OSError, subprocess.CalledProcessError):
return True
return declarations(head, children, whole) != declarations(before, children, whole)
def narrow(dependencies, artifacts, graph, changed=()):
"""A harness's dependencies narrowed to what its entry points can reach: {dependency: (whole, children)}, where
whole says the file counts for its own content (else only through its `mod` declarations of children, as a
module ancestor of a reached file) and children are its reached child modules. Without known entry points (a
probe or binary the harness built that isn't found here), every dependency is kept whole."""
plain = [dependency.split("#keep:")[0] for dependency in dependencies]
crates = {PurePosixPath(path).parts[1] for path in plain if path.startswith("crates/")}
rust = {path for path in plain if is_workspace_rust(path)}
added = {path for path in changed if is_workspace_rust(path) and PurePosixPath(path).parts[1] in crates
and path not in rust and (graph.root / path).is_file()}
entries = graph.entries(artifacts, rust)
if entries is None:
return {dependency: (True, None) for dependency in dependencies}
missing = {path for path in rust if graph.parse(path) is None}
reached = graph.reachable(entries, rust | added | entries) | missing
named, named_in_crate = set(), {}
for path in rust | added:
parsed = graph.parse(path)
file_names = set(parsed["file_names"]) if parsed else set()
if path in reached:
named |= file_names
named_in_crate.setdefault(PurePosixPath(path).parts[1], set()).update(file_names)
kept = {}
for dependency, path in zip(dependencies, plain):
if is_workspace_rust(path):
if path in reached:
kept[dependency] = (True, set())
elif not path.startswith("crates/") or path.endswith(("Cargo.toml", "build.rs")) \
or PurePosixPath(path).name in named \
or PurePosixPath(path).name not in named_in_crate.get(PurePosixPath(path).parts[1], ()):
kept[dependency] = (True, None)
for path in sorted(added & reached):
kept[path] = (True, set())
for path in sorted(reached):
child = path
while child in graph.parent:
parent = graph.parent[child]
kept.setdefault(parent, (False, set()))[1].add(graph.module_name[child])
child = parent
return kept
+41
View File
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
"""Compare item lore tooltip styles with the pinned vanilla client.
Vanilla prints what ``ItemLore.addToTooltip`` adds after ``ComponentUtils.mergeStyles``
with dark purple italic (VanillaLoreTooltipOracle). Horizon prints what
``item_tooltip::lore_texts`` gives (examples/probe/lore_tooltip_fixture).
"""
try:
from . import lab, oracle_harness as harness
except ImportError:
import lab, oracle_harness as harness
# SNBT stacks. A lore line is a text component; italic:0b is an explicit false.
CASES = [
'{id:"minecraft:stick",count:1}',
'{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"Plain"\']}}',
'{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"Gold","color":"gold","italic":0b}\']}}',
'{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"A","extra":[{"text":"B","color":"aqua","italic":0b}]}\']}}',
'{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"One"\',\'{"text":"Two","bold":1b}\']}}',
'{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"Hidden"\'],"minecraft:tooltip_display":{hidden_components:["minecraft:lore"]}}}',
'{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"Line","underlined":1b,"strikethrough":1b}\']}}',
]
def cases(build):
path = build / 'cases.txt'
path.write_text(''.join(line + '\n' for line in CASES))
return path, len(CASES)
if __name__ == '__main__':
harness.differential(
'lore-tooltip-parity', 'VanillaLoreTooltipOracle', 'lore_tooltip_fixture', cases,
'Item lore tooltip lines: dark purple italic, child colours, explicit non-italic, and tooltip_display',
sources=[
lab.ROOT / 'crates/horizon-client/src/item_tooltip.rs',
lab.ROOT / 'crates/horizon-client/src/item_data.rs',
lab.ROOT / 'crates/horizon-client/src/text.rs',
lab.ROOT / 'crates/horizon-client/src/component.rs',
],
expected_lines=len(CASES))
+39 -5
View File
@@ -964,13 +964,16 @@
{
"id": "opengl-fallback",
"title": "OpenGL for old computers",
"status": "later",
"status": "shipped",
"area": "platforms",
"plain": "A way to play on old PCs and Linux machines that have no Vulkan or DirectX 12, through OpenGL.",
"dev": "Players on older hardware ask whether Vulkan is the only way. Horizon draws with wgpu, which has an OpenGL backend; what is needed is to see which features hold up on it, how fast it runs, and a launcher switch like the DirectX 12 fallback that already ships.",
"plain": "Horizon now starts on old PCs whose graphics card has no Vulkan or DirectX 12 driver: it switches to OpenGL by itself, and Options > Graphics can pick OpenGL by hand.",
"dev": "wgpu's GL backend (WGL on Windows, EGL on Linux) as the last fallback after Vulkan and DirectX 12 (#1917), sprite arrays and CPU sprite animation for drivers without compute (#1920), no indirect draws below GL 4.3 (#1936). Minimum OpenGL 3.3 core. Shader packs don't run on it yet.",
"links": [
"docs/graphics.md"
]
"#1917",
"#1920",
"#1936"
],
"shipped_in": "v0.2.63"
},
{
"id": "phones-and-browser",
@@ -1202,6 +1205,37 @@
"docs/hosting.md"
],
"shipped_in": "launcher-v0.15.0"
},
{
"id": "cli-server-install",
"title": "A server you install with one command",
"status": "next",
"area": "servers",
"plain": "Install and run a Horizon dedicated server from a terminal with a single command, no launcher needed, for VPSes and headless machines.",
"dev": "A player's idea (#horizon-ideas). `horizon-server` already runs headless; left: an install script (Linux/macOS shell, Windows PowerShell) that fetches the signed release, checks it, asks for or points at the player's own game files and starts the server, documented in docs/hosting.md.",
"links": [
"docs/hosting.md"
]
},
{
"id": "launcher-look-menus",
"title": "Game menus in the launcher's style",
"status": "later",
"area": "extras",
"plain": "An optional look for the game's own menus and options that matches the Horizon launcher. Off by default: the vanilla menus stay as they are.",
"dev": "A player's idea (#horizon-ideas). A built-in resource pack or a Horizon setting that restyles the title, pause and options screens with the launcher's design tokens (crates/horizon-theme), following docs/design-system.md; off by default because it changes the vanilla look.",
"links": [
"docs/design-system.md"
]
},
{
"id": "linux-packages",
"title": "AppImage and a Nix flake",
"status": "later",
"area": "platforms",
"plain": "Install Horizon on any Linux distribution as an AppImage, and on NixOS through a Nix flake.",
"dev": "A player's idea (#horizon-ideas). An AppImage of the launcher and game from the release workflow, and a flake.nix packaging the release binaries with their runtime libraries (Vulkan loader, X11/Wayland, ALSA), the way horizon-parity's NixOS setup runs them today.",
"links": []
}
],
"journal": [