Files
horizon-airdef/crates/horizon-port-pack/src/jar.rs
T
Ludovic Marie 886def4360 horizon-port-pack carry: templates and numbers from the NeoForge oracle, 1.21.1 loot upgraded
Carrying over seven real ATM10 mods (Cloud Glass, Factory Blocks,
Glassential, Macaw's Doors, Fences, Paths and Trapdoors) showed what the
guesses missed:

- Loot tables: 1.21.1's `conditions` lists, `functions` and
  `condition`/`function` kinds are now 26.3's single `condition`,
  `modifier` and `type`; silk-touch and shears tool matches become
  `minecraft:tool/can_silk_touch` and `can_shear`. Before, every drop
  condition was ignored (glass dropped itself by hand, upper door halves
  dropped doors, double slabs dropped one).
- With the oracle's facts (<work>/oracle/facts.json, from
  tools/neoforge_oracle.py in the next PR), every registered block gets
  the vanilla template of the same vanilla class and states that matches
  its shapes, sounds, BlockSetType and WoodType best, and the mod's own
  hardness, blast resistance, light, tool need, friction, map colour and
  sounds. Items get stack size and rarity. The carry report lists what a
  template does not reproduce and why a block was left out.
- Convention tags a recipe uses (c:dyes/red) are written from the
  oracle's tags, vanilla and own members only; the jar's own c tags stay
  inside the mod.
- Without the oracle, blockstates that omit properties their models
  ignore (powered, waterlogged) still find their template, and names pick
  between fences and panes, doors and trapdoors.
- `version = "${file.jarVersion}"` is read from MANIFEST.MF.
- `--resources <prepared>` limits templates to blocks Horizon has.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 23:25:54 +02:00

262 lines
8.5 KiB
Rust

