From 0d0378916b83f96024cde11b0d29c5a4626424cf Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 14 Oct 2026 15:10:01 +0200 Subject: [PATCH] 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 --- .../examples/pack_resources_fixture.rs | 92 ++++++++ crates/horizon-client/src/assets.rs | 37 ++- crates/horizon-client/src/audio.rs | 10 + crates/horizon-client/src/game.rs | 93 +++++--- crates/horizon-client/src/game/options.rs | 6 +- crates/horizon-client/src/game/packs.rs | 122 +++++----- crates/horizon-client/src/gfx.rs | 7 +- crates/horizon-client/src/lang.rs | 33 ++- crates/horizon-client/src/packs.rs | 213 +++++++++++++++++- crates/horizon-client/src/sound_events.rs | 60 +++-- crates/horizon-client/src/sprites.rs | 96 +++++++- crates/horizon-client/src/streaming.rs | 59 +++-- .../parity/VanillaPackResourcesOracle.java | 160 +++++++++++++ tools/test_resource_pack_parity.py | 138 ++++++++++++ 14 files changed, 960 insertions(+), 166 deletions(-) create mode 100644 crates/horizon-client/examples/pack_resources_fixture.rs create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPackResourcesOracle.java create mode 100644 tools/test_resource_pack_parity.py diff --git a/crates/horizon-client/examples/pack_resources_fixture.rs b/crates/horizon-client/examples/pack_resources_fixture.rs new file mode 100644 index 000000000..00238fa94 --- /dev/null +++ b/crates/horizon-client/examples/pack_resources_fixture.rs @@ -0,0 +1,92 @@ +//! 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> { + 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 = 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(()) +} diff --git a/crates/horizon-client/src/assets.rs b/crates/horizon-client/src/assets.rs index fd6e196ee..edfdcf0ea 100644 --- a/crates/horizon-client/src/assets.rs +++ b/crates/horizon-client/src/assets.rs @@ -180,15 +180,43 @@ impl Assets { .collect::>()?, )); let mut material_states = [BlockStateId(0); 6]; - let mut sprites = crate::sprites::Sprites::default(); for (layer, name) in MATERIALS.iter().enumerate() { - let state = registry + material_states[layer] = registry .default_state(&format!("minecraft:{name}")) .ok_or_else(|| invalid("missing test-scene material"))?; - material_states[layer] = state; - sprites.insert(root, &format!("minecraft:block/{name}"))?; } let registry: &'static BlockRegistry = Box::leak(Box::new(registry)); + Self::with_models( + root, + registry, + properties, + material_states, + blocks_fluid_flow, + ) + } + /// The same blocks with the models and sprites of another resource root + /// (resource packs changed): the registry and block facts come from the + /// game data, which resource packs do not touch. + pub fn reload(&self, root: &Path) -> Result { + Self::with_models( + root, + self.registry, + self.properties.clone(), + self.material_states, + self.blocks_fluid_flow.clone(), + ) + } + fn with_models( + root: &Path, + registry: &'static BlockRegistry, + properties: Properties, + material_states: [BlockStateId; 6], + blocks_fluid_flow: BTreeSet, + ) -> Result { + let mut sprites = crate::sprites::Sprites::default(); + for name in MATERIALS { + sprites.insert(root, &format!("minecraft:block/{name}"))?; + } let models = crate::models::prepare(root, registry, &mut sprites)?; // Fluids are drawn by the fluid renderer, not block models; breaking // progress uses the destroy-stage overlays. @@ -204,6 +232,7 @@ impl Assets { for stage in 0..10 { sprites.insert(root, &format!("minecraft:block/destroy_stage_{stage}"))?; } + sprites.finish(); Ok(Self { registry, properties, diff --git a/crates/horizon-client/src/audio.rs b/crates/horizon-client/src/audio.rs index 69a29217c..034354ade 100644 --- a/crates/horizon-client/src/audio.rs +++ b/crates/horizon-client/src/audio.rs @@ -600,6 +600,16 @@ impl Audio { pub fn stop_all(&mut self) { self.engine.stop_all(&mut self.backend); } + /// `SoundManager.reload` (resource packs changed): stop every sound, then + /// read sound events and sound files from the new resource root. + pub fn reload(&mut self, root: &Path) { + self.stop_all(); + self.engine.registry = SoundRegistry::load(root); + self.backend.root = root.to_owned(); + let mut cache = self.backend.cache.lock().unwrap(); + cache.entries.clear(); + cache.bytes = 0; + } /// Play a sound event at a world position, as `ClientLevel.playLocalSound` /// (a `SimpleSoundInstance` seeded from the level random), or relative to /// the listener without attenuation (`None`). diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 56af58e7d..99a12c5a5 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -6629,6 +6629,25 @@ impl Game<'_> { } out } + /// The renderer for the window, with the current block sprites, shared + /// atlas and world border texture. + fn new_renderer(&self, window: Arc) -> Result { + let assets = &self.res.assets; + pollster::block_on(Renderer::new( + window, + self.options.offscreen, + &assets.pixels, + assets.sprite_size, + assets.texture_layers, + &assets.animations, + ( + &self.res.atlas.pixels, + self.res.atlas.width, + self.res.atlas.height, + ), + (&self.res.forcefield.2, self.res.forcefield.0), + )) + } fn apply_settings(&mut self) { self.streaming.radius = self.settings.render_distance; self.streaming.biome_blend = self.settings.biome_blend; @@ -7774,21 +7793,7 @@ impl ApplicationHandler for Game<'_> { return; } }; - let assets = &self.res.assets; - match pollster::block_on(Renderer::new( - window.clone(), - self.options.offscreen, - &assets.pixels, - assets.sprite_size, - assets.texture_layers, - &assets.animations, - ( - &self.res.atlas.pixels, - self.res.atlas.width, - self.res.atlas.height, - ), - (&self.res.forcefield.2, self.res.forcefield.0), - )) { + match self.new_renderer(window.clone()) { Ok(r) => self.renderer = Some(r), Err(e) => { self.error = Some(e.to_string()); @@ -8345,6 +8350,25 @@ impl ApplicationHandler for Game<'_> { event_loop.set_control_flow(ControlFlow::Poll); } } +/// clouds.png as filled cells (`CloudRenderer.prepare`: alpha 10 or more). +fn load_clouds(root: &std::path::Path) -> (u32, u32, Vec) { + horizon_client::hud::decode(&root.join("assets/minecraft/textures/environment/clouds.png")) + .map(|(w, h, px)| { + ( + w, + h, + px.as_chunks::<4>().0.iter().map(|p| p[3] >= 10).collect(), + ) + }) + .unwrap_or((1, 1, vec![false])) +} +/// misc/forcefield.png for the world border wall (square, power of two). +fn load_forcefield(root: &std::path::Path) -> (u32, u32, Vec) { + horizon_client::hud::decode(&root.join("assets/minecraft/textures/misc/forcefield.png")) + .ok() + .filter(|(w, h, _)| w == h && w.is_power_of_two()) + .unwrap_or((1, 1, vec![0; 4])) +} pub fn run() -> Result<()> { let Some(mut options) = Options::read()? else { return Ok(()); @@ -8357,10 +8381,24 @@ pub fn run() -> Result<()> { let vanilla_root = options.resources.clone(); let early_settings = Settings::load(&std::env::current_dir()?.join("options.txt")); options.resources = packs::startup_root(&vanilla_root, &early_settings.resource_packs); + let assets = match Assets::load(&options.resources) { + Ok(assets) => assets, + // Minecraft.rollbackResourcePacks: the packs break loading, start + // with the game's own resources. + Err(e) if options.resources != vanilla_root => { + eprintln!("Horizon: resource packs failed to load ({e}); using the default resources"); + options.resources = vanilla_root.clone(); + Assets::load(&vanilla_root)? + } + Err(e) => { + return Err(format!( + "{e}\nPrepare resources with: python3 tools/prepare_native_client.py --prepare" + ) + .into()); + } + }; let root = options.resources.clone(); - let assets = Arc::new(Assets::load(&root).map_err(|e| { - format!("{e}\nPrepare resources with: python3 tools/prepare_native_client.py --prepare") - })?); + let assets = Arc::new(assets); eprintln!("load assets: {:.2}s", started.elapsed().as_secs_f32()); let facts = BlockFacts::load(&root, assets.registry)?; let mob_data = horizon_client::mobs::MobData::load(&root, &facts); @@ -8461,23 +8499,8 @@ pub fn run() -> Result<()> { end_environment: Environment::load_dimension(&root, "the_end", false)?, behaviours: Behaviours::load(registry_owner.registry, &root)?, names: Names::load(&root), - clouds: horizon_client::hud::decode( - &root.join("assets/minecraft/textures/environment/clouds.png"), - ) - .map(|(w, h, px)| { - ( - w, - h, - px.as_chunks::<4>().0.iter().map(|p| p[3] >= 10).collect(), - ) - }) - .unwrap_or((1, 1, vec![false])), - forcefield: horizon_client::hud::decode( - &root.join("assets/minecraft/textures/misc/forcefield.png"), - ) - .ok() - .filter(|(w, h, _)| w == h && w.is_power_of_two()) - .unwrap_or((1, 1, vec![0; 4])), + clouds: load_clouds(&root), + forcefield: load_forcefield(&root), mob_types: MobTypes::load(&root)? .types .into_iter() diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index 810a71538..3c710c91e 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -121,11 +121,9 @@ impl Game<'_> { self.reload_packs(); } Event::OpenPackFolder => { - let dir = std::env::current_dir() - .unwrap_or_default() - .join("resourcepacks"); + let dir = super::packs::packs_dir(); let _ = std::fs::create_dir_all(&dir); - let _ = std::process::Command::new("xdg-open").arg(&dir).spawn(); + super::packs::open_folder(&dir); } } } diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs index 910053ba6..3d4258362 100644 --- a/crates/horizon-client/src/game/packs.rs +++ b/crates/horizon-client/src/game/packs.rs @@ -1,6 +1,5 @@ -//! Resource pack selection bound to the game: the pack screen lists and -//! reloading after a change (atlas, fonts, GUI metadata, languages and item -//! icons live; block textures and models from the next start). +//! Resource pack selection bound to the game: the pack screen lists, and a +//! full resource reload after a change, while playing. use super::Game; use horizon_client::{ hud::HudAtlas, @@ -22,6 +21,19 @@ pub fn packs_dir() -> PathBuf { .unwrap_or_default() .join("resourcepacks") } +/// `Util.getPlatform().openPath`: the system file manager on the folder. +pub fn open_folder(dir: &Path) { + let program = if cfg!(target_os = "windows") { + "explorer" + } else if cfg!(target_os = "macos") { + "open" + } else { + "xdg-open" + }; + if let Err(e) = std::process::Command::new(program).arg(dir).spawn() { + eprintln!("Horizon: could not open {}: {e}", dir.display()); + } +} /// The resource root to load from, given the vanilla root and the selection. pub fn startup_root(vanilla: &Path, selected: &[String]) -> PathBuf { let all = packs::discover(vanilla, &packs_dir()); @@ -81,63 +93,67 @@ impl Game<'_> { o.pack_selection = self.settings.resource_packs.clone(); o.pack_screen = None; } - /// Rebuild the overlay and reload what can change while running. + /// `Minecraft.reloadResourcePacks`: rebuild the overlay and reload every + /// client resource from it, as vanilla's reload listeners do: block + /// models and sprites (every section is meshed again, + /// `LevelRenderer.allChanged`), the shared atlas (GUI, item, entity, + /// particle and font textures), item models, sounds, languages, GUI + /// metadata and the cloud and world border textures. pub(super) fn reload_packs(&mut self) { let vanilla = self.vanilla_root(); if self.ui.vanilla_root.as_os_str().is_empty() { self.ui.vanilla_root = vanilla.clone(); } let root = startup_root(&vanilla, &self.settings.resource_packs); - self.options.resources = root.clone(); - horizon_client::ui::load_metadata(&root); - match HudAtlas::build(&root) { - Ok(mut atlas) => { - atlas.font.uniform = self.settings.force_unicode; - atlas.font.japanese = self.settings.japanese_glyphs; - let same_size = - atlas.width == self.res.atlas.width && atlas.height == self.res.atlas.height; - if same_size { - if let Some(texture) = self - .renderer - .as_ref() - .and_then(|r| r.hud.atlas_texture.clone()) - && let Some(r) = self.renderer.as_ref() - { - r.queue.write_texture( - wgpu::TexelCopyTextureInfo { - texture: &texture, - mip_level: 0, - origin: wgpu::Origin3d::ZERO, - aspect: wgpu::TextureAspect::All, - }, - &atlas.pixels, - wgpu::TexelCopyBufferLayout { - offset: 0, - bytes_per_row: Some(atlas.width * 4), - rows_per_image: None, - }, - wgpu::Extent3d { - width: atlas.width, - height: atlas.height, - depth_or_array_layers: 1, - }, - ); - } - self.res.atlas = atlas; - match horizon_client::item_render::ItemRenderer::load(&root) { - Ok(r) => self.res.item_renderer = std::sync::Arc::new(r), - Err(e) => eprintln!("Horizon: item model reload failed: {e}"), - } - } else { - eprintln!( - "Horizon: resource pack atlas size changed; restart to apply GUI textures" - ); - } - } - Err(e) => eprintln!("Horizon: resource pack reload failed: {e}"), + if let Err(e) = self.reload_resources(&root) { + eprintln!("Horizon: resource reload failed: {e}"); + } + } + fn reload_resources(&mut self, root: &Path) -> horizon_client::Result<()> { + let assets = std::sync::Arc::new(self.res.assets.reload(root)?); + let mut atlas = HudAtlas::build(root)?; + atlas.font.uniform = self.settings.force_unicode; + atlas.font.japanese = self.settings.japanese_glyphs; + let item_renderer = horizon_client::item_render::ItemRenderer::load(root)?; + let visibility = horizon_client::models::load_visibility(root, assets.registry)?; + let tables = std::sync::Arc::new(horizon_client::mesher::MeshTables::new( + &assets, + &self.res.facts, + visibility, + )?); + // Everything loaded: switch over. + self.options.resources = root.to_path_buf(); + let context = std::sync::Arc::new(horizon_client::streaming::WorkerContext { + assets: assets.clone(), + tables, + biomes: self.res.context.biomes.clone(), + light: self.res.context.light.clone(), + }); + self.res.layer_names = assets + .sprite_layers + .iter() + .map(|(name, layer)| (*layer, name.trim_start_matches("minecraft:").to_owned())) + .collect(); + self.res.assets = assets; + self.res.context = context.clone(); + self.res.atlas = atlas; + self.res.item_renderer = std::sync::Arc::new(item_renderer); + self.res.clouds = super::load_clouds(root); + self.res.forcefield = super::load_forcefield(root); + self.res.names = super::Names::load(root); + horizon_client::ui::load_metadata(root); + lang::set(lang::Language::load(&[&root.to_path_buf()], &lang::code())); + self.audio.reload(root); + self.streaming.reload_resources(&mut self.world, context); + self.pending_uploads.clear(); + // New GPU textures: a renderer built from the reloaded resources; the + // sections are uploaded again as they are meshed. + if let Some(window) = self.window.clone() { + self.renderer = None; + self.renderer = Some(self.new_renderer(window)?); + self.apply_ui_settings(); } - let code = lang::code(); - lang::set(lang::Language::load(&[&root], &code)); self.ui.chat.rescale(&self.res.atlas); + Ok(()) } } diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index f5b92d30b..9b1af363a 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -255,6 +255,8 @@ pub struct Renderer { lightmap_view: wgpu::TextureView, lightmap_sampler: wgpu::Sampler, animations: Vec<(SpriteAnimation, usize)>, + /// Edge length of the block sprite array's layers. + sprite_size: u32, indices: wgpu::Buffer, index_quads: u32, instances: wgpu::Buffer, @@ -1327,6 +1329,7 @@ impl Renderer { lightmap_view, lightmap_sampler, animations: animations.iter().map(|a| (a.clone(), usize::MAX)).collect(), + sprite_size, indices, index_quads, instances, @@ -1704,10 +1707,10 @@ impl Renderer { continue; } *previous = frame; - let (mips, levels) = mip_chain(animation.pixels(frame), 16, 1); + let (mips, levels) = mip_chain(animation.pixels(frame), self.sprite_size, 1); let mut offset = 0; for level in 0..levels.min(self.mip_levels) { - let size = 16 >> level; + let size = self.sprite_size >> level; let bytes = (size * size * 4) as usize; self.queue.write_texture( wgpu::TexelCopyTextureInfo { diff --git a/crates/horizon-client/src/lang.rs b/crates/horizon-client/src/lang.rs index 7cd24a62b..9f04ef27a 100644 --- a/crates/horizon-client/src/lang.rs +++ b/crates/horizon-client/src/lang.rs @@ -32,11 +32,24 @@ static CURRENT: RwLock>> = RwLock::new(None); /// A source of resource files: the prepared root plus packs, highest priority last. pub trait ResourceSource { fn read(&self, path: &str) -> Option>; + /// The namespaces under `assets/` (resource packs may add their own). + fn namespaces(&self) -> Vec { + vec!["minecraft".into()] + } } impl ResourceSource for std::path::PathBuf { fn read(&self, path: &str) -> Option> { std::fs::read(self.join(path)).ok() } + fn namespaces(&self) -> Vec { + std::fs::read_dir(self.join("assets")) + .into_iter() + .flatten() + .flatten() + .filter(|e| e.path().is_dir()) + .filter_map(|e| e.file_name().to_str().map(str::to_owned)) + .collect() + } } /// DeprecatedTranslationsInfo.applyToMap: drop removed keys, then move each @@ -71,20 +84,26 @@ impl Language { } } /// ClientLanguage.loadFrom: en_us first, then the selected language, each - /// from every source in priority order (later sources override). + /// from every namespace and every source in priority order (later sources + /// override). pub fn load(sources: &[&dyn ResourceSource], code: &str) -> Self { let mut map = HashMap::new(); let mut codes = vec![DEFAULT]; if code != DEFAULT { codes.push(code); } + let namespaces: std::collections::BTreeSet = + sources.iter().flat_map(|s| s.namespaces()).collect(); for c in codes { - for source in sources { - let Some(bytes) = source.read(&format!("assets/minecraft/lang/{c}.json")) else { - continue; - }; - if let Ok(entries) = serde_json::from_slice::>(&bytes) { - map.extend(entries); + for namespace in &namespaces { + for source in sources { + let Some(bytes) = source.read(&format!("assets/{namespace}/lang/{c}.json")) + else { + continue; + }; + if let Ok(entries) = serde_json::from_slice::>(&bytes) { + map.extend(entries); + } } } } diff --git a/crates/horizon-client/src/packs.rs b/crates/horizon-client/src/packs.rs index af0f3ba38..c5bd7c341 100644 --- a/crates/horizon-client/src/packs.rs +++ b/crates/horizon-client/src/packs.rs @@ -387,10 +387,19 @@ pub fn build_overlay(vanilla: &Path, packs: &[Pack], out: &Path) -> io::Result<( fs::create_dir_all(out)?; // Final owner of every overridden file. let mut files: BTreeMap = BTreeMap::new(); + // Every pack's copy of the files vanilla reads as a resource stack. + let mut stacks: BTreeMap> = BTreeMap::new(); for (i, p) in packs.iter().enumerate() { // A pack's filter hides what the packs below it provide. files.retain(|name, _| !p.filter.iter().any(|f| f.blocks(name))); + stacks.retain(|name, _| !p.filter.iter().any(|f| f.blocks(name))); for (name, provided) in pack_files(p)? { + if merge_kind(&name).is_some() { + stacks + .entry(name.clone()) + .or_default() + .push((i, provided.clone())); + } files.insert(name, (i, provided)); } } @@ -413,12 +422,39 @@ pub fn build_overlay(vanilla: &Path, packs: &[Pack], out: &Path) -> io::Result<( } }) .collect(); + // Stacked files merge every layer, vanilla's first (unless filtered). + let mut merged: BTreeMap> = BTreeMap::new(); + for (name, stack) in &stacks { + let mut layers = Vec::new(); + if !filters.iter().any(|f| f.blocks(name)) + && let Ok(bytes) = fs::read(vanilla.join(name)) + { + layers.push(bytes); + } + for (owner, (path, inner)) in stack { + let bytes = match path { + Some(p) => fs::read(p).ok(), + None => zips + .get(*owner) + .and_then(Option::as_ref) + .and_then(|z| z.read(inner).ok()), + }; + layers.extend(bytes); + } + if let Some(kind) = merge_kind(name) + && let Some(bytes) = merge(kind, &layers) + { + merged.insert(name.clone(), bytes); + } + } + #[allow(clippy::too_many_arguments)] fn mirror( rel: &str, vanilla: &Path, out: &Path, touched: &BTreeSet, files: &BTreeMap, + merged: &BTreeMap>, filters: &[&Filter], zips: &[Option], ) -> io::Result<()> { @@ -451,7 +487,9 @@ pub fn build_overlay(vanilla: &Path, packs: &[Pack], out: &Path) -> io::Result<( } for name in names { let child = format!("{prefix}{name}"); - if let Some((owner, (path, inner))) = files.get(&child) { + if let Some(bytes) = merged.get(&child) { + fs::write(out.join(&child), bytes)?; + } else if let Some((owner, (path, inner))) = files.get(&child) { let target = out.join(&child); match path { Some(p) => link(p, &target)?, @@ -464,7 +502,7 @@ pub fn build_overlay(vanilla: &Path, packs: &[Pack], out: &Path) -> io::Result<( } else if touched.contains(&child) || (vanilla.join(&child).is_dir() && !filters.is_empty()) { - mirror(&child, vanilla, out, touched, files, filters, zips)?; + mirror(&child, vanilla, out, touched, files, merged, filters, zips)?; } else if filters.iter().any(|f| f.blocks(&child)) { // Hidden by a pack's filter. } else { @@ -473,7 +511,93 @@ pub fn build_overlay(vanilla: &Path, packs: &[Pack], out: &Path) -> io::Result<( } Ok(()) } - mirror("", vanilla, out, &touched_dirs, &files, &filters, &zips) + mirror( + "", + vanilla, + out, + &touched_dirs, + &files, + &merged, + &filters, + &zips, + ) +} +/// Files vanilla loads from every pack in turn (`getResourceStack`) rather +/// than from the top pack only. +#[derive(Clone, Copy, Debug, PartialEq)] +enum Merge { + /// `ClientLanguage.appendFrom`: later packs override keys. + Lang, + /// `SoundManager.Preparations.handleRegistration`: sounds add up unless + /// an event says `replace`. + Sounds, + /// `FontManager.loadResourceStack`: the top pack's providers come first. + Font, + /// `SpriteSourceList.load`: sources from the bottom pack up. + Atlas, +} +fn merge_kind(name: &str) -> Option { + let (_, rest) = name.strip_prefix("assets/")?.split_once('/')?; + if rest == "sounds.json" { + return Some(Merge::Sounds); + } + let dir = rest.strip_suffix(".json")?.split_once('/')?.0; + match dir { + "lang" => Some(Merge::Lang), + "font" => Some(Merge::Font), + "atlases" => Some(Merge::Atlas), + _ => None, + } +} +/// One file with the effect of loading `layers` (bottom first) as vanilla +/// does. Layers that do not parse are skipped, as vanilla logs and skips them. +fn merge(kind: Merge, layers: &[Vec]) -> Option> { + use serde_json::{Map, Value}; + let parsed: Vec> = layers + .iter() + // SoundManager skips a sounds.json Gson rejects, whole. + .filter(|b| kind != Merge::Sounds || crate::sound_events::is_valid_file(b)) + .filter_map(|b| match serde_json::from_slice(b) { + Ok(Value::Object(m)) => Some(m), + _ => None, + }) + .collect(); + let list = |key: &str, layers: &mut dyn Iterator>| { + let items: Vec = layers + .filter_map(|l| l.get(key).and_then(Value::as_array)) + .flatten() + .cloned() + .collect(); + Value::Object(Map::from_iter([(key.to_owned(), Value::Array(items))])) + }; + let out = match kind { + Merge::Lang => Value::Object(parsed.into_iter().flatten().collect()), + Merge::Sounds => { + let mut events = Map::new(); + for (event, def) in parsed.into_iter().flatten() { + if !events.contains_key(&event) + || crate::sound_events::json_bool(&def["replace"], false) + { + let mut fresh = Map::new(); + if let Some(subtitle) = def.get("subtitle") { + fresh.insert("subtitle".into(), subtitle.clone()); + } + fresh.insert("sounds".into(), Value::Array(Vec::new())); + events.insert(event.clone(), Value::Object(fresh)); + } + if let (Some(Value::Array(sounds)), Some(Value::Array(more))) = ( + events.get_mut(&event).and_then(|e| e.get_mut("sounds")), + def.get("sounds"), + ) { + sounds.extend(more.iter().cloned()); + } + } + Value::Object(events) + } + Merge::Font => list("providers", &mut parsed.iter().rev()), + Merge::Atlas => list("sources", &mut parsed.iter()), + }; + serde_json::to_vec(&out).ok() } /// The packs to apply for an options.txt `resourcePacks` list (bottom first). pub fn enabled(all: &[Pack], ids: &[String]) -> Vec { @@ -709,6 +833,74 @@ mod tests { ); } #[test] + fn stacked_resources_merge_like_vanilla() { + let json = |v: &str| v.as_bytes().to_vec(); + let parse = |b: Vec| serde_json::from_slice::(&b).unwrap(); + // ClientLanguage: later packs override keys, the rest stays. + let lang = merge( + Merge::Lang, + &[ + json(r#"{"a":"vanilla a","b":"vanilla b"}"#), + json(r#"{"b":"pack b"}"#), + ], + ); + assert_eq!( + parse(lang.unwrap()), + serde_json::json!({"a":"vanilla a","b":"pack b"}) + ); + // SoundManager: sounds add up; `replace` starts the event over. + let sounds = merge( + Merge::Sounds, + &[ + json(r#"{"x":{"subtitle":"s.x","sounds":["v1"]},"y":{"sounds":["v2"]}}"#), + json( + r#"{"x":{"sounds":["p1"]},"y":{"replace":true,"subtitle":"s.y","sounds":["p2"]}}"#, + ), + b"not json".to_vec(), + ], + ); + assert_eq!( + parse(sounds.unwrap()), + serde_json::json!({ + "x":{"subtitle":"s.x","sounds":["v1","p1"]}, + "y":{"subtitle":"s.y","sounds":["p2"]} + }) + ); + // FontManager: the top pack's providers come first. + let font = merge( + Merge::Font, + &[json(r#"{"providers":[1,2]}"#), json(r#"{"providers":[3]}"#)], + ); + assert_eq!( + parse(font.unwrap()), + serde_json::json!({"providers":[3,1,2]}) + ); + // SpriteSourceList: sources from the bottom pack up. + let atlas = merge( + Merge::Atlas, + &[json(r#"{"sources":[1]}"#), json(r#"{"sources":[2]}"#)], + ); + assert_eq!(parse(atlas.unwrap()), serde_json::json!({"sources":[1,2]})); + assert_eq!( + merge_kind("assets/minecraft/lang/en_us.json"), + Some(Merge::Lang) + ); + assert_eq!( + merge_kind("assets/minecraft/sounds.json"), + Some(Merge::Sounds) + ); + assert_eq!( + merge_kind("assets/minecraft/font/default.json"), + Some(Merge::Font) + ); + assert_eq!(merge_kind("assets/minecraft/font/unifont.zip"), None); + assert_eq!( + merge_kind("assets/minecraft/atlases/blocks.json"), + Some(Merge::Atlas) + ); + assert_eq!(merge_kind("assets/minecraft/models/block/stone.json"), None); + } + #[test] fn overlay_layers_packs_over_vanilla() { let base = std::env::temp_dir().join(format!("horizon-packs-{}", std::process::id())); let _ = fs::remove_dir_all(&base); @@ -725,7 +917,11 @@ mod tests { b"vanilla dirt", ) .unwrap(); - fs::write(vanilla.join("assets/minecraft/lang/en_us.json"), b"{}").unwrap(); + fs::write( + vanilla.join("assets/minecraft/lang/en_us.json"), + br#"{"a":"vanilla","b":"vanilla"}"#, + ) + .unwrap(); fs::write(vanilla.join("facts.json"), b"[]").unwrap(); let pack_dir = base.join("packs/Mine"); fs::create_dir_all(pack_dir.join("assets/minecraft/textures/block")).unwrap(); @@ -739,6 +935,12 @@ mod tests { b"pack", ) .unwrap(); + fs::create_dir_all(pack_dir.join("assets/minecraft/lang")).unwrap(); + fs::write( + pack_dir.join("assets/minecraft/lang/en_us.json"), + br#"{"b":"pack"}"#, + ) + .unwrap(); let packs = discover(&vanilla, &base.join("packs")); assert_eq!(packs.len(), 1); assert_eq!(packs[0].id, "file/Mine"); @@ -752,9 +954,10 @@ mod tests { fs::read(out.join("assets/minecraft/textures/block/dirt.png")).unwrap(), b"vanilla dirt" ); + // A pack's language file adds to vanilla's instead of replacing it. assert_eq!( fs::read(out.join("assets/minecraft/lang/en_us.json")).unwrap(), - b"{}" + br#"{"a":"vanilla","b":"pack"}"# ); assert_eq!(fs::read(out.join("facts.json")).unwrap(), b"[]"); let _ = fs::remove_dir_all(base); diff --git a/crates/horizon-client/src/sound_events.rs b/crates/horizon-client/src/sound_events.rs index 4f1af3670..3304ca4a9 100644 --- a/crates/horizon-client/src/sound_events.rs +++ b/crates/horizon-client/src/sound_events.rs @@ -106,6 +106,42 @@ pub struct WeighedSoundEvents { /// The subtitle translation key. pub subtitle: Option, } +/// A `sounds.json` entry: event, `replace`, subtitle and sounds. +type Registration = (String, bool, Option, Vec); +/// Parse a `sounds.json`; `None` when Gson would reject the whole file (a +/// malformed entry anywhere in it). +fn parse_file(namespace: &str, text: &[u8]) -> Option> { + let Ok(Value::Object(map)) = serde_json::from_slice::(text) else { + return None; + }; + let mut parsed = Vec::new(); + for (name, def) in &map { + let Value::Object(object) = def else { + return None; + }; + let replace = json_bool(object.get("replace").unwrap_or(&Value::Null), false); + let subtitle = match object.get("subtitle") { + None | Some(Value::Null) => None, + Some(Value::String(s)) => Some(s.clone()), + Some(_) => return None, + }; + let mut sounds = Vec::new(); + if let Some(list) = object.get("sounds") { + let Value::Array(list) = list else { + return None; + }; + for s in list { + sounds.push(parse_sound(s)?); + } + } + parsed.push((format!("{namespace}:{name}"), replace, subtitle, sounds)); + } + Some(parsed) +} +/// Whether vanilla's `SoundManager` reads this `sounds.json` at all. +pub fn is_valid_file(text: &[u8]) -> bool { + parse_file("minecraft", text).is_some() +} /// The sound event registry (`SoundManager.registry`). #[derive(Clone, Debug, Default)] pub struct SoundRegistry { @@ -148,7 +184,7 @@ fn json_int(v: &Value, fallback: i32) -> i32 { _ => fallback, } } -fn json_bool(v: &Value, fallback: bool) -> bool { +pub(crate) fn json_bool(v: &Value, fallback: bool) -> bool { match v { Value::Bool(b) => *b, Value::String(s) => s == "true", @@ -213,29 +249,9 @@ impl SoundRegistry { /// One `sounds.json` (`Preparations.handleRegistration` for each entry). /// `exists(namespace, path)` says whether a sound file is present. pub fn add_file(&mut self, namespace: &str, text: &[u8], exists: &dyn Fn(&str, &str) -> bool) { - let Ok(Value::Object(map)) = serde_json::from_slice::(text) else { + let Some(parsed) = parse_file(namespace, text) else { return; }; - // A malformed entry makes Gson reject the whole file. - let mut parsed = Vec::new(); - for (name, def) in &map { - let Value::Object(object) = def else { return }; - let replace = json_bool(object.get("replace").unwrap_or(&Value::Null), false); - let subtitle = match object.get("subtitle") { - None | Some(Value::Null) => None, - Some(Value::String(s)) => Some(s.clone()), - Some(_) => return, - }; - let mut sounds = Vec::new(); - if let Some(list) = object.get("sounds") { - let Value::Array(list) = list else { return }; - for s in list { - let Some(sound) = parse_sound(s) else { return }; - sounds.push(sound); - } - } - parsed.push((format!("{namespace}:{name}"), replace, subtitle, sounds)); - } for (location, replace, subtitle, sounds) in parsed { let entry = match self.events.get_mut(&location) { Some(entry) if !replace => entry, diff --git a/crates/horizon-client/src/sprites.rs b/crates/horizon-client/src/sprites.rs index 758e9e857..39707ef1d 100644 --- a/crates/horizon-client/src/sprites.rs +++ b/crates/horizon-client/src/sprites.rs @@ -26,20 +26,96 @@ impl SpriteAnimation { #[derive(Default)] pub struct Sprites { + /// Edge length of every array layer and the layers' current frames, set + /// by [`Sprites::finish`]. pub size: u32, pub pixels: Vec, pub layers: BTreeMap, pub animations: Vec, pub opaque: Vec, pub translucent: Vec, + /// Each layer's frames at the texture's own resolution (square), and the + /// frame shown first. + native: Vec<(u32, Arc>>, usize)>, } -/// Edge length of every sprite array layer. +/// Edge length of vanilla's block textures: the smallest array layer. pub const SPRITE_SIZE: u32 = 16; +/// Largest sprite array (all layers of the top mip level): resource packs +/// with bigger textures are sampled down to fit. +const MAX_ARRAY_BYTES: u64 = 256 << 20; +/// Nearest-resample a square RGBA frame to another square size. +fn resample(frame: &[u8], from: u32, to: u32) -> Vec { + if from == to { + return frame.to_vec(); + } + let mut out = Vec::with_capacity((to * to * 4) as usize); + for row in 0..to { + let source_row = row * from / to; + for column in 0..to { + let offset = ((source_row * from + column * from / to) * 4) as usize; + out.extend_from_slice(&frame[offset..offset + 4]); + } + } + out +} impl Sprites { + /// Lay every layer out at one size: the largest texture's (resource packs + /// may ship 32x, 64x... textures, like vanilla's stitched atlas), rounded + /// up to a power of two and limited to [`MAX_ARRAY_BYTES`]. Smaller + /// textures are scaled up by whole pixels, so UVs stay per layer. + pub fn finish(&mut self) { + let layers = self.native.len().max(1) as u64; + let largest = self.native.iter().map(|n| n.0).max().unwrap_or(SPRITE_SIZE); + let mut size = largest.max(SPRITE_SIZE).next_power_of_two(); + while size > SPRITE_SIZE && layers * u64::from(size * size) * 4 > MAX_ARRAY_BYTES { + size /= 2; + } + let mut resized = Vec::with_capacity(self.native.len()); + self.pixels.clear(); + for (from, frames, initial) in &self.native { + let frames: Arc>> = + Arc::new(frames.iter().map(|f| resample(f, *from, size)).collect()); + self.pixels.extend_from_slice(&frames[*initial]); + resized.push(frames); + } + for animation in &mut self.animations { + animation.frames = resized[animation.layer as usize].clone(); + } + self.size = size; + } + /// The layer of a texture, added on first use. A texture that is missing + /// or does not load (a resource pack's filter or a broken file) becomes + /// vanilla's missing texture, as in a stitched atlas. pub fn insert(&mut self, root: &Path, name: &str) -> Result { if let Some(layer) = self.layers.get(name) { return Ok(*layer); } + match self.load(root, name) { + Ok(layer) => Ok(layer), + Err(e) => { + eprintln!("Horizon: using the missing texture for {name}: {e}"); + // MissingTextureAtlasSprite.generateMissingImage: 16x16, + // magenta and black quarters. + let frame: Vec = (0..SPRITE_SIZE * SPRITE_SIZE) + .flat_map(|i| { + let (x, y) = (i % SPRITE_SIZE, i / SPRITE_SIZE); + if (y < SPRITE_SIZE / 2) ^ (x < SPRITE_SIZE / 2) { + [248, 0, 248, 255] + } else { + [0, 0, 0, 255] + } + }) + .collect(); + let layer = u32::try_from(self.layers.len())?; + self.opaque.push(true); + self.translucent.push(false); + self.layers.insert(name.to_owned(), layer); + self.native.push((SPRITE_SIZE, Arc::new(vec![frame]), 0)); + Ok(layer) + } + } + } + fn load(&mut self, root: &Path, name: &str) -> Result { let (namespace, path) = name.split_once(':').ok_or("invalid sprite identifier")?; let path = root.join(format!("assets/{namespace}/textures/{path}.png")); let metadata = match fs::read_to_string(path.with_extension("png.mcmeta")) { @@ -89,18 +165,19 @@ impl Sprites { let layer = u32::try_from(self.layers.len())?; let columns = info.width / width; let count = columns * (info.height / height); + // Layers are square; a frame of another shape is nearest-resampled to + // its longer side so UVs (normalized per layer) are unchanged. + let side = width.max(height); let mut frames = Vec::new(); for index in 0..count { let x = index % columns * width; let y = index / columns * height; - // Every array layer is SPRITE_SIZE square; other sizes are - // nearest-resampled so UVs (normalized per layer) are unchanged. - let mut frame = Vec::with_capacity((SPRITE_SIZE * SPRITE_SIZE * 4) as usize); - for row in 0..SPRITE_SIZE { - let source_row = y + row * height / SPRITE_SIZE; - for column in 0..SPRITE_SIZE { + let mut frame = Vec::with_capacity((side * side * 4) as usize); + for row in 0..side { + let source_row = y + row * height / side; + for column in 0..side { let offset = - ((source_row * info.width + x + column * width / SPRITE_SIZE) * 4) as usize; + ((source_row * info.width + x + column * width / side) * 4) as usize; frame.extend_from_slice(&pixels[offset..offset + 4]); } } @@ -131,8 +208,7 @@ impl Sprites { .any(|p| p[3] != 0 && p[3] != 255), ); self.layers.insert(name.to_owned(), layer); - self.size = SPRITE_SIZE; - self.pixels.extend_from_slice(&frames[initial]); + self.native.push((side, frames, initial)); Ok(layer) } } diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index eec4b9eeb..4d9bac937 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -14,7 +14,7 @@ use horizon_world::worldgen::decoration_order::DecorationOrder; use std::{ collections::{BinaryHeap, HashSet}, sync::{ - Arc, Condvar, Mutex, + Arc, Condvar, Mutex, RwLock, atomic::{AtomicBool, Ordering}, mpsc, }, @@ -94,6 +94,9 @@ struct Shared { queue: Mutex>, ready: Condvar, stop: AtomicBool, + /// What meshing reads (block models and sprites), swapped when resource + /// packs reload; each mesh job takes the current one. + context: RwLock>, } pub struct Streaming { shared: Arc, @@ -165,6 +168,7 @@ impl Streaming { queue: Mutex::new(BinaryHeap::new()), ready: Condvar::new(), stop: AtomicBool::new(false), + context: RwLock::new(context.clone()), }); let (send, results) = mpsc::channel(); let light_tables = context.light.clone(); @@ -173,22 +177,11 @@ impl Streaming { let shared = shared.clone(); let send = send.clone(); let generator = generator.clone(); - let context = context.clone(); + let light = context.light.clone(); thread::Builder::new() .name(format!("horizon-worker-{i}")) .spawn(move || { let mut worker = generator.worker(); - let mut mesher = Mesher { - nether_shading: false, - zoom: BiomeZoom::new(0), - blend_radius: 2, - smooth_lighting: true, - cutout_leaves: true, - assets: &context.assets, - tables: &context.tables, - biomes: &context.biomes, - visibility: context.tables.visibility.clone(), - }; loop { let job = { let mut queue = shared.queue.lock().unwrap(); @@ -212,18 +205,22 @@ impl Streaming { Done::Decorated(x, z, region) } Job::Finish(proto) => Done::Generated(generator.finish(proto)), - Job::Light(x, z, region, seq, sky) => Done::Lit( - x, - z, - light_chunk(&borrow(®ion), &context.light, sky), - seq, - ), + Job::Light(x, z, region, seq, sky) => { + Done::Lit(x, z, light_chunk(&borrow(®ion), &light, sky), seq) + } Job::Mesh(x, sy, z, region, seq, settings) => { - mesher.nether_shading = settings.nether; - mesher.zoom = settings.zoom; - mesher.blend_radius = settings.blend_radius; - mesher.smooth_lighting = settings.smooth_lighting; - mesher.cutout_leaves = settings.cutout_leaves; + let context = shared.context.read().unwrap().clone(); + let mut mesher = Mesher { + nether_shading: settings.nether, + zoom: settings.zoom, + blend_radius: settings.blend_radius, + smooth_lighting: settings.smooth_lighting, + cutout_leaves: settings.cutout_leaves, + assets: &context.assets, + tables: &context.tables, + biomes: &context.biomes, + visibility: context.tables.visibility.clone(), + }; Done::Meshed(x, sy, z, mesher.mesh(&borrow(®ion), sy), seq) } }; @@ -273,6 +270,20 @@ impl Streaming { extra: Vec::new(), } } + /// Resource packs reloaded (`LevelRenderer.allChanged`): mesh with the new + /// models and sprites, and rebuild every loaded section. Meshes in flight + /// were made with the old resources; their results are dropped. + pub fn reload_resources(&mut self, world: &mut World, context: Arc) { + *self.shared.context.write().unwrap() = context; + self.meshing.clear(); + for &(x, z) in world.chunks.keys() { + if self.lit.contains(&(x, z)) { + for sy in 0..SECTIONS as i32 { + world.dirty_sections.insert((x, sy, z)); + } + } + } + } /// A column received from a server: store it and relight it and its neighbours. pub fn insert_remote(&mut self, world: &mut World, chunk: Chunk) { let key = (chunk.x, chunk.z); diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPackResourcesOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPackResourcesOracle.java new file mode 100644 index 000000000..93564094e --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPackResourcesOracle.java @@ -0,0 +1,160 @@ +package dev.horizonlab.parity; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.mojang.serialization.JsonOps; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import net.minecraft.client.gui.font.providers.GlyphProviderDefinition; +import net.minecraft.client.renderer.texture.atlas.SpriteSourceList; +import net.minecraft.client.renderer.texture.atlas.SpriteSources; +import net.minecraft.client.resources.language.ClientLanguage; +import net.minecraft.client.resources.sounds.Sound; +import net.minecraft.client.sounds.SoundManager; +import net.minecraft.client.sounds.WeighedSoundEvents; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.contents.TranslatableContents; +import net.minecraft.resources.Identifier; +import net.minecraft.server.packs.PackLocationInfo; +import net.minecraft.server.packs.PackResources; +import net.minecraft.server.packs.PackType; +import net.minecraft.server.packs.PathPackResources; +import net.minecraft.server.packs.repository.FolderRepositorySource; +import net.minecraft.server.packs.repository.PackRepository; +import net.minecraft.server.packs.repository.PackSource; +import net.minecraft.server.packs.resources.MultiPackResourceManager; +import net.minecraft.server.packs.resources.Resource; +import net.minecraft.util.profiling.InactiveProfiler; +import net.minecraft.world.level.validation.DirectoryValidator; + +/** + * The client resource manager over vanilla's resources and a selection of + * resource packs from a folder (FolderRepositorySource: folder and zip packs, + * pack.mcmeta overlays and filters), and what the reload listeners read from + * it: which pack provides a file, translations (ClientLanguage), sound events + * (SoundManager.prepare), font providers (FontManager) and atlas sources + * (SpriteSourceList). + * + * Input: a first line "root VANILLA PACKS ID..." (bottom first), then queries: + * "file PATH", "lang KEY", "sound EVENT", "font ID", "atlas ID". + */ +public final class VanillaPackResourcesOracle { + public static void main(String[] args) throws Exception { + net.minecraft.SharedConstants.tryDetectVersion(); + net.minecraft.server.Bootstrap.bootStrap(); + SpriteSources.bootstrap(); + List lines = Files.readAllLines(Path.of(args[0])); + String[] root = lines.get(0).split(" "); + List packs = new ArrayList<>(); + packs.add(new PathPackResources( + new PackLocationInfo("vanilla", Component.literal("vanilla"), PackSource.BUILT_IN, Optional.empty()), Path.of(root[1]))); + var repository = new PackRepository(new FolderRepositorySource( + Path.of(root[2]), PackType.CLIENT_RESOURCES, PackSource.DEFAULT, new DirectoryValidator(path -> true))); + repository.reload(); + for (int i = 3; i < root.length; i++) { + repository.getPack(root[i]).open().forEach(packs::add); + } + var manager = new MultiPackResourceManager(PackType.CLIENT_RESOURCES, packs); + ClientLanguage language = ClientLanguage.loadFrom(manager, List.of("en_us"), false); + Object unsafe = theUnsafe(); + SoundManager sounds = (SoundManager) unsafe.getClass().getMethod("allocateInstance", Class.class) + .invoke(unsafe, SoundManager.class); + Method prepare = SoundManager.class.getDeclaredMethod("prepare", + net.minecraft.server.packs.resources.ResourceManager.class, net.minecraft.util.profiling.ProfilerFiller.class); + prepare.setAccessible(true); + Object preparations = prepare.invoke(sounds, manager, InactiveProfiler.INSTANCE); + var registryField = preparations.getClass().getDeclaredField("registry"); + registryField.setAccessible(true); + @SuppressWarnings("unchecked") + Map registry = (Map) registryField.get(preparations); + Method fontStack = net.minecraft.client.gui.font.FontManager.class.getDeclaredMethod("loadResourceStack", List.class, Identifier.class); + fontStack.setAccessible(true); + + StringBuilder out = new StringBuilder(); + for (String line : lines.subList(1, lines.size())) { + String[] q = line.split(" ", 2); + out.append(line).append(" =>"); + switch (q[0]) { + case "file" -> { + Optional resource = manager.getResource(Identifier.parse(q[1])); + if (resource.isEmpty()) { + out.append(" missing"); + } else { + try (InputStream in = resource.get().open()) { + out.append(' ').append(Long.toHexString(fnv(in.readAllBytes()))); + } + } + } + case "lang" -> out.append(' ').append(language.getOrDefault(q[1])); + case "sound" -> { + WeighedSoundEvents event = registry.get(Identifier.parse(q[1])); + if (event == null) { + out.append(" missing"); + break; + } + Component subtitle = event.getSubtitle(); + out.append(" subtitle=").append(subtitle == null ? "none" + : ((TranslatableContents) subtitle.getContents()).getKey()); + var listField = WeighedSoundEvents.class.getDeclaredField("list"); + listField.setAccessible(true); + for (Object weighted : (List) listField.get(event)) { + out.append(' ').append(weighted instanceof Sound sound + ? sound.getLocation() + "*" + sound.getWeight() : "event"); + } + } + case "font" -> { + Identifier id = Identifier.parse(q[1]); + List stack = manager.getResourceStack(id.withPath(p -> "font/" + p + ".json")); + List builders = new ArrayList<>((List) fontStack.invoke(null, stack, id)); + java.util.Collections.reverse(builders); + for (Object pair : builders) { + var conditional = (GlyphProviderDefinition.Conditional) ((com.mojang.datafixers.util.Pair) pair).getSecond(); + JsonElement json = GlyphProviderDefinition.Conditional.CODEC.encodeStart(JsonOps.INSTANCE, conditional).getOrThrow(); + out.append(' ').append(strip(json.getAsJsonObject().get("type").getAsString())); + } + } + case "atlas" -> { + SpriteSourceList list = SpriteSourceList.load(manager, Identifier.parse(q[1])); + var sourcesField = SpriteSourceList.class.getDeclaredField("sources"); + sourcesField.setAccessible(true); + @SuppressWarnings("unchecked") + var sources = (List) sourcesField.get(list); + JsonArray json = SpriteSources.FILE_CODEC.encodeStart(JsonOps.INSTANCE, sources).getOrThrow() + .getAsJsonObject().getAsJsonArray("sources"); + for (JsonElement source : json) { + out.append(' ').append(strip(source.getAsJsonObject().get("type").getAsString())); + } + } + default -> throw new IllegalArgumentException(line); + } + out.append('\n'); + } + Files.writeString(Path.of(args[1]), out); + } + + private static String strip(String type) { + return type.startsWith("minecraft:") ? type.substring("minecraft:".length()) : type; + } + + /** FNV-1a 64 of a file's bytes. */ + private static long fnv(byte[] bytes) { + long hash = 0xcbf29ce484222325L; + for (byte b : bytes) { + hash = (hash ^ (b & 0xff)) * 0x100000001b3L; + } + return hash; + } + + private static Object theUnsafe() throws Exception { + Class unsafeClass = Class.forName("sun.misc.Unsafe"); + var field = unsafeClass.getDeclaredField("theUnsafe"); + field.setAccessible(true); + return field.get(null); + } +} diff --git a/tools/test_resource_pack_parity.py b/tools/test_resource_pack_parity.py new file mode 100644 index 000000000..87d173628 --- /dev/null +++ b/tools/test_resource_pack_parity.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python3 +"""Compare the resources a selection of resource packs gives the client with 26.3. + +Builds test packs in a folder (a folder pack and a zip pack, with pack.mcmeta +overlays for this and an older format, a filter, block textures, language +files, sounds.json with added and replaced events, font providers and atlas +sources) and selects them over the prepared vanilla resources. The Java oracle +(VanillaPackResourcesOracle) opens them with vanilla's FolderRepositorySource +and MultiPackResourceManager and asks what each reload listener reads: which +pack a file comes from, translations (ClientLanguage), sound events +(SoundManager.prepare), font providers (FontManager) and atlas sources +(SpriteSourceList). examples/pack_resources_fixture builds Horizon's resource +root for the same selection (packs::build_overlay) and reads it with Horizon's +loaders. Every answer must agree. +""" +import json +import shutil +import zipfile + +try: + from . import lab, oracle_harness as harness +except ImportError: + import lab, oracle_harness as harness + +FORMAT = {"min_format": [97, 0], "max_format": [97, 1]} + + +def write(root, files): + for name, data in files.items(): + path = root / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(data if isinstance(data, bytes) else json.dumps(data).encode()) + + +def packs(folder): + shutil.rmtree(folder, ignore_errors=True) + folder.mkdir(parents=True) + write(folder / "Alpha", { + "pack.mcmeta": {"pack": {"description": "alpha", **FORMAT}, "overlays": {"entries": [ + {"directory": "alpha_now", **FORMAT}, + {"directory": "alpha_old", "min_format": [80, 0], "max_format": [80, 0]}]}}, + "assets/minecraft/textures/block/stone.png": b"alpha stone", + "assets/minecraft/textures/block/granite.png": b"alpha granite", + "alpha_now/assets/minecraft/textures/block/dirt.png": b"alpha overlay dirt", + "alpha_old/assets/minecraft/textures/block/sand.png": b"old overlay sand", + "assets/minecraft/lang/en_us.json": {"block.minecraft.stone": "Alpha Stone", "horizon.k1": "A1", + "horizon.k2": "A2"}, + "assets/minecraft/sounds.json": { + "horizon.test": {"subtitle": "subtitles.horizon.a", "sounds": ["horizon/a1"]}, + "block.stone.break": {"sounds": [{"name": "horizon/a2", "weight": 2}]}, + "horizon.ref": {"sounds": [{"name": "horizon.test", "type": "event"}]}}, + "assets/minecraft/sounds/horizon/a1.ogg": b"a1", + "assets/minecraft/sounds/horizon/a2.ogg": b"a2", + "assets/minecraft/font/default.json": {"providers": [{"type": "space", "advances": {" ": 9}}]}, + "assets/minecraft/atlases/blocks.json": {"sources": [{"type": "single", "resource": "horizon:block/a"}]}, + "assets/horizon/lang/en_us.json": {"horizon.ns": "Alpha namespace"}, + }) + beta = folder / "beta-src" + write(beta, { + "pack.mcmeta": {"pack": {"description": "beta", **FORMAT}, + "filter": {"block": [{"namespace": "minecraft", "path": "textures/block/stone\\.png"}]}}, + "assets/minecraft/textures/block/granite.png": b"beta granite", + "assets/minecraft/lang/en_us.json": {"horizon.k1": "B1"}, + "assets/minecraft/sounds.json": { + "horizon.test": {"replace": True, "subtitle": "subtitles.horizon.b", + "sounds": ["horizon/b1", {"name": "horizon/a1", "weight": 3}]}, + "block.stone.break": {"sounds": ["horizon/missing"]}}, + "assets/minecraft/sounds/horizon/b1.ogg": b"b1", + "assets/minecraft/font/default.json": {"providers": [{"type": "space", "advances": {" ": 4}}, + {"type": "space", "advances": {" ": 5}}]}, + "assets/minecraft/atlases/blocks.json": {"sources": [{"type": "single", "resource": "horizon:block/b"}]}, + }) + # Gamma: a sounds.json Gson rejects (a bad entry) is skipped whole. + write(folder / "Gamma", { + "pack.mcmeta": {"pack": {"description": "gamma", **FORMAT}}, + "assets/minecraft/sounds.json": {"horizon.test": {"sounds": ["horizon/a1"]}, "bad": 3}, + "assets/minecraft/lang/en_us.json": {"horizon.k2": "G2"}, + }) + with zipfile.ZipFile(folder / "Beta.zip", "w") as z: + for path in sorted(beta.rglob("*")): + if path.is_file(): + z.write(path, path.relative_to(beta).as_posix()) + shutil.rmtree(beta) + + +QUERIES = [ + "file minecraft:textures/block/stone.png", + "file minecraft:textures/block/granite.png", + "file minecraft:textures/block/dirt.png", + "file minecraft:textures/block/sand.png", + "file minecraft:textures/block/oak_planks.png", + "lang block.minecraft.stone", + "lang block.minecraft.dirt", + "lang horizon.k1", + "lang horizon.k2", + "lang horizon.ns", + "lang menu.singleplayer", + "sound horizon.test", + "sound horizon.ref", + "sound block.stone.break", + "sound block.stone.place", + "font minecraft:default", + "font minecraft:uniform", + "atlas minecraft:blocks", + "atlas minecraft:gui", +] +SELECTIONS = [ + ["file/Alpha", "file/Beta.zip", "file/Gamma"], + ["file/Beta.zip", "file/Alpha"], + ["file/Alpha"], + [], +] + + +def main(): + total = 0 + for index, selection in enumerate(SELECTIONS): + # One run per selection: the input's first line names the packs, bottom first. + def make(build, selection=selection): + folder = build / "resourcepacks" + packs(folder) + vanilla = lab.LOCAL / "horizon-client" / "26.3" + path = build / "inputs.txt" + path.write_text(f"root {vanilla} {folder} {' '.join(selection)}".rstrip() + "\n" + + "".join(q + "\n" for q in QUERIES)) + return path, len(QUERIES) + harness.differential(f"resource-pack-parity-{index}", "VanillaPackResourcesOracle", + "pack_resources_fixture", make, + "Pack discovery, overlays, filters and the resource stacks vanilla merges.", + sources=[lab.ROOT / "crates/horizon-client/src/packs.rs", + lab.ROOT / "crates/horizon-client/src/sound_events.rs", + lab.ROOT / "crates/horizon-client/src/lang.rs"]) + total += len(QUERIES) + print(f"{total} resource pack queries match the pinned client over {len(SELECTIONS)} selections") + + +if __name__ == "__main__": + main()