Merge feat/horizon-server-control into feat/horizon-server-backups

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-16 22:28:18 +02:00
56 changed files with 4833 additions and 125 deletions
+27 -12
View File
@@ -14,7 +14,7 @@ use serde_json::Value;
use std::{
collections::{HashMap, HashSet},
fs,
path::Path,
path::{Path, PathBuf},
sync::Arc,
};
@@ -1182,18 +1182,33 @@ impl Recipes {
let mut crafting = Vec::new();
let mut cooking = Vec::new();
let mut meta = HashMap::new();
let mut files: Vec<_> = fs::read_dir(root.join("data/minecraft/recipe"))?
.flatten()
.map(|e| e.path())
.filter(|p| p.extension().is_some_and(|e| e == "json"))
let json_files = |dir: &Path| -> Result<Vec<PathBuf>> {
let mut files: Vec<_> = fs::read_dir(dir)?
.flatten()
.map(|e| e.path())
.filter(|p| p.extension().is_some_and(|e| e == "json"))
.collect();
files.sort();
Ok(files)
};
// Vanilla's recipes by bare name, then content mods' as `<ns>:<name>`.
let mut files: Vec<(String, PathBuf)> = json_files(&root.join("data/minecraft/recipe"))?
.into_iter()
.map(|p| (String::new(), p))
.collect();
files.sort();
for path in files {
let name = path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("")
.to_owned();
for namespace in crate::mod_content::namespaces(root) {
let dir = root.join(format!("data/{namespace}/recipe"));
if dir.is_dir() {
files.extend(
json_files(&dir)?
.into_iter()
.map(|p| (format!("{namespace}:"), p)),
);
}
}
for (prefix, path) in files {
let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
let name = format!("{prefix}{stem}");
let v: Value = serde_json::from_slice(&fs::read(&path)?)?;
let text = |k: &str| v[k].as_str().unwrap_or("").to_owned();
meta.insert(name.clone(), (text("category"), text("group")));
+14 -7
View File
@@ -49,17 +49,24 @@ pub struct Names {
}
impl Names {
pub fn load(root: &Path) -> Self {
let map = fs::read(root.join("assets/minecraft/lang/en_us.json"))
.ok()
.and_then(|b| serde_json::from_slice::<HashMap<String, String>>(&b).ok())
.unwrap_or_default();
let read = |namespace: &str| {
fs::read(root.join(format!("assets/{namespace}/lang/en_us.json")))
.ok()
.and_then(|b| serde_json::from_slice::<HashMap<String, String>>(&b).ok())
.unwrap_or_default()
};
let mut map = read("minecraft");
// Content mods' names (docs/modpacks.md); none in a vanilla root.
for namespace in crate::mod_content::namespaces(root) {
map.extend(read(&namespace));
}
Self { map }
}
pub fn item(&self, id: &str) -> String {
let path = id.trim_start_matches("minecraft:");
let (namespace, path) = id.split_once(':').unwrap_or(("minecraft", id));
self.map
.get(&format!("item.minecraft.{path}"))
.or_else(|| self.map.get(&format!("block.minecraft.{path}")))
.get(&format!("item.{namespace}.{path}"))
.or_else(|| self.map.get(&format!("block.{namespace}.{path}")))
.cloned()
.unwrap_or_else(|| path.replace('_', " "))
}
+8 -1
View File
@@ -4109,7 +4109,9 @@ impl Game<'_> {
fn slot_view(&mut self, i: usize) -> Option<ItemStack> {
match i {
200 => self.craft_result(),
309 => self.crafter_result(),
309 if matches!(self.container_kind(), Some(BlockEntity::Crafter(_))) => {
self.crafter_result()
}
_ => self.slot_mut(i).clone(),
}
}
@@ -9746,6 +9748,11 @@ 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);
options.resources = mods::content_root(
&options.resources,
&early_settings.mods.enabled,
&options.dev_mods,
);
let assets = match Assets::load(&options.resources) {
Ok(assets) => assets,
// Minecraft.rollbackResourcePacks: the packs break loading, start
+54
View File
@@ -47,6 +47,60 @@ fn vanilla_command(name: &str) -> bool {
.any(|&c| d.nodes[c].name() == name)
}
/// `root` with the content mods that are on layered over it
/// (docs/modpacks.md): their blocks, items, data and assets. Read once at
/// start, before the resources load; a mod whose content is refused is left
/// out here and its row on the Mods page says why.
pub(super) fn content_root(
root: &std::path::Path,
enabled: &std::collections::BTreeSet<String>,
dev: &[std::path::PathBuf],
) -> std::path::PathBuf {
use horizon_client::mod_content::{self, ContentMod};
use horizon_mods::{Capability, Content, Package};
let here = std::env::current_dir().unwrap_or_default();
let mut paths = horizon_mods::find(&here.join(mod_list::DIR));
paths.retain(|p| !dev.contains(p));
paths.extend(dev.iter().cloned());
let mut mods: Vec<ContentMod> = Vec::new();
for path in &paths {
let Ok(package) = Package::read(path) else {
continue;
};
let m = &package.manifest;
let on = enabled.contains(&m.id) || dev.contains(path);
if !on
|| !m.capabilities.contains(&Capability::Registries)
|| mods.iter().any(|c| c.id == m.id)
{
continue;
}
match Content::read(path, m) {
Ok(content) => mods.push(ContentMod {
id: m.id.clone(),
content,
}),
Err(e) => log::warn!("Horizon: mod {} content not loaded: {e}", m.id),
}
}
match mod_content::resource_root(root, &mods, &here.join(".horizon-cache")) {
Ok(out) => {
if out != root {
log::info!(
"Horizon: content from {} mod(s) in {}",
mods.len(),
out.display()
);
}
out
}
Err(e) => {
log::warn!("Horizon: content mods not loaded: {e}");
root.to_owned()
}
}
}
impl Game<'_> {
/// Reads `horizon-mods/` and the dev mods, and starts the ones turned on.
pub(super) fn start_mods(&mut self) {
+2
View File
@@ -147,6 +147,8 @@ impl Game<'_> {
self.res.clouds = super::load_clouds(root);
self.res.forcefield = super::load_forcefield(root);
self.res.names = super::Names::load(root);
self.res.mob_data.villager_hats =
horizon_client::mobs::villager_hats::VillagerHats::load(root);
horizon_client::ui::load_metadata(root);
lang::set(lang::Language::load(&[&root.to_path_buf()], &lang::code()));
self.audio.reload(root);
+4 -2
View File
@@ -102,8 +102,10 @@ fn atlas_sprites(root: &Path, packed: &[PackedImage]) -> Vec<PackedImage> {
let image = match &origin {
SpriteOrigin::File(texture) => {
let path = texture.strip_prefix("minecraft:").unwrap_or(texture);
// Packed under its own name already: only a renamed file is new.
if path == name {
// A vanilla file is packed under its own name already:
// only a renamed one is new. Other namespaces' files
// (content mods) are never in the walked directories.
if texture.starts_with("minecraft:") && path == name {
continue;
}
load(texture, "textures").map(|(w, h, p)| {
+28 -10
View File
@@ -512,11 +512,16 @@ impl Items {
let mut loot = HashMap::new();
// Every loot table, keyed by its id (blocks/stone, entities/sheep/white,
// gameplay/chicken_lay, shearing/sheep/red...).
fn walk_loot(dir: &Path, base: &Path, loot: &mut HashMap<String, Value>) -> Result<()> {
fn walk_loot(
dir: &Path,
base: &Path,
namespace: &str,
loot: &mut HashMap<String, Value>,
) -> Result<()> {
for entry in fs::read_dir(dir)?.flatten() {
let p = entry.path();
if p.is_dir() {
walk_loot(&p, base, loot)?;
walk_loot(&p, base, namespace, loot)?;
} else if p.extension().is_some_and(|e| e == "json") {
let id = p
.strip_prefix(base)?
@@ -524,7 +529,7 @@ impl Items {
.to_string_lossy()
.replace('\\', "/");
loot.insert(
format!("minecraft:{id}"),
format!("{namespace}:{id}"),
serde_json::from_slice(&fs::read(&p)?)?,
);
}
@@ -532,7 +537,14 @@ impl Items {
Ok(())
}
let loot_root = root.join("data/minecraft/loot_table");
walk_loot(&loot_root, &loot_root, &mut loot)?;
walk_loot(&loot_root, &loot_root, "minecraft", &mut loot)?;
// Content mods' tables (docs/modpacks.md): `<ns>:blocks/...`.
for namespace in crate::mod_content::namespaces(root) {
let dir = root.join(format!("data/{namespace}/loot_table"));
if dir.is_dir() {
walk_loot(&dir, &dir, &namespace, &mut loot)?;
}
}
let registry_order = registry_order(root, &order);
Ok(Self {
items,
@@ -644,12 +656,15 @@ impl Items {
state: BlockStateId,
) -> Option<String> {
let block = registry.state(state)?;
let name = block.name.trim_start_matches("minecraft:");
let (namespace, name) = block
.name
.split_once(':')
.unwrap_or(("minecraft", &block.name));
let standing = name.replacen("_wall_", "_", 1);
let standing = standing.strip_prefix("wall_").unwrap_or(&standing);
let standing = standing.replacen("wall_", "", 1);
self.loot_sequence(&format!("minecraft:blocks/{name}"))
.or_else(|| self.loot_sequence(&format!("minecraft:blocks/{standing}")))
self.loot_sequence(&format!("{namespace}:blocks/{name}"))
.or_else(|| self.loot_sequence(&format!("{namespace}:blocks/{standing}")))
}
/// Evaluate the block's loot table for a player break without explosions.
pub fn block_drops<R: RandomSource>(
@@ -737,7 +752,10 @@ impl Items {
let Some(block) = registry.state(state) else {
return vec![];
};
let name = block.name.trim_start_matches("minecraft:");
let (namespace, name) = block
.name
.split_once(':')
.unwrap_or(("minecraft", &block.name));
// `Blocks.wallVariant`: wall signs, banners, heads, fans and torches
// reuse the standing block's loot table.
let standing = name.replacen("_wall_", "_", 1);
@@ -745,8 +763,8 @@ impl Items {
let standing = standing.replacen("wall_", "", 1);
let table = self
.loot
.get(&format!("minecraft:blocks/{name}"))
.or_else(|| self.loot.get(&format!("minecraft:blocks/{standing}")));
.get(&format!("{namespace}:blocks/{name}"))
.or_else(|| self.loot.get(&format!("{namespace}:blocks/{standing}")));
let Some(table) = table else {
return vec![];
};
+1
View File
@@ -107,6 +107,7 @@ pub mod mesher;
pub mod minimap;
pub mod mipmap;
pub mod mobs;
pub mod mod_content;
pub mod mod_list;
mod model_library;
pub mod models;
+1
View File
@@ -71,6 +71,7 @@ pub mod undead;
pub mod vex;
pub mod villager;
pub mod villager_brain;
pub mod villager_hats;
pub mod warden;
pub mod warden_brain;
pub mod witch;
+4
View File
@@ -46,6 +46,7 @@ pub struct MobData {
/// Each entity type's `SpawnPlacements.getHeightmapType`.
pub heightmaps: HashMap<String, String>,
pub animations: HashMap<String, Animation>,
pub villager_hats: super::villager_hats::VillagerHats,
/// Biome tags (spawns_warm_variant_farm_animals, is_ocean...) by tag id.
pub biome_tags: HashMap<String, std::collections::HashSet<String>>,
/// Entity type tags (`data/*/tags/entity_type`).
@@ -71,6 +72,7 @@ impl MobData {
};
data.biome_tags = crate::items::load_tags(root, "worldgen/biome").unwrap_or_default();
data.entity_tags = crate::items::load_tags(root, "entity_type").unwrap_or_default();
data.villager_hats = super::villager_hats::VillagerHats::load(root);
data
}
/// Only the keyframe animation definitions of `mob-data.json`.
@@ -131,6 +133,7 @@ impl MobData {
placements,
heightmaps,
animations,
villager_hats: Default::default(),
biome_tags: HashMap::new(),
entity_tags: HashMap::new(),
pois: Default::default(),
@@ -186,6 +189,7 @@ impl MobData {
placements: HashMap::new(),
heightmaps: HashMap::new(),
animations: HashMap::new(),
villager_hats: Default::default(),
biome_tags: HashMap::new(),
entity_tags: HashMap::new(),
pois: Default::default(),
+8
View File
@@ -1061,6 +1061,14 @@ fn render_visible(
};
let mut poses = Poses::new(&part);
anim::setup(m, &s, &mut poses, &data.animations);
if matches!(m.species(), Species::Villager | Species::ZombieVillager)
&& let Some((folder, variant)) = layer.texture.split_once("/type/")
{
let variant = variant.strip_prefix("baby/").unwrap_or(variant);
let profession = PROFESSIONS[m.extra.variant2 as usize % PROFESSIONS.len()];
data.villager_hats
.apply(folder, variant, profession, &mut poses);
}
let light = if layer.full_bright {
240 | 240 << 8
} else {
@@ -0,0 +1,128 @@
//! VillagerProfessionLayer's cached texture metadata and type-layer head visibility.
use super::{
anim::Poses,
render::{PROFESSIONS, VILLAGER_TYPES},
};
use std::{collections::HashMap, fs, path::Path};
#[derive(Clone, Copy, Default, PartialEq, Eq, Debug)]
enum Hat {
#[default]
None,
Partial,
Full,
}
#[derive(Default)]
pub struct VillagerHats(HashMap<String, Hat>);
impl VillagerHats {
pub fn load(root: &Path) -> Self {
let mut hats = HashMap::new();
for folder in ["villager", "zombie_villager"] {
for (group, names) in [
("type", VILLAGER_TYPES.as_slice()),
("profession", PROFESSIONS.as_slice()),
] {
for name in names {
let texture = format!("entity/{folder}/{group}/{name}");
let path = root.join(format!("assets/minecraft/textures/{texture}.png.mcmeta"));
let metadata = fs::read(path)
.ok()
.and_then(|bytes| serde_json::from_slice::<serde_json::Value>(&bytes).ok());
let hat = match metadata
.as_ref()
.and_then(|v| v["villager"]["hat"].as_str())
{
Some("partial") => Hat::Partial,
Some("full") => Hat::Full,
_ => Hat::None,
};
hats.insert(texture, hat);
}
}
}
Self(hats)
}
pub fn apply(&self, folder: &str, variant: &str, profession: &str, poses: &mut Poses) {
let hat = |group, name| {
self.0
.get(&format!("{folder}/{group}/{name}"))
.copied()
.unwrap_or_default()
};
let visible = match hat("profession", profession) {
Hat::None => true,
Hat::Partial => hat("type", variant) != Hat::Full,
Hat::Full => false,
};
// VillagerModel.createNoHatModel clears the head and every child.
poses.visible("head", visible);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore = "requires prepared 26.3 models and textures in HORIZON_TEST_RESOURCES"]
fn metadata_head_visibility_matches_exported_no_hat_models() {
use crate::mobs::{load_model, render::draw};
let root = std::env::var_os("HORIZON_TEST_RESOURCES").expect("HORIZON_TEST_RESOURCES");
let mut hats = VillagerHats::load(Path::new(&root));
assert_eq!(hats.0["entity/villager/profession/farmer"], Hat::Full);
assert_eq!(hats.0["entity/villager/type/plains"], Hat::None);
for (name, folder) in [
("villager", "entity/villager"),
("villager_baby", "entity/villager"),
("zombie_villager", "entity/zombie_villager"),
("zombie_villager_baby", "entity/zombie_villager"),
] {
let model = load_model(Path::new(&root), &format!("minecraft:{name}#main")).unwrap();
let no_hat =
load_model(Path::new(&root), &format!("minecraft:{name}_no_hat#main")).unwrap();
for (type_hat, profession_hat, hidden) in [
(Hat::None, Hat::Full, true),
(Hat::Full, Hat::Partial, true),
(Hat::Partial, Hat::Partial, false),
(Hat::Full, Hat::None, false),
] {
hats.0.insert(format!("{folder}/type/plains"), type_hat);
hats.0
.insert(format!("{folder}/profession/farmer"), profession_hat);
let mut poses = Poses::new(&model);
hats.apply(folder, "plains", "farmer", &mut poses);
let mut actual = Vec::new();
draw(
&model,
glam::Mat4::IDENTITY,
&poses,
[0.0, 0.0, 1.0, 1.0],
u32::MAX,
0,
0,
&mut actual,
);
let expected_model = if hidden { &no_hat } else { &model };
let mut expected = Vec::new();
draw(
expected_model,
glam::Mat4::IDENTITY,
&Poses::new(expected_model),
[0.0, 0.0, 1.0, 1.0],
u32::MAX,
0,
0,
&mut expected,
);
assert_eq!(
bytemuck::cast_slice::<_, u8>(&actual),
bytemuck::cast_slice::<_, u8>(&expected),
"{name}: {type_hat:?}/{profession_hat:?}"
);
}
}
}
}
+857
View File
@@ -0,0 +1,857 @@
//! Content mods in the resource root (docs/modpacks.md, registries 0.2).
//!
//! Every loader reads one resource root, and most of its block and item
//! tables are exports from vanilla indexed by block-state id or keyed by
//! name. A content mod's block copies a vanilla *template*. So the mods'
//! root is an overlay, like the resource-pack one:
//! - the base root linked, with the per-state and per-name tables
//! rewritten: each mod block gets its template's rows appended (states
//! after vanilla's, mods and blocks in id order) with names, translation
//! keys and in-table state ids moved over, and the declared overrides
//! applied;
//! - each mod's own `assets/` and `data/` files;
//! - additions to tags merged into the tag files and bundles.
//!
//! The loaders need no mod code, vanilla state ids never move, and with no
//! content mod the root is the base root itself.
use crate::{Result, blocks::MAP_COLORS};
use horizon_mods::content::{Block, Content, Rarity};
use horizon_world::nbt::{self, JavaString, Limits, Tag};
use serde::{Deserialize, Serialize, de};
use std::{
collections::BTreeMap,
fmt, fs,
ops::Range,
path::{Path, PathBuf},
};
/// A content mod to put in the root: its id and content.
pub struct ContentMod {
pub id: String,
pub content: Content,
}
/// JSON with its object keys in file order, so a rewritten table differs
/// from the original only by what was added.
#[derive(Clone, Debug, PartialEq)]
enum J {
Null,
Bool(bool),
Num(serde_json::Number),
Str(String),
Arr(Vec<J>),
Obj(Vec<(String, J)>),
}
impl<'de> Deserialize<'de> for J {
fn deserialize<D: de::Deserializer<'de>>(d: D) -> std::result::Result<Self, D::Error> {
struct V;
impl<'de> de::Visitor<'de> for V {
type Value = J;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("JSON")
}
fn visit_unit<E>(self) -> std::result::Result<J, E> {
Ok(J::Null)
}
fn visit_bool<E>(self, v: bool) -> std::result::Result<J, E> {
Ok(J::Bool(v))
}
fn visit_i64<E>(self, v: i64) -> std::result::Result<J, E> {
Ok(J::Num(v.into()))
}
fn visit_u64<E>(self, v: u64) -> std::result::Result<J, E> {
Ok(J::Num(v.into()))
}
fn visit_f64<E: de::Error>(self, v: f64) -> std::result::Result<J, E> {
serde_json::Number::from_f64(v)
.map(J::Num)
.ok_or_else(|| E::custom("not a JSON number"))
}
fn visit_str<E>(self, v: &str) -> std::result::Result<J, E> {
Ok(J::Str(v.to_owned()))
}
fn visit_string<E>(self, v: String) -> std::result::Result<J, E> {
Ok(J::Str(v))
}
fn visit_seq<A: de::SeqAccess<'de>>(
self,
mut a: A,
) -> std::result::Result<J, A::Error> {
let mut out = Vec::new();
while let Some(v) = a.next_element()? {
out.push(v);
}
Ok(J::Arr(out))
}
fn visit_map<A: de::MapAccess<'de>>(
self,
mut a: A,
) -> std::result::Result<J, A::Error> {
let mut out = Vec::new();
while let Some(kv) = a.next_entry()? {
out.push(kv);
}
Ok(J::Obj(out))
}
}
d.deserialize_any(V)
}
}
impl Serialize for J {
fn serialize<S: serde::Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
use serde::ser::{SerializeMap, SerializeSeq};
match self {
J::Null => s.serialize_unit(),
J::Bool(b) => s.serialize_bool(*b),
J::Num(n) => n.serialize(s),
J::Str(t) => s.serialize_str(t),
J::Arr(a) => {
let mut seq = s.serialize_seq(Some(a.len()))?;
for v in a {
seq.serialize_element(v)?;
}
seq.end()
}
J::Obj(o) => {
let mut map = s.serialize_map(Some(o.len()))?;
for (k, v) in o {
map.serialize_entry(k, v)?;
}
map.end()
}
}
}
}
impl J {
fn get(&self, key: &str) -> Option<&J> {
match self {
J::Obj(o) => o.iter().find(|(k, _)| k == key).map(|(_, v)| v),
_ => None,
}
}
fn get_mut(&mut self, key: &str) -> Option<&mut J> {
match self {
J::Obj(o) => o.iter_mut().find(|(k, _)| k == key).map(|(_, v)| v),
_ => None,
}
}
/// Sets `key`, in place if present, else at the end.
fn set(&mut self, key: &str, value: J) {
if let Some(v) = self.get_mut(key) {
*v = value;
} else if let J::Obj(o) = self {
o.push((key.to_owned(), value));
}
}
/// The value at a `/`-separated path ("" is the document).
fn at(&mut self, path: &str) -> Option<&mut J> {
path.split('/')
.filter(|p| !p.is_empty())
.try_fold(self, |j, key| j.get_mut(key))
}
fn f64(v: f64) -> J {
serde_json::Number::from_f64(v).map_or(J::Null, J::Num)
}
fn as_u32(&self) -> Option<u32> {
match self {
J::Num(n) => n.as_u64().and_then(|v| u32::try_from(v).ok()),
_ => None,
}
}
/// Renames strings and, with `ids`, moves state ids, everywhere inside.
fn rename(&mut self, r: &Rename, ids: bool) {
match self {
J::Str(s) => {
if let Some(new) = r.string(s) {
*s = new;
}
}
J::Num(n) if ids => {
if let Some(new) = n
.as_u64()
.and_then(|v| u32::try_from(v).ok())
.and_then(|id| r.id(id))
{
*n = new.into();
}
}
J::Arr(a) => a.iter_mut().for_each(|v| v.rename(r, ids)),
J::Obj(o) => o.iter_mut().for_each(|(_, v)| v.rename(r, ids)),
_ => {}
}
}
}
/// One id moving to another: `minecraft:iron_block` → `ruby:ruby_block`,
/// with its state range for blocks.
#[derive(Clone)]
struct Rename {
from: String,
to: String,
states: Range<u32>,
first: u32,
}
impl Rename {
fn new(from: &str, to: &str) -> Self {
Rename {
from: from.to_owned(),
to: to.to_owned(),
states: 0..0,
first: 0,
}
}
/// The id itself, `Block{id}`, and `block.<ns>.<path>` / `item.<ns>.<path>` keys.
fn string(&self, s: &str) -> Option<String> {
if s == self.from {
return Some(self.to.clone());
}
if s.strip_prefix("Block{").and_then(|x| x.strip_suffix('}')) == Some(&self.from) {
return Some(format!("Block{{{}}}", self.to));
}
let key = |id: &str| id.replacen(':', ".", 1).replace('/', ".");
for kind in ["block", "item"] {
if s.strip_prefix(kind).and_then(|x| x.strip_prefix('.')) == Some(&key(&self.from)) {
return Some(format!("{kind}.{}", key(&self.to)));
}
}
None
}
fn id(&self, id: u32) -> Option<u32> {
self.states
.contains(&id)
.then(|| self.first + (id - self.states.start))
}
}
/// A block being added: its declaration and where its states go.
struct NewBlock<'a> {
decl: &'a Block,
rename: Rename,
/// The template's item, if it has one.
item: Option<String>,
}
/// An item being added (declared, or a block's).
struct NewItem {
rename: Rename,
max_stack_size: Option<u8>,
rarity: Option<Rarity>,
}
struct Plan<'a> {
blocks: Vec<NewBlock<'a>>,
items: Vec<NewItem>,
/// Vanilla's state count (the tables' length before).
states: usize,
/// Each vanilla block's default state.
defaults: BTreeMap<String, u32>,
/// The gameplay rows' sound objects, by block, for `sounds` overrides.
sounds: BTreeMap<String, J>,
}
fn read_json(path: &Path) -> Result<J> {
Ok(serde_json::from_slice(&fs::read(path)?).map_err(|e| format!("{}: {e}", path.display()))?)
}
impl<'a> Plan<'a> {
fn new(base: &Path, mods: &'a [ContentMod]) -> Result<Self> {
let mut facts = read_json(&base.join("facts.json"))?;
let names: Vec<String> = match facts.at("states") {
Some(J::Arr(states)) => states
.iter()
.map(|s| match s {
J::Arr(row) => match row.first() {
Some(J::Str(name)) => Ok(name.clone()),
_ => Err("facts.json: a state without a name"),
},
_ => Err("facts.json: a state that is not a row"),
})
.collect::<std::result::Result<_, _>>()?,
_ => return Err("facts.json: no states".into()),
};
let defaults: BTreeMap<String, u32> = match facts.at("defaults") {
Some(J::Obj(o)) => o
.iter()
.filter_map(|(k, v)| Some((k.clone(), v.as_u32()?)))
.collect(),
_ => return Err("facts.json: no defaults".into()),
};
let range = |name: &str| -> Option<Range<u32>> {
let start = names.iter().position(|n| n == name)?;
let len = names[start..].iter().take_while(|n| *n == name).count();
// A block's states are one run in registry order.
(names[start + len..].iter().all(|n| n != name))
.then_some(start as u32..(start + len) as u32)
};
let mut blocks = Vec::new();
let mut next = names.len() as u32;
let mut sorted: Vec<&ContentMod> = mods.iter().collect();
sorted.sort_by(|a, b| a.id.cmp(&b.id));
let mut wanted_sounds = Vec::new();
for m in &sorted {
let mut declared: Vec<&Block> = m.content.blocks.iter().collect();
declared.sort_by(|a, b| a.id.cmp(&b.id));
for decl in declared {
if defaults.contains_key(&decl.id) {
return Err(format!("{}: {} is already a block", m.id, decl.id).into());
}
let states = range(&decl.template).ok_or_else(|| {
format!("{}: template {} is not a block", decl.id, decl.template)
})?;
let mut rename = Rename::new(&decl.template, &decl.id);
rename.first = next;
next += states.len() as u32;
rename.states = states;
if let Some(s) = &decl.properties.sounds {
if !defaults.contains_key(s) {
return Err(format!("{}: sounds {s} is not a block", decl.id).into());
}
wanted_sounds.push(s.clone());
}
blocks.push(NewBlock {
decl,
rename,
item: None,
});
}
}
// Block items copy the template block's item.
let gameplay = read_json(&base.join("gameplay-facts.json"))?;
let rows = match gameplay.get("states") {
Some(J::Arr(rows)) if rows.len() == names.len() => rows,
_ => return Err("gameplay-facts.json does not match facts.json".into()),
};
let mut sounds = BTreeMap::new();
for s in wanted_sounds {
let row = &rows[defaults[&s] as usize];
sounds.insert(s, row.get("sound").cloned().unwrap_or(J::Null));
}
let mut items = Vec::new();
for b in &mut blocks {
let row = &rows[b.rename.states.start as usize];
b.item = match row.get("item") {
Some(J::Str(i)) if i != "minecraft:air" => Some(i.clone()),
_ => None,
};
if b.decl.item {
let item = b.item.as_deref().ok_or_else(|| {
format!("{}: template {} has no item", b.decl.id, b.decl.template)
})?;
items.push(NewItem {
rename: Rename::new(item, &b.decl.id),
max_stack_size: None,
rarity: None,
});
}
}
for m in &sorted {
for i in &m.content.items {
items.push(NewItem {
rename: Rename::new(&i.template, &i.id),
max_stack_size: i.max_stack_size,
rarity: i.rarity,
});
}
}
Ok(Plan {
blocks,
items,
states: names.len(),
defaults,
sounds,
})
}
/// What moves in a new block's copies of its template's rows: the
/// block, and the template's item when it has another name. A block
/// without an item gets `minecraft:air` where rows name one.
fn renames(&self, b: &NewBlock) -> Vec<Rename> {
let mut out = vec![b.rename.clone()];
if let Some(item) = b.item.as_deref().filter(|i| *i != b.decl.template)
&& b.decl.item
{
out.push(Rename::new(item, &b.decl.id));
}
out
}
/// Appends each new block's copies of the template's states to `list`;
/// `gameplay` rows get their index and the declared overrides.
fn states(&self, list: &mut Vec<J>, ids: bool, gameplay: bool) -> Result<()> {
if list.len() != self.states {
return Err(format!("{} states, not {}", list.len(), self.states).into());
}
for b in &self.blocks {
let renames = self.renames(b);
for (i, id) in b.rename.states.clone().enumerate() {
let mut row = list[id as usize].clone();
for r in &renames {
row.rename(r, ids);
}
if gameplay {
row.set("id", J::Num((b.rename.first + i as u32).into()));
if !b.decl.item {
row.set("item", J::Str("minecraft:air".into()));
}
self.override_gameplay(b, &mut row);
}
list.push(row);
}
}
Ok(())
}
/// The declared properties, on a gameplay row.
fn override_gameplay(&self, b: &NewBlock, row: &mut J) {
let p = &b.decl.properties;
if let Some(v) = p.hardness {
row.set("hardness", J::f64(v as f64));
}
if let Some(v) = p.explosion_resistance {
row.set("explosion_resistance", J::f64(v as f64));
}
if let Some(v) = p.light {
row.set("emission", J::Num(v.into()));
}
if let Some(v) = p.requires_tool {
row.set("requires_tool", J::Bool(v));
}
if let Some(v) = p.friction {
row.set("friction", J::f64(v as f64));
}
if let Some(v) = p.map_color {
let col = if v == 0 { 0 } else { MAP_COLORS[v as usize] };
row.set("map_color", J::Num(col.into()));
}
if let Some(s) = &p.sounds {
row.set("sound", self.sounds[s].clone());
}
}
/// Adds each new block's copy of the template's entry to a map by name.
fn names(&self, map: &mut J, ids: bool) {
for b in &self.blocks {
if let Some(v) = map.get(&b.decl.template) {
let mut v = v.clone();
for r in &self.renames(b) {
v.rename(r, ids);
}
map.set(&b.decl.id, v);
}
}
}
/// Adds each new item's copy of its template's entry to a map by name;
/// `components` applies the overrides (items.json).
fn items(&self, map: &mut J, components: bool) {
for i in &self.items {
let Some(v) = map.get(&i.rename.from) else {
continue;
};
let mut v = v.clone();
v.rename(&i.rename, false);
if components {
if let Some(n) = i.max_stack_size {
v.set("minecraft:max_stack_size", J::Num(n.into()));
}
if let Some(r) = i.rarity {
let r = match r {
Rarity::Common => "common",
Rarity::Uncommon => "uncommon",
Rarity::Rare => "rare",
Rarity::Epic => "epic",
};
v.set("minecraft:rarity", J::Str(r.into()));
}
}
map.set(&i.rename.to, v);
}
}
/// One JSON table, rewritten.
fn json(&self, name: &str, doc: &mut J) -> Result<()> {
let states = |doc: &mut J, path: &str, ids: bool, gameplay: bool| -> Result<()> {
match doc.at(path) {
Some(J::Arr(a)) => self.states(a, ids, gameplay),
_ => Err(format!("no list at /{path}").into()),
}
};
match name {
"facts.json" => {
states(doc, "states", false, false)?;
let defaults = doc.at("defaults").ok_or("no defaults")?;
for b in &self.blocks {
let default = self.defaults[&b.decl.template];
let id = b.rename.id(default).ok_or("a default outside its block")?;
defaults.set(&b.decl.id, J::Num(id.into()));
}
}
"gameplay-facts.json" => states(doc, "states", false, true)?,
"block-behaviours.json" => {
states(doc, "states", false, false)?;
let Some(J::Arr(blocks)) = doc.at("blocks") else {
return Err("no blocks".into());
};
for b in &self.blocks {
let template = blocks.iter().find(
|x| matches!(x.get("name"), Some(J::Str(n)) if *n == b.decl.template),
);
if let Some(mut v) = template.cloned() {
for r in &self.renames(b) {
v.rename(r, false);
}
if !b.decl.item {
v.set("item", J::Str("minecraft:air".into()));
}
blocks.push(v);
}
}
self.items(doc.at("items").ok_or("no items")?, false);
for odds in ["fire/burnOdds", "fire/igniteOdds"] {
if let Some(map) = doc.at(odds) {
self.names(map, false);
}
}
}
"model-states.json"
| "landing-definitions.json"
| "worldgen/blocks/survival-rules.json" => self.names(doc, false),
"render-definitions.json" => self.names(doc, true),
"worldgen/blocks/survival-extra-definitions.json" => {
if let Some(map) = doc.at("ignition_odds") {
self.names(map, false);
}
}
"items.json" => self.items(doc, true),
"command-registries.json" => {
self.names(doc.at("blocks").ok_or("no blocks")?, false);
let new_items = || self.items.iter().map(|i| J::Str(i.rename.to.clone()));
let new_blocks = || self.blocks.iter().map(|b| J::Str(b.decl.id.clone()));
let Some(J::Arr(items)) = doc.at("items") else {
return Err("no items".into());
};
items.extend(new_items());
// The registries commands check ids against.
if let Some(J::Arr(ids)) = doc.at("by_registry/minecraft:item/ids") {
ids.extend(new_items());
}
if let Some(J::Arr(ids)) = doc.at("by_registry/minecraft:block/ids") {
ids.extend(new_blocks());
}
}
"mob-data.json" => states(doc, "states", false, false)?,
"worldgen/block_flags.json" => states(doc, "", false, false)?,
"worldgen/blocks/structure-blocks.json" => {
for key in ["rotate", "mirror", "full_block"] {
states(doc, key, true, false)?;
}
let added: usize = self.blocks.iter().map(|b| b.rename.states.len()).sum();
doc.set("states", J::Num(((self.states + added) as u64).into()));
}
_ => return Err("no rule for this table".into()),
}
Ok(())
}
/// One NBT table: `states` lists, `cached_shapes` arrays and `blocks`
/// compounds, wherever they are.
fn nbt(&self, tag: &mut Tag) {
let Tag::Compound(c) = tag else {
return;
};
for (key, value) in c.iter_mut() {
match (key.to_string_lossy().as_str(), value) {
("states", Tag::List(list)) if list.len() == self.states => {
for b in &self.blocks {
for id in b.rename.states.clone() {
let mut state = list[id as usize].clone();
if let Some(slot) = state.get_mut("id") {
*slot = Tag::String(b.decl.id.as_str().into());
}
list.push(state);
}
}
}
("cached_shapes", Tag::IntArray(a)) if a.len() == self.states => {
for b in &self.blocks {
let r = &b.rename.states;
let copy = a[r.start as usize..r.end as usize].to_vec();
a.extend(copy);
}
}
("blocks", Tag::Compound(map)) => {
for b in &self.blocks {
let Some(mut v) = map
.get(&JavaString::from(b.decl.template.as_str()))
.cloned()
else {
continue;
};
if let (Some(f), Some(slot)) =
(b.decl.properties.friction, v.get_mut("friction"))
{
*slot = Tag::Float(f.to_bits());
}
map.insert(b.decl.id.as_str().into(), v);
}
}
(_, inner @ Tag::Compound(_)) => self.nbt(inner),
_ => {}
}
}
}
}
/// The JSON and NBT tables rewritten for new blocks and items.
const JSON_TABLES: [&str; 13] = [
"facts.json",
"gameplay-facts.json",
"block-behaviours.json",
"model-states.json",
"render-definitions.json",
"landing-definitions.json",
"items.json",
"command-registries.json",
"mob-data.json",
"worldgen/block_flags.json",
"worldgen/blocks/structure-blocks.json",
"worldgen/blocks/survival-rules.json",
"worldgen/blocks/survival-extra-definitions.json",
];
const NBT_TABLES: [&str; 6] = [
"occlusion.nbt",
"cached-collision.nbt",
"movement-properties.nbt",
"worldgen/blocks/block-properties.nbt",
"worldgen/blocks/cached-support.nbt",
"worldgen/blocks/cached-collision.nbt",
];
/// In a root built here: the content mods' ids, the namespaces loaders
/// read besides `minecraft`.
const MARKER: &str = "horizon-mod-content.json";
/// The content mods' namespaces in a root built by [`build`]; none in any
/// other root, so vanilla's loaders read exactly what they did.
pub fn namespaces(root: &Path) -> Vec<String> {
fs::read(root.join(MARKER))
.ok()
.and_then(|b| serde_json::from_slice(&b).ok())
.unwrap_or_default()
}
/// Bundles of block tag files (tag id → the tag file's text).
const TAG_BUNDLES: [&str; 2] = ["block-tags.json", "worldgen/block_tags.json"];
/// The overlay being written: `out` mirrors `base` through links, and a
/// directory becomes real only when something inside it is written.
struct Overlay<'a> {
base: &'a Path,
out: &'a Path,
}
impl Overlay<'_> {
fn new<'a>(base: &'a Path, out: &'a Path) -> Result<Overlay<'a>> {
if fs::symlink_metadata(out).is_ok() {
fs::remove_dir_all(out)?;
}
fs::create_dir_all(out)?;
let overlay = Overlay { base, out };
overlay.link_children(Path::new(""))?;
Ok(overlay)
}
fn link_children(&self, rel: &Path) -> Result<()> {
let Ok(read) = fs::read_dir(self.base.join(rel)) else {
return Ok(());
};
for entry in read.flatten() {
let rel = rel.join(entry.file_name());
link(&self.base.join(&rel), &self.out.join(&rel))?;
}
Ok(())
}
/// Makes `rel`'s directories real, each holding links to the base's.
fn dir(&self, rel: &Path) -> Result<()> {
let mut at = PathBuf::new();
for part in rel.iter() {
at.push(part);
let path = self.out.join(&at);
let meta = fs::symlink_metadata(&path);
if meta
.as_ref()
.is_ok_and(|m| m.is_dir() && !m.file_type().is_symlink())
{
continue;
}
if meta.is_ok() {
fs::remove_file(&path)?;
}
fs::create_dir(&path)?;
self.link_children(&at)?;
}
Ok(())
}
fn write(&self, rel: &str, bytes: &[u8]) -> Result<()> {
let rel = Path::new(rel);
if let Some(parent) = rel.parent() {
self.dir(parent)?;
}
let path = self.out.join(rel);
if fs::symlink_metadata(&path).is_ok() {
fs::remove_file(&path)?;
}
fs::write(path, bytes)?;
Ok(())
}
}
#[cfg(unix)]
fn link(target: &Path, at: &Path) -> std::io::Result<()> {
std::os::unix::fs::symlink(target, at)
}
#[cfg(not(unix))]
fn link(target: &Path, at: &Path) -> std::io::Result<()> {
if target.is_dir() {
fs::create_dir_all(at)?;
for entry in fs::read_dir(target)?.flatten() {
link(&entry.path(), &at.join(entry.file_name()))?;
}
Ok(())
} else {
fs::copy(target, at).map(|_| ())
}
}
/// A tag file's JSON text with `more` appended to its values (no repeats).
fn merge_tag(text: Option<&str>, more: &[J], what: &str) -> Result<String> {
let mut doc = match text {
Some(t) => serde_json::from_str(t).map_err(|e| format!("{what}: {e}"))?,
None => J::Obj(vec![("values".into(), J::Arr(Vec::new()))]),
};
let Some(J::Arr(values)) = doc.get_mut("values") else {
return Err(format!("{what}: no values").into());
};
for v in more {
if !values.contains(v) {
values.push(v.clone());
}
}
Ok(serde_json::to_string_pretty(&doc)?)
}
/// Builds the content root for `mods` over `base` in `out`.
pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
let plan = Plan::new(base, mods)?;
let overlay = Overlay::new(base, out)?;
for name in JSON_TABLES {
let path = base.join(name);
if !path.exists() {
continue;
}
let mut doc = read_json(&path)?;
plan.json(name, &mut doc)
.map_err(|e| format!("{name}: {e}"))?;
overlay.write(name, &serde_json::to_vec(&doc)?)?;
}
for name in NBT_TABLES {
let path = base.join(name);
if !path.exists() {
continue;
}
let (mut named, _) = nbt::read_named(&fs::read(&path)?, Limits::default())
.map_err(|e| format!("{name}: {e}"))?;
plan.nbt(&mut named.tag);
let bytes =
nbt::write_named(&named, Limits::default()).map_err(|e| format!("{name}: {e}"))?;
overlay.write(name, &bytes)?;
}
// Mod files, and every tag addition gathered per tag file.
let mut tags: BTreeMap<String, Vec<J>> = BTreeMap::new();
for m in mods {
let declarations = format!("data/{}/horizon/", m.id);
for file in &m.content.files {
let bytes = m.content.file(file)?;
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());
} else if !file.starts_with(&declarations) {
overlay.write(file, &bytes)?;
}
}
}
let mut bundles: Vec<(&str, J)> = Vec::new();
for name in TAG_BUNDLES {
if base.join(name).exists() {
bundles.push((name, read_json(&base.join(name))?));
}
}
for (file, values) in &tags {
let vanilla = fs::read_to_string(base.join(file)).ok();
overlay.write(
file,
merge_tag(vanilla.as_deref(), values, file)?.as_bytes(),
)?;
// data/<ns>/tags/block/<path>.json is the block tag <ns>:<path>.
let Some((ns, path)) = file
.strip_prefix("data/")
.and_then(|r| r.split_once("/tags/block/"))
.and_then(|(ns, p)| Some((ns, p.strip_suffix(".json")?)))
else {
continue;
};
let id = format!("{ns}:{path}");
for (name, doc) in &mut bundles {
let text = match doc.get(&id) {
Some(J::Str(t)) => Some(t.clone()),
_ => None,
};
let merged = merge_tag(text.as_deref(), values, name)?;
doc.set(&id, J::Str(merged));
}
}
for (name, doc) in &bundles {
overlay.write(name, &serde_json::to_vec(doc)?)?;
}
// A block without its own blockstate looks like its template, so every
// state still has a model.
for m in mods {
for block in &m.content.blocks {
let (ns, path) = block.id.split_once(':').unwrap_or((&m.id, &block.id));
let own = format!("assets/{ns}/blockstates/{path}.json");
if m.content.files.contains(&own) {
continue;
}
let template = block.template.trim_start_matches("minecraft:");
let vanilla = base.join(format!("assets/minecraft/blockstates/{template}.json"));
overlay.write(&own, &fs::read(&vanilla)?)?;
}
}
let mut ids: Vec<&str> = mods.iter().map(|m| m.id.as_str()).collect();
ids.sort();
overlay.write(MARKER, &serde_json::to_vec(&ids)?)?;
Ok(())
}
/// The root to load: `base` itself with no content, else the mods' overlay
/// over it, built in `cache/mod-content`.
pub fn resource_root(base: &Path, mods: &[ContentMod], cache: &Path) -> Result<PathBuf> {
if mods.iter().all(|m| m.content.is_empty()) {
return Ok(base.to_owned());
}
let out = cache.join("mod-content");
build(base, mods, &out)?;
Ok(out)
}
#[cfg(test)]
#[path = "mod_content_tests.rs"]
mod tests;
@@ -0,0 +1,218 @@
use super::*;
use crate::{assets::Assets, blocks::BlockFacts, items::Items};
#[test]
fn renames_ids_keys_and_states() {
let mut r = Rename::new("minecraft:iron_block", "ruby:ruby_block");
r.states = 10..12;
r.first = 100;
assert_eq!(
r.string("minecraft:iron_block").as_deref(),
Some("ruby:ruby_block")
);
assert_eq!(
r.string("Block{minecraft:iron_block}").as_deref(),
Some("Block{ruby:ruby_block}")
);
assert_eq!(
r.string("block.minecraft.iron_block").as_deref(),
Some("block.ruby.ruby_block")
);
assert_eq!(r.string("minecraft:iron_blocks"), None);
assert_eq!(r.string("block.minecraft.iron_bars"), None);
assert_eq!(
(r.id(9), r.id(10), r.id(11), r.id(12)),
(None, Some(100), Some(101), None)
);
}
#[test]
fn json_keeps_key_order_and_numbers() {
let text = r#"{"z":1,"a":[0.6,5.0,-1,{"m":null,"b":true}],"k":"v"}"#;
let doc: J = serde_json::from_str(text).unwrap();
assert_eq!(serde_json::to_string(&doc).unwrap(), text);
}
#[test]
fn tags_merge_without_repeats() {
let vanilla = r#"{"values": ["minecraft:stone"]}"#;
let more = [
J::Str("ruby:ruby_block".into()),
J::Str("minecraft:stone".into()),
];
let merged: serde_json::Value =
serde_json::from_str(&merge_tag(Some(vanilla), &more, "t").unwrap()).unwrap();
assert_eq!(
merged["values"],
serde_json::json!(["minecraft:stone", "ruby:ruby_block"])
);
}
fn prepared() -> Option<PathBuf> {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3");
root.join("gameplay-facts.json").exists().then_some(root)
}
/// A content mod folder: a full block, a door and an item-less glass from
/// templates, an item, a vanilla tag addition and a language file.
fn ruby_mod(dir: &Path) -> ContentMod {
let files: [(&str, &str); 7] = [
(
"horizon-mod.toml",
"id = \"ruby\"\nname = \"Ruby\"\nversion = \"0.1.0\"\napi = \"0.2\"\nside = \"both\"\ncapabilities = [\"registries\"]\n",
),
(
"data/ruby/horizon/block/ruby_block.json",
r#"{"template": "minecraft:iron_block", "properties": {"hardness": 4.0, "light": 3,
"sounds": "minecraft:amethyst_block", "map_color": 28, "friction": 0.8}}"#,
),
(
"data/ruby/horizon/block/ruby_door.json",
r#"{"template": "minecraft:oak_door"}"#,
),
(
"data/ruby/horizon/block/ruby_glass.json",
r#"{"template": "minecraft:glass", "item": false}"#,
),
(
"data/ruby/horizon/item/ruby.json",
r#"{"template": "minecraft:diamond", "max_stack_size": 16, "rarity": "rare"}"#,
),
(
"data/minecraft/tags/block/mineable/pickaxe.json",
r#"{"values": ["ruby:ruby_block"]}"#,
),
(
"assets/ruby/lang/en_us.json",
r#"{"block.ruby.ruby_block": "Block of Ruby"}"#,
),
];
for (name, text) in files {
let path = dir.join(name);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, text).unwrap();
}
let package = horizon_mods::Package::read(dir).unwrap();
ContentMod {
id: "ruby".into(),
content: Content::read(dir, &package.manifest).unwrap(),
}
}
/// The overlay over the real prepared resources: every loader reads it, the
/// new blocks behave like their templates with the overrides, and vanilla's
/// states are where they were.
#[test]
fn content_root_loads_with_every_table() {
let Some(base) = prepared() else {
return;
};
let scratch = std::env::temp_dir().join(format!("horizon-mod-content-{}", std::process::id()));
let _ = fs::remove_dir_all(&scratch);
let m = ruby_mod(&scratch.join("ruby"));
let root = resource_root(&base, std::slice::from_ref(&m), &scratch.join("cache")).unwrap();
assert_ne!(root, base);
let vanilla = Assets::load(&base).unwrap();
let assets = Assets::load(&root).unwrap();
let (old, new) = (vanilla.registry, assets.registry);
let n = old.len() as u32;
let iron = old.default_state("minecraft:iron_block").unwrap();
let door = old.default_state("minecraft:oak_door").unwrap();
let door_first = (0..n)
.map(BlockStateId)
.find(|&s| old.state(s).unwrap().name == "minecraft:oak_door")
.unwrap();
// Vanilla keeps its ids; blocks come after, sorted: ruby_block, ruby_door, ruby_glass.
assert_eq!(new.len(), old.len() + 1 + 64 + 1);
assert_eq!(new.default_state("minecraft:iron_block"), Some(iron));
let ruby = new.default_state("ruby:ruby_block").unwrap();
assert_eq!(ruby, BlockStateId(n));
let ruby_door = new.default_state("ruby:ruby_door").unwrap();
assert_eq!(ruby_door, BlockStateId(n + 1 + (door.0 - door_first.0)));
assert_eq!(
new.default_state("ruby:ruby_glass"),
Some(BlockStateId(n + 65))
);
let old_facts = BlockFacts::load(&base, old).unwrap();
let facts = BlockFacts::load(&root, new).unwrap();
let (a, b) = (old_facts.get(iron), facts.get(ruby));
assert_eq!((b.hardness, b.emission, b.friction), (4.0, 3, 0.8));
assert_eq!(b.explosion_resistance, a.explosion_resistance);
assert_eq!(b.outline, a.outline);
assert_eq!(b.classes, a.classes);
assert_eq!(&*b.item, "ruby:ruby_block");
assert_eq!(b.map_color, 28);
let amethyst = old_facts.get(old.default_state("minecraft:amethyst_block").unwrap());
assert_eq!(b.sounds.break_sound, amethyst.sounds.break_sound);
for i in 0..64 {
let (v, m) = (
old_facts.get(BlockStateId(door_first.0 + i)),
facts.get(BlockStateId(n + 1 + i)),
);
assert_eq!(
(v.outline.clone(), v.hardness, v.classes.clone()),
(m.outline.clone(), m.hardness, m.classes.clone())
);
assert_eq!(&*m.name, "ruby:ruby_door");
}
let glass = facts.get(BlockStateId(n + 65));
assert_eq!(&*glass.item, "minecraft:air");
// Vanilla's own rows are untouched.
assert_eq!(facts.get(iron).hardness, a.hardness);
assert_eq!(&*facts.get(iron).name, "minecraft:iron_block");
let items = Items::load(&root, new, &facts).unwrap();
let ruby_block = &items.items["ruby:ruby_block"];
assert_eq!(ruby_block.block, Some(ruby));
let gem = &items.items["ruby:ruby"];
assert_eq!(gem.max_stack, 16);
assert!(!items.items.contains_key("ruby:ruby_glass"));
assert!(items.block_tags["minecraft:mineable/pickaxe"].contains("ruby:ruby_block"));
crate::collisions::load(&root, new).unwrap();
crate::models::load_visibility(&root, new).unwrap();
crate::light::LightTables::load(&root, new, &facts).unwrap();
crate::motion::MotionResources::load(&root, &assets).unwrap();
horizon_world::worldgen::generator::GeneratorData::load(&root.join("worldgen"), new).unwrap();
assert!(root.join("assets/ruby/lang/en_us.json").is_file());
// Blocks without a blockstate of their own take their template's.
assert_eq!(
fs::read(root.join("assets/ruby/blockstates/ruby_door.json")).unwrap(),
fs::read(base.join("assets/minecraft/blockstates/oak_door.json")).unwrap()
);
// The base root was only read.
assert!(
fs::symlink_metadata(base.join("facts.json"))
.unwrap()
.is_file()
);
fs::remove_dir_all(&scratch).unwrap();
}
#[test]
fn refuses_unknown_templates_and_vanilla_ids() {
let Some(base) = prepared() else {
return;
};
let scratch = std::env::temp_dir().join(format!("horizon-mod-refuse-{}", std::process::id()));
let _ = fs::remove_dir_all(&scratch);
let mut m = ruby_mod(&scratch.join("ruby"));
m.content.blocks[0].template = "minecraft:not_a_block".into();
let e = build(&base, std::slice::from_ref(&m), &scratch.join("out")).unwrap_err();
assert!(e.to_string().contains("is not a block"), "{e}");
fs::remove_dir_all(&scratch).unwrap();
}
#[test]
fn no_content_keeps_the_base_root() {
let base = Path::new("/nonexistent/base");
assert_eq!(
resource_root(base, &[], Path::new("/nonexistent/cache")).unwrap(),
base
);
}
use horizon_world::registry::BlockStateId;
+1 -1
View File
@@ -73,7 +73,7 @@ pub(crate) fn prepare_with_models(
let blocks: Vec<&str> = facts
.keys()
.filter(|name| registry.default_state(name).is_some())
.filter_map(|name| name.strip_prefix("minecraft:"))
.map(|name| name.strip_prefix("minecraft:").unwrap_or(name))
.collect();
let catalog =
StateModels::from_facts_with_models(root, registry, &blocks, sprites, &facts, models)?;
+4 -2
View File
@@ -94,7 +94,9 @@ impl StateModels {
}
}
for block in blocks.iter().copied().collect::<BTreeSet<_>>() {
let name = format!("minecraft:{block}");
// Vanilla blocks come bare; content mods' blocks with their namespace.
let (namespace, path) = block.split_once(':').unwrap_or(("minecraft", block));
let name = format!("{namespace}:{path}");
let facts = facts.get(&name).ok_or("missing block model state schema")?;
let schema: PropertySchema = facts["schema"]
.as_object()
@@ -133,7 +135,7 @@ impl StateModels {
.collect::<Result<_>>()?;
let possible: Vec<Properties> = serde_json::from_value(facts["states"].clone())?;
let resource = fs::read_to_string(
root.join(format!("assets/minecraft/blockstates/{block}.json")),
root.join(format!("assets/{namespace}/blockstates/{path}.json")),
)?;
let definition = BlockStateDefinition::from_json(&resource)?;
let bound = definition.bind(&schema, &possible)?;
+112
View File
@@ -223,6 +223,25 @@ impl Opt {
_ => Kind::Cycle,
}
}
/// OptionInstance's static or value-dependent tooltip translation.
fn tooltip_key(self, s: &Settings) -> String {
let key = match self {
Opt::InactivityFps => {
if s.inactivity_fps_limit == "minimized" {
"options.inactivityFpsLimit.minimized"
} else {
"options.inactivityFpsLimit.afk"
}
}
Opt::ChunkBuilder => [
"options.prioritizeChunkUpdates.none",
"options.prioritizeChunkUpdates.byPlayer",
"options.prioritizeChunkUpdates.nearby",
][usize::from(s.prioritize_chunk_updates.min(2))],
_ => self.caption(),
};
format!("{key}.tooltip")
}
/// A slider's position (0..1), or `None` for a cycle button.
pub fn slider_position(self, s: &Settings) -> Option<f32> {
match self.kind(s) {
@@ -1611,6 +1630,27 @@ impl OptionsUi {
let start = batch.vertices.len();
let hover = ui::hovered(cursor, wd.x, wd.y, wd.w, wd.h)
&& (!wd.list || cursor.1 >= top && cursor.1 < bottom);
if hover {
let key = match &wd.kind {
WKind::Button {
act: Act::Cycle(opt),
..
}
| WKind::Slider { opt, .. } => Some(opt.tooltip_key(s)),
WKind::Button {
act: Act::Push(Page::Language),
..
} => Some("options.language.tooltip".into()),
WKind::Button {
act: Act::Push(Page::Accessibility),
..
} => Some("options.accessibility.tooltip".into()),
_ => None,
};
if let Some(key) = key.filter(|key| lang::has(key)) {
tooltip = Some(atlas.font.split(&tr(&key), 170.0));
}
}
match &wd.kind {
WKind::Button { label, active, act } => {
ui::button(batch, atlas, wd.x, wd.y, wd.w, wd.h, label, *active, hover);
@@ -2161,6 +2201,78 @@ mod tests {
HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap()
}
#[test]
fn option_tooltip_keys_follow_value_dependent_descriptions() {
let mut s = Settings::default();
assert_eq!(
Opt::InactivityFps.tooltip_key(&s),
"options.inactivityFpsLimit.afk.tooltip"
);
s.inactivity_fps_limit = "minimized".into();
assert_eq!(
Opt::InactivityFps.tooltip_key(&s),
"options.inactivityFpsLimit.minimized.tooltip"
);
for (value, name) in ["none", "byPlayer", "nearby"].iter().enumerate() {
s.prioritize_chunk_updates = value as u8;
assert_eq!(
Opt::ChunkBuilder.tooltip_key(&s),
format!("options.prioritizeChunkUpdates.{name}.tooltip")
);
}
assert_eq!(
Opt::MenuBlur.tooltip_key(&s),
"options.accessibility.menu_background_blurriness.tooltip"
);
}
#[test]
#[ignore = "requires prepared language data in HORIZON_TEST_RESOURCES; run alone"]
fn classic_hover_draws_the_selected_options_wrapped_tooltip() {
let root = std::path::PathBuf::from(
std::env::var_os("HORIZON_TEST_RESOURCES").expect("HORIZON_TEST_RESOURCES"),
);
lang::set(lang::Language::load(&[&root], "en_us"));
let atlas = atlas();
let mut s = Settings::default();
let mut ui = OptionsUi::default();
ui.show(Page::Video);
for mode in ["afk", "minimized"] {
s.inactivity_fps_limit = mode.into();
let (widgets, _, _, _) = ui.layout(&s, 480.0, 480.0);
let option = widgets
.iter()
.find(|w| {
matches!(
w.kind,
WKind::Button {
act: Act::Cycle(Opt::InactivityFps),
..
}
)
})
.unwrap();
let cursor = (option.x + 1.0, option.y + 1.0);
let mut actual = HudBatch::new([480, 480]);
ui.draw(&s, &mut actual, &atlas, cursor);
let key = format!("options.inactivityFpsLimit.{mode}.tooltip");
assert!(lang::has(&key));
let mut expected = HudBatch::new([480, 480]);
ui::tooltip(
&mut expected,
&atlas,
&atlas.font.split(&tr(&key), 170.0),
cursor,
);
assert!(!expected.vertices.is_empty());
let tail = &actual.vertices[actual.vertices.len() - expected.vertices.len()..];
assert_eq!(
bytemuck::cast_slice::<_, u8>(tail),
bytemuck::cast_slice::<_, u8>(&expected.vertices)
);
}
}
#[test]
fn classic_video_gui_scale_cycles_on_press_without_dragging() {
let atlas = atlas();
@@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "ruby:block/ruby_block"
}
}
}
@@ -0,0 +1,6 @@
{
"model": {
"type": "minecraft:model",
"model": "ruby:item/ruby"
}
}
@@ -0,0 +1,6 @@
{
"model": {
"type": "minecraft:model",
"model": "ruby:block/ruby_block"
}
}
@@ -0,0 +1,4 @@
{
"block.ruby.ruby_block": "Block of Ruby",
"item.ruby.ruby": "Ruby"
}
@@ -0,0 +1,6 @@
{
"parent": "minecraft:block/cube_all",
"textures": {
"all": "ruby:block/ruby_block"
}
}
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "ruby:item/ruby"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 149 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 149 B

@@ -0,0 +1,5 @@
{
"values": [
"ruby:ruby_block"
]
}
@@ -0,0 +1,5 @@
{
"values": [
"ruby:ruby_block"
]
}
@@ -0,0 +1,9 @@
{
"template": "minecraft:iron_block",
"properties": {
"hardness": 4.0,
"light": 3,
"sounds": "minecraft:amethyst_block",
"map_color": 28
}
}
@@ -0,0 +1,4 @@
{
"template": "minecraft:diamond",
"rarity": "uncommon"
}
@@ -0,0 +1,18 @@
{
"type": "minecraft:block",
"pools": [
{
"condition": {
"type": "minecraft:survives_explosion"
},
"entries": [
{
"type": "minecraft:item",
"name": "ruby:ruby_block"
}
],
"rolls": 1
}
],
"random_sequence": "ruby:blocks/ruby_block"
}
@@ -0,0 +1,15 @@
{
"type": "minecraft:crafting_shaped",
"category": "building",
"key": {
"#": "ruby:ruby"
},
"pattern": [
"###",
"###",
"###"
],
"result": {
"id": "ruby:ruby_block"
}
}
@@ -0,0 +1,10 @@
{
"type": "minecraft:crafting_shapeless",
"ingredients": [
"ruby:ruby_block"
],
"result": {
"count": 9,
"id": "ruby:ruby"
}
}
@@ -0,0 +1,11 @@
# A content-only Horizon mod (API 0.2, docs/modpacks.md): no code, a block
# and an item declared from vanilla templates, with their recipes, loot,
# tags, models, textures and names. Try it: horizon-client --dev-mod <this folder>
id = "ruby"
name = "Ruby"
version = "0.1.0"
description = "A ruby gem and a block of ruby"
authors = ["Horizon"]
api = "0.2"
side = "both"
capabilities = ["registries"]
+1
View File
@@ -8,6 +8,7 @@ description = "Horizon's mod runtime: sandboxed WebAssembly components (docs/mod
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
toml = "1.1.6"
zip = { version = "2", default-features = false, features = ["deflate"] }
log = "0.4"
+557
View File
@@ -0,0 +1,557 @@
//! A content mod's `assets/` and `data/` (API 0.2, docs/modpacks.md): the
//! files it adds to the game's resource tree, and the blocks and items it
//! declares in `data/<id>/horizon/block/` and `data/<id>/horizon/item/`.
//!
//! Content is data, not code: nothing here runs in the sandbox. What a mod
//! may add is fenced instead. Its own namespace, plus additions to vanilla
//! tags. Paths are plain, files and totals are capped, and vanilla ids
//! cannot be redefined.
use crate::manifest::{Capability, MANIFEST, Manifest};
use serde::Deserialize;
use std::{
collections::BTreeSet,
io::Read,
path::{Path, PathBuf},
};
/// Content files per mod, bytes per file and bytes per mod.
pub const MAX_FILES: usize = 65_536;
pub const MAX_FILE: u64 = 16 << 20;
pub const MAX_TOTAL: u64 = 512 << 20;
/// Checks a content path: `assets/` or `data/`, then `/`-separated parts of
/// `[a-z0-9_.-]`, none of them `.` or `..` (vanilla's resource-location
/// characters, so the path means the same on every OS).
pub fn check_path(name: &str) -> Result<(), String> {
let bad = || format!("{name:?} is not a plain assets/ or data/ path");
let mut parts = name.split('/');
if !matches!(parts.next(), Some("assets" | "data")) {
return Err(bad());
}
let mut count = 0;
for part in parts {
count += 1;
let chars_ok = part
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"_.-".contains(&b));
if part.is_empty() || part == "." || part == ".." || !chars_ok {
return Err(bad());
}
}
// assets/<namespace>/<file> at least.
if count < 2 {
return Err(bad());
}
Ok(())
}
/// A vanilla identifier, `minecraft:path`.
fn vanilla_id(id: &str) -> bool {
id.strip_prefix("minecraft:").is_some_and(|path| {
!path.is_empty()
&& path
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"_./-".contains(&b))
})
}
/// What a declared block changes from its template; unset keeps the
/// template's value.
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct BlockProperties {
/// Destroy time in seconds of hand mining (-1: unbreakable).
pub hardness: Option<f32>,
pub explosion_resistance: Option<f32>,
/// Light emitted, 0 to 15.
pub light: Option<u8>,
pub requires_tool: Option<bool>,
pub friction: Option<f32>,
/// A `MapColor` id, 0 to 61.
pub map_color: Option<u8>,
/// A vanilla block whose sounds this block uses.
pub sounds: Option<String>,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct BlockFile {
template: String,
#[serde(default)]
properties: BlockProperties,
#[serde(default = "yes")]
item: bool,
}
fn yes() -> bool {
true
}
/// A block the mod adds: a vanilla template's behaviour, states and shapes
/// under a new id, with `properties` applied.
#[derive(Clone, Debug, PartialEq)]
pub struct Block {
/// `<mod id>:<path>`.
pub id: String,
/// The vanilla block it copies, `minecraft:<path>`.
pub template: String,
pub properties: BlockProperties,
/// Also add a block item with the same id.
pub item: bool,
}
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Rarity {
Common,
Uncommon,
Rare,
Epic,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct ItemFile {
template: String,
max_stack_size: Option<u8>,
rarity: Option<Rarity>,
}
/// An item the mod adds: a vanilla template item's components under a new
/// id, with these changed.
#[derive(Clone, Debug, PartialEq)]
pub struct Item {
pub id: String,
/// The vanilla item it copies, `minecraft:<path>`.
pub template: String,
pub max_stack_size: Option<u8>,
pub rarity: Option<Rarity>,
}
#[derive(Debug)]
enum Source {
Dir(PathBuf),
Zip(PathBuf),
}
/// A mod's content: its files and declarations.
#[derive(Debug)]
pub struct Content {
source: Source,
/// Every content file (`assets/...`, `data/...`), sorted.
pub files: Vec<String>,
pub blocks: Vec<Block>,
pub items: Vec<Item>,
}
impl Content {
/// The content of the mod at `path` (a `.hmod` or a mod folder).
pub fn read(path: &Path, manifest: &Manifest) -> Result<Content, String> {
let (source, sized) = if path.is_dir() {
let mut out = Vec::new();
for top in ["assets", "data"] {
walk(path, &path.join(top), &mut out)?;
}
(Source::Dir(path.to_owned()), out)
} else {
let mut zip = open_zip(path)?;
let mut out = Vec::new();
for i in 0..zip.len() {
let entry = zip.by_index_raw(i).map_err(|e| e.to_string())?;
let name = entry.name();
if (name.starts_with("assets/") || name.starts_with("data/")) && !entry.is_dir() {
out.push((name.to_owned(), entry.size()));
}
}
(Source::Zip(path.to_owned()), out)
};
let mut files = Vec::with_capacity(sized.len());
let mut total = 0u64;
for (name, size) in sized {
check_path(&name)?;
if size > MAX_FILE {
return Err(format!("{name} is over {} MiB", MAX_FILE >> 20));
}
total += size;
files.push(name);
}
if files.len() > MAX_FILES {
return Err(format!("more than {MAX_FILES} content files"));
}
if total > MAX_TOTAL {
return Err(format!("content is over {} MiB", MAX_TOTAL >> 20));
}
files.sort();
files.dedup();
let mut content = Content {
source,
files,
blocks: Vec::new(),
items: Vec::new(),
};
content.check(manifest)?;
Ok(content)
}
/// No files: a mod with code only.
pub fn is_empty(&self) -> bool {
self.files.is_empty()
}
/// One content file's bytes.
pub fn file(&self, name: &str) -> Result<Vec<u8>, String> {
check_path(name)?;
let mut out = Vec::new();
match &self.source {
Source::Dir(root) => {
let file =
std::fs::File::open(root.join(name)).map_err(|e| format!("{name}: {e}"))?;
file.take(MAX_FILE + 1)
.read_to_end(&mut out)
.map_err(|e| format!("{name}: {e}"))?;
}
Source::Zip(path) => {
let mut zip = open_zip(path)?;
let entry = zip.by_name(name).map_err(|e| format!("{name}: {e}"))?;
entry
.take(MAX_FILE + 1)
.read_to_end(&mut out)
.map_err(|e| format!("{name}: {e}"))?;
}
}
if out.len() as u64 > MAX_FILE {
return Err(format!("{name} is over {} MiB", MAX_FILE >> 20));
}
Ok(out)
}
/// Where content may go, and the declarations.
fn check(&mut self, manifest: &Manifest) -> Result<(), String> {
let id = manifest.id.as_str();
if !self.files.is_empty() && !manifest.capabilities.contains(&Capability::Registries) {
return Err(format!(
"has assets/ or data/ but not capability registries in {MANIFEST}"
));
}
let mut blocks = Vec::new();
let mut items = Vec::new();
for name in &self.files {
let mut parts = name.splitn(3, '/');
let (top, namespace, rest) = (
parts.next().unwrap_or(""),
parts.next().unwrap_or(""),
parts.next().unwrap_or(""),
);
let own = namespace == id;
// Vanilla's own files stay vanilla's; a mod may only add to its tags.
let allowed =
own || (top == "data" && namespace == "minecraft" && rest.starts_with("tags/"));
if !allowed {
return Err(format!(
"{name}: a mod adds files in its own namespace ({top}/{id}/) or to vanilla tags"
));
}
let Some(decl) = (own && top == "data")
.then(|| rest.strip_prefix("horizon/"))
.flatten()
else {
continue;
};
let parse = |kind: &str| -> Option<String> {
let path = decl
.strip_prefix(kind)?
.strip_prefix('/')?
.strip_suffix(".json")?;
Some(format!("{id}:{path}"))
};
if let Some(block) = parse("block") {
blocks.push((block, name.clone()));
} else if let Some(item) = parse("item") {
items.push((item, name.clone()));
} else {
return Err(format!(
"{name}: data/{id}/horizon/ holds block/*.json and item/*.json only"
));
}
}
let mut ids = BTreeSet::new();
for (block, name) in blocks {
let file: BlockFile =
serde_json::from_slice(&self.file(&name)?).map_err(|e| format!("{name}: {e}"))?;
check_block(&file).map_err(|e| format!("{name}: {e}"))?;
if file.item {
ids.insert(block.clone());
}
self.blocks.push(Block {
id: block,
template: file.template,
properties: file.properties,
item: file.item,
});
}
for (item, name) in items {
let file: ItemFile =
serde_json::from_slice(&self.file(&name)?).map_err(|e| format!("{name}: {e}"))?;
if !vanilla_id(&file.template) {
return Err(format!(
"{name}: template {:?} is not a vanilla item (minecraft:...)",
file.template
));
}
if file.max_stack_size.is_some_and(|n| !(1..=99).contains(&n)) {
return Err(format!("{name}: max_stack_size is 1 to 99"));
}
if !ids.insert(item.clone()) {
return Err(format!("{name}: {item} is also a block's item"));
}
self.items.push(Item {
id: item,
template: file.template,
max_stack_size: file.max_stack_size,
rarity: file.rarity,
});
}
Ok(())
}
}
fn check_block(file: &BlockFile) -> Result<(), String> {
if !vanilla_id(&file.template) {
return Err(format!(
"template {:?} is not a vanilla block (minecraft:...)",
file.template
));
}
let p = &file.properties;
let finite = |v: Option<f32>| v.is_none_or(f32::is_finite);
if !finite(p.hardness) || !finite(p.explosion_resistance) || !finite(p.friction) {
return Err("hardness, explosion_resistance and friction are numbers".into());
}
if p.hardness.is_some_and(|h| h < 0.0 && h != -1.0) {
return Err("hardness is 0 or more, or -1 (unbreakable)".into());
}
if p.explosion_resistance.is_some_and(|r| r < 0.0) {
return Err("explosion_resistance is 0 or more".into());
}
if p.friction.is_some_and(|f| !(0.0..=2.0).contains(&f)) {
return Err("friction is 0 to 2".into());
}
if p.light.is_some_and(|l| l > 15) {
return Err("light is 0 to 15".into());
}
if p.map_color.is_some_and(|c| c > 61) {
return Err("map_color is a MapColor id, 0 to 61".into());
}
if p.sounds.as_deref().is_some_and(|s| !vanilla_id(s)) {
return Err("sounds names a vanilla block (minecraft:...)".into());
}
Ok(())
}
fn open_zip(path: &Path) -> Result<zip::ZipArchive<std::fs::File>, String> {
let file = std::fs::File::open(path).map_err(|e| e.to_string())?;
zip::ZipArchive::new(file).map_err(|e| e.to_string())
}
/// Files under `dir`, relative to `root` with `/` separators. Symbolic links
/// are not followed: a mod folder holds its own files.
fn walk(root: &Path, dir: &Path, out: &mut Vec<(String, u64)>) -> Result<(), String> {
let Ok(read) = std::fs::read_dir(dir) else {
return Ok(());
};
for entry in read.flatten() {
let path = entry.path();
let meta = std::fs::symlink_metadata(&path).map_err(|e| e.to_string())?;
if meta.is_dir() {
walk(root, &path, out)?;
} else if meta.is_file() {
let rel = path.strip_prefix(root).map_err(|e| e.to_string())?;
let parts: Option<Vec<&str>> = rel.iter().map(|p| p.to_str()).collect();
let name = parts.ok_or_else(|| format!("{} is not UTF-8", rel.display()))?;
out.push((name.join("/"), meta.len()));
}
if out.len() > MAX_FILES {
return Err(format!("more than {MAX_FILES} content files"));
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::manifest::Package;
const MANIFEST_TEXT: &str = r#"
id = "ruby"
name = "Ruby"
version = "0.1.0"
api = "0.2"
side = "both"
capabilities = ["registries"]
"#;
const BLOCK: &[u8] = br#"{"template": "minecraft:iron_block",
"properties": {"hardness": 4.0, "light": 3, "sounds": "minecraft:amethyst_block"}}"#;
const ITEM: &[u8] = br#"{"template": "minecraft:diamond", "rarity": "uncommon"}"#;
fn scratch(name: &str) -> PathBuf {
let dir =
std::env::temp_dir().join(format!("horizon-content-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn hmod(name: &str, manifest: &str, files: &[(&str, &[u8])]) -> Result<Content, String> {
let dir = scratch(name);
let path = dir.join("m.hmod");
std::fs::write(&path, Package::write_files(manifest, None, files)?).unwrap();
let package = Package::read(&path);
let content = package.and_then(|p| Content::read(&path, &p.manifest));
std::fs::remove_dir_all(&dir).unwrap();
content
}
#[test]
fn content_only_mod_round_trips() {
let files: &[(&str, &[u8])] = &[
("data/ruby/horizon/block/ruby_block.json", BLOCK),
("data/ruby/horizon/item/ruby.json", ITEM),
("data/ruby/recipe/ruby_block.json", b"{}"),
("data/minecraft/tags/block/mineable/pickaxe.json", b"{}"),
("assets/ruby/textures/block/ruby_block.png", b"png"),
];
let content = hmod("ok", MANIFEST_TEXT, files).unwrap();
assert_eq!(content.files.len(), 5);
assert_eq!(
content.blocks,
[Block {
id: "ruby:ruby_block".into(),
template: "minecraft:iron_block".into(),
properties: BlockProperties {
hardness: Some(4.0),
light: Some(3),
sounds: Some("minecraft:amethyst_block".into()),
..BlockProperties::default()
},
item: true,
}]
);
assert_eq!(content.items[0].id, "ruby:ruby");
assert_eq!(content.items[0].rarity, Some(Rarity::Uncommon));
}
#[test]
fn folder_mod_reads_the_same() {
let dir = scratch("folder");
std::fs::write(dir.join(MANIFEST), MANIFEST_TEXT).unwrap();
let block = dir.join("data/ruby/horizon/block");
std::fs::create_dir_all(&block).unwrap();
std::fs::write(block.join("ruby_block.json"), BLOCK).unwrap();
let package = Package::read(&dir).unwrap();
assert!(package.wasm.is_none());
let content = Content::read(&dir, &package.manifest).unwrap();
assert_eq!(content.files, ["data/ruby/horizon/block/ruby_block.json"]);
assert_eq!(content.file(&content.files[0]).unwrap(), BLOCK);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn refuses_content_outside_its_fence() {
let refused = |name: &str, files: &[(&str, &[u8])]| {
let e = hmod(name, MANIFEST_TEXT, files).unwrap_err();
assert!(!e.is_empty());
e
};
let e = refused("other-ns", &[("data/other/horizon/block/a.json", BLOCK)]);
assert!(e.contains("own namespace"), "{e}");
refused(
"vanilla-asset",
&[("assets/minecraft/textures/block/stone.png", b"x")],
);
refused(
"vanilla-recipe",
&[("data/minecraft/recipe/stone.json", b"{}")],
);
let e = refused(
"modded-template",
&[(
"data/ruby/horizon/block/a.json",
br#"{"template": "ruby:other"}"#,
)],
);
assert!(e.contains("not a vanilla block"), "{e}");
refused(
"light",
&[(
"data/ruby/horizon/block/a.json",
br#"{"template": "minecraft:stone", "properties": {"light": 16}}"#,
)],
);
refused(
"unknown-key",
&[(
"data/ruby/horizon/block/a.json",
br#"{"template": "minecraft:stone", "glow": true}"#,
)],
);
let e = refused(
"clash",
&[
("data/ruby/horizon/block/a.json", BLOCK),
("data/ruby/horizon/item/a.json", ITEM),
],
);
assert!(e.contains("also a block's item"), "{e}");
refused("other-kind", &[("data/ruby/horizon/entity/a.json", b"{}")]);
refused(
"stack",
&[(
"data/ruby/horizon/item/a.json",
br#"{"template": "minecraft:stick", "max_stack_size": 100}"#,
)],
);
}
#[test]
fn content_needs_the_registries_capability() {
let code_only = MANIFEST_TEXT
.replace("api = \"0.2\"", "api = \"0.1\"")
.replace("capabilities = [\"registries\"]", "capabilities = []");
// No code and no registries: nothing to load.
let e = hmod("empty", &code_only, &[]).unwrap_err();
assert!(e.contains("mod.wasm: missing"), "{e}");
// Registries on API 0.1.
let old = MANIFEST_TEXT.replace("api = \"0.2\"", "api = \"0.1\"");
assert!(Manifest::parse(&old).unwrap_err().contains("needs api"));
// Content without the capability, in a mod with code.
let dir = scratch("no-cap");
let path = dir.join("m.hmod");
let files: &[(&str, &[u8])] = &[("assets/ruby/lang/en_us.json", b"{}")];
std::fs::write(
&path,
Package::write_files(&code_only, Some(b"\0asm"), files).unwrap(),
)
.unwrap();
let package = Package::read(&path).unwrap();
let e = Content::read(&path, &package.manifest).err().unwrap();
assert!(e.contains("not capability registries"), "{e}");
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn paths_are_plain() {
assert!(check_path("assets/ruby/textures/block/ruby_block.png").is_ok());
assert!(check_path("data/ruby/loot_table/blocks/a.json").is_ok());
for bad in [
"assets/ruby",
"assets/../x/y",
"assets/ruby//a",
"assets/Ruby/a",
"assets/ruby/a b",
"/assets/ruby/a",
"other/ruby/a",
"assets\\ruby\\a",
] {
assert!(check_path(bad).is_err(), "{bad}");
}
}
}
+2
View File
@@ -5,10 +5,12 @@
//!
//! Nothing here runs unless a mod is installed and turned on: an empty
//! [`Mods`] makes no engine and every hook returns at once.
pub mod content;
pub mod manifest;
#[cfg(feature = "runtime")]
mod runtime;
pub use content::Content;
pub use manifest::{Capability, Manifest, Package, Side};
#[cfg(feature = "runtime")]
pub use runtime::*;
+80 -30
View File
@@ -9,6 +9,10 @@ pub const WASM: &str = "mod.wasm";
/// The API (WIT package `horizon:mods`) versions this build runs: a mod
/// built for any 0.1.x SDK.
pub const API: &str = "0.1";
/// The manifest `api` lines this build loads. 0.2 is 0.1 plus content:
/// `assets/` and `data/` in the `.hmod`, with blocks and items declared in
/// `data/<id>/horizon/` (docs/modpacks.md). Code still links API 0.1.
pub const APIS: [&str; 2] = [API, "0.2"];
/// The biggest `mod.wasm` a mod may ship.
const MAX_WASM: u64 = 32 << 20;
@@ -23,7 +27,7 @@ pub struct Manifest {
pub description: String,
#[serde(default)]
pub authors: Vec<String>,
/// The API version the mod was built for: `0.1`.
/// The API line the mod was built for: `0.1`, or `0.2` for content.
pub api: String,
#[serde(default)]
pub side: Side,
@@ -51,15 +55,24 @@ pub enum Capability {
GameRead,
Chat,
Hud,
/// New blocks and items from the mod's `data/` (API 0.2): no WIT
/// interface, the game reads them before the world loads.
Registries,
}
impl Capability {
pub const ALL: [Capability; 3] = [Capability::GameRead, Capability::Chat, Capability::Hud];
pub const ALL: [Capability; 4] = [
Capability::GameRead,
Capability::Chat,
Capability::Hud,
Capability::Registries,
];
/// The WIT interface it grants, in `horizon:mods`.
pub fn interface(self) -> &'static str {
match self {
Capability::GameRead => "game",
Capability::Chat => "chat",
Capability::Hud => "hud",
Capability::Registries => "registries",
}
}
/// What the player reads before turning the mod on: "May <this>".
@@ -68,6 +81,7 @@ impl Capability {
Capability::GameRead => "see your position, health and the time",
Capability::Chat => "show you chat messages",
Capability::Hud => "draw text on your screen",
Capability::Registries => "add blocks and items to your worlds",
}
}
/// The capability a component import needs, `Ok(None)` for one every
@@ -84,6 +98,7 @@ impl Capability {
}
Capability::ALL
.into_iter()
.filter(|c| *c != Capability::Registries)
.find(|c| c.interface() == interface)
.map(Some)
.ok_or_else(unknown)
@@ -95,6 +110,7 @@ impl fmt::Display for Capability {
Capability::GameRead => "game-read",
Capability::Chat => "chat",
Capability::Hud => "hud",
Capability::Registries => "registries",
};
f.write_str(name)
}
@@ -113,8 +129,17 @@ impl Manifest {
m.id
));
}
if m.api != API {
return Err(format!("built for API {}; this Horizon runs {API}", m.api));
if !APIS.contains(&m.api.as_str()) {
return Err(format!(
"built for API {}; this Horizon runs {}",
m.api,
APIS.join(" and ")
));
}
if m.capabilities.contains(&Capability::Registries) && m.api == API {
return Err(format!(
"{MANIFEST}: capability registries needs api = \"0.2\""
));
}
Ok(m)
}
@@ -137,54 +162,79 @@ impl Manifest {
/// A mod as found on disk: its manifest and component bytes.
pub struct Package {
pub manifest: Manifest,
pub wasm: Vec<u8>,
/// None for a content-only mod (capability `registries`, no code).
pub wasm: Option<Vec<u8>>,
}
impl Package {
/// A `.hmod` (a zip holding `horizon-mod.toml` and `mod.wasm`) or a
/// folder holding the same two files.
/// A `.hmod` (a zip holding `horizon-mod.toml`, `mod.wasm` and, for
/// content, `assets/` and `data/`) or a folder holding the same.
pub fn read(path: &Path) -> Result<Self, String> {
let (toml, wasm) = if path.is_dir() {
let toml = std::fs::read_to_string(path.join(MANIFEST));
let wasm = std::fs::read(path.join(WASM));
(
toml.map_err(|e| format!("{MANIFEST}: {e}"))?,
wasm.map_err(|e| format!("{WASM}: {e}"))?,
)
let wasm = path.join(WASM);
let wasm = match wasm.exists() {
true => Some(std::fs::read(wasm).map_err(|e| format!("{WASM}: {e}"))?),
false => None,
};
(toml.map_err(|e| format!("{MANIFEST}: {e}"))?, wasm)
} else {
let file = std::fs::File::open(path).map_err(|e| e.to_string())?;
let mut zip = zip::ZipArchive::new(file).map_err(|e| e.to_string())?;
let mut entry = |name: &str| -> Result<Vec<u8>, String> {
let f = zip.by_name(name).map_err(|e| format!("{name}: {e}"))?;
let mut entry = |name: &str| -> Result<Option<Vec<u8>>, String> {
let f = match zip.by_name(name) {
Ok(f) => f,
Err(zip::result::ZipError::FileNotFound) => return Ok(None),
Err(e) => return Err(format!("{name}: {e}")),
};
let mut out = Vec::new();
f.take(MAX_WASM + 1)
.read_to_end(&mut out)
.map_err(|e| e.to_string())?;
Ok(out)
Ok(Some(out))
};
let toml = String::from_utf8(entry(MANIFEST)?)
.map_err(|_| format!("{MANIFEST}: not UTF-8"))?;
let toml = entry(MANIFEST)?.ok_or_else(|| format!("{MANIFEST}: missing"))?;
let toml = String::from_utf8(toml).map_err(|_| format!("{MANIFEST}: not UTF-8"))?;
(toml, entry(WASM)?)
};
if wasm.len() as u64 > MAX_WASM {
if wasm.as_ref().is_some_and(|w| w.len() as u64 > MAX_WASM) {
return Err(format!("{WASM} is over {} MiB", MAX_WASM >> 20));
}
Ok(Package {
manifest: Manifest::parse(&toml)?,
wasm,
})
let manifest = Manifest::parse(&toml)?;
if wasm.is_none() && !manifest.capabilities.contains(&Capability::Registries) {
return Err(format!(
"{WASM}: missing (only a content mod, with capability registries, may have no code)"
));
}
Ok(Package { manifest, wasm })
}
/// The `.hmod` bytes for `manifest_text` and a component.
pub fn write(manifest_text: &str, wasm: &[u8]) -> Result<Vec<u8>, String> {
Self::write_files(manifest_text, Some(wasm), &[])
}
/// The `.hmod` bytes for `manifest_text`, a component if the mod has
/// code, and content `files` (`assets/...`, `data/...`) by path.
pub fn write_files(
manifest_text: &str,
wasm: Option<&[u8]>,
files: &[(&str, &[u8])],
) -> Result<Vec<u8>, String> {
use std::io::Write;
Manifest::parse(manifest_text)?;
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let options = zip::write::SimpleFileOptions::default();
let err = |e: zip::result::ZipError| e.to_string();
zip.start_file(MANIFEST, options).map_err(err)?;
zip.write_all(manifest_text.as_bytes())
.map_err(|e| e.to_string())?;
zip.start_file(WASM, options).map_err(err)?;
zip.write_all(wasm).map_err(|e| e.to_string())?;
let mut put = |name: &str, bytes: &[u8]| -> Result<(), String> {
zip.start_file(name, options).map_err(err)?;
zip.write_all(bytes).map_err(|e| e.to_string())
};
put(MANIFEST, manifest_text.as_bytes())?;
if let Some(wasm) = wasm {
put(WASM, wasm)?;
}
for (name, bytes) in files {
crate::content::check_path(name)?;
put(name, bytes)?;
}
Ok(zip.finish().map_err(err)?.into_inner())
}
}
@@ -256,8 +306,8 @@ capabilities = ["game-read", "hud"]
fn refuses_bad_ids_and_apis() {
let bad_id = EXAMPLE.replace("coords_hud", "Coords HUD");
assert!(Manifest::parse(&bad_id).unwrap_err().contains("is not"));
let bad_api = EXAMPLE.replace("api = \"0.1\"", "api = \"0.2\"");
assert!(Manifest::parse(&bad_api).unwrap_err().contains("API 0.2"));
let bad_api = EXAMPLE.replace("api = \"0.1\"", "api = \"0.3\"");
assert!(Manifest::parse(&bad_api).unwrap_err().contains("API 0.3"));
let unknown = format!("{EXAMPLE}network = true\n");
assert!(Manifest::parse(&unknown).is_err());
}
@@ -270,7 +320,7 @@ capabilities = ["game-read", "hud"]
std::fs::write(&path, Package::write(EXAMPLE, b"\0asm").unwrap()).unwrap();
let p = Package::read(&path).unwrap();
assert_eq!(p.manifest.id, "coords_hud");
assert_eq!(p.wasm, b"\0asm");
assert_eq!(p.wasm.as_deref(), Some(&b"\0asm"[..]));
std::fs::remove_dir_all(&dir).unwrap();
}
}
+15 -7
View File
@@ -153,9 +153,12 @@ impl Mods {
let on = enabled(&package.manifest.id);
entry.manifest = Some(package.manifest.clone());
if on {
entry.status = match mods.start(&package, &reserved) {
// Content is read before the world loads; a mod whose
// content is refused stays off, with the reason.
let content = crate::Content::read(path, &package.manifest);
entry.status = match content.and_then(|_| mods.start(&package, &reserved)) {
Ok(run) => {
entry.run = Some(run);
entry.run = run;
Status::Running
}
Err(e) => Status::Failed(e),
@@ -184,12 +187,17 @@ impl Mods {
Ok(self.engine.as_ref().expect("made above"))
}
/// Starts a mod's code; `Ok(None)` for a content-only mod, which has
/// nothing to run (the game reads its content before the world loads).
fn start(
&mut self,
package: &Package,
reserved: &dyn Fn(&str) -> bool,
) -> Result<Running, String> {
) -> Result<Option<Running>, String> {
let m = &package.manifest;
let Some(wasm) = &package.wasm else {
return Ok(None);
};
if m.side == Side::Server {
return Err("server mods do not run yet".into());
}
@@ -202,7 +210,7 @@ impl Mods {
let snapshot = self.snapshot.clone();
let (engine, linker) = self.engine()?;
let component =
Component::new(engine, &package.wasm).map_err(|e| format!("not a component: {e}"))?;
Component::new(engine, wasm).map_err(|e| format!("not a component: {e}"))?;
let ty = component.component_type();
let imports: Vec<String> = ty
.imports(engine)
@@ -237,11 +245,11 @@ impl Mods {
free
});
self.out.append(&mut store.data_mut().out);
Ok(Running {
Ok(Some(Running {
store,
exports,
hooks,
})
}))
}
/// True when at least one mod runs: the game skips its mod work otherwise.
@@ -403,7 +411,7 @@ impl Mods {
match started {
Ok(run) => {
log::info!("Horizon: mod {id} reloaded");
entry.run = Some(run);
entry.run = run;
entry.status = Status::Running;
}
Err(e) => {
+33 -1
View File
@@ -131,7 +131,8 @@ fn undeclared_capabilities_are_refused() {
std::fs::create_dir_all(&dir).unwrap();
let wasm = Package::read(&mods_dir().join("horizon_mods_test_guest.hmod"))
.unwrap()
.wasm;
.wasm
.expect("the guest has code");
let manifest = GUEST_MANIFEST.replace("[\"chat\"]", "[]");
std::fs::write(dir.join("horizon-mod.toml"), manifest).unwrap();
std::fs::write(dir.join("mod.wasm"), wasm).unwrap();
@@ -241,3 +242,34 @@ fn hook_overhead() {
started.elapsed().as_secs_f64() * 1000.0
);
}
#[test]
fn content_only_mods_load_without_an_engine() {
let manifest = r#"
id = "ruby"
name = "Ruby"
version = "0.1.0"
api = "0.2"
side = "both"
capabilities = ["registries"]
"#;
let block: &[u8] = br#"{"template": "minecraft:iron_block"}"#;
let dir = mods_dir().join("content");
std::fs::create_dir_all(&dir).unwrap();
let ok = dir.join("ruby.hmod");
let files: &[(&str, &[u8])] = &[("data/ruby/horizon/block/ruby_block.json", block)];
std::fs::write(&ok, Package::write_files(manifest, None, files).unwrap()).unwrap();
let bad = dir.join("bad.hmod");
let files: &[(&str, &[u8])] = &[("data/minecraft/recipe/stone.json", b"{}")];
let bad_manifest = manifest.replace("\"ruby\"", "\"bad\"");
std::fs::write(
&bad,
Package::write_files(&bad_manifest, None, files).unwrap(),
)
.unwrap();
let mods = Mods::load(&[ok, bad], |_| true, |_| false);
assert_eq!(mods.entries()[0].status, Status::Running);
assert!(matches!(&mods.entries()[1].status, Status::Failed(e) if e.contains("own namespace")));
// Content has no hooks: the game's per-tick mod work stays skipped.
assert!(!mods.is_running());
}
+21
View File
@@ -0,0 +1,21 @@
[package]
name = "horizon-port-pack"
version = "0.1.0"
edition.workspace = true
license.workspace = true
rust-version = "1.86"
description = "Port a Java modpack to Horizon: import, inventory, classification and data carry-over (docs/modpacks.md)"
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sha1 = "0.10"
toml = "1.1.6"
ureq = { version = "2", default-features = false, features = ["tls"] }
zip = { version = "2", default-features = false, features = ["deflate"] }
[dev-dependencies]
horizon-mods = { path = "../horizon-mods", default-features = false }
[lints]
workspace = true
+440
View File
@@ -0,0 +1,440 @@
//! Step 4, carry-over: a jar's `assets/` and `data/` into a folder content
//! mod (API 0.2) for local use. Files are copied, never code; recipes are
//! 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 serde::Serialize;
use serde_json::{Value, json};
use std::{
collections::{BTreeMap, BTreeSet},
fs,
io::Read,
path::Path,
};
#[derive(Debug, Default, Serialize)]
pub struct Report {
pub copied: usize,
/// Files outside what a content mod may hold (other namespaces, vanilla
/// data other than tags), kept in `outside/` for review.
pub outside: Vec<String>,
pub recipes_upgraded: usize,
/// Recipes left as they were: custom types, NeoForge conditions.
pub recipes_kept: Vec<String>,
pub item_definitions_added: usize,
/// Declared blocks and their guessed templates.
pub blocks: BTreeMap<String, String>,
/// Blockstates whose properties match no template.
pub blocks_unknown: Vec<String>,
pub items: usize,
}
/// Vanilla blocks by their sorted property names.
const TEMPLATES: &[(&str, &str)] = &[
("", "minecraft:stone"),
("axis", "minecraft:oak_log"),
("type,waterlogged", "minecraft:oak_slab"),
("facing,half,shape,waterlogged", "minecraft:oak_stairs"),
("facing,half,hinge,open,powered", "minecraft:oak_door"),
(
"facing,half,open,powered,waterlogged",
"minecraft:oak_trapdoor",
),
("east,north,south,waterlogged,west", "minecraft:oak_fence"),
(
"east,north,south,up,waterlogged,west",
"minecraft:cobblestone_wall",
),
("facing,in_wall,open,powered", "minecraft:oak_fence_gate"),
("face,facing,powered", "minecraft:oak_button"),
("powered", "minecraft:oak_pressure_plate"),
("facing", "minecraft:carved_pumpkin"),
("facing,lit", "minecraft:furnace"),
("lit", "minecraft:redstone_lamp"),
("waterlogged", "minecraft:chain"),
("age", "minecraft:wheat"),
("distance,persistent,waterlogged", "minecraft:oak_leaves"),
("stage", "minecraft:oak_sapling"),
];
pub fn template(properties: &BTreeSet<String>) -> Option<&'static str> {
let key = properties.iter().cloned().collect::<Vec<_>>().join(",");
TEMPLATES.iter().find(|(k, _)| *k == key).map(|(_, t)| *t)
}
/// The property names a blockstate file uses.
pub fn properties(blockstate: &Value) -> BTreeSet<String> {
let mut out = BTreeSet::new();
let mut add = |pairs: &str| {
for pair in pairs.split(',').filter(|p| !p.is_empty()) {
out.insert(pair.split('=').next().unwrap_or(pair).to_owned());
}
};
for key in blockstate["variants"]
.as_object()
.into_iter()
.flatten()
.map(|(k, _)| k)
{
add(key);
}
for part in blockstate["multipart"].as_array().into_iter().flatten() {
let when = &part["when"];
let mut conditions: Vec<&Value> = vec![when];
for or in ["OR", "AND"] {
conditions.extend(when[or].as_array().into_iter().flatten());
}
for c in conditions {
for (k, _) in c.as_object().into_iter().flatten() {
if k != "OR" && k != "AND" {
out.insert(k.clone());
}
}
}
}
out
}
/// A 1.21.1 ingredient (`{"item": id}`, `{"tag": t}` or a list of those) as
/// 26.3's (`"id"`, `"#t"`, `["id", ...]`); `None` for what has no 26.3 form
/// (custom ingredient types, tags inside lists).
pub fn ingredient(v: &Value) -> Option<Value> {
match v {
Value::String(_) => Some(v.clone()),
Value::Object(o) if o.len() == 1 => {
if let Some(Value::String(id)) = o.get("item") {
Some(json!(id))
} else if let Some(Value::String(tag)) = o.get("tag") {
Some(json!(format!("#{tag}")))
} else {
None
}
}
Value::Array(list) => {
let ids: Option<Vec<Value>> = list
.iter()
.map(|x| {
x.get("item")
.filter(|_| x.as_object().is_some_and(|o| o.len() == 1))
.cloned()
})
.collect();
match ids? {
ids if ids.len() == 1 => ids.into_iter().next(),
ids => Some(Value::Array(ids)),
}
}
_ => None,
}
}
/// A vanilla recipe upgraded in place; `false` (unchanged) when it is not
/// a vanilla type or one of its ingredients has no 26.3 form.
pub fn upgrade_recipe(recipe: &mut Value) -> bool {
let kind = recipe["type"].as_str().unwrap_or("");
if !kind.starts_with("minecraft:") || recipe.get("neoforge:conditions").is_some() {
return false;
}
let mut out = recipe.clone();
if let Some(key) = out.get_mut("key").and_then(Value::as_object_mut) {
for v in key.values_mut() {
let Some(new) = ingredient(v) else {
return false;
};
*v = new;
}
}
if let Some(list) = out.get_mut("ingredients").and_then(Value::as_array_mut) {
for v in list.iter_mut() {
let Some(new) = ingredient(v) else {
return false;
};
*v = new;
}
}
for field in ["ingredient", "base", "addition", "template"] {
if let Some(v) = out.get_mut(field) {
let Some(new) = ingredient(v) else {
return false;
};
*v = new;
}
}
*recipe = out;
true
}
/// Where a jar entry goes: `Some(true)` inside the content mod, `Some(false)`
/// to `outside/`, `None` not carried (code, metadata).
fn place(name: &str, id: &str) -> Option<bool> {
let mut parts = name.splitn(3, '/');
let (root, ns, rest) = (parts.next()?, parts.next()?, parts.next()?);
if !(root == "assets" || root == "data") || rest.is_empty() || name.ends_with('/') {
return None;
}
Some(ns == id || (root == "data" && ns == "minecraft" && rest.starts_with("tags/")))
}
pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result<Report, String> {
let id = &info.mod_id;
if id.is_empty()
|| id.len() > 64
|| !id
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_')
{
return Err(format!(
"mod id {id:?} is not [a-z0-9_]{{1,64}}: rename it before carrying over"
));
}
let file = fs::File::open(jar).map_err(|e| format!("{}: {e}", jar.display()))?;
let mut zip = zip::ZipArchive::new(file).map_err(|e| format!("{}: {e}", jar.display()))?;
let (inside, outside) = (out.join("carry"), out.join("outside"));
let _ = fs::remove_dir_all(&inside);
let _ = fs::remove_dir_all(&outside);
let mut report = Report::default();
let mut blockstates: BTreeMap<String, Value> = BTreeMap::new();
let mut item_models = 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();
let Some(keep) = place(&name, id) else {
continue;
};
if name.split('/').any(|p| p == ".." || p.is_empty()) {
continue;
}
let mut bytes = Vec::new();
entry
.read_to_end(&mut bytes)
.map_err(|e| format!("{name}: {e}"))?;
let dest = if keep {
report.copied += 1;
inside.join(&name)
} else {
report.outside.push(name.clone());
outside.join(&name)
};
if keep {
let rel = name.split_once(&format!("/{id}/")).map_or("", |(_, r)| r);
if let Some(file) = rel
.strip_prefix("recipe/")
.filter(|_| name.starts_with("data/"))
{
if let Ok(mut recipe) = serde_json::from_slice::<Value>(&bytes) {
if upgrade_recipe(&mut recipe) {
report.recipes_upgraded += 1;
bytes = serde_json::to_vec_pretty(&recipe).unwrap_or(bytes);
} else {
report.recipes_kept.push(file.to_owned());
}
}
} else if let Some(b) = rel
.strip_prefix("blockstates/")
.and_then(|b| b.strip_suffix(".json"))
{
if let Ok(v) = serde_json::from_slice(&bytes) {
blockstates.insert(b.to_owned(), v);
}
} else if let Some(m) = rel
.strip_prefix("models/item/")
.and_then(|m| m.strip_suffix(".json"))
{
item_models.insert(m.to_owned());
}
}
write(&dest, &bytes)?;
}
// 1.21.4 moved item models behind `items/<name>.json` definitions.
for m in &item_models {
let def = inside.join(format!("assets/{id}/items/{m}.json"));
if !def.exists() {
let model =
json!({"model": {"type": "minecraft:model", "model": format!("{id}:item/{m}")}});
write(&def, &serde_json::to_vec_pretty(&model).unwrap_or_default())?;
report.item_definitions_added += 1;
}
}
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());
}
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;
}
let q = |s: &str| toml::Value::String(s.to_owned()).to_string();
let name = if info.name.is_empty() { id } else { &info.name };
let manifest = format!(
"# Carried over from {jar} by horizon-port-pack, for local use only.\n# Licence: {license}\nid = {}\nname = {}\nversion = {}\napi = \"0.2\"\nside = \"both\"\ncapabilities = [\"registries\"]\n",
q(id),
q(name),
q(&info.version),
jar = jar.file_name().unwrap_or_default().to_string_lossy(),
license = if info.license.is_empty() {
"not stated"
} else {
&info.license
},
);
write(&inside.join("horizon-mod.toml"), manifest.as_bytes())?;
write(
&out.join("carry-report.json"),
&serde_json::to_vec_pretty(&report).unwrap_or_default(),
)?;
Ok(report)
}
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()))?;
}
fs::write(path, bytes).map_err(|e| format!("{}: {e}", path.display()))
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
#[test]
fn upgrades_ingredients() {
let mut r = json!({"type": "minecraft:crafting_shaped", "pattern": ["##"],
"key": {"#": {"item": "gem:gem"}, "s": {"tag": "c:rods/wooden"}},
"result": {"id": "gem:gem_block", "count": 1}});
assert!(upgrade_recipe(&mut r));
assert_eq!(r["key"], json!({"#": "gem:gem", "s": "#c:rods/wooden"}));
let mut custom = json!({"type": "gem:infuse", "ingredient": {"item": "gem:gem"}});
assert!(!upgrade_recipe(&mut custom));
let mut tag_list = json!({"type": "minecraft:smelting",
"ingredient": [{"tag": "c:ores"}, {"item": "gem:gem"}]});
assert!(!upgrade_recipe(&mut tag_list));
}
#[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"));
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": {"": {}}}))),
Some("minecraft:stone")
);
let odd = json!({"variants": {"charge=1,mode=fast": {}}});
assert_eq!(template(&properties(&odd)), None);
}
/// A small NeoForge jar carried over is a content mod Horizon loads.
#[test]
fn carried_jar_is_a_content_mod() {
let dir = std::env::temp_dir().join(format!("horizon-port-pack-{}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
let jar = dir.join("gem-1.0.jar");
let mut zip = zip::ZipWriter::new(fs::File::create(&jar).unwrap());
let files: &[(&str, &str)] = &[
(
"META-INF/neoforge.mods.toml",
"license = \"MIT\"\n[[mods]]\nmodId = \"gem\"\nversion = \"1.0\"\ndisplayName = \"Gem\"\n",
),
("com/example/gem/Gem.class", "not real bytecode"),
(
"assets/gem/blockstates/gem_block.json",
r#"{"variants": {"": {"model": "gem:block/gem_block"}}}"#,
),
(
"assets/gem/blockstates/gem_door.json",
r#"{"variants": {"facing=east,half=lower,hinge=left,open=false,powered=false": {"model": "gem:block/gem_door"}}}"#,
),
(
"assets/gem/blockstates/gem_core.json",
r#"{"variants": {"charge=1": {"model": "gem:block/gem_core"}}}"#,
),
(
"assets/gem/models/item/gem.json",
r#"{"parent": "minecraft:item/generated", "textures": {"layer0": "gem:item/gem"}}"#,
),
(
"assets/gem/models/item/gem_block.json",
r#"{"parent": "gem:block/gem_block"}"#,
),
("assets/gem/textures/item/gem.png", "png"),
("assets/gem/lang/en_us.json", r#"{"item.gem.gem": "Gem"}"#),
(
"data/gem/recipe/gem_block.json",
r####"{"type": "minecraft:crafting_shaped", "pattern": ["###", "###", "###"],
"key": {"#": {"item": "gem:gem"}}, "result": {"id": "gem:gem_block", "count": 1}}"####,
),
(
"data/gem/recipe/infuse.json",
r#"{"type": "gem:infuse", "ingredient": {"item": "gem:gem"}}"#,
),
(
"data/gem/loot_table/blocks/gem_block.json",
r#"{"type": "minecraft:block", "pools": []}"#,
),
(
"data/minecraft/tags/block/mineable/pickaxe.json",
r#"{"values": ["gem:gem_block"]}"#,
),
("data/c/tags/item/gems.json", r#"{"values": ["gem:gem"]}"#),
];
for (name, text) in files {
zip.start_file(*name, zip::write::SimpleFileOptions::default())
.unwrap();
zip.write_all(text.as_bytes()).unwrap();
}
zip.finish().unwrap();
let info = crate::jar::read(&jar).unwrap();
assert_eq!(
(info.mod_id.as_str(), info.license.as_str()),
("gem", "MIT")
);
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"]);
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.items, 1);
let carried = out.join("carry");
assert!(!carried.join("com").exists());
let recipe: Value = serde_json::from_slice(
&fs::read(carried.join("data/gem/recipe/gem_block.json")).unwrap(),
)
.unwrap();
assert_eq!(recipe["key"]["#"], "gem:gem");
// Horizon's own reader accepts it: blocks and items declared, files fenced.
let package = horizon_mods::Package::read(&carried).unwrap();
let content = horizon_mods::Content::read(&carried, &package.manifest).unwrap();
let blocks: Vec<&str> = content.blocks.iter().map(|b| b.id.as_str()).collect();
assert_eq!(blocks, ["gem:gem_block", "gem:gem_door"]);
assert_eq!(content.items.len(), 1);
assert_eq!(content.items[0].id, "gem:gem");
fs::remove_dir_all(&dir).unwrap();
}
}
+233
View File
@@ -0,0 +1,233 @@
//! Step 2 and 3: classify each mod (class and API tier, `docs/modpacks.md`)
//! and order them by required dependencies.
use std::collections::{BTreeMap, HashMap};
/// The reviewed table: ATM10's 495 rows (`docs/modpacks/atm10-mods.csv`).
/// It wins over the heuristics.
const KNOWN_CSV: &str = include_str!("../../../docs/modpacks/atm10-mods.csv");
pub struct Known {
by_key: HashMap<String, (String, String)>,
}
/// Lowercase letters and digits only: `Get It Together, Drops!` ->
/// `getittogetherdrops`.
pub fn normalise(s: &str) -> String {
s.chars()
.filter(char::is_ascii_alphanumeric)
.map(|c| c.to_ascii_lowercase())
.collect()
}
/// One CSV line, with `"quoted, fields"`.
fn csv_fields(line: &str) -> Vec<String> {
let (mut out, mut field, mut quoted) = (Vec::new(), String::new(), false);
let mut chars = line.chars().peekable();
while let Some(c) = chars.next() {
match c {
'"' if quoted && chars.peek() == Some(&'"') => {
field.push('"');
chars.next();
}
'"' => quoted = !quoted,
',' if !quoted => out.push(std::mem::take(&mut field)),
c => field.push(c),
}
}
out.push(field);
out
}
impl Known {
pub fn atm10() -> Self {
let mut by_key = HashMap::new();
for line in KNOWN_CSV.lines().skip(1) {
let f = csv_fields(line);
if f.len() < 6 || f[5] == "guess" {
continue;
}
let value = (f[3].clone(), f[4].clone());
by_key.insert(normalise(&f[0]), value.clone());
by_key.entry(normalise(&f[1])).or_insert(value);
}
Known { by_key }
}
/// By mod id, Modrinth slug or name, in that order.
pub fn get(&self, keys: &[&str]) -> Option<&(String, String)> {
keys.iter()
.filter(|k| !k.is_empty())
.find_map(|k| self.by_key.get(&normalise(k)))
}
}
/// What the rules look at.
#[derive(Default)]
pub struct Facts<'a> {
/// `mods`, `resourcepacks`, `shaderpacks`, `datapacks`.
pub kind: &'a str,
pub client: &'a str,
pub server: &'a str,
/// Modrinth categories.
pub categories: &'a [String],
/// From the jar: classes, and whether it has blocks, items or data.
pub classes: Option<usize>,
pub has_content: bool,
}
/// Class, tier and confidence (`known` is set by the caller).
pub fn rules(f: &Facts) -> (&'static str, &'static str, &'static str) {
let has = |c: &str| f.categories.iter().any(|x| x == c);
match f.kind {
"resourcepacks" => return ("other", "data", "kind"),
"shaderpacks" => return ("other", "none", "kind"),
"datapacks" => return ("content", "data", "kind"),
_ => {}
}
if has("library") {
return ("library", "none", "categories");
}
if has("optimization") {
return ("performance", "none", "categories");
}
if f.client != "unsupported" && f.server == "unsupported" {
return ("client-ui", "client", "sides");
}
if f.client == "unsupported" && f.server != "unsupported" && !f.server.is_empty() {
return ("server-only", "logic", "sides");
}
if f.classes == Some(0) && f.has_content {
return ("content", "data", "jar");
}
let content = [
"adventure",
"decoration",
"equipment",
"food",
"game-mechanics",
"magic",
"mobs",
"storage",
"technology",
"transportation",
"worldgen",
];
if f.categories.iter().any(|c| content.contains(&c.as_str())) {
let tier = if has("mobs") || has("magic") || has("adventure") {
"deep"
} else if has("technology") || has("storage") || has("transportation") {
"machines"
} else if f.categories.iter().all(|c| {
["worldgen", "decoration", "food"].contains(&c.as_str())
|| !content.contains(&c.as_str())
}) {
"data"
} else {
"logic"
};
return ("content", tier, "categories");
}
if has("utility") || has("management") || has("social") {
return ("other", "logic", "guess");
}
("unknown", "none", "unknown")
}
/// Layer of each mod: 0 needs nothing in the pack, n needs layer n-1.
/// Mods in a cycle get one layer past the rest; dependencies outside the
/// pack are left out (the caller lists them).
pub fn layers(depends: &BTreeMap<String, Vec<String>>) -> BTreeMap<String, usize> {
let mut out: BTreeMap<String, usize> = BTreeMap::new();
loop {
let mut progressed = false;
for (id, deps) in depends {
if out.contains_key(id) {
continue;
}
let inside = deps.iter().filter(|d| depends.contains_key(*d) && *d != id);
let mut layer = 0;
let mut ready = true;
for d in inside {
match out.get(d) {
Some(l) => layer = layer.max(l + 1),
None => ready = false,
}
}
if ready {
out.insert(id.clone(), layer);
progressed = true;
}
}
if !progressed {
break;
}
}
let past = out.values().max().map_or(0, |m| m + 1);
for id in depends.keys() {
out.entry(id.clone()).or_insert(past);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn known_table_matches_ids_and_names() {
let k = Known::atm10();
assert_eq!(k.get(&["create"]).map(|v| v.0.as_str()), Some("content"));
assert_eq!(
k.get(&["", "Get It Together, Drops!"])
.map(|v| v.0.as_str()),
Some("performance")
);
assert_eq!(k.get(&["sodium"]).map(|v| v.1.as_str()), Some("none"));
assert!(k.get(&["not-a-mod"]).is_none());
}
#[test]
fn rules_follow_sides_then_categories() {
let cats = |c: &[&str]| c.iter().map(|s| s.to_string()).collect::<Vec<_>>();
let f = |client, server, c: &[String]| {
rules(&Facts {
kind: "mods",
client,
server,
categories: c,
..Facts::default()
})
};
assert_eq!(f("required", "unsupported", &[]).0, "client-ui");
assert_eq!(f("unsupported", "required", &[]).0, "server-only");
assert_eq!(f("required", "required", &cats(&["library"])).0, "library");
assert_eq!(
f("required", "unsupported", &cats(&["optimization"])).0,
"performance"
);
assert_eq!(
f("required", "required", &cats(&["technology", "utility"])),
("content", "machines", "categories")
);
assert_eq!(
f("required", "required", &cats(&["worldgen", "neoforge"])),
("content", "data", "categories")
);
assert_eq!(f("", "", &[]).0, "unknown");
}
#[test]
fn layers_put_dependencies_first() {
let d = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
let deps = BTreeMap::from([
("lib".to_string(), d(&[])),
("create".to_string(), d(&["lib", "outside"])),
("addon".to_string(), d(&["create", "lib"])),
("a".to_string(), d(&["b"])),
("b".to_string(), d(&["a"])),
]);
let l = layers(&deps);
assert_eq!((l["lib"], l["create"], l["addon"]), (0, 1, 2));
assert_eq!((l["a"], l["b"]), (3, 3));
}
}
+335
View File
@@ -0,0 +1,335 @@
//! Step 1, import: a Modrinth `.mrpack`, a CurseForge zip, or an instance
//! folder (Prism / MultiMC, or a plain `mods/` folder) to `pack.json`.
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
collections::BTreeMap,
fs,
io::Read,
path::{Path, PathBuf},
};
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Pack {
pub name: String,
/// `mrpack`, `curseforge` or `instance`.
pub format: String,
pub minecraft: String,
/// `neoforge`, `forge`, `fabric` or `quilt`.
pub loader: String,
pub loader_version: String,
pub files: Vec<PackFile>,
/// Override files by top folder (`config`, `kubejs`, ...).
pub overrides: BTreeMap<String, usize>,
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct PackFile {
/// Path in the instance (`mods/create-1.21.1-6.0.4.jar`).
pub path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sha1: Option<String>,
/// `required`, `optional` or `unsupported`; empty when not stated.
pub client: String,
pub server: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub downloads: Vec<String>,
/// CurseForge project and file ids.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub curseforge: Option<(u64, u64)>,
/// The jar on disk (instances only).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub jar: Option<PathBuf>,
}
impl PackFile {
/// `mods`, `resourcepacks`, `shaderpacks`, `datapacks`...
pub fn kind(&self) -> &str {
self.path.split('/').next().unwrap_or("")
}
pub fn file_name(&self) -> &str {
self.path.rsplit('/').next().unwrap_or(&self.path)
}
}
const LOADERS: [(&str, &str); 4] = [
("neoforge", "neoforge"),
("forge", "forge"),
("fabric-loader", "fabric"),
("quilt-loader", "quilt"),
];
pub fn import(path: &Path) -> Result<Pack, String> {
if path.is_dir() {
return instance(path);
}
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()))?;
if let Some(index) = read_json(&mut zip, "modrinth.index.json")? {
return Ok(mrpack(&index, &names(&zip)));
}
if let Some(manifest) = read_json(&mut zip, "manifest.json")? {
return Ok(curseforge(&manifest, &names(&zip)));
}
Err(format!(
"{}: neither modrinth.index.json nor manifest.json",
path.display()
))
}
fn names(zip: &zip::ZipArchive<fs::File>) -> Vec<String> {
zip.file_names().map(str::to_owned).collect()
}
fn read_json(zip: &mut zip::ZipArchive<fs::File>, name: &str) -> Result<Option<Value>, String> {
let Ok(mut entry) = zip.by_name(name) else {
return Ok(None);
};
let mut text = String::new();
entry
.read_to_string(&mut text)
.map_err(|e| format!("{name}: {e}"))?;
serde_json::from_str(&text)
.map(Some)
.map_err(|e| format!("{name}: {e}"))
}
fn str_of(v: &Value, key: &str) -> String {
v.get(key).and_then(Value::as_str).unwrap_or("").to_owned()
}
/// Override files by top folder, below any of `roots`.
fn overrides(names: &[String], roots: &[&str]) -> BTreeMap<String, usize> {
let mut out = BTreeMap::new();
for name in names.iter().filter(|n| !n.ends_with('/')) {
for root in roots {
if let Some(rest) = name.strip_prefix(root).and_then(|r| r.strip_prefix('/')) {
let top = rest.split('/').next().unwrap_or(rest);
*out.entry(top.to_owned()).or_default() += 1;
}
}
}
out
}
pub fn mrpack(index: &Value, names: &[String]) -> Pack {
let deps = &index["dependencies"];
let (loader, loader_version) = LOADERS
.iter()
.find_map(|(key, name)| Some((name.to_string(), deps.get(*key)?.as_str()?.to_owned())))
.unwrap_or_default();
let files = index["files"]
.as_array()
.into_iter()
.flatten()
.map(|f| PackFile {
path: str_of(f, "path"),
sha1: f["hashes"]["sha1"].as_str().map(str::to_owned),
client: str_of(&f["env"], "client"),
server: str_of(&f["env"], "server"),
downloads: f["downloads"]
.as_array()
.into_iter()
.flatten()
.filter_map(|d| d.as_str().map(str::to_owned))
.collect(),
..PackFile::default()
})
.collect();
Pack {
name: str_of(index, "name"),
format: "mrpack".into(),
minecraft: str_of(deps, "minecraft"),
loader,
loader_version,
files,
overrides: overrides(
names,
&["overrides", "client-overrides", "server-overrides"],
),
}
}
pub fn curseforge(manifest: &Value, names: &[String]) -> Pack {
let primary = manifest["minecraft"]["modLoaders"]
.as_array()
.into_iter()
.flatten()
.find(|l| l["primary"].as_bool().unwrap_or(true))
.map(|l| str_of(l, "id"))
.unwrap_or_default();
let (loader, loader_version) = primary.split_once('-').unwrap_or((&primary, ""));
let files = manifest["files"]
.as_array()
.into_iter()
.flatten()
.map(|f| {
let (p, id) = (f["projectID"].as_u64(), f["fileID"].as_u64());
PackFile {
path: format!("mods/curseforge-{}-{}", p.unwrap_or(0), id.unwrap_or(0)),
// CurseForge states no side; its API needs a key, so names
// come from the user's installed instance (import that).
curseforge: p.zip(id),
..PackFile::default()
}
})
.collect();
let root = manifest["overrides"].as_str().unwrap_or("overrides");
Pack {
name: str_of(manifest, "name"),
format: "curseforge".into(),
minecraft: str_of(&manifest["minecraft"], "version"),
loader: loader.to_owned(),
loader_version: loader_version.to_owned(),
files,
overrides: overrides(names, &[root]),
}
}
/// A Prism / MultiMC instance (`mmc-pack.json`), a game folder, or a
/// folder of jars: the jars on disk, hashed.
fn instance(dir: &Path) -> Result<Pack, String> {
let mut pack = Pack {
name: dir
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default(),
format: "instance".into(),
..Pack::default()
};
if let Ok(text) = fs::read_to_string(dir.join("mmc-pack.json")) {
let mmc: Value = serde_json::from_str(&text).map_err(|e| format!("mmc-pack.json: {e}"))?;
for c in mmc["components"].as_array().into_iter().flatten() {
let version = str_of(c, "version");
match c["uid"].as_str().unwrap_or("") {
"net.minecraft" => pack.minecraft = version,
"net.neoforged" => {
(pack.loader, pack.loader_version) = ("neoforge".into(), version)
}
"net.minecraftforge" => {
(pack.loader, pack.loader_version) = ("forge".into(), version)
}
"net.fabricmc.fabric-loader" => {
(pack.loader, pack.loader_version) = ("fabric".into(), version)
}
"org.quiltmc.quilt-loader" => {
(pack.loader, pack.loader_version) = ("quilt".into(), version)
}
_ => {}
}
}
}
let game = [".minecraft", "minecraft", "."]
.iter()
.map(|g| dir.join(g))
.find(|g| g.join("mods").is_dir())
.or_else(|| dir.read_dir().is_ok().then(|| dir.to_path_buf()))
.ok_or_else(|| format!("{}: no mods folder", dir.display()))?;
let mods = if game.join("mods").is_dir() {
game.join("mods")
} else {
game.clone()
};
let mut jars: Vec<PathBuf> = fs::read_dir(&mods)
.map_err(|e| format!("{}: {e}", mods.display()))?
.flatten()
.map(|e| e.path())
.filter(|p| p.extension().is_some_and(|x| x == "jar"))
.collect();
jars.sort();
for jar in jars {
let bytes = fs::read(&jar).map_err(|e| format!("{}: {e}", jar.display()))?;
pack.files.push(PackFile {
path: format!(
"mods/{}",
jar.file_name().unwrap_or_default().to_string_lossy()
),
sha1: Some(sha1_hex(&bytes)),
jar: Some(jar),
..PackFile::default()
});
}
for top in ["config", "kubejs", "defaultconfigs", "datapacks"] {
let n = count_files(&game.join(top));
if n > 0 {
pack.overrides.insert(top.into(), n);
}
}
Ok(pack)
}
fn count_files(dir: &Path) -> usize {
let Ok(entries) = fs::read_dir(dir) else {
return 0;
};
entries
.flatten()
.map(|e| {
let p = e.path();
if p.is_dir() { count_files(&p) } else { 1 }
})
.sum()
}
pub fn sha1_hex(bytes: &[u8]) -> String {
use sha1::Digest;
sha1::Sha1::digest(bytes)
.iter()
.map(|b| format!("{b:02x}"))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn reads_an_mrpack_index() {
let index = json!({"name": "Small", "dependencies": {"minecraft": "1.21.1", "neoforge": "21.1.233"},
"files": [{"path": "mods/create.jar", "hashes": {"sha1": "ab"}, "env": {"client": "required", "server": "required"},
"downloads": ["https://cdn.modrinth.com/create.jar"]},
{"path": "resourcepacks/look.zip", "hashes": {"sha1": "cd"}}]});
let names = [
"overrides/config/a.toml",
"overrides/config/b/c.toml",
"client-overrides/kubejs/x.js",
"overrides/",
]
.map(String::from);
let p = mrpack(&index, &names);
assert_eq!(
(
p.minecraft.as_str(),
p.loader.as_str(),
p.loader_version.as_str()
),
("1.21.1", "neoforge", "21.1.233")
);
assert_eq!(p.files.len(), 2);
assert_eq!(
(p.files[0].kind(), p.files[0].file_name()),
("mods", "create.jar")
);
assert_eq!(p.files[0].sha1.as_deref(), Some("ab"));
assert_eq!(p.files[1].kind(), "resourcepacks");
assert_eq!(
p.overrides,
BTreeMap::from([("config".into(), 2), ("kubejs".into(), 1)])
);
}
#[test]
fn reads_a_curseforge_manifest() {
let manifest = json!({"name": "CF", "overrides": "overrides",
"minecraft": {"version": "1.21.1", "modLoaders": [{"id": "neoforge-21.1.259", "primary": true}]},
"files": [{"projectID": 238222, "fileID": 5101366, "required": true}]});
let p = curseforge(&manifest, &["overrides/kubejs/server_scripts/a.js".into()]);
assert_eq!(
(p.loader.as_str(), p.loader_version.as_str()),
("neoforge", "21.1.259")
);
assert_eq!(p.files[0].curseforge, Some((238222, 5101366)));
assert_eq!(p.overrides["kubejs"], 1);
}
}
+225
View File
@@ -0,0 +1,225 @@
//! Step 2, inventory: one record per pack file, from the jar (when the
//! user's instance has it) and Modrinth's metadata, classified and ordered.
use crate::{
classify::{self, Facts, Known},
import::Pack,
jar::{self, JarInfo},
modrinth::Cache,
};
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, HashMap},
path::{Path, PathBuf},
};
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Record {
/// The mod id from the jar, else the Modrinth slug, else the file name.
pub key: String,
pub name: String,
pub version: String,
pub path: String,
pub class: String,
pub tier: String,
/// `known` (reviewed table), `sides`, `categories`, `jar`, `kind`,
/// `guess` or `unknown`.
pub confidence: String,
/// `modrinth`, `curseforge` or `file`.
pub source: String,
pub license: String,
pub client: String,
pub server: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub categories: Vec<String>,
/// Required dependencies inside the pack, by key.
pub depends: Vec<String>,
/// Required dependencies the pack does not have.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub missing: Vec<String>,
pub layer: usize,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub jar: Option<PathBuf>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub info: Option<JarInfo>,
}
/// The jar of a pack file: the instance's own, or one the user put in
/// `<work>/jars/`.
fn jar_of(pack_jar: Option<&PathBuf>, file_name: &str, work: &Path) -> Option<PathBuf> {
pack_jar
.cloned()
.or_else(|| Some(work.join("jars").join(file_name)))
.filter(|p| p.is_file())
}
pub fn build(pack: &Pack, cache: &Cache, work: &Path) -> Vec<Record> {
let known = Known::atm10();
let mut records = Vec::new();
// Modrinth project id -> record key, for dependencies.
let mut by_project: HashMap<String, String> = HashMap::new();
let mut project_deps: Vec<Vec<String>> = Vec::new();
for f in &pack.files {
let jar = jar_of(f.jar.as_ref(), f.file_name(), work);
let info = jar.as_deref().and_then(|j| jar::read(j).ok());
let sha1 = f.sha1.as_deref().unwrap_or("");
let project = cache.project_of(sha1);
let p = |k: &str| project.and_then(|p| p[k].as_str()).unwrap_or("").to_owned();
let categories: Vec<String> = project
.into_iter()
.flat_map(|p| {
["categories", "additional_categories"]
.iter()
.flat_map(|k| p[*k].as_array().cloned().unwrap_or_default())
})
.filter_map(|c| c.as_str().map(str::to_owned))
.collect();
let stem = f.file_name().trim_end_matches(".jar").to_owned();
let key = info
.as_ref()
.map(|i| i.mod_id.clone())
.filter(|k| !k.is_empty())
.or_else(|| Some(p("slug")).filter(|s| !s.is_empty()))
.unwrap_or(stem);
let (client, server) = match project {
Some(_) if f.client.is_empty() => (p("client_side"), p("server_side")),
_ => (f.client.clone(), f.server.clone()),
};
let census = info.as_ref().map(|i| &i.census);
let facts = Facts {
kind: f.kind(),
client: &client,
server: &server,
categories: &categories,
classes: census.map(|c| c.classes),
has_content: census.is_some_and(|c| c.blockstates + c.item_models + c.recipes > 0),
};
let name = info
.as_ref()
.map(|i| i.name.clone())
.filter(|n| !n.is_empty())
.unwrap_or_else(|| p("title"));
let (class, tier, confidence) =
match known.get(&[key.as_str(), p("slug").as_str(), name.as_str()]) {
Some((c, t)) => (c.clone(), t.clone(), "known".to_owned()),
None => {
let (c, t, conf) = classify::rules(&facts);
(c.into(), t.into(), conf.into())
}
};
let license = info
.as_ref()
.map(|i| i.license.clone())
.filter(|l| !l.is_empty())
.or_else(|| project.and_then(|p| p["license"]["id"].as_str().map(str::to_owned)))
.unwrap_or_else(|| "unknown".into());
if let Some(id) = project.and_then(|p| p["id"].as_str()) {
by_project.insert(id.to_owned(), key.clone());
}
project_deps.push(cache.required(sha1));
records.push(Record {
name,
version: info.as_ref().map(|i| i.version.clone()).unwrap_or_else(|| {
cache
.versions
.get(sha1)
.and_then(|v| v["version_number"].as_str())
.unwrap_or("")
.to_owned()
}),
path: f.path.clone(),
class,
tier,
confidence,
source: if project.is_some() {
"modrinth".into()
} else if f.curseforge.is_some() {
"curseforge".into()
} else {
"file".into()
},
license,
client,
server,
categories,
depends: info.as_ref().map(|i| i.depends.clone()).unwrap_or_default(),
jar,
info,
key,
..Record::default()
});
}
// Dependencies: the jar's mod ids, else Modrinth's required projects.
let keys: std::collections::HashSet<String> = records.iter().map(|r| r.key.clone()).collect();
for (r, deps) in records.iter_mut().zip(project_deps) {
if r.depends.is_empty() {
r.depends = deps
.iter()
.map(|p| {
by_project
.get(p)
.cloned()
.unwrap_or_else(|| format!("modrinth:{p}"))
})
.collect();
}
let (inside, missing): (Vec<String>, Vec<String>) =
r.depends.drain(..).partition(|d| keys.contains(d));
r.depends = inside;
r.missing = missing;
}
let graph: BTreeMap<String, Vec<String>> = records
.iter()
.map(|r| (r.key.clone(), r.depends.clone()))
.collect();
let layers = classify::layers(&graph);
for r in &mut records {
r.layer = layers.get(&r.key).copied().unwrap_or(0);
}
records.sort_by(|a, b| (a.layer, &a.key).cmp(&(b.layer, &b.key)));
records
}
/// Counts by a field, largest first.
pub fn counts<'a>(
records: &'a [Record],
field: impl Fn(&'a Record) -> &'a str,
) -> Vec<(&'a str, usize)> {
let mut m: BTreeMap<&str, usize> = BTreeMap::new();
for r in records {
*m.entry(field(r)).or_default() += 1;
}
let mut v: Vec<_> = m.into_iter().collect();
v.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(b.0)));
v
}
/// `inventory.csv`: the columns of `docs/modpacks/atm10-mods.csv`, plus the layer.
pub fn csv(records: &[Record]) -> String {
let field = |s: &str| {
if s.contains([',', '"', '\n']) {
format!("\"{}\"", s.replace('"', "\"\""))
} else {
s.to_owned()
}
};
let mut out = String::from(
"mod_id,name,version,class,tier,confidence,source,license,client,server,layer\n",
);
for r in records {
let row = [
&r.key,
&r.name,
&r.version,
&r.class,
&r.tier,
&r.confidence,
&r.source,
&r.license,
&r.client,
&r.server,
];
let cells: Vec<String> = row.iter().map(|s| field(s)).collect();
out.push_str(&format!("{},{}\n", cells.join(","), r.layer));
}
out
}
+245
View File
@@ -0,0 +1,245 @@
//! 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()
};
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)
}
/// `[[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));
}
}
+152
View File
@@ -0,0 +1,152 @@
//! `horizon-port-pack`: port a Java modpack to Horizon (`docs/modpacks.md`).
//!
//! - `import <pack> <work>`: a `.mrpack`, CurseForge zip or instance folder
//! to `<work>/pack.json`.
//! - `inventory <work> [--offline]`: Modrinth metadata (cached in
//! `<work>/modrinth.json`), the jars in the instance or `<work>/jars/`,
//! classes, tiers and dependency layers in `<work>/inventory.json` and
//! `inventory.csv`.
//! - `carry <work> [<mod id>...]`: the content mods' data and assets into
//! `<work>/mods/<id>/carry/`, a folder mod for local use.
//!
//! The work folder belongs to the user: jars and carried-over files stay
//! there and are never uploaded or committed.
mod carry;
mod classify;
mod import;
mod inventory;
mod jar;
mod modrinth;
use std::{fs, path::Path, process::ExitCode};
const USAGE: &str = "usage: horizon-port-pack import <pack> <work> | inventory <work> [--offline] | carry <work> [<mod id>...]";
fn main() -> ExitCode {
let args: Vec<String> = std::env::args().skip(1).collect();
let path = |i: usize| args.get(i).map(Path::new).ok_or(USAGE.to_owned());
let result = match args.first().map(String::as_str) {
Some("import") => path(1)
.and_then(|p| Ok((p, path(2)?)))
.and_then(|(p, w)| import(p, w)),
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..])),
_ => Err(USAGE.to_owned()),
};
match result {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("error: {e}");
ExitCode::FAILURE
}
}
}
fn write_json(path: &Path, value: &impl serde::Serialize) -> Result<(), String> {
let text = serde_json::to_string_pretty(value).map_err(|e| e.to_string())?;
fs::write(path, text).map_err(|e| format!("{}: {e}", path.display()))
}
fn read_json<T: serde::de::DeserializeOwned>(path: &Path) -> Result<T, String> {
let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display()))
}
fn import(pack: &Path, work: &Path) -> Result<(), String> {
let pack = import::import(pack)?;
fs::create_dir_all(work).map_err(|e| format!("{}: {e}", work.display()))?;
write_json(&work.join("pack.json"), &pack)?;
let mut kinds = std::collections::BTreeMap::<&str, usize>::new();
for f in &pack.files {
*kinds.entry(f.kind()).or_default() += 1;
}
println!(
"{} ({}): Minecraft {}, {} {}; files {kinds:?}; overrides {:?}",
pack.name, pack.format, pack.minecraft, pack.loader, pack.loader_version, pack.overrides
);
Ok(())
}
fn inventory(work: &Path, offline: bool) -> Result<(), String> {
let pack: import::Pack = read_json(&work.join("pack.json"))?;
let cache_path = work.join("modrinth.json");
let mut cache = modrinth::Cache::load(&cache_path);
if !offline {
let hashes: Vec<String> = pack.files.iter().filter_map(|f| f.sha1.clone()).collect();
cache.fetch(&hashes)?;
write_json(&cache_path, &cache)?;
}
let records = inventory::build(&pack, &cache, work);
write_json(&work.join("inventory.json"), &records)?;
fs::write(work.join("inventory.csv"), inventory::csv(&records))
.map_err(|e| format!("inventory.csv: {e}"))?;
let jars = records.iter().filter(|r| r.jar.is_some()).count();
println!("{} files, {jars} jars read", records.len());
for (title, field) in [
(
"class",
(|r: &inventory::Record| r.class.as_str()) as fn(&inventory::Record) -> &str,
),
("tier", |r| r.tier.as_str()),
("confidence", |r| r.confidence.as_str()),
("source", |r| r.source.as_str()),
] {
println!("{title}: {:?}", inventory::counts(&records, field));
}
let layers = records.iter().map(|r| r.layer).max().map_or(0, |m| m + 1);
let per_layer: Vec<usize> = (0..layers)
.map(|l| records.iter().filter(|r| r.layer == l).count())
.collect();
println!("layers: {per_layer:?}");
let missing: usize = records.iter().map(|r| r.missing.len()).sum();
if missing > 0 {
println!("{missing} required dependencies are not in the pack (see inventory.json)");
}
Ok(())
}
fn carry(work: &Path, only: &[String]) -> Result<(), String> {
let records: Vec<inventory::Record> = read_json(&work.join("inventory.json"))?;
let mut done = 0;
for r in &records {
let chosen = if only.is_empty() {
r.class == "content"
} else {
only.contains(&r.key)
};
let (Some(jar), Some(info)) = (&r.jar, &r.info) else {
if chosen && !only.is_empty() {
eprintln!("{}: no jar (put it in {}/jars/)", r.key, work.display());
}
continue;
};
if !chosen {
continue;
}
match carry::carry(jar, info, &work.join("mods").join(&r.key)) {
Ok(report) => {
done += 1;
println!(
"{}: {} files, {} recipes upgraded ({} kept), {} item definitions added, {} blocks ({} without a template), {} items, {} outside",
r.key,
report.copied,
report.recipes_upgraded,
report.recipes_kept.len(),
report.item_definitions_added,
report.blocks.len(),
report.blocks_unknown.len(),
report.items,
report.outside.len()
);
}
Err(e) => eprintln!("{}: {e}", r.key),
}
}
println!(
"{done} mods carried over into {}",
work.join("mods").display()
);
Ok(())
}
+113
View File
@@ -0,0 +1,113 @@
//! Modrinth metadata for the pack's files: the version of each file by
//! SHA-1, and its project (categories, licence, sides). Only metadata: no
//! jar is downloaded. Answers are cached in `<work>/modrinth.json`, so a
//! rerun works offline.
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{collections::BTreeMap, path::Path};
const API: &str = "https://api.modrinth.com/v2";
const AGENT: &str = "ludovic111/horizon horizon-port-pack/0.1 (docs/modpacks.md)";
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Cache {
/// Version objects by file SHA-1.
pub versions: BTreeMap<String, Value>,
/// Project objects by project id.
pub projects: BTreeMap<String, Value>,
}
impl Cache {
pub fn load(path: &Path) -> Self {
std::fs::read_to_string(path)
.ok()
.and_then(|t| serde_json::from_str(&t).ok())
.unwrap_or_default()
}
/// Ask Modrinth about the hashes and projects not cached yet.
pub fn fetch(&mut self, hashes: &[String]) -> Result<(), String> {
let missing: Vec<&String> = hashes
.iter()
.filter(|h| !self.versions.contains_key(*h))
.collect();
for chunk in missing.chunks(500) {
let body = serde_json::json!({"hashes": chunk, "algorithm": "sha1"});
let found = post(&format!("{API}/version_files"), &body)?;
for (hash, version) in found.as_object().into_iter().flatten() {
self.versions.insert(hash.clone(), version.clone());
}
}
let mut ids: Vec<String> = self
.versions
.values()
.filter_map(|v| v["project_id"].as_str().map(str::to_owned))
.filter(|id| !self.projects.contains_key(id))
.collect();
ids.sort();
ids.dedup();
for chunk in ids.chunks(100) {
let list = serde_json::to_string(chunk).unwrap_or_default();
let found = get(&format!("{API}/projects?ids={}", encode(&list)))?;
for project in found.as_array().into_iter().flatten() {
if let Some(id) = project["id"].as_str() {
self.projects.insert(id.to_owned(), project.clone());
}
}
}
Ok(())
}
pub fn project_of(&self, sha1: &str) -> Option<&Value> {
let id = self.versions.get(sha1)?["project_id"].as_str()?;
self.projects.get(id)
}
/// Required dependencies of a file's version, as project ids.
pub fn required(&self, sha1: &str) -> Vec<String> {
let Some(v) = self.versions.get(sha1) else {
return Vec::new();
};
v["dependencies"]
.as_array()
.into_iter()
.flatten()
.filter(|d| d["dependency_type"] == "required")
.filter_map(|d| d["project_id"].as_str().map(str::to_owned))
.collect()
}
}
fn post(url: &str, body: &Value) -> Result<Value, String> {
let response = ureq::post(url)
.set("User-Agent", AGENT)
.set("Content-Type", "application/json")
.send_string(&body.to_string())
.map_err(|e| format!("{url}: {e}"))?;
read(url, response)
}
fn get(url: &str) -> Result<Value, String> {
let response = ureq::get(url)
.set("User-Agent", AGENT)
.call()
.map_err(|e| format!("{url}: {e}"))?;
read(url, response)
}
fn read(url: &str, response: ureq::Response) -> Result<Value, String> {
let text = response.into_string().map_err(|e| format!("{url}: {e}"))?;
serde_json::from_str(&text).map_err(|e| format!("{url}: {e}"))
}
/// Percent-encoding for a query value.
fn encode(s: &str) -> String {
s.bytes()
.map(|b| match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
(b as char).to_string()
}
_ => format!("%{b:02X}"),
})
.collect()
}
@@ -5,7 +5,10 @@ use super::{
generator::{GeneratorData, GeneratorError},
structure::{
StructureResources,
start::{Rings, StructureRegistry, StructureStart, beardifier, create_starts},
start::{
Rings, StartContext, StructureRegistry, StructureStart, beardifier, create_starts,
},
stronghold::RingSearch,
},
};
use horizon_core::{density::Beardifier, position::ChunkPos};
@@ -13,7 +16,10 @@ use serde_json::Value;
use std::collections::BTreeMap;
use std::{
collections::{BTreeSet, HashMap},
sync::{Arc, Mutex, OnceLock},
sync::{
Arc, Condvar, Mutex, OnceLock,
atomic::{AtomicUsize, Ordering},
},
};
pub(super) type Starts = Arc<Vec<Arc<StructureStart>>>;
@@ -27,6 +33,78 @@ pub(super) struct StructureState {
starts: Mutex<HashMap<(i32, i32), Starts>>,
/// Concentric ring positions, computed on first use.
rings: OnceLock<Rings>,
/// Their biome searches while they are computed.
ring_work: OnceLock<RingWork>,
}
/// generateRingPositions' biome searches, shared out. Vanilla runs each as
/// its own task on its worker pool; here every thread that needs the rings
/// before they exist takes searches until none are left, then waits for the
/// ones others took. Done by the first thread alone, the 128 stronghold
/// searches kept every other worker blocked for seconds after each world load.
struct RingWork {
/// The concentric-ring sets, some with no positions.
sets: Vec<usize>,
/// Set index and search, sets in `possible_sets` order.
searches: Vec<(usize, RingSearch)>,
next: AtomicUsize,
found: Vec<OnceLock<ChunkPos>>,
left: Mutex<usize>,
finished: Condvar,
}
impl RingWork {
fn new(sets: Vec<(usize, Vec<RingSearch>)>) -> Self {
let indices = sets.iter().map(|(set, _)| *set).collect();
let searches: Vec<(usize, RingSearch)> = sets
.into_iter()
.flat_map(|(set, searches)| searches.into_iter().map(move |s| (set, s)))
.collect();
Self {
sets: indices,
next: AtomicUsize::new(0),
found: (0..searches.len()).map(|_| OnceLock::new()).collect(),
left: Mutex::new(searches.len()),
finished: Condvar::new(),
searches,
}
}
/// Run searches until none are left to take, then wait for the rest.
fn help<'a>(
&self,
preferred: impl Fn(usize) -> &'a BTreeSet<BiomeId>,
biome: &mut dyn FnMut([i32; 3]) -> BiomeId,
) {
loop {
let i = self.next.fetch_add(1, Ordering::Relaxed);
let Some((set, search)) = self.searches.get(i) else {
break;
};
let _ = self.found[i].set(search.find(preferred(*set), biome));
let mut left = self.left.lock().unwrap();
*left -= 1;
if *left == 0 {
self.finished.notify_all();
}
}
let mut left = self.left.lock().unwrap();
while *left > 0 {
left = self.finished.wait(left).unwrap();
}
}
/// The positions by set name, once every search is done.
fn rings(&self, name: impl Fn(usize) -> String) -> Rings {
let mut rings: Rings = self
.sets
.iter()
.map(|&set| (name(set), Vec::new()))
.collect();
for ((set, _), found) in self.searches.iter().zip(&self.found) {
if let Some(positions) = rings.get_mut(&name(*set)) {
positions.push(*found.get().expect("every search finished"));
}
}
rings
}
}
const START_CACHE_LIMIT: usize = 65536;
@@ -89,6 +167,7 @@ impl StructureState {
by_step,
starts: Mutex::new(HashMap::new()),
rings: OnceLock::new(),
ring_work: OnceLock::new(),
}))
}
/// Concentric ring positions of a structure set, once the first start
@@ -96,6 +175,25 @@ impl StructureState {
pub(super) fn cached_rings(&self, set: &str) -> Option<Vec<ChunkPos>> {
self.rings.get().and_then(|rings| rings.get(set).cloned())
}
/// The concentric ring positions, computing them with the threads that
/// need them first (see `RingWork`).
fn rings(&self, context: &mut impl StartContext) -> &Rings {
if let Some(rings) = self.rings.get() {
return rings;
}
let work = self.ring_work.get_or_init(|| {
RingWork::new(
self.registry
.ring_searches(&self.possible_sets, context.seed()),
)
});
let registry = &self.registry;
work.help(|set| registry.ring_biomes(set), &mut |q| {
context.noise_biome(q)
});
self.rings
.get_or_init(|| work.rings(|set| registry.sets[set].name.clone()))
}
/// Drop the cached starts, so that later chunks build fresh pieces (the
/// pieces keep per-start placement state such as a shipwreck's height).
pub(super) fn forget_starts(&self) {
@@ -110,11 +208,7 @@ impl StructureState {
if let Some(found) = self.starts.lock().unwrap().get(&(chunk.x, chunk.z)) {
return found.clone();
}
let rings = self.rings.get_or_init(|| {
let seed = context.seed();
self.registry
.ring_positions(&self.possible_sets, seed, &mut |q| context.noise_biome(q))
});
let rings = self.rings(context);
let starts: Starts = Arc::new(
create_starts(
&self.registry,
@@ -148,13 +242,7 @@ impl StructureState {
radius: i32,
) -> Option<(String, [i32; 3])> {
let seed = context.seed();
let rings = self
.rings
.get_or_init(|| {
self.registry
.ring_positions(&self.possible_sets, seed, &mut |q| context.noise_biome(q))
})
.clone();
let rings = self.rings(context).clone();
let wanted = |s: usize| names.iter().any(|n| *n == self.registry.structures[s].name);
let dist = |p: [i32; 3]| {
let (dx, dz) = (f64::from(p[0] - from[0]), f64::from(p[2] - from[2]));
@@ -288,3 +376,43 @@ impl StructureState {
beardifier(&self.registry, references, chunk)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::worldgen::structure::stronghold::{ring_positions, ring_searches};
/// Searches shared between threads give the positions of one thread
/// running them in order, set by set.
#[test]
fn ring_work_shared_between_threads_matches_serial_search() {
let preferred: BTreeSet<BiomeId> = (0..20).map(|b| BiomeId(b * 3)).collect();
let biome = |q: [i32; 3]| {
BiomeId((q[0].wrapping_mul(31) ^ q[2].wrapping_mul(17)).rem_euclid(64) as u32)
};
let none = BTreeSet::new();
for seed in [0, 1234, -77, i64::MAX] {
let work = RingWork::new(vec![
(0, ring_searches(seed, 32, 3, 128)),
(1, ring_searches(seed ^ 5, 10, 4, 0)),
(2, ring_searches(seed.wrapping_add(1), 20, 5, 40)),
]);
let sets = [&preferred, &none, &preferred];
std::thread::scope(|scope| {
for _ in 0..4 {
scope.spawn(|| work.help(|set| sets[set], &mut { biome }));
}
});
let rings = work.rings(|set| format!("set{set}"));
let serial = |s: i64, distance, spread, count, preferred| {
ring_positions(s, distance, spread, count, preferred, &mut { biome })
};
assert_eq!(rings["set0"], serial(seed, 32, 3, 128, &preferred));
assert_eq!(rings["set1"], Vec::new());
assert_eq!(
rings["set2"],
serial(seed.wrapping_add(1), 20, 5, 40, &preferred)
);
}
}
}
@@ -299,31 +299,37 @@ impl StructureRegistry {
pools: Pools::new(pools),
})
}
/// ChunkGeneratorStructureState ring positions of the concentric-ring
/// sets among `possible_sets`.
pub fn ring_positions(
/// ChunkGeneratorStructureState.generatePositions: the ring positions of
/// each concentric-ring set among `possible_sets` before their biome
/// searches (set index, searches in order).
pub fn ring_searches(
&self,
possible_sets: &[usize],
seed: i64,
biome: &mut dyn FnMut([i32; 3]) -> BiomeId,
) -> Rings {
let mut rings = Rings::new();
for &i in possible_sets {
let set = &self.sets[i];
if let Placement::ConcentricRings {
distance,
spread,
count,
preferred,
} = &set.placement
{
let positions = super::stronghold::ring_positions(
seed, *distance, *spread, *count, preferred, biome,
);
rings.insert(set.name.clone(), positions);
}
) -> Vec<(usize, Vec<super::stronghold::RingSearch>)> {
possible_sets
.iter()
.filter_map(|&i| match &self.sets[i].placement {
Placement::ConcentricRings {
distance,
spread,
count,
..
} => Some((
i,
super::stronghold::ring_searches(seed, *distance, *spread, *count),
)),
_ => None,
})
.collect()
}
/// The preferred biomes of a concentric-ring set (empty for others).
pub fn ring_biomes(&self, set: usize) -> &BTreeSet<BiomeId> {
static NONE: BTreeSet<BiomeId> = BTreeSet::new();
match &self.sets[set].placement {
Placement::ConcentricRings { preferred, .. } => preferred,
_ => &NONE,
}
rings
}
/// RandomSpreadStructurePlacement.getPotentialStructureChunk et al.
/// RandomSpreadStructurePlacement.getPotentialStructureChunk for the
@@ -22,20 +22,30 @@ pub fn ring_positions(
preferred: &BTreeSet<BiomeId>,
biome: &mut dyn FnMut([i32; 3]) -> BiomeId,
) -> Vec<ChunkPos> {
let mut random = LegacyRandom::new(seed);
let mut angle = random.next_double() * PI * 2.0;
let (mut in_circle, mut circle, mut spread) = (0, 0, spread);
let mut out = Vec::with_capacity(count.max(0) as usize);
for i in 0..count {
let dist = f64::from(4 * distance + distance * circle * 6)
+ (random.next_double() - 0.5) * (f64::from(distance) * 2.5);
let round = |v: f64| (v + 0.5).floor() as i64 as i32;
let (x, z) = (
round(horizon_core::jmath::cos(angle) * dist),
round(horizon_core::jmath::sin(angle) * dist),
);
let mut search = random.fork();
let (cx, cz) = ((x * 16 + 8) >> 2, (z * 16 + 8) >> 2);
ring_searches(seed, distance, spread, count)
.iter()
.map(|search| search.find(preferred, biome))
.collect()
}
/// One ring position before its biome search: the chunk on the ring and the
/// random its search uses. Vanilla runs each search as its own task.
#[derive(Clone, Debug)]
pub struct RingSearch {
x: i32,
z: i32,
random: LegacyRandom,
}
impl RingSearch {
/// BiomeSource.findBiomeHorizontal around the ring chunk's centre: a
/// random preferred biome within 112 blocks, else the ring chunk itself.
pub fn find(
&self,
preferred: &BTreeSet<BiomeId>,
biome: &mut dyn FnMut([i32; 3]) -> BiomeId,
) -> ChunkPos {
let mut search = self.random.clone();
let (cx, cz) = ((self.x * 16 + 8) >> 2, (self.z * 16 + 8) >> 2);
let mut found = 0;
let mut result = None;
for dz in -28..=28 {
@@ -48,9 +58,28 @@ pub fn ring_positions(
}
}
}
out.push(match result {
match result {
Some((bx, bz)) => ChunkPos::new(bx >> 4, bz >> 4),
None => ChunkPos::new(x, z),
None => ChunkPos::new(self.x, self.z),
}
}
}
/// The ring chunks and search randoms of generateRingPositions, in order:
/// the serial part, cheap; the searches can then run anywhere.
pub fn ring_searches(seed: i64, distance: i32, spread: i32, count: i32) -> Vec<RingSearch> {
let mut random = LegacyRandom::new(seed);
let mut angle = random.next_double() * PI * 2.0;
let (mut in_circle, mut circle, mut spread) = (0, 0, spread);
let mut out = Vec::with_capacity(count.max(0) as usize);
for i in 0..count {
let dist = f64::from(4 * distance + distance * circle * 6)
+ (random.next_double() - 0.5) * (f64::from(distance) * 2.5);
let round = |v: f64| (v + 0.5).floor() as i64 as i32;
out.push(RingSearch {
x: round(horizon_core::jmath::cos(angle) * dist),
z: round(horizon_core::jmath::sin(angle) * dist),
random: random.fork(),
});
angle += PI * 2.0 / f64::from(spread);
in_circle += 1;