diff --git a/crates/horizon-client/src/mod_content.rs b/crates/horizon-client/src/mod_content.rs index f1ecfa8d2..0497f06b4 100644 --- a/crates/horizon-client/src/mod_content.rs +++ b/crates/horizon-client/src/mod_content.rs @@ -874,7 +874,7 @@ struct Known { tag_files: std::collections::BTreeSet, } impl Known { - fn new(base: &Path, plan: &Plan, tags: &BTreeMap>) -> Result { + fn new(base: &Path, plan: &Plan, tags: &BTreeMap) -> Result { let mut blocks: std::collections::BTreeSet = plan.defaults.keys().cloned().collect(); blocks.extend(plan.blocks.iter().map(|b| b.decl.id.clone())); @@ -945,8 +945,60 @@ impl Known { } } -/// Builds the content root for `mods` over `base` in `out`. -pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { +/// What the mods do to one tag file, in load order (a data pack's +/// TagLoader): each adds its values, and one that replaces the tag first +/// drops vanilla's entries and the mods' before it. +#[derive(Default)] +struct TagMerge { + /// A mod replaced the tag: vanilla's entries are left out. + replaced: bool, + /// The mods whose values are in `values`. + mods: Vec, + values: Vec, +} + +impl TagMerge { + /// Adds one mod's tag file. A replace that drops entries (vanilla's in a + /// `shared` tag, one not in the mod's namespaces, or other mods') warns. + fn add( + &mut self, + id: &str, + file: &str, + replace: bool, + shared: bool, + values: &[J], + ) -> Option { + let mut warning = None; + if replace { + let mut dropped: Vec = Vec::new(); + if shared && !self.replaced { + dropped.push("vanilla's entries".into()); + } + if !self.mods.is_empty() { + dropped.push(format!("those of {}", self.mods.join(", "))); + } + if !dropped.is_empty() { + warning = Some(format!( + "mod {id} replaces {file}: {} are left out, as a data pack's replace \ + does; the mods after it in load order still add to it", + dropped.join(" and ") + )); + } + self.replaced = true; + self.mods.clear(); + self.values.clear(); + } + if !self.mods.iter().any(|m| m == id) { + self.mods.push(id.to_owned()); + } + self.values.extend(values.iter().cloned()); + warning + } +} + +/// Builds the content root for `mods` over `base` in `out`. Returns the +/// warnings for the log: the shared tags a mod replaced. +pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result> { // Two mods owning one namespace would mix their files. for (i, a) in mods.iter().enumerate() { for b in &mods[..i] { @@ -986,7 +1038,8 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { overlay.write(name, &bytes)?; } // Mod files, and every tag addition gathered per tag file. - let mut tags: BTreeMap> = BTreeMap::new(); + let mut tags: BTreeMap = BTreeMap::new(); + let mut warnings = Vec::new(); let mut worldgen: BTreeMap<&str, Vec<(String, String)>> = BTreeMap::new(); let mut additions: BTreeMap<&str, Vec> = BTreeMap::new(); for m in mods { @@ -996,19 +1049,14 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { let is_tag = file.starts_with("data/") && file.contains("/tags/"); if is_tag && file.ends_with(".json") { let doc: J = serde_json::from_slice(&bytes).map_err(|e| format!("{file}: {e}"))?; - if doc.get("replace") == Some(&J::Bool(true)) { - let e = format!( - "{}: {file}: a mod adds to tags, it cannot replace them", - m.id - ); - return Err(e.into()); - } let Some(J::Arr(values)) = doc.get("values") else { return Err(format!("{}: {file}: no values", m.id).into()); }; - tags.entry(file.clone()) - .or_default() - .extend(values.iter().cloned()); + let replace = doc.get("replace") == Some(&J::Bool(true)); + let ns = file.split('/').nth(1).unwrap_or_default(); + let shared = !m.content.namespaces.iter().any(|n| n == ns); + let merge = tags.entry(file.clone()).or_default(); + warnings.extend(merge.add(&m.id, file, replace, shared, values)); } else if !file.starts_with(&declarations) { overlay.write(file, &bytes)?; if let Some((bundle, id)) = worldgen_entry(file) { @@ -1034,8 +1082,8 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { // carried-over mods name blocks left out or other mods' ids: such // entries are dropped instead, so the rest of the tag holds. let known = Known::new(base, &plan, &tags)?; - for (file, values) in &mut tags { - values.retain(|v| known.keeps(file, v)); + for (file, merge) in &mut tags { + merge.values.retain(|v| known.keeps(file, v)); } let mut bundles: Vec<(&str, J)> = Vec::new(); for name in TAG_BUNDLES { @@ -1043,11 +1091,13 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { bundles.push((name, read_json(&base.join(name))?)); } } - for (file, values) in &tags { - let vanilla = fs::read_to_string(base.join(file)).ok(); + for (file, merge) in &tags { + let vanilla = fs::read_to_string(base.join(file)) + .ok() + .filter(|_| !merge.replaced); overlay.write( file, - merge_tag(vanilla.as_deref(), values, file)?.as_bytes(), + merge_tag(vanilla.as_deref(), &merge.values, file)?.as_bytes(), )?; // data//tags/block/.json is the block tag :. let Some((ns, path)) = file @@ -1060,10 +1110,10 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { let id = format!("{ns}:{path}"); for (name, doc) in &mut bundles { let text = match doc.get(&id) { - Some(J::Str(t)) => Some(t.clone()), + Some(J::Str(t)) if !merge.replaced => Some(t.clone()), _ => None, }; - let merged = merge_tag(text.as_deref(), values, name)?; + let merged = merge_tag(text.as_deref(), &merge.values, name)?; doc.set(&id, J::Str(merged)); } } @@ -1071,23 +1121,24 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { overlay.write(name, &serde_json::to_vec(doc)?)?; } // Biome tags, merged into the worldgen loader's bundle as block tags are. - let biome_tags: Vec<(String, &Vec)> = tags + let biome_tags: Vec<(String, &TagMerge)> = tags .iter() - .filter_map(|(file, values)| { + .filter_map(|(file, merge)| { let (ns, path) = file .strip_prefix("data/")? .split_once("/tags/worldgen/biome/")?; - Some((format!("{ns}:{}", path.strip_suffix(".json")?), values)) + Some((format!("{ns}:{}", path.strip_suffix(".json")?), merge)) }) .collect(); if !biome_tags.is_empty() && base.join(BIOME_TAGS).exists() { let mut doc = read_json(&base.join(BIOME_TAGS))?; - for (id, values) in biome_tags { + for (id, merge) in biome_tags { let text = match doc.get(&id) { - Some(J::Str(t)) => Some(t.clone()), + Some(J::Str(t)) if !merge.replaced => Some(t.clone()), _ => None, }; - doc.set(&id, J::Str(merge_tag(text.as_deref(), values, BIOME_TAGS)?)); + let merged = merge_tag(text.as_deref(), &merge.values, BIOME_TAGS)?; + doc.set(&id, J::Str(merged)); } overlay.write(BIOME_TAGS, &serde_json::to_vec(&doc)?)?; } @@ -1128,7 +1179,8 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { .collect(); namespaces.sort(); overlay.write(MARKER, &serde_json::to_vec(&namespaces)?)?; - Ok(()) + overlay.write(WARNINGS, warnings.join("\n").as_bytes())?; + Ok(warnings) } /// The root to load: `base` itself with no content, else the mods' overlay @@ -1144,13 +1196,21 @@ pub fn resource_root(base: &Path, mods: &[ContentMod], cache: &Path) -> Result

Vec { ids.iter().map(|i| J::Str((*i).into())).collect() }; + let file = "data/minecraft/tags/worldgen/biome/has_structure/village_plains.json"; + let mut merge = TagMerge::default(); + assert_eq!(merge.add("a", file, false, true, &v(&["a:x"])), None); + let warning = merge.add("b", file, true, true, &v(&["minecraft:plains"])); + assert_eq!( + warning.as_deref(), + Some( + "mod b replaces data/minecraft/tags/worldgen/biome/has_structure/village_plains.json: \ + vanilla's entries and those of a are left out, as a data pack's replace does; \ + the mods after it in load order still add to it" + ) + ); + assert_eq!(merge.add("c", file, false, true, &v(&["c:y"])), None); + assert!(merge.replaced); + assert_eq!(merge.values, v(&["minecraft:plains", "c:y"])); + // A second replace drops only the mods' entries. + let warning = merge.add("d", file, true, true, &v(&["d:z"])).unwrap(); + assert!( + warning.contains(": those of b, c are left out"), + "{warning}" + ); + assert_eq!(merge.values, v(&["d:z"])); + // A mod replacing a tag of its own that nobody added to drops nothing. + let own = "data/a/tags/item/gems.json"; + assert_eq!( + TagMerge::default().add("a", own, true, false, &v(&["a:x"])), + None + ); +} + +/// A replacing mod's tag leaves vanilla's entries out of the tag file and of +/// the worldgen loader's biome tags, and the root keeps the warning for the +/// starts that reuse it. +#[test] +fn a_replacing_mod_tag_reaches_the_root() { + let Some(base) = prepared() else { + return; + }; + let scratch = std::env::temp_dir().join(format!("horizon-mod-replace-{}", std::process::id())); + let _ = fs::remove_dir_all(&scratch); + let file = "data/minecraft/tags/worldgen/biome/has_structure/village_plains.json"; + let adds = folder_mod( + &scratch.join("adds"), + &[ + ("horizon-mod.toml", &manifest("adds", "")), + (file, r#"{"values": ["minecraft:forest"]}"#), + ], + ); + let towns = folder_mod( + &scratch.join("towns"), + &[ + ("horizon-mod.toml", &manifest("towns", "")), + (file, r#"{"replace": true, "values": ["minecraft:plains"]}"#), + ], + ); + let late = folder_mod( + &scratch.join("late"), + &[ + ("horizon-mod.toml", &manifest("late", "")), + (file, r#"{"values": ["minecraft:savanna"]}"#), + ], + ); + let vanilla: serde_json::Value = + serde_json::from_slice(&fs::read(base.join(file)).unwrap()).unwrap(); + assert!(vanilla["values"].as_array().unwrap().len() > 1, "{vanilla}"); + let root = resource_root(&base, &[adds, towns, late], &scratch.join("cache")).unwrap(); + let expected = serde_json::json!(["minecraft:plains", "minecraft:savanna"]); + let tag: serde_json::Value = + serde_json::from_slice(&fs::read(root.join(file)).unwrap()).unwrap(); + assert_eq!(tag["values"], expected); + let tags: BTreeMap = + serde_json::from_slice(&fs::read(root.join("worldgen/biome_tags.json")).unwrap()).unwrap(); + let bundled: serde_json::Value = + serde_json::from_str(&tags["minecraft:has_structure/village_plains"]).unwrap(); + assert_eq!(bundled["values"], expected); + let warnings = fs::read_to_string(root.join(WARNINGS)).unwrap(); + assert!( + warnings.starts_with("mod towns replaces data/minecraft/tags/worldgen/biome/has_structure/village_plains.json: vanilla's entries and those of adds are left out"), + "{warnings}" + ); + let _ = fs::remove_dir_all(&scratch); +} diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index de987df2c..00cab9aa7 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -24,7 +24,7 @@ pub struct Report { pub outside: Vec, /// Files of the mod's own content the game would refuse, kept in /// `outside/` too, with why: a path vanilla does not read (upper case), - /// JSON that does not parse, a vanilla tag the mod replaces. + /// JSON that does not parse. pub unreadable: BTreeMap, pub recipes_upgraded: usize, pub loot_tables_upgraded: usize, @@ -445,13 +445,12 @@ fn unreadable(name: &str, bytes: &mut Vec) -> Option { Err(_) => return Some(format!("does not parse as JSON: {e}")), }, }; + // A vanilla or convention tag that replaces is kept: the game honours it + // as a data pack's, with a warning (docs/mods.md). A tag of the mod's own + // that replaces: no pack before it defines the tag, so it is the same tag + // added to (Towns and Towers' biome tags). let shared_tag = name.starts_with("data/minecraft/tags/") || name.starts_with("data/c/tags/"); - if shared_tag && value.get("replace").and_then(Value::as_bool) == Some(true) { - return Some("replaces a shared tag; a content mod may only add to it".into()); - } - // A tag of the mod's own that replaces: no pack before it defines the - // tag, so it is the same tag added to (Towns and Towers' biome tags). - let own_tag = name.starts_with("data/") && name.contains("/tags/"); + let own_tag = !shared_tag && name.starts_with("data/") && name.contains("/tags/"); if own_tag && let Some(o) = value.as_object_mut().filter(|o| o.contains_key("replace")) { o.remove("replace"); *bytes = serde_json::to_vec_pretty(&value).expect("a parsed value serializes"); @@ -1180,13 +1179,13 @@ mod tests { "location": "gem:hut", "projection": "rigid", "processors": {"processors": [{"processor_type": "lithostitched:block_swap"}]}}}]}"#, ), - // What the game would refuse: vanilla skips these files alone. - ("assets/gem/lang/ry_UA.json", r#"{"item.gem.gem": "Gem"}"#), - ("data/gem/tags/item/empty.json", ""), ( "data/minecraft/tags/worldgen/biome/has_structure/village_plains.json", r#"{"replace": true, "values": []}"#, ), + // What the game would refuse: vanilla skips these files alone. + ("assets/gem/lang/ry_UA.json", r#"{"item.gem.gem": "Gem"}"#), + ("data/gem/tags/item/empty.json", ""), ( "assets/gem/lang/de_de.json", "\u{feff}{\"item.gem.gem\": \"Juwel\"}", @@ -1219,17 +1218,25 @@ mod tests { &BTreeMap::new(), ) .unwrap(); - assert_eq!(report.copied, 14); + assert_eq!(report.copied, 15); let unreadable: Vec<&str> = report.unreadable.keys().map(String::as_str).collect(); assert_eq!( unreadable, [ "assets/gem/lang/ry_UA.json", - "data/gem/tags/item/empty.json", - "data/minecraft/tags/worldgen/biome/has_structure/village_plains.json", + "data/gem/tags/item/empty.json" ] ); - assert_eq!(report.outside.len(), 3); + assert_eq!(report.outside.len(), 2); + // A vanilla tag that replaces is kept as it is: the game honours it. + let replacing: Value = serde_json::from_slice( + &fs::read(out.join( + "carry/data/minecraft/tags/worldgen/biome/has_structure/village_plains.json", + )) + .unwrap(), + ) + .unwrap(); + assert_eq!(replacing, json!({"replace": true, "values": []})); assert!(!out.join("carry/data/gem/tags/item/empty.json").exists()); // A byte-order mark is dropped, the file kept. assert_eq!( diff --git a/docs/modpacks.md b/docs/modpacks.md index 86083be5a..a6bbd5402 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -325,8 +325,10 @@ into `mods//carry/`, a folder content mod (API 0.2) the game loads with `--d keeps what a content mod may not hold (other namespaces, vanilla data) in `outside/`. Files of the mod's own content that the game's content loader would refuse whole also go to `outside/`, listed with the reason in the report's `unreadable`: paths vanilla does not read (upper case), -JSON that does not parse, and shared tags with `"replace": true`. Vanilla skips them one by one. -A byte-order mark is dropped. For ATM10's 42 mods that is 19 files in 7 mods, and the 42 load as +and JSON that does not parse. Vanilla skips them one by one. A vanilla or convention tag +with `"replace": true` is carried as it is: the game honours it as a data pack's, with a warning +(docs/mods.md); Towns and Towers' seven `minecraft:has_structure/*` biome tags are such. +A byte-order mark is dropped. For ATM10's 42 mods that is 6 files in 4 mods, and the 42 load as carried. No class file is copied. It upgrades from 1.21.1 to 26.3: vanilla-type recipes (ingredients), loot tables (`conditions` lists become one `condition`, `functions` become `modifier`, kinds move to `type`, tool matches on silk touch or shears become `minecraft:tool/can_silk_touch` and diff --git a/docs/mods.md b/docs/mods.md index 758ac7af4..bc179b8f6 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -307,6 +307,17 @@ its row. The game reads this content into its block and item tables nobody registers (a block a port left out, another mod's item) is dropped, so the rest of the tag still loads; vanilla would drop the whole tag. +Tags merge as a data pack's do. Every mod's file for one tag (vanilla's +`data/minecraft/tags/`, the convention `data/c/tags/`, or another mod's) adds +its values after vanilla's and the mods' before it, in load order. A file with +`"replace": true` first drops what came before it, vanilla's entries and the +earlier mods', and the mods after it still add theirs, as a higher data pack +does. Since the result depends on load order, the game says so in the log: +"mod towns replaces data/minecraft/tags/worldgen/biome/has_structure/village_plains.json: +vanilla's entries and those of adds are left out, ...". Tags are the only +`minecraft` files a mod may hold: its other content stays in its own +namespaces. + A mod adds to structure sets and template pools that are not its own (vanilla's villages, another mod's houses) with a set or pool file in its own namespace holding `horizon:add_to`, a key vanilla data never has. Its entries