diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index 42c457f02..a637d6246 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -3,7 +3,7 @@ //! upgraded from 1.21.1 to 26.3, item models get their `items/` client //! definitions, and each blockstate gets a vanilla template guessed from its //! properties until the oracle's facts replace the guess. -use crate::jar::JarInfo; +use crate::{jar::JarInfo, oracle::Facts}; use serde::Serialize; use serde_json::{Value, json}; use std::{ @@ -20,14 +20,26 @@ pub struct Report { /// data other than tags), kept in `outside/` for review. pub outside: Vec, pub recipes_upgraded: usize, + pub loot_tables_upgraded: usize, + /// Worldgen and structure files (`data//worldgen/`, `structure/`). + pub worldgen: usize, /// Recipes left as they were: custom types, NeoForge conditions. pub recipes_kept: Vec, pub item_definitions_added: usize, - /// Declared blocks and their guessed templates. + /// Templates and properties came from the NeoForge oracle's facts, not + /// from guesses. + pub oracle: bool, + /// Declared blocks and their templates. pub blocks: BTreeMap, - /// Blockstates whose properties match no template. - pub blocks_unknown: Vec, + /// What a declared block's template does not reproduce (oracle only). + pub differences: BTreeMap>, + /// Blocks left out, and why. + pub blocks_unknown: BTreeMap, pub items: usize, + /// Items left out, and why (oracle only). + pub items_unknown: BTreeMap, + /// Convention (`c:`) tags the recipes use, filled in from the oracle. + pub convention_tags: usize, } /// Vanilla blocks by their sorted property names. @@ -42,6 +54,7 @@ const TEMPLATES: &[(&str, &str)] = &[ "minecraft:oak_trapdoor", ), ("east,north,south,waterlogged,west", "minecraft:oak_fence"), + ("east,north,south,waterlogged,west", "minecraft:glass_pane"), ( "east,north,south,up,waterlogged,west", "minecraft:cobblestone_wall", @@ -58,9 +71,24 @@ const TEMPLATES: &[(&str, &str)] = &[ ("stage", "minecraft:oak_sapling"), ]; -pub fn template(properties: &BTreeSet) -> Option<&'static str> { - let key = properties.iter().cloned().collect::>().join(","); - TEMPLATES.iter().find(|(k, _)| *k == key).map(|(_, t)| *t) +/// A guess without the oracle: the template whose properties include the +/// blockstate's with the fewest left over (a blockstate may leave out what +/// its models ignore, such as `powered` or `waterlogged`), the block's name +/// breaking ties (`_pane`, `_gate`, `_trapdoor`). +pub fn template(name: &str, properties: &BTreeSet) -> Option<&'static str> { + let fits = TEMPLATES.iter().filter_map(|(k, t)| { + let have: BTreeSet<&str> = k.split(',').filter(|p| !p.is_empty()).collect(); + properties + .iter() + .all(|p| have.contains(p.as_str())) + .then(|| (have.len() - properties.len(), *t)) + }); + let hint = |t: &str| { + let kind = t.rsplit('_').next().unwrap_or(t); + !name.ends_with(&format!("_{kind}")) + }; + fits.min_by_key(|&(extra, t)| (extra, hint(t))) + .map(|(_, t)| t) } /// The property names a blockstate file uses. @@ -165,7 +193,92 @@ pub fn upgrade_recipe(recipe: &mut Value) -> bool { true } -/// Where a jar entry goes: `Some(true)` inside the content mod, `Some(false)` +/// A 1.21.1 loot table in 26.3's shape, in place: `conditions` lists become +/// one `condition` (`all_of` for several), `functions` become `modifier`, +/// and conditions and functions name their kind in `type`. A tool match on +/// silk touch alone, or on shears alone, becomes 26.3's +/// `minecraft:tool/can_silk_touch` or `minecraft:tool/can_shear`. +pub fn upgrade_loot(v: &mut Value) { + match v { + Value::Array(a) => a.iter_mut().for_each(upgrade_loot), + Value::Object(o) => { + if let Some(Value::Array(mut list)) = o.remove("conditions") { + list.iter_mut().for_each(upgrade_condition); + let one = match <[Value; 1]>::try_from(list) { + Ok([c]) => c, + Err(list) => json!({"type": "minecraft:all_of", "terms": list}), + }; + o.insert("condition".to_owned(), one); + } + if let Some(Value::Array(mut list)) = o.remove("functions") { + for f in &mut list { + rename_kind(f, "function"); + upgrade_loot(f); + } + o.insert("modifier".to_owned(), Value::Array(list)); + } + for (k, x) in o.iter_mut() { + if k != "condition" && k != "modifier" { + upgrade_loot(x); + } + } + } + _ => {} + } +} + +/// `{"": kind, ...}` as `{"type": kind, ...}`. +fn rename_kind(v: &mut Value, key: &str) { + if let Some(o) = v.as_object_mut() + && !o.contains_key("type") + && let Some(kind) = o.remove(key) + { + o.insert("type".to_owned(), kind); + } +} + +/// One 1.21.1 loot condition in 26.3's shape. +fn upgrade_condition(c: &mut Value) { + rename_kind(c, "condition"); + if let Some(term) = c.get_mut("term") { + upgrade_condition(term); + } + if let Some(Value::Array(terms)) = c.get_mut("terms") { + terms.iter_mut().for_each(upgrade_condition); + } + if c["type"] == "minecraft:match_tool" + && let Some(named) = named_tool_condition(&c["predicate"]) + { + *c = json!(named); + } +} + +/// 26.3's named tool conditions for the 1.21.1 predicates they replace. +fn named_tool_condition(predicate: &Value) -> Option<&'static str> { + let keys: Vec<&str> = predicate.as_object()?.keys().map(String::as_str).collect(); + match keys.as_slice() { + ["predicates"] => { + let list = predicate["predicates"]["minecraft:enchantments"].as_array()?; + let [e] = list.as_slice() else { + return None; + }; + let silk = e["enchantments"] == "minecraft:silk_touch" + && e["levels"]["min"].as_u64().unwrap_or(1) <= 1 + && e["levels"].get("max").is_none() + && predicate["predicates"].as_object()?.len() == 1; + silk.then_some("minecraft:tool/can_silk_touch") + } + ["items"] => { + let items = &predicate["items"]; + let shears = items == "minecraft:shears" || *items == json!(["minecraft:shears"]); + shears.then_some("minecraft:tool/can_shear") + } + _ => None, + } +} + +/// Where a jar entry goes: `Some(true)` inside the content mod (its own +/// namespace, additions to vanilla and convention tags), `Some(false)` /// to `outside/`, `None` not carried (code, metadata). fn place(name: &str, id: &str) -> Option { let mut parts = name.splitn(3, '/'); @@ -173,10 +286,21 @@ fn place(name: &str, id: &str) -> Option { if !(root == "assets" || root == "data") || rest.is_empty() || name.ends_with('/') { return None; } - Some(ns == id || (root == "data" && ns == "minecraft" && rest.starts_with("tags/"))) + let shared_tags = + root == "data" && (ns == "minecraft" || ns == "c") && rest.starts_with("tags/"); + Some(ns == id || shared_tags) } -pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { +/// Carries `jar` over into `out/carry/`. With the oracle's `facts`, blocks +/// and items are the ones the mod registers, with the templates and numbers +/// the facts give; `allowed` says which vanilla blocks the game has. +pub fn carry( + jar: &Path, + info: &JarInfo, + out: &Path, + facts: Option<&Facts>, + allowed: &dyn Fn(&str) -> bool, +) -> Result { let id = &info.mod_id; if id.is_empty() || id.len() > 64 @@ -196,6 +320,7 @@ pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { let mut report = Report::default(); let mut blockstates: BTreeMap = BTreeMap::new(); let mut item_models = BTreeSet::new(); + let mut convention = BTreeSet::new(); for i in 0..zip.len() { let mut entry = zip.by_index(i).map_err(|e| e.to_string())?; let name = entry.name().to_owned(); @@ -218,11 +343,17 @@ pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { }; if keep { let rel = name.split_once(&format!("/{id}/")).map_or("", |(_, r)| r); + if name.starts_with("data/") + && (rel.starts_with("worldgen/") || rel.starts_with("structure/")) + { + report.worldgen += 1; + } if let Some(file) = rel .strip_prefix("recipe/") .filter(|_| name.starts_with("data/")) { if let Ok(mut recipe) = serde_json::from_slice::(&bytes) { + tags_used(&recipe, &mut convention); if upgrade_recipe(&mut recipe) { report.recipes_upgraded += 1; bytes = serde_json::to_vec_pretty(&recipe).unwrap_or(bytes); @@ -230,6 +361,15 @@ pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { report.recipes_kept.push(file.to_owned()); } } + } else if rel.starts_with("loot_table/") && name.starts_with("data/") { + if let Ok(mut table) = serde_json::from_slice::(&bytes) { + let before = table.clone(); + upgrade_loot(&mut table); + if table != before { + report.loot_tables_upgraded += 1; + bytes = serde_json::to_vec_pretty(&table).unwrap_or(bytes); + } + } } else if let Some(b) = rel .strip_prefix("blockstates/") .and_then(|b| b.strip_suffix(".json")) @@ -257,25 +397,116 @@ pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { } } let decl = inside.join(format!("data/{id}/horizon")); - for (b, state) in &blockstates { - match template(&properties(state)) { - Some(t) => { - write( - &decl.join(format!("block/{b}.json")), - json!({"template": t}).to_string().as_bytes(), - )?; - report.blocks.insert(b.clone(), t.to_owned()); + let write_decl = |kind: &str, name: &str, v: &Value| { + let text = serde_json::to_vec_pretty(v).unwrap_or_default(); + write(&decl.join(format!("{kind}/{name}.json")), &text) + }; + if let Some(facts) = facts { + report.oracle = true; + let prefix = format!("{id}:"); + let mut declared = BTreeSet::new(); + for (full, block) in facts.blocks.range(prefix.clone()..) { + let Some(b) = full.strip_prefix(&prefix) else { + break; + }; + let Some(choice) = facts.template(full, allowed) else { + let props: Vec<&str> = block.properties.keys().map(String::as_str).collect(); + let why = format!( + "no vanilla {} with properties [{}]", + block.vanilla_class().rsplit('.').next().unwrap_or(""), + props.join(", ") + ); + report.blocks_unknown.insert(b.to_owned(), why); + continue; + }; + let mut v = json!({"template": choice.template, "properties": choice.properties}); + if block.item == "minecraft:air" { + v["item"] = json!(false); + } else if block.item != *full { + v["item"] = json!(false); + report + .differences + .entry(b.to_owned()) + .or_default() + .push(format!("its item is {}", block.item)); + } else { + declared.insert(full.clone()); + } + write_decl("block", b, &v)?; + report.blocks.insert(b.to_owned(), choice.template); + if !choice.differences.is_empty() { + report + .differences + .entry(b.to_owned()) + .or_default() + .extend(choice.differences); } - None => report.blocks_unknown.push(b.clone()), } - } - for m in item_models.iter().filter(|m| !blockstates.contains_key(*m)) { - let item = json!({"template": "minecraft:stick"}); - write( - &decl.join(format!("item/{m}.json")), - item.to_string().as_bytes(), - )?; - report.items += 1; + for (full, item) in facts.items.range(prefix.clone()..) { + let Some(name) = full.strip_prefix(&prefix) else { + break; + }; + if declared.contains(full) { + continue; + } + if item + .block + .as_ref() + .is_some_and(|b| facts.blocks.contains_key(b)) + && report.blocks_unknown.contains_key(name) + { + report + .items_unknown + .insert(name.to_owned(), "its block is left out".into()); + continue; + } + match facts.item(full) { + Ok((v, differences)) => { + write_decl("item", name, &v)?; + report.items += 1; + if !differences.is_empty() { + report.differences.insert(name.to_owned(), differences); + } + } + Err(e) => { + report.items_unknown.insert(name.to_owned(), e); + } + } + } + // Convention tags are NeoForge's and the pack's: the recipes get the + // members the oracle saw, those of vanilla and of this mod. + for tag in &convention { + let Some(path) = tag.strip_prefix("c:") else { + continue; + }; + let file = inside.join(format!("data/c/tags/item/{path}.json")); + if file.exists() { + continue; + } + if let Some(members) = facts.tag_members("item", tag, id) { + let v = json!({"values": members}); + write(&file, &serde_json::to_vec_pretty(&v).unwrap_or_default())?; + report.convention_tags += 1; + } + } + } else { + for (b, state) in &blockstates { + match template(b, &properties(state)) { + Some(t) => { + write_decl("block", b, &json!({"template": t}))?; + report.blocks.insert(b.clone(), t.to_owned()); + } + None => { + let props: Vec = properties(state).into_iter().collect(); + let why = format!("no template for properties [{}]", props.join(", ")); + report.blocks_unknown.insert(b.clone(), why); + } + } + } + for m in item_models.iter().filter(|m| !blockstates.contains_key(*m)) { + write_decl("item", m, &json!({"template": "minecraft:stick"}))?; + report.items += 1; + } } let q = |s: &str| toml::Value::String(s.to_owned()).to_string(); let name = if info.name.is_empty() { id } else { &info.name }; @@ -299,6 +530,30 @@ pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { Ok(report) } +/// The tags a recipe's ingredients name (`#ns:path` or `{"tag": ...}`). +fn tags_used(v: &Value, out: &mut BTreeSet) { + match v { + Value::String(s) => { + if let Some(t) = s.strip_prefix('#') { + out.insert(t.to_owned()); + } + } + Value::Array(a) => a.iter().for_each(|x| tags_used(x, out)), + Value::Object(o) => { + for (k, x) in o { + match (k.as_str(), x) { + ("tag", Value::String(t)) => { + out.insert(t.clone()); + } + ("result", _) => {} + _ => tags_used(x, out), + } + } + } + _ => {} + } +} + fn write(path: &Path, bytes: &[u8]) -> Result<(), String> { if let Some(dir) = path.parent() { fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?; @@ -325,21 +580,86 @@ mod tests { assert!(!upgrade_recipe(&mut tag_list)); } + #[test] + fn upgrades_loot_tables() { + let mut slab = json!({"type": "minecraft:block", "pools": [{"rolls": 1.0, "entries": [{ + "type": "minecraft:item", "name": "m:slab", + "functions": [{"function": "minecraft:set_count", "count": 2.0, "add": false, + "conditions": [{"condition": "minecraft:block_state_property", "block": "m:slab", + "properties": {"type": "double"}}]}, + {"function": "minecraft:explosion_decay"}]}]}]}); + upgrade_loot(&mut slab); + assert_eq!( + slab["pools"][0]["entries"][0]["modifier"][0], + json!({"type": "minecraft:set_count", "count": 2.0, "add": false, + "condition": {"type": "minecraft:block_state_property", "block": "m:slab", + "properties": {"type": "double"}}}) + ); + let silk = json!({"condition": "minecraft:match_tool", "predicate": {"predicates": { + "minecraft:enchantments": [{"enchantments": "minecraft:silk_touch", "levels": {"min": 1}}]}}}); + let shears = json!({"condition": "minecraft:match_tool", "predicate": {"items": "minecraft:shears"}}); + let mut glass = json!({"pools": [{"conditions": [silk.clone()], "entries": []}, + {"conditions": [{"condition": "minecraft:any_of", "terms": [silk, shears]}, + {"condition": "minecraft:survives_explosion"}], "entries": []}]}); + upgrade_loot(&mut glass); + assert_eq!( + glass["pools"][0]["condition"], + "minecraft:tool/can_silk_touch" + ); + assert_eq!( + glass["pools"][1]["condition"], + json!({"type": "minecraft:all_of", "terms": [ + {"type": "minecraft:any_of", "terms": ["minecraft:tool/can_silk_touch", "minecraft:tool/can_shear"]}, + {"type": "minecraft:survives_explosion"}]}) + ); + // A loot table already in 26.3's shape stays as it is. + let mut new = glass.clone(); + upgrade_loot(&mut new); + assert_eq!(new, glass); + } + #[test] fn templates_from_blockstate_properties() { let door = json!({"variants": {"facing=east,half=lower,hinge=left,open=false,powered=false": {}}}); - assert_eq!(template(&properties(&door)), Some("minecraft:oak_door")); + assert_eq!( + template("gem_door", &properties(&door)), + Some("minecraft:oak_door") + ); + // Models that ignore `powered`: still a door. + let door = json!({"variants": {"facing=east,half=lower,hinge=left,open=false": {}}}); + assert_eq!( + template("gem_door", &properties(&door)), + Some("minecraft:oak_door") + ); + let trapdoor = json!({"variants": {"facing=east,half=top,open=false": {}}}); + assert_eq!( + template("gem_trapdoor", &properties(&trapdoor)), + Some("minecraft:oak_trapdoor") + ); + let sides = json!({"multipart": [{"when": {"north": "true"}}, {"when": {"east": "true"}}, + {"when": {"south": "true"}}, {"when": {"west": "true"}}]}); + assert_eq!( + template("gem_pane", &properties(&sides)), + Some("minecraft:glass_pane") + ); + assert_eq!( + template("gem_fence", &properties(&sides)), + Some("minecraft:oak_fence") + ); let fence = json!({"multipart": [{"apply": {}}, {"when": {"north": "true"}, "apply": {}}, {"when": {"east": "true"}}, {"when": {"south": "true"}}, {"when": {"west": "true"}}, {"when": {"OR": [{"waterlogged": "true"}]}}]}); - assert_eq!(template(&properties(&fence)), Some("minecraft:oak_fence")); assert_eq!( - template(&properties(&json!({"variants": {"": {}}}))), + template("gem_fence", &properties(&fence)), + Some("minecraft:oak_fence") + ); + assert_eq!( + template("gem_block", &properties(&json!({"variants": {"": {}}}))), Some("minecraft:stone") ); let odd = json!({"variants": {"charge=1,mode=fast": {}}}); - assert_eq!(template(&properties(&odd)), None); + assert_eq!(template("gem_core", &properties(&odd)), None); } /// A small NeoForge jar carried over is a content mod Horizon loads. @@ -411,14 +731,17 @@ mod tests { ); assert_eq!(info.census.classes, 1); let out = dir.join("work/mods/gem"); - let report = carry(&jar, &info, &out).unwrap(); - assert_eq!(report.copied, 11); - assert_eq!(report.outside, ["data/c/tags/item/gems.json"]); + let report = carry(&jar, &info, &out, None, &|_| true).unwrap(); + assert_eq!(report.copied, 12); + assert!(report.outside.is_empty()); assert_eq!((report.recipes_upgraded, report.recipes_kept.len()), (1, 1)); assert_eq!(report.item_definitions_added, 2); assert_eq!(report.blocks["gem_door"], "minecraft:oak_door"); assert_eq!(report.blocks["gem_block"], "minecraft:stone"); - assert_eq!(report.blocks_unknown, ["gem_core"]); + assert_eq!( + report.blocks_unknown.keys().collect::>(), + ["gem_core"] + ); assert_eq!(report.items, 1); let carried = out.join("carry"); assert!(!carried.join("com").exists()); diff --git a/crates/horizon-port-pack/src/jar.rs b/crates/horizon-port-pack/src/jar.rs index 67028755e..86e936e44 100644 --- a/crates/horizon-port-pack/src/jar.rs +++ b/crates/horizon-port-pack/src/jar.rs @@ -50,6 +50,12 @@ pub fn read(path: &Path) -> Result { } else { JarInfo::default() }; + // `version = "${file.jarVersion}"` is filled in from the manifest at load. + if info.version.starts_with("${") { + info.version = text(&mut zip, "META-INF/MANIFEST.MF") + .and_then(|m| manifest_value(&m, "Implementation-Version")) + .unwrap_or_default(); + } let names: Vec = zip.file_names().map(str::to_owned).collect(); info.census = census(&names); if info.mod_id.is_empty() { @@ -61,6 +67,16 @@ pub fn read(path: &Path) -> Result { Ok(info) } +/// A main-section attribute of `META-INF/MANIFEST.MF`. +fn manifest_value(manifest: &str, key: &str) -> Option { + manifest + .lines() + .take_while(|l| !l.trim().is_empty()) + .find_map(|l| l.strip_prefix(key)?.strip_prefix(':')) + .map(|v| v.trim().to_owned()) + .filter(|v| !v.is_empty()) +} + /// `[[mods]]` (the first one is the jar's mod), the top-level `license`, and /// `[[dependencies.]]` with `type = "required"` (or the older /// `mandatory = true`). diff --git a/crates/horizon-port-pack/src/main.rs b/crates/horizon-port-pack/src/main.rs index 4df3ef3ca..74178f503 100644 --- a/crates/horizon-port-pack/src/main.rs +++ b/crates/horizon-port-pack/src/main.rs @@ -6,8 +6,11 @@ //! `/modrinth.json`), the jars in the instance or `/jars/`, //! classes, tiers and dependency layers in `/inventory.json` and //! `inventory.csv`. -//! - `carry [...]`: the content mods' data and assets into -//! `/mods//carry/`, a folder mod for local use. +//! - `carry [--resources ] [...]`: the content +//! mods' data and assets into `/mods//carry/`, a folder mod for +//! local use. With the NeoForge oracle's `/oracle/facts.json` +//! (`tools/neoforge_oracle.py`), blocks and items come from its facts; +//! `--resources` limits templates to the blocks Horizon's tables have. //! //! The work folder belongs to the user: jars and carried-over files stay //! there and are never uploaded or committed. @@ -17,10 +20,11 @@ mod import; mod inventory; mod jar; mod modrinth; +mod oracle; use std::{fs, path::Path, process::ExitCode}; -const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline] | carry [...]"; +const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline] | carry [--resources ] [...]"; fn main() -> ExitCode { let args: Vec = std::env::args().skip(1).collect(); @@ -32,7 +36,19 @@ fn main() -> ExitCode { Some("inventory") => { path(1).and_then(|w| inventory(w, args.iter().any(|a| a == "--offline"))) } - Some("carry") => path(1).and_then(|w| carry(w, &args[2..])), + Some("carry") => path(1).and_then(|w| { + let mut rest = args[2..].to_vec(); + let resources = match rest.iter().position(|a| a == "--resources") { + Some(i) if i + 1 < rest.len() => { + let r = rest.remove(i + 1); + rest.remove(i); + Some(std::path::PathBuf::from(r)) + } + Some(_) => return Err(USAGE.to_owned()), + None => None, + }; + carry(w, resources.as_deref(), &rest) + }), _ => Err(USAGE.to_owned()), }; match result { @@ -107,8 +123,31 @@ fn inventory(work: &Path, offline: bool) -> Result<(), String> { Ok(()) } -fn carry(work: &Path, only: &[String]) -> Result<(), String> { +/// The vanilla blocks a prepared resource root has (`facts.json`'s defaults). +fn vanilla_blocks(resources: &Path) -> Result, String> { + let facts: serde_json::Value = read_json(&resources.join("facts.json"))?; + let defaults = facts["defaults"] + .as_object() + .ok_or("facts.json: no defaults")?; + Ok(defaults.keys().cloned().collect()) +} + +fn carry(work: &Path, resources: Option<&Path>, only: &[String]) -> Result<(), String> { let records: Vec = read_json(&work.join("inventory.json"))?; + let facts_path = work.join("oracle/facts.json"); + let facts = if facts_path.exists() { + let f = oracle::Facts::read(&facts_path)?; + println!( + "oracle facts: {} blocks, {} items", + f.blocks.len(), + f.items.len() + ); + Some(f) + } else { + None + }; + let vanilla = resources.map(vanilla_blocks).transpose()?; + let allowed = |b: &str| vanilla.as_ref().is_none_or(|v| v.contains(b)); let mut done = 0; for r in &records { let chosen = if only.is_empty() { @@ -125,19 +164,29 @@ fn carry(work: &Path, only: &[String]) -> Result<(), String> { if !chosen { continue; } - match carry::carry(jar, info, &work.join("mods").join(&r.key)) { + match carry::carry( + jar, + info, + &work.join("mods").join(&r.key), + facts.as_ref(), + &allowed, + ) { Ok(report) => { done += 1; println!( - "{}: {} files, {} recipes upgraded ({} kept), {} item definitions added, {} blocks ({} without a template), {} items, {} outside", + "{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded, {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside", r.key, report.copied, report.recipes_upgraded, report.recipes_kept.len(), + report.loot_tables_upgraded, report.item_definitions_added, report.blocks.len(), report.blocks_unknown.len(), + report.differences.len(), report.items, + report.items_unknown.len(), + report.convention_tags, report.outside.len() ); } diff --git a/crates/horizon-port-pack/src/oracle.rs b/crates/horizon-port-pack/src/oracle.rs new file mode 100644 index 000000000..1fa9a717c --- /dev/null +++ b/crates/horizon-port-pack/src/oracle.rs @@ -0,0 +1,384 @@ +//! The NeoForge oracle's facts (`tools/neoforge_oracle.py export`, in +//! `/oracle/facts.json`): every block, item, recipe and tag the pack's +//! mods register, read from the running game, and the vanilla blocks beside +//! them. Carry-over uses them in place of guesses: a block's template is the +//! vanilla block of the same class and states whose shapes and sounds match +//! best, and the declared properties are the mod's own numbers. +use serde::Deserialize; +use serde_json::{Map, Value, json}; +use std::{ + collections::{BTreeMap, BTreeSet}, + path::Path, +}; + +#[derive(Debug, Deserialize)] +pub struct Facts { + pub blocks: BTreeMap, + pub items: BTreeMap, + #[serde(default)] + pub tags: Tags, +} + +#[derive(Debug, Default, Deserialize)] +pub struct Tags { + #[serde(default)] + pub block: BTreeMap>, + #[serde(default)] + pub item: BTreeMap>, +} + +#[derive(Debug, Deserialize)] +pub struct Block { + /// The class and its superclasses, most derived first. + pub class: Vec, + pub properties: BTreeMap>, + pub default: String, + pub item: String, + pub explosion_resistance: f32, + pub friction: f32, + pub speed_factor: f32, + pub jump_factor: f32, + #[serde(default)] + pub block_set_type: Option, + #[serde(default)] + pub wood_type: Option, + pub states: BTreeMap, +} + +#[derive(Debug, Deserialize)] +pub struct SetType { + pub name: String, + pub can_open_by_hand: bool, +} + +#[derive(Debug, Deserialize)] +pub struct State { + pub hardness: f32, + pub emission: u8, + pub requires_tool: bool, + pub map_color_id: u8, + pub sound: Value, + pub outline: Value, + pub collision: Value, +} + +#[derive(Debug, Deserialize)] +pub struct Item { + pub class: String, + pub max_stack_size: u8, + pub rarity: String, + #[serde(default)] + pub block: Option, + #[serde(default)] + pub food: Option, +} + +const VANILLA_PACKAGE: &str = "net.minecraft."; + +/// A state key with its properties sorted, so keys compare across blocks. +pub fn state_key(key: &str) -> String { + let mut parts: Vec<&str> = key.split(',').filter(|p| !p.is_empty()).collect(); + parts.sort_unstable(); + parts.join(",") +} + +impl Block { + /// The first vanilla class in the chain. + pub fn vanilla_class(&self) -> &str { + self.class + .iter() + .find(|c| c.starts_with(VANILLA_PACKAGE)) + .map_or("", String::as_str) + } + /// Its own class is a mod class: behaviour code the template may not have. + pub fn custom_class(&self) -> bool { + self.class + .first() + .is_some_and(|c| !c.starts_with(VANILLA_PACKAGE)) + } + fn default_state(&self) -> Option<&State> { + self.states + .get(&self.default) + .or_else(|| self.states.values().next()) + } + fn same_states(&self, other: &Block) -> bool { + self.properties.len() == other.properties.len() + && self.properties.iter().all(|(k, v)| { + other.properties.get(k).is_some_and(|w| { + v.iter().collect::>() == w.iter().collect::>() + }) + }) + } + fn by_key(&self) -> BTreeMap { + self.states.iter().map(|(k, s)| (state_key(k), s)).collect() + } +} + +/// The template carry-over gives a mod block. +#[derive(Debug, Clone, PartialEq)] +pub struct Choice { + pub template: String, + /// `data//horizon/block/.json`'s `properties`. + pub properties: Map, + /// What the template does not reproduce, for the report. + pub differences: Vec, +} + +impl Facts { + pub fn read(path: &Path) -> Result { + let bytes = std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?; + serde_json::from_slice(&bytes).map_err(|e| format!("{}: {e}", path.display())) + } + + /// The vanilla block that best stands in for `id`: the same vanilla + /// class and states, then the most states with the same outline and + /// collision, then the same sounds, set type, wood type and hardness. + /// `None` when no vanilla block has its class and states. + pub fn template(&self, id: &str, allowed: &dyn Fn(&str) -> bool) -> Option { + let block = self.blocks.get(id)?; + let class = block.vanilla_class(); + let own = block.by_key(); + let ours = block.default_state()?; + let mut best: Option<(i64, &str, &Block)> = None; + for (name, v) in &self.blocks { + if !name.starts_with("minecraft:") + || v.vanilla_class() != class + || v.custom_class() + || !block.same_states(v) + || !allowed(name) + { + continue; + } + let theirs = v.by_key(); + let shapes = own + .iter() + .filter(|(k, s)| { + theirs + .get(*k) + .is_some_and(|t| t.outline == s.outline && t.collision == s.collision) + }) + .count() as i64; + let Some(td) = v.default_state() else { + continue; + }; + let same_set = match (&block.block_set_type, &v.block_set_type) { + (Some(a), Some(b)) => { + (a.name == b.name) as i64 * 2 + + (a.can_open_by_hand == b.can_open_by_hand) as i64 * 8 + } + _ => 0, + }; + let score = shapes * 1000 + + (td.sound == ours.sound) as i64 * 100 + + same_set * 10 + + (block.wood_type.is_some() && block.wood_type == v.wood_type) as i64 * 20 + + (td.hardness == ours.hardness) as i64 * 5 + + (td.map_color_id == ours.map_color_id) as i64; + if best.is_none_or(|(s, n, _)| score > s || (score == s && name.as_str() < n)) { + best = Some((score, name, v)); + } + } + let (_, template, t) = best?; + Some(self.choice(block, template, t, allowed)) + } + + fn choice( + &self, + block: &Block, + template: &str, + t: &Block, + allowed: &dyn Fn(&str) -> bool, + ) -> Choice { + let (ours, theirs) = (block.default_state(), t.default_state()); + let mut properties = Map::new(); + let mut differences = Vec::new(); + if let Some(s) = ours { + properties.insert("hardness".into(), json!(s.hardness)); + properties.insert( + "explosion_resistance".into(), + json!(block.explosion_resistance), + ); + properties.insert("light".into(), json!(s.emission)); + properties.insert("requires_tool".into(), json!(s.requires_tool)); + properties.insert("friction".into(), json!(block.friction)); + properties.insert("map_color".into(), json!(s.map_color_id)); + if theirs.is_none_or(|t| t.sound != s.sound) { + match self.sound_block(&s.sound, allowed) { + Some(b) => { + properties.insert("sounds".into(), json!(b)); + } + None => differences.push("sounds: no vanilla block has them".into()), + } + } + } + if (block.speed_factor, block.jump_factor) != (t.speed_factor, t.jump_factor) { + differences.push(format!( + "speed and jump factors {} {} (template {} {})", + block.speed_factor, block.jump_factor, t.speed_factor, t.jump_factor + )); + } + let theirs = t.by_key(); + let mut shapes = 0; + let mut varying = BTreeSet::new(); + let first = ours.map(|s| (s.hardness, s.emission, s.requires_tool, s.map_color_id)); + for (k, s) in block.by_key() { + let Some(v) = theirs.get(&k) else { + continue; + }; + if v.outline != s.outline || v.collision != s.collision { + shapes += 1; + } + if Some((s.hardness, s.emission, s.requires_tool, s.map_color_id)) != first { + varying.insert("per-state hardness, light or map colour"); + } + } + if shapes > 0 { + differences.push(format!( + "shapes differ from {template} in {shapes} of {} states", + block.states.len() + )); + } + differences.extend(varying.into_iter().map(str::to_owned)); + if block.custom_class() { + differences.push(format!( + "class {} (behaviour beyond {})", + block.class[0], + block.vanilla_class() + )); + } + Choice { + template: template.to_owned(), + properties, + differences, + } + } + + /// A vanilla block whose default state has these sounds. + fn sound_block(&self, sound: &Value, allowed: &dyn Fn(&str) -> bool) -> Option<&str> { + self.blocks + .iter() + .filter(|(n, _)| n.starts_with("minecraft:") && allowed(n)) + .find(|(_, b)| b.default_state().is_some_and(|s| &s.sound == sound)) + .map(|(n, _)| n.as_str()) + } + + /// An item declaration for a mod item that is not a declared block's: + /// a vanilla template item with its stack size and rarity, and what it + /// does not reproduce. + pub fn item(&self, id: &str) -> Result<(Value, Vec), String> { + let item = self.items.get(id).ok_or("not in the oracle")?; + if item.block.is_some() { + return Err("a block item without a block".into()); + } + let mut differences = Vec::new(); + if !item.class.starts_with(VANILLA_PACKAGE) { + differences.push(format!("class {} (behaviour beyond Item)", item.class)); + } + if item.food.is_some() { + return Err("food (no food template yet)".into()); + } + let mut decl = json!({"template": "minecraft:stick"}); + if item.max_stack_size != 64 { + decl["max_stack_size"] = json!(item.max_stack_size); + } + if item.rarity != "common" { + decl["rarity"] = json!(item.rarity); + } + Ok((decl, differences)) + } + + /// A tag's members the content mod may name: vanilla's and its own. + pub fn tag_members(&self, kind: &str, tag: &str, own: &str) -> Option> { + let map = if kind == "block" { + &self.tags.block + } else { + &self.tags.item + }; + let members = map.get(tag)?; + Some( + members + .iter() + .filter(|m| { + m.split_once(':') + .is_some_and(|(ns, _)| ns == "minecraft" || ns == own) + }) + .cloned() + .collect(), + ) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn state(hardness: f32, sound: &str, outline: Value) -> Value { + json!({"hardness": hardness, "emission": 0, "requires_tool": false, "map_color_id": 13, + "sound": {"break": sound}, "outline": outline, "collision": outline}) + } + fn block(class: &[&str], props: Value, states: Value) -> Value { + let default = states + .as_object() + .unwrap() + .keys() + .next() + .unwrap() + .to_owned(); + json!({"class": class, "properties": props, "default": default, "item": "x", + "explosion_resistance": 3.0, "friction": 0.6, "speed_factor": 1.0, + "jump_factor": 1.0, "states": states}) + } + + #[test] + fn templates_follow_class_shapes_and_sounds() { + let cube = json!([[0, 0, 0, 1, 1, 1]]); + let low = json!([[0, 0, 0, 1, 0.9375, 1]]); + let block_class = ["net.minecraft.world.level.block.Block"]; + let path = ["net.minecraft.world.level.block.DirtPathBlock"]; + let facts: Facts = serde_json::from_value(json!({ + "blocks": { + "minecraft:stone": block(&block_class, json!({}), json!({"": state(1.5, "stone", cube.clone())})), + "minecraft:oak_planks": block(&block_class, json!({}), json!({"": state(2.0, "wood", cube.clone())})), + "minecraft:dirt_path": block(&path, json!({}), json!({"": state(0.65, "grass", low.clone())})), + "m:wood_tile": block(&["m.Tile", block_class[0]], json!({}), json!({"": state(2.5, "wood", cube.clone())})), + "m:path": block(&["m.Path", block_class[0]], json!({}), json!({"": state(1.0, "stone", low.clone())})), + "m:gadget": block(&["m.Gadget", block_class[0]], json!({"mode": ["a", "b"]}), + json!({"mode=a": state(1.0, "stone", cube.clone()), "mode=b": state(1.0, "stone", cube)})), + }, + "items": {} + })) + .unwrap(); + let any = |_: &str| true; + let tile = facts.template("m:wood_tile", &any).unwrap(); + assert_eq!(tile.template, "minecraft:oak_planks"); + assert_eq!(tile.properties["hardness"], json!(2.5)); + assert!(!tile.properties.contains_key("sounds")); + assert_eq!( + tile.differences, + ["class m.Tile (behaviour beyond net.minecraft.world.level.block.Block)"] + ); + // A plain Block is never templated on a DirtPathBlock: shapes differ, and say so. + let path = facts.template("m:path", &any).unwrap(); + assert_eq!(path.template, "minecraft:stone"); + assert!( + path.differences[0].starts_with("shapes differ"), + "{:?}", + path.differences + ); + assert_eq!(facts.template("m:gadget", &any), None); + let tile = facts + .template("m:wood_tile", &|n: &str| n != "minecraft:oak_planks") + .unwrap(); + assert_eq!(tile.template, "minecraft:stone"); + assert_eq!(tile.differences[0], "sounds: no vanilla block has them"); + } + + #[test] + fn state_keys_sort() { + assert_eq!( + state_key("half=lower,facing=north"), + "facing=north,half=lower" + ); + assert_eq!(state_key(""), ""); + } +} diff --git a/docs/modpacks.md b/docs/modpacks.md index 7951ad4d4..ed77ca411 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -268,6 +268,7 @@ inspected: cargo run -p horizon-port-pack -- import ~/Downloads/pack.mrpack ~/.horizon/ports/pack cargo run -p horizon-port-pack -- inventory ~/.horizon/ports/pack # --offline reuses modrinth.json cargo run -p horizon-port-pack -- carry ~/.horizon/ports/pack # content mods with a jar, or name them +# --resources : only templates Horizon's tables have ``` `inventory` reads metadata only: Modrinth answers by file hash, and the jars only if the user's @@ -277,14 +278,26 @@ classes come from the reviewed table, Modrinth categories and sides; with jars, only): content 54, library 33, client-ui 26, performance 25, other 14, unknown 2, server-only 1; 40 rows matched the reviewed ATM10 table; three dependency layers (93 / 57 / 5). -`carry` copies a jar's `assets//`, `data//` and `data/minecraft/tags/` into -`mods//carry/`, a folder content mod (API 0.2) the game loads with `--dev-mod`, and keeps -what a content mod may not hold (other namespaces such as `c` tags, vanilla data) in `outside/`. -No class file is copied. It upgrades vanilla-type recipes from 1.21.1 ingredients to 26.3's, -adds the `items/` definitions 1.21.4 introduced, and declares each block from a vanilla template -guessed from its blockstate properties (stone for none, a door for `facing, half, hinge, open, -powered`, ...); blocks with an unknown property set, custom recipe types and NeoForge -conditions are listed in `carry-report.json` for the oracle step. +`carry` copies a jar's `assets//`, `data//`, `data/minecraft/tags/` and `data/c/tags/` +into `mods//carry/`, a folder content mod (API 0.2) the game loads with `--dev-mod`, and +keeps what a content mod may not hold (other namespaces, vanilla data) in `outside/`. 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 +`minecraft:tool/can_shear`), and the `items/` definitions 1.21.4 introduced. + +Blocks and items come from the NeoForge oracle when `/oracle/facts.json` exists (step 6): +every block the mod registers gets the vanilla template of the same vanilla class and states +whose shapes, sounds, set type and wood type match best, with the mod's own hardness, blast +resistance, light, tool need, friction, map colour and sounds as overrides. `--resources +` limits templates to blocks Horizon's tables have. `carry-report.json` lists, per +block, what the template does not reproduce (other shapes, sounds no vanilla block has, a class +of its own), and the blocks left out: those whose vanilla class has no vanilla block with the +same states (Macaw's garage doors, multi-part gates). Recipes get the convention tags they use +(`c:dyes/red`), filled from the oracle with vanilla's and the mod's own members. Without the +oracle, templates are guessed from the blockstate's properties: the template whose properties +include them with the fewest left over (models often ignore `powered` or `waterlogged`), the +block's name breaking ties (`_pane`, `_gate`, `_trapdoor`). ### 1. Import