Files
horizon-airdef/crates/horizon-client/examples/pack_resources_fixture.rs
T
Ludovic Marie 0d0378916b Resource packs: reload everything in game and merge stacked files like vanilla (#72)
Choosing packs in the Resource Packs screen only reloaded the GUI atlas
(and only when it kept its size), item models and languages; block
textures and models changed after a restart, and sounds, clouds and the
world border never did. Block textures were also squeezed to 16x16, and
the pack overlay took every file from the top pack, so a pack's own
en_us.json, sounds.json, font or atlas definition replaced vanilla's
instead of adding to it (all other text, or every vanilla sound, gone).

- Minecraft.reloadResourcePacks: rebuild block sprites and models, mesh
  tables, the shared atlas, item models, sounds, languages, GUI
  metadata, clouds.png and forcefield.png from the new overlay; swap the
  mesher's context (workers read it per job), mesh every loaded section
  again (LevelRenderer.allChanged) and build a renderer with the new
  textures. Nothing switches if loading fails.
- The block sprite array takes the largest texture's resolution (power
  of two, at most 256 MiB), so 32x/64x packs keep their detail;
  animations upload at that size.
- Files vanilla reads as resource stacks are merged in the overlay:
  ClientLanguage (keys override), SoundManager (sounds add up unless an
  event replaces; a sounds.json Gson rejects is skipped whole),
  FontManager (top pack's providers first), SpriteSourceList (sources
  bottom up). Languages are read from every namespace, not only
  minecraft.
- Missing or broken block textures use vanilla's missing texture; packs
  that still break startup fall back to the default resources
  (Minecraft.rollbackResourcePacks).
- Open Pack Folder opens the folder on macOS and Windows too.

tools/test_resource_pack_parity.py checks pack discovery, overlays,
filters, which pack provides a file and the merged languages, sounds,
fonts and atlases against vanilla's FolderRepositorySource,
MultiPackResourceManager and reload listeners.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 15:33:34 +02:00

93 lines
3.6 KiB
Rust

//! The resource root Horizon builds for a pack selection (`packs::build_overlay`)
//! and what its loaders read from it, for VanillaPackResourcesOracle.
use horizon_client::{lang::Language, packs, sound_events::SoundRegistry};
use std::{fmt::Write, fs, path::PathBuf};
fn fnv(bytes: &[u8]) -> u64 {
bytes.iter().fold(0xcbf2_9ce4_8422_2325, |h, b| {
(h ^ u64::from(*b)).wrapping_mul(0x100_0000_01b3)
})
}
/// `namespace:path` to a file under the root's `assets/`.
fn asset(root: &std::path::Path, id: &str, prefix: &str, suffix: &str) -> PathBuf {
let (namespace, path) = id.split_once(':').unwrap_or(("minecraft", id));
root.join("assets")
.join(namespace)
.join(format!("{prefix}{path}{suffix}"))
}
fn types(root: &std::path::Path, file: PathBuf, key: &str) -> String {
let _ = root;
let value: serde_json::Value = fs::read(file)
.ok()
.and_then(|b| serde_json::from_slice(&b).ok())
.unwrap_or_default();
value[key]
.as_array()
.into_iter()
.flatten()
.map(|v| {
let t = v["type"].as_str().unwrap_or("?");
format!(" {}", t.strip_prefix("minecraft:").unwrap_or(t))
})
.collect()
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<_> = std::env::args().collect();
let input = fs::read_to_string(&args[1])?;
let mut lines = input.lines();
let root: Vec<&str> = lines.next().ok_or("empty input")?.split(' ').collect();
let vanilla = PathBuf::from(root[1]);
let ids: Vec<String> = root[3..].iter().map(|s| (*s).to_owned()).collect();
let all = packs::discover(&vanilla, &PathBuf::from(root[2]));
let enabled = packs::enabled(&all, &ids);
let overlay = PathBuf::from(&args[2]).with_extension("overlay");
packs::build_overlay(&vanilla, &enabled, &overlay)?;
let language = Language::load(&[&overlay], "en_us");
let sounds = SoundRegistry::load(&overlay);
let mut out = String::new();
for line in lines {
let (kind, arg) = line.split_once(' ').unwrap_or((line, ""));
write!(out, "{line} =>")?;
match kind {
"file" => match fs::read(asset(&overlay, arg, "", "")) {
Ok(bytes) => write!(out, " {:x}", fnv(&bytes))?,
Err(_) => out.push_str(" missing"),
},
"lang" => write!(out, " {}", language.get(arg).unwrap_or(arg))?,
"sound" => {
let event = if arg.contains(':') {
arg.to_owned()
} else {
format!("minecraft:{arg}")
};
match sounds.get(&event) {
None => out.push_str(" missing"),
Some(e) => {
write!(out, " subtitle={}", e.subtitle.as_deref().unwrap_or("none"))?;
for s in &e.sounds {
if s.event {
out.push_str(" event");
} else {
write!(out, " {}*{}", s.location, s.weight)?;
}
}
}
}
}
"font" => out.push_str(&types(
&overlay,
asset(&overlay, arg, "font/", ".json"),
"providers",
)),
"atlas" => out.push_str(&types(
&overlay,
asset(&overlay, arg, "atlases/", ".json"),
"sources",
)),
_ => return Err(format!("unknown query {line}").into()),
}
out.push('\n');
}
fs::write(&args[2], out)?;
Ok(())
}