Merge origin/main into refactor/horizon-items

#2037's loot-enchantment tests stay in horizon-items: they load the
prepared items through the block registry and facts directly instead of
the client's Assets::load.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-26 15:57:41 +01:00
126 changed files with 5087 additions and 727 deletions
+6 -3
View File
@@ -48,6 +48,9 @@ jobs:
GH_TOKEN: ${{ github.token }}
run: echo "sha=$(python3 tools/parity_main.py base)" >> "$GITHUB_OUTPUT"
- name: The harnesses the merges since then can affect
id: harnesses
# A failure here is judged by the next step: the run passes when every failed harness passes again at HEAD.
continue-on-error: true
env:
PARITY_HOSTS: ''
BASE_SHA: ${{ steps.base.outputs.sha }}
@@ -55,8 +58,8 @@ jobs:
echo "Merges since ${BASE_SHA}:"
git log --first-parent --format=' %h %s' "$BASE_SHA..HEAD"
python3 tools/parity.py --changed "$BASE_SHA" --timeout 2400
- name: The merge that broke them
if: failure()
- name: A flake, or the merge that broke them
if: steps.harnesses.outcome == 'failure'
env:
GH_TOKEN: ${{ github.token }}
PARITY_HOSTS: ''
@@ -65,7 +68,7 @@ jobs:
rm -rf .local/parity-main-logs && cp -R .local/parity-runner/logs .local/parity-main-logs 2>/dev/null || true
python3 tools/parity_main.py bisect "$BASE_SHA" --comment
- name: Logs of the failed harnesses
if: failure()
if: steps.harnesses.outcome == 'failure'
uses: actions/upload-artifact@v4
with:
name: parity-main-logs
+60 -50
View File
@@ -160,6 +160,66 @@ Translatable components resolved through the selected language for items, contai
</details>
#### Cooldowns drawn on items
After you throw an ender pearl, its slot should show the cooldown sweeping away, as in vanilla.
<details><summary>Technical</summary>
Vanilla draws `ItemCooldowns`' percent as a white overlay over the slot (`GuiGraphicsExtractor.itemDecorations` and `itemCooldown`) in the hotbar and in inventories. Issue [#1763](https://github.com/ludovic111/horizon/issues/1763).
More: [#1933](https://github.com/ludovic111/horizon/issues/1933)
</details>
#### Chat settings and team prefixes
The Hidden and Commands Only chat settings should hide chat, and team prefixes should appear before names in chat.
<details><summary>Technical</summary>
Vanilla checks `ChatVisiblity` before it delivers chat and system messages ([#1376](https://github.com/ludovic111/horizon/issues/1376)), and chat uses the team-formatted display name ([#1378](https://github.com/ludovic111/horizon/issues/1378)). Both issues have exact steps to reproduce.
More: [#1932](https://github.com/ludovic111/horizon/issues/1932) · [#1841](https://github.com/ludovic111/horizon/issues/1841)
</details>
#### A pointing hand over buttons
The mouse pointer should turn into a pointing hand over menu buttons, as in vanilla 26.3.
<details><summary>Technical</summary>
26.3's `AbstractWidget` asks for a cursor from `CursorTypes` when it is hovered; winit's `CursorIcon::Pointer` gives the same shape. Issue [#1347](https://github.com/ludovic111/horizon/issues/1347).
More: [#1897](https://github.com/ludovic111/horizon/issues/1897)
</details>
#### Ctrl+Backspace and scroll bars in menus
Ctrl+Backspace should delete a whole word in the creative search, and every list's scroll bar should be draggable.
<details><summary>Technical</summary>
`EditBox` deletes by word with Ctrl ([#1764](https://github.com/ludovic111/horizon/issues/1764)); the Game Rules and Statistics lists' scroll bars can't be dragged ([#1782](https://github.com/ludovic111/horizon/issues/1782), after [#978](https://github.com/ludovic111/horizon/issues/978)).
More: [#1884](https://github.com/ludovic111/horizon/issues/1884) · [#1931](https://github.com/ludovic111/horizon/issues/1931)
</details>
#### Chickens flap while they fall
Chickens should flap their wings while they fall, not stand still in the air.
<details><summary>Technical</summary>
Vanilla's `Chicken.aiStep` updates `flap` and `flapSpeed` when the chicken isn't on the ground, and the model's wings follow them. Issue [#1706](https://github.com/ludovic111/horizon/issues/1706).
More: [#1892](https://github.com/ludovic111/horizon/issues/1892)
</details>
### Rendering
#### Shader packs, written again from scratch
@@ -902,56 +962,6 @@ Armour and tools in End city chests should come enchanted, as in vanilla.
</details>
#### Cooldowns drawn on items
After you throw an ender pearl, its slot should show the cooldown sweeping away, as in vanilla.
<details><summary>Technical</summary>
Vanilla draws `ItemCooldowns`' percent as a white overlay over the slot (`GuiGraphicsExtractor.itemDecorations` and `itemCooldown`) in the hotbar and in inventories. Issue [#1763](https://github.com/ludovic111/horizon/issues/1763).
</details>
#### Chat settings and team prefixes
The Hidden and Commands Only chat settings should hide chat, and team prefixes should appear before names in chat.
<details><summary>Technical</summary>
Vanilla checks `ChatVisiblity` before it delivers chat and system messages ([#1376](https://github.com/ludovic111/horizon/issues/1376)), and chat uses the team-formatted display name ([#1378](https://github.com/ludovic111/horizon/issues/1378)). Both issues have exact steps to reproduce.
</details>
#### A pointing hand over buttons
The mouse pointer should turn into a pointing hand over menu buttons, as in vanilla 26.3.
<details><summary>Technical</summary>
26.3's `AbstractWidget` asks for a cursor from `CursorTypes` when it is hovered; winit's `CursorIcon::Pointer` gives the same shape. Issue [#1347](https://github.com/ludovic111/horizon/issues/1347).
</details>
#### Ctrl+Backspace and scroll bars in menus
Ctrl+Backspace should delete a whole word in the creative search, and every list's scroll bar should be draggable.
<details><summary>Technical</summary>
`EditBox` deletes by word with Ctrl ([#1764](https://github.com/ludovic111/horizon/issues/1764)); the Game Rules and Statistics lists' scroll bars can't be dragged ([#1782](https://github.com/ludovic111/horizon/issues/1782), after [#978](https://github.com/ludovic111/horizon/issues/978)).
</details>
#### Chickens flap while they fall
Chickens should flap their wings while they fall, not stand still in the air.
<details><summary>Technical</summary>
Vanilla's `Chicken.aiStep` updates `flap` and `flapSpeed` when the chicken isn't on the ground, and the model's wings follow them. Issue [#1706](https://github.com/ludovic111/horizon/issues/1706).
</details>
## 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)
+2 -1
View File
@@ -17,4 +17,5 @@ by The Pixelify Sans Project Authors, licensed under the SIL Open Font License 1
`crates/horizon-theme/icons/pixel/*.svg` are icons from
[Pixelarticons](https://github.com/halfmage/pixelarticons) by Gerrit Halfmann, under the MIT
license (`crates/horizon-theme/icons/pixel/LICENSE-pixelarticons.txt`).
license (`crates/horizon-theme/icons/pixel/LICENSE-pixelarticons.txt`). `star.svg` is Horizon's own, drawn on
the same grid.
@@ -9,7 +9,7 @@ use horizon_client::mesher::Vertex;
use horizon_client::mobs::{
Mob, MobData, MobTypes, Part, Species, allay,
anim::{self, Poses, State},
aquatic, body_anim as b_anim, equine, guardian,
aquatic, bat, body_anim as b_anim, equine, guardian,
keyframes::AnimationState,
render, shulker, sniffer, species, tame,
};
@@ -108,6 +108,22 @@ fn entity_state(mob: &mut Mob, species: Species, case: &Value) {
let flag = |k: &str| case[k].as_bool().unwrap_or(false);
let num = |k: &str| case[k].as_f64().unwrap_or(0.0) as f32;
match species {
Species::Bat => {
mob.extra.flag = flag("isResting");
mob.anim.states[0].started = case["flyAnimationState"].as_f64().map(|v| v as f32);
mob.anim.states[1].started = case["restAnimationState"].as_f64().map(|v| v as f32);
if let Some(ticks) = case["@batTicks"].as_array() {
for (i, flags) in ticks.iter().enumerate() {
mob.age = i as u32 + 1;
bat::set_resting(mob, flags.as_i64().unwrap_or(0) & 1 != 0);
if flag("@batInFrame") {
bat::tick(mob);
} else {
bat::setup_animation_states(mob);
}
}
}
}
Species::Sheep => mob.extra.anim_ticks = num("eatTick") as i32,
Species::Guardian | Species::ElderGuardian => {
mob.anim.v[guardian::TAIL] = num("tailAnimation");
@@ -377,7 +393,7 @@ pub fn main() -> horizon_client::Result<()> {
mob.extra.age = -24000;
}
mob.age = f("ageInTicks") as u32;
let state = State {
let mut state = State {
pos: f("walkAnimationPos"),
speed: f("walkAnimationSpeed"),
age: f("ageInTicks"),
@@ -389,6 +405,20 @@ pub fn main() -> horizon_client::Result<()> {
};
let mut poses = Poses::new(&model);
entity_state(&mut mob, def.species, case);
if let Some(ticks) = case["@walkTicks"].as_array() {
for speed in ticks {
mob.previous_limb_speed = mob.limb_speed;
mob.limb_speed += (speed.as_f64().unwrap() as f32 - mob.limb_speed) * 0.4;
mob.limb_position += mob.limb_speed;
}
if !case["@alive"].as_bool().unwrap() {
mob.health = 0.0;
}
let extracted = anim::state(&mob, f("@partial"));
state.pos = extracted.pos;
state.speed = extracted.speed;
}
let flag = |k: &str| case[k].as_bool().unwrap_or(false);
let num = |k: &str| case[k].as_f64().unwrap_or(0.0) as f32;
let started = |k: &str| AnimationState {
@@ -456,14 +486,6 @@ pub fn main() -> horizon_client::Result<()> {
roll_amount: num("rollAmount"),
},
),
Species::Bat => b_anim::bat(
p,
s,
flag("isResting"),
&started("flyAnimationState"),
&started("restAnimationState"),
&animations,
),
Species::Armadillo => b_anim::armadillo(
p,
s,
@@ -590,7 +612,11 @@ pub fn main() -> horizon_client::Result<()> {
}
_ => anim::setup(&mob, s, p, &animations),
}
out.push_str(&format!("{}\n", emit(&model, &mut poses, case, layer)));
let mut result = emit(&model, &mut poses, case, layer);
if case["@walkTicks"].is_array() {
result["walk"] = json!([state.pos, state.speed]);
}
out.push_str(&format!("{result}\n"));
}
fs::write(&args[2], out)?;
Ok(())
@@ -49,6 +49,7 @@ struct Scenario {
name: String,
ticks: i32,
time: i64,
difficulty: u8,
rain: bool,
thunder: bool,
origin: [i32; 2],
@@ -85,11 +86,13 @@ fn parse(text: &str) -> Vec<Scenario> {
"scenario" => {
s.name = f[1].to_owned();
s.time = 6000;
s.difficulty = 2;
for kv in &f[2..] {
let (k, v) = kv.split_once('=').unwrap_or((kv, ""));
match k {
"ticks" => s.ticks = v.parse().unwrap(),
"time" => s.time = v.parse().unwrap(),
"difficulty" => s.difficulty = v.parse().unwrap(),
"rain" => s.rain = true,
"thunder" => s.thunder = true,
"levelseed" => {}
@@ -954,6 +957,10 @@ pub fn main() -> horizon_client::Result<()> {
for flag in &spec.flags {
match flag.as_str() {
"persistent" => m.persistent = true,
"named" => {
m.custom_name = Some("Peaceful regression".into());
m.persistent = true;
}
"ridden" => {
m.extra.head = Some("minecraft:red_harness".into());
m.rider = Some(horizon_client::mobs::equine::Rider::default());
@@ -1090,7 +1097,7 @@ pub fn main() -> horizon_client::Result<()> {
block_tags: &items.block_tags,
biomes: &biomes,
mob_griefing: true,
difficulty: LocalDifficulty::new(2, world.day_time, 0, 0.0),
difficulty: LocalDifficulty::new(s.difficulty, world.day_time, 0, 0.0),
end: false,
world_seed: 0,
// FlatLevelSource.getSeaLevel.
+31
View File
@@ -323,10 +323,15 @@ const QUIET_MESHES_PER_S: f64 = 100.0;
/// happened (`tools/perf_bench.py`'s `wait_loaded`). With `status.streaming`, the sections are
/// drawn once no stage has held work for half a second: blocks changing after the load (fluids
/// settling, random ticks) only remesh in bursts. Without it, meshing has to go quiet.
///
/// Right after a teleport (and at spawn) the game reports nothing queued and steady counts for over
/// a second before it starts loading: the counts must change once, or stay full for 5 s, and the
/// resident chunks reach [`view_chunks`], before "steady" means loaded.
fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result<Instant, String> {
let deadline = Instant::now() + LOAD_LIMIT;
let (mut last, mut changed, mut meshing) = (None, Instant::now(), Instant::now());
let mut previous: Option<(Instant, f64)> = None;
let (mut first, mut moved, began) = (None, false, Instant::now());
while Instant::now() < deadline {
let s = control.status();
let now = Instant::now();
@@ -339,6 +344,19 @@ fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result<Insta
(last, changed, previous, meshing) = (None, now, None, now);
} else {
let counts = (s["chunks"].as_u64(), s["generated_chunks"].as_u64());
let resident = counts.0.unwrap_or(0);
let min_chunks = view_chunks(s["render_distance"].as_u64().unwrap_or(0));
let first = *first.get_or_insert(counts);
moved = moved
|| counts != first
|| (min_chunks > 0
&& resident >= min_chunks
&& now - began > Duration::from_secs(5));
if min_chunks > 0 && (!moved || resident < min_chunks) {
(last, changed, previous, meshing) = (None, now, None, now);
thread::sleep(Duration::from_millis(50));
continue;
}
if last != Some(counts) {
(last, changed) = (Some(counts), now);
}
@@ -370,6 +388,12 @@ fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result<Insta
Err(format!("not loaded after {} s", LOAD_LIMIT.as_secs()))
}
/// 80% of the view circle (pi x RD^2): the game keeps 637 columns at RD 12 and 3725 at RD 32
/// (`tools/perf_bench.py`'s `view_chunks`).
fn view_chunks(distance: u64) -> u64 {
(0.8 * std::f64::consts::PI * (distance * distance) as f64) as u64
}
/// Vanilla's yaw facing from `start` to `end`.
fn yaw(start: [f64; 3], end: [f64; 3]) -> f64 {
(-(end[0] - start[0])).atan2(end[2] - start[2]).to_degrees()
@@ -420,6 +444,13 @@ mod tests {
assert!(scenes("nether").is_none());
}
#[test]
fn view_chunks_matches_perf_bench() {
assert_eq!(view_chunks(12), 361);
assert_eq!(view_chunks(32), 2573);
assert_eq!(view_chunks(0), 0);
}
#[test]
fn yaw_matches_vanilla() {
// South (+Z) is 0, west (-X) 90, east (+X) -90.
@@ -558,12 +558,11 @@ fn frac(x: f32) -> f32 {
/// `StringSplitter.splitLines(...).get(0)` for plain text: the first line,
/// wrapped at the last space that fits or mid-word when none does.
fn first_line(line: &str, max_width: f32, advance: &dyn Fn(char) -> f32) -> String {
let chars: Vec<char> = line.chars().collect();
let mut width = 0.0;
let mut last_space = None;
for (i, &c) in chars.iter().enumerate() {
for (i, c) in line.char_indices() {
if c == '\n' {
return chars[..i].iter().collect();
return line[..i].to_owned();
}
if c == ' ' {
last_space = Some(i);
@@ -572,9 +571,11 @@ fn first_line(line: &str, max_width: f32, advance: &dyn Fn(char) -> f32) -> Stri
if width > max_width {
let end = match last_space {
Some(s) if s > 0 => s,
_ => i.max(1),
_ if i > 0 => i,
// The first char alone is too wide: take that one char.
_ => c.len_utf8(),
};
return chars[..end].iter().collect();
return line[..end].to_owned();
}
}
line.to_owned()
@@ -2098,4 +2099,13 @@ mod tests {
assert_eq!(out[1].pos, [2.0, 0.0, 0.0]);
assert_eq!(out[2].uv, [0.5, 0.5]);
}
#[test]
fn first_line_wraps_on_bytes_without_splitting_a_char() {
let advance = |c: char| if c == ' ' { 4.0 } else { 6.0 };
assert_eq!(first_line("ab cd ef", 13.0, &advance), "ab");
assert_eq!(first_line("abcdef", 13.0, &advance), "ab");
assert_eq!(first_line("\u{6771}", 3.0, &advance), "\u{6771}");
assert_eq!(first_line("ab\ncd", 100.0, &advance), "ab");
}
}
+31
View File
@@ -436,3 +436,34 @@ fn the_chat_option_filters_the_lines_both_ways() {
chat.set_abilities(&font, ChatAbilities::new(ChatVisibility::Full));
assert_eq!(shown(&chat), ["client", "server", "player"]);
}
/// The in-frame LAN host's chat: a guest's line is a player message, so the
/// Chat option's player-message permission hides it at Commands Only and
/// Hidden; command feedback (`/list`, `/tp`) is a system message and stays
/// at Commands Only.
#[test]
fn a_guests_line_follows_the_player_message_permission() {
let font = font();
let shown = |visibility, source| {
let mut chat = Chat::default();
chat.set_abilities(&font, ChatAbilities::new(visibility));
chat.add_message(&font, Text::literal("line"), None, source, 0, false);
chat.messages.len()
};
for (visibility, player, system) in [
(ChatVisibility::Full, 1, 1),
(ChatVisibility::System, 0, 1),
(ChatVisibility::Hidden, 0, 0),
] {
assert_eq!(
shown(visibility, ChatSource::Player),
player,
"{visibility:?}"
);
assert_eq!(
shown(visibility, ChatSource::Server),
system,
"{visibility:?}"
);
}
}
+14
View File
@@ -24,6 +24,20 @@ pub fn block_strategy() -> Strategy {
Strategy::blocks(BLOCK_BITS.load(std::sync::atomic::Ordering::Relaxed))
}
/// Frees `columns` (`count` of them) on a short-lived thread of its own
/// when there are many: a teleport or a dimension change drops a few
/// thousand columns at once, and freeing their sections took the frame
/// 15-20 ms. A few are freed here.
pub fn free_columns<T: Send + 'static>(columns: T, count: usize) {
if count < 64 {
return;
}
// Should the thread not start, the closure drops `columns` here.
let _ = std::thread::Builder::new()
.name("Chunk freeing".into())
.spawn(move || drop(columns));
}
/// Sizes the block states' global palette for a registry of `states`.
pub fn set_block_states(states: usize) {
let bits = horizon_core::paletted::ceil_log2(states).max(16);
@@ -275,4 +275,30 @@ mod tests {
Release::Click(Input::QuickMove)
);
}
/// A press whose release never arrives (the pointer left the window, or
/// the screen closed first) leaves `isQuickCrafting` set; the next screen
/// starts with its own state, as `AbstractContainerScreen` does, so the
/// reset the game runs on open/close makes clicks work again.
#[test]
fn a_lost_release_does_not_wedge_the_next_screen() {
let mut clicks = Clicks::default();
let slot = Target::<usize>::Slot(3);
// Pressed with a stack carried: a quick craft drag starts, and its
// release is lost.
assert_eq!(clicks.press(&slot, Button::Left, false, false, false), None);
assert!(clicks.quick_crafting().is_some());
assert_eq!(
clicks.press(&slot, Button::Left, true, false, false),
None,
"the stuck drag swallows the next press"
);
// A new screen: fresh state, so the press is a normal click again.
clicks = Clicks::default();
assert!(clicks.quick_crafting().is_none());
assert_eq!(
clicks.press(&slot, Button::Left, true, false, false),
Some(Input::Pickup)
);
}
}
+15 -1
View File
@@ -49,6 +49,17 @@ pub struct Furnace {
/// rather than per item, and a save keeps it as the `RecipesUsed` compound.
pub recipes_used: HashMap<String, u32>,
}
/// The key `recipes_used` keeps a recipe under: its full id, as vanilla's `ResourceKey<Recipe>`
/// writes it in `RecipesUsed`. Horizon's own recipe ids are file names without the namespace, so
/// keeping both forms would write a key twice, and vanilla refuses such a furnace
/// ("Duplicate entry for key").
pub fn recipe_used_key(id: &str) -> String {
if id.contains(':') {
id.to_owned()
} else {
format!("minecraft:{id}")
}
}
/// A hopper's slots: the only plain container with five.
pub const HOPPER_SLOTS: usize = 5;
@@ -638,7 +649,10 @@ impl Furnace {
Some(out) => out.count += recipe.result.count,
None => self.items[2] = Some(recipe.result.clone()),
}
*self.recipes_used.entry(recipe.id.clone()).or_insert(0) += 1;
*self
.recipes_used
.entry(recipe_used_key(&recipe.id))
.or_insert(0) += 1;
if let Some(i) = self.items[0].as_mut() {
i.count -= 1;
if i.count == 0 {
+2
View File
@@ -1612,7 +1612,9 @@ impl Recipes {
}
/// The `experience` of the cooking recipe with this file name, and 0 for one this data
/// pack does not have (a save from another pack can name it).
/// `id` with or without the `minecraft:` namespace (a furnace keeps the full id).
pub fn cooking_experience(&self, id: &str) -> f32 {
let id = id.strip_prefix("minecraft:").unwrap_or(id);
self.cooking
.iter()
.find(|c| c.id == id)
+4 -5
View File
@@ -221,18 +221,17 @@ pub fn scale(
pub fn draw(font: &Font, columns: [&[String]; 2], scale: f32, width: i32) -> Vec<GuiDraw> {
let mut out = vec![GuiDraw::Push(vec![PoseOp::Scale(scale, scale)])];
for (lines, align_left) in [(columns[0], true), (columns[1], false)] {
let placed: Vec<(&String, i32, i32)> = lines
let placed: Vec<(&String, i32, i32, i32)> = lines
.iter()
.enumerate()
.filter(|(_, l)| !l.is_empty())
.map(|(i, l)| {
let w = font.width(l) as i32;
let left = if align_left { 2 } else { width - 2 - w };
(l, left, 2 + LINE_HEIGHT * i as i32)
(l, left, 2 + LINE_HEIGHT * i as i32, w)
})
.collect();
for (line, left, top) in &placed {
let w = font.width(line) as i32;
for (_, left, top, w) in &placed {
out.push(fill(
left - 1,
top - 1,
@@ -241,7 +240,7 @@ pub fn draw(font: &Font, columns: [&[String]; 2], scale: f32, width: i32) -> Vec
BACKGROUND,
));
}
for (line, left, top) in placed {
for (line, left, top, _) in placed {
out.push(GuiDraw::Text {
text: Text::literal(line.clone()),
x: left,
+25
View File
@@ -1201,6 +1201,31 @@ mod tests {
assert!(!font.take_patches().is_empty());
}
/// A renderer rebuilt over the same font (the loading screen's handoff)
/// makes its atlas texture from the textures alone, so the glyphs it had
/// rasterized into the previous texture must be placed into the new one.
#[test]
fn a_rebuilt_atlas_gets_its_glyphs_again() {
let sheet = png(16, 8, |x, _| x < 2 || (8..14).contains(&x));
let default = br#"{"providers":[{"type":"bitmap","file":"minecraft:font/a.png","ascent":7,"chars":["ab"]}]}"#;
let root = root(
"rebuild",
&[
("assets/minecraft/font/default.json", default),
("assets/minecraft/textures/font/a.png", &sheet),
],
);
let font = load(&root);
let _ = std::fs::remove_dir_all(&root);
let first = font.glyph('a');
assert_eq!(font.take_patches().len(), 1);
font.reset_cache();
// Nothing is left over: drawing the character again places its pixels.
assert!(font.take_patches().is_empty());
assert_eq!(font.glyph('a').advance, first.advance);
assert_eq!(font.take_patches().len(), 1);
}
#[test]
fn provider_filters_follow_the_font_options() {
let sheet = png(8, 8, |x, _| x < 4);
+69 -19
View File
@@ -80,6 +80,7 @@ mod lan_mobs;
mod lan_mount_menu;
mod lan_particles;
mod lan_pvp;
mod lan_quiet;
mod lan_screen;
mod lan_stations;
mod lan_trading;
@@ -147,6 +148,7 @@ mod scoreboard;
mod screens;
mod sculk;
mod sculk_fixture;
mod server_mods;
mod server_packs;
mod server_travel;
mod shader_packs;
@@ -1580,6 +1582,8 @@ struct Game<'a> {
/// position) for the guests.
lan_block_events: Vec<([i32; 3], i32, i32, u32)>,
lan_entity_events: Vec<(i32, i8, [f64; 3])>,
/// Successful crits (attacking guest, hit entity), queued while the hub is taken.
lan_critical_hits: Vec<(Option<u32>, i32)>,
/// Global level events for every guest (`ServerLevel.globalLevelEvent`).
lan_global_events: Vec<(i32, [i32; 3], i32)>,
/// `ContainerOpenersCounter`: how many players (the host and the guests)
@@ -3336,6 +3340,7 @@ impl Game<'_> {
self.pad_rumble(horizon_client::gamepad::Rumble::BlockBroken);
// ServerPlayerGameMode.destroyBlock.
let state = self.world.block(pos[0], pos[1], pos[2]);
let broken = state;
let facts = self.res.facts().get(state).clone();
let state = self.crack_mined_pot(pos, state);
// Block.playerWillDestroy: mining their gold angers all nearby piglins.
@@ -3367,6 +3372,9 @@ impl Game<'_> {
creative,
)
});
if removed {
self.mod_block_event(pos, broken, false);
}
if removed && !creative {
let held = self.held().cloned();
self.mine_block_with_held(&facts.name, facts.hardness);
@@ -3709,6 +3717,7 @@ impl Game<'_> {
return Ok(true);
};
let target = [pos.x, pos.y, pos.z];
self.mod_block_event(target, state, true);
if let Some(entity) = BlockEntity::for_block(&self.res.facts().get(state).name) {
let key = (target[0], target[1], target[2]);
self.world.block_entities.insert(key, entity);
@@ -4242,6 +4251,7 @@ impl Game<'_> {
self.update_totem_ready();
self.tick_command_extras();
self.tick_mods();
self.tick_server_mods();
let camera_eye = self.camera_eye_height();
let stick = self.pad_move();
if self.riding.is_some() && self.player.is_some() {
@@ -4752,7 +4762,10 @@ impl Game<'_> {
if capture && !self.focused {
return;
}
if !capture {
if capture {
// The inventory's screens are behind us: no drag survives them.
self.reset_inventory_clicks();
} else {
self.stop_mining();
}
// The real pointer takes over from an agent's.
@@ -5591,7 +5604,18 @@ impl Game<'_> {
self.draw_inventory_effects(batch, (ox, oy), 176);
}
}
/// `AbstractContainerScreen` keeps `isQuickCrafting` and the drag it
/// belongs to on the screen; Horizon keeps them on the game, so they are
/// dropped when a screen closes. A press whose release never reaches the
/// screen (the pointer left the window, or the screen closed first) would
/// otherwise leave every later press on every later screen swallowed.
pub(super) fn reset_inventory_clicks(&mut self) {
self.inv_clicks = Default::default();
self.inv_drag = None;
self.last_inv_click = None;
}
pub(super) fn close_container(&mut self) {
self.reset_inventory_clicks();
self.close_station();
self.mount_closed();
self.recipe_ui.reset_screen();
@@ -6895,6 +6919,9 @@ impl Game<'_> {
self.prepare_entity_culling(camera);
self.prepare_trims();
self.mark_entity(out.len(), "minecraft:entity_flame");
// One scan for both passes that draw them (block entities below and the
// overlay near the end of this function).
let cracks = self.cracks(camera);
let fire_yaw = if self.camera_type == perspective::CameraType::ThirdPersonFront {
self.yaw + 180.0
} else {
@@ -7120,7 +7147,7 @@ impl Game<'_> {
.render(&scene, visible, &mut out);
}
self.mark_stage(out.len(), "DESTROY");
for (pos, stage) in self.cracks(camera) {
for &(pos, stage) in &cracks {
let state = self.world.block(pos[0], pos[1], pos[2]);
self.res
.block_entity_render()
@@ -7263,7 +7290,7 @@ impl Game<'_> {
);
// Breaking progress overlays (destroy_stage_0..9): ours and the other players'.
self.mark_stage(out.len(), "DESTROY");
for (pos, stage) in self.cracks(camera) {
for &(pos, stage) in &cracks {
let o = (glam::DVec3::new(f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2]))
- camera)
.as_vec3();
@@ -7311,6 +7338,20 @@ impl Game<'_> {
/// other players' (`renderBlockDestroyAnimation`: within 32 blocks of the camera, the
/// furthest progress on each block).
fn cracks(&self, camera: glam::DVec3) -> Vec<([i32; 3], [f32; 2])> {
// `destroy_stage_0` .. `destroy_stage_9`: the sprite name by stage, so the
// lookup does not format a string per crack.
const DESTROY_STAGES: [&str; 10] = [
"block/destroy_stage_0",
"block/destroy_stage_1",
"block/destroy_stage_2",
"block/destroy_stage_3",
"block/destroy_stage_4",
"block/destroy_stage_5",
"block/destroy_stage_6",
"block/destroy_stage_7",
"block/destroy_stage_8",
"block/destroy_stage_9",
];
let mut stages: Vec<([i32; 3], u32)> = Vec::new();
let mut add = |pos: [i32; 3], stage: u32| match stages.iter_mut().find(|c| c.0 == pos) {
Some(c) => c.1 = c.1.max(stage),
@@ -7336,11 +7377,8 @@ impl Game<'_> {
d.length_squared() <= 1024.0
})
.filter_map(|(pos, stage)| {
let r = self
.gui
.atlas
.sprites
.get(&format!("block/destroy_stage_{stage}"))?;
let name = DESTROY_STAGES.get(stage as usize)?;
let r = self.gui.atlas.sprites.get(*name)?;
Some((pos, [r.u0, r.v0]))
})
.collect()
@@ -7605,10 +7643,9 @@ impl Game<'_> {
self.stats
.custom("damage_dealt", (actual * 10.0).round() as i32);
if crit {
self.particles.tracking_emitter(
horizon_client::particles::ParticleOptions::simple(
horizon_client::particles::ParticleType::Crit,
),
self.critical_hit(
self.lan_acting,
self.mobs[index].id as i32,
feet,
width,
height,
@@ -10303,8 +10340,6 @@ impl Game<'_> {
Vec::new()
};
let mut hud = horizon_client::spans::time("hud", || self.hud(size));
// Map textures changed since the last drawn frame (game/maps.rs).
hud.patches.append(&mut self.map_patches);
// LevelLoadingScreen (Reason.OTHER) draws the title's panorama.
let title = self.title.is_some()
|| self.loading_reason() == Some(horizon_client::level_loading::Reason::Other);
@@ -10406,6 +10441,10 @@ impl Game<'_> {
} else {
self.shader_pack_inputs(camera, alpha as f32)
};
// Map textures and font glyphs changed while building this frame's
// geometry (game/maps.rs, world text) upload with this batch; draining
// map_patches any earlier leaves them a frame behind.
hud.patches.append(&mut self.map_patches);
let entity_spans = std::mem::take(self.entity_spans.get_mut());
let frame = Frame {
weather: &weather,
@@ -11021,11 +11060,7 @@ impl Game<'_> {
bytes += size;
}
}
for c in unloaded {
for sy in 0..(HEIGHT / 16) {
renderer.remove((c.x, sy, c.z));
}
}
renderer.remove_columns(unloaded.iter().map(|c| (c.x, c.z)));
if let (Some(lod), Some(player)) = (&mut self.lod, &self.player) {
let lod_distance = self.settings.horizon.lod_distance_on(&renderer.info);
lod.set_near_loading(self.streaming.busy());
@@ -12365,6 +12400,7 @@ impl<'a> Game<'a> {
lan_events: Vec::new(),
lan_block_events: Vec::new(),
lan_entity_events: Vec::new(),
lan_critical_hits: Vec::new(),
lan_global_events: Vec::new(),
lid_openers: HashMap::new(),
lan_acting: None,
@@ -12995,6 +13031,14 @@ fn build_renderer(
) -> Result<Renderer> {
let assets = &pack.render;
let atlas = &gui.atlas;
// The new renderer's atlas texture starts from `HudAtlas.pixels`, which
// holds the textures but never a glyph rasterized at run time. The font's
// cache still answers with the rects it drew into the previous renderer's
// texture, so forget them: the glyphs that are drawn again are placed into
// this texture (as `ui_theme` does when its atlas changes). Without this,
// Chinese and Japanese menu labels go blank when the loading screen hands
// over to the game.
atlas.font.reset_cache();
let mut renderer = pollster::block_on(Renderer::new(
window,
offscreen,
@@ -13041,6 +13085,12 @@ fn loading_renderer(
Some(atlas) => (atlas.pixels.as_slice(), atlas.width, atlas.height),
None => (EMPTY_HUD.as_slice(), 1, 1),
};
// A glyph rasterized into an earlier texture is not in these pixels; the
// new texture gets it again when the character is drawn (see
// `build_renderer`).
if let Some(atlas) = atlas {
atlas.font.reset_cache();
}
let mut renderer = pollster::block_on(Renderer::new(
window,
offscreen,
+29 -4
View File
@@ -464,7 +464,28 @@ impl Game<'_> {
return;
}
// The Chat option's Hidden level takes the server's messages too.
if !self.ui.chat.shows(ChatSource::Server) {
self.local_chat_line(text, Some(SYSTEM_INDICATOR), ChatSource::Server);
}
/// A player's chat line on the window's own chat: a LAN guest's message
/// while the host runs in-frame (`HostEvent::Chat`). `ChatListener`
/// checks `canReceivePlayerMessages` for these, so Commands Only and
/// Hidden drop them; command feedback (`/list`, `/tp`) is a system
/// message and takes the system-message permission instead.
pub(super) fn player_message(&mut self, text: Text) {
if let Some(capture) = self.chat_capture.as_mut() {
capture.push(text);
return;
}
// A server's client receives the line as a packet (PlayerChat or
// DisguisedChat) and filters it there.
if self.remote.is_none() {
self.local_chat_line(text, None, ChatSource::Player);
}
}
/// A line on the window's own chat when the Chat option lets `source`
/// through; running mods hear it.
fn local_chat_line(&mut self, text: Text, indicator: Option<u32>, source: ChatSource) {
if !self.ui.chat.shows(source) {
return;
}
let focused = self.screen == Screen::Chat;
@@ -472,8 +493,8 @@ impl Game<'_> {
self.ui.chat.add(
&self.gui.atlas,
text,
Some(SYSTEM_INDICATOR),
ChatSource::Server,
indicator,
source,
self.ticks,
focused,
);
@@ -602,7 +623,10 @@ impl Game<'_> {
return;
}
if let Some(command) = message.strip_prefix('/') {
self.run_command(command);
// A client mod's command first, then a server mod's, as on a server.
if !self.mod_command(command) && !self.local_server_mod_command(command) {
self.run_command(command);
}
} else {
let line = Text::translate(
"chat.type.text",
@@ -622,6 +646,7 @@ impl Game<'_> {
self.mods_chat(&line);
}
self.lan_host_chat(message);
self.mod_player_chat(super::server_mods::Who::Local, message);
}
}
pub(super) fn chat_scroll(&mut self, amount: f32) {
+8 -3
View File
@@ -52,11 +52,16 @@ impl Game<'_> {
/// Remember who deals the next `damage_type` damage to the player.
pub(super) fn note_attacker(&mut self, damage_type: &str, mob: u32) {
if let Some(name) = self.mob_display_name(mob) {
self.combat
.pending
.push((damage_type.to_owned(), (mob, name)));
self.note_named_attacker(damage_type, mob, name);
}
}
/// `note_attacker` for an entity that is not one of `self.mobs` (the
/// ender dragon).
pub(super) fn note_named_attacker(&mut self, damage_type: &str, id: u32, name: Text) {
self.combat
.pending
.push((damage_type.to_owned(), (id, name)));
}
/// `LivingEntity.handleDamageEvent` for the player: the hurt sound of the
/// source's `DamageEffects` (`Player.getHurtSound`).
pub(super) fn player_hurt_sound(&mut self, sound: &str, at: [f64; 3]) {
+18 -3
View File
@@ -85,16 +85,16 @@ fn format_double(v: f64) -> String {
impl Game<'_> {
/// Commands.performCommand for a line without its slash.
pub(super) fn run_command(&mut self, command: &str) {
pub(super) fn run_command(&mut self, command: &str) -> bool {
if self.mod_command(command) {
return;
return true;
}
let d = vanilla::game_dispatcher();
let parse = d.parse(command, &self.ui.registries);
let result = match parse.executable() {
Err(e) => {
self.syntax_failure(&e, command);
return;
return false;
}
Ok((ctx, _)) => {
let ctx = ctx.clone();
@@ -103,6 +103,7 @@ impl Game<'_> {
self.run_chain(&root, &ctx)
}
};
let ok = result.is_ok();
if let Err(e) = result {
if e.input.is_some() {
self.syntax_failure(&e, command);
@@ -114,6 +115,20 @@ impl Game<'_> {
if self.player.is_some() {
self.check_inventory_trigger();
}
ok
}
/// `run_command` with its output suppressed
/// (`CommandSourceStack.withSuppressedOutput`): the player's own source,
/// rights and checks, no feedback and no failure in the chat.
pub(super) fn run_command_quietly(&mut self, command: &str) -> bool {
let quiet = execute::Source {
silent: true,
..self.player_source()
};
let outer = self.command_source.replace(quiet);
let ok = self.run_command(command);
self.command_source = outer;
ok
}
fn syntax_failure(&mut self, e: &CommandError, command: &str) {
self.command_failure(e.message.clone());
+8 -2
View File
@@ -391,6 +391,9 @@ impl Game<'_> {
}
pub(super) fn mob_world_base<'s>(&'s self, others: &'s [Seen]) -> MobWorld<'s> {
let (rain, thunder) = self.world.weather.levels(1.0);
// Level.canHaveWeather: only the overworld's rain and thunder levels
// ever rise, so nothing rains or thunders in the Nether or the End.
let weather = self.dim == Dim::Overworld;
let player = self.player_view();
MobWorld {
world: &self.world,
@@ -407,8 +410,8 @@ impl Game<'_> {
daylight: self.dim != Dim::Nether && self.sky_darken() < 4,
game_time: self.world.game_time,
day_time: self.world.day_time,
raining: rain > 0.2,
thundering: thunder > 0.9,
raining: weather && rain > 0.2,
thundering: weather && thunder > 0.9,
nether: self.dim == Dim::Nether,
block_tags: &self.res.items().block_tags,
biomes: self.res.biomes(),
@@ -1084,6 +1087,9 @@ impl Game<'_> {
self.ensure_item_particle_sprites();
}
let options = horizon_client::particles::ParticleOptions::simple(ty);
if ty == horizon_client::particles::ParticleType::SonicBoom {
self.lan_sonic_boom(&options, at);
}
// Count 0: one particle exactly there, at rest.
if count == 0 {
self.particles.add(&options, at, [0.0; 3]);
+14 -3
View File
@@ -12,6 +12,7 @@ use horizon_client::{
hud_layout, hud_overlays,
mesher::Vertex,
particles::{ParticleOptions, ParticleType},
text::Text,
};
use horizon_core::{
geometry::{Aabb, Vec3},
@@ -1870,7 +1871,7 @@ impl Game<'_> {
pl.velocity.z += zd / dd * 4.0;
}
if !sitting {
g.hurt_player_by("minecraft:mob_attack", 5.0);
g.dragon_hurts_player("minecraft:mob_attack", 5.0);
}
});
}
@@ -1879,7 +1880,7 @@ impl Game<'_> {
for h in heads {
if h.inflate(Vec3::new(1.0, 1.0, 1.0)).intersects(p.bounds) {
self.as_fight_player(p.who, |g| {
g.hurt_player_by("minecraft:mob_attack", 10.0);
g.dragon_hurts_player("minecraft:mob_attack", 10.0);
});
}
}
@@ -2161,10 +2162,20 @@ impl Game<'_> {
self.breath_clouds.append(&mut clouds);
for (who, amount) in harmed {
self.as_fight_player(who, |g| {
g.hurt_player_by("minecraft:indirect_magic", amount);
g.dragon_hurts_player("minecraft:indirect_magic", amount);
});
}
}
/// Damage from the dragon, which is its causing entity
/// (`DamageSources.mobAttack(dragon)`, `indirectMagic(cloud, dragon)`): the
/// death message names it.
fn dragon_hurts_player(&mut self, damage_type: &str, amount: f32) {
if let Some(id) = self.dragon.as_ref().map(|d| d.id) {
let name = Text::translate("entity.minecraft.ender_dragon", &[]);
self.note_named_attacker(damage_type, id, name);
}
self.hurt_player_by(damage_type, amount);
}
/// `BossHealthOverlay.extractRenderState`: whatever `publish_boss_bars`
/// (or a server's packets) put on this client's overlay.
pub(super) fn draw_boss_bars(&self, batch: &mut HudBatch) {
+3 -1
View File
@@ -222,7 +222,6 @@ impl Game<'_> {
options.report = None;
options.server = None;
options.world = None;
options.dev_mods.clear();
self.join_world_rest()?;
let (res, pack, settings) = (self.res.clone(), self.pack.clone(), self.settings.clone());
let flags = Arc::new(Flags::default());
@@ -292,6 +291,9 @@ impl Game<'_> {
launch.create.as_ref(),
)?)?;
let saved = Box::new(self.level.clone());
// IntegratedServer runs the server mods; the window the client mods.
self.mods.dev = self.options.dev_mods.clone();
self.start_mods();
// Math.max(2, options.renderDistance): past vanilla's 32 when the
// window's render distance is (Horizon's options go to 64).
let view_distance = self.settings.render_distance.max(2);
+57 -1
View File
@@ -71,6 +71,8 @@ pub(super) struct LanHost {
/// Guests' commands waiting for the hub to be back in `self.lan`, where
/// a command can reach every player (game/lan_actions.rs).
pub commands: Vec<super::lan_actions::GuestCommand>,
/// Mods' quiet commands waiting to run a few a tick (game/lan_quiet.rs).
pub quiet: std::collections::VecDeque<super::lan_quiet::QuietBatch>,
/// The entities the guests track, described again every tick
/// (`lan_entities`) over last tick's states.
states: lan::entity::States,
@@ -194,6 +196,13 @@ fn mob_meta(
let e = &m.extra;
let id = |registry: &str, name: &str| hub.registry_id(registry, &format!("minecraft:{name}"));
match kind {
// Bat.DATA_ID_FLAGS: resting in bit 0, flying by default.
"minecraft:bat" => {
if e.flag {
meta.push((16, serializer::BYTE, Value::Byte(1)));
}
defaults.push((16, serializer::BYTE, Value::Byte(0)));
}
// Sheep.DATA_WOOL_ID: the colour in the low nibble, 16 when sheared.
"minecraft:sheep" => {
let value = (e.color & 15) | if e.sheared { 16 } else { 0 };
@@ -596,6 +605,7 @@ impl Game<'_> {
waypoint_dead: Default::default(),
border_sent: None,
commands: Vec::new(),
quiet: std::collections::VecDeque::new(),
states: Default::default(),
});
self.audio.capture = Some(Vec::new());
@@ -634,6 +644,7 @@ impl Game<'_> {
self.world.light_log = None;
self.audio.capture = None;
self.lan_events.clear();
self.lan_critical_hits.clear();
self.streaming.extra.clear();
if announce {
self.system_message(Text::translate(
@@ -960,6 +971,9 @@ impl Game<'_> {
event @ (HostEvent::Chat { .. } | HostEvent::Warn(_) | HostEvent::Error(_)) => {
self.lan_log_event(event);
}
HostEvent::PlayerChat { sender, text } => {
self.mod_player_chat(super::server_mods::Who::Uuid(sender), &text);
}
HostEvent::Joined(conn) => {
self.lan_guest_joined(&mut host, conn, state.as_ref(), entities, spawn);
if self.options.dedicated.is_some()
@@ -1029,6 +1043,14 @@ impl Game<'_> {
}
host.states = states;
}
if !host.quiet.is_empty() {
self.lan = Some(host);
self.run_quiet_commands();
host = self
.lan
.take()
.expect("the hub is back after the quiet commands");
}
self.lan_flush_effects(&mut host);
if !paused {
self.tick_guests(&mut host);
@@ -1162,8 +1184,30 @@ impl Game<'_> {
self.lan_entity_events.push((id, event, at));
}
}
/// `ServerPlayer.crit`: this window's emitter, and an animation for the
/// attacker's client and observers once the hub is back in place.
pub(super) fn critical_hit(
&mut self,
attacker: Option<u32>,
id: i32,
feet: [f64; 3],
width: f64,
height: f64,
) {
use horizon_client::particles::{ParticleOptions, ParticleType};
self.particles.tracking_emitter(
ParticleOptions::simple(ParticleType::Crit),
feet,
width,
height,
);
if self.audio.capture.is_some() {
self.lan_critical_hits.push((attacker, id));
}
}
/// Send the tick's level events, sounds and mob animations.
fn lan_flush_effects(&mut self, host: &mut LanHost) {
let critical_hits = std::mem::take(&mut self.lan_critical_hits);
for (kind, pos, data, acting) in std::mem::take(&mut self.lan_events) {
if kind == 2001 {
host.broken
@@ -1236,6 +1280,14 @@ impl Game<'_> {
seen.insert(m.id, (m.hurt_time, dead, swing));
}
host.mob_state.insert(self.dim, seen);
for (attacker, target) in critical_hits {
let Some(attacker) = attacker.map_or(Some(host.host_entity), |conn| {
host.hub.guest(conn).map(|g| g.entity_id)
}) else {
continue;
};
host.hub.critical_hit(attacker, target);
}
}
/// Keep the chunks around every guest loaded.
@@ -1403,7 +1455,11 @@ impl Game<'_> {
let tag = if secure { "" } else { "[Not Secure] " };
super::dedicated::log("INFO", &format!("{tag}{line}"));
}
HostEvent::Chat { line, .. } => self.system_message(Text::literal(line)),
// Player chat (and `/say`'s or `/me`'s, which are chat messages
// too): the host's Chat option decides, as `ChatListener` does
// for the guests' packets. Command feedback is a system message
// and takes the system-message permission instead.
HostEvent::Chat { line, .. } => self.player_message(Text::literal(line)),
HostEvent::Warn(line) if dedicated => super::dedicated::log("WARN", &line),
HostEvent::Warn(line) => log::warn!("{line}"),
HostEvent::Error(line) if dedicated => super::dedicated::log("ERROR", &line),
+60 -36
View File
@@ -94,11 +94,14 @@ pub(super) struct GuestPlay {
/// A guest's command waiting for the hub (`LanHost::commands`).
pub(super) struct GuestCommand {
conn: u32,
command: String,
permission: u8,
pub(super) conn: u32,
pub(super) command: String,
pub(super) permission: u8,
/// An integrated server's: no level teleports (yet).
integrated: bool,
pub(super) integrated: bool,
/// A mod's quiet command (game/lan_quiet.rs): its output suppressed, as
/// `CommandSourceStack.withSuppressedOutput` does.
pub(super) quiet: bool,
}
/// The game's own state while a guest's is swapped in (`enter_guest`), and
@@ -1210,6 +1213,9 @@ impl Game<'_> {
self.guest_carried_back(host, gi);
}
S::ChatCommand(c) => self.guest_chat_command(host, conn, &c.command),
S::CustomPayload(p) if p.channel == horizon_protocol::quiet::CHANNEL => {
self.guest_quiet_commands(host, conn, &p.data)
}
S::ChatCommandSigned(p) => self.guest_signed_command(host, conn, p),
S::ChangeDifficulty(p) if host.hub.guests[gi].permission >= 2 => {
if !self.level.difficulty_locked && (0..=3).contains(&p.difficulty) {
@@ -1533,6 +1539,10 @@ impl Game<'_> {
);
return;
}
// A server mod's command, for every player whatever their level.
if self.guest_server_mod_command(host, conn, command.trim_start_matches('/')) {
return;
}
// An integrated server runs every player's commands at their own
// permission level (`CommandSourceStack.permissions`); a LAN world
// follows its Allow Commands, and a dedicated server leaves it to
@@ -1559,6 +1569,7 @@ impl Game<'_> {
command: command.trim_start_matches('/').to_owned(),
permission,
integrated,
quiet: false,
});
}
@@ -1573,41 +1584,54 @@ impl Game<'_> {
return;
}
let c = host.commands.remove(0);
let mut host = self.lan.take().expect("checked above");
let Some(swap) = self.enter_guest(&mut host, c.conn) else {
self.lan = Some(host);
continue;
};
self.lan = Some(host);
self.chat_capture = Some(Vec::new());
let level = std::mem::replace(&mut self.ui.registries.permission, c.permission);
self.run_command(&c.command);
self.ui.registries.permission = level;
let lines = self.chat_capture.take().unwrap_or_default();
let mut host = self.lan.take().expect("a command puts the hub back");
self.leave_guest(&mut host, swap);
host.hub.sync_all_effects();
if !c.integrated {
// A teleport into another level happens before its feedback is sent.
self.run_level_teleports(&mut host);
}
// What the command earned is announced before its feedback.
self.announce_guest_advancements(&mut host);
for line in lines {
host.hub.send_to(
c.conn,
&C::SystemChat(play::SystemChat {
content: horizon_client::lan::data::component_nbt(&line.component()),
overlay: false,
}),
);
}
// The other operators hear of it; never the player whose command it was.
self.send_admin_feedback(&mut host, Some(c.conn));
self.lan = Some(host);
self.run_guest_command(c);
}
}
/// One guest's command as that guest, its feedback sent to them; false
/// when it failed (or the guest is gone).
pub(super) fn run_guest_command(&mut self, c: GuestCommand) -> bool {
let Some(mut host) = self.lan.take() else {
return false;
};
let Some(swap) = self.enter_guest(&mut host, c.conn) else {
self.lan = Some(host);
return false;
};
self.lan = Some(host);
self.chat_capture = Some(Vec::new());
let level = std::mem::replace(&mut self.ui.registries.permission, c.permission);
let ok = if c.quiet {
self.run_command_quietly(&c.command)
} else {
self.run_command(&c.command)
};
self.ui.registries.permission = level;
let lines = self.chat_capture.take().unwrap_or_default();
let mut host = self.lan.take().expect("a command puts the hub back");
self.leave_guest(&mut host, swap);
host.hub.sync_all_effects();
if !c.integrated {
// A teleport into another level happens before its feedback is sent.
self.run_level_teleports(&mut host);
}
// What the command earned is announced before its feedback.
self.announce_guest_advancements(&mut host);
for line in lines {
host.hub.send_to(
c.conn,
&C::SystemChat(play::SystemChat {
content: horizon_client::lan::data::component_nbt(&line.component()),
overlay: false,
}),
);
}
// The other operators hear of it; never the player whose command it was.
self.send_admin_feedback(&mut host, Some(c.conn));
self.lan = Some(host);
ok
}
/// The `broadcastToAdmins` lines a command queued (`source`: the guest
/// whose it was; none for the host's or the console's): every other
/// operator gets them, and the console logs them as the server's own
+5 -1
View File
@@ -28,7 +28,11 @@ impl Game<'_> {
/// SignableCommand.of: the command's message arguments, by name, with
/// their text, not past a redirect to the root (`execute ... run`).
fn signable_arguments(&mut self, permission: u8, command: &str) -> Vec<(String, String)> {
pub(super) fn signable_arguments(
&mut self,
permission: u8,
command: &str,
) -> Vec<(String, String)> {
let parse = self.parse_as(permission, command);
let nodes = &vanilla::game_dispatcher().nodes;
let root = parse.context.root;
@@ -25,6 +25,33 @@ fn rgb(c: [f32; 4]) -> [f32; 3] {
}
impl Game<'_> {
/// `SonicBoom.tick` uses `ServerLevel.sendParticles` for each ray point.
/// Keep the local event's rendering; connected players receive one at rest.
pub(super) fn lan_sonic_boom(&self, options: &ParticleOptions, at: [f64; 3]) {
let (Some(host), Some(particle)) = (&self.lan, self.network_particle(options, None)) else {
return;
};
let packet = C::LevelParticles(play::LevelParticles {
particle,
override_limiter: false,
always_show: false,
pos: at,
dist: [0.0; 3],
max_speed: [0.0; 3],
count: 0,
randomization: 0,
});
for g in host.hub.guests.iter().filter(|g| host.hub.here(g)) {
// ServerLevel compares the player's block centre, strictly within 32.
let distance: f64 = (0..3)
.map(|k| (g.position[k].floor() + 0.5 - at[k]).powi(2))
.sum();
if distance < 32.0 * 32.0 {
g.send(&packet);
}
}
}
/// `ParticleTypes.STREAM_CODEC`'s value for `options`: the type's
/// network id and the data it carries (`state` for the block particles).
/// None while not serving, or for a particle whose data isn't sent from
+22
View File
@@ -32,6 +32,8 @@ pub(super) enum Victim {
/// The attacker's side of the hit.
pub(super) struct Strike {
/// The attacking guest, kept after their turn through `as_guest` ends.
attacker: Option<u32>,
amount: f32,
scale: f32,
crit: bool,
@@ -119,6 +121,7 @@ impl Game<'_> {
}
self.swing_arm();
Some(Strike {
attacker: self.lan_acting,
amount,
scale,
crit,
@@ -250,6 +253,25 @@ impl Game<'_> {
Victim::Host => land(self),
Victim::Guest(conn) => self.guest_struck_by(host, conn, land).unwrap_or(false),
};
if hurt && s.crit {
let target = match victim {
Victim::Host => self.player.as_ref().map(|p| (host.host_entity, p)),
Victim::Guest(conn) => host
.hub
.guest(conn)
.and_then(|g| g.motion.as_deref().map(|p| (g.entity_id, p))),
};
if let Some((id, p)) = target {
let d = p.dimensions;
self.critical_hit(
s.attacker,
id,
[p.position.x, p.position.y, p.position.z],
f64::from(d.width),
f64::from(d.height),
);
}
}
let sound = match (hurt, s.crit, s.scale > 0.9) {
(false, ..) => "minecraft:entity.player.attack.nodamage",
(true, true, _) => "minecraft:entity.player.attack.crit",
+165
View File
@@ -0,0 +1,165 @@
//! Quiet commands (`horizon_protocol::quiet`): a Horizon client's mod sends a
//! batch of command lines in one payload, and the server runs each exactly as
//! that player typing it would (`guest_command`'s checks, the player's own
//! permission level, at the time it runs) with its output suppressed, as
//! vanilla's functions run (`CommandSourceStack.withSuppressedOutput`): no
//! feedback and no failure in anyone's chat. A batch waits in a queue and runs
//! [`PER_TICK`] lines a player a tick, so a mod can't flood a server; each
//! finished batch is one line in the server's log.
use super::{Game, lan::LanHost, lan_actions::GuestCommand};
use horizon_protocol::quiet;
use std::collections::{HashMap, VecDeque};
/// Lines a player may have waiting at most; a batch past it loses the rest.
pub(super) const PENDING: usize = 4096;
/// Lines run a player a tick at most.
pub(super) const PER_TICK: usize = 64;
/// A batch waiting to run.
pub(super) struct QuietBatch {
conn: u32,
mod_id: String,
lines: VecDeque<String>,
ran: usize,
failed: usize,
/// Lines past [`PENDING`], never run.
dropped: usize,
}
impl Game<'_> {
/// A `horizon:quiet_commands` payload from `conn`: queued (or logged as
/// refused when it breaks the payload's caps).
pub(super) fn guest_quiet_commands(&mut self, host: &mut LanHost, conn: u32, data: &[u8]) {
let Some(name) = host.hub.guest(conn).map(|g| g.name.clone()) else {
return;
};
let batch = match quiet::decode(data) {
Ok(b) => b,
Err(e) => {
super::dedicated::log(
"WARN",
&format!("{name} sent quiet commands that were refused: {e}"),
);
return;
}
};
let waiting: usize = host
.quiet
.iter()
.filter(|b| b.conn == conn)
.map(|b| b.lines.len())
.sum();
let room = PENDING.saturating_sub(waiting);
let dropped = batch.lines.len().saturating_sub(room);
host.quiet.push_back(QuietBatch {
conn,
mod_id: batch.mod_id,
lines: batch.lines.into_iter().take(room).collect(),
ran: 0,
failed: 0,
dropped,
});
}
/// The waiting batches, [`PER_TICK`] lines a player this tick, in order.
pub(super) fn run_quiet_commands(&mut self) {
let mut left: HashMap<u32, usize> = HashMap::new();
let mut i = 0;
loop {
let Some(host) = self.lan.as_mut() else {
return;
};
let Some(batch) = host.quiet.get_mut(i) else {
return;
};
let conn = batch.conn;
let budget = left.entry(conn).or_insert(PER_TICK);
let gone = host.hub.guest(conn).is_none();
if gone || batch.lines.is_empty() {
let batch = host.quiet.remove(i).expect("got it above");
if !gone {
let name = host
.hub
.guest(conn)
.map_or(String::new(), |g| g.name.clone());
super::dedicated::log("INFO", &summary(&name, &batch));
}
continue;
}
if *budget == 0 {
i += 1;
continue;
}
*budget -= 1;
let line = batch.lines.pop_front().expect("not empty");
let ok = self.run_quiet_line(conn, &line);
if let Some(batch) = self.lan.as_mut().and_then(|h| h.quiet.get_mut(i)) {
batch.ran += 1;
batch.failed += usize::from(!ok);
}
}
}
/// One line as `guest_command` would run it for `conn`, quietly. False
/// when it is refused or fails. A command that carries a chat message
/// (`say`, `msg`...) is refused: it would need the player's signature.
fn run_quiet_line(&mut self, conn: u32, line: &str) -> bool {
let allowed = self.options.dedicated.is_some() || self.allow_commands();
let Some(host) = self.lan.as_ref() else {
return false;
};
let integrated = host.hub.config.integrated;
if !integrated && (!host.settings.guest_commands || !allowed) {
return false;
}
let Some(permission) = host.hub.guest(conn).map(|g| g.permission) else {
return false;
};
let command = line.trim_start_matches('/');
if !self.signable_arguments(permission, command).is_empty() {
return false;
}
self.run_guest_command(GuestCommand {
conn,
command: command.to_owned(),
permission,
integrated,
quiet: true,
})
}
}
/// The batch's line in the server's log.
fn summary(name: &str, b: &QuietBatch) -> String {
let mut line = format!(
"{name} ran {} quiet command(s) from mod {}: {} failed",
b.ran, b.mod_id, b.failed
);
if b.dropped > 0 {
line += &format!(", {} dropped (more than {PENDING} waiting)", b.dropped);
}
line
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_log_line_counts_the_batch() {
let b = QuietBatch {
conn: 1,
mod_id: "orbital".into(),
lines: VecDeque::new(),
ran: 12,
failed: 3,
dropped: 0,
};
assert_eq!(
summary("Alex", &b),
"Alex ran 12 quiet command(s) from mod orbital: 3 failed"
);
let b = QuietBatch { dropped: 5, ..b };
assert!(summary("Alex", &b).ends_with(", 5 dropped (more than 4096 waiting)"));
}
}
+3
View File
@@ -74,6 +74,7 @@ pub(super) struct Parked {
lan_events: Vec<(i32, [i32; 3], i32, Option<u32>)>,
lan_block_events: Vec<([i32; 3], i32, i32, u32)>,
lan_entity_events: Vec<(i32, i8, [f64; 3])>,
lan_critical_hits: Vec<(Option<u32>, i32)>,
/// Only the per-level parts of these are used (`swap_level`).
anims: BlockEntityAnims,
raiding: Raiding,
@@ -159,6 +160,7 @@ impl Game<'_> {
lan_events: Vec::new(),
lan_block_events: Vec::new(),
lan_entity_events: Vec::new(),
lan_critical_hits: Vec::new(),
anims: Default::default(),
raiding: Default::default(),
sculk: Default::default(),
@@ -209,6 +211,7 @@ impl Game<'_> {
swap(&mut self.lan_events, &mut p.lan_events);
swap(&mut self.lan_block_events, &mut p.lan_block_events);
swap(&mut self.lan_entity_events, &mut p.lan_entity_events);
swap(&mut self.lan_critical_hits, &mut p.lan_critical_hits);
let (a, b) = (&mut self.be_anims, &mut p.anims);
swap(&mut a.chests, &mut b.chests);
swap(&mut a.books, &mut b.books);
+144 -31
View File
@@ -17,6 +17,7 @@ use std::{path::PathBuf, time::Duration};
#[cfg(feature = "mods")]
use {
super::Screen,
super::{execution_role::ExecutionRole, lan::LanHost, server_mods::ModEvent},
horizon_client::{
extras::Corner,
extras_hud,
@@ -28,7 +29,8 @@ use {
ui,
},
horizon_mods::{
Clock, ModKey, Mods, Output, Player, Runs, Snapshot, Status, UiScreen, UiWidget,
Clock, ModKey, Mods, Output, Player, Runs, ServerPlayer, Snapshot, Status, UiScreen,
UiWidget,
},
std::time::Instant,
winit::{event::MouseButton, keyboard::KeyCode},
@@ -52,6 +54,15 @@ pub(super) struct ModsState {
/// The chat's command tree while a running mod owns commands.
#[cfg(feature = "mods")]
tree: Option<Dispatcher>,
/// A server mod runs here: what players do is queued for it.
#[cfg(feature = "mods")]
pub(super) server: bool,
/// The players server mods saw last tick, for joins and leaves.
#[cfg(feature = "mods")]
pub(super) players: Vec<ServerPlayer>,
/// What players did this tick, for server mods at its end.
#[cfg(feature = "mods")]
pub(super) events: Vec<ModEvent>,
/// `command.run` lines for this window's own world, run on the next tick:
/// run at once, their chat lines would reach the mods while their output
/// is still being shown, and a mod answering its own lines would recurse.
@@ -272,7 +283,7 @@ impl Game<'_> {
.collect();
let enabled = &self.settings.mods.enabled;
let on = |id: &str| enabled.contains(id) || dev_ids.iter().any(|d| d == id);
let mods = Mods::load_for(Runs::Client, &paths, on, vanilla_command);
let mods = Mods::load_for(self.mod_runs(), &paths, on, vanilla_command);
self.mods.mods = mods;
let chosen = &self.settings.mods;
self.mods.mods.set_bindings(|id| chosen.keys_of(id));
@@ -282,8 +293,18 @@ impl Game<'_> {
self.sync_mod_commands();
self.show_mod_output();
}
/// Which mods this game runs: a server the server mods, the window
/// client mods, and both when it simulates the world itself (in-frame).
fn mod_runs(&self) -> Runs {
match self.options.execution_role {
ExecutionRole::IntegratedServer | ExecutionRole::DedicatedServer => Runs::Server,
ExecutionRole::Client if self.options.server_thread => Runs::Client,
ExecutionRole::Client => Runs::Both,
}
}
/// The Mods page's rows, from what loaded.
fn list_mods(&mut self) {
self.mods.server = self.mods.mods.runs_server();
let dev = &self.mods.dev;
self.settings.mods.installed = self
.mods
@@ -369,8 +390,18 @@ impl Game<'_> {
self.start_mods();
}
}
/// What mods read this tick (or frame, for mods with a `frame` hook).
/// What mods read this tick (or frame, for mods with a `frame` hook): the
/// server's players as they were at the last tick's end.
fn mod_snapshot(&mut self) -> Snapshot {
let players = if self.mods.server {
self.mods.players.clone()
} else {
Vec::new()
};
self.mod_snapshot_with(players)
}
/// What mods read, with these players on the server.
pub(super) fn mod_snapshot_with(&mut self, players: Vec<ServerPlayer>) -> Snapshot {
let player = self.player.as_ref().map(|p| Player {
x: p.position.x,
y: p.position.y,
@@ -414,9 +445,66 @@ impl Game<'_> {
mappings,
cps,
font_widths: self.mods.font_widths.clone(),
players: Vec::new(),
players,
view_hit: self.mod_view_hit(),
}
}
/// The first block along the player's view within
/// [`horizon_mods::RAYCAST_MAX`] and its distance from the eye
/// (`game.raycast`): through the loaded chunks as the crosshair's ray
/// goes (fluids pass), then across the distant terrain's ground heights,
/// a block at a time.
fn mod_view_hit(&self) -> Option<(f32, horizon_mods::Hit)> {
if !self.mods.mods.wants_raycast() {
return None;
}
let eye = self.eye_position()?;
let dir = super::view_vector(self.yaw, self.pitch);
let reach = f64::from(horizon_mods::RAYCAST_MAX);
let (pos, point, state) = match self.clip_ray(eye, dir, reach) {
Some(hit) => (
hit.pos,
hit.point,
self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]),
),
None => self.lod_ray(eye, dir, reach)?,
};
let distance = (0..3).map(|i| (point[i] - eye[i]).powi(2)).sum::<f64>();
let hit = horizon_mods::Hit {
block_x: pos[0],
block_y: pos[1],
block_z: pos[2],
x: point[0],
y: point[1],
z: point[2],
block: self.res.facts().get(state).name.to_string(),
};
Some((distance.sqrt() as f32, hit))
}
/// Past the resident chunks: the view walked across the distant
/// terrain's ground heights (`Lod::ground`), a block at a time. Columns in
/// loaded chunks are left to `clip_ray`, which has already missed them.
fn lod_ray(
&self,
eye: [f64; 3],
dir: [f64; 3],
reach: f64,
) -> Option<([i32; 3], [f64; 3], horizon_world::registry::BlockStateId)> {
let lod = self.lod.as_ref()?;
let mut t = 0.0;
while t <= reach {
let p = [0, 1, 2].map(|i| eye[i] + dir[i] * t);
let (x, z) = (p[0].floor() as i32, p[2].floor() as i32);
if !self.world.loaded(x, z)
&& let Some((height, state)) = lod.ground(x, z)
&& p[1] <= f64::from(height)
{
return Some(([x, height - 1, z], [p[0], f64::from(height), p[2]], state));
}
t += 1.0;
}
None
}
/// A game tick.
pub(super) fn tick_mods(&mut self) {
// Here, at the player's level: shown inside a console call (a control
@@ -433,23 +521,32 @@ impl Game<'_> {
self.mods.mods.tick(self.ticks);
self.show_mod_output();
}
/// Once per frame: the frame hook, and dev mods' rebuilds.
pub(super) fn mods_frame(&mut self, elapsed: Duration) {
if !self.mods.dev.is_empty()
&& self
/// Restarts the dev mods rebuilt since the last check, once a second.
pub(super) fn reload_dev_mods(&mut self) {
if self.mods.dev.is_empty()
|| self
.mods
.checked
.is_none_or(|t| t.elapsed() >= RELOAD_CHECK)
.is_some_and(|t| t.elapsed() < RELOAD_CHECK)
{
self.mods.checked = Some(Instant::now());
if self.mods.mods.reload_changed(vanilla_command) {
let chosen = &self.settings.mods;
self.mods.mods.set_bindings(|id| chosen.keys_of(id));
self.list_mods();
self.sync_mod_commands();
return;
}
self.mods.checked = Some(Instant::now());
if self.mods.mods.reload_changed(vanilla_command) {
let chosen = &self.settings.mods;
self.mods.mods.set_bindings(|id| chosen.keys_of(id));
self.list_mods();
self.sync_mod_commands();
if self.options.execution_role.is_client() {
self.system_message(Text::literal("Mods reloaded").with_color(text::GRAY));
} else {
log::info!("Horizon: mods reloaded");
}
}
}
/// Once per frame: the frame hook, and dev mods' rebuilds.
pub(super) fn mods_frame(&mut self, elapsed: Duration) {
self.reload_dev_mods();
if !self.mods.mods.is_running() {
return;
}
@@ -480,8 +577,13 @@ impl Game<'_> {
self.mods.mods.chat(line);
self.show_mod_output();
}
/// Chat lines, command feedback and stopped mods, into the chat.
fn show_mod_output(&mut self) {
/// What mods showed: client mods' lines into the chat, server mods'
/// to their players.
pub(super) fn show_mod_output(&mut self) {
self.route_mod_output(None);
}
/// [`Game::show_mod_output`], with the hub when it is out of `self.lan`.
pub(super) fn route_mod_output(&mut self, mut host: Option<&mut LanHost>) {
for output in self.mods.mods.drain() {
let line = match output {
Output::Chat { name, text } => {
@@ -509,7 +611,16 @@ impl Game<'_> {
// `mod_ui_view` finds no widgets and the screen closes.
self.list_mods();
self.sync_mod_commands();
Text::literal(format!("Mod {name} stopped: {reason}")).with_color(text::RED)
let line = format!("Mod {name} stopped: {reason}");
if self.options.dedicated.is_some() {
super::dedicated::log("WARN", &line);
}
Text::literal(line).with_color(text::RED)
}
server
@ (Output::Tell { .. } | Output::Broadcast { .. } | Output::Reply { .. }) => {
self.route_server_output(host.as_deref_mut(), server);
continue;
}
Output::OpenScreen { owner } => {
self.open_mod_screen(owner, false);
@@ -525,20 +636,8 @@ impl Game<'_> {
}
continue;
}
// Server mods' lines: this game runs no server mod yet.
Output::Tell { .. } | Output::Broadcast { .. } | Output::Reply { .. } => continue,
};
// Not through system_message: a mod must not hear its own lines.
// Written by this client, so the Chat option doesn't hide it.
let focused = self.screen == Screen::Chat;
self.ui.chat.add(
&self.gui.atlas,
line,
None,
horizon_client::chat::ChatSource::Client,
self.ticks,
focused,
);
self.add_mod_chat_line(line);
}
// A mod may set or clear its settings page in any call, not only
// while loading: its row's Settings button follows.
@@ -547,6 +646,20 @@ impl Game<'_> {
row.has_settings = e.has_ui_settings();
}
}
/// A mod's line in the window's chat; not through system_message: a mod
/// must not hear its own lines.
/// Written by this client, so the Chat option doesn't hide it.
pub(super) fn add_mod_chat_line(&mut self, line: Text) {
let focused = self.screen == Screen::Chat;
self.ui.chat.add(
&self.gui.atlas,
line,
None,
horizon_client::chat::ChatSource::Client,
self.ticks,
focused,
);
}
/// The mods' HUD panels, stacked with the extras in their corners.
pub(super) fn draw_mod_panels(&self, batch: &mut HudBatch, corners: &mut Corners) {
for panel in self.mods.mods.panels() {
+7 -3
View File
@@ -86,6 +86,10 @@ impl Game<'_> {
.iter()
.filter_map(|m| Some((m, m.custom_name.clone()?, false)));
let players = players.into_iter().map(|(m, name)| (m, name, true));
// Who is riding whom: asked once per tag below, and rescanning every mob
// for each tag made this quadratic in the number of loaded mobs.
let ridden: std::collections::HashSet<u32> =
self.mobs.iter().filter_map(|o| o.extra.mount).collect();
for (m, name, player) in named.chain(players) {
if !m.alive() {
continue;
@@ -134,7 +138,7 @@ impl Game<'_> {
invisible_to_viewer: invisible,
hud_hidden: self.ui.hide_gui,
is_camera: false,
is_vehicle: self.mobs.iter().any(|o| o.extra.mount == Some(m.id)),
is_vehicle: ridden.contains(&m.id),
};
// MobRenderer.shouldShowName: a visible name, or the mob under the
// crosshair; players (LivingEntityRenderer) need neither.
@@ -310,7 +314,7 @@ impl Game<'_> {
}
/// The lines of one name tag, `o` from the camera: the text at
/// `text_alpha` in its layer, on the translucent black background
/// (unlit, a hair behind) when `background`.
/// (in `text_layer` like the glyphs, a hair behind) when `background`.
#[allow(clippy::too_many_arguments)]
fn name_tag_quads(
&self,
@@ -382,7 +386,7 @@ impl Game<'_> {
[white.u0, white.v0, white.u1, white.v1],
BACKGROUND,
true,
2,
text_layer,
);
}
let mut x = x0;
+23 -1
View File
@@ -3,7 +3,10 @@
//! window, waits until it stands in the world with the chunks around it,
//! sends a chat message and `/list`, collects the chat it is shown, leaves
//! and writes a JSON report (position, chunks, chat lines, whether the
//! server kept it connected throughout).
//! server kept it connected throughout). With `HORIZON_NET_QUIET` (lines
//! split by `;`) it then sends them as one batch of a mod's quiet commands
//! (`horizon_protocol::quiet`, `tools/quiet_commands_e2e.py`) and waits two
//! seconds for any chat they bring.
use super::Game;
use horizon_client::Result;
use serde_json::json;
@@ -23,6 +26,9 @@ impl Game<'_> {
let mut step_at = Instant::now();
let mut chat_at = 0;
let mut position = None;
let quiet: Vec<String> = std::env::var("HORIZON_NET_QUIET")
.map(|v| v.split(';').map(str::to_owned).collect())
.unwrap_or_default();
let mut chunks = 0;
let mut error = None;
while Instant::now() < deadline {
@@ -71,6 +77,22 @@ impl Game<'_> {
step_at = now;
chat_at = shown;
}
2 if answered && !quiet.is_empty() => {
for data in horizon_protocol::quiet::encode("net_harness", &quiet) {
self.send(horizon_protocol::packets::play::Serverbound::CustomPayload(
horizon_protocol::packets::configuration::CustomPayload {
channel: horizon_protocol::quiet::CHANNEL.into(),
data,
},
));
}
step = 4;
step_at = now;
}
4 if now - step_at > Duration::from_secs(2) => {
step = 3;
break;
}
2 if answered => {
step = 3;
break;
+48 -24
View File
@@ -11,7 +11,7 @@ use super::{Dim, Game, Mining, Screen};
use horizon_client::first_person::{Hand, SwingAnimation};
use horizon_client::{
Result,
chunk::{Chunk, SECTIONS},
chunk::Chunk,
containers::{BlockEntity, Furnace, FurnaceKind},
entities::{Experience, ExperienceOrb, ItemEntity},
hud::{HudBatch, rgba},
@@ -839,7 +839,10 @@ impl Game<'_> {
remote.brand = brand;
}
}
Event::Reconfigured(registries) => self.set_registries(registries),
Event::Reconfigured(registries) => {
self.boss_overlay.reset();
self.set_registries(registries);
}
// handleTransfer: "Cannot transfer to server from
// singleplayer" (no ServerData), so the world just closes.
Event::Transfer { host, port } => {
@@ -998,6 +1001,9 @@ impl Game<'_> {
self.world.day_time,
self.world.weather,
);
let old = std::mem::take(&mut self.world.chunks);
let count = old.len();
horizon_client::chunk::free_columns(old, count);
self.world = World::new(self.res.facts().air, spawn.seed);
self.world.game_time = time;
self.world.day_time = day;
@@ -1059,14 +1065,14 @@ impl Game<'_> {
if gone.is_empty() {
return;
}
let mut freed = Vec::with_capacity(gone.len());
for &(x, z) in &gone {
self.streaming.forget(&mut self.world, x, z);
if let Some(renderer) = self.renderer.as_mut() {
for sy in 0..SECTIONS as i32 {
renderer.remove((x, sy, z));
}
}
freed.extend(self.streaming.forget(&mut self.world, x, z));
}
if let Some(renderer) = self.renderer.as_mut() {
renderer.remove_columns(gone.iter().copied());
}
horizon_client::chunk::free_columns(freed, gone.len());
// One pass for all of them.
self.world
.block_entities
@@ -1631,9 +1637,7 @@ impl Game<'_> {
.block_entities
.retain(|k, _| (k.0 >> 4, k.2 >> 4) != (x, z));
if let Some(renderer) = self.renderer.as_mut() {
for sy in 0..SECTIONS as i32 {
renderer.remove((x, sy, z));
}
renderer.remove_columns([(x, z)]);
}
}
C::ChunkBatchFinished(_) => {
@@ -3061,6 +3065,12 @@ impl Game<'_> {
fn tick_remote_level(&mut self) -> Result<()> {
self.ticks += 1;
self.track_experience_display();
// Player.tick's ItemCooldowns.tick: this path replaces `tick_world`'s
// for a server's client (multiplayer and the default single player),
// so the entries advance here or the slot stays shaded until the
// server says the entry ended. `onCooldownEnded` is the plain
// ItemCooldowns' no-op on a client.
self.cooldowns.tick();
// Options.broadcastOptions: a changed render distance or Chat option
// is sent again.
let view = client_view_distance(self.settings.render_distance);
@@ -3491,6 +3501,9 @@ impl Game<'_> {
if m.species() == horizon_client::mobs::Species::Rabbit {
horizon_client::mobs::rabbit::post_tick(m);
}
if m.species() == horizon_client::mobs::Species::Bat {
horizon_client::mobs::bat::setup_animation_states(m);
}
}
for o in self.orbs.iter_mut().filter(|_| running) {
o.previous = o.position;
@@ -3820,19 +3833,25 @@ impl Game<'_> {
.and_modify(|p| p.0 = sequence)
.or_insert((sequence, state.0 as i32));
}
let s = self.res.facts().get(state).sounds.clone();
let at = [
f64::from(pos[0]) + 0.5,
f64::from(pos[1]) + 0.5,
f64::from(pos[2]) + 0.5,
];
self.audio.play(
&s.break_sound,
Some(at),
(s.volume + 1.0) / 2.0,
s.pitch * 0.8,
);
self.destroy_particles(pos, state);
// Block.playerWillDestroy's destroy event (particles and the break
// sound), which BaseFireBlock skips: the server's extinguish sound
// is all a player hears.
let facts = self.res.facts().get(state);
if !facts.has_class("BaseFireBlock") {
let s = facts.sounds.clone();
let at = [
f64::from(pos[0]) + 0.5,
f64::from(pos[1]) + 0.5,
f64::from(pos[2]) + 0.5,
];
self.audio.play(
&s.break_sound,
Some(at),
(s.volume + 1.0) / 2.0,
s.pitch * 0.8,
);
self.destroy_particles(pos, state);
}
// The server's block update follows (waterlogged blocks leave water).
let air = self.world.air;
if self.world.set(pos[0], pos[1], pos[2], air).is_ok() {
@@ -4506,6 +4525,7 @@ impl Game<'_> {
}
KeyCode::KeyE => {
// The inventory opens without telling the server.
self.recipe_ui.reset_screen();
self.screen = if self.creative {
Screen::Creative
} else {
@@ -4772,6 +4792,7 @@ impl Game<'_> {
self.remote_close_local();
}
fn remote_close_local(&mut self) {
self.recipe_ui.reset_screen();
if let Some(r) = self.remote.as_mut() {
r.menu = None;
r.menu_title = None;
@@ -4842,6 +4863,9 @@ impl Game<'_> {
}
/// ClientboundOpenScreenPacket for the menus Horizon draws.
fn remote_open_screen(&mut self, p: play::OpenScreen) {
// A new RecipeBookComponent owns fresh ghost slots and an overlay,
// even when the server replaces a menu without a close packet.
self.recipe_ui.reset_screen();
let menu = self
.remote
.as_ref()
@@ -4,7 +4,7 @@
//! own simulation, which a client of a server doesn't run.
use super::Game;
use horizon_client::mobs::render::{PROFESSIONS, VILLAGER_TYPES};
use horizon_client::mobs::{Mob, Species, tame};
use horizon_client::mobs::{Mob, Species, bat, tame};
use horizon_protocol::entity_data::Value;
use horizon_protocol::registry::Builtin;
@@ -46,6 +46,8 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) {
_ => {}
}
match (species, *index, value) {
// Bat.DATA_ID_FLAGS: resting in bit 0.
(Species::Bat, 16, Value::Byte(flags)) => bat::set_resting(m, flags & 1 != 0),
// Sheep.DATA_WOOL_ID: the colour in the low nibble, 16 sheared.
(Species::Sheep, 18, Value::Byte(wool)) => {
m.extra.color = (*wool & 15) as u8;
@@ -126,6 +128,10 @@ pub(super) fn client_defaults(m: &mut Mob) {
if matches!(m.species(), Species::Wolf | Species::Cat) {
m.extra.color = tame::RED;
}
if m.species() == Species::Bat {
// Bat's constructor sets resting only on the server; the data default is 0.
bat::set_resting(m, false);
}
}
#[cfg(test)]
@@ -174,6 +180,69 @@ mod tests {
assert!(!calf.is_baby());
}
#[test]
fn bat_flags_select_continuous_client_clocks_without_changing_motion() {
use horizon_client::mobs::{MobTypes, anim, keyframes::Animation};
use horizon_core::geometry::Vec3;
use serde_json::json;
use std::{collections::HashMap, fs, sync::Arc};
// Original minimal resources keep this regression runnable without vanilla data.
let root =
std::env::temp_dir().join(format!("horizon-bat-animation-{}", std::process::id()));
fs::create_dir(&root).unwrap();
fs::write(root.join("entity-types.json"), r#"{"minecraft:bat": {}}"#).unwrap();
fs::write(
root.join("entity-models.json"),
r#"{"minecraft:bat#main": {"children": {"right_wing": {}, "head": {}}}}"#,
)
.unwrap();
let mut types = MobTypes::load(&root).unwrap();
fs::remove_dir_all(root).unwrap();
let position = Vec3::new(1.0, 2.25, 3.0);
let velocity = Vec3::new(0.2, 0.3, 0.4);
let mut m = Mob::new(
1,
Arc::new(types.types.remove("minecraft:bat").unwrap()),
position,
0.0,
);
assert!(bat::resting(&m));
client_defaults(&mut m);
assert!(!bat::resting(&m));
m.velocity = velocity;
m.age = 1;
bat::setup_animation_states(&mut m);
m.age = 4;
apply(&mut m, &[(16, s::BYTE, Value::Byte(2))]);
bat::setup_animation_states(&mut m);
assert_eq!(m.anim.states[0].started, Some(1.0));
let curve = Animation::parse(&json!({"length": 1, "looping": true,
"bones": {"right_wing": [{"target": "rotation", "keyframes":
[[0,[0,0,0],[0,0,0],"linear"],[1,[0,1,0],[0,1,0],"linear"]]}]}}));
let animations = HashMap::from([("BatAnimation.BAT_FLYING".into(), curve)]);
let wing = |partial| {
let mut poses = anim::Poses::new(&m.kind.model);
anim::setup(&m, &anim::state(&m, partial), &mut poses, &animations);
poses.get("right_wing")[4]
};
assert!(wing(0.5) > wing(0.0));
m.age = 5;
apply(&mut m, &[(16, s::BYTE, Value::Byte(3))]);
bat::setup_animation_states(&mut m);
assert!(bat::resting(&m));
assert_eq!(m.anim.states[0].started, None);
assert_eq!(m.anim.states[1].started, Some(5.0));
m.age = 7;
apply(&mut m, &[(16, s::BYTE, Value::Byte(0))]);
bat::setup_animation_states(&mut m);
assert_eq!(m.anim.states[0].started, Some(7.0));
assert_eq!(m.anim.states[1].started, None);
assert_eq!((m.position, m.velocity), (position, velocity));
}
#[test]
fn a_wolf_takes_its_owner_flags_and_collar() {
let Some(mut m) = mob("minecraft:wolf") else {
@@ -0,0 +1,356 @@
//! Server mods in the game (`docs/mods.md`, "Server mods"): the game that
//! simulates the world (in-frame single player, the server thread, a LAN
//! host, the dedicated server) runs them. At the end of each tick it tells
//! them who joined and left, then what players said, broke and placed since
//! the last one, then ticks them; a player's command reaches them at once.
//! Their lines go to the players they name. With no server mod on, each hook
//! here is one check.
use super::{Game, lan::LanHost};
#[cfg(not(feature = "mods"))]
use horizon_world::registry::BlockStateId;
/// Who did something a server mod hears about, resolved at the tick's end.
#[cfg_attr(not(feature = "mods"), allow(dead_code))]
pub(super) enum Who {
/// The window's own player, in-frame.
Local,
/// A connection's player (the server thread's owner is one).
Conn(u32),
Uuid(u128),
}
/// What players did during a tick, for the server mods at its end.
#[cfg_attr(not(feature = "mods"), allow(dead_code))]
pub(super) enum ModEvent {
Chat(Who, String),
Block {
who: Who,
pos: [i32; 3],
state: String,
placed: bool,
},
}
#[cfg(not(feature = "mods"))]
impl Game<'_> {
pub(super) fn tick_server_mods(&mut self) {}
pub(super) fn mod_block_event(&mut self, _pos: [i32; 3], _state: BlockStateId, _placed: bool) {}
pub(super) fn mod_player_chat(&mut self, _who: Who, _text: &str) {}
pub(super) fn local_server_mod_command(&mut self, _line: &str) -> bool {
false
}
pub(super) fn guest_server_mod_command(
&mut self,
_host: &mut LanHost,
_conn: u32,
_line: &str,
) -> bool {
false
}
}
#[cfg(feature = "mods")]
mod run {
use super::{Game, LanHost, ModEvent, Who};
use horizon_client::{
command::args::GameMode as Mode,
lan::data::component_nbt,
text::{self, Text},
};
use horizon_mods::{BlockPos, GameMode, Output, ServerPlayer};
use horizon_protocol::packets::play::{self, Clientbound as C};
use horizon_world::registry::BlockStateId;
/// `UUID.toString()`.
fn uuid_string(u: u128) -> String {
let h = format!("{u:032x}");
format!(
"{}-{}-{}-{}-{}",
&h[..8],
&h[8..12],
&h[12..16],
&h[16..20],
&h[20..]
)
}
fn mode(id: i32) -> GameMode {
match id {
1 => GameMode::Creative,
2 => GameMode::Adventure,
3 => GameMode::Spectator,
_ => GameMode::Survival,
}
}
impl Game<'_> {
/// The players on this game's server: the window's own player
/// in-frame, then every connection's. `host` is the hub when it is
/// out of `self.lan`.
pub(in crate::game) fn server_players(&self, host: Option<&LanHost>) -> Vec<ServerPlayer> {
let mut out = Vec::new();
if self.options.execution_role.is_client()
&& self.remote.is_none()
&& let Some(p) = &self.player
{
out.push(ServerPlayer {
id: self.player_uuid_string(),
name: crate::game::chat::PLAYER_NAME.to_owned(),
x: p.position.x,
y: p.position.y,
z: p.position.z,
dimension: self.dim.id().to_owned(),
game_mode: match self.mode {
Mode::Survival => GameMode::Survival,
Mode::Creative => GameMode::Creative,
Mode::Adventure => GameMode::Adventure,
Mode::Spectator => GameMode::Spectator,
},
health: p.health.health(),
});
}
let guests = host
.or(self.lan.as_ref())
.into_iter()
.flat_map(|h| &h.hub.guests);
out.extend(guests.filter(|g| g.placed).map(|g| ServerPlayer {
id: uuid_string(g.uuid),
name: g.name.clone(),
x: g.position[0],
y: g.position[1],
z: g.position[2],
dimension: g.dimension.clone(),
game_mode: mode(g.game_mode),
health: g.health,
}));
out
}
/// Whether what players do now reaches server mods: one is running
/// and this game simulates the world.
fn hears_players(&self) -> bool {
self.mods.server && self.remote.is_none()
}
/// The tick's end: joins and leaves, the tick's events, the tick hook.
pub(in crate::game) fn tick_server_mods(&mut self) {
if !self.options.execution_role.is_client() {
// A server has no frames to check its dev mods in.
self.reload_dev_mods();
}
if !self.mods.server {
return;
}
let players = self.server_players(None);
let snapshot = self.mod_snapshot_with(players.clone());
self.mods.mods.set_snapshot(snapshot);
let before = std::mem::take(&mut self.mods.players);
for p in before
.iter()
.filter(|p| !players.iter().any(|q| q.id == p.id))
{
self.mods.mods.player_left(p);
}
for p in players
.iter()
.filter(|p| !before.iter().any(|q| q.id == p.id))
{
self.mods.mods.player_joined(p);
}
for event in std::mem::take(&mut self.mods.events) {
let who = |w: &Who| {
let id = match w {
Who::Local => self.player_uuid_string(),
Who::Uuid(u) => uuid_string(*u),
Who::Conn(c) => {
let guests = self.lan.iter().flat_map(|h| &h.hub.guests);
let g = guests.into_iter().find(|g| g.conn == *c);
g.map_or_else(String::new, |g| uuid_string(g.uuid))
}
};
players.iter().chain(&before).find(|p| p.id == id).cloned()
};
match event {
ModEvent::Chat(w, text) => {
if let Some(p) = who(&w) {
self.mods.mods.player_chat(&p, &text);
}
}
ModEvent::Block {
who: w,
pos,
state,
placed,
} => {
let Some(p) = who(&w) else { continue };
let pos = BlockPos {
x: pos[0],
y: pos[1],
z: pos[2],
};
if placed {
self.mods.mods.block_placed(&p, pos, &state);
} else {
self.mods.mods.block_broken(&p, pos, &state);
}
}
}
}
self.mods.mods.server_tick(self.ticks);
self.mods.players = players;
self.show_mod_output();
}
/// A player broke (or placed) `state` at `pos`: the player acting now.
pub(in crate::game) fn mod_block_event(
&mut self,
pos: [i32; 3],
state: BlockStateId,
placed: bool,
) {
if !self.hears_players() {
return;
}
let who = self.lan_acting.map_or(Who::Local, Who::Conn);
let state = self.state_string(state);
self.mods.events.push(ModEvent::Block {
who,
pos,
state,
placed,
});
}
/// A chat message a player sent.
pub(in crate::game) fn mod_player_chat(&mut self, who: Who, text: &str) {
if self.hears_players() {
self.mods.events.push(ModEvent::Chat(who, text.to_owned()));
}
}
/// `/line` typed by the window's own player in-frame: true when a
/// server mod owns it.
pub(in crate::game) fn local_server_mod_command(&mut self, line: &str) -> bool {
if !self.hears_players() {
return false;
}
let players = self.server_players(None);
let Some(me) = players.first().cloned() else {
return false;
};
let snapshot = self.mod_snapshot_with(players);
self.mods.mods.set_snapshot(snapshot);
let owned = self.mods.mods.server_command(&me, line);
self.show_mod_output();
owned
}
/// `/line` typed by a connection's player: true when a server mod
/// owns it, whatever the player's permission level.
pub(in crate::game) fn guest_server_mod_command(
&mut self,
host: &mut LanHost,
conn: u32,
line: &str,
) -> bool {
if !self.hears_players() {
return false;
}
let Some(g) = host.hub.guest(conn) else {
return false;
};
let id = uuid_string(g.uuid);
let players = self.server_players(Some(host));
let Some(me) = players.iter().find(|p| p.id == id).cloned() else {
return false;
};
let snapshot = self.mod_snapshot_with(players);
self.mods.mods.set_snapshot(snapshot);
if !self.mods.mods.server_command(&me, line) {
return false;
}
self.route_mod_output(Some(host));
true
}
/// Sends a server mod's line to the player with this id: the
/// window's own chat for the local player, a system message for a
/// connection's. `host` is the hub when it is out of `self.lan`.
pub(in crate::game) fn tell_server_player(
&mut self,
host: Option<&mut LanHost>,
id: &str,
line: Text,
) {
let local = self.options.execution_role.is_client()
&& self.remote.is_none()
&& self.player.is_some();
if local && self.player_uuid_string() == id {
self.add_mod_chat_line(line);
return;
}
let Some(host) = host.or(self.lan.as_mut()) else {
return;
};
let to = host
.hub
.guests
.iter()
.find(|g| g.placed && uuid_string(g.uuid) == id);
if let Some(conn) = to.map(|g| g.conn) {
host.hub.send_to(
conn,
&C::SystemChat(play::SystemChat {
content: component_nbt(&line.component()),
overlay: false,
}),
);
}
}
/// Routes a server mod's output: `Tell` and `Reply` to their player,
/// `Broadcast` to everyone and the log.
pub(in crate::game) fn route_server_output(
&mut self,
mut host: Option<&mut LanHost>,
output: Output,
) {
let named = |name: &str, text: String| {
let mut line = Text::literal(format!("[{name}] ")).with_color(text::GRAY);
line.push(Text::literal(text));
line
};
match output {
Output::Tell { name, player, text } => {
self.tell_server_player(host, &player, named(&name, text));
}
Output::Reply {
player,
text,
error,
} => {
let line = Text::literal(text);
let line = if error {
line.with_color(text::RED)
} else {
line
};
self.tell_server_player(host, &player, line);
}
Output::Broadcast { name, text } => {
let plain = format!("[{name}] {text}");
if self.options.dedicated.is_some() {
super::super::dedicated::log("INFO", &plain);
} else {
log::info!("{plain}");
}
for p in self.server_players(host.as_deref()) {
let line = named(&name, text.clone());
self.tell_server_player(host.as_deref_mut(), &p.id, line);
}
}
_ => {}
}
}
}
}
@@ -210,6 +210,7 @@ impl Game<'_> {
if bg != 0 {
batch.fill(
atlas,
// submitTextBackground(-1, -1, width, height): corners, not a size.
-1.0,
-1.0,
width + 1.0,
+81 -30
View File
@@ -67,6 +67,7 @@ mod end_sky;
mod flat;
pub mod lightning;
mod lod;
mod native_material;
mod native_pack;
mod native_post;
mod native_sky;
@@ -429,6 +430,8 @@ pub struct Renderer {
/// The mipmap levels sampled (the Mipmap Levels option), at most `mip_levels - 1`.
mipmaps: u32,
terrain_layout: wgpu::BindGroupLayout,
/// terrain.wgsl's `SPRITE_ARRAYS` and `SPRITE_SPLIT`, for a shader pack's terrain.
terrain_sprites: [f64; 2],
sprite_sampler: wgpu::Sampler,
lightmap_view: wgpu::TextureView,
lightmap_sampler: wgpu::Sampler,
@@ -460,7 +463,7 @@ pub struct Renderer {
lod_retiring: crate::fast_hash::FxHashSet<SectionKey>,
/// Finished tile meshes `upload_lods` has yet to upload.
lod_pending: Option<lod::Pending>,
detail_columns: HashMap<(i32, i32), u32>,
detail_columns: crate::fast_hash::FxHashMap<(i32, i32), u32>,
/// Bumped whenever a column becomes or stops being complete.
detail_revision: u64,
arena: arena::Arena,
@@ -494,7 +497,7 @@ pub struct Renderer {
/// The shadow camera's draws by page, in bands of distance from the light.
shadow_runs: page_runs::PageRuns,
/// Block-entity blocks per meshed section (world position, state id).
block_entities: HashMap<SectionKey, Vec<([i32; 3], u32)>>,
block_entities: crate::fast_hash::FxHashMap<SectionKey, Vec<([i32; 3], u32)>>,
/// Changes whenever `block_entities` does (unique across renderers).
block_entity_revision: u64,
/// VisGraph face connectivity per meshed section (cave culling).
@@ -2370,6 +2373,7 @@ impl Renderer {
mip_levels,
mipmaps: mip_levels - 1,
terrain_layout: bindings,
terrain_sprites: sprite_constants.map(|(_, v)| v),
sprite_sampler,
lightmap_view,
lightmap_sampler,
@@ -2391,7 +2395,7 @@ impl Renderer {
lod_cutout: Default::default(),
lod_retiring: Default::default(),
lod_pending: None,
detail_columns: HashMap::new(),
detail_columns: Default::default(),
detail_revision: 0,
arena,
sorted_indices,
@@ -2412,7 +2416,7 @@ impl Renderer {
solid_runs: page_runs::PageRuns::default(),
phase2_runs: page_runs::PageRuns::default(),
shadow_runs: page_runs::PageRuns::default(),
block_entities: HashMap::new(),
block_entities: Default::default(),
block_entity_revision: next_block_entity_revision(),
occlusion: occlusion::Occlusion::new(crate::chunk::MIN_Y, crate::chunk::HEIGHT / 16),
world_post,
@@ -2848,18 +2852,47 @@ impl Renderer {
self.detail_columns.insert(column, next);
}
}
pub fn remove(&mut self, key: SectionKey) {
self.set_detail(key, false);
if self.block_entities.remove(&key).is_some() {
/// Every section of these columns goes: one lookup per column where the
/// tables are by column, and the meshes' ranges freed in one batch. A
/// teleport removes a few thousand columns in one frame.
pub fn remove_columns(&mut self, columns: impl IntoIterator<Item = (i32, i32)>) {
let mut released = Vec::new();
let mut block_entities = false;
for (x, z) in columns {
if self.detail_columns.remove(&(x, z)) == Some(COMPLETE_COLUMN) {
self.detail_revision += 1;
}
self.occlusion.remove_column(x, z);
let retiring = !self.lod_cutout.is_empty() && self.lod_cutout.contains(&(x, z));
for sy in 0..crate::chunk::SECTIONS as i32 {
let key = (x, sy, z);
block_entities |= self.block_entities.remove(&key).is_some();
if retiring {
// Distant tiles still leave its column out: drawn until they don't.
self.lod_retiring.insert(key);
} else if let Some(section) = self.sections.remove(key) {
released.push(section);
}
}
}
if block_entities {
self.block_entity_revision = next_block_entity_revision();
}
self.occlusion.remove(key);
if !self.lod_cutout.is_empty() && self.lod_cutout.contains(&(key.0, key.2)) {
// Distant tiles still leave its column out: drawn until they don't.
self.lod_retiring.insert(key);
} else {
self.release_section(key);
if let Some(flat) = &mut self.flat {
for id in released.iter().filter_map(|s| s.flat_id) {
flat.remove(id);
}
}
self.sorted_indices
.release_all(released.iter().filter_map(|s| {
let (page, start) = s.sorted_indices?;
Some((page, start, u64::from(s.translucent) * 6))
}));
self.arena.release_all(
released
.iter()
.map(|s| (s.page, s.start, u64::from(s.solid + s.translucent) * 4)),
);
}
/// Horizon's resolution scale (world pixels per window pixel) and FXAA.
pub fn set_world_post(&mut self, scale: f32, antialias: bool) {
@@ -2937,9 +2970,9 @@ impl Renderer {
self.native = None;
self.pack_atlas = None;
}
/// Runs the sky and post stages of one of Horizon's own shader packs from now on (`None`:
/// no pack), with its options' values; replaces a `shaders/` pack. Returns the stages'
/// labels; on an error the pack that ran keeps running.
/// Runs the sky, material and post stages of one of Horizon's own shader packs from now
/// on (`None`: no pack), with its options' values; replaces a `shaders/` pack. Returns the
/// stages' labels; on an error the pack that ran keeps running.
pub fn set_native_shader_pack(
&mut self,
pack: Option<(horizon_shader_api::Report, Vec<(String, f64)>)>,
@@ -2948,14 +2981,14 @@ impl Renderer {
self.native = None;
return Ok(Vec::new());
};
let format = self.config.format.remove_srgb_suffix();
let (pack, labels) = native_pack::NativePack::new(
&self.device,
&self.terrain_layout,
report,
&constants,
format,
)?;
let terrain = native_material::TerrainShape {
globals: &self.terrain_layout,
flat: self.flat.as_ref().map(|f| &f.draw_layout),
sprites: self.terrain_sprites,
format: self.config.format.remove_srgb_suffix(),
};
let (pack, labels) =
native_pack::NativePack::new(&self.device, &terrain, report, &constants)?;
self.native = pack.draws().then_some(pack);
if self.native.is_some() {
self.pack_post = None;
@@ -4179,6 +4212,21 @@ impl Renderer {
pass.set_bind_group(0, &self.group, &[]);
sky.draw(&mut pass, &native.frame);
}
// One of Horizon's own packs with a material stage: terrain draws with its pipelines,
// which read the pack's frame at group 3.
let material = self
.native
.as_ref()
.filter(|_| !frame.title)
.and_then(|n| Some((n.material.as_ref()?, &n.frame.group)));
let solid_pipeline = material.map_or(&self.solid, |(m, _)| &m.solid);
let translucent_pipeline = material.map_or(&self.translucent, |(m, _)| &m.translucent);
let pack_flat = material.and_then(|(m, _)| m.flat.as_ref());
let pack_frame = |pass: &mut wgpu::RenderPass<'_>| {
if let Some((_, group)) = material {
pass.set_bind_group(horizon_shader_api::stages::GROUP, group, &[]);
}
};
{
let attachments = [Some(wgpu::RenderPassColorAttachment {
view: world_view,
@@ -4212,6 +4260,7 @@ impl Renderer {
..Default::default()
});
pass.set_bind_group(0, &self.group, &[]);
pack_frame(&mut pass);
if frame.title || (sky_visible && (frame.skybox || frame.end_sky)) {
if !frame.title && frame.skybox && !pack_draws_sky && !native_sky_pass {
match self
@@ -4260,7 +4309,7 @@ impl Renderer {
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
}
Some(flat) => {
pass.set_pipeline(&flat.solid);
pass.set_pipeline(pack_flat.map_or(&flat.solid, |p| &p.0));
let vertices = |p| self.arena.buffer(p);
match cull {
// As below: phase 1, the test, then phase 2's draws
@@ -4278,7 +4327,8 @@ impl Renderer {
pack_reads_world,
);
pass.set_bind_group(0, &self.group, &[]);
pass.set_pipeline(&flat.solid);
pack_frame(&mut pass);
pass.set_pipeline(pack_flat.map_or(&flat.solid, |p| &p.0));
flat.draw_phase2(&mut pass, vertices);
}
None => flat.draw(&mut pass, &flat.calls[..flat.solid_calls], vertices),
@@ -4287,7 +4337,7 @@ impl Renderer {
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
}
None => {
pass.set_pipeline(&self.solid);
pass.set_pipeline(solid_pipeline);
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
pass.set_vertex_buffer(1, self.instances.slice(..));
match cull {
@@ -4307,7 +4357,8 @@ impl Renderer {
pack_reads_world,
);
pass.set_bind_group(0, &self.group, &[]);
pass.set_pipeline(&self.solid);
pack_frame(&mut pass);
pass.set_pipeline(solid_pipeline);
pass.set_index_buffer(
self.indices.slice(..),
wgpu::IndexFormat::Uint32,
@@ -4399,7 +4450,7 @@ impl Renderer {
// The pack draws translucent terrain after its deferred programs.
_ if pack_draws_water => {}
Some(flat) => {
pass.set_pipeline(&flat.translucent);
pass.set_pipeline(pack_flat.map_or(&flat.translucent, |p| &p.1));
flat.draw(&mut pass, flat.translucent_calls(), |p| {
self.arena.buffer(p)
});
@@ -4407,7 +4458,7 @@ impl Renderer {
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
}
None => {
pass.set_pipeline(&self.translucent);
pass.set_pipeline(translucent_pipeline);
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
pass.set_vertex_buffer(1, self.instances.slice(..));
self.draw_sections(
+87
View File
@@ -39,6 +39,35 @@ impl FreeList {
}
self.0.insert(start, len);
}
/// `give` for many ranges: one merge of the sorted ranges into the list,
/// where giving them one by one costs a few tree lookups each.
fn give_all(&mut self, mut ranges: Vec<(u64, u64)>) {
if ranges.len() < 64 {
for (start, len) in ranges {
self.give(start, len);
}
return;
}
ranges.sort_unstable();
let mut free = std::mem::take(&mut self.0).into_iter().peekable();
let mut given = ranges.into_iter().peekable();
let mut merged: Vec<(u64, u64)> = Vec::new();
loop {
let next = match (free.peek(), given.peek()) {
(Some(a), Some(b)) if a.0 <= b.0 => free.next(),
(_, Some(_)) => given.next(),
(Some(_), None) => free.next(),
(None, None) => break,
};
let (start, len) = next.expect("peeked");
match merged.last_mut() {
Some(last) if last.0 + last.1 == start => last.1 += len,
_ => merged.push((start, len)),
}
}
// Sorted: the tree is built in one pass.
self.0 = merged.into_iter().collect();
}
}
/// One vertex buffer of the arena; only the newest page grows.
@@ -237,6 +266,26 @@ impl Arena {
.free
.give(start, len);
}
/// `release` for many ranges (page, start, vertices) at once.
pub fn release_all(&mut self, ranges: impl IntoIterator<Item = (u32, u64, u64)>) {
let mut by_page: Vec<Vec<(u64, u64)>> = Vec::new();
for (page, start, vertices) in ranges {
let page = page as usize;
if by_page.len() <= page {
by_page.resize_with(page + 1, Vec::new);
}
by_page[page].push((start, Self::span(vertices).min(self.page_limit)));
}
for (page, ranges) in by_page.into_iter().enumerate() {
if !ranges.is_empty() {
self.pages[page]
.as_mut()
.expect("live arena page")
.free
.give_all(ranges);
}
}
}
/// Moves a page into a larger buffer; false (the page unchanged) when
/// there is no memory for it.
fn grow(
@@ -435,6 +484,44 @@ mod tests {
assert_eq!(free, FreeList::new(4 * GRAIN));
}
#[test]
fn many_ranges_given_at_once_coalesce_as_one_by_one() {
let fill = || {
let mut free = FreeList::new(1 << 30);
let taken: Vec<(u64, u64)> = (0..500u64)
.map(|i| {
let len = GRAIN * (1 + i * 7 % 5);
(free.take(len).unwrap(), len)
})
.collect();
for &(start, len) in taken.iter().step_by(3) {
free.give(start, len);
}
(free, taken)
};
// Every other range of those still taken, in a scattered order.
let (mut one_by_one, taken) = fill();
let (mut at_once, _) = fill();
let mut given: Vec<(u64, u64)> = (0..taken.len())
.filter(|i| i % 3 != 0 && i % 2 == 0)
.map(|i| taken[i])
.collect();
given.sort_by_key(|&(start, _)| start.wrapping_mul(0x9E37_79B9_7F4A_7C15));
assert!(given.len() >= 64);
for &(start, len) in &given {
one_by_one.give(start, len);
}
at_once.give_all(given);
assert_eq!(at_once, one_by_one);
// The rest too: everything is free again, in one range.
let rest: Vec<(u64, u64)> = (0..taken.len())
.filter(|i| i % 3 != 0 && i % 2 != 0)
.map(|i| taken[i])
.collect();
at_once.give_all(rest);
assert_eq!(at_once, FreeList::new(1 << 30));
}
#[test]
fn first_fit_reuses_the_lowest_hole() {
let mut free = FreeList::new(8 * GRAIN);
@@ -0,0 +1,328 @@
//! The material stage of Horizon's own shader packs (docs/shaders.md): the pack's `material`
//! function gives the lit colour of each point of a block's surface, before the alpha test and
//! fog. Terrain still draws with Horizon's meshes, culling and draw lists; only its pipelines
//! change. Their shader is the pack's stage (`CheckedStage::source`), then a copy of
//! terrain.wgsl's terrain section whose names all take a `horizon_terrain_` prefix
//! (`wgsl_prefix.rs`), then entries that hand the pack what vanilla's `fs_solid` and
//! `fs_translucent` combine: the texture, the vertex tint, the light levels and the lightmap's
//! colour. They read the terrain's group 0, the flat draws' group 1 where those run, and the
//! pack's frame at group 3.
use horizon_shader_api::{check::CheckedStage, stages::GROUP};
use super::native_post::{NativeFrame, present_constants};
use super::wgsl_prefix::{TERRAIN, prefixed, section};
const PREFIX: &str = "horizon_terrain_";
/// The entries around the pack's `material`: vanilla's vertex work without the lightmap
/// product, then the material, the alpha test and fog as `fs_solid` and `fs_translucent` do.
const ENTRIES: &str = "
struct HorizonMaterialOut {
@builtin(position) clip: vec4<f32>,
@location(0) uv: vec2<f32>,
@location(1) @interpolate(flat) layer: u32,
@location(2) tint: vec4<f32>,
@location(3) light: vec2<f32>,
@location(4) lightmap: vec3<f32>,
@location(5) position: vec3<f32>,
@location(6) spherical: f32,
@location(7) cylindrical: f32,
}
fn horizon_material_vertex(position: vec3<f32>, uv: vec2<f32>, layer: u32, color: vec4<f32>,
light: vec2<u32>, origin: vec3<f32>) -> HorizonMaterialOut {
let pos = position + origin;
var out: HorizonMaterialOut;
out.clip = horizon_terrain_globals.view_proj * vec4(pos, 1.0);
out.uv = uv;
out.layer = layer;
out.tint = color;
let block = max(f32(light.y), horizon_terrain_dynamic_block_light(pos));
let sky = f32(light.x);
// 16 per level, 15 levels.
out.light = min(vec2(block, sky) / 240.0, vec2(1.0));
out.lightmap = horizon_terrain_sample_lightmap(sky / 16.0, block / 16.0).rgb;
out.position = pos;
out.spherical = length(pos);
out.cylindrical = max(length(pos.xz), abs(pos.y));
return out;
}
@vertex fn horizon_material_vs(
@location(0) position: vec3<f32>, @location(1) uv: vec2<u32>,
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>,
@location(5) origin: vec4<f32>) -> HorizonMaterialOut {
return horizon_material_vertex(position, horizon_terrain_terrain_uv(uv, origin.w), layer,
color, light, origin.xyz);
}
@vertex fn horizon_material_vs_flat(
@builtin(vertex_index) vertex: u32,
@location(0) position: vec3<f32>, @location(1) uv: vec2<u32>,
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>
) -> HorizonMaterialOut {
let origin = horizon_terrain_slot_origins[horizon_terrain_block_slots[vertex >> 8u]];
return horizon_material_vertex(position, horizon_terrain_terrain_uv(uv, origin.w), layer,
color, light, origin.xyz);
}
fn horizon_material_color(in: HorizonMaterialOut, translucent: bool) -> vec4<f32> {
let albedo = horizon_terrain_sprite(in.uv, in.layer & 0x7fffu);
return material(MaterialInput(albedo, in.tint, in.light, in.lightmap, in.position, in.uv,
translucent)).color;
}
@fragment fn horizon_material_fs_solid(in: HorizonMaterialOut) -> @location(0) vec4<f32> {
let color = horizon_material_color(in, false);
// Bit 15 of the layer marks opaque leaves (forceOpaque): no alpha cutout.
if color.a < 0.5 && (in.layer & 0x8000u) == 0u { discard; }
return horizon_terrain_apply_fog(vec4(color.rgb, 1.0), in.spherical, in.cylindrical);
}
@fragment fn horizon_material_fs_translucent(in: HorizonMaterialOut) -> @location(0) vec4<f32> {
let color = horizon_material_color(in, true);
if color.a < 0.01 { discard; }
return horizon_terrain_apply_fog(color, in.spherical, in.cylindrical);
}
";
/// What terrain pipelines are built against: the renderer's layouts and constants.
pub struct TerrainShape<'a> {
/// The terrain's group 0.
pub globals: &'a wgpu::BindGroupLayout,
/// The flat draws' group 1, where they run (`flat.rs`).
pub flat: Option<&'a wgpu::BindGroupLayout>,
/// terrain.wgsl's `SPRITE_ARRAYS` and `SPRITE_SPLIT`.
pub sprites: [f64; 2],
/// The world target's colour format.
pub format: wgpu::TextureFormat,
}
/// The pack's terrain pipelines: solid (with the cutout test) and translucent, for the
/// renderer's instanced draws and, where they run, its flat ones.
pub struct NativeMaterial {
pub solid: wgpu::RenderPipeline,
pub translucent: wgpu::RenderPipeline,
pub flat: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline)>,
}
/// terrain.wgsl's terrain section (its globals, bindings, sampling, lighting and fog), with
/// every name it declares prefixed.
fn vanilla_terrain() -> String {
prefixed(
section(TERRAIN, "struct Globals {", "// Sky (SkyRenderer)"),
PREFIX,
)
}
/// The terrain shader for the pack's material stage `source`.
fn shader_source(source: &str) -> String {
format!(
"{source}\n// Horizon's terrain (terrain.wgsl)\n{}{ENTRIES}",
vanilla_terrain()
)
}
/// The material's naga module, its errors in the stage's file.
fn module(stage: &CheckedStage) -> Result<naga::Module, String> {
let text = shader_source(&stage.source);
naga::front::wgsl::parse_str(&text)
.map_err(|e| format!("{}: {}", stage.file, e.emit_to_string(&text)))
}
impl NativeMaterial {
/// The pipelines of the pack's material `stage` with the option values `constants`.
pub fn new(
device: &wgpu::Device,
shape: &TerrainShape<'_>,
frame: &NativeFrame,
stage: &CheckedStage,
constants: &[(String, f64)],
) -> Result<Self, String> {
let module = module(stage)?;
let mut constants = present_constants(&module, constants);
let sprite_names = [
"horizon_terrain_SPRITE_ARRAYS",
"horizon_terrain_SPRITE_SPLIT",
];
constants.extend(sprite_names.into_iter().zip(shape.sprites));
let scope = device.push_error_scope(wgpu::ErrorFilter::Validation);
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some(&stage.label),
source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(module)),
});
let layout = |flat: Option<&wgpu::BindGroupLayout>| {
let mut groups: [Option<&wgpu::BindGroupLayout>; GROUP as usize + 1] =
Default::default();
groups[0] = Some(shape.globals);
groups[1] = flat;
groups[GROUP as usize] = Some(&frame.layout);
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Native shader pack terrain"),
bind_group_layouts: &groups,
..Default::default()
})
};
let instance_attributes = wgpu::vertex_attr_array![5=>Float32x4];
let vertices = wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<super::TerrainVertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &super::TERRAIN_ATTRIBUTES,
};
let instances = wgpu::VertexBufferLayout {
array_stride: 16,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &instance_attributes,
};
// As the renderer's terrain pipelines (gfx.rs): back faces culled, reversed depth.
let pipeline = |layout: &wgpu::PipelineLayout,
vertex: &str,
buffers: &[Option<wgpu::VertexBufferLayout<'_>>],
fragment: &str,
blend: Option<wgpu::BlendState>| {
let options = wgpu::PipelineCompilationOptions {
constants: &constants,
..Default::default()
};
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some(&stage.label),
layout: Some(layout),
vertex: wgpu::VertexState {
module: &module,
entry_point: Some(vertex),
compilation_options: options.clone(),
buffers,
},
primitive: wgpu::PrimitiveState {
cull_mode: Some(wgpu::Face::Back),
..Default::default()
},
depth_stencil: Some(wgpu::DepthStencilState {
format: wgpu::TextureFormat::Depth32Float,
depth_write_enabled: Some(true),
depth_compare: Some(wgpu::CompareFunction::GreaterEqual),
stencil: Default::default(),
bias: Default::default(),
}),
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
module: &module,
entry_point: Some(fragment),
compilation_options: options,
targets: &[Some(wgpu::ColorTargetState {
format: shape.format,
blend,
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview_mask: None,
cache: None,
})
};
let blend = Some(wgpu::BlendState::ALPHA_BLENDING);
let instanced = layout(None);
let buffers = [Some(vertices.clone()), Some(instances)];
let solid = pipeline(
&instanced,
"horizon_material_vs",
&buffers,
"horizon_material_fs_solid",
None,
);
let translucent = pipeline(
&instanced,
"horizon_material_vs",
&buffers,
"horizon_material_fs_translucent",
blend,
);
let flat = shape.flat.map(|group| {
let layout = layout(Some(group));
let buffers = [Some(vertices.clone())];
let vs = "horizon_material_vs_flat";
(
pipeline(&layout, vs, &buffers, "horizon_material_fs_solid", None),
pipeline(
&layout,
vs,
&buffers,
"horizon_material_fs_translucent",
blend,
),
)
});
if let Some(e) = pollster::block_on(scope.pop()) {
return Err(format!("{}: {e}", stage.file));
}
Ok(Self {
solid,
translucent,
flat,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use horizon_shader_api::{MANIFEST, Pack, StageKind, check};
fn stage(material: &str) -> CheckedStage {
let manifest = "schema = 1\nid = \"t\"\nname = \"T\"\nversion = \"1\"\napi = \"0.1\"\n\
[material]\nfile = \"material.wgsl\"\n\
[[options]]\nid = \"gain\"\nname = \"Gain\"\ntype = \"float\"\n\
default = 1.0\nmin = 0.0\nmax = 2.0\n";
let files = [(MANIFEST, manifest), ("material.wgsl", material)]
.map(|(n, t)| (n.to_owned(), t.as_bytes().to_vec()))
.into();
let mut report = check(&Pack::from_files(files).unwrap()).unwrap();
assert_eq!(report.stages[0].kind, StageKind::Material);
report.stages.remove(0)
}
#[test]
fn the_terrain_takes_a_prefix_and_validates_with_a_pack() {
let terrain = vanilla_terrain();
for name in [
"Globals",
"globals",
"sprites_7",
"sprite",
"SPRITE_SPLIT",
"block_slots",
"sample_lightmap",
"apply_fog",
] {
assert!(terrain.contains(&format!("{PREFIX}{name}")), "{name}");
}
assert!(!terrain.contains("fs_sky"), "the sky stays out");
// A pack may use any name Horizon's terrain uses unprefixed.
let own = "override gain: f32;\nconst PI: f32 = 3.0;\n\
fn sprite(c: vec4<f32>) -> vec4<f32> { return c * PI; }\n\
fn apply_fog(c: vec4<f32>) -> vec4<f32> { return c * gain; }\n\
fn material(input: MaterialInput) -> Surface {\n \
let lit = input.albedo * input.tint * vec4(input.lightmap, 1.0);\n \
return Surface(apply_fog(sprite(lit)));\n}\n";
let stage = stage(own);
let module = module(&stage).unwrap();
let info = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::all(),
)
.validate(&module)
.unwrap_or_else(|e| panic!("{}", e.emit_to_string(&shader_source(&stage.source))));
let entries: Vec<&str> = module
.entry_points
.iter()
.map(|e| e.name.as_str())
.collect();
for entry in [
"horizon_material_vs",
"horizon_material_vs_flat",
"horizon_material_fs_solid",
"horizon_material_fs_translucent",
] {
assert!(entries.contains(&entry), "{entry}");
}
drop(info);
assert_eq!(
present_constants(&module, &[("gain".into(), 1.5), ("other".into(), 1.0)]),
[("gain", 1.5)]
);
}
}
+19 -5
View File
@@ -1,8 +1,9 @@
//! One of Horizon's own shader packs while it runs (docs/shaders.md): the frame its stages
//! read, its sky and post stages, and the timer of their passes.
//! read, its sky, material and post stages, and the timer of their passes.
use horizon_shader_api::{Report, StageKind};
use super::native_material::{NativeMaterial, TerrainShape};
use super::native_post::{NativeFrame, NativePost};
use super::native_sky::NativeSky;
use super::pack_timing::PassTiming;
@@ -10,6 +11,7 @@ use super::pack_timing::PassTiming;
pub struct NativePack {
pub frame: NativeFrame,
pub sky: Option<NativeSky>,
pub material: Option<NativeMaterial>,
pub post: Option<NativePost>,
/// GPU time of each pass, while the renderer asks for it (`pack_timing.rs`).
pub timing: Option<PassTiming>,
@@ -17,16 +19,16 @@ pub struct NativePack {
impl NativePack {
/// The pack of the checked stages `report` with the option values `constants`, drawing
/// onto targets of `format`; `globals` is the terrain's group 0, which the sky reads.
/// onto the world's targets as `terrain` describes; the sky reads the terrain's group 0.
/// Also returns its stages' labels, in the order they draw. Errors name the stage wgpu
/// refused.
pub fn new(
device: &wgpu::Device,
globals: &wgpu::BindGroupLayout,
terrain: &TerrainShape<'_>,
report: Report,
constants: &[(String, f64)],
format: wgpu::TextureFormat,
) -> Result<(Self, Vec<String>), String> {
let (globals, format) = (terrain.globals, terrain.format);
let frame = NativeFrame::new(device);
let (post, others): (Vec<_>, Vec<_>) = report
.stages
@@ -37,16 +39,28 @@ impl NativePack {
.find(|s| s.kind == StageKind::Sky)
.map(|stage| NativeSky::new(device, globals, &frame, stage, constants, format))
.transpose()?;
let material = others
.iter()
.find(|s| s.kind == StageKind::Material)
.map(|stage| NativeMaterial::new(device, terrain, &frame, stage, constants))
.transpose()?;
let post = (!post.is_empty())
.then(|| NativePost::new(device, post, constants, format, &frame))
.transpose()?;
let mut labels: Vec<String> = sky.iter().map(|s| s.label.clone()).collect();
labels.extend(
others
.iter()
.filter(|s| material.is_some() && s.kind == StageKind::Material)
.map(|s| s.label.clone()),
);
if let Some(post) = &post {
labels.extend(post.labels().into_iter().map(str::to_owned));
}
let pack = Self {
frame,
sky,
material,
post,
timing: None,
};
@@ -55,6 +69,6 @@ impl NativePack {
/// Whether the pack draws anything the game runs today.
pub fn draws(&self) -> bool {
self.sky.is_some() || self.post.is_some()
self.sky.is_some() || self.material.is_some() || self.post.is_some()
}
}
+19 -8
View File
@@ -522,16 +522,27 @@ impl Occlusion {
}
pub fn set(&mut self, key: SectionKey, vis: u64) {
if self.columns.set(key, vis) {
self.edits.push(Edit::Set(key, vis));
self.dirty = true;
self.changed.push(key);
if self.running.is_some() {
self.changed_late.push(key);
}
self.changed(key, vis);
}
}
pub fn remove(&mut self, key: SectionKey) {
self.set(key, ALL_VISIBLE);
fn changed(&mut self, key: SectionKey, vis: u64) {
self.edits.push(Edit::Set(key, vis));
self.dirty = true;
self.changed.push(key);
if self.running.is_some() {
self.changed_late.push(key);
}
}
/// Every section of the column `x`, `z` goes (sees through every face).
pub fn remove_column(&mut self, x: i32, z: i32) {
let Some(column) = self.columns.map.remove(&(x, z)) else {
return;
};
for (y, &vis) in column.iter().enumerate() {
if vis != ALL_VISIBLE {
self.changed((x, y as i32, z), ALL_VISIBLE);
}
}
}
pub fn clear(&mut self) {
self.columns.map.clear();
@@ -399,7 +399,8 @@ impl FrameUniforms {
_ => Value::i(&[0]),
}
};
self.values = NAMES.iter().map(|n| value(n)).collect();
self.values.clear();
self.values.extend(NAMES.iter().map(|n| value(n)));
self.frame = (self.frame + 1) % 720_720;
}
+1 -1
View File
@@ -444,7 +444,7 @@ pub fn apply_preset(preset: Preset, s: &mut crate::settings::Settings) {
let g = &mut s.horizon;
g.preset = preset;
g.cave_culling = true;
g.animated_textures = preset != Preset::Performance;
g.animated_textures = true;
g.chunk_updates = 0;
g.upload_budget_kib = 0;
g.sky = true;
+4 -4
View File
@@ -1111,8 +1111,9 @@ impl SubtitleOverlay {
s.purge_old_instances(3000.0 * frame.multiplier, now);
}
if s.audible_from(position) && !s.played.is_empty() {
width = width.max(width_of(&Text::translate(&s.key, &[])));
audible.push(i);
let text_width = width_of(&Text::translate(&s.key, &[]));
width = width.max(text_width);
audible.push((i, text_width));
}
}
if audible.is_empty() {
@@ -1121,7 +1122,7 @@ impl SubtitleOverlay {
let w = |s: &str| font.width(s) as i32;
width += w("<") + w(" ") + w(">") + w(" ");
let mut row = 0;
for i in audible {
for (i, text_width) in audible {
let s = &self.subtitles[i];
let text_value = Text::translate(&s.key, &[]);
let Some(closest) = s.best_candidate(position) else {
@@ -1145,7 +1146,6 @@ impl SubtitleOverlay {
let half_width = width / 2;
let height = 9;
let half_height = height / 2;
let text_width = width_of(&text_value);
let brightness = s.brightness(closest, frame.multiplier, now);
out.push(GuiDraw::Push(vec![
PoseOp::Translate(
+3 -4
View File
@@ -56,10 +56,9 @@ impl<'a> TooltipDisplay<'a> {
else {
return true;
};
let component = horizon_world::nbt::JavaString::from(component);
!hidden
.iter()
.any(|tag| matches!(tag, Tag::String(id) if id == &component))
!hidden.iter().any(|tag| {
matches!(tag, Tag::String(id) if id.0.iter().copied().eq(component.encode_utf16()))
})
}
}
+11
View File
@@ -823,6 +823,17 @@ fn login_and_configure(
_ => {}
}
};
// A Horizon client hears that this server runs its mods' quiet commands
// (horizon_protocol::quiet); a vanilla one is told nothing.
if brand == "horizon" {
send(
c,
cfg::Clientbound::CustomPayload(cfg::CustomPayload {
channel: horizon_protocol::quiet::CHANNEL.into(),
data: Vec::new(),
}),
)?;
}
for packet in sync.registry_packets(known_pack) {
send(c, cfg::Clientbound::RegistryData(packet))?;
}
@@ -120,6 +120,21 @@ impl Hub {
);
}
/// `ServerPlayer.crit`: send the victim's animation to the players tracking
/// the attacker and the attacker themselves (`sendToTrackingPlayersAndSelf`).
pub fn critical_hit(&self, attacker: i32, target: i32) {
let body = C::Animate(play::Animate {
id: target,
action: 1,
})
.encode();
for g in self.guests.iter().filter(|g| {
self.here(g) && (g.entity_id == attacker || g.tracker.is_tracking(attacker))
}) {
g.sender.send(body.clone());
}
}
/// `ServerLevel.broadcastDamageEvent`: the clients around show the hurt
/// entity's flash and hear its hurt sound.
pub fn damage_event(
+5
View File
@@ -128,6 +128,11 @@ pub enum HostEvent {
line: String,
secure: bool,
},
/// A chat message a player sent (after it was broadcast), for server mods.
PlayerChat {
sender: u128,
text: String,
},
/// A server log warning (ServerGamePacketListenerImpl's LOGGER.warn).
Warn(String),
/// A server log error (LOGGER.error).
@@ -219,6 +219,10 @@ impl Hub {
};
let line = format!("<{name}> {}", message.content);
self.broadcast_chat(&message, &bound, line, events);
events.push(HostEvent::PlayerChat {
sender: uuid,
text: message.content,
});
}
/// handleChatSessionUpdate: a new profile key, validated against the
+19
View File
@@ -752,6 +752,25 @@ impl Lod {
saved_order: VecDeque::new(),
}
}
/// The solid ground at block column (x, z): its top face's height and
/// its block, from the finest tile sampled there. For aiming past the
/// resident chunks (`game.raycast`); water is passed, as the view's ray
/// passes it.
pub fn ground(&self, x: i32, z: i32) -> Option<(i32, BlockStateId)> {
(0..=MAX_LEVEL).find_map(|level| {
let size = 16 << level;
let key = TileKey {
x: x.div_euclid(size),
z: z.div_euclid(size),
level,
};
let columns = self.ready.get(&key).filter(|c| c.len() == CELLS * CELLS)?;
let cell = size / CELLS as i32;
let (cx, cz) = ((x - key.x * size) / cell, (z - key.z * size) / cell);
let surface = columns[cz as usize * CELLS + cx as usize].surface?;
Some((surface.height, surface.state))
})
}
pub fn stats(&self) -> (usize, usize) {
(self.active.len(), self.ready.len())
}
+4
View File
@@ -1024,6 +1024,10 @@ fn check_despawn(m: &mut Mob, w: &MobWorld) -> bool {
if m.species() == Species::Mannequin {
return true;
}
// Mob.checkDespawn discards disallowed types even when persistent or NoAI.
if w.difficulty.effective == 0.0 && !super::spawn_rules::allowed_in_peaceful(m.species()) {
return false;
}
let Some(p) = w.nearest_player else {
return true;
};
+17 -12
View File
@@ -102,7 +102,7 @@ impl AnimState {
pub fn end_tick(&mut self, _swinging: bool) {}
}
/// Part poses for one draw, keyed by part name.
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct Poses {
map: HashMap<String, [f32; 9]>,
dummy: [f32; 9],
@@ -113,20 +113,24 @@ pub struct Poses {
}
impl Poses {
pub fn new(model: &Part) -> Self {
let mut map = HashMap::new();
let mut poses = Self::default();
poses.reset(model);
poses
}
/// Refills this for another model, keeping the map and the sets' capacity:
/// a mob draws its body and every equipment layer each frame.
pub fn reset(&mut self, model: &Part) {
fn walk(p: &Part, map: &mut HashMap<String, [f32; 9]>) {
map.insert(p.name.clone(), p.pose);
for c in &p.children {
walk(c, map);
}
}
walk(model, &mut map);
Self {
map,
dummy: [0.0; 9],
skipped: Default::default(),
hidden: Default::default(),
}
self.map.clear();
self.dummy = [0.0; 9];
self.skipped.clear();
self.hidden.clear();
walk(model, &mut self.map);
}
pub fn get(&mut self, name: &str) -> &mut [f32; 9] {
match self.map.get_mut(name) {
@@ -189,14 +193,15 @@ pub fn state(m: &Mob, alpha: f32) -> State {
swing += 1.0;
}
let swing = m.anim.attack_anim_o + swing * alpha;
let alive = m.alive();
// LivingEntityRenderer.extractRenderState: riding motion is not walking.
let walking = m.alive() && !m.extra.passenger;
State {
pos: if alive {
pos: if walking {
m.limb_position - m.limb_speed * (1.0 - alpha)
} else {
0.0
},
speed: if alive {
speed: if walking {
(m.previous_limb_speed + (m.limb_speed - m.previous_limb_speed) * alpha).min(1.0)
} else {
0.0
+4
View File
@@ -132,6 +132,10 @@ pub fn tick(m: &mut Mob) {
} else {
m.velocity.y *= 0.6;
}
setup_animation_states(m);
}
/// `Bat.setupAnimationStates`: client visuals, without changing server motion.
pub fn setup_animation_states(m: &mut Mob) {
let age = m.age as f32;
if resting(m) {
m.anim.states[0].stop();
+3 -2
View File
@@ -138,9 +138,10 @@ fn teleport_towards(
let z = m.position.z + (m.random.next_double() - 0.5) * 8.0 - d[2] * 16.0;
teleport_to(m, w, x, y, z, events)
}
/// Enderman.customServerAiStep: in daylight, flee by teleporting.
/// Enderman.customServerAiStep: in daylight, flee by teleporting. The End has a
/// fixed time, so `Level.isBrightOutside` is never true there.
pub fn custom_server_ai_step(m: &mut Mob, cx: &mut Cx) {
if cx.w.daylight && m.age as i32 >= m.extra.counter + 600 {
if cx.w.daylight && !cx.w.end && m.age as i32 >= m.extra.counter + 600 {
let br = super::hostile::mob_brightness(m, cx.w);
let (x, y, z) = (
m.position.x.floor() as i32,
+35 -19
View File
@@ -951,21 +951,25 @@ fn draw_normals(
return;
}
let normals = normals.part(p);
for poly in part.polygons.iter().filter(|_| !poses.skips(part)) {
let normal = poly.vertex_normal(normals);
for v in poly.vertices {
let pos = m.transform_point3(glam::Vec3::new(v[0], v[1], v[2]) / 16.0);
out.push(Vertex {
normal,
pos: pos.to_array(),
uv: [
uv[0] + (uv[2] - uv[0]) * v[3],
uv[1] + (uv[3] - uv[1]) * v[4],
],
layer: overlay,
color,
light,
});
// `skips` is a name lookup: ask once per part, not once per polygon. A part
// hidden this way still draws its children, so only the polygons are gated.
if !poses.skips(part) {
for poly in &part.polygons {
let normal = poly.vertex_normal(normals);
for v in poly.vertices {
let pos = m.transform_point3(glam::Vec3::new(v[0], v[1], v[2]) / 16.0);
out.push(Vertex {
normal,
pos: pos.to_array(),
uv: [
uv[0] + (uv[2] - uv[0]) * v[3],
uv[1] + (uv[3] - uv[1]) * v[4],
],
layer: overlay,
color,
light,
});
}
}
}
for c in &part.children {
@@ -1094,6 +1098,9 @@ fn render_visible(
}
}
}
// One Poses refilled for every layer (armour draws up to seven): it keeps
// its maps' capacity.
let mut layer_poses = Poses::default();
for layer in all_layers {
let Some(part) = m.kind.layer(&layer.model).cloned() else {
continue;
@@ -1101,8 +1108,8 @@ fn render_visible(
let Some(uv) = atlas(&layer.texture) else {
continue;
};
let mut poses = Poses::new(&part);
anim::setup(m, &s, &mut poses, &data.animations);
layer_poses.reset(&part);
anim::setup(m, &s, &mut layer_poses, &data.animations);
if matches!(m.species(), Species::Villager | Species::ZombieVillager)
&& let Some((folder, variant)) = layer
.texture
@@ -1113,7 +1120,7 @@ fn render_visible(
data.villager_hats
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.apply(folder, variant, profession, &mut poses);
.apply(folder, variant, profession, &mut layer_poses);
}
let light = if layer.full_bright {
240 | 240 << 8
@@ -1128,7 +1135,16 @@ fn render_visible(
None => overlay,
};
let start = out.len();
draw(&part, base, &poses, uv, layer.color, light, overlay, out);
draw(
&part,
base,
&layer_poses,
uv,
layer.color,
light,
overlay,
out,
);
if layer.full_bright && m.species() != Species::Warden {
for vertex in &mut out[start..] {
vertex.normal = [0; 4];
+67 -1
View File
@@ -284,12 +284,14 @@ pub fn xp(m: &Mob) -> i32 {
m.extra.trade.as_ref().map_or(0, |t| t.xp)
}
/// `Villager.setVillagerData(withProfession)`: a new profession forgets the
/// offers.
/// offers (`offers = null`), so the next `getOffers` rolls the new
/// profession's trades.
pub fn set_profession(m: &mut Mob, profession: u8) {
if m.extra.variant2 != profession {
m.extra.variant2 = profession;
if let Some(t) = m.extra.trade.as_mut() {
t.offers.clear();
t.unset = true;
}
}
}
@@ -846,6 +848,70 @@ pub fn read_nbt(m: &mut Mob, tag: &horizon_world::nbt::Tag) {
mod tests {
use super::*;
fn villager() -> Mob {
use crate::mobs::{Anim, Brain, Flags, MobType, Part, Species};
use std::{collections::HashMap, sync::Arc};
let kind = Arc::new(MobType {
id: "minecraft:villager".into(),
species: Species::Villager,
width: 0.6,
height: 1.95,
eye_height: 1.62,
category: "misc".into(),
max_health: 20.0,
speed: 0.5,
flying_speed: 0.02,
attack_damage: 0.0,
attack_knockback: 0.0,
follow_range: 48.0,
step_height: 0.6,
armor: 0.0,
armor_toughness: 0.0,
knockback_resistance: 0.0,
jump_strength: 0.42,
attributes: Arc::default(),
gravity: 0.08,
safe_fall_distance: 3.0,
fall_damage_multiplier: 1.0,
water_efficiency: 0.0,
water_slowdown: 0.8,
air_speed: 0.02,
baby_scale: 0.5,
head_rot_speed: 10.0,
max_head_x_rot: 40.0,
max_head_y_rot: 75.0,
ambient_interval: 80,
flags: Flags::default(),
model: Arc::new(Part::parse("root", &serde_json::json!({}))),
layers: HashMap::new(),
texture: String::new(),
brain: Brain::Passive,
anim: Anim::Villager,
sounds: [""; 3],
step_sound: None,
food: Arc::default(),
tempt: Arc::default(),
});
Mob::new(1, kind, horizon_core::geometry::Vec3::ZERO, 0.0)
}
#[test]
fn a_new_profession_rolls_new_offers() {
let mut m = villager();
set_profession(&mut m, 1);
// Offers made for the first profession.
m.extra.trade.get_or_insert_default().unset = false;
// The same profession keeps them.
set_profession(&mut m, 1);
assert!(!m.extra.trade.as_ref().unwrap().unset);
// A new one forgets them (offers = null), so the next getOffers
// (ensure_offers) rolls the new profession's trades.
set_profession(&mut m, 2);
let t = m.extra.trade.as_ref().unwrap();
assert!(t.offers.is_empty());
assert!(t.unset);
assert_eq!(m.extra.variant2, 2);
}
fn stack(item: &str, count: u32) -> ItemStack {
ItemStack::new(item, count)
}
+14 -14
View File
@@ -491,7 +491,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 4.0 + 96.0 + 18.0,
200.0,
"Done",
&crate::lang::get_or("gui.done", "Done"),
servers::parse_address(address).is_some(),
50,
),
@@ -499,7 +499,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 4.0 + 120.0 + 18.0,
200.0,
"Cancel",
&crate::lang::get_or("gui.cancel", "Cancel"),
true,
51,
),
@@ -532,7 +532,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 4.0 + 96.0 + 12.0,
200.0,
"Join Server",
&crate::lang::get_or("selectServer.select", "Join Server"),
servers::parse_address(address).is_some(),
52,
),
@@ -540,7 +540,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 4.0 + 120.0 + 12.0,
200.0,
"Cancel",
&crate::lang::get_or("gui.cancel", "Cancel"),
true,
53,
),
@@ -558,7 +558,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 4.0 + 120.0 + 18.0,
200.0,
"Cancel",
&crate::lang::get_or("gui.cancel", "Cancel"),
true,
57,
),
@@ -568,7 +568,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 2.0 + 30.0,
200.0,
"Back to Server List",
&crate::lang::get_or("gui.toMenu", "Back to Server List"),
true,
54,
)];
@@ -876,10 +876,10 @@ impl Screen {
} => {
centered(
batch,
if index.is_some() {
"Edit Server Info"
&if index.is_some() {
crate::lang::get_or("manageServer.edit.title", "Edit Server Info")
} else {
"Add Server"
crate::lang::get_or("manageServer.add.title", "Add Server")
},
17.0,
u32::MAX,
@@ -888,7 +888,7 @@ impl Screen {
Self::field(
atlas,
batch,
"Server Name",
&crate::lang::get_or("manageServer.enterName", "Server Name"),
name,
(53.0, 66.0),
*focus == 0,
@@ -897,7 +897,7 @@ impl Screen {
Self::field(
atlas,
batch,
"Server Address",
&crate::lang::get_or("manageServer.enterIp", "Server Address"),
address,
(94.0, 106.0),
*focus == 1,
@@ -916,7 +916,7 @@ impl Screen {
Self::field(
atlas,
batch,
&crate::lang::get_or("addServer.enterIp", "Server Address"),
&crate::lang::get_or("manageServer.enterIp", "Server Address"),
address,
(100.0, 116.0),
true,
@@ -973,7 +973,7 @@ impl Screen {
Self::Disconnected { reason, retry } => {
centered(
batch,
"Connection Lost",
&crate::lang::get_or("disconnect.lost", "Connection Lost"),
h / 2.0 - 40.0,
rgba(170, 170, 170, 255),
);
@@ -1019,7 +1019,7 @@ impl Screen {
(Self::List { .. }, Some(42)) => {
*self = Self::Edit {
index: None,
name: "Minecraft Server".into(),
name: crate::lang::get_or("selectServer.defaultName", "Minecraft Server"),
address: String::new(),
focus: 1,
packs: None,
+28 -2
View File
@@ -604,8 +604,10 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain
Tag::Int(n) => *n,
_ => continue,
};
f.recipes_used
.insert(id.to_string_lossy(), count.max(0) as u32);
// Saves from before the key was namespaced hold both forms of an id.
*f.recipes_used
.entry(crate::containers::recipe_used_key(&id.to_string_lossy()))
.or_insert(0) += count.max(0) as u32;
}
}
}
@@ -4313,4 +4315,28 @@ mod tests {
assert_eq!(read.game_rule("advance_time"), 0);
let _ = std::fs::remove_dir_all(dir);
}
/// A save written before the key was namespaced holds an id twice, once bare: vanilla
/// refuses such a furnace, so a load merges them under the full id.
#[test]
fn a_furnaces_recipes_used_merges_bare_and_full_ids() {
use crate::containers::BlockEntity;
let Some(BlockEntity::Furnace(mut f)) = BlockEntity::for_block("minecraft:furnace") else {
panic!("a furnace");
};
f.recipes_used
.insert("iron_ingot_from_smelting_iron_ore".to_owned(), 2);
f.recipes_used
.insert("minecraft:iron_ingot_from_smelting_iron_ore".to_owned(), 3);
let tag = encode_block_entity((1, 64, 2), &BlockEntity::Furnace(f), "minecraft:furnace");
let Some((_, BlockEntity::Furnace(back))) = decode_block_entity(&tag) else {
panic!("a furnace back");
};
assert_eq!(back.recipes_used.len(), 1);
assert_eq!(
back.recipes_used
.get("minecraft:iron_ingot_from_smelting_iron_ore"),
Some(&5)
);
}
}
+73
View File
@@ -134,3 +134,76 @@ impl SprintState {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn standing() -> Conditions {
Conditions {
enough_food: true,
on_ground: true,
..Conditions::default()
}
}
fn step(state: &mut SprintState, keys: Keys, sprinting: &mut bool) {
let current = *sprinting;
let mut next = current;
state
.tick(
keys,
Conditions {
sprinting: current,
..standing()
},
7,
|value| {
next = value;
Ok::<(), ()>(())
},
)
.unwrap();
*sprinting = next;
}
/// Vanilla sprint is a state. Releasing the sprint key, including on the
/// jump tick, keeps it while forward is held; releasing forward stops it.
#[test]
fn sprint_survives_releasing_the_key_through_a_jump() {
let mut state = SprintState::default();
let mut sprinting = false;
step(
&mut state,
Keys {
forward: true,
sprint: true,
..Keys::default()
},
&mut sprinting,
);
assert!(sprinting);
step(
&mut state,
Keys {
forward: true,
jump: true,
..Keys::default()
},
&mut sprinting,
);
assert!(
sprinting,
"a jump after the sprint key comes up must stay a sprint"
);
step(
&mut state,
Keys {
jump: true,
..Keys::default()
},
&mut sprinting,
);
assert!(!sprinting);
}
}
+3 -2
View File
@@ -14,7 +14,8 @@ use std::{fs, path::Path, sync::Arc};
/// Everything the workstation menus read, loaded once with the resources.
pub struct StationData {
pub enchantments: Enchantments,
/// The same registry `Items` carries (`Items::enchantments`).
pub enchantments: std::sync::Arc<Enchantments>,
pub recipes: StationRecipes,
pub brewing: crate::brewing::Brewing,
pub trades: crate::merchant::Trades,
@@ -25,7 +26,7 @@ pub struct StationData {
impl StationData {
pub fn load(root: &Path, items: &Items) -> Self {
Self {
enchantments: Enchantments::load(root),
enchantments: std::sync::Arc::clone(&items.enchantments),
recipes: StationRecipes::load(root, items),
brewing: crate::brewing::Brewing::load(root, items),
trades: crate::merchant::Trades::load(root),
+9 -3
View File
@@ -192,7 +192,7 @@ pub fn list_worlds(saves: &Path) -> Vec<WorldEntry> {
commands: matches!(data.get("allowCommands"), Some(horizon_world::nbt::Tag::Byte(v)) if *v != 0),
version: data.get("Version").and_then(|v| v.get("Name"))
.and_then(|v| match v { horizon_world::nbt::Tag::String(s) => Some(s.to_string_lossy()), _ => None })
.unwrap_or_else(|| "Unknown".into()),
.unwrap_or_else(|| lang::get_or("selectWorld.versionUnknown", "Unknown")),
})
})
.collect();
@@ -272,7 +272,7 @@ impl Title {
}
pub(crate) fn new_world() -> Mode {
Mode::Create {
name: "New World".into(),
name: lang::get_or("selectWorld.newWorld", "New World"),
seed: String::new(),
creative: false,
focus: 0,
@@ -867,7 +867,13 @@ impl Title {
}
Mode::Select { worlds, selected } => {
crate::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false);
centered(atlas, batch, "Select World", 8.0, u32::MAX);
centered(
atlas,
batch,
&lang::get_or("selectWorld.title", "Select World"),
8.0,
u32::MAX,
);
self.caret.borrow_mut().draw(
batch,
atlas,
+2
View File
@@ -339,7 +339,9 @@ pub fn shared_data(
if let Some((_, data)) = cache.iter().find(|(path, _)| path == dir) {
return Ok(data.clone());
}
let started = std::time::Instant::now();
let data = GeneratorData::load(dir, registry)?;
log::info!("Horizon: worldgen data loaded in {:?}", started.elapsed());
cache.push((dir.to_owned(), data.clone()));
Ok(data)
}
+170 -1
View File
@@ -251,6 +251,10 @@ pub struct Items {
loot: HashMap<String, Value>,
/// The `jukebox_song` registry.
pub songs: HashMap<String, Song>,
/// The enchantment registry: loot functions enchant the items they roll
/// (`EnchantWithLevelsFunction`, `EnchantRandomlyFunction`), and the
/// station menus share this copy.
pub enchantments: Arc<crate::enchant::Enchantments>,
}
/// A `JukeboxSong`.
#[derive(Clone, Debug)]
@@ -701,12 +705,15 @@ impl Items {
item_tag_order: load_tag_lists(root, "item")?,
loot,
songs: load_songs(root),
enchantments: Arc::new(crate::enchant::Enchantments::load(root)),
})
}
pub fn get(&self, id: &str) -> Option<&ItemInfo> {
self.items.get(id)
}
/// Only the block and item tags, for tools that need tag membership.
/// Only the block and item tags (and the enchantment registry, which the
/// station rules and loot functions are built from), for tools that need
/// tag membership.
pub fn tags_only(root: &Path) -> Result<Self> {
Ok(Self {
items: HashMap::new(),
@@ -717,6 +724,7 @@ impl Items {
item_tag_order: load_tag_lists(root, "item")?,
loot: HashMap::new(),
songs: HashMap::new(),
enchantments: Arc::new(crate::enchant::Enchantments::load(root)),
})
}
/// An item table with only tags, for unit tests of item rules.
@@ -734,6 +742,7 @@ impl Items {
item_tags,
loot: HashMap::new(),
songs: HashMap::new(),
enchantments: Arc::new(crate::enchant::Enchantments::default()),
}
}
/// Whether `block` is in the block tag `tag` (`minecraft:fire`, no `#`).
@@ -1371,6 +1380,16 @@ impl LootContext<'_> {
self.modifiers(&modifiers, &mut stack, random);
produced.push(stack);
}
/// An enchantment function's `options` (`HolderSet`): a tag, a list or
/// one id; every enchantment when absent (`registryAccess().listElements`).
fn enchant_pool(e: &crate::enchant::Enchantments, options: &Value) -> Vec<Arc<str>> {
match options {
Value::String(s) if s.starts_with('#') => e.tag(s).to_vec(),
Value::String(s) => vec![Arc::from(s.as_str())],
Value::Array(a) => a.iter().filter_map(|x| x.as_str().map(Arc::from)).collect(),
_ => e.list.iter().map(|x| Arc::clone(&x.id)).collect(),
}
}
fn modifiers<R: RandomSource>(&self, m: &Value, stack: &mut ItemStack, random: &mut R) {
let list: Vec<&Value> = match m {
Value::Array(a) => a.iter().collect(),
@@ -1449,6 +1468,54 @@ impl LootContext<'_> {
.with_potion(id);
}
}
// EnchantWithLevelsFunction: EnchantmentHelper.enchantItem on
// the context's random (the End cities' gear).
"minecraft:enchant_with_levels" => {
let cost = Self::int_number(&f["levels"], random);
let e = &self.items.enchantments;
let pool = Self::enchant_pool(e, &f["options"]);
let chosen = e.select(self.items, random, &stack.item, cost, &pool);
// EnchantmentHelper.enchantItem: a book becomes one
// enchanted book (the fresh stack's count).
if &*stack.item == "minecraft:book" {
*stack = ItemStack::new("minecraft:enchanted_book", 1);
}
for (id, level) in chosen {
stack.enchant(&id, level);
}
}
// EnchantRandomlyFunction: one enchantment the item accepts,
// at a level between its min and max.
"minecraft:enchant_randomly" => {
let e = &self.items.enchantments;
let book = &*stack.item == "minecraft:book";
let only_compatible = f["only_compatible"].as_bool().unwrap_or(true);
let options: Vec<Arc<str>> = Self::enchant_pool(e, &f["options"])
.into_iter()
.filter(|id| {
book || !only_compatible || e.can_enchant(self.items, id, &stack.item)
})
.collect();
if options.is_empty() {
continue;
}
let id = options
[random.next_int_bound(options.len() as i32).unwrap_or(0) as usize]
.clone();
let max = e.get(&id).map_or(1, |d| d.max_level).max(1);
// `Mth.nextInt(random, 1, max)`: a single-level
// enchantment (Mending) returns 1 without drawing, so the
// rolls after this one keep vanilla's sequence.
let level = if max <= 1 {
1
} else {
1 + random.next_int_bound(max as i32).unwrap_or(0) as u32
};
if book {
*stack = ItemStack::new("minecraft:enchanted_book", 1);
}
stack.enchant(&id, level);
}
"minecraft:limit_count" => {
let limit = &f["limit"];
if let Some(min) = limit["min"].as_i64() {
@@ -1574,6 +1641,22 @@ mod tests {
}
}
/// The items of the prepared resources, when they are on this machine
/// (the block registry and facts as the client's `Assets::load` reads them).
fn prepared_items() -> Option<Items> {
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../.local/horizon-client/26.3");
if !root.join("items.json").exists() {
return None;
}
let (registry, _) = BlockRegistry::from_facts_json_with_flags(
std::fs::File::open(root.join("facts.json")).ok()?,
)
.ok()?;
let facts = BlockFacts::load(&root, &registry).ok()?;
Items::load(&root, &registry, &facts).ok()
}
#[test]
fn fortune_silk_touch_and_looting() {
let items = Items::for_tests(HashMap::new());
@@ -1657,6 +1740,92 @@ mod tests {
assert_eq!(&*drops[0].item, "minecraft:splash_potion");
assert_eq!(drops[0].potion(), Some("minecraft:fire_resistance"));
}
/// `/loot give` and every other table this dispatcher rolls: the two
/// enchantment functions (`EnchantWithLevelsFunction`,
/// `EnchantRandomlyFunction`) put their enchantments on the stack.
#[test]
fn loot_enchant_functions_enchant_the_rolled_item() {
let Some(items) = prepared_items() else {
return;
};
let props = BTreeMap::new();
let mut random = LegacyRandom::new(11);
// A sword at levels 20..39 comes out enchanted, as in an End city.
let levels = json!({"pools":[{"rolls":1,"entries":[{"type":"minecraft:item",
"name":"minecraft:diamond_sword","modifier":[
{"type":"minecraft:enchant_with_levels","levels":{"type":"minecraft:uniform","min":20.0,"max":39.0}}]}]}]});
let mut enchanted = 0;
for _ in 0..40 {
let drops = ctx(&items, None, &props, "").evaluate(&levels, &mut random);
if drops[0].enchantments().is_empty() {
continue;
}
enchanted += 1;
assert_eq!(&*drops[0].item, "minecraft:diamond_sword");
// Levels stay within the enchantment's range.
for (id, level) in drops[0].enchantments() {
let max = items
.enchantments
.get(id)
.expect("a known enchantment")
.max_level;
assert!((1..=max).contains(level), "{id} at {level}");
}
}
assert!(
enchanted > 30,
"only {enchanted} of 40 rolls were enchanted"
);
// `enchant_randomly` on a book makes one enchanted book, its levels
// stored; the pool is the enchantment tag it names.
let book = json!({"pools":[{"rolls":1,"entries":[{"type":"minecraft:item",
"name":"minecraft:book","modifier":[
{"type":"minecraft:enchant_randomly","options":"#minecraft:on_random_loot"}]}]}]});
let drops = ctx(&items, None, &props, "").evaluate(&book, &mut random);
assert_eq!(&*drops[0].item, "minecraft:enchanted_book");
assert_eq!(drops[0].count, 1);
assert!(drops[0].enchantments().is_empty());
let stored = drops[0].stored_enchantments();
assert_eq!(stored.len(), 1, "{stored:?}");
assert!(
items
.enchantments
.in_tag("minecraft:on_random_loot", &stored[0].0)
);
// An item no enchantment accepts keeps none of them.
let dirt = json!({"pools":[{"rolls":1,"entries":[{"type":"minecraft:item",
"name":"minecraft:dirt","modifier":[
{"type":"minecraft:enchant_with_levels","levels":{"type":"minecraft:constant","value":30.0}}]}]}]});
let drops = ctx(&items, None, &props, "").evaluate(&dirt, &mut random);
assert!(drops[0].enchantments().is_empty());
}
/// `Mth.nextInt(random, 1, max)` returns `1` without drawing when the
/// enchantment has one level (`EnchantRandomlyFunction.enchantItem`), so
/// the rolls after it must not shift. Vanilla with `LegacyRandom(11)`: a
/// Mending book, then `set_count` uniform 1..64, is 46.
#[test]
fn a_single_level_enchantment_draws_no_level() {
let Some(items) = prepared_items() else {
return;
};
let props = BTreeMap::new();
let table = json!({"pools":[{"rolls":1,"entries":[{"type":"minecraft:item",
"name":"minecraft:book","modifier":[
{"type":"minecraft:enchant_randomly","options":"minecraft:mending"},
{"type":"minecraft:set_count","count":{"type":"minecraft:uniform","min":1.0,"max":64.0}}]}]}]});
let drops = ctx(&items, None, &props, "").evaluate(&table, &mut LegacyRandom::new(11));
assert_eq!(drops.len(), 1);
assert_eq!(&*drops[0].item, "minecraft:enchanted_book");
assert_eq!(
drops[0].stored_enchantments(),
[("minecraft:mending".to_owned().into(), 1)],
"the book keeps its single-level enchantment"
);
// 46 is vanilla's count for this seed; an extra level draw gives 28.
assert_eq!(drops[0].count, 46);
}
}
/// Minecraft.handleKeybinds: spectators cannot drop from the selected slot.
+79 -5
View File
@@ -294,6 +294,18 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "fallible-iterator"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
[[package]]
name = "fallible-streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]]
name = "find-msvc-tools"
version = "0.1.15"
@@ -310,6 +322,12 @@ dependencies = [
"miniz_oxide",
]
[[package]]
name = "foldhash"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
[[package]]
name = "foldhash"
version = "0.2.0"
@@ -394,17 +412,35 @@ dependencies = [
"zerocopy",
]
[[package]]
name = "hashbrown"
version = "0.15.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
dependencies = [
"foldhash 0.1.5",
]
[[package]]
name = "hashbrown"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
dependencies = [
"foldhash",
"foldhash 0.2.0",
"serde",
"serde_core",
]
[[package]]
name = "hashlink"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1"
dependencies = [
"hashbrown 0.15.5",
]
[[package]]
name = "hmac"
version = "0.12.1"
@@ -424,6 +460,7 @@ dependencies = [
"hmac",
"horizon-mods",
"http-body-util",
"rusqlite",
"serde",
"serde_json",
"sha2",
@@ -651,7 +688,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855"
dependencies = [
"equivalent",
"hashbrown",
"hashbrown 0.17.1",
"serde",
"serde_core",
]
@@ -674,6 +711,17 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
[[package]]
name = "libsqlite3-sys"
version = "0.35.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "133c182a6a2c87864fe97778797e46c7e999672690dc9fa3ee8e241aa4a9c13f"
dependencies = [
"cc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "litemap"
version = "0.8.3"
@@ -767,7 +815,7 @@ dependencies = [
"cfg_aliases",
"codespan-reporting",
"half",
"hashbrown",
"hashbrown 0.17.1",
"indexmap",
"libm",
"log",
@@ -786,7 +834,7 @@ version = "30.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "590afbf58a6f4f62873cd5cff4468061844bafa1cdf399cc954537c22d768d49"
dependencies = [
"hashbrown",
"hashbrown 0.17.1",
"indexmap",
"rustc-hash",
"thiserror",
@@ -820,6 +868,12 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
[[package]]
name = "pkg-config"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548"
[[package]]
name = "potential_utf"
version = "0.1.6"
@@ -867,6 +921,20 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "rusqlite"
version = "0.37.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "165ca6e57b20e1351573e3729b958bc62f0e48025386970b6e4d29e7a7e71f3f"
dependencies = [
"bitflags",
"fallible-iterator",
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
"smallvec",
]
[[package]]
name = "rustc-hash"
version = "1.1.0"
@@ -1316,6 +1384,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.5"
@@ -1344,7 +1418,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5ff8685b587722894b6f25958fc608d315b93b8d23647bfb567664e7006d757"
dependencies = [
"bitflags",
"hashbrown",
"hashbrown 0.17.1",
"indexmap",
"semver",
"serde",
+2
View File
@@ -23,6 +23,8 @@ hmac = "0.12"
sha2 = "0.10"
base64 = "0.22"
getrandom = "0.3"
# Ratings: one SQLite file on a small volume, as the telemetry server keeps its data.
rusqlite = { version = "0.37", features = ["bundled"] }
[dev-dependencies]
tower = { version = "0.5", features = ["util"] }
+6 -2
View File
@@ -4,11 +4,15 @@ WORKDIR /src
# The root manifest only for the shared crate's workspace settings (edition, lints).
COPY Cargo.toml Cargo.toml
COPY crates/horizon-mods crates/horizon-mods
# horizon-mods' optional dependency: cargo needs its manifest even with the feature off.
COPY crates/horizon-shader-api crates/horizon-shader-api
COPY crates/horizon-mod-registry crates/horizon-mod-registry
RUN cargo build --release --locked --manifest-path crates/horizon-mod-registry/Cargo.toml
FROM debian:stable-slim
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates && rm -rf /var/lib/apt/lists/*
COPY --from=build /src/crates/horizon-mod-registry/target/release/horizon-mod-registry /usr/local/bin/
USER nobody
CMD ["horizon-mod-registry"]
ENV REGISTRY_DB=/data/registry.sqlite3
# Railway mounts the ratings' volume at /data owned by root: hand it to nobody, then run as nobody (setpriv is
# util-linux's, in every Debian image).
CMD ["sh", "-c", "mkdir -p /data && chown nobody:nogroup /data && exec setpriv --reuid=nobody --regid=nogroup --clear-groups horizon-mod-registry"]
+16 -8
View File
@@ -7,20 +7,26 @@ entry's fields), uploads it to the `staging` release of
request adding or updating `mods/<id>.toml`. That repository's CI checks it again, with a trial run of a mod in the
sandbox; reviewers decide, and merging publishes. Formats and rules: [docs/api/mod-index.md](../../docs/api/mod-index.md).
It keeps no data: a session is a signed cookie holding the GitHub login, and the user's OAuth token is used once, to
read that login, and dropped. The sign-in asks for no scope.
Signed-in players also rate published projects, 1 to 5 stars: one rating per GitHub account and project, which they can
change, never their own project's. That is the only data it keeps, in one SQLite file (`REGISTRY_DB`); it only ever
shows totals, never who rated. A session is a signed cookie holding the GitHub login and id, and the user's OAuth token
is used once, to read them, and dropped. The sign-in asks for no scope.
| Route | |
| --- | --- |
| `GET /api/mods/auth/login` | Starts the GitHub sign-in |
| `GET /api/mods/auth/callback` | GitHub's redirect: sets the session, back to `/mods/submit` |
| `GET /api/mods/auth/login` | Starts the GitHub sign-in; `?back=/mods/<id>` (or `/mods`) comes back to that page |
| `GET /api/mods/auth/callback` | GitHub's redirect: sets the session, back to `/mods/submit` or the `back` page |
| `POST /api/mods/auth/logout` | Ends it |
| `GET /api/mods/me` | `{login, csrf}`, or 401 |
| `POST /api/mods/submit` | Multipart: `entry` (JSON: `id kind name summary description authors license source icon key signature version game changelog`) and `file`; `{pull_request}` or `{error}` |
| `GET /api/mods/ratings` | `{projects: {<id>: {average, count}}}` for every rated project; cached a minute |
| `GET /api/mods/<id>/rating` | The signed-in account's own `{stars}` (null when it hasn't rated), or 401 |
| `POST /api/mods/<id>/rating` | JSON `{stars}`, 1 to 5: sets or changes it; `{stars, average, count}` or `{error}` |
| `GET /healthz` | `ok` |
A submission must come from `REGISTRY_PUBLIC_URL`'s origin with the session's `X-CSRF-Token`. Six per account and sixty
in all per hour. Only an entry's `owners` publish its updates; a key change, an icon in the repository or anything else
A submission or a rating must come from `REGISTRY_PUBLIC_URL`'s origin with the session's `X-CSRF-Token`. Six
submissions per account and sixty in all per hour; thirty ratings per account and 600 in all. A project's owners can't
rate it, and only projects on the registry's `main` can be rated. Only an entry's `owners` publish its updates; a key change, an icon in the repository or anything else
is a pull request by hand.
## Running it
@@ -28,7 +34,7 @@ is a pull request by hand.
```sh
cargo test # against a mock GitHub (tests/submit.rs)
REGISTRY_PUBLIC_URL=http://localhost:4173 REGISTRY_SESSION_KEY=... GITHUB_CLIENT_ID=... GITHUB_CLIENT_SECRET=... \
GITHUB_BOT_TOKEN=... REGISTRY_REPO=you/horizon-mods-test cargo run
GITHUB_BOT_TOKEN=... REGISTRY_REPO=you/horizon-mods-test REGISTRY_DB=/tmp/registry.sqlite3 cargo run
```
| Variable | |
@@ -38,10 +44,12 @@ REGISTRY_PUBLIC_URL=http://localhost:4173 REGISTRY_SESSION_KEY=... GITHUB_CLIENT
| `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET` | The OAuth app |
| `GITHUB_BOT_TOKEN` | Fine-grained, the registry repository only: Contents and Pull requests read and write |
| `REGISTRY_REPO` | Default `ludovic111/horizon-mods` |
| `REGISTRY_DB` | The ratings' SQLite file: default `/data/registry.sqlite3`, the volume |
| `PORT` | Default 8080 |
On Railway it is the service `horizon-mod-registry` of the project `horizon-site`, next to the website: built from
`main` with this `Dockerfile` from the repository root, health check `/healthz`, no public domain and no volume. The
`main` with this `Dockerfile` from the repository root, health check `/healthz`, no public domain, and the volume
`registry-data` at `/data` for the ratings. The
website's Caddy sends `/api/mods/*` to it over Railway's private network (`REGISTRY_UPSTREAM` on the website service:
`http://horizon-mod-registry.railway.internal:8080`). `railway.json` lists the same settings; Railway reads them from
the service, not from the file.
+235 -45
View File
@@ -2,13 +2,15 @@
//! `playhorizon.dev/api/mods/*` (docs/api/mod-index.md). A creator signs in
//! with GitHub, sends a file and its page; the service runs the file's own
//! checks, uploads it to the registry's `staging` release and opens a pull
//! request with the entry. Reviewers decide; merging publishes. It keeps no
//! data: sessions are signed cookies, and GitHub holds everything else.
//! request with the entry. Reviewers decide; merging publishes. Signed-in
//! players also rate published projects (`ratings`, its only data: sessions
//! are signed cookies, and GitHub holds everything else).
pub mod github;
pub mod ratings;
use axum::{
Json, Router,
extract::{DefaultBodyLimit, Multipart, Query, State},
extract::{DefaultBodyLimit, Multipart, Path as UrlPath, Query, State},
http::{HeaderMap, StatusCode, header},
response::{IntoResponse, Redirect, Response},
routing::{get, post},
@@ -20,6 +22,7 @@ use horizon_mods::index::{
self, Kind,
registry::{self, Entry, EntryVersion, Sources},
};
use ratings::Ratings;
use serde::Deserialize;
use serde_json::json;
use sha2::Sha256;
@@ -37,6 +40,12 @@ const PER_ACCOUNT: usize = 6;
const PER_HOUR: usize = 60;
/// The page's JSON.
const MAX_FORM: usize = 64 << 10;
/// Ratings set per account per hour, and by everyone.
const RATINGS_PER_ACCOUNT: usize = 30;
const RATINGS_PER_HOUR: usize = 600;
/// How long the ratings' totals are served from memory (and cached by
/// browsers and the launcher).
const TOTALS_FOR: Duration = Duration::from_secs(60);
pub struct Config {
/// `https://playhorizon.dev`: where the form is, and the only origin it
@@ -47,11 +56,14 @@ pub struct Config {
pub github: GitHub,
/// Where files wait during a submission.
pub scratch: PathBuf,
/// The ratings' SQLite file (a volume): `:memory:` keeps them in memory.
pub database: PathBuf,
}
impl Config {
/// `REGISTRY_PUBLIC_URL`, `REGISTRY_SESSION_KEY`, `REGISTRY_REPO`,
/// `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET`, `GITHUB_BOT_TOKEN`.
/// `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET`, `GITHUB_BOT_TOKEN`,
/// `REGISTRY_DB` (default `/data/registry.sqlite3`).
pub fn from_env() -> Result<Config, String> {
let var = |name: &str| std::env::var(name).map_err(|_| format!("{name} is not set"));
let key = var("REGISTRY_SESSION_KEY")?;
@@ -69,6 +81,8 @@ impl Config {
var("GITHUB_BOT_TOKEN")?,
),
scratch: std::env::temp_dir().join("horizon-mod-registry"),
database: std::env::var_os("REGISTRY_DB")
.map_or_else(|| PathBuf::from("/data/registry.sqlite3"), PathBuf::from),
})
}
@@ -80,17 +94,31 @@ impl Config {
#[derive(Clone)]
pub struct App(Arc<Inner>);
type Windows = Mutex<HashMap<String, VecDeque<Instant>>>;
struct Inner {
config: Config,
recent: Mutex<HashMap<String, VecDeque<Instant>>>,
/// Submissions in the last hour, by account ("" for everyone's).
recent: Windows,
/// Ratings set in the last hour, the same way.
rated: Windows,
ratings: Mutex<Ratings>,
/// `GET /api/mods/ratings`' body, and when it was made.
totals: Mutex<Option<(Instant, String)>>,
}
impl App {
pub fn new(config: Config) -> App {
App(Arc::new(Inner {
/// Fails when the ratings' file can't be opened.
pub fn new(config: Config) -> Result<App, String> {
let ratings = Ratings::open(&config.database)
.map_err(|e| format!("{}: {e}", config.database.display()))?;
Ok(App(Arc::new(Inner {
config,
recent: Mutex::new(HashMap::new()),
}))
recent: Mutex::default(),
rated: Mutex::default(),
ratings: Mutex::new(ratings),
totals: Mutex::default(),
})))
}
}
@@ -101,6 +129,8 @@ pub fn router(app: App) -> Router {
.route("/api/mods/auth/callback", get(callback))
.route("/api/mods/auth/logout", post(logout))
.route("/api/mods/me", get(me))
.route("/api/mods/ratings", get(totals))
.route("/api/mods/{id}/rating", get(my_rating).post(rate))
.route(
"/api/mods/submit",
post(submit).layer(DefaultBodyLimit::max(
@@ -203,9 +233,29 @@ fn csrf(app: &Inner, headers: &HeaderMap) -> Option<String> {
Some(signed.rsplit('.').next()?.chars().take(32).collect())
}
async fn login(State(app): State<App>) -> Response {
#[derive(Deserialize)]
struct Login {
/// The site's page to come back to: `/mods`, `/mods/submit` (the
/// default) or `/mods/<id>`.
back: Option<String>,
}
/// Whether `path` is a registry page a sign-in may come back to.
fn registry_page(path: &str) -> bool {
match path.strip_prefix("/mods") {
Some("") => true,
Some(rest) => rest.strip_prefix('/').is_some_and(index::valid_id) || rest == "/submit",
None => false,
}
}
async fn login(State(app): State<App>, Query(q): Query<Login>) -> Response {
let state = random_hex();
let signed = sign(&app.0.config.session_key, &state);
let back = q
.back
.filter(|b| registry_page(b))
.unwrap_or_else(|| "/mods/submit".into());
let signed = sign(&app.0.config.session_key, &format!("{state}\n{back}"));
let url = app
.0
.config
@@ -229,13 +279,17 @@ async fn callback(
headers: HeaderMap,
Query(q): Query<Callback>,
) -> Result<Response, Refusal> {
let expected = cookie(&headers, "hm_state").and_then(|v| unsign(&app.0.config.session_key, &v));
if expected.as_deref() != Some(q.state.as_str()) {
let started = cookie(&headers, "hm_state").and_then(|v| unsign(&app.0.config.session_key, &v));
let Some((_, back)) = started
.as_deref()
.and_then(|s| s.split_once('\n'))
.filter(|(state, _)| *state == q.state)
else {
return Err(Refusal::new(
StatusCode::FORBIDDEN,
"the sign-in expired or didn't start here: try again",
));
}
};
let inner = app.0.clone();
let (login, id) = tokio::task::spawn_blocking(move || {
inner
@@ -250,7 +304,7 @@ async fn callback(
&app.0.config.session_key,
&format!("{login}\n{id}\n{}", now() + SESSION),
);
let back = format!("{}/mods/submit", app.0.config.public_url);
let back = format!("{}{back}", app.0.config.public_url);
let mut response = Redirect::to(&back).into_response();
let headers = response.headers_mut();
headers.append(
@@ -283,6 +337,131 @@ async fn me(
))
}
fn database(e: rusqlite::Error) -> Refusal {
eprintln!("horizon-mod-registry: ratings: {e}");
Refusal::new(
StatusCode::INTERNAL_SERVER_ERROR,
"the ratings can't be read right now",
)
}
/// A mean as the pages show it: two decimals.
fn rounded(average: f64) -> f64 {
(average * 100.0).round() / 100.0
}
/// `GET /api/mods/ratings`: `{"projects": {"<id>": {"average", "count"}}}` for
/// every rated project. Totals only: never who rated.
async fn totals(State(app): State<App>) -> Result<Response, Refusal> {
let mut cached = app.0.totals.lock().unwrap();
let body = match cached.as_ref().filter(|(at, _)| at.elapsed() < TOTALS_FOR) {
Some((_, body)) => body.clone(),
None => {
let all = app
.0
.ratings
.lock()
.unwrap()
.summaries()
.map_err(database)?;
let projects: serde_json::Map<String, serde_json::Value> = all
.into_iter()
.map(|(id, s)| (id, json!({"average": rounded(s.average), "count": s.count})))
.collect();
let body = json!({ "projects": projects }).to_string();
*cached = Some((Instant::now(), body.clone()));
body
}
};
Ok((
[
(header::CONTENT_TYPE, "application/json"),
(header::CACHE_CONTROL, "public, max-age=60"),
],
body,
)
.into_response())
}
/// `GET /api/mods/{id}/rating`: the signed-in account's own rating,
/// `{"stars": 1..5}` or `{"stars": null}`.
async fn my_rating(
State(app): State<App>,
headers: HeaderMap,
UrlPath(id): UrlPath<String>,
) -> Result<Json<serde_json::Value>, Refusal> {
let (_, account) = session(&app.0, &headers)
.ok_or_else(|| Refusal::new(StatusCode::UNAUTHORIZED, "not signed in"))?;
let stars = app
.0
.ratings
.lock()
.unwrap()
.mine(&id, account)
.map_err(database)?;
Ok(Json(json!({ "stars": stars })))
}
#[derive(Deserialize)]
struct Rating {
stars: u8,
}
/// `POST /api/mods/{id}/rating` with `{"stars": 1..5}`: sets or changes the
/// signed-in account's rating of a published project, not its own. Answers
/// the project's new totals and `stars`.
async fn rate(
State(app): State<App>,
headers: HeaderMap,
UrlPath(id): UrlPath<String>,
Json(rating): Json<Rating>,
) -> Result<Json<serde_json::Value>, Refusal> {
let inner = app.0.clone();
let (login, account) = signed_in_here(&inner, &headers)?;
if !(1..=5).contains(&rating.stars) {
return Err(bad("a rating is 1 to 5 stars"));
}
if !index::valid_id(&id) {
return Err(Refusal::new(StatusCode::NOT_FOUND, "no such project"));
}
if !allow(&inner.rated, &login, RATINGS_PER_ACCOUNT, RATINGS_PER_HOUR) {
return Err(Refusal::new(
StatusCode::TOO_MANY_REQUESTS,
"too many ratings this hour: try again later",
));
}
let lookup = inner.clone();
let wanted = id.clone();
let entry = tokio::task::spawn_blocking(move || lookup.config.github.entry(&wanted))
.await
.map_err(|_| Refusal::new(StatusCode::INTERNAL_SERVER_ERROR, "the check crashed"))?
.map_err(github_down)?;
let Some((entry, _)) = entry else {
return Err(Refusal::new(
StatusCode::NOT_FOUND,
format!("{id} isn't published in the registry"),
));
};
if entry.owners.iter().any(|o| o.eq_ignore_ascii_case(&login)) {
return Err(Refusal::new(
StatusCode::FORBIDDEN,
"you publish this project: its players rate it",
));
}
let summary = {
let ratings = inner.ratings.lock().unwrap();
ratings
.rate(&id, account, rating.stars, now())
.map_err(database)?;
ratings.summary(&id).map_err(database)?
};
*inner.totals.lock().unwrap() = None;
let (average, count) = summary.map_or((0.0, 0), |s| (rounded(s.average), s.count));
Ok(Json(
json!({"stars": rating.stars, "average": average, "count": count}),
))
}
/// The page a submission fills in: a project's fields, and the version's.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
@@ -322,45 +501,56 @@ fn take(window: &mut VecDeque<Instant>, max: usize) -> bool {
window.len() < max
}
/// A change sent by the site's own pages: from its origin, signed in, with
/// the session's CSRF token. The account: (login, id).
fn signed_in_here(app: &Inner, headers: &HeaderMap) -> Result<(String, u64), Refusal> {
let origin = headers.get(header::ORIGIN).and_then(|v| v.to_str().ok());
if origin != Some(app.config.public_url.as_str()) {
return Err(Refusal::new(
StatusCode::FORBIDDEN,
"send it from the registry's pages",
));
}
let account = session(app, headers)
.ok_or_else(|| Refusal::new(StatusCode::UNAUTHORIZED, "sign in with GitHub first"))?;
let sent = headers.get("x-csrf-token").and_then(|v| v.to_str().ok());
if sent.is_none() || sent.map(str::to_owned) != csrf(app, headers) {
return Err(Refusal::new(
StatusCode::FORBIDDEN,
"reload the page and try again",
));
}
Ok(account)
}
/// Counts one more for `login` and for everyone, unless either is at its
/// hourly limit.
fn allow(windows: &Windows, login: &str, per_account: usize, per_hour: usize) -> bool {
let mut windows = windows.lock().unwrap();
let account = login.to_lowercase();
let ok = take(windows.entry(String::new()).or_default(), per_hour)
&& take(windows.entry(account.clone()).or_default(), per_account);
if ok {
let at = Instant::now();
windows.entry(String::new()).or_default().push_back(at);
windows.entry(account).or_default().push_back(at);
}
ok
}
async fn submit(
State(app): State<App>,
headers: HeaderMap,
mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, Refusal> {
let inner = app.0.clone();
let origin = headers.get(header::ORIGIN).and_then(|v| v.to_str().ok());
if origin != Some(inner.config.public_url.as_str()) {
let (login, user_id) = signed_in_here(&inner, &headers)?;
if !allow(&inner.recent, &login, PER_ACCOUNT, PER_HOUR) {
return Err(Refusal::new(
StatusCode::FORBIDDEN,
"submit from the registry's form",
StatusCode::TOO_MANY_REQUESTS,
"too many submissions this hour: try again later",
));
}
let (login, user_id) = session(&inner, &headers)
.ok_or_else(|| Refusal::new(StatusCode::UNAUTHORIZED, "sign in with GitHub first"))?;
let sent = headers.get("x-csrf-token").and_then(|v| v.to_str().ok());
if sent.is_none() || sent.map(str::to_owned) != csrf(&inner, &headers) {
return Err(Refusal::new(
StatusCode::FORBIDDEN,
"reload the form and try again",
));
}
{
let mut recent = inner.recent.lock().unwrap();
let ok = take(recent.entry(String::new()).or_default(), PER_HOUR)
&& take(recent.entry(login.to_lowercase()).or_default(), PER_ACCOUNT);
if !ok {
return Err(Refusal::new(
StatusCode::TOO_MANY_REQUESTS,
"too many submissions this hour: try again later",
));
}
let at = Instant::now();
recent.entry(String::new()).or_default().push_back(at);
recent
.entry(login.to_lowercase())
.or_default()
.push_back(at);
}
let dir = inner.config.scratch.join(random_hex());
std::fs::create_dir_all(&dir)
.map_err(|e| Refusal::new(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
+3 -2
View File
@@ -1,5 +1,5 @@
//! `horizon-mod-registry`: serves on `PORT` (default 8080). Configured by the
//! environment (see `Config::from_env`); keeps no data.
//! environment (see `Config::from_env`); keeps the ratings in `REGISTRY_DB`.
use std::net::SocketAddr;
use horizon_mod_registry::{App, Config, router};
@@ -16,7 +16,8 @@ async fn main() {
.await
.unwrap_or_else(|e| panic!("port {port}: {e}"));
eprintln!("horizon-mod-registry: listening on {port}, submitting to {repo}");
axum::serve(listener, router(App::new(config)))
let app = App::new(config).unwrap_or_else(|e| panic!("horizon-mod-registry: {e}"));
axum::serve(listener, router(app))
.with_graceful_shutdown(async {
let _ = tokio::signal::ctrl_c().await;
})
+129
View File
@@ -0,0 +1,129 @@
//! Players' ratings of published projects, 1 to 5 stars: one per GitHub
//! account and project, which its rater can change. Stored in one SQLite
//! file (a Railway volume). Only totals leave it: never who rated what,
//! except a signed-in account's own rating, to that account.
use rusqlite::{Connection, OptionalExtension, params};
use std::{collections::BTreeMap, path::Path};
const SCHEMA: &str = "
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
CREATE TABLE IF NOT EXISTS ratings (
project TEXT NOT NULL,
account INTEGER NOT NULL,
stars INTEGER NOT NULL CHECK (stars BETWEEN 1 AND 5),
updated INTEGER NOT NULL,
PRIMARY KEY (project, account)
) WITHOUT ROWID;
";
/// A project's ratings: their mean, and how many.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Summary {
pub average: f64,
pub count: u64,
}
pub struct Ratings {
db: Connection,
}
impl Ratings {
/// Opens (or creates) the file at `path`; `:memory:` for a test.
pub fn open(path: &Path) -> rusqlite::Result<Ratings> {
let db = Connection::open(path)?;
db.execute_batch(SCHEMA)?;
Ok(Ratings { db })
}
/// Sets `account`'s rating of `project`, replacing an earlier one.
pub fn rate(&self, project: &str, account: u64, stars: u8, now: u64) -> rusqlite::Result<()> {
self.db.execute(
"INSERT INTO ratings (project, account, stars, updated) VALUES (?1, ?2, ?3, ?4)
ON CONFLICT (project, account) DO UPDATE SET stars = excluded.stars, updated = excluded.updated",
params![project, account as i64, stars, now as i64],
)?;
Ok(())
}
/// `account`'s own rating of `project`.
pub fn mine(&self, project: &str, account: u64) -> rusqlite::Result<Option<u8>> {
self.db
.query_row(
"SELECT stars FROM ratings WHERE project = ?1 AND account = ?2",
params![project, account as i64],
|r| r.get(0),
)
.optional()
}
/// `project`'s summary; None when nobody rated it.
pub fn summary(&self, project: &str) -> rusqlite::Result<Option<Summary>> {
self.db
.query_row(
"SELECT avg(stars), count(*) FROM ratings WHERE project = ?1 HAVING count(*) > 0",
[project],
|r| {
Ok(Summary {
average: r.get(0)?,
count: r.get::<_, i64>(1)? as u64,
})
},
)
.optional()
}
/// Every rated project's summary.
pub fn summaries(&self) -> rusqlite::Result<BTreeMap<String, Summary>> {
let mut statement = self
.db
.prepare("SELECT project, avg(stars), count(*) FROM ratings GROUP BY project")?;
let rows = statement.query_map([], |r| {
Ok((
r.get::<_, String>(0)?,
Summary {
average: r.get(1)?,
count: r.get::<_, i64>(2)? as u64,
},
))
})?;
rows.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_rating_per_account_and_project() {
let r = Ratings::open(Path::new(":memory:")).unwrap();
r.rate("zoom", 1, 5, 10).unwrap();
r.rate("zoom", 2, 2, 10).unwrap();
r.rate("zoom", 1, 4, 11).unwrap();
r.rate("minimap", 1, 3, 11).unwrap();
assert_eq!(r.mine("zoom", 1).unwrap(), Some(4));
assert_eq!(r.mine("zoom", 3).unwrap(), None);
let all = r.summaries().unwrap();
assert_eq!(
all["zoom"],
Summary {
average: 3.0,
count: 2
}
);
assert_eq!(
all["minimap"],
Summary {
average: 3.0,
count: 1
}
);
assert_eq!(r.summary("zoom").unwrap(), Some(all["zoom"]));
assert_eq!(r.summary("nothing").unwrap(), None);
assert!(
r.rate("zoom", 1, 6, 12).is_err(),
"the table checks the range too"
);
}
}
+113 -8
View File
@@ -1,6 +1,6 @@
//! The whole submission against a mock GitHub: sign in, submit the sample
//! signed mod (`crates/horizon-mods/tests/index`), and what lands on GitHub;
//! then what is refused.
//! then what is refused. Then ratings: who may rate what, and what is shown.
use axum::{
Json, Router,
body::{Body, Bytes},
@@ -55,7 +55,9 @@ async fn mock_github() -> (String, Shared) {
.route(
"/user",
get(|State(s): State<Shared>| async move {
Json(json!({"login": s.lock().unwrap().login, "id": 7}))
let login = s.lock().unwrap().login.clone();
let id = if login == "alice" { 7 } else { 8 };
Json(json!({"login": login, "id": id}))
}),
)
.route(
@@ -174,7 +176,16 @@ impl Client {
}
async fn sign_in(&mut self) -> String {
let (status, _, location) = self.send(self.get("/api/mods/auth/login")).await;
self.sign_in_from("").await
}
/// Signs in from the page `back` (`?back=` of the sign-in; "" for none).
async fn sign_in_from(&mut self, back: &str) -> String {
let login = match back {
"" => "/api/mods/auth/login".to_owned(),
back => format!("/api/mods/auth/login?back={back}"),
};
let (status, _, location) = self.send(self.get(&login)).await;
assert_eq!(status, StatusCode::SEE_OTHER);
let location = location.unwrap();
assert!(location.contains(
@@ -197,10 +208,11 @@ impl Client {
.send(self.get(&format!("/api/mods/auth/callback?code=good&state={state}")))
.await;
assert_eq!(status, StatusCode::SEE_OTHER);
assert_eq!(
location.as_deref(),
Some("https://playhorizon.test/mods/submit")
);
let page = match back {
"/mods" | "/mods/zoom" => back,
_ => "/mods/submit",
};
assert_eq!(location, Some(format!("https://playhorizon.test{page}")));
let (status, me, _) = self.send(self.get("/api/mods/me")).await;
assert_eq!(status, StatusCode::OK);
me["csrf"].as_str().unwrap().to_owned()
@@ -265,9 +277,10 @@ async fn setup() -> (Client, Shared) {
"bot".into(),
),
scratch,
database: ":memory:".into(),
};
let client = Client {
app: router(App::new(config)),
app: router(App::new(config).unwrap()),
cookies: HashMap::new(),
};
(client, mock)
@@ -376,3 +389,95 @@ async fn what_the_form_refuses() {
"nothing reached GitHub"
);
}
const ENTRY: &str = "kind = \"mod\"\nname = \"N\"\nsummary = \"s\"\nlicense = \"MIT\"\nsource = \"https://x.test\"\n\n[[versions]]\nversion = \"1.0.0\"\npublished = \"2026-01-01\"\ngame = \"26.3\"\nfile = \"f.hmod\"\nsha256 = \"0000000000000000000000000000000000000000000000000000000000000000\"\n";
impl Client {
async fn rate(&mut self, csrf: &str, id: &str, body: &str) -> (StatusCode, Value) {
let request = Request::post(format!("/api/mods/{id}/rating"))
.header(header::COOKIE, self.cookie_header())
.header(header::ORIGIN, PUBLIC)
.header("x-csrf-token", csrf)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(body.to_owned()))
.unwrap();
let (status, json, _) = self.send(request).await;
(status, json)
}
}
#[tokio::test(flavor = "multi_thread")]
async fn ratings() {
let (mut client, mock) = setup().await;
{
let mut m = mock.lock().unwrap();
m.main.insert(
"mods/zoom.toml".into(),
format!("id = \"zoom\"\nowners = [\"bob\"]\n{ENTRY}"),
);
m.main.insert(
"mods/mine.toml".into(),
format!("id = \"mine\"\nowners = [\"Alice\"]\n{ENTRY}"),
);
}
// Nothing rated yet; signed out, no own rating and no rating at all.
let (status, json, _) = client.send(client.get("/api/mods/ratings")).await;
assert_eq!((status, json), (StatusCode::OK, json!({"projects": {}})));
let (status, ..) = client.send(client.get("/api/mods/zoom/rating")).await;
assert_eq!(status, StatusCode::UNAUTHORIZED);
let (status, _) = client.rate("x", "zoom", r#"{"stars": 4}"#).await;
assert_eq!(status, StatusCode::UNAUTHORIZED);
// A sign-in from a project's page comes back to it; anywhere else is the form.
client.sign_in_from("https://evil.test/").await;
let csrf = client.sign_in_from("/mods/zoom").await;
let (status, json) = client.rate("wrong", "zoom", r#"{"stars": 4}"#).await;
assert_eq!(status, StatusCode::FORBIDDEN, "{json}");
for stars in ["0", "6"] {
let (status, json) = client
.rate(&csrf, "zoom", &format!(r#"{{"stars": {stars}}}"#))
.await;
assert_eq!(status, StatusCode::BAD_REQUEST, "{json}");
}
let (status, json) = client.rate(&csrf, "zoom", r#"{"stars": 4}"#).await;
assert_eq!(status, StatusCode::OK, "{json}");
assert_eq!(json, json!({"stars": 4, "average": 4.0, "count": 1}));
// Changing it replaces it.
let (status, json) = client.rate(&csrf, "zoom", r#"{"stars": 2}"#).await;
assert_eq!(status, StatusCode::OK, "{json}");
assert_eq!(json["count"], 1);
let (_, json, _) = client.send(client.get("/api/mods/zoom/rating")).await;
assert_eq!(json, json!({"stars": 2}));
let (_, json, _) = client.send(client.get("/api/mods/minimap/rating")).await;
assert_eq!(json, json!({"stars": null}));
// Not one's own project, nor one that isn't published.
let (status, json) = client.rate(&csrf, "mine", r#"{"stars": 5}"#).await;
assert_eq!(status, StatusCode::FORBIDDEN, "{json}");
let (status, json) = client.rate(&csrf, "nothing", r#"{"stars": 5}"#).await;
assert_eq!(status, StatusCode::NOT_FOUND, "{json}");
// Another account's rating counts once more; the totals never say whose.
let mut other = Client {
app: client.app.clone(),
cookies: HashMap::new(),
};
mock.lock().unwrap().login = "carol".into();
let carol = other.sign_in().await;
let (status, json) = other.rate(&carol, "zoom", r#"{"stars": 5}"#).await;
assert_eq!(status, StatusCode::OK, "{json}");
assert_eq!(json, json!({"stars": 5, "average": 3.5, "count": 2}));
let (status, json, _) = client.send(client.get("/api/mods/ratings")).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(
json,
json!({"projects": {"zoom": {"average": 3.5, "count": 2}}})
);
// The limit per account and hour.
let mut last = StatusCode::OK;
for _ in 0..40 {
(last, _) = client.rate(&csrf, "zoom", r#"{"stars": 3}"#).await;
}
assert_eq!(last, StatusCode::TOO_MANY_REQUESTS);
}
+20 -1
View File
@@ -18,7 +18,8 @@ interface log {
write: func(level: level, message: string);
}
/// Capability `game-read`: the local player and the world clock.
/// Capability `game-read`: the local player, the world clock and the block
/// the player looks at.
interface game {
record player {
/// Feet position, in blocks.
@@ -46,6 +47,24 @@ interface game {
get-player: func() -> option<player>;
/// None on the title screen.
get-clock: func() -> option<clock>;
/// A block along the player's view.
record hit {
/// The block hit.
block-x: s32,
block-y: s32,
block-z: s32,
/// The point on its face where the view meets it.
x: f64,
y: f64,
z: f64,
/// `minecraft:stone`, ...
block: string,
}
/// The first block along the player's view (fluids pass), up to
/// `distance` blocks (at most 1024; past the loaded chunks, across the
/// distant terrain when LOD Distance is on), as of this tick. None when
/// nothing is hit, and on the title screen.
raycast: func(distance: f32) -> option<hit>;
}
/// Capability `chat`: messages only the local player sees.
+3 -2
View File
@@ -7,13 +7,13 @@
//! version's level so the launcher can tell before it installs.
/// The newest level this Horizon serves.
pub const LEVEL: u32 = 1;
pub const LEVEL: u32 = 2;
/// Every function the game offers mods, as `interface.function`, with its
/// level. Level 0 is the contract of Horizon 0.2.67 to 0.2.69. A new
/// function takes the next level and raises [`LEVEL`] (the `api_reference`
/// test fails until it is listed here).
pub const FUNCTIONS: [(&str, u32); 32] = [
pub const FUNCTIONS: [(&str, u32); 33] = [
("log.write", 0),
("game.get-player", 0),
("game.get-clock", 0),
@@ -46,6 +46,7 @@ pub const FUNCTIONS: [(&str, u32); 32] = [
("players.online", 1),
("players.tell", 1),
("players.broadcast", 1),
("game.raycast", 2),
];
/// What the game's program carries so the launcher can read its level
+8
View File
@@ -122,6 +122,14 @@ pub struct Project {
pub key: Option<String>,
/// Newest first.
pub versions: Vec<Version>,
/// Its files' downloads from their releases, all versions together:
/// GitHub's counts, added by the registry's publish job (hourly).
#[serde(default, skip_serializing_if = "is_zero")]
pub downloads: u64,
}
fn is_zero(n: &u64) -> bool {
*n == 0
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@@ -231,6 +231,7 @@ pub fn project(entry: &Entry, src: &Sources) -> Result<Project, Vec<String>> {
icon,
key: entry.key.as_ref().map(|k| k.trim().to_owned()),
versions,
downloads: 0,
})
}
+7 -1
View File
@@ -34,8 +34,14 @@ fn reads_the_sample_and_keeps_unknown_kinds() {
[Capability::GameRead, Capability::Hud, Capability::Chat]
);
// What CI writes reads back the same.
let again = Index::parse(&serde_json::to_string(&index).unwrap()).unwrap();
let text = serde_json::to_string(&index).unwrap();
let again = Index::parse(&text).unwrap();
assert_eq!(again, index);
// Download counts: the publish job adds them; none is left out.
assert_eq!(hud.downloads, 0);
assert!(!text.contains("downloads"));
let counted = text.replacen("\"versions\"", "\"downloads\":12345,\"versions\"", 1);
assert_eq!(Index::parse(&counted).unwrap().projects[0].downloads, 12345);
}
#[test]
+27 -2
View File
@@ -41,7 +41,7 @@ mod ui_bindings {
});
}
pub use bindings::horizon::mods::{
game::{Clock, Player},
game::{Clock, Hit, Player},
hud::{Corner, Line},
};
use bindings::{
@@ -89,6 +89,8 @@ const FUEL_FRAME: u64 = 10_000_000;
const FUEL_EVENT: u64 = 100_000_000;
/// Chat lines a mod may show per call, HUD lines and their length.
const CHAT_PER_CALL: usize = 8;
/// How far `game.raycast` sees, in blocks.
pub const RAYCAST_MAX: f32 = 1024.0;
const HUD_LINES: usize = 16;
const LINE_CHARS: usize = 256;
/// Total bytes a mod's `storage` may hold on disk, across all its keys.
@@ -124,6 +126,10 @@ pub struct Snapshot {
pub font_widths: Arc<[f32]>,
/// The players on the server, for server mods.
pub players: Vec<ServerPlayer>,
/// The first block along the player's view within [`RAYCAST_MAX`] and
/// its distance from the eye, for `game.raycast`; the game works it out
/// only while a mod calls it ([`Mods::wants_raycast`]).
pub view_hit: Option<(f32, Hit)>,
}
/// Which mods a game starts: the window's game the client mods, the game
@@ -227,6 +233,8 @@ struct Running {
/// server mod.
ui: Option<UiHost>,
hooks: Hooks,
/// It imports `game.raycast`.
raycasts: bool,
}
/// A running mod's exports and the hooks its `init` asked for.
@@ -490,7 +498,12 @@ impl Mods {
free
});
self.out.append(&mut store.data_mut().out);
Ok(Some(Running { store, ui, hooks }))
Ok(Some(Running {
store,
ui,
hooks,
raycasts: functions.iter().any(|f| f == "game.raycast"),
}))
}
/// True when at least one mod runs: the game skips its mod work otherwise.
@@ -743,6 +756,13 @@ impl Mods {
.iter()
.filter_map(|e| e.run.as_ref()?.store.data().corner.as_ref())
}
/// Some running mod calls `game.raycast`: the snapshot needs a
/// [`Snapshot::view_hit`].
pub fn wants_raycast(&self) -> bool {
self.entries
.iter()
.any(|e| e.run.as_ref().is_some_and(|r| r.raycasts))
}
/// Some running mod has a `frame` hook: the game refreshes the snapshot
/// every frame for it.
pub fn wants_frames(&self) -> bool {
@@ -1093,6 +1113,11 @@ impl game::Host for State {
fn get_clock(&mut self) -> Option<Clock> {
self.snapshot.clock
}
fn raycast(&mut self, distance: f32) -> Option<Hit> {
let (at, hit) = self.snapshot.view_hit.as_ref()?;
// NaN compares false: no hit.
(*at <= distance.min(RAYCAST_MAX) && !distance.is_nan()).then(|| hit.clone())
}
}
impl State {
/// Queues a chat line unless the call already sent [`CHAT_PER_CALL`].
+44 -3
View File
@@ -2,8 +2,8 @@
//! examples and a guest
//! that misbehaves on command. Needs `rustup target add wasm32-unknown-unknown`.
use horizon_mods::{
BlockPos, Clock, Corner, GameMode, HeldItem, Mods, Output, Package, Player, Runs, ServerPlayer,
Shape, Snapshot, Status,
BlockPos, Clock, Corner, GameMode, HeldItem, Hit, Mods, Output, Package, Player, Runs,
ServerPlayer, Shape, Snapshot, Status,
manifest::componentize,
testing::{Script, TestGame},
};
@@ -26,7 +26,7 @@ id = "test_ui_guest"
name = "Test UI Guest"
version = "0.1.0"
api = "0.1"
capabilities = ["chat", "ui", "render", "input"]
capabilities = ["game-read", "chat", "ui", "render", "input"]
experimental = ["ui", "render", "input"]
"#;
@@ -722,6 +722,47 @@ fn render_minimap_is_clamped_and_hidden() {
assert_eq!(map(&mut mods, "off"), None);
}
/// `game.raycast`: the view's hit the game worked out, up to the distance
/// asked for; the game works it out only while a mod imports the call.
#[test]
fn game_raycast_sees_the_view_hit_within_its_distance() {
let mut mods = load("horizon_mods_test_ui_guest.hmod");
assert!(mods.wants_raycast());
assert!(!load("coords_hud.hmod").wants_raycast());
let said = |mods: &mut Mods, args: &str| -> Vec<String> {
assert!(mods.command(&format!("raycast {args}")));
mods.drain()
.into_iter()
.filter_map(|o| match o {
Output::Chat { text, .. } => Some(text),
_ => None,
})
.collect()
};
assert_eq!(said(&mut mods, "100"), ["no hit"]);
mods.set_snapshot(Snapshot {
view_hit: Some((
40.5,
Hit {
block_x: 12,
block_y: 63,
block_z: -44,
x: 12.5,
y: 64.0,
z: -43.75,
block: "minecraft:grass_block".into(),
},
)),
..player()
});
let hit = "hit minecraft:grass_block at 12 63 -44 (12.5 64.0 -43.8)";
assert_eq!(said(&mut mods, "100"), [hit]);
assert_eq!(said(&mut mods, "40"), ["no hit"]);
// NaN asks for nothing; past 1024 is 1024.
assert_eq!(said(&mut mods, "x"), ["no hit"]);
assert_eq!(said(&mut mods, "1e9"), [hit]);
}
#[test]
fn corner_panels_text_width_and_readings() {
let mut mods = load("horizon_mods_test_ui_guest.hmod");
+14 -2
View File
@@ -4,9 +4,10 @@
//! `/render_hud`, `/render_clear` and `/render_shake` test `render`;
//! `/input_claim`, `/input_poll`, `/input_held` and `/input_wheel` test
//! `input`; `/zoom FACTOR SMOOTH TURN` tests `render.set-zoom`;
//! `/corner` and `/readings` the calls the official extras mods use.
//! `/corner` and `/readings` the calls the official extras mods use;
//! `/raycast DISTANCE` says what `game.raycast` sees.
use horizon_mod_sdk::{
Mod, Registration, chat,
Mod, Registration, chat, game,
input::{self, Event},
render::{self, FillShape, ItemShape, Point, QuadShape, Rect, Shape, SpriteShape, TextShape},
ui::{self, ItemWidget, ListWidget, Screen, TextWidget, Widget},
@@ -97,6 +98,7 @@ impl Mod for Guest {
"input_wheel",
"zoom",
"minimap",
"raycast",
"corner",
"readings",
]
@@ -217,6 +219,16 @@ impl Mod for Guest {
});
render::set_minimap(map);
}
"raycast" => {
let distance = args.parse().unwrap_or(f32::NAN);
chat::show(&match game::raycast(distance) {
Some(h) => format!(
"hit {} at {} {} {} ({:.1} {:.1} {:.1})",
h.block, h.block_x, h.block_y, h.block_z, h.x, h.y, h.z
),
None => "no hit".into(),
});
}
// Clamped to (1, 10); the two after it shake nothing.
"render_shake" => {
render::shake(2.0, 99.0);
+250 -32
View File
@@ -1,5 +1,5 @@
//! The player's own game files: the pinned version JSON, client jar and libraries from their
//! official launcher install, and the server jar they downloaded, each checked against
//! official launcher or ATLauncher install, and the server jar they downloaded, each checked against
//! research-lock.json before it is copied to `.local/minecraft/<version>` (tools/lab.py's
//! `prepare`, which the Python preparer ran first), then checked again on every run
//! (tools/test_rust_parity.py's `verify_artifacts`).
@@ -12,9 +12,10 @@ use std::path::{Component, Path, PathBuf};
const LAUNCHER_HINT: &str =
"Horizon does not download the game: play {version} once in the launcher you play \
Minecraft Java Edition with (the official launcher, Prism Launcher, MultiMC...) so it downloads \
the game's files, then rerun. If that launcher keeps them in another folder, pass \
--minecraft-dir or set HORIZON_MINECRAFT_DIR.";
Minecraft Java Edition with so it downloads the game's files, then rerun. Horizon reads the \
official launcher's folder, ATLauncher's and the CurseForge app's; for Prism Launcher, MultiMC, \
the Modrinth App or GDLauncher, prepare from the Horizon launcher, which finds their files. If your launcher keeps \
them in another folder, pass --minecraft-dir or set HORIZON_MINECRAFT_DIR.";
const SERVER_HINT: &str = "Horizon does not download the game: download the {version} server jar yourself (minecraft.net/download/server \
while {version} is the current release, otherwise the downloads.server.url in your launcher's \
versions/{version}/{version}.json), then pass --server-jar PATH or set HORIZON_SERVER_JAR.";
@@ -23,11 +24,6 @@ fn hint(text: &str, version: &str) -> String {
text.replace("{version}", version)
}
/// What to do when the player's launcher install lacks the pinned version's files.
pub(crate) fn launcher_hint(version: &str) -> String {
hint(LAUNCHER_HINT, version)
}
/// research-lock.json and the folder the pinned version's files go to.
pub struct Lock {
pub json: Value,
@@ -134,6 +130,137 @@ pub fn minecraft_dir(chosen: Option<&Path>) -> PathBuf {
}
}
/// One launcher's copy of the pinned version: its version JSON (Mojang's file, checked by its
/// sha1), the client jar, and the folder holding its `libraries` (at the version JSON's paths)
/// and `assets`.
pub struct GameFiles {
pub folder: PathBuf,
pub manifest: PathBuf,
pub client: PathBuf,
/// Mojang's Java runtimes the launcher downloaded (java::minecraft_runtimes).
pub runtimes: PathBuf,
/// The folders looked in, each with its launcher, when none held the version.
searched: Vec<(&'static str, PathBuf)>,
}
impl GameFiles {
/// What to do when these files are missing, with the folders looked in.
pub(crate) fn hint(&self, version: &str) -> String {
let mut text = hint(LAUNCHER_HINT, version);
if !self.searched.is_empty() {
text += &format!(" Horizon looked for {version} in:");
for (launcher, folder) in &self.searched {
text += &format!("\n {} ({launcher})", folder.display());
}
}
text
}
}
/// The layouts horizon-prepare reads in `folder`: the official launcher's, then ATLauncher's,
/// which saves Mojang's version JSON as is in `configs/json/minecraft` and the client jar as
/// the library `net.minecraft:client:<version>`.
fn layouts(folder: &Path, version: &str) -> [GameFiles; 2] {
let files = |manifest: String, client: String, runtimes: &str| GameFiles {
folder: folder.to_path_buf(),
manifest: folder.join(manifest),
client: folder.join(client),
runtimes: folder.join(runtimes),
searched: Vec::new(),
};
let v = version;
[
files(
format!("versions/{v}/{v}.json"),
format!("versions/{v}/{v}.jar"),
"runtime",
),
files(
format!("configs/json/minecraft/{v}.json"),
format!("libraries/net/minecraft/client/{v}/client-{v}.jar"),
"runtimes/minecraft",
),
]
}
/// The launchers whose folders are looked in when none is chosen: the official launcher's
/// (`minecraft_dir`), ATLauncher's, then the CurseForge app's `Install` folder, where it runs
/// the official launcher (its default modding folder, `curseforge/minecraft`, is in the home
/// folder on Windows and in Documents elsewhere). ATLauncher keeps its files in its working folder:
/// where its Windows installer puts it, inside the macOS app, and the folders its Linux
/// packages and Flatpak start it in.
fn launcher_folders() -> Vec<(&'static str, PathBuf)> {
let mut folders = vec![("the official launcher", minecraft_dir(None))];
// The same folders as the Horizon launcher's (launcher/src/sources.rs `known_folders`).
let atlauncher: Vec<PathBuf> = if cfg!(target_os = "macos") {
let app = Path::new("Applications/ATLauncher.app/Contents/Java");
vec![
home().join("Library/Application Support/ATLauncher"),
Path::new("/").join(app),
home().join(app),
]
} else if cfg!(windows) {
std::env::var_os("APPDATA")
.map(|d| PathBuf::from(d).join("ATLauncher"))
.into_iter()
.collect()
} else {
let data = std::env::var_os("XDG_DATA_HOME")
.filter(|d| !d.is_empty())
.map_or_else(|| home().join(".local/share"), PathBuf::from);
let flatpak = home().join(".var/app/com.atlauncher.ATLauncher/data");
vec![data.join("atlauncher"), flatpak.join("atlauncher"), flatpak]
};
folders.extend(atlauncher.into_iter().map(|dir| ("ATLauncher", dir)));
let curseforge = if cfg!(windows) { "" } else { "Documents" };
folders.push((
"the CurseForge app",
home().join(curseforge).join("curseforge/minecraft/Install"),
));
folders
}
/// The pinned version's files in the chosen folder (`--minecraft-dir`, HORIZON_MINECRAFT_DIR),
/// or the launcher above it when an instance folder (`instances/<name>`) is chosen; with no
/// folder chosen, in the first launcher folder that has them. When none does, the official
/// layout of the first folder is returned, for the errors to name, with the folders looked in.
pub fn game_files(chosen: Option<&Path>, version: &str) -> GameFiles {
let chosen = chosen
.filter(|d| !d.as_os_str().is_empty())
.map(Path::to_path_buf)
.or_else(|| std::env::var_os("HORIZON_MINECRAFT_DIR").map(PathBuf::from))
.filter(|d| !d.as_os_str().is_empty())
.map(|d| expand_user(&d));
let folders = match chosen {
Some(dir) => {
let launcher = dir
.ancestors()
.find(|a| a.file_name().is_some_and(|n| n == "instances"))
.and_then(Path::parent)
.map(Path::to_path_buf);
let mut folders = vec![("the folder chosen", dir)];
folders.extend(launcher.map(|dir| ("the launcher above it", dir)));
folders
}
None => launcher_folders(),
};
// A folder with both files first: a version JSON left without its jar is used, for the
// error to name the jar, only when no folder has both.
let complete = |files: &GameFiles| files.manifest.is_file() && files.client.is_file();
let partial = |files: &GameFiles| files.manifest.is_file();
for found in [complete, partial] {
let mut all = folders
.iter()
.flat_map(|(_, folder)| layouts(folder, version));
if let Some(files) = all.find(found) {
return files;
}
}
let [mut files, _] = layouts(&folders[0].1, version);
files.searched = folders;
files
}
/// Copy one file from the player's install, checked like a download; an existing verified
/// copy is kept.
pub(crate) fn import_copy(
@@ -156,17 +283,14 @@ pub(crate) fn import_copy(
.commit()
}
/// The pinned version JSON from the launcher's versions/ directory. The launcher's copy wins
/// over the cached one so the manifest matches the asset index installed alongside it.
pub(crate) fn import_manifest(lock: &Lock, minecraft: &Path) -> Result<Value, String> {
/// The pinned version JSON from the launcher's install. The launcher's copy wins over the
/// cached one so the manifest matches the asset index installed alongside it.
pub(crate) fn import_manifest(lock: &Lock, files: &GameFiles) -> Result<Value, String> {
let version = &lock.version;
let destination = lock.directory.join("manifest.json");
let source = minecraft
.join("versions")
.join(version)
.join(format!("{version}.json"));
let source = &files.manifest;
if source.is_file() {
let manifest = read_json(&source)?;
let manifest = read_json(source)?;
let downloads = manifest.get("downloads");
let sha1 = |side: &str| {
downloads
@@ -183,7 +307,7 @@ pub(crate) fn import_manifest(lock: &Lock, minecraft: &Path) -> Result<Value, St
source.display()
));
}
let checksum = digest(&source, "sha1")?;
let checksum = digest(source, "sha1")?;
if !lock.manifest_accepted(&checksum) {
// Another launcher's rewrite of Mojang's file (HMCL and PCL add their own fields to
// the same jars, #1772), or one Mojang republished: Mojang's own copy saved as the
@@ -202,13 +326,13 @@ pub(crate) fn import_manifest(lock: &Lock, minecraft: &Path) -> Result<Value, St
}
} else if !(destination.exists() && digest(&destination, "sha1")? == checksum) {
fs::create_dir_all(&lock.directory).map_err(|e| e.to_string())?;
fs::copy(&source, &destination).map_err(|e| e.to_string())?;
fs::copy(source, &destination).map_err(|e| e.to_string())?;
}
} else if !(destination.exists() && lock.manifest_accepted(&digest(&destination, "sha1")?)) {
return Err(format!(
"Missing {}. {}",
source.display(),
hint(LAUNCHER_HINT, version)
files.hint(version)
));
}
let manifest = read_json(&destination)?;
@@ -519,18 +643,15 @@ pub fn import(
server_jar: Option<&Path>,
log: Log,
) -> Result<(), String> {
let minecraft = minecraft_dir(minecraft);
let version = &lock.version;
let manifest = import_manifest(lock, &minecraft)?;
let files = game_files(minecraft, &lock.version);
let minecraft = &files.folder;
let manifest = import_manifest(lock, &files)?;
let client = import_copy(
&minecraft
.join("versions")
.join(version)
.join(format!("{version}.jar")),
&files.client,
&lock.directory.join("jars/client.jar"),
lock.pin("client_sha1")?,
"sha1",
&hint(LAUNCHER_HINT, version),
&files.hint(&lock.version),
)?;
let downloads = manifest.get("downloads");
if downloads.and_then(|d| d.get("client_mappings")).is_some()
@@ -555,7 +676,7 @@ pub fn import(
for (name, artifact) in client_library_artifacts(&manifest)? {
let (path, sha1) = artifact_fields(artifact)?;
let relative = safe_relative(path).ok_or("Unsafe library path")?;
let source = launcher_library(&minecraft, name, &relative);
let source = launcher_library(minecraft, name, &relative);
let destination = lock.directory.join("libraries/client").join(&relative);
if runs_without(name) && !source.is_file() && !destination.exists() {
log(&format!(
@@ -569,7 +690,7 @@ pub fn import(
&destination,
sha1,
"sha1",
&hint(LAUNCHER_HINT, version),
&files.hint(&lock.version),
)?);
}
write_import_metadata(
@@ -1122,7 +1243,7 @@ mod tests {
let lock = Lock::read(&root).unwrap();
let destination = lock.directory.join("manifest.json");
let error = import_manifest(&lock, &minecraft).unwrap_err();
let error = import_manifest(&lock, &game_files(Some(&minecraft), "9.9")).unwrap_err();
assert!(error.contains("may have rewritten it"), "{error}");
assert!(
error.contains(&format!("packages/{pinned}/9.9.json")),
@@ -1136,7 +1257,7 @@ mod tests {
fs::create_dir_all(&lock.directory).unwrap();
fs::copy(root.join("mojang.json"), &destination).unwrap();
let manifest = import_manifest(&lock, &minecraft).unwrap();
let manifest = import_manifest(&lock, &game_files(Some(&minecraft), "9.9")).unwrap();
assert!(manifest.get("patches").is_none());
assert_eq!(digest(&destination, "sha1").unwrap(), pinned);
fs::remove_dir_all(&root).unwrap();
@@ -1253,6 +1374,103 @@ mod tests {
assert!(prepare().unwrap_err().starts_with("Missing "));
}
/// ATLauncher keeps Mojang's version JSON in `configs/json/minecraft` and the client jar as
/// a library; its folder, or one of its instances, prepares like the official launcher's,
/// with the same pins.
#[test]
fn an_atlauncher_install_prepares() {
let fixture = Imports::new();
let old = fixture.imported(&fixture.source);
let atlauncher = fixture.directory.join("ATLauncher");
let put = |path: &str, data: &[u8]| {
let path = atlauncher.join(path);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, data).unwrap();
};
let client = fs::read(old.join("jars/client.jar")).unwrap();
put("libraries/net/minecraft/client/9.9/client-9.9.jar", &client);
put("libraries/client.jar", b"client library");
put("instances/Minecraft99/instance.json", b"{}");
// A version JSON left without its jar does not hide the launcher's complete copy.
put("instances/Minecraft99/versions/9.9/9.9.json", b"{}");
let manifest = fs::read_to_string(old.join("manifest.json")).unwrap();
let manifest = manifest.replacen('{', r#"{"javaVersion":{"majorVersion":25},"#, 1);
put("configs/json/minecraft/9.9.json", manifest.as_bytes());
let pinned = digest(&atlauncher.join("configs/json/minecraft/9.9.json"), "sha1").unwrap();
let lock_path = fixture.target.join("research-lock.json");
let text = fs::read_to_string(&lock_path).unwrap();
let old_sha1 = digest(&old.join("manifest.json"), "sha1").unwrap();
fs::write(&lock_path, text.replace(&old_sha1, &pinned)).unwrap();
let lock = Lock::read(&fixture.target).unwrap();
let server = old.join("jars/server.jar");
let prepare = |dir: &Path| {
let _ = fs::remove_dir_all(&lock.directory);
import(&fixture.target, &lock, Some(dir), Some(&server), &|_| {})
};
for chosen in [atlauncher.clone(), atlauncher.join("instances/Minecraft99")] {
prepare(&chosen).unwrap();
let artifacts = verify(&lock).unwrap();
assert_eq!(fs::read(&artifacts.jars["client"]).unwrap(), client);
assert_eq!(
client_libraries(&lock, &artifacts.manifest).unwrap().len(),
1
);
}
let files = game_files(Some(&atlauncher), "9.9");
assert_eq!(files.runtimes, atlauncher.join("runtimes/minecraft"));
// The pins gate it as they gate the official layout.
put(
"libraries/net/minecraft/client/9.9/client-9.9.jar",
b"other",
);
assert!(prepare(&atlauncher)
.unwrap_err()
.contains("is not the pinned file"));
put("libraries/net/minecraft/client/9.9/client-9.9.jar", &client);
put(
"configs/json/minecraft/9.9.json",
manifest.replace(',', ", ").as_bytes(),
);
assert!(prepare(&atlauncher)
.unwrap_err()
.contains("may have rewritten it"));
}
/// With no launcher's files in the chosen folder, the error names where Horizon looked and
/// the launchers it reads.
#[test]
fn the_error_names_the_folders_looked_in() {
let fixture = Imports::new();
let empty = fixture.directory.join("instances/empty");
fs::create_dir_all(&empty).unwrap();
let lock = Lock::read(&fixture.target).unwrap();
let error = import(&fixture.target, &lock, Some(&empty), None, &|_| {}).unwrap_err();
assert!(error.starts_with(&format!(
"Missing {}",
empty.join("versions/9.9/9.9.json").display()
)));
assert!(error.contains("Horizon looked for 9.9 in:"), "{error}");
assert!(error.contains(&format!("\n {} (the folder chosen)", empty.display())));
assert!(error.contains(&format!(
"\n {} (the launcher above it)",
fixture.directory.display()
)));
assert!(error.contains("ATLauncher"));
}
#[test]
fn every_launcher_horizon_reads_is_looked_in() {
let launchers: Vec<_> = launcher_folders()
.into_iter()
.map(|(name, _)| name)
.collect();
for name in ["the official launcher", "ATLauncher", "the CurseForge app"] {
assert!(launchers.contains(&name), "{name}");
}
}
#[test]
fn runs_without_only_netty_native_transports() {
assert!(runs_without(
+1 -1
View File
@@ -434,7 +434,7 @@ fn prepare_impl(options: &Options, log: Log, compile_only: bool) -> Result<PathB
// Java first: nothing below can finish without it, and the player should hear it at once.
let mut homes: Vec<PathBuf> = options.java_home.iter().cloned().collect();
homes.extend(java::minecraft_runtimes(
&artifacts::minecraft_dir(options.minecraft_dir.as_deref()).join("runtime"),
&artifacts::game_files(options.minecraft_dir.as_deref(), &lock.version).runtimes,
));
let jdk = java::find_runtime(lock.java_major, &homes)?;
let reused = if options.import {
+12 -8
View File
@@ -1,5 +1,5 @@
//! The pinned version's sounds, title panorama, Unifont glyphs and built-in resource packs
//! from the player's own official launcher install, each object checked against the asset index's SHA-1, copied into
//! from the player's own launcher install, each object checked against the asset index's SHA-1, copied into
//! `.local/horizon-client/<version>/assets` where the game reads them (tools/fetch_sounds.py and
//! the tools/lab.py functions it called). Nothing is downloaded.
@@ -18,11 +18,11 @@ use std::path::{Path, PathBuf};
fn launcher_assets(
lock: &Lock,
manifest: &Value,
minecraft: &Path,
files: &artifacts::GameFiles,
cached_index: &Path,
log: Log,
) -> Result<(PathBuf, PathBuf, Vec<u8>), String> {
let assets = minecraft.join("assets");
let assets = files.folder.join("assets");
let info = manifest
.get("assetIndex")
.ok_or("The version JSON has no assetIndex")?;
@@ -44,7 +44,7 @@ fn launcher_assets(
return Err(format!(
"Missing {}. {}",
index.display(),
artifacts::launcher_hint(version)
files.hint(version)
));
}
let bytes = fs::read(&index).map_err(|e| format!("{}: {e}", index.display()))?;
@@ -240,13 +240,13 @@ pub(crate) struct Assets {
impl Assets {
pub(crate) fn load(root: &Path, minecraft: Option<&Path>, log: Log) -> Result<Self, String> {
let lock = Lock::read(root)?;
let minecraft = artifacts::minecraft_dir(minecraft);
let manifest = artifacts::import_manifest(&lock, &minecraft)?;
let files = artifacts::game_files(minecraft, &lock.version);
let manifest = artifacts::import_manifest(&lock, &files)?;
let target = root.join(".local/horizon-client").join(&lock.version);
let (store, index, bytes) = launcher_assets(
&lock,
&manifest,
&minecraft,
&files,
&target.join("asset-index.json"),
log,
)?;
@@ -677,7 +677,11 @@ mod tests {
for missing in ["store", "install", "last-object"] {
let f = Fixture::new(&format!("optional-panorama-{missing}"));
// The JARs and manifest were imported earlier; the launcher need not remain.
artifacts::import_manifest(&Lock::read(&f.root).unwrap(), &f.minecraft).unwrap();
artifacts::import_manifest(
&Lock::read(&f.root).unwrap(),
&artifacts::game_files(Some(&f.minecraft), "9.9"),
)
.unwrap();
let target = f.root.join(".local/horizon-client/9.9/assets");
for name in PANORAMA {
let path = target.join(name);
+1
View File
@@ -11,6 +11,7 @@ pub mod frame;
pub mod ids;
pub mod item;
pub mod packets;
pub mod quiet;
pub mod registry;
mod resolver;
pub mod rsa;
+166
View File
@@ -0,0 +1,166 @@
//! Quiet commands: a client mod's batch of commands that a Horizon server runs
//! as the player who sent them, at that player's own permission level, with
//! no feedback in the chat (docs/mods.md, `command.run-quiet`).
//!
//! The server announces it understands them with an empty [`CHANNEL`] payload
//! in the configuration phase (a vanilla client ignores it); a client that
//! hears it sends each batch as a serverbound [`CHANNEL`] payload, and falls
//! back to chat commands on any other server. A payload is the mod's id, then
//! the lines without their slash.
use crate::{
Error, Result,
buf::{Reader, Writer},
};
/// The custom payload channel, both ways.
pub const CHANNEL: &str = "horizon:quiet_commands";
/// Lines in one payload at most; a payload with more is refused whole.
pub const MAX_LINES: usize = 256;
/// Characters in a line at most (a chat command's limit).
pub const MAX_LINE: usize = 256;
/// Characters in a mod id at most.
pub const MAX_MOD_ID: usize = 64;
/// Bytes of a serverbound custom payload's data at most
/// (ServerboundCustomPayloadPacket.MAX_PAYLOAD_SIZE).
pub const MAX_PAYLOAD: usize = 32767;
/// A batch as the server reads it.
#[derive(Clone, Debug, PartialEq)]
pub struct Batch {
pub mod_id: String,
pub lines: Vec<String>,
}
/// `lines` as few payloads as fit [`MAX_PAYLOAD`] and [`MAX_LINES`], in
/// order. Lines longer than [`MAX_LINE`] are cut, as a chat command's are.
pub fn encode(mod_id: &str, lines: &[String]) -> Vec<Vec<u8>> {
let mod_id: String = mod_id.chars().take(MAX_MOD_ID).collect();
let mut payloads = Vec::new();
let mut rest = lines
.iter()
.map(|l| -> String { l.chars().take(MAX_LINE).collect() });
let mut next = rest.next();
while next.is_some() {
let mut batch = Vec::new();
// The id, the count (a varint of at most 2 bytes for 256).
let mut size = string_size(&mod_id) + 2;
while let Some(line) = next.take() {
let more = string_size(&line);
if !batch.is_empty() && (batch.len() == MAX_LINES || size + more > MAX_PAYLOAD) {
next = Some(line);
break;
}
size += more;
batch.push(line);
next = rest.next();
}
let mut w = Writer::new();
w.string(&mod_id);
w.varint(batch.len() as i32);
for line in &batch {
w.string(line);
}
payloads.push(w.data);
}
payloads
}
fn string_size(s: &str) -> usize {
crate::buf::varint_size(s.len() as i32) + s.len()
}
/// A payload's batch; an error for one that breaks a cap.
pub fn decode(data: &[u8]) -> Result<Batch> {
if data.len() > MAX_PAYLOAD {
return Err(Error::new("quiet commands: payload too big"));
}
let mut r = Reader::new(data);
let mod_id = r.string(MAX_MOD_ID)?;
let count = r.varint()?;
if !(0..=MAX_LINES as i32).contains(&count) {
return Err(Error::new(format!("quiet commands: {count} lines")));
}
let lines = (0..count)
.map(|_| r.string(MAX_LINE))
.collect::<Result<Vec<_>>>()?;
if r.remaining() > 0 {
return Err(Error::new("quiet commands: trailing bytes"));
}
Ok(Batch { mod_id, lines })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_batch_round_trips() {
let lines = vec!["fill 0 0 0 1 1 1 stone".to_string(), "say hi".to_string()];
let payloads = encode("orbital", &lines);
assert_eq!(payloads.len(), 1);
assert_eq!(
decode(&payloads[0]).unwrap(),
Batch {
mod_id: "orbital".into(),
lines
}
);
}
#[test]
fn big_batches_split_by_lines_and_bytes() {
let lines: Vec<String> = (0..600)
.map(|i| format!("setblock {i} 0 0 stone"))
.collect();
let payloads = encode("m", &lines);
let decoded: Vec<Batch> = payloads.iter().map(|p| decode(p).unwrap()).collect();
assert_eq!(
decoded.iter().map(|b| b.lines.len()).collect::<Vec<_>>(),
[256, 256, 88]
);
assert_eq!(
decoded
.into_iter()
.flat_map(|b| b.lines)
.collect::<Vec<_>>(),
lines
);
let long: Vec<String> = (0..256).map(|_| "x".repeat(300)).collect();
for p in encode("m", &long) {
assert!(p.len() <= MAX_PAYLOAD);
assert!(
decode(&p)
.unwrap()
.lines
.iter()
.all(|l| l.len() == MAX_LINE)
);
}
}
#[test]
fn payloads_over_the_caps_are_refused() {
let mut w = Writer::new();
w.string("m");
w.varint(MAX_LINES as i32 + 1);
for _ in 0..=MAX_LINES {
w.string("say");
}
assert!(decode(&w.data).is_err());
let mut w = Writer::new();
w.string("m");
w.varint(1);
w.string(&"x".repeat(MAX_LINE + 1));
assert!(decode(&w.data).is_err());
let mut w = Writer::new();
w.string("m");
w.varint(-1);
assert!(decode(&w.data).is_err());
assert!(decode(&[0xFF]).is_err());
let mut w = Writer::new();
w.string("m");
w.varint(0);
w.u8(1);
assert!(decode(&w.data).is_err());
}
}
@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" viewBox="0 0 24 24">
<path d="M10 1h4v2h-4ZM10 3h4v2h-4ZM8 5h8v2h-8ZM0 7h24v2h-24ZM2 9h20v2h-20ZM4 11h16v2h-16ZM6 13h12v2h-12ZM6 15h12v2h-12ZM4 17h6v2h-6ZM14 17h6v2h-6ZM4 19h4v2h-4ZM16 19h4v2h-4ZM2 21h4v2h-4ZM18 21h4v2h-4Z"/>
</svg>

After

Width:  |  Height:  |  Size: 318 B

+2
View File
@@ -67,6 +67,8 @@ pub mod icons {
"search",
"server",
"sliders",
// Not Pixelarticons': drawn for Horizon's ratings, in the same 2-unit grid.
"star",
"terminal",
"trash",
"user",
+4 -2
View File
@@ -16,7 +16,7 @@ use serde_json::Value;
use std::{
collections::HashMap,
fs::{self, File},
io::{self, BufReader},
io,
path::Path,
sync::Arc,
};
@@ -582,8 +582,10 @@ fn fail(message: &str) -> io::Error {
invalid(message)
}
/// From the file's bytes in memory: several times faster than through a
/// reader on gameplay-facts.json (55 MB), which a first start parses.
fn read_json(path: &Path) -> io::Result<Value> {
serde_json::from_reader(BufReader::new(File::open(path)?)).map_err(|e| invalid(e.to_string()))
serde_json::from_slice(&fs::read(path)?).map_err(|e| invalid(e.to_string()))
}
fn interned(cache: &mut HashMap<String, Arc<str>>, text: &str) -> Arc<str> {
+116
View File
@@ -122,3 +122,119 @@ pub fn air_drag<W: LivingMotionWorld>(world: &mut W) -> Result<f32, W::Error> {
};
Ok(modified_friction(drag, world.air_drag_modifier()? as f32))
}
#[cfg(test)]
mod tests {
use super::*;
use horizon_core::geometry::{Vec3, trig};
struct Jumper {
velocity: Vec3,
sprinting: bool,
yaw: f32,
synced: bool,
}
impl LivingMotionWorld for Jumper {
type Error = ();
fn velocity(&self) -> Vec3 {
self.velocity
}
fn set_velocity(&mut self, velocity: Vec3) -> Result<(), ()> {
self.velocity = velocity;
Ok(())
}
fn jump_strength(&mut self) -> Result<f64, ()> {
Ok(0.42)
}
fn block_jump_factor(&mut self) -> Result<f32, ()> {
Ok(1.0)
}
fn has_jump_boost(&mut self) -> Result<bool, ()> {
Ok(false)
}
fn jump_boost_amplifier(&mut self) -> Result<i32, ()> {
Ok(0)
}
fn sprinting(&mut self) -> Result<bool, ()> {
Ok(self.sprinting)
}
fn yaw(&mut self) -> Result<f32, ()> {
Ok(self.yaw)
}
fn mark_needs_sync(&mut self) {
self.synced = true;
}
fn no_gravity(&mut self) -> Result<bool, ()> {
Ok(false)
}
fn gravity_attribute(&mut self) -> Result<f64, ()> {
Ok(0.08)
}
fn has_slow_falling(&mut self) -> Result<bool, ()> {
Ok(false)
}
fn controlling_passenger_is_player(&mut self) -> Result<bool, ()> {
Ok(false)
}
fn speed(&mut self) -> Result<f32, ()> {
Ok(0.1)
}
fn omnidirectional_air_mover(&mut self) -> Result<bool, ()> {
Ok(false)
}
fn air_drag_modifier(&mut self) -> Result<f64, ()> {
Ok(0.0)
}
}
fn impulse(yaw: f32) -> (f64, f64) {
let angle = f64::from(yaw * trig::DEG_TO_RAD);
(
f64::from(-trig::sin(angle)) * 0.2,
f64::from(trig::cos(angle)) * 0.2,
)
}
/// LivingEntity.jumpFromGround: a sprint adds ~0.2 along look; a walk does not.
#[test]
fn sprint_jump_adds_look_direction_impulse() {
for yaw in [0.0, 45.0, 90.0, 180.0, 270.0, 300.0] {
let before = Vec3::new(0.13, -0.08, -0.05);
let mut sprinting = Jumper {
velocity: before,
sprinting: true,
yaw,
synced: false,
};
jump_from_ground(&mut sprinting).unwrap();
let (dx, dz) = impulse(yaw);
let horizontal = ((dx * dx) + (dz * dz)).sqrt();
assert!(
(horizontal - 0.2).abs() < 1.0e-4,
"yaw {yaw}: impulse length {horizontal}"
);
assert!(
(sprinting.velocity.x - (before.x + dx)).abs() < 1.0e-9,
"yaw {yaw}: x {:?} expected {}",
sprinting.velocity,
before.x + dx
);
assert!((sprinting.velocity.z - (before.z + dz)).abs() < 1.0e-9);
// jump_power narrows the 0.42 strength through float, as vanilla does.
let jump_y = f64::from(0.42_f32);
assert!((sprinting.velocity.y - jump_y).abs() < 1.0e-9);
assert!(sprinting.synced);
let mut walking = Jumper {
velocity: before,
sprinting: false,
yaw,
synced: false,
};
jump_from_ground(&mut walking).unwrap();
assert_eq!(walking.velocity.x, before.x);
assert_eq!(walking.velocity.z, before.z);
assert!((walking.velocity.y - jump_y).abs() < 1.0e-9);
}
}
}
+68 -44
View File
@@ -269,6 +269,20 @@ fn quantize_parameter_report(json: &str) -> Result<String, GeneratorError> {
}
Ok(Value::Array(entries).to_string())
}
/// `load` on a thread of its own ("Worldgen loader").
fn loading<T: Send + 'static>(
load: impl FnOnce() -> T + Send + 'static,
) -> io::Result<std::thread::JoinHandle<T>> {
std::thread::Builder::new()
.name("Worldgen loader".into())
.spawn(load)
}
/// A loading thread's result; its panic goes on in this thread.
fn joined<T>(thread: std::thread::JoinHandle<T>) -> T {
thread
.join()
.unwrap_or_else(|panic| std::panic::resume_unwind(panic))
}
impl GeneratorData {
/// Load a worldgen bundle directory. Required files are JSON objects mapping
/// resource identifiers to the resource's JSON text: `noises`, `densities`,
@@ -290,6 +304,50 @@ impl GeneratorData {
let block_tags = build_tags(&read_bundle(dir, "block_tags")?, |id| {
registry.default_state(id).is_some()
})?;
let fluid_tags = match read_optional_bundle(dir, "fluid_tags")? {
Some(raw) => build_tags(&raw, |id| {
matches!(
id,
"minecraft:water"
| "minecraft:flowing_water"
| "minecraft:lava"
| "minecraft:flowing_lava"
| "minecraft:empty"
)
})?,
None => BTreeMap::new(),
};
// The three biggest reads (the block tables, the structure templates
// and the block behaviours) need only the tags: they run on threads of
// their own while this one reads the rest. A new world's first chunks
// wait for all of it, with the chunk workers idle.
let blocks = loading({
let (dir, tags, fluids) = (dir.join("blocks"), block_tags.clone(), fluid_tags.clone());
move || super::generator_blocks::BlockBehavior::load(&dir, registry, &tags, &fluids)
})?;
let structures = loading({
let (dir, tags) = (dir.to_owned(), block_tags.clone());
move || -> Result<_, GeneratorError> {
let Ok(json) = fs::read_to_string(dir.join("blocks/structure-blocks.json")) else {
return Ok(None);
};
Ok(Some(Arc::new(super::structure::StructureResources::new(
registry,
super::structure::StateTransforms::from_json(&json, registry.len())?,
&dir.join("structure"),
read_optional_bundle(&dir, "processor_lists")?.unwrap_or_default(),
&tags,
))))
}
})?;
let behaviours = loading({
let root = dir.parent().map(Path::to_owned);
move || {
root.filter(|root| root.join("block-behaviours.json").exists())
.and_then(|root| crate::level::Behaviours::load(registry, &root).ok())
.map(Arc::new)
}
})?;
let tag_set = |name: &str| -> Result<BTreeSet<&str>, GeneratorError> {
Ok(block_tags
.get(name)
@@ -382,47 +440,17 @@ impl GeneratorData {
read_optional_bundle(dir, "features")?,
read_optional_bundle(dir, "placed_features")?,
) {
(Some(features), Some(placed)) => {
let fluid_tags = match read_optional_bundle(dir, "fluid_tags")? {
Some(raw) => build_tags(&raw, |id| {
matches!(
id,
"minecraft:water"
| "minecraft:flowing_water"
| "minecraft:lava"
| "minecraft:flowing_lava"
| "minecraft:empty"
)
})?,
None => BTreeMap::new(),
};
Some(FeatureResources {
features,
placed,
feature_tags: read_optional_bundle(dir, "feature_tags")?.unwrap_or_default(),
blocks: super::generator_blocks::BlockBehavior::load(
&dir.join("blocks"),
registry,
&block_tags,
&fluid_tags,
)?,
fluid_tags,
state_providers: read_optional_bundle(dir, "state_providers")?
.unwrap_or_default(),
})
}
(Some(features), Some(placed)) => Some(FeatureResources {
features,
placed,
feature_tags: read_optional_bundle(dir, "feature_tags")?.unwrap_or_default(),
blocks: joined(blocks)?,
fluid_tags,
state_providers: read_optional_bundle(dir, "state_providers")?.unwrap_or_default(),
}),
_ => None,
};
let structures = match fs::read_to_string(dir.join("blocks/structure-blocks.json")) {
Ok(json) => Some(Arc::new(super::structure::StructureResources::new(
registry,
super::structure::StateTransforms::from_json(&json, registry.len())?,
&dir.join("structure"),
read_optional_bundle(dir, "processor_lists")?.unwrap_or_default(),
&block_tags,
))),
Err(_) => None,
};
let structures = joined(structures)?;
let structure_resources = match (
read_optional_bundle(dir, "structures")?,
read_optional_bundle(dir, "structure_sets")?,
@@ -435,11 +463,7 @@ impl GeneratorData {
}),
_ => None,
};
let behaviours = dir
.parent()
.filter(|root| root.join("block-behaviours.json").exists())
.and_then(|root| crate::level::Behaviours::load(registry, root).ok())
.map(Arc::new);
let behaviours = joined(behaviours);
Ok(Arc::new(Self {
behaviours,
structure_resources,
@@ -108,15 +108,26 @@ impl BlockBehavior {
let json = |name: &str| -> Result<Value, GeneratorError> {
Ok(serde_json::from_str(&fs::read_to_string(dir.join(name))?)?)
};
let collisions =
CachedCollisionRegistry::from_table(read("cached-collision.nbt")?.table("collision")?)?;
let properties = BlockPropertiesRegistry::from_table(
read("block-properties.nbt")?.table("block property")?,
&collisions,
)?;
let support = Arc::new(BlockSupportRegistry::from_table(
read("cached-support.nbt")?.table("support")?,
)?);
// The support table reads on its own thread while the collision and
// property tables (which need the collisions) read on this one.
let (collisions, properties, support) = std::thread::scope(|scope| {
let support = scope.spawn(|| -> Result<_, GeneratorError> {
Ok(BlockSupportRegistry::from_table(
read("cached-support.nbt")?.table("support")?,
)?)
});
let collisions = CachedCollisionRegistry::from_table(
read("cached-collision.nbt")?.table("collision")?,
)?;
let properties = BlockPropertiesRegistry::from_table(
read("block-properties.nbt")?.table("block property")?,
&collisions,
)?;
let support = support
.join()
.unwrap_or_else(|panic| std::panic::resume_unwind(panic))?;
Ok::<_, GeneratorError>((collisions, properties, Arc::new(support)))
})?;
let collision = Arc::new(BlockCollisionRegistry::new(Arc::new(collisions))?);
let rules = json("survival-rules.json")?
.as_object()
+3 -1
View File
@@ -18,7 +18,7 @@ Level: preview ([stability.md](stability.md)). Schema 1.
"projects": [{
"id": "coords_hud", "kind": "mod", "name": "Coordinates HUD", "summary": "One line.",
"description": "Markdown.", "authors": ["Horizon"], "owners": ["ludovic111"],
"license": "MIT", "source": "https://github.com/...", "icon": "https://...png", "key": "RWT...",
"license": "MIT", "source": "https://github.com/...", "icon": "https://...png", "key": "RWT...", "downloads": 1234,
"versions": [{
"version": "0.1.0", "published": "2026-10-24", "changelog": "",
"api": "0.1", "side": "client", "capabilities": ["game-read", "hud", "chat"], "api_level": 0, "game": "26.3",
@@ -41,6 +41,7 @@ Level: preview ([stability.md](stability.md)). Schema 1.
| `icon` | `https://`, optional. |
| `key` | The author's [minisign](https://jedisct1.github.io/minisign/) public key. When set, every version must be signed with it; an update signed with another key is refused. Absent: the project is unsigned and the launcher says so. |
| `versions` | Newest first, at least one. |
| `downloads` | How many times its files were downloaded, every version together: GitHub's count of each release asset, summed by the publish job, which runs every hour. Absent until a file is downloaded. Read-only: never in an entry. |
| `api` | A mod's API line (its manifest's `api`); a modpack's manifest format (`1`). |
| `side`, `capabilities` | A mod's, read from its manifest by CI, never typed by hand. |
| `api_level` | The mod API level a mod's code needs ([stability.md](stability.md#mod-api-levels)): its newest imported function's, read from the file by CI. Absent for content-only mods and in indexes built before levels. Installers take the newest version whose level the running game serves. |
@@ -167,6 +168,7 @@ opens.
## Changelog
- 2026-10-26: `projects[].downloads`, GitHub's download counts of a project's files, refreshed every hour.
- 2026-10-26: the launcher installs `shader` files into `shaderpacks/`; the sample index lists one (`glow`).
- 2026-10-25: `shader` opens, checked by `horizon_shader_api::check`; its files are `.hshader`.
- Schema 1 (2026-10-24): kinds `mod`, `modpack`, `resourcepack`, `shader`; `.hpack` format 1.
+16 -1
View File
@@ -76,7 +76,7 @@ The mod's log, in the game's log under the mod's id. Always granted.
Access: capability `game-read`.
Capability `game-read`: the local player and the world clock.
Capability `game-read`: the local player, the world clock and the block the player looks at.
### record `player`
@@ -98,10 +98,25 @@ Capability `game-read`: the local player and the world clock.
| `game-time` | `u64` | Ticks since the world was created. |
| `day-time` | `u64` | 24000 ticks per day; 0 is 6:00 on the first day. |
### record `hit`
A block along the player's view.
| Field | Type | |
| --- | --- | --- |
| `block-x` | `s32` | The block hit. |
| `block-y` | `s32` | |
| `block-z` | `s32` | |
| `x` | `f64` | The point on its face where the view meets it. |
| `y` | `f64` | |
| `z` | `f64` | |
| `block` | `string` | `minecraft:stone`, ... |
| Function | Signature | What it does |
| --- | --- | --- |
| `get-player` | `func() -> option<player>` | None on the title screen. |
| `get-clock` | `func() -> option<clock>` | None on the title screen. |
| `raycast` | `func(distance: f32) -> option<hit>` | The first block along the player's view (fluids pass), up to `distance` blocks (at most 1024; past the loaded chunks, across the distant terrain when LOD Distance is on), as of this tick. None when nothing is hit, and on the title screen. |
## `chat`
+7 -5
View File
@@ -1,7 +1,7 @@
# Mod API
Mods are Rust crates compiled to sandboxed WebAssembly components. They run in the player's game and, once server
mods land, wherever a world is simulated. Design and the runtime: [mods.md](../mods.md).
Mods are Rust crates compiled to sandboxed WebAssembly components. Client mods run in the player's game; server mods
(experimental) run wherever a world is simulated: single player, a LAN host, the dedicated server. Design and the runtime: [mods.md](../mods.md).
## Overview
@@ -87,9 +87,10 @@ release.
What real mods need next, in the order modpacks and servers need it:
- **Server mods in the game** (`api = "0.3"`, [Server mods](../mods.md#server-mods-api-03-experimental)): the
contract, runtime and SDK are here; next the server thread, LAN host and dedicated server start them and call
their hooks, then entity events, game rules and server commands with typed arguments.
- **More for server mods** (`api = "0.3"`, [Server mods](../mods.md#server-mods-api-03-experimental)): they run
on the server thread, a LAN host and the dedicated server today, with joins, chat, commands and players' block
breaking and placing; next entity events, game rules, server commands with typed arguments sent to clients'
command trees.
- **World and blocks**: read blocks, block states and biomes in loaded chunks; change them through the server's block
log, so clients and the parity harnesses see vanilla's updates.
- **Items and inventories**: the player's inventory, held item and open container; item stacks with components.
@@ -105,6 +106,7 @@ What real mods need next, in the order modpacks and servers need it:
## Changelog
- Server mods run in the game: single player (server thread and in-frame), LAN hosts, the dedicated server.
- API 0.3 (server mods, experimental): world `horizon-server-mod`, capability `players`, the `server-events`
hooks; SDK `server::ServerMod`; example `welcome`.
- SDK: `cargo horizon-mod test` and `horizon_mods::testing`, test scripts in TOML; the template has one.
+3 -2
View File
@@ -91,11 +91,12 @@ pack exist ([stability.md](stability.md)).
## Not there yet
The material stage in the game (the sky and post stages run, with hot reload), option values other than the defaults,
then the overlay, lighting and shadow stages.
Option values other than the defaults (the three stages run, with hot reload), then the overlay, lighting and shadow
stages.
## Changelog
- 2026-10-26: the material stage in the game: terrain's surfaces through the pack's `material`.
- 2026-10-26: the sky stage in the game: the pack's `sky` gets vanilla's colour along each ray.
- 2026-10-26: the post stage in the game (`HORIZON_SHADER_PACK`, hot reload with the last good pack kept).
- 2026-10-25: `horizon-shader` (`new`, `check`, `pack`), three templates, the example pack and the guide.
+1 -1
View File
@@ -38,7 +38,7 @@ don't import it); faster, or fixing behaviour to what the reference already says
A new WIT function doesn't break older mods, but a mod that calls it can't run on a Horizon from before it. So every
function the game offers mods has an **API level**, the one it arrived at (`crates/horizon-mods/src/api_level.rs`):
level 0 is the contract of Horizon 0.2.67 to 0.2.69, level 1 adds `render.set-minimap` and server mods' `players`. A mod needs the level of
level 0 is the contract of Horizon 0.2.67 to 0.2.69, level 1 adds `render.set-minimap` and server mods' `players`, level 2 `game.raycast`. A mod needs the level of
the newest function it imports (a component imports only the functions it calls). The level is read from the file,
never declared:
+43 -9
View File
@@ -83,7 +83,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3.
| Interface | Capability | Functions |
|---|---|---|
| `log` | always | `write(level, message)`, in the game log as `[mod <id>]` |
| `game` | `game-read` | `get-player()`: x y z, yaw, pitch, health, food, dimension; `get-clock()`: game time, day time |
| `game` | `game-read` | `get-player()`: x y z, yaw, pitch, health, food, dimension; `get-clock()`: game time, day time; `raycast(distance)`: the first block along the view (fluids pass), up to 1024 blocks and across the distant terrain, with the point hit and the block's id |
| `chat` | `chat` | `show(text)`: a line only the local player sees, after the mod's name (8 per call) |
| `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters |
| `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call |
@@ -491,10 +491,40 @@ its component targets the WIT world `horizon-server-mod` instead of
Capabilities that act on a local player (`game-read`, `chat`, `hud`) are for
client mods; `players` is for server mods. In Rust: `impl
horizon_mod_sdk::server::ServerMod` and `export_server_mod!`; the example is
[`welcome`](../crates/horizon-mod-sdk/examples/welcome/src/lib.rs). The
runtime, the registry's `check` and the SDK run them today; the game starts
them once its server code calls these hooks (the next step), and until then a
server mod that is turned on shows as run elsewhere.
[`welcome`](../crates/horizon-mod-sdk/examples/welcome/src/lib.rs).
**Where they run.** The game that simulates the world starts the server mods
that are on, and only that one:
| Mode | Runs server mods | Players they see |
|---|---|---|
| Single player (default) | the server thread | the window's player, a connection like any other |
| Single player `--in-frame` | the window's own game, beside its client mods | the window's player |
| Open to LAN, Play with Friends | the host's server thread (or its window, in-frame) | the host and every guest |
| Dedicated server (`--dedicated`, `horizon-server`) | the server | every connected player |
A window that is a client of a server (single player's window, a LAN guest,
any multiplayer server) never runs them; its Mods page lists a server mod with
the others, and its switch takes effect the next time a world opens. The mods
live in `horizon-mods/` of the game folder, or of the server's folder on a
dedicated server, and are turned on as client mods are: Options > Mods, or
`mod.<id>:true` in `horizon-mods.txt`. `--dev-mod <file>` runs one always
on and reloads it when its file changes, on a server too.
**When hooks run.** At the end of each server tick the game tells the server
mods who joined and who left since the last one, then what players said,
broke and placed, in order, then calls `tick`. A command a player types
reaches its mod at once, whatever the player's permission level, and its
answer goes to that player. `tell` reaches one player, the window's own
chat for the in-frame player and a system message for a connection;
`broadcast` reaches every player and the server's log. With no server mod
on, each of these points costs one branch (measured in the PR that added
them).
**Not yet.** Clients aren't sent server mods' commands with vanilla's
command tree, so a player's chat box doesn't complete or colour them; typing
them works. Block events are a player's own breaking and placing, not
explosions, pistons or other mods.
## Publish your mod
@@ -515,6 +545,10 @@ published. Players find them at [playhorizon.dev/mods](https://playhorizon.dev/m
5. A reviewer merges it: within minutes the file is a release of horizon-mods, and the project is on the site and in
the launcher.
Once it is published, its page and its card in the launcher show its downloads (GitHub's counts of its files,
refreshed every hour) and its rating: players signed in with GitHub on its page give it 1 to 5 stars, one rating per
account, which they can change. Its owners can't rate it, and nobody sees who rated.
An update is the same form with a new version; only the project's `owners` can publish one, signed with its key if it
has one. A published file never changes. Without the form, open the pull request by hand (the repository's README
says how). The formats: [api/mod-index.md](api/mod-index.md).
@@ -561,10 +595,10 @@ so the same rustc and `Cargo.lock` give the same file.
in single player and on any server, vanilla or Horizon. They see only what
the client already knows (the snapshot), like F3 does, and act only on the
local screen and chat. A server cannot see them.
- **Server mods** (`side = "server"`) will run where the world is simulated:
Horizon's integrated server in single player and LAN, and the dedicated
server. Vanilla clients can join if the mod only uses vanilla content
(rules, commands, events).
- **Server mods** (`side = "server"`, API 0.3) run where the world is
simulated: single player's server thread (or the window, in-frame), a LAN
host and the dedicated server ([Server mods](#server-mods-api-03-experimental)).
They use only vanilla messages, so any client, vanilla or Horizon, can join.
- **Both** (`side = "both"`): a server mod with a client half (new blocks,
their textures, a custom screen). On join the Horizon server announces its
mods (id, version, SHA-256) on a `horizon:mods` custom payload channel; a
+11 -2
View File
@@ -28,12 +28,12 @@ The job is `.github/workflows/parity.yml`, a reusable workflow: `checks` calls i
repository, `run-parity.yml` for a fork's pull request a maintainer labels `run-parity`.
Horizon never downloads the game, so harnesses that compare with vanilla run only where the maintainer's own
copy is: the Mac mini and his Linux desktop. Their runners run as the maintainer's account and keep their checkout
copy is: the Mac mini, his Linux desktop and playhorizon (below). Their runners run as the maintainer's account and keep their checkout
between runs (`clean: false`), so the probes build incrementally into `.local/parity-target` and earlier passes
stand.
1. `tools/ci/runner-env.sh` puts the machine's Python 3.13 and the JDK `research-lock.json` pins on the PATH
(Homebrew and `java_home` on the Mac; on NixOS what `linux-runner.sh` pinned, else nixpkgs), and sccache when
(Homebrew and `java_home` on the Mac; on Linux what `linux-runner.sh` pinned, else nixpkgs), and sccache when
the machine has it.
2. `tools/ci/vanilla-data.sh` links `.local/` to the runner's copy of the vanilla data, cloned once from
`~/code/horizon-parity/shared-local` into `~/.actions-runners/horizon/_work/horizon/shared-local`, and
@@ -94,6 +94,15 @@ same. The nightly audit and parity-main stay on the Mac mini's runners: the audi
so it stays on one platform, and a parity-main job slowed down on the desktop because he came back could time out,
and its failures bisect and comment on merges.
**playhorizon's runner.** `playhorizon-horizon` runs on a server a hosting partner lends the project (Ubuntu, 8
AMD EPYC threads, 30 GB, in Detroit; reachable only through the maintainer's tailnet). Nobody sits at it, so the
watcher never pauses it except by hand, and it gets the machine: `RUNNER_CPUS=7 RUNNER_MEMORY=24 RUNNER_JOBS=6`.
Without Nix, `linux-runner.sh` takes the build libraries, the JDK, sccache, Xvfb and Mesa's software Vulkan
(lavapipe; there is no GPU) from apt, and Python 3.13 from uv. The partner can reach the machine, so gh is not
logged in there: the registration token comes from another machine (`RUNNER_TOKEN`), and no job with secrets may
run there (parity has none). It is also a parity host (`playhorizon` in `tools/parity_hosts.json`, 6 jobs). Like
the desktop's, it gets the `horizon-game` label only once a full trial has passed there.
## Merging
Pull requests merge with a merge commit. The orchestrator sessions review a pull request, then turn on
+7 -7
View File
@@ -13,9 +13,8 @@ Horizon's own format.
| Authoring tool | `horizon-shader` (same crate) | `new`, `check`, `pack` |
| Templates | `crates/horizon-shader-api/templates/` | post, sky, material |
| Example pack | `crates/horizon-shader-api/packs/golden_hour` | a warm sky and two post passes |
| In the game: the sky and post stages | Options > Horizon Settings > Shader Packs | sky, post passes, hot reload |
| In the game: every stage | Options > Horizon Settings > Shader Packs | sky, material, post passes, hot reload |
| The registry and the launcher | `shader` kind ([api/mod-index.md](api/mod-index.md)); the Mods page's Shader packs tab | checked, then installed into `shaderpacks/` |
| In the game: the material stage | | not yet |
## Quick start
@@ -53,8 +52,7 @@ it on Options > Horizon Settings > Shader Packs, which lists Horizon's packs, ma
name>` runs a pack whatever the page says, for development. The game checks the pack first, and refuses one that
fails, with its problems in the chat. While it runs, the game looks at the pack twice a second: save a file
and the pack is checked and rebuilt in place ("Shader pack ... reloaded"); if the new version fails, the chat says
where and the last good version keeps drawing. The sky and post stages run today; the material stage is checked but
not drawn yet.
where and the last good version keeps drawing. All three stages run.
## A pack
@@ -90,7 +88,7 @@ by Horizon: a pack changes how things look, not how they are drawn.
| Stage | Your function | What it does |
| --- | --- | --- |
| `[sky]` | `fn sky(input: SkyInput) -> vec4<f32>` | the sky's colour along each view ray, opaque (alpha is ignored), drawn where vanilla draws its sky dome (the Overworld's sky; not the Nether's or the End's); the sun, moon, stars and clouds are drawn over it |
| `[material]` | `fn material(input: MaterialInput) -> Surface` | the lit colour of each point of a block's surface, before the alpha test and fog |
| `[material]` | `fn material(input: MaterialInput) -> Surface` | the lit colour of each point of a block's surface, before the alpha test and fog: every terrain section and distant tile, solid, cutout and translucent (not entities, block entities or particles) |
| `[[post]]` | `fn post(input: PostInput) -> vec4<f32>` | a full-screen pass over the world's picture (up to 8, in order), under the hand and HUD |
A pack has at least one stage; a stage it leaves out keeps vanilla's look. Each input says what vanilla would draw
@@ -133,8 +131,10 @@ one keeps drawing), the launcher before it installs one, and the registry's CI b
`[budget] gpu_ms` is the GPU time your pack aims to add to a frame. To compare, open F3 in the game: while it is
open, each pass is timed on the GPU and the overlay shows the pack's total and its costliest passes, by their `name`
(the median of the last 15 timed frames). Timing waits for nothing, so it does not slow the game. In a timed frame the sky
stage draws in a pass of its own (it shares the world's pass otherwise), so its time includes clearing the picture.
stage draws in a pass of its own (it shares the world's pass otherwise), so its time includes clearing the picture. The
material stage runs inside terrain's own draws, so it has no time of its own: compare the frame's GPU time with and
without the pack.
## Next
The material stage in the game.
The overlay, lighting and shadow stages, and option values other than the defaults.
+104 -4
View File
@@ -8,6 +8,7 @@ use super::{
download, installed, java, FileRef, Kind, Project, Query, Source, SourceId, Version, GAME,
};
use horizon_mods::index::{self, Index, Trust};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
@@ -27,6 +28,11 @@ const FRESH: Duration = Duration::from_secs(600);
const MAX_INDEX: u64 = 16 << 20;
/// The last index fetched, kept in the game folder for when the registry can't be reached.
const CACHE: &str = "mod-browser/horizon-index.json";
/// Players' ratings of every project (the registry service, crates/horizon-mod-registry).
const RATINGS_URL: &str = "https://playhorizon.dev/api/mods/ratings";
/// Where a player rates a project: the site's GitHub sign-in, which comes back to its page.
/// The launcher has no GitHub account of its own.
const RATE: &str = "https://playhorizon.dev/api/mods/auth/login?back=/mods/";
/// Where the index comes from: `HORIZON_MOD_INDEX` (a file or an `https://` address, for a
/// test registry), else [`INDEX_URL`].
@@ -110,6 +116,92 @@ fn fetch_index() -> Result<(Index, String), String> {
Ok((index, text))
}
/// A project's ratings: 1 to 5 stars, their mean and how many.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Rating {
pub average: f32,
pub count: u64,
}
/// When the ratings were fetched last, and every rated project's [`Rating`] (None: that failed).
type Ratings = Option<Arc<HashMap<String, Rating>>>;
static RATINGS: Mutex<Option<(Instant, Ratings)>> = Mutex::new(None);
/// A project's rating as fetched last, without asking the network: None when the ratings
/// aren't known (not fetched, or the service didn't answer), `Some(None)` when it has none.
pub fn rating(id: &str) -> Option<Option<Rating>> {
let ratings = RATINGS.lock().unwrap();
let known = ratings.as_ref()?.1.as_ref()?;
Some(known.get(id).copied())
}
/// The page that signs the player in with GitHub on the site and lets them rate `id`.
pub fn rate_url(id: &str) -> String {
format!("{RATE}{id}")
}
/// Fetches the ratings again when the last try is ten minutes old: from `HORIZON_MOD_RATINGS`
/// (a file or an `https://` address, for a test registry), else [`RATINGS_URL`]; a local test
/// registry has none unless given. After a failure the browser shows downloads only.
fn refresh_ratings() {
if RATINGS
.lock()
.unwrap()
.as_ref()
.is_some_and(|(at, _)| at.elapsed() < FRESH)
{
return;
}
let given = std::env::var("HORIZON_MOD_RATINGS")
.ok()
.filter(|v| !v.is_empty());
if given.is_none() && local() {
return;
}
let at = given.unwrap_or_else(|| RATINGS_URL.to_owned());
let text = if at.starts_with("https://") {
download::agent().get(&at).call().ok().and_then(|r| {
use std::io::Read;
let mut text = String::new();
r.into_reader()
.take(MAX_INDEX)
.read_to_string(&mut text)
.ok()
.map(|_| text)
})
} else {
fs::read_to_string(&at).ok()
};
let fetched = text.map(|t| Arc::new(parse_ratings(&t)));
*RATINGS.lock().unwrap() = Some((Instant::now(), fetched));
}
/// `{"projects": {"<id>": {"average", "count"}}}`, keeping only well-formed entries.
fn parse_ratings(text: &str) -> HashMap<String, Rating> {
let Ok(json) = serde_json::from_str::<serde_json::Value>(text) else {
return HashMap::new();
};
let Some(projects) = json["projects"].as_object() else {
return HashMap::new();
};
projects
.iter()
.filter_map(|(id, r)| {
let average = r["average"].as_f64().filter(|a| (1.0..=5.0).contains(a))?;
let count = r["count"].as_u64().filter(|&c| c > 0)?;
index::valid_id(id).then(|| {
(
id.clone(),
Rating {
average: average as f32,
count,
},
)
})
})
.collect()
}
/// The browser's kind for a registry kind; None for one the launcher doesn't install yet
/// (kinds newer than this launcher).
fn kind_of(kind: &index::Kind) -> Option<Kind> {
@@ -158,7 +250,7 @@ fn project_of(p: &index::Project) -> Option<Project> {
title: p.name.clone(),
summary: p.summary.clone(),
author: p.authors.join(", "),
downloads: 0,
downloads: p.downloads,
page: format!("{SITE}/{}", p.id),
icon: p.icon.clone(),
license: p.license.clone(),
@@ -218,14 +310,19 @@ fn matches(p: &index::Project, text: &str) -> bool {
.all(|word| hay.contains(&word.to_lowercase()))
}
/// The projects of the query's kind whose words match, for this game unless `everything`.
/// The projects of the query's kind whose words match, for this game unless `everything`: the
/// most downloaded first.
fn search(index: &Index, query: &Query) -> Vec<Project> {
index
let mut found: Vec<Project> = index
.projects
.iter()
.filter(|p| kind_of(&p.kind) == Some(query.kind) && matches(p, &query.text))
.filter(|p| query.everything || p.versions.iter().any(|v| v.game == GAME))
.filter_map(project_of)
.collect();
found.sort_by_key(|p| std::cmp::Reverse(p.downloads));
found
.into_iter()
.skip(query.offset)
.take(query.limit)
.collect()
@@ -240,7 +337,9 @@ impl Source for Registry {
}
fn search(&self, query: &Query) -> Result<Vec<Project>, String> {
Ok(search(&*index()?, query))
let found = search(&*index()?, query);
refresh_ratings();
Ok(found)
}
fn project(&self, id: &str) -> Result<Project, String> {
@@ -270,6 +369,7 @@ impl Source for Registry {
}
fn details(&self, project: &Project) -> Result<Details, String> {
refresh_ratings();
let index = index()?;
let p = index
.project(&project.id)
+40
View File
@@ -49,6 +49,18 @@ fn searches_by_kind_and_words() {
assert_eq!(found("", Kind::Modpack), ["cozy"]);
assert!(found("skyblock", Kind::ResourcePack).is_empty());
assert_eq!(found("", Kind::Shader), ["glow"]);
// The index's download counts, most downloaded first.
let mut counted = index.clone();
counted.projects.push(index::Project {
id: "zoom".into(),
name: "Zoom".into(),
downloads: 12_345,
..index.project("coords_hud").unwrap().clone()
});
counted.projects[0].downloads = 80;
let first = search(&counted, &Query::new("", Kind::Mod));
let order: Vec<(&str, u64)> = first.iter().map(|p| (p.id.as_str(), p.downloads)).collect();
assert_eq!(order, [("zoom", 12_345), ("coords_hud", 80)]);
let p = hud(&index);
assert_eq!(p.support(), super::super::Support::Works);
assert_eq!(p.page, "https://playhorizon.dev/mods/coords_hud");
@@ -216,3 +228,31 @@ fn versions_for_a_newer_horizon_are_not_installed() {
assert_eq!(v.blocked.as_deref(), Some(NEEDS_NEWER));
assert_eq!(version_of(&index, p, &p.versions[0], Some(1)).blocked, None);
}
#[test]
fn ratings_keep_only_what_is_well_formed() {
let text = r#"{"projects": {
"zoom": {"average": 4.25, "count": 12},
"minimap": {"average": 9, "count": 3},
"keystrokes": {"average": 3, "count": 0},
"Bad Id": {"average": 4, "count": 1},
"fps_overlay": {"average": "5", "count": 1}
}}"#;
let ratings = parse_ratings(text);
assert_eq!(
ratings.into_iter().collect::<Vec<_>>(),
[(
"zoom".to_owned(),
Rating {
average: 4.25,
count: 12
}
)]
);
assert!(parse_ratings("not json").is_empty());
assert!(parse_ratings(r#"{"projects": []}"#).is_empty());
assert_eq!(
rate_url("zoom"),
"https://playhorizon.dev/api/mods/auth/login?back=/mods/zoom"
);
}
+34 -3
View File
@@ -175,6 +175,34 @@ fn count(n: u64) -> String {
}
}
/// A registry project's rating: five pixel stars, the mean's nearest whole number of them lit, the
/// mean and how many; None while the ratings aren't known.
pub(super) fn stars(id: &str, t: &theme::Theme) -> Option<Div> {
let row = div().flex().items_center().gap(px(2.));
Some(match horizon::rating(id)? {
None => row.text_color(t.muted).child("No ratings yet"),
Some(r) => {
let lit = r.average.round() as usize;
row.children((0..5).map(|i| {
blocks::icon(
"star",
12.,
if i < lit {
t.warn
} else {
t.muted.opacity(0.6)
},
)
}))
.child(div().ml(px(4.)).child(format!(
"{:.1} ({})",
r.average,
count(r.count)
)))
}
})
}
/// A checksum as the page shows it: the algorithm and the first and last hex digits.
fn short_sum(checksum: &str) -> String {
let (algo, hex) = checksum.split_once(':').unwrap_or(("", checksum));
@@ -968,9 +996,7 @@ impl Launcher {
if !p.author.is_empty() {
by.push(format!("by {}", p.author));
}
if p.source != SourceId::Horizon {
by.push(format!("{} downloads", count(p.downloads)));
}
by.push(format!("{} downloads", count(p.downloads)));
by.push(format!("from {}", p.source.label()));
if !p.license.is_empty() {
by.push(p.license.clone());
@@ -980,6 +1006,11 @@ impl Launcher {
.flex_col()
.child(div().truncate().child(p.summary.clone()))
.child(by.join(" · "))
.children(
(p.source == SourceId::Horizon)
.then(|| stars(&p.id, &t))
.flatten(),
)
.children(
support
.reason()

Some files were not shown because too many files have changed in this diff Show More