Cache resolved block behaviours across launches
This commit is contained in:
Generated
+57
@@ -196,6 +196,15 @@ dependencies = [
|
||||
"libloading",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atomic-polyfill"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4"
|
||||
dependencies = [
|
||||
"critical-section",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atomic-waker"
|
||||
version = "1.1.2"
|
||||
@@ -322,6 +331,12 @@ dependencies = [
|
||||
"syn 3.0.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.12.1"
|
||||
@@ -742,6 +757,12 @@ dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "critical-section"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.23"
|
||||
@@ -1307,6 +1328,15 @@ dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hash32"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.5"
|
||||
@@ -1337,6 +1367,20 @@ dependencies = [
|
||||
"foldhash 0.2.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heapless"
|
||||
version = "0.7.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f"
|
||||
dependencies = [
|
||||
"atomic-polyfill",
|
||||
"hash32",
|
||||
"rustc_version",
|
||||
"serde",
|
||||
"spin",
|
||||
"stable_deref_trait",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
@@ -1608,7 +1652,10 @@ dependencies = [
|
||||
"horizon-core",
|
||||
"libm",
|
||||
"lz4_flex",
|
||||
"postcard",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"xxhash-rust",
|
||||
]
|
||||
|
||||
@@ -2872,6 +2919,7 @@ dependencies = [
|
||||
"cobs",
|
||||
"embedded-io 0.4.0",
|
||||
"embedded-io 0.6.1",
|
||||
"heapless",
|
||||
"serde",
|
||||
]
|
||||
|
||||
@@ -3458,6 +3506,15 @@ dependencies = [
|
||||
"sha2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.9.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spirv"
|
||||
version = "0.4.0+sdk-1.4.341.0"
|
||||
|
||||
@@ -9,6 +9,9 @@ description = "Horizon vanilla world data, NBT, regions and chunk storage"
|
||||
[dependencies]
|
||||
libm = "=0.2.16"
|
||||
serde_json = { version = "1.0.151", features = ["raw_value"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
sha2 = "0.10"
|
||||
postcard = { version = "=1.1.3", features = ["alloc"] }
|
||||
horizon-core = { path = "../horizon-core" }
|
||||
flate2 = { version = "1.1.10", default-features = false, features = ["rust_backend"] }
|
||||
lz4_flex = { version = "0.14", default-features = false, features = ["std", "safe-encode", "safe-decode", "checked-decode"] }
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
//! Measure registry and behaviour load times using prepared client resources.
|
||||
use horizon_world::level::{Behaviours, data::load_registry};
|
||||
use std::{path::Path, time::Instant};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let root = std::env::args()
|
||||
.nth(1)
|
||||
.expect("resource directory argument");
|
||||
let root = Path::new(&root);
|
||||
let started = Instant::now();
|
||||
let registry = load_registry(root)?;
|
||||
eprintln!(
|
||||
"registry_ms={:.3}",
|
||||
started.elapsed().as_secs_f64() * 1000.0
|
||||
);
|
||||
for iteration in 0..3 {
|
||||
let started = Instant::now();
|
||||
let behaviours = Behaviours::load(®istry, root)?;
|
||||
eprintln!(
|
||||
"behaviours_{}_ms={:.3}",
|
||||
iteration,
|
||||
started.elapsed().as_secs_f64() * 1000.0
|
||||
);
|
||||
std::hint::black_box(behaviours);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -21,6 +21,8 @@ use std::{
|
||||
path::Path,
|
||||
sync::Arc,
|
||||
};
|
||||
#[path = "data/cache.rs"]
|
||||
mod cache;
|
||||
|
||||
/// Per-state flags read from vanilla accessors over an empty level.
|
||||
pub mod state_flags {
|
||||
@@ -251,7 +253,7 @@ macro_rules! named_blocks {
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Named { $(pub $field: BlockId,)* }
|
||||
impl Named {
|
||||
fn resolve(table: &StateTable) -> io::Result<Self> {
|
||||
pub(super) fn resolve(table: &StateTable) -> io::Result<Self> {
|
||||
Ok(Self { $($field: table.block_by_name(concat!("minecraft:", $name)).ok_or_else(|| invalid(concat!("missing block ", $name)))?,)* })
|
||||
}
|
||||
}
|
||||
@@ -393,7 +395,7 @@ macro_rules! tags {
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Tags { $(pub $field: Arc<BlockSet>,)* }
|
||||
impl Tags {
|
||||
fn resolve(all: &HashMap<String, Arc<BlockSet>>) -> Self {
|
||||
pub(super) fn resolve(all: &HashMap<String, Arc<BlockSet>>) -> Self {
|
||||
Self { $($field: all.get(concat!("minecraft:", $name)).cloned().unwrap_or_default(),)* }
|
||||
}
|
||||
}
|
||||
@@ -487,7 +489,7 @@ macro_rules! props {
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Props { $(pub $field: PropKey,)* }
|
||||
impl Props {
|
||||
fn resolve(table: &mut StateTable) -> Self {
|
||||
pub(super) fn resolve(table: &mut StateTable) -> Self {
|
||||
Self { $($field: table.key($name),)* }
|
||||
}
|
||||
}
|
||||
@@ -653,6 +655,15 @@ pub fn load_registry(root: &Path) -> io::Result<BlockRegistry> {
|
||||
}
|
||||
|
||||
pub(super) fn load(registry: &BlockRegistry, root: &Path) -> io::Result<Behaviours> {
|
||||
if let Some(behaviours) = cache::load(registry, root) {
|
||||
return Ok(behaviours);
|
||||
}
|
||||
let behaviours = load_uncached(registry, root)?;
|
||||
cache::store(registry, root, &behaviours);
|
||||
Ok(behaviours)
|
||||
}
|
||||
|
||||
fn load_uncached(registry: &BlockRegistry, root: &Path) -> io::Result<Behaviours> {
|
||||
let mut table = StateTable::new(registry)?;
|
||||
let props = Props::resolve(&mut table);
|
||||
let named = Named::resolve(&table)?;
|
||||
|
||||
@@ -0,0 +1,810 @@
|
||||
//! A disposable, versioned snapshot of resolved behaviour tables.
|
||||
//!
|
||||
//! The source resources remain authoritative. Failed reads, unsupported
|
||||
//! versions, corrupt payloads and write failures always use the normal loader.
|
||||
use super::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{
|
||||
io::Write,
|
||||
path::PathBuf,
|
||||
sync::atomic::{AtomicU64, Ordering},
|
||||
};
|
||||
|
||||
const MAGIC: &[u8; 8] = b"HZBEHAV\0";
|
||||
const VERSION: u32 = 2;
|
||||
const FILE_NAME: &str = ".horizon-behaviours-v2.bin";
|
||||
const MAX_FILE_BYTES: usize = 128 * 1024 * 1024;
|
||||
const MAX_DECODED_BYTES: usize = 256 * 1024 * 1024;
|
||||
static NEXT_TEMP: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Snapshot {
|
||||
tags: HashMap<String, Vec<u64>>,
|
||||
facts: Vec<Fact>,
|
||||
shapes: Vec<Shape>,
|
||||
shape_info: Vec<[u8; 3]>,
|
||||
fluid_tags: [u8; 6],
|
||||
blocks: Vec<Block>,
|
||||
items: HashMap<String, Item>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Fact {
|
||||
flags: u32,
|
||||
sturdy: u32,
|
||||
weak: u32,
|
||||
strong: u32,
|
||||
fluid_kind: u8,
|
||||
fluid_amount: u8,
|
||||
fluid_falling: bool,
|
||||
emission: u8,
|
||||
dampening: u8,
|
||||
hardness: f32,
|
||||
push: u8,
|
||||
instrument: u8,
|
||||
shape: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Shape {
|
||||
coords: [Vec<f64>; 3],
|
||||
occupied: Vec<bool>,
|
||||
cube: bool,
|
||||
coordinate_cubes: [bool; 3],
|
||||
min: [i32; 3],
|
||||
max: [i32; 3],
|
||||
/// Preserve the shared singleton identities used by vanilla shape shortcuts.
|
||||
singleton: u8,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct SetTypeSnapshot {
|
||||
name: String,
|
||||
can_open_by_hand: bool,
|
||||
can_open_by_wind_charge: bool,
|
||||
arrows: bool,
|
||||
mobs_only: bool,
|
||||
door_open: String,
|
||||
door_close: String,
|
||||
trapdoor_open: String,
|
||||
trapdoor_close: String,
|
||||
plate_on: String,
|
||||
plate_off: String,
|
||||
button_on: String,
|
||||
button_off: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct SoundsSnapshot {
|
||||
volume: f32,
|
||||
pitch: f32,
|
||||
place: String,
|
||||
break_: String,
|
||||
fall: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Block {
|
||||
chain: Vec<u16>,
|
||||
interfaces: u16,
|
||||
set_type: Option<SetTypeSnapshot>,
|
||||
gate_open: Option<String>,
|
||||
gate_close: Option<String>,
|
||||
open_sound: Option<String>,
|
||||
close_sound: Option<String>,
|
||||
ticks_to_stay_pressed: i32,
|
||||
max_weight: i32,
|
||||
link: Option<u16>,
|
||||
link2: Option<u16>,
|
||||
support_tag: Option<Vec<u64>>,
|
||||
support_tag2: Option<Vec<u64>>,
|
||||
feature: Option<String>,
|
||||
grow_probability: f64,
|
||||
growth_direction: u8,
|
||||
schedule_fluid_ticks: bool,
|
||||
weather: u8,
|
||||
ignite_odds: i32,
|
||||
burn_odds: i32,
|
||||
sounds: SoundsSnapshot,
|
||||
item: String,
|
||||
dynamic_shape: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Item {
|
||||
block: u16,
|
||||
wall_block: Option<u16>,
|
||||
attachment: Option<u8>,
|
||||
kind: u8,
|
||||
game_master: bool,
|
||||
}
|
||||
|
||||
fn fingerprint(registry: &BlockRegistry, root: &Path) -> io::Result<[u8; 32]> {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(b"horizon resolved behaviours 2");
|
||||
for relative in [
|
||||
"facts.json",
|
||||
"block-behaviours.json",
|
||||
"gameplay-facts.json",
|
||||
"block-tags.json",
|
||||
"cached-collision.nbt",
|
||||
] {
|
||||
hash_file(&mut hasher, root, &root.join(relative))?;
|
||||
}
|
||||
let mut fluids: Vec<PathBuf> = fs::read_dir(root.join("data/minecraft/tags/fluid"))?
|
||||
.map(|entry| entry.map(|entry| entry.path()))
|
||||
.collect::<io::Result<_>>()?;
|
||||
fluids.sort();
|
||||
for path in fluids {
|
||||
hash_file(&mut hasher, root, &path)?;
|
||||
}
|
||||
// The registry argument may be modified independently of facts.json.
|
||||
hasher.update((registry.len() as u64).to_le_bytes());
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for index in 0..registry.len() {
|
||||
let state = registry
|
||||
.state(BlockStateId(index as u32))
|
||||
.ok_or_else(|| invalid("missing registry state"))?;
|
||||
hash_str(&mut hasher, &state.name);
|
||||
let mut props: Vec<_> = state.properties.iter().collect();
|
||||
props.sort_by_key(|(key, _)| *key);
|
||||
hasher.update((props.len() as u32).to_le_bytes());
|
||||
for (key, value) in props {
|
||||
hash_str(&mut hasher, key);
|
||||
hash_str(&mut hasher, value);
|
||||
}
|
||||
if seen.insert(state.name.as_str()) {
|
||||
let default = registry
|
||||
.default_state(&state.name)
|
||||
.ok_or_else(|| invalid("missing default state"))?;
|
||||
hasher.update(default.0.to_le_bytes());
|
||||
}
|
||||
}
|
||||
Ok(hasher.finalize().into())
|
||||
}
|
||||
|
||||
fn hash_str(hasher: &mut Sha256, text: &str) {
|
||||
hasher.update((text.len() as u64).to_le_bytes());
|
||||
hasher.update(text.as_bytes());
|
||||
}
|
||||
|
||||
fn hash_file(hasher: &mut Sha256, root: &Path, path: &Path) -> io::Result<()> {
|
||||
let relative = path
|
||||
.strip_prefix(root)
|
||||
.map_err(|e| invalid(e.to_string()))?;
|
||||
hash_str(hasher, &relative.to_string_lossy());
|
||||
let bytes = fs::read(path)?;
|
||||
hasher.update((bytes.len() as u64).to_le_bytes());
|
||||
hasher.update(bytes);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn direction(index: u8) -> io::Result<Direction> {
|
||||
Direction::ALL
|
||||
.get(index as usize)
|
||||
.copied()
|
||||
.ok_or_else(|| invalid("cached direction out of range"))
|
||||
}
|
||||
fn direction_index(direction: Direction) -> u8 {
|
||||
Direction::ALL.iter().position(|d| *d == direction).unwrap() as u8
|
||||
}
|
||||
fn class(index: u16) -> io::Result<Class> {
|
||||
Class::ALL
|
||||
.get(index as usize)
|
||||
.copied()
|
||||
.ok_or_else(|| invalid("cached block class out of range"))
|
||||
}
|
||||
fn kind(index: u8) -> io::Result<ItemKind> {
|
||||
Ok(match index {
|
||||
0 => ItemKind::Block,
|
||||
1 => ItemKind::DoubleHigh,
|
||||
2 => ItemKind::StandingAndWall,
|
||||
3 => ItemKind::Scaffolding,
|
||||
4 => ItemKind::PlaceOnWater,
|
||||
5 => ItemKind::HangingSign,
|
||||
6 => ItemKind::SolidBucket,
|
||||
7 => ItemKind::Other,
|
||||
_ => return Err(invalid("cached item kind out of range")),
|
||||
})
|
||||
}
|
||||
fn kind_index(kind: ItemKind) -> u8 {
|
||||
match kind {
|
||||
ItemKind::Block => 0,
|
||||
ItemKind::DoubleHigh => 1,
|
||||
ItemKind::StandingAndWall => 2,
|
||||
ItemKind::Scaffolding => 3,
|
||||
ItemKind::PlaceOnWater => 4,
|
||||
ItemKind::HangingSign => 5,
|
||||
ItemKind::SolidBucket => 6,
|
||||
ItemKind::Other => 7,
|
||||
}
|
||||
}
|
||||
fn push(index: u8) -> io::Result<PushReaction> {
|
||||
Ok(match index {
|
||||
0 => PushReaction::PushPull,
|
||||
1 => PushReaction::Push,
|
||||
2 => PushReaction::Popped,
|
||||
3 => PushReaction::Immoveable,
|
||||
4 => PushReaction::IgnoreEntity,
|
||||
_ => return Err(invalid("cached push reaction out of range")),
|
||||
})
|
||||
}
|
||||
fn push_index(push: PushReaction) -> u8 {
|
||||
match push {
|
||||
PushReaction::PushPull => 0,
|
||||
PushReaction::Push => 1,
|
||||
PushReaction::Popped => 2,
|
||||
PushReaction::Immoveable => 3,
|
||||
PushReaction::IgnoreEntity => 4,
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&SetType> for SetTypeSnapshot {
|
||||
fn from(value: &SetType) -> Self {
|
||||
Self {
|
||||
name: value.name.to_string(),
|
||||
can_open_by_hand: value.can_open_by_hand,
|
||||
can_open_by_wind_charge: value.can_open_by_wind_charge,
|
||||
arrows: value.arrows,
|
||||
mobs_only: value.mobs_only,
|
||||
door_open: value.door_open.to_string(),
|
||||
door_close: value.door_close.to_string(),
|
||||
trapdoor_open: value.trapdoor_open.to_string(),
|
||||
trapdoor_close: value.trapdoor_close.to_string(),
|
||||
plate_on: value.plate_on.to_string(),
|
||||
plate_off: value.plate_off.to_string(),
|
||||
button_on: value.button_on.to_string(),
|
||||
button_off: value.button_off.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<SetTypeSnapshot> for SetType {
|
||||
fn from(value: SetTypeSnapshot) -> Self {
|
||||
Self {
|
||||
name: value.name.into(),
|
||||
can_open_by_hand: value.can_open_by_hand,
|
||||
can_open_by_wind_charge: value.can_open_by_wind_charge,
|
||||
arrows: value.arrows,
|
||||
mobs_only: value.mobs_only,
|
||||
door_open: Arc::from(value.door_open),
|
||||
door_close: Arc::from(value.door_close),
|
||||
trapdoor_open: Arc::from(value.trapdoor_open),
|
||||
trapdoor_close: Arc::from(value.trapdoor_close),
|
||||
plate_on: Arc::from(value.plate_on),
|
||||
plate_off: Arc::from(value.plate_off),
|
||||
button_on: Arc::from(value.button_on),
|
||||
button_off: Arc::from(value.button_off),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<&Sounds> for SoundsSnapshot {
|
||||
fn from(value: &Sounds) -> Self {
|
||||
Self {
|
||||
volume: value.volume,
|
||||
pitch: value.pitch,
|
||||
place: value.place.to_string(),
|
||||
break_: value.break_.to_string(),
|
||||
fall: value.fall.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<SoundsSnapshot> for Sounds {
|
||||
fn from(value: SoundsSnapshot) -> Self {
|
||||
Self {
|
||||
volume: value.volume,
|
||||
pitch: value.pitch,
|
||||
place: Arc::from(value.place),
|
||||
break_: Arc::from(value.break_),
|
||||
fall: Arc::from(value.fall),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Snapshot {
|
||||
fn capture(behaviours: &Behaviours) -> Self {
|
||||
let tags = behaviours
|
||||
.all_tags
|
||||
.iter()
|
||||
.map(|(key, value)| (key.clone(), value.0.clone()))
|
||||
.collect();
|
||||
let facts = behaviours
|
||||
.facts
|
||||
.iter()
|
||||
.map(|fact| Fact {
|
||||
flags: fact.flags,
|
||||
sturdy: fact.sturdy,
|
||||
weak: fact.weak,
|
||||
strong: fact.strong,
|
||||
fluid_kind: fact.fluid.kind() as u8,
|
||||
fluid_amount: fact.fluid.amount(),
|
||||
fluid_falling: fact.fluid.falling(),
|
||||
emission: fact.emission,
|
||||
dampening: fact.dampening,
|
||||
hardness: fact.hardness,
|
||||
push: push_index(fact.push),
|
||||
instrument: fact.instrument,
|
||||
shape: fact.shape,
|
||||
})
|
||||
.collect();
|
||||
let shapes = behaviours
|
||||
.shapes
|
||||
.iter()
|
||||
.map(|shape| {
|
||||
let sizes = shape.sizes();
|
||||
let mut occupied = Vec::with_capacity(sizes.iter().product());
|
||||
for x in 0..sizes[0] {
|
||||
for y in 0..sizes[1] {
|
||||
for z in 0..sizes[2] {
|
||||
occupied.push(shape.is_full(x as i32, y as i32, z as i32));
|
||||
}
|
||||
}
|
||||
}
|
||||
let (min, max) = shape.discrete_bounds();
|
||||
Shape {
|
||||
coords: std::array::from_fn(|axis| {
|
||||
shape
|
||||
.coordinates(
|
||||
[
|
||||
horizon_core::geometry::Axis::X,
|
||||
horizon_core::geometry::Axis::Y,
|
||||
horizon_core::geometry::Axis::Z,
|
||||
][axis],
|
||||
)
|
||||
.to_vec()
|
||||
}),
|
||||
occupied,
|
||||
cube: shape.is_cube(),
|
||||
coordinate_cubes: shape.cube_coordinate_axes(),
|
||||
min,
|
||||
max,
|
||||
singleton: if shape.same_identity(&VoxelShape::block()) {
|
||||
1
|
||||
} else if shape.same_identity(&VoxelShape::empty()) {
|
||||
2
|
||||
} else {
|
||||
0
|
||||
},
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let shape_info = behaviours
|
||||
.shape_info
|
||||
.iter()
|
||||
.map(|info| [info.full_faces, info.nonempty_faces, info.wall_cover])
|
||||
.collect();
|
||||
let tags = tags;
|
||||
let f = behaviours.fluid_tags;
|
||||
let fluid_tags = [
|
||||
f.water,
|
||||
f.lava,
|
||||
f.supports_frogspawn,
|
||||
f.supports_lily_pad,
|
||||
f.supports_sugar_cane_adjacently,
|
||||
f.bubble_column_can_occupy,
|
||||
];
|
||||
let blocks = behaviours
|
||||
.blocks
|
||||
.iter()
|
||||
.map(|block| Block {
|
||||
chain: block.chain.iter().map(|class| *class as u16).collect(),
|
||||
interfaces: block.interfaces,
|
||||
set_type: block
|
||||
.set_type
|
||||
.as_ref()
|
||||
.map(|value| SetTypeSnapshot::from(value.as_ref())),
|
||||
gate_open: block.gate_open.as_ref().map(|v| v.to_string()),
|
||||
gate_close: block.gate_close.as_ref().map(|v| v.to_string()),
|
||||
open_sound: block.open_sound.as_ref().map(|v| v.to_string()),
|
||||
close_sound: block.close_sound.as_ref().map(|v| v.to_string()),
|
||||
ticks_to_stay_pressed: block.ticks_to_stay_pressed,
|
||||
max_weight: block.max_weight,
|
||||
link: block.link.map(|v| v.0),
|
||||
link2: block.link2.map(|v| v.0),
|
||||
support_tag: block.support_tag.as_ref().map(|v| v.0.clone()),
|
||||
support_tag2: block.support_tag2.as_ref().map(|v| v.0.clone()),
|
||||
feature: block.feature.as_ref().map(|v| v.to_string()),
|
||||
grow_probability: block.grow_probability,
|
||||
growth_direction: direction_index(block.growth_direction),
|
||||
schedule_fluid_ticks: block.schedule_fluid_ticks,
|
||||
weather: block.weather,
|
||||
ignite_odds: block.ignite_odds,
|
||||
burn_odds: block.burn_odds,
|
||||
sounds: SoundsSnapshot::from(block.sounds.as_ref()),
|
||||
item: block.item.to_string(),
|
||||
dynamic_shape: block.dynamic_shape,
|
||||
})
|
||||
.collect();
|
||||
let items = behaviours
|
||||
.items
|
||||
.iter()
|
||||
.map(|(name, item)| {
|
||||
(
|
||||
name.clone(),
|
||||
Item {
|
||||
block: item.block.0,
|
||||
wall_block: item.wall_block.map(|v| v.0),
|
||||
attachment: item.attachment.map(direction_index),
|
||||
kind: kind_index(item.kind),
|
||||
game_master: item.game_master,
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
Self {
|
||||
tags,
|
||||
facts,
|
||||
shapes,
|
||||
shape_info,
|
||||
fluid_tags,
|
||||
blocks,
|
||||
items,
|
||||
}
|
||||
}
|
||||
|
||||
fn restore(self, registry: &BlockRegistry) -> io::Result<Behaviours> {
|
||||
let mut table = StateTable::new(registry)?;
|
||||
let props = Props::resolve(&mut table);
|
||||
let named = Named::resolve(&table)?;
|
||||
if self.facts.len() != table.state_count()
|
||||
|| self.blocks.len() != table.block_count()
|
||||
|| self.shape_info.len() != self.shapes.len()
|
||||
{
|
||||
return Err(invalid("cached behaviour table size mismatch"));
|
||||
}
|
||||
let all_tags: HashMap<String, Arc<BlockSet>> = self
|
||||
.tags
|
||||
.into_iter()
|
||||
.map(|(key, value)| (key, Arc::new(BlockSet(value))))
|
||||
.collect();
|
||||
let tags = Tags::resolve(&all_tags);
|
||||
let shapes = self
|
||||
.shapes
|
||||
.into_iter()
|
||||
.map(|wire| {
|
||||
let shape = match wire.singleton {
|
||||
1 => VoxelShape::block(),
|
||||
2 => VoxelShape::empty(),
|
||||
0 => {
|
||||
if wire.cube {
|
||||
let sizes = std::array::from_fn(|axis| {
|
||||
wire.coords[axis].len().saturating_sub(1)
|
||||
});
|
||||
VoxelShape::from_cube(sizes, &wire.occupied)
|
||||
.map_err(|e| invalid(e.to_string()))?
|
||||
} else {
|
||||
VoxelShape::from_mixed_grid(
|
||||
wire.coords,
|
||||
&wire.occupied,
|
||||
wire.coordinate_cubes,
|
||||
)
|
||||
.map_err(|e| invalid(e.to_string()))?
|
||||
}
|
||||
}
|
||||
_ => return Err(invalid("invalid cached shape identity")),
|
||||
};
|
||||
shape
|
||||
.with_discrete_bounds(wire.min, wire.max)
|
||||
.map_err(|e| invalid(e.to_string()))
|
||||
})
|
||||
.collect::<io::Result<Vec<_>>>()?;
|
||||
let shape_info = self
|
||||
.shape_info
|
||||
.into_iter()
|
||||
.map(|row| ShapeInfo {
|
||||
full_faces: row[0],
|
||||
nonempty_faces: row[1],
|
||||
wall_cover: row[2],
|
||||
})
|
||||
.collect();
|
||||
let facts = self
|
||||
.facts
|
||||
.into_iter()
|
||||
.map(|wire| {
|
||||
if wire.shape < -1 || wire.shape >= shapes.len() as i32 {
|
||||
return Err(invalid("cached shape index out of range"));
|
||||
}
|
||||
let fluid_kind = FluidKind::ALL
|
||||
.get(wire.fluid_kind as usize)
|
||||
.copied()
|
||||
.ok_or_else(|| invalid("cached fluid kind out of range"))?;
|
||||
Ok(StateFacts {
|
||||
flags: wire.flags,
|
||||
sturdy: wire.sturdy,
|
||||
weak: wire.weak,
|
||||
strong: wire.strong,
|
||||
fluid: FluidState::new(fluid_kind, wire.fluid_amount, wire.fluid_falling),
|
||||
emission: wire.emission,
|
||||
dampening: wire.dampening,
|
||||
hardness: wire.hardness,
|
||||
push: push(wire.push)?,
|
||||
instrument: wire.instrument,
|
||||
shape: wire.shape,
|
||||
})
|
||||
})
|
||||
.collect::<io::Result<Vec<_>>>()?;
|
||||
let f = self.fluid_tags;
|
||||
let fluid_tags = FluidTags {
|
||||
water: f[0],
|
||||
lava: f[1],
|
||||
supports_frogspawn: f[2],
|
||||
supports_lily_pad: f[3],
|
||||
supports_sugar_cane_adjacently: f[4],
|
||||
bubble_column_can_occupy: f[5],
|
||||
};
|
||||
let blocks = self
|
||||
.blocks
|
||||
.into_iter()
|
||||
.map(|wire| {
|
||||
let chain = wire
|
||||
.chain
|
||||
.into_iter()
|
||||
.map(class)
|
||||
.collect::<io::Result<Vec<_>>>()?;
|
||||
let mut classes = ClassSet::default();
|
||||
for class in &chain {
|
||||
classes.insert(*class);
|
||||
}
|
||||
let class = chain.first().copied().unwrap_or(Class::Block);
|
||||
for link in [wire.link, wire.link2].into_iter().flatten() {
|
||||
if link as usize >= table.block_count() {
|
||||
return Err(invalid("cached block link out of range"));
|
||||
}
|
||||
}
|
||||
Ok(BlockData {
|
||||
class,
|
||||
chain,
|
||||
classes,
|
||||
interfaces: wire.interfaces,
|
||||
set_type: wire.set_type.map(|v| Arc::new(SetType::from(v))),
|
||||
gate_open: wire.gate_open.map(Arc::from),
|
||||
gate_close: wire.gate_close.map(Arc::from),
|
||||
open_sound: wire.open_sound.map(Arc::from),
|
||||
close_sound: wire.close_sound.map(Arc::from),
|
||||
ticks_to_stay_pressed: wire.ticks_to_stay_pressed,
|
||||
max_weight: wire.max_weight,
|
||||
link: wire.link.map(BlockId),
|
||||
link2: wire.link2.map(BlockId),
|
||||
support_tag: wire.support_tag.map(|v| Arc::new(BlockSet(v))),
|
||||
support_tag2: wire.support_tag2.map(|v| Arc::new(BlockSet(v))),
|
||||
feature: wire.feature.map(Arc::from),
|
||||
grow_probability: wire.grow_probability,
|
||||
growth_direction: direction(wire.growth_direction)?,
|
||||
schedule_fluid_ticks: wire.schedule_fluid_ticks,
|
||||
weather: wire.weather,
|
||||
ignite_odds: wire.ignite_odds,
|
||||
burn_odds: wire.burn_odds,
|
||||
sounds: Arc::new(Sounds::from(wire.sounds)),
|
||||
item: Arc::from(wire.item),
|
||||
dynamic_shape: wire.dynamic_shape,
|
||||
})
|
||||
})
|
||||
.collect::<io::Result<Vec<_>>>()?;
|
||||
let items = self
|
||||
.items
|
||||
.into_iter()
|
||||
.map(|(name, wire)| {
|
||||
if wire.block as usize >= table.block_count()
|
||||
|| wire
|
||||
.wall_block
|
||||
.is_some_and(|id| id as usize >= table.block_count())
|
||||
{
|
||||
return Err(invalid("cached item block out of range"));
|
||||
}
|
||||
Ok((
|
||||
name,
|
||||
ItemBinding {
|
||||
block: BlockId(wire.block),
|
||||
wall_block: wire.wall_block.map(BlockId),
|
||||
attachment: wire.attachment.map(direction).transpose()?,
|
||||
kind: kind(wire.kind)?,
|
||||
game_master: wire.game_master,
|
||||
},
|
||||
))
|
||||
})
|
||||
.collect::<io::Result<HashMap<_, _>>>()?;
|
||||
let liquid_blocks = FluidBlockBindings::new(registry)?;
|
||||
let mut dynamic = HashMap::new();
|
||||
for (id, fact) in facts.iter().enumerate() {
|
||||
if fact.shape < 0 {
|
||||
let state = BlockStateId(id as u32);
|
||||
if let Some(support) = crate::dynamic_support::DynamicSupport::from_state(
|
||||
registry.state(state).unwrap(),
|
||||
)? {
|
||||
dynamic.insert(state, support);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Behaviours {
|
||||
table,
|
||||
props,
|
||||
named,
|
||||
tags,
|
||||
all_tags,
|
||||
facts,
|
||||
shapes,
|
||||
shape_info,
|
||||
fluid_tags,
|
||||
blocks,
|
||||
items,
|
||||
liquid_blocks,
|
||||
dynamic,
|
||||
occlusion_cache: Default::default(),
|
||||
growth: Default::default(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn load(registry: &BlockRegistry, root: &Path) -> Option<Behaviours> {
|
||||
let fingerprint = fingerprint(registry, root).ok()?;
|
||||
let bytes = fs::read(root.join(FILE_NAME)).ok()?;
|
||||
// Validate bounds before decoding untrusted cache contents.
|
||||
if bytes.len() < 8 + 4 + 32 + 32 || bytes.len() > MAX_FILE_BYTES {
|
||||
return None;
|
||||
}
|
||||
if &bytes[..8] != MAGIC
|
||||
|| u32::from_le_bytes(bytes[8..12].try_into().ok()?) != VERSION
|
||||
|| bytes[12..44] != fingerprint
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let body = &bytes[76..];
|
||||
if Sha256::digest(body).as_slice() != &bytes[44..76] {
|
||||
return None;
|
||||
}
|
||||
let expanded_len = u32::from_le_bytes(body.get(..4)?.try_into().ok()?) as usize;
|
||||
if expanded_len > MAX_DECODED_BYTES {
|
||||
return None;
|
||||
}
|
||||
let expanded = lz4_flex::decompress_size_prepended(body).ok()?;
|
||||
let snapshot: Snapshot = postcard::from_bytes(&expanded).ok()?;
|
||||
snapshot.restore(registry).ok()
|
||||
}
|
||||
|
||||
pub(super) fn store(registry: &BlockRegistry, root: &Path, behaviours: &Behaviours) {
|
||||
let _ = write(registry, root, behaviours);
|
||||
}
|
||||
|
||||
fn write(registry: &BlockRegistry, root: &Path, behaviours: &Behaviours) -> io::Result<()> {
|
||||
let fingerprint = fingerprint(registry, root)?;
|
||||
let snapshot = Snapshot::capture(behaviours);
|
||||
let serialized = postcard::to_allocvec(&snapshot).map_err(|e| invalid(e.to_string()))?;
|
||||
let body = lz4_flex::compress_prepend_size(&serialized);
|
||||
if body.len() + 76 > MAX_FILE_BYTES {
|
||||
return Err(invalid("behaviour cache too large"));
|
||||
}
|
||||
let mut bytes = Vec::with_capacity(76 + body.len());
|
||||
bytes.extend_from_slice(MAGIC);
|
||||
bytes.extend_from_slice(&VERSION.to_le_bytes());
|
||||
bytes.extend_from_slice(&fingerprint);
|
||||
bytes.extend_from_slice(&Sha256::digest(&body));
|
||||
bytes.extend_from_slice(&body);
|
||||
let path = root.join(FILE_NAME);
|
||||
let tmp = root.join(format!(
|
||||
"{FILE_NAME}.tmp-{}-{}",
|
||||
std::process::id(),
|
||||
NEXT_TEMP.fetch_add(1, Ordering::Relaxed)
|
||||
));
|
||||
let result = (|| {
|
||||
let mut file = fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(&tmp)?;
|
||||
file.write_all(&bytes)?;
|
||||
file.sync_all()?;
|
||||
fs::rename(&tmp, &path)
|
||||
})();
|
||||
if result.is_err() {
|
||||
let _ = fs::remove_file(&tmp);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::atomic::AtomicU64;
|
||||
|
||||
static NEXT_FIXTURE: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
struct FixtureDir(PathBuf);
|
||||
impl Drop for FixtureDir {
|
||||
fn drop(&mut self) {
|
||||
let _ = fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn fixture() -> Option<FixtureDir> {
|
||||
let source = std::env::var_os("HORIZON_RESOURCES")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3")
|
||||
});
|
||||
if !source.join("block-behaviours.json").exists() {
|
||||
eprintln!("skipping behaviour cache tests: no client resources");
|
||||
return None;
|
||||
}
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"horizon-behaviour-cache-test-{}-{}",
|
||||
std::process::id(),
|
||||
NEXT_FIXTURE.fetch_add(1, Ordering::Relaxed),
|
||||
));
|
||||
fs::create_dir_all(dir.join("data/minecraft/tags/fluid")).unwrap();
|
||||
for file in [
|
||||
"facts.json",
|
||||
"block-behaviours.json",
|
||||
"gameplay-facts.json",
|
||||
"block-tags.json",
|
||||
"cached-collision.nbt",
|
||||
] {
|
||||
fs::copy(source.join(file), dir.join(file)).unwrap();
|
||||
}
|
||||
for entry in fs::read_dir(source.join("data/minecraft/tags/fluid")).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
if entry.file_type().unwrap().is_file() {
|
||||
fs::copy(
|
||||
entry.path(),
|
||||
dir.join("data/minecraft/tags/fluid")
|
||||
.join(entry.file_name()),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
Some(FixtureDir(dir))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolved_snapshot_roundtrips_all_fields() {
|
||||
let Some(root) = fixture() else {
|
||||
return;
|
||||
};
|
||||
let registry = super::super::load_registry(&root.0).unwrap();
|
||||
let original = super::super::load_uncached(®istry, &root.0).unwrap();
|
||||
let captured = Snapshot::capture(&original);
|
||||
let restored = captured.restore(®istry).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::to_value(Snapshot::capture(&original)).unwrap(),
|
||||
serde_json::to_value(Snapshot::capture(&restored)).unwrap(),
|
||||
"all resolved fields, shape grids and identities should roundtrip",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cache_invalidates_sources_and_recovers_from_corruption() {
|
||||
let Some(root) = fixture() else {
|
||||
return;
|
||||
};
|
||||
let registry = super::super::load_registry(&root.0).unwrap();
|
||||
assert!(load(®istry, &root.0).is_none());
|
||||
let initial = super::super::load(®istry, &root.0).unwrap();
|
||||
assert!(root.0.join(FILE_NAME).is_file());
|
||||
let hit = load(®istry, &root.0).expect("cache hit");
|
||||
assert_eq!(initial.table.state_count(), hit.table.state_count());
|
||||
let tags = root.0.join("block-tags.json");
|
||||
fs::OpenOptions::new()
|
||||
.append(true)
|
||||
.open(&tags)
|
||||
.unwrap()
|
||||
.write_all(b"\n")
|
||||
.unwrap();
|
||||
assert!(
|
||||
load(®istry, &root.0).is_none(),
|
||||
"changed resource must invalidate"
|
||||
);
|
||||
super::super::load(®istry, &root.0).expect("source fallback");
|
||||
assert!(
|
||||
load(®istry, &root.0).is_some(),
|
||||
"fallback should replace cache"
|
||||
);
|
||||
let cache_file = root.0.join(FILE_NAME);
|
||||
let mut bytes = fs::read(&cache_file).unwrap();
|
||||
bytes[80] ^= 0x80;
|
||||
fs::write(&cache_file, bytes).unwrap();
|
||||
assert!(
|
||||
load(®istry, &root.0).is_none(),
|
||||
"checksum failure must invalidate"
|
||||
);
|
||||
super::super::load(®istry, &root.0).expect("corrupt cache fallback");
|
||||
assert!(load(®istry, &root.0).is_some());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Measure the resource-dependent Behaviours::load portion of client startup.
|
||||
|
||||
Build the behaviour_load_profile example in the checkout to be measured first:
|
||||
cargo build --release -p horizon-world --example behaviour_load_profile
|
||||
|
||||
Run the profiler from the repository root with the matching example binary.
|
||||
This measures behaviour loading, not time to first rendered frame.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import re
|
||||
import statistics
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--binary", type=Path, default=Path("target/release/examples/behaviour_load_profile"))
|
||||
parser.add_argument("--resources", type=Path, default=Path(".local/horizon-client/26.3"))
|
||||
parser.add_argument("--repeats", type=int, default=5)
|
||||
parser.add_argument("--output", type=Path)
|
||||
args = parser.parse_args()
|
||||
if args.repeats < 1:
|
||||
parser.error("--repeats must be at least one")
|
||||
samples = []
|
||||
for _ in range(args.repeats):
|
||||
run = subprocess.run([str(args.binary.resolve()), str(args.resources.resolve())],
|
||||
text=True, capture_output=True, check=True)
|
||||
values = {key: float(value) for key, value in
|
||||
re.findall(r"^(registry_ms|behaviours_[0-2]_ms)=([\d.]+)$", run.stderr, re.M)}
|
||||
if len(values) != 4:
|
||||
raise RuntimeError(f"incomplete profile output: {run.stderr}")
|
||||
samples.append(values)
|
||||
summary = {key: {"median_ms": round(statistics.median(row[key] for row in samples), 3),
|
||||
"samples_ms": [row[key] for row in samples]}
|
||||
for key in samples[0]}
|
||||
result = {"scope": "behaviour loader only; not full client startup",
|
||||
"binary": str(args.binary), "resources": str(args.resources), "repeats": args.repeats,
|
||||
"metrics": summary}
|
||||
print(json.dumps(result, indent=2))
|
||||
if args.output:
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text(json.dumps(result, indent=2) + "\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user