Skip to content

Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.

Fusion packs: read the fusion section of texture .mcmeta files

some-du6e67788b979f09
diff --git a/crates/horizon-optifine/src/fusion.rs b/crates/horizon-optifine/src/fusion.rs
index db00d1ab4..91cb0b933 100644
--- a/crates/horizon-optifine/src/fusion.rs
+++ b/crates/horizon-optifine/src/fusion.rs
@@ -1,5 +1,5 @@
//! Fusion's resource pack format (the Fusion Connected Textures mod): a `fusion` section in a
-//! texture's `.mcmeta` (`texture`: connecting, random, continuous and scrolling textures, and
+//! texture's `.mcmeta` ([`texture`]: connecting, random, continuous and scrolling textures, and
//! the emissive, render type and tinting options every type takes), the connection predicates
//! that say when a connecting texture joins a neighbour ([`predicate`]), the `connections` of a
//! `fusion:connecting` block model (`model`), and which tiles of a connecting texture's image a
@@ -14,9 +14,11 @@
pub mod layout;
pub mod predicate;
+pub mod texture;
pub use layout::{Layout, Tiles};
pub use predicate::Connection;
+pub use texture::Meta;
use crate::cem::json::Json;
@@ -25,6 +27,14 @@ fn kind(name: &str) -> &str {
name.strip_prefix("fusion:").unwrap_or(name)
}
+fn bool_field(json: &Json, key: &str) -> Result<Option<bool>, String> {
+ match json.get(key) {
+ None => Ok(None),
+ Some(Json::Bool(b)) => Ok(Some(*b)),
+ Some(_) => Err(format!("`{key}` is not true or false")),
+ }
+}
+
fn str_field<'a>(json: &'a Json, key: &str) -> Result<Option<&'a str>, String> {
match json.get(key) {
None => Ok(None),
@@ -32,3 +42,21 @@ fn str_field<'a>(json: &'a Json, key: &str) -> Result<Option<&'a str>, String> {
Some(_) => Err(format!("`{key}` is not a string")),
}
}
+
+/// An integer field in `range`, else `default`.
+fn int_field(
+ json: &Json,
+ key: &str,
+ range: std::ops::RangeInclusive<i64>,
+ default: i64,
+) -> Result<i64, String> {
+ match json.get(key) {
+ None => Ok(default),
+ Some(Json::Num(n)) if n.fract() == 0.0 && range.contains(&(*n as i64)) => Ok(*n as i64),
+ Some(_) => Err(format!(
+ "`{key}` is not an integer from {} to {}",
+ range.start(),
+ range.end()
+ )),
+ }
+}
diff --git a/crates/horizon-optifine/src/fusion/texture.rs b/crates/horizon-optifine/src/fusion/texture.rs
new file mode 100644
index 000000000..439ad2f55
--- /dev/null
+++ b/crates/horizon-optifine/src/fusion/texture.rs
@@ -0,0 +1,365 @@
+//! The `fusion` section of a texture's `.mcmeta` (beside vanilla's `animation`): the texture's
+//! type and its options.
+
+use super::{Connection, Layout, bool_field, int_field, kind, str_field};
+use crate::cem::json::Json;
+
+/// A texture's Fusion metadata.
+#[derive(Clone, Debug, PartialEq)]
+pub struct Meta {
+ pub base: Base,
+ pub kind: Kind,
+}
+
+/// The options every type takes (`base` on its own).
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+pub struct Base {
+ /// Drawn fully bright, as at light level 15.
+ pub emissive: bool,
+ pub render_type: Option<RenderType>,
+ /// Tinted as this, whatever the block tints.
+ pub tinting: Option<Tinting>,
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum RenderType {
+ Opaque,
+ Cutout,
+ Translucent,
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum Tinting {
+ Grass,
+ Foliage,
+ Water,
+}
+
+#[derive(Clone, Debug, PartialEq)]
+pub enum Kind {
+ Base,
+ Connecting {
+ layout: Layout,
+ /// The texture's own; a `fusion:connecting` model's replace them.
+ connections: Connection,
+ /// Each tile's own metadata, as if it were a texture.
+ sub_texture: Option<Box<Meta>>,
+ /// Each tile holds its animation frames, instead of each frame holding every tile.
+ per_tile_animation: bool,
+ },
+ Random {
+ columns: u32,
+ rows: u32,
+ source: RandomSource,
+ seed: Option<i64>,
+ sub_texture: Option<Box<Meta>>,
+ per_tile_animation: bool,
+ },
+ /// One image stretched over `columns` x `rows` blocks.
+ Continuous {
+ columns: u32,
+ rows: u32,
+ },
+ Scrolling(Scrolling),
+}
+
+/// What picks a random texture's tile.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum RandomSource {
+ Position,
+ PositionFacing,
+ PositionAxis,
+}
+
+/// An animation whose frame, `frame_width` x `frame_height` pixels, moves across the image from
+/// one corner to another, each step shown for `frame_time` ticks.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct Scrolling {
+ pub from: Corner,
+ pub to: Corner,
+ pub frame_width: u32,
+ pub frame_height: u32,
+ pub frame_time: u32,
+ pub loop_type: LoopType,
+ /// Ticks between loops.
+ pub loop_pause: u32,
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum Corner {
+ TopLeft,
+ TopRight,
+ BottomLeft,
+ BottomRight,
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum LoopType {
+ /// Around the image to the start.
+ Wrap,
+ /// Back to the start at once.
+ Reset,
+ /// Back the way it came.
+ Reverse,
+}
+
+/// `key`'s value among `options`, else `default`.
+fn choice<T: Copy>(
+ json: &Json,
+ key: &str,
+ options: &[(&str, T)],
+ default: Option<T>,
+) -> Result<Option<T>, String> {
+ match str_field(json, key)? {
+ None => Ok(default),
+ Some(value) => options
+ .iter()
+ .find(|(name, _)| *name == value)
+ .map(|&(_, v)| Some(v))
+ .ok_or_else(|| format!("`{key}` can't be `{value}`")),
+ }
+}
+
+const CORNERS: [(&str, Corner); 4] = [
+ ("top_left", Corner::TopLeft),
+ ("top_right", Corner::TopRight),
+ ("bottom_left", Corner::BottomLeft),
+ ("bottom_right", Corner::BottomRight),
+];
+
+impl Meta {
+ /// A `.mcmeta` file's Fusion metadata: `None` when it has no `fusion` section.
+ pub fn from_mcmeta(text: &str) -> Result<Option<Self>, String> {
+ let json = Json::parse(text)?;
+ json.get("fusion").map(Self::parse).transpose()
+ }
+
+ /// A `fusion` section (or a `sub_texture`).
+ pub fn parse(json: &Json) -> Result<Self, String> {
+ let base = Base {
+ emissive: bool_field(json, "emissive")?.unwrap_or(false),
+ render_type: choice(
+ json,
+ "render_type",
+ &[
+ ("opaque", RenderType::Opaque),
+ ("cutout", RenderType::Cutout),
+ ("translucent", RenderType::Translucent),
+ ],
+ None,
+ )?,
+ tinting: choice(
+ json,
+ "tinting",
+ &[
+ ("biome_grass", Tinting::Grass),
+ ("biome_foliage", Tinting::Foliage),
+ ("biome_water", Tinting::Water),
+ ],
+ None,
+ )?,
+ };
+ let sub_texture = || {
+ json.get("sub_texture")
+ .map(|s| Self::parse(s).map(Box::new))
+ .transpose()
+ };
+ let per_tile_animation =
+ || Ok::<_, String>(bool_field(json, "per_tile_animation")?.unwrap_or(false));
+ let ty = str_field(json, "type")?.ok_or("no `type`")?;
+ let kind = match kind(ty) {
+ "base" => Kind::Base,
+ "connecting" => Kind::Connecting {
+ layout: match str_field(json, "layout")? {
+ None => Layout::Full,
+ Some(l) => Layout::parse(l).ok_or_else(|| format!("unknown layout `{l}`"))?,
+ },
+ connections: match json.get("connections") {
+ None => Connection::SameState,
+ Some(c) => Connection::parse(c)?,
+ },
+ sub_texture: sub_texture()?,
+ per_tile_animation: per_tile_animation()?,
+ },
+ "random" => Kind::Random {
+ columns: int_field(json, "columns", 1..=100, 1)? as u32,
+ rows: int_field(json, "rows", 1..=100, 1)? as u32,
+ source: choice(
+ json,
+ "randomness_source",
+ &[
+ ("position", RandomSource::Position),
+ ("position_facing", RandomSource::PositionFacing),
+ ("position_axis", RandomSource::PositionAxis),
+ ],
+ Some(RandomSource::PositionFacing),
+ )?
+ .expect("a default"),
+ seed: match json.get("seed") {
+ None => None,
+ Some(_) => Some(int_field(json, "seed", i64::MIN..=i64::MAX, 0)?),
+ },
+ sub_texture: sub_texture()?,
+ per_tile_animation: per_tile_animation()?,
+ },
+ "continuous" => Kind::Continuous {
+ columns: int_field(json, "columns", 1..=32, 1)? as u32,
+ rows: int_field(json, "rows", 1..=32, 1)? as u32,
+ },
+ "scrolling" => {
+ let positive =
+ |key, default| int_field(json, key, 1..=i64::from(u32::MAX), default);
+ Kind::Scrolling(Scrolling {
+ from: choice(json, "from", &CORNERS, Some(Corner::TopLeft))?
+ .expect("a default"),
+ to: choice(json, "to", &CORNERS, Some(Corner::BottomLeft))?.expect("a default"),
+ frame_width: positive("frame_width", 16)? as u32,
+ frame_height: positive("frame_height", 16)? as u32,
+ frame_time: positive("frame_time", 10)? as u32,
+ loop_type: choice(
+ json,
+ "loop_type",
+ &[
+ ("wrap", LoopType::Wrap),
+ ("reset", LoopType::Reset),
+ ("reverse", LoopType::Reverse),
+ ],
+ Some(LoopType::Wrap),
+ )?
+ .expect("a default"),
+ loop_pause: int_field(json, "loop_pause", 0..=i64::from(u32::MAX), 0)? as u32,
+ })
+ }
+ other => return Err(format!("unknown texture type `{other}`")),
+ };
+ Ok(Self { base, kind })
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::BlockMatch;
+
+ fn meta(text: &str) -> Meta {
+ Meta::from_mcmeta(text).unwrap().unwrap()
+ }
+
+ #[test]
+ fn vanilla_mcmeta_has_none() {
+ assert_eq!(
+ Meta::from_mcmeta(r#"{"animation": {"frametime": 2}}"#),
+ Ok(None)
+ );
+ }
+
+ #[test]
+ fn connecting_with_defaults_and_base_options() {
+ let m = meta(
+ r#"{"fusion": {"type": "connecting", "render_type": "cutout", "tinting": "biome_grass"},
+ "animation": {"frametime": 10}}"#,
+ );
+ assert_eq!(
+ m,
+ Meta {
+ base: Base {
+ emissive: false,
+ render_type: Some(RenderType::Cutout),
+ tinting: Some(Tinting::Grass),
+ },
+ kind: Kind::Connecting {
+ layout: Layout::Full,
+ connections: Connection::SameState,
+ sub_texture: None,
+ per_tile_animation: false,
+ },
+ }
+ );
+ }
+
+ #[test]
+ fn connecting_with_a_random_sub_texture() {
+ let m = meta(
+ r#"{"fusion": {"type": "fusion:connecting", "layout": "pieced", "emissive": true,
+ "connections": [{"type": "is_same_block"}, {"type": "match_block", "block": "iron_block"}],
+ "sub_texture": {"type": "random", "rows": 5, "columns": 4, "seed": -3},
+ "per_tile_animation": true}}"#,
+ );
+ assert!(m.base.emissive);
+ let Kind::Connecting {
+ layout,
+ connections,
+ sub_texture,
+ per_tile_animation,
+ } = m.kind
+ else {
+ panic!("{m:?}");
+ };
+ assert_eq!(layout, Layout::Pieced);
+ assert!(per_tile_animation);
+ assert_eq!(
+ connections,
+ Connection::Or(vec![
+ Connection::SameBlock,
+ Connection::Neighbour(vec![BlockMatch {
+ id: "minecraft:iron_block".into(),
+ states: vec![],
+ }]),
+ ])
+ );
+ assert_eq!(
+ sub_texture.unwrap().kind,
+ Kind::Random {
+ columns: 4,
+ rows: 5,
+ source: RandomSource::PositionFacing,
+ seed: Some(-3),
+ sub_texture: None,
+ per_tile_animation: false,
+ }
+ );
+ }
+
+ #[test]
+ fn continuous_and_scrolling() {
+ assert_eq!(
+ meta(r#"{"fusion": {"type": "continuous", "rows": 2, "columns": 3}}"#).kind,
+ Kind::Continuous {
+ columns: 3,
+ rows: 2,
+ }
+ );
+ assert_eq!(
+ meta(
+ r#"{"fusion": {"type": "scrolling", "to": "top_right", "frame_time": 4,
+ "frame_height": 8, "loop_type": "reverse", "loop_pause": 20}}"#
+ )
+ .kind,
+ Kind::Scrolling(Scrolling {
+ from: Corner::TopLeft,
+ to: Corner::TopRight,
+ frame_width: 16,
+ frame_height: 8,
+ frame_time: 4,
+ loop_type: LoopType::Reverse,
+ loop_pause: 20,
+ })
+ );
+ }
+
+ #[test]
+ fn refuses_bad_values() {
+ for bad in [
+ r#"{"fusion": {}}"#,
+ r#"{"fusion": {"type": "glowing"}}"#,
+ r#"{"fusion": {"type": "connecting", "layout": "diagonal"}}"#,
+ r#"{"fusion": {"type": "base", "emissive": "yes"}}"#,
+ r#"{"fusion": {"type": "base", "render_type": "solid"}}"#,
+ r#"{"fusion": {"type": "random", "rows": 0}}"#,
+ r#"{"fusion": {"type": "continuous", "columns": 33}}"#,
+ r#"{"fusion": {"type": "scrolling", "frame_width": 1.5}}"#,
+ ] {
+ assert!(Meta::from_mcmeta(bad).is_err(), "{bad}");
+ }
+ }
+}