//! What a mod jar holds, read without decompiling: its metadata
//! (`neoforge.mods.toml`, `mods.toml`, `fabric.mod.json`) and a census of
//! its data, assets and classes.
use serde::{Deserialize, Serialize};
use std::{fs, io::Read, path::Path};
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct JarInfo {
pub mod_id: String,
pub name: String,
pub version: String,
pub license: String,
/// Required dependencies by mod id (loaders and `minecraft` left out).
pub depends: Vec<String>,
pub census: Census,
}
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct Census {
pub blockstates: usize,
pub item_models: usize,
pub recipes: usize,
pub loot_tables: usize,
pub tags: usize,
pub worldgen: usize,
pub structures: usize,
pub languages: usize,
pub textures: usize,
pub sounds: usize,
pub classes: usize,
pub mixin_configs: usize,
}
const NOT_MODS: [&str; 5] = ["minecraft", "neoforge", "forge", "fabricloader", "java"];
pub fn read(path: &Path) -> Result<JarInfo, String> {
let file = fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?;
let mut zip = zip::ZipArchive::new(file).map_err(|e| format!("{}: {e}", path.display()))?;
let text = |zip: &mut zip::ZipArchive<fs::File>, name: &str| -> Option<String> {
let mut s = String::new();
zip.by_name(name).ok()?.read_to_string(&mut s).ok()?;
Some(s)
};
let mut info = if let Some(t) = text(&mut zip, "META-INF/neoforge.mods.toml")
.or_else(|| text(&mut zip, "META-INF/mods.toml"))
{
mods_toml(&t)?
} else if let Some(t) = text(&mut zip, "fabric.mod.json") {
fabric(&t)?
} 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<String> = zip.file_names().map(str::to_owned).collect();
info.census = census(&names);
if info.mod_id.is_empty() {
info.mod_id = path
.file_stem()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_default();
}
Ok(info)
}
/// A main-section attribute of `META-INF/MANIFEST.MF`.
fn manifest_value(manifest: &str, key: &str) -> Option<String> {
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.<id>]]` with `type = "required"` (or the older
/// `mandatory = true`).
pub fn mods_toml(text: &str) -> Result<JarInfo, String> {
let doc: toml::Table = toml::from_str(text).map_err(|e| format!("mods.toml: {e}"))?;
let first = doc
.get("mods")
.and_then(|m| m.as_array())
.and_then(|m| m.first())
.and_then(|m| m.as_table());
let get = |t: Option<&toml::Table>, k: &str| {
t.and_then(|t| t.get(k))
.and_then(|v| v.as_str())
.unwrap_or("")
.to_owned()
};
let mod_id = get(first, "modId");
let mut depends = Vec::new();
if let Some(deps) = doc
.get("dependencies")
.and_then(|d| d.get(&mod_id))
.and_then(|d| d.as_array())
{
for d in deps.iter().filter_map(|d| d.as_table()) {
let required = d.get("type").and_then(|t| t.as_str()).map_or_else(
|| d.get("mandatory").and_then(|m| m.as_bool()) == Some(true),
|t| t.eq_ignore_ascii_case("required"),
);
let id = get(Some(d), "modId");
if required && !NOT_MODS.contains(&id.as_str()) && !id.is_empty() {
depends.push(id);
}
}
}
Ok(JarInfo {
name: get(first, "displayName"),
version: get(first, "version"),
license: get(Some(&doc), "license"),
mod_id,
depends,
census: Census::default(),
})
}
pub fn fabric(text: &str) -> Result<JarInfo, String> {
let v: serde_json::Value =
serde_json::from_str(text).map_err(|e| format!("fabric.mod.json: {e}"))?;
let s = |k: &str| v[k].as_str().unwrap_or("").to_owned();
let license = match &v["license"] {
serde_json::Value::Array(l) => l
.iter()
.filter_map(|x| x.as_str())
.collect::<Vec<_>>()
.join(" OR "),
l => l.as_str().unwrap_or("").to_owned(),
};
let depends = v["depends"]
.as_object()
.into_iter()
.flatten()
.map(|(id, _)| id.clone())
.filter(|id| !NOT_MODS.contains(&id.as_str()) && !id.starts_with("fabric"))
.collect();
Ok(JarInfo {
mod_id: s("id"),
name: s("name"),
version: s("version"),
license,
depends,
census: Census::default(),
})
}
pub fn census(names: &[String]) -> Census {
let mut c = Census::default();
for n in names {
let parts: Vec<&str> = n.split('/').collect();
if n.ends_with(".class") {
c.classes += 1;
} else if n.ends_with(".mixins.json") || (parts.len() == 1 && n.contains("mixins")) {
c.mixin_configs += 1;
}
// assets/<ns>/<kind>/... and data/<ns>/<kind>/...
let (Some(root), Some(kind)) = (parts.first(), parts.get(2)) else {
continue;
};
let json = n.ends_with(".json");
match (*root, *kind) {
("assets", "blockstates") if json => c.blockstates += 1,
("assets", "models") if json && parts.get(3) == Some(&"item") => c.item_models += 1,
("assets", "lang") => c.languages += 1,
("assets", "textures") if n.ends_with(".png") => c.textures += 1,
("assets", "sounds.json") => c.sounds += 1,
("data", "recipe" | "recipes") if json => c.recipes += 1,
("data", "loot_table" | "loot_tables") if json => c.loot_tables += 1,
("data", "tags") if json => c.tags += 1,
("data", "worldgen") if json => c.worldgen += 1,
("data", "structure" | "structures") if n.ends_with(".nbt") => c.structures += 1,
_ => {}
}
}
c
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_neoforge_metadata() {
let info = mods_toml(
r#"modLoader = "javafml"
license = "MIT"
[[mods]]
modId = "ruby"
version = "1.2.0"
displayName = "Ruby"
[[dependencies.ruby]]
modId = "neoforge"
type = "required"
[[dependencies.ruby]]
modId = "geckolib"
type = "required"
[[dependencies.ruby]]
modId = "jei"
type = "optional"
[[dependencies.ruby]]
modId = "curios"
mandatory = true
"#,
)
.unwrap();
assert_eq!(
(
info.mod_id.as_str(),
info.version.as_str(),
info.license.as_str()
),
("ruby", "1.2.0", "MIT")
);
assert_eq!(info.depends, ["geckolib", "curios"]);
}
#[test]
fn reads_fabric_metadata() {
let info = fabric(
r#"{"id": "gem", "version": "3", "name": "Gem", "license": ["MIT", "Apache-2.0"],
"depends": {"fabricloader": "*", "fabric-api": "*", "minecraft": "1.21.1", "cloth-config": "*"}}"#,
)
.unwrap();
assert_eq!(info.license, "MIT OR Apache-2.0");
assert_eq!(info.depends, ["cloth-config"]);
}
#[test]
fn counts_files_by_kind() {
let names = [
"assets/ruby/blockstates/ruby_block.json",
"assets/ruby/models/item/ruby.json",
"assets/ruby/models/block/ruby_block.json",
"assets/ruby/textures/item/ruby.png",
"assets/ruby/lang/en_us.json",
"data/ruby/recipe/ruby.json",
"data/ruby/loot_table/blocks/ruby_block.json",
"data/minecraft/tags/block/mineable/pickaxe.json",
"data/ruby/structure/hut.nbt",
"com/example/Ruby.class",
"ruby.mixins.json",
]
.map(String::from);
let c = census(&names);
assert_eq!(
(c.blockstates, c.item_models, c.textures, c.languages),
(1, 1, 1, 1)
);
assert_eq!(
(c.recipes, c.loot_tables, c.tags, c.structures),
(1, 1, 1, 1)
);
assert_eq!((c.classes, c.mixin_configs), (1, 1));
}
}