Merge pull request #969 from ludovic111/feat/mod-extra-namespaces
🤖 Agent: merged after verify and CI parity passed, plus modpacks-10's local integration (all nine branches on main: parity 301/301, structure-start oracle 150/150 and 134/134). Content mods may own namespaces besides their id; two mods may not share one.
This commit is contained in:
@@ -84,8 +84,10 @@ fn main() -> Result<()> {
|
||||
horizon_client::collisions::load(&root, registry).map_err(step("collisions"))?;
|
||||
let loaded = began.elapsed().as_secs_f64() - built;
|
||||
let ours = |id: &str| {
|
||||
id.split_once(':')
|
||||
.is_some_and(|(ns, _)| mods.iter().any(|m| m.id == ns))
|
||||
id.split_once(':').is_some_and(|(ns, _)| {
|
||||
mods.iter()
|
||||
.any(|m| m.content.namespaces.iter().any(|n| n == ns))
|
||||
})
|
||||
};
|
||||
let tools: Vec<String> = serde_json::from_value(oracle["tools"].clone())?;
|
||||
|
||||
|
||||
@@ -75,7 +75,18 @@ pub(super) fn content_root(
|
||||
{
|
||||
continue;
|
||||
}
|
||||
match Content::read(path, m) {
|
||||
let content = Content::read(path, m).and_then(|content| {
|
||||
// The first of two mods owning one namespace stays on.
|
||||
let own = (m.id.as_str(), content.namespaces.as_slice());
|
||||
match mods
|
||||
.iter()
|
||||
.find_map(|c| horizon_mods::content::clash((&c.id, &c.content.namespaces), own))
|
||||
{
|
||||
Some(e) => Err(e),
|
||||
None => Ok(content),
|
||||
}
|
||||
});
|
||||
match content {
|
||||
Ok(content) => mods.push(ContentMod {
|
||||
id: m.id.clone(),
|
||||
content,
|
||||
|
||||
@@ -641,8 +641,8 @@ const NBT_TABLES: [&str; 6] = [
|
||||
"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`.
|
||||
/// In a root built here: the namespaces the content mods own (their ids and
|
||||
/// listed namespaces), which loaders read besides `minecraft`.
|
||||
const MARKER: &str = "horizon-mod-content.json";
|
||||
/// Block states a world can hold. Chunk sections widen their global palette
|
||||
/// past vanilla's 16 bits to fit the registry (crate::chunk::set_block_states);
|
||||
@@ -947,6 +947,18 @@ impl Known {
|
||||
|
||||
/// Builds the content root for `mods` over `base` in `out`.
|
||||
pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
|
||||
// Two mods owning one namespace would mix their files.
|
||||
for (i, a) in mods.iter().enumerate() {
|
||||
for b in &mods[..i] {
|
||||
let (b, a) = (
|
||||
(b.id.as_str(), b.content.namespaces.as_slice()),
|
||||
(a.id.as_str(), a.content.namespaces.as_slice()),
|
||||
);
|
||||
if let Some(e) = horizon_mods::content::clash(b, a) {
|
||||
return Err(e.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
// The overlay links to the base: a relative base would leave them dangling.
|
||||
let base = &std::path::absolute(base)?;
|
||||
let plan = Plan::new(base, mods)?;
|
||||
@@ -1110,9 +1122,12 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
|
||||
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)?)?;
|
||||
let mut namespaces: Vec<&str> = mods
|
||||
.iter()
|
||||
.flat_map(|m| m.content.namespaces.iter().map(String::as_str))
|
||||
.collect();
|
||||
namespaces.sort();
|
||||
overlay.write(MARKER, &serde_json::to_vec(&namespaces)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -661,3 +661,100 @@ fn content_root_carries_a_structure_mod() {
|
||||
assert!(at[0].abs() < 1024 && at[2].abs() < 1024, "{at:?}");
|
||||
let _ = fs::remove_dir_all(&scratch);
|
||||
}
|
||||
|
||||
/// A content mod folder from `(path, text)` pairs, read as the game reads it.
|
||||
fn folder_mod(dir: &Path, files: &[(&str, &str)]) -> ContentMod {
|
||||
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: package.manifest.id.clone(),
|
||||
content: Content::read(dir, &package.manifest).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
fn manifest(id: &str, namespaces: &str) -> String {
|
||||
format!(
|
||||
"id = \"{id}\"\nname = \"{id}\"\nversion = \"1.0\"\napi = \"0.2\"\ncapabilities = [\"registries\"]\nnamespaces = [{namespaces}]\n"
|
||||
)
|
||||
}
|
||||
|
||||
/// A mod whose files sit under namespaces besides its id (Towns and Towers'
|
||||
/// `kaisyn`): they join the root, and loaders read those namespaces.
|
||||
#[test]
|
||||
fn listed_namespaces_join_the_root() {
|
||||
let Some(base) = prepared() else {
|
||||
return;
|
||||
};
|
||||
let scratch = std::env::temp_dir().join(format!("horizon-mod-ns-{}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&scratch);
|
||||
let toml = manifest("towers", "\"kaisyn\"");
|
||||
let m = folder_mod(
|
||||
&scratch.join("towers"),
|
||||
&[
|
||||
("horizon-mod.toml", &toml),
|
||||
(
|
||||
"data/kaisyn/worldgen/processor_list/mossy.json",
|
||||
r#"{"processors": []}"#,
|
||||
),
|
||||
(
|
||||
"data/kaisyn/tags/worldgen/biome/has_tower.json",
|
||||
r#"{"values": ["minecraft:plains"]}"#,
|
||||
),
|
||||
(
|
||||
"assets/kaisyn/lang/en_us.json",
|
||||
r#"{"block.kaisyn.x": "X"}"#,
|
||||
),
|
||||
],
|
||||
);
|
||||
let root = scratch.join("root");
|
||||
build(&base, &[m], &root).unwrap();
|
||||
assert_eq!(namespaces(&root), ["kaisyn", "towers"]);
|
||||
assert!(tag_namespaces(&root).contains(&"kaisyn".to_owned()));
|
||||
let lists: BTreeMap<String, String> =
|
||||
serde_json::from_slice(&fs::read(root.join("worldgen/processor_lists.json")).unwrap())
|
||||
.unwrap();
|
||||
assert!(lists.contains_key("kaisyn:mossy"));
|
||||
let tags: BTreeMap<String, String> =
|
||||
serde_json::from_slice(&fs::read(root.join("worldgen/biome_tags.json")).unwrap()).unwrap();
|
||||
assert!(tags.contains_key("kaisyn:has_tower"));
|
||||
let _ = fs::remove_dir_all(&scratch);
|
||||
}
|
||||
|
||||
/// Two mods owning one namespace (one's listed namespace is the other's id,
|
||||
/// or both list it) are refused together, naming both.
|
||||
#[test]
|
||||
fn mods_sharing_a_namespace_are_refused() {
|
||||
let scratch = std::env::temp_dir().join(format!("horizon-mod-clash-{}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&scratch);
|
||||
let towers = folder_mod(
|
||||
&scratch.join("towers"),
|
||||
&[("horizon-mod.toml", &manifest("towers", "\"kaisyn\""))],
|
||||
);
|
||||
let kaisyn = folder_mod(
|
||||
&scratch.join("kaisyn"),
|
||||
&[("horizon-mod.toml", &manifest("kaisyn", ""))],
|
||||
);
|
||||
let other = folder_mod(
|
||||
&scratch.join("other"),
|
||||
&[("horizon-mod.toml", &manifest("other", "\"kaisyn\""))],
|
||||
);
|
||||
// The check runs before the base is read.
|
||||
let base = scratch.join("no-base");
|
||||
let out = scratch.join("root");
|
||||
let e = build(&base, &[towers, kaisyn], &out).unwrap_err();
|
||||
assert_eq!(
|
||||
e.to_string(),
|
||||
"mods towers and kaisyn both use the namespace kaisyn; turn one of them off"
|
||||
);
|
||||
let towers = folder_mod(
|
||||
&scratch.join("towers"),
|
||||
&[("horizon-mod.toml", &manifest("towers", "\"kaisyn\""))],
|
||||
);
|
||||
let e = build(&base, &[towers, other], &out).unwrap_err();
|
||||
assert!(e.to_string().starts_with("mods towers and other"), "{e}");
|
||||
let _ = fs::remove_dir_all(&scratch);
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
//! 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 and to the convention tags (`c`, which NeoForge and Fabric mods share). Paths are plain, files and totals are capped, and vanilla ids
|
||||
//! cannot be redefined.
|
||||
//! may add is fenced instead. Its own namespaces (its id and the ones its
|
||||
//! manifest lists), plus additions to vanilla tags and to the convention
|
||||
//! tags (`c`, which NeoForge and Fabric mods share). Paths are plain, files
|
||||
//! and totals are capped, vanilla ids cannot be redefined, and no two mods
|
||||
//! share a namespace ([`clash`]).
|
||||
use crate::manifest::{Capability, MANIFEST, Manifest};
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
@@ -141,6 +143,19 @@ pub struct Content {
|
||||
pub files: Vec<String>,
|
||||
pub blocks: Vec<Block>,
|
||||
pub items: Vec<Item>,
|
||||
/// The namespaces it owns: the mod id, then the manifest's `namespaces`.
|
||||
pub namespaces: Vec<String>,
|
||||
}
|
||||
|
||||
/// Why two content mods cannot be on together: a namespace both own (one's
|
||||
/// id or listed namespace is the other's), which would mix their files.
|
||||
/// `a` and `b` are (mod id, owned namespaces).
|
||||
pub fn clash(a: (&str, &[String]), b: (&str, &[String])) -> Option<String> {
|
||||
let shared = a.1.iter().find(|ns| b.1.contains(ns))?;
|
||||
Some(format!(
|
||||
"mods {} and {} both use the namespace {shared}; turn one of them off",
|
||||
a.0, b.0
|
||||
))
|
||||
}
|
||||
|
||||
impl Content {
|
||||
@@ -187,6 +202,7 @@ impl Content {
|
||||
files,
|
||||
blocks: Vec::new(),
|
||||
items: Vec::new(),
|
||||
namespaces: manifest.owned_namespaces().map(str::to_owned).collect(),
|
||||
};
|
||||
content.check(manifest)?;
|
||||
Ok(content)
|
||||
@@ -249,13 +265,19 @@ impl Content {
|
||||
parts.next().unwrap_or(""),
|
||||
);
|
||||
let own = namespace == id;
|
||||
let owned = own || manifest.namespaces.iter().any(|n| n == namespace);
|
||||
// Vanilla's own files stay vanilla's; a mod may only add to its
|
||||
// tags, and to the convention tags mods share.
|
||||
let shared = matches!(namespace, "minecraft" | "c");
|
||||
let allowed = own || (top == "data" && shared && rest.starts_with("tags/"));
|
||||
let allowed = owned || (top == "data" && shared && rest.starts_with("tags/"));
|
||||
if !allowed {
|
||||
return Err(format!(
|
||||
"{name}: a mod adds files in its own namespace ({top}/{id}/) or to vanilla and convention (c) tags"
|
||||
"{name}: a mod adds files in its own namespaces ({top}/{id}/ and those listed in namespaces) or to vanilla and convention (c) tags"
|
||||
));
|
||||
}
|
||||
if !own && top == "data" && rest.starts_with("horizon/") {
|
||||
return Err(format!(
|
||||
"{name}: blocks and items are declared in data/{id}/horizon/"
|
||||
));
|
||||
}
|
||||
let Some(decl) = (own && top == "data")
|
||||
@@ -471,7 +493,7 @@ capabilities = ["registries"]
|
||||
e
|
||||
};
|
||||
let e = refused("other-ns", &[("data/other/horizon/block/a.json", BLOCK)]);
|
||||
assert!(e.contains("own namespace"), "{e}");
|
||||
assert!(e.contains("own namespaces"), "{e}");
|
||||
refused(
|
||||
"vanilla-asset",
|
||||
&[("assets/minecraft/textures/block/stone.png", b"x")],
|
||||
@@ -522,6 +544,38 @@ capabilities = ["registries"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn listed_namespaces_hold_content() {
|
||||
let manifest = format!("{MANIFEST_TEXT}namespaces = [\"kaisyn\"]\n");
|
||||
let files: &[(&str, &[u8])] = &[
|
||||
("data/kaisyn/worldgen/template_pool/a.json", b"{}"),
|
||||
("data/kaisyn/structure/a.nbt", b"nbt"),
|
||||
("assets/kaisyn/lang/en_us.json", b"{}"),
|
||||
];
|
||||
let content = hmod("listed", &manifest, files).unwrap();
|
||||
assert_eq!(content.files.len(), 3);
|
||||
assert_eq!(content.namespaces, ["ruby", "kaisyn"]);
|
||||
// Declarations stay in the id's namespace.
|
||||
let decl: &[(&str, &[u8])] = &[("data/kaisyn/horizon/block/a.json", BLOCK)];
|
||||
let e = hmod("listed-decl", &manifest, decl).unwrap_err();
|
||||
assert!(e.contains("declared in data/ruby/horizon/"), "{e}");
|
||||
// A namespace it does not list is still outside.
|
||||
let other: &[(&str, &[u8])] = &[("data/other/structure/a.nbt", b"nbt")];
|
||||
assert!(hmod("listed-other", &manifest, other).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mods_sharing_a_namespace_clash() {
|
||||
let t_and_t = ["t_and_t".to_owned(), "kaisyn".to_owned()];
|
||||
let kaisyn = ["kaisyn".to_owned()];
|
||||
let ruby = ["ruby".to_owned(), "gems".to_owned()];
|
||||
assert_eq!(
|
||||
clash(("t_and_t", &t_and_t), ("kaisyn", &kaisyn)).as_deref(),
|
||||
Some("mods t_and_t and kaisyn both use the namespace kaisyn; turn one of them off")
|
||||
);
|
||||
assert_eq!(clash(("t_and_t", &t_and_t), ("ruby", &ruby)), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn content_needs_the_registries_capability() {
|
||||
let code_only = MANIFEST_TEXT
|
||||
|
||||
@@ -13,6 +13,9 @@ pub const API: &str = "0.1";
|
||||
/// `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"];
|
||||
/// Namespaces no mod may claim: vanilla's, the convention tags' (mods add
|
||||
/// to those, docs/mods.md) and the game's own.
|
||||
const RESERVED_NAMESPACES: [&str; 3] = ["minecraft", "c", "horizon"];
|
||||
/// The biggest `mod.wasm` a mod may ship.
|
||||
const MAX_WASM: u64 = 32 << 20;
|
||||
|
||||
@@ -33,6 +36,12 @@ pub struct Manifest {
|
||||
pub side: Side,
|
||||
#[serde(default)]
|
||||
pub capabilities: Vec<Capability>,
|
||||
/// Namespaces besides `id` that the mod's `assets/` and `data/` use, for
|
||||
/// a content mod whose files sit under other names than its id (Towns
|
||||
/// and Towers ships `kaisyn` and `towns_and_towers`). No two content mods
|
||||
/// that are on may share one (`content::clash`).
|
||||
#[serde(default)]
|
||||
pub namespaces: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq)]
|
||||
@@ -141,8 +150,37 @@ impl Manifest {
|
||||
"{MANIFEST}: capability registries needs api = \"0.2\""
|
||||
));
|
||||
}
|
||||
if !m.namespaces.is_empty() && !m.capabilities.contains(&Capability::Registries) {
|
||||
return Err(format!(
|
||||
"{MANIFEST}: namespaces are for content, which needs capability registries"
|
||||
));
|
||||
}
|
||||
for (i, ns) in m.namespaces.iter().enumerate() {
|
||||
// Vanilla's namespace characters, at most as long as an id.
|
||||
let chars_ok = (1..=64).contains(&ns.len())
|
||||
&& ns
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"_.-".contains(&b));
|
||||
if !chars_ok {
|
||||
return Err(format!(
|
||||
"{MANIFEST}: namespace {ns:?} is not [a-z0-9_.-]{{1,64}}"
|
||||
));
|
||||
}
|
||||
if RESERVED_NAMESPACES.contains(&ns.as_str()) {
|
||||
return Err(format!(
|
||||
"{MANIFEST}: namespace {ns} is not a mod's to claim (only to add to its tags)"
|
||||
));
|
||||
}
|
||||
if *ns == m.id || m.namespaces[..i].contains(ns) {
|
||||
return Err(format!("{MANIFEST}: namespace {ns} is listed twice"));
|
||||
}
|
||||
}
|
||||
Ok(m)
|
||||
}
|
||||
/// The namespaces the mod's content owns: its id, then `namespaces`.
|
||||
pub fn owned_namespaces(&self) -> impl Iterator<Item = &str> {
|
||||
std::iter::once(self.id.as_str()).chain(self.namespaces.iter().map(String::as_str))
|
||||
}
|
||||
/// Checks that the component's imports need only declared capabilities.
|
||||
pub fn check_imports<'a>(
|
||||
&self,
|
||||
@@ -312,6 +350,25 @@ capabilities = ["game-read", "hud"]
|
||||
assert!(Manifest::parse(&unknown).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checks_extra_namespaces() {
|
||||
let content = "id = \"t_and_t\"\nname = \"T\"\nversion = \"1\"\napi = \"0.2\"\ncapabilities = [\"registries\"]\n";
|
||||
let with = |list: &str| Manifest::parse(&format!("{content}namespaces = [{list}]\n"));
|
||||
let m = with("\"kaisyn\", \"towns_and_towers\"").unwrap();
|
||||
let owned: Vec<&str> = m.owned_namespaces().collect();
|
||||
assert_eq!(owned, ["t_and_t", "kaisyn", "towns_and_towers"]);
|
||||
for reserved in ["minecraft", "c", "horizon"] {
|
||||
let e = with(&format!("\"{reserved}\"")).unwrap_err();
|
||||
assert!(e.contains("not a mod's to claim"), "{e}");
|
||||
}
|
||||
assert!(with("\"t_and_t\"").unwrap_err().contains("twice"));
|
||||
assert!(with("\"a\", \"a\"").unwrap_err().contains("twice"));
|
||||
assert!(with("\"Kaisyn\"").unwrap_err().contains("is not"));
|
||||
assert!(with("\"\"").unwrap_err().contains("is not"));
|
||||
let code = format!("{EXAMPLE}namespaces = [\"kaisyn\"]\n");
|
||||
assert!(Manifest::parse(&code).unwrap_err().contains("registries"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hmod_round_trips() {
|
||||
let dir = std::env::temp_dir().join(format!("horizon-mods-{}", std::process::id()));
|
||||
|
||||
@@ -236,6 +236,10 @@ How the game loads them, without touching vanilla's path:
|
||||
so the same mod set gives the same ids. Saves store block names, so ids never reach the disk.
|
||||
- Loaders that read only `data/minecraft/` (recipes, loot, tags) read every namespace, as
|
||||
vanilla does. With no mods there is only `minecraft`, so nothing changes.
|
||||
- **Namespaces.** A mod's files go under its id, and under the namespaces its manifest lists
|
||||
when its Java original ships them so (`namespaces = ["kaisyn", "towns_and_towers"]` for Towns
|
||||
and Towers, mod id `t_and_t`; docs/mods.md). No namespace has two owners: a mod whose id or
|
||||
listed namespace another mod that is on already owns is left out, with both named in the log.
|
||||
- The overlay is built on the first start with a set of mods and kept for later starts. A stamp
|
||||
(`horizon-mod-content.stamp`) hashes the game executable, the base tables and every mod file's
|
||||
size and mtime. Adding, removing or editing a mod, or a new build of Horizon, rebuilds it. For
|
||||
|
||||
+24
-4
@@ -276,10 +276,30 @@ blocks and items by declaring them in `data/<id>/horizon/block/*.json` and
|
||||
"item": true }
|
||||
```
|
||||
|
||||
The fence: files go in the mod's own namespace (`assets/<id>/`,
|
||||
`data/<id>/`), plus additions to vanilla tags (`data/minecraft/tags/`) and
|
||||
to the convention tags NeoForge and Fabric mods share (`data/c/tags/`).
|
||||
Templates and `sounds` name vanilla blocks; unknown keys are refused. Paths
|
||||
The fence: files go in the mod's own namespaces (`assets/<id>/`,
|
||||
`data/<id>/`, and those its manifest lists), plus additions to vanilla tags
|
||||
(`data/minecraft/tags/`) and to the convention tags NeoForge and Fabric mods
|
||||
share (`data/c/tags/`).
|
||||
Templates and `sounds` name vanilla blocks; unknown keys are refused.
|
||||
|
||||
Some Java mods ship their files under other names than their mod id (Towns
|
||||
and Towers, mod id `t_and_t`, under `kaisyn` and `towns_and_towers`). A
|
||||
content mod lists those in its manifest:
|
||||
|
||||
```toml
|
||||
id = "t_and_t"
|
||||
api = "0.2"
|
||||
capabilities = ["registries"]
|
||||
namespaces = ["kaisyn", "towns_and_towers"]
|
||||
```
|
||||
|
||||
A listed namespace is the mod's as its id is: its `assets/` and `data/`
|
||||
files load, and the game reads its tags, recipes, names and loot. Blocks and
|
||||
items are still declared under `data/<id>/horizon/` only. `minecraft`, `c`
|
||||
and `horizon` cannot be listed. Two mods that are on may not own the same
|
||||
namespace (one's id or listed namespace being the other's): the game keeps
|
||||
the first in load order and leaves the other's content out, with
|
||||
"mods A and B both use the namespace N; turn one of them off" in the log. Paths
|
||||
use vanilla's resource characters, at most 65,536 files, 16 MiB each and
|
||||
512 MiB in all. A mod whose content is refused stays off, with the reason on
|
||||
its row. The game reads this content into its block and item tables
|
||||
|
||||
Reference in New Issue
Block a user