Merge pull request #85 from ludovic111/fix/resource-packs-72

Resource packs: reload everything in game and merge stacked files like vanilla (fixes #72)
This commit is contained in:
Ludovic Marie
2026-10-14 15:44:44 +02:00
committed by GitHub
14 changed files with 960 additions and 166 deletions
@@ -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<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(())
}
+33 -4
View File
@@ -180,15 +180,43 @@ impl Assets {
.collect::<Result<_>>()?,
));
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> {
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<String>,
) -> Result<Self> {
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,
+10
View File
@@ -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`).
+58 -35
View File
@@ -6649,6 +6649,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<Window>) -> Result<Renderer> {
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;
@@ -7796,21 +7815,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());
@@ -8369,6 +8374,25 @@ impl ApplicationHandler for Game<'_> {
}
}
}
/// clouds.png as filled cells (`CloudRenderer.prepare`: alpha 10 or more).
fn load_clouds(root: &std::path::Path) -> (u32, u32, Vec<bool>) {
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<u8>) {
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(());
@@ -8381,10 +8405,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);
@@ -8485,23 +8523,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()
+2 -4
View File
@@ -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);
}
}
}
+69 -53
View File
@@ -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(())
}
}
+5 -2
View File
@@ -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 {
+26 -7
View File
@@ -32,11 +32,24 @@ static CURRENT: RwLock<Option<Arc<Language>>> = 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<Vec<u8>>;
/// The namespaces under `assets/` (resource packs may add their own).
fn namespaces(&self) -> Vec<String> {
vec!["minecraft".into()]
}
}
impl ResourceSource for std::path::PathBuf {
fn read(&self, path: &str) -> Option<Vec<u8>> {
std::fs::read(self.join(path)).ok()
}
fn namespaces(&self) -> Vec<String> {
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<String> =
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::<HashMap<String, String>>(&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::<HashMap<String, String>>(&bytes) {
map.extend(entries);
}
}
}
}
+208 -5
View File
@@ -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<String, (usize, Provided)> = BTreeMap::new();
// Every pack's copy of the files vanilla reads as a resource stack.
let mut stacks: BTreeMap<String, Vec<(usize, Provided)>> = 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<String, Vec<u8>> = 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<String>,
files: &BTreeMap<String, (usize, Provided)>,
merged: &BTreeMap<String, Vec<u8>>,
filters: &[&Filter],
zips: &[Option<Zip>],
) -> 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<Merge> {
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<u8>]) -> Option<Vec<u8>> {
use serde_json::{Map, Value};
let parsed: Vec<Map<String, Value>> = 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<Item = &Map<String, Value>>| {
let items: Vec<Value> = 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<Pack> {
@@ -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<u8>| serde_json::from_slice::<serde_json::Value>(&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);
+38 -22
View File
@@ -106,6 +106,42 @@ pub struct WeighedSoundEvents {
/// The subtitle translation key.
pub subtitle: Option<String>,
}
/// A `sounds.json` entry: event, `replace`, subtitle and sounds.
type Registration = (String, bool, Option<String>, Vec<Sound>);
/// 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<Vec<Registration>> {
let Ok(Value::Object(map)) = serde_json::from_slice::<Value>(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::<Value>(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,
+86 -10
View File
@@ -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<u8>,
pub layers: BTreeMap<String, u32>,
pub animations: Vec<SpriteAnimation>,
pub opaque: Vec<bool>,
pub translucent: Vec<bool>,
/// Each layer's frames at the texture's own resolution (square), and the
/// frame shown first.
native: Vec<(u32, Arc<Vec<Vec<u8>>>, 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<u8> {
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<Vec<Vec<u8>>> =
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<u32> {
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<u8> = (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<u32> {
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)
}
}
+35 -24
View File
@@ -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<BinaryHeap<Queued>>,
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<Arc<WorkerContext>>,
}
pub struct Streaming {
shared: Arc<Shared>,
@@ -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(&region), &context.light, sky),
seq,
),
Job::Light(x, z, region, seq, sky) => {
Done::Lit(x, z, light_chunk(&borrow(&region), &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(&region), 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<WorkerContext>) {
*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);
@@ -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<String> lines = Files.readAllLines(Path.of(args[0]));
String[] root = lines.get(0).split(" ");
List<PackResources> 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<Identifier, WeighedSoundEvents> registry = (Map<Identifier, WeighedSoundEvents>) 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> 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<Resource> 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<net.minecraft.client.renderer.texture.atlas.SpriteSource>) 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);
}
}
+138
View File
@@ -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()