Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/app/validate/src/check_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn check_chunks(state: &ServerState) -> Result<(), String> {
if chunk_format_hash_str != Chunk::type_hash() {
error!(
"Chunk format hash mismatch. Expected 0X{:0X}, got 0X{:0X}. This likely means that the chunk format has changed since saving. If you have \
recently updated Temper you will have to go back to the older version until a world format converter is implemented.)",
recently updated Temper you will have to go back to the older version until a world format converter is implemented.",
Chunk::type_hash(),
chunk_format_hash_str
);
Expand All @@ -36,7 +36,7 @@ pub fn check_chunks(state: &ServerState) -> Result<(), String> {
"Could not find 'chunk-format-hash' in metadata. This likely means that the world was saved with an older version of Temper that did not include this metadata, or that the metadata is corrupted."
);
error!(
"If you have recently updated Temper you will have to go back to the older version until a world format converter is implemented.)"
"If you have recently updated Temper you will have to go back to the older version until a world format converter is implemented."
);
exit(1);
}
Expand Down Expand Up @@ -100,12 +100,12 @@ pub fn check_chunks(state: &ServerState) -> Result<(), String> {
exit(1);
}
}
if let Err(e) = bitcode::decode::<Chunk>(&data) {
if let Err(e) = bitcode::deserialize::<Chunk>(&data) {
progress_bar.finish();
error!("Chunk {} failed to decode: {}", decoded_key, e);
error!(
"This generally means that the chunk format has changed since saving. If you have \
recently updated Temper you will have to go back to the older version until a world format converter is implemented.)"
recently updated Temper you will have to go back to the older version until a world format converter is implemented."
);
exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/base/logging/src/tui_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl LogFormatter for TuiTracingFormatter {

spans.push(
evt.timestamp
.format("%Y-%m-%d %H:%M:%S | ")
.strftime("%Y-%m-%d %H:%M:%S | ")
.to_string()
.dim(),
);
Expand Down
4 changes: 2 additions & 2 deletions src/core/src/block_state_id.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::block_data::BlockData;
use ahash::RandomState;
use bitcode_derive::{Decode, Encode};
use deepsize::DeepSizeOf;
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Display;
use std::process::exit;
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn create_block_mappings() -> (Vec<BlockData>, HashMap<BlockData, i32, Rando
///
/// This should be used over `BlockData` in most cases, as it's much more efficient to store and pass around.
/// You can also generate a block's id at runtime with the [temper_macros::block!] macro.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Encode, Decode, DeepSizeOf, TypeHash)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize, DeepSizeOf, TypeHash)]
pub struct BlockStateId(u32);

impl BlockStateId {
Expand Down
4 changes: 2 additions & 2 deletions src/core/src/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use bevy_math::U8Vec3;
use bevy_math::Vec2Swizzles;
use bevy_math::Vec3Swizzles;
use bevy_math::{DVec3, I16Vec3};
use bitcode_derive::{Decode, Encode};
use deepsize::DeepSizeOf;
use serde::{Deserialize, Serialize};
use temper_codec::net_types::network_position::NetworkPosition;
use type_hash::TypeHash;

Expand Down Expand Up @@ -92,7 +92,7 @@ impl Add<(i32, i32, i32)> for BlockPos {
}
}

#[derive(Clone, Copy, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Clone, Copy, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub struct ChunkHeight {
pub min_y: i16,
pub height: u16,
Expand Down
6 changes: 3 additions & 3 deletions src/world/db/src/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn save_chunk_internal(
storage.create_table("chunks".to_string())?;
}
let as_bytes = yazi::compress(
&bitcode::encode(chunk),
&bitcode::serialize(chunk).expect("Unable to serialize chunk"),
yazi::Format::Zlib,
CompressionLevel::BestSpeed,
)?;
Expand Down Expand Up @@ -47,8 +47,8 @@ pub fn load_chunk_internal(
warn!("Chunk data does not have a checksum, skipping verification.");
}
}
let chunk: Chunk = bitcode::decode(&data)
.map_err(|e| WorldError::BitcodeDecodeError(e.to_string()))?;
let chunk: Chunk = bitcode::deserialize(&data)
.map_err(|e| WorldError::BitcodeDeserializeError(e.to_string()))?;
Ok(chunk)
}
None => Err(WorldError::ChunkNotFound),
Expand Down
16 changes: 14 additions & 2 deletions src/world/format/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2024"

[dependencies]
bitcode_derive = { workspace = true }
bitcode = { workspace = true }
bitcode = { workspace = true, features = ["serde"] }
temper-codec = { workspace = true }
temper-macros = { workspace = true }
deepsize = { workspace = true }
Expand All @@ -21,4 +21,16 @@ temper-nbt = { workspace = true }
serde = { workspace = true }
bytemuck = { workspace = true }
tokio = { workspace = true }
type_hash = { workspace = true }
type_hash = { workspace = true }

[dev-dependencies]
criterion = { workspace = true }
rand = { workspace = true }

[[bench]]

name = "bench"
harness = false



9 changes: 9 additions & 0 deletions src/world/format/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use criterion::{Criterion, criterion_group, criterion_main};
mod serialize;

fn world_format_bench(c: &mut Criterion) {
serialize::bench_serialize_world(c);
}

criterion_group!(benches, world_format_bench);
criterion_main!(benches);
38 changes: 38 additions & 0 deletions src/world/format/benches/serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use criterion::Criterion;
use std::hint::black_box;
use temper_core::block_state_id::BlockStateId;
use temper_core::pos::{ChunkBlockPos, ChunkHeight};
use temper_world_format::Chunk;

fn serialize_chunk(chunk: &mut Chunk) -> Vec<u8> {
black_box(bitcode::serialize(black_box(chunk)).unwrap())
}

pub fn bench_serialize_world(c: &mut Criterion) {
let mut chunk = Chunk::new_empty_with_height(ChunkHeight::new(-64, 384));
for x in 0..16 {
for y in -64..320 {
for z in 0..16 {
if (x + z) % 3 == 0 {
chunk.set_block(
ChunkBlockPos::new(x, y, z),
BlockStateId::new(rand::random_range(0..20_000)),
)
}
}
}
}
c.bench_function("format/Serialize", |b| {
b.iter(|| {
black_box(serialize_chunk(&mut chunk));
})
});

let serialized = serialize_chunk(&mut chunk);

c.bench_function("format/Deserialize", |b| {
b.iter(|| {
black_box(bitcode::deserialize::<Chunk>(black_box(&serialized))).unwrap();
})
});
}
8 changes: 4 additions & 4 deletions src/world/format/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ pub enum WorldError {
GenericIOError(String),
#[error("A database error occurred from the world crate: {0}")]
DatabaseError(StorageError),
#[error("There was an error with bitcode's decoding: {0}")]
BitcodeDecodeError(String),
#[error("There was an error with bitcode's encoding: {0}")]
BitcodeEncodeError(String),
#[error("There was an error with bitcode's deserializing: {0}")]
BitcodeDeserializeError(String),
#[error("There was an error with bitcode's serializing: {0}")]
BitcodeSerializeError(String),
#[error("Chunk not found")]
ChunkNotFound,
#[error("Anvil Decode Error: {0}")]
Expand Down
6 changes: 3 additions & 3 deletions src/world/format/src/heightmap.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::errors::WorldError;
use crate::vanilla_chunk_format::VanillaHeightmaps;
use bitcode_derive::{Decode, Encode};
use deepsize::DeepSizeOf;
use serde_derive::{Deserialize, Serialize};
use temper_codec::net_types::length_prefixed_vec::LengthPrefixedVec;
use temper_codec::net_types::var_int::VarInt;
use temper_macros::NetEncode;
use type_hash::TypeHash;

#[derive(Default, Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Default, Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub struct Heightmaps {
pub world_surface: ChunkHeightmap,
pub motion_blocking: ChunkHeightmap,
}

#[derive(Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub struct ChunkHeightmap {
data: Box<[i16]>,
}
Expand Down
4 changes: 2 additions & 2 deletions src/world/format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ pub mod vanilla_chunk_format;
use crate::errors::WorldError;
use crate::heightmap::Heightmaps;
use crate::section::{AIR, ChunkSection};
use bitcode_derive::{Decode, Encode};
use deepsize::DeepSizeOf;
use serde_derive::{Deserialize, Serialize};
use temper_core::block_state_id::BlockStateId;
use temper_core::pos::{ChunkBlockPos, ChunkHeight};
use temper_macros::block;
use type_hash::TypeHash;
use vanilla_chunk_format::VanillaChunk;

#[derive(Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub struct Chunk {
pub sections: Box<[ChunkSection]>,
height: ChunkHeight,
Expand Down
6 changes: 3 additions & 3 deletions src/world/format/src/light/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use bitcode_derive::{Decode, Encode};
use deepsize::DeepSizeOf;
use serde_derive::{Deserialize, Serialize};
use type_hash::TypeHash;

pub mod network;

#[derive(Default, Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Default, Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub(crate) enum LightStorage {
#[default]
Empty,
Expand All @@ -14,7 +14,7 @@ pub(crate) enum LightStorage {
},
}

#[derive(Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub struct SectionLightData {
sky_light: LightStorage,
block_light: LightStorage,
Expand Down
4 changes: 2 additions & 2 deletions src/world/format/src/palette.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::section::AIR;
use bitcode_derive::{Decode, Encode};
use deepsize::DeepSizeOf;
use serde_derive::{Deserialize, Serialize};
use std::num::NonZeroU16;
use temper_core::block_state_id::BlockStateId;
use type_hash::TypeHash;
Expand All @@ -18,7 +18,7 @@ pub enum BlockPaletteResult {
ConvertToUniform(BlockStateId),
}

#[derive(Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub struct BlockPalette {
pub(crate) palette: Vec<Option<(BlockStateId, NonZeroU16)>>,
pub(crate) free_count: u16,
Expand Down
8 changes: 5 additions & 3 deletions src/world/format/src/section/biome.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use bitcode_derive::{Decode, Encode};
use bytemuck::{Pod, Zeroable};
use deepsize::DeepSizeOf;
use serde_derive::{Deserialize, Serialize};
use temper_core::pos::SectionBlockPos;
use type_hash::TypeHash;

#[repr(transparent)]
#[derive(Copy, Clone, Encode, Decode, Default, PartialEq, DeepSizeOf, Pod, Zeroable, TypeHash)]
#[derive(
Copy, Clone, Serialize, Deserialize, Default, PartialEq, DeepSizeOf, Pod, Zeroable, TypeHash,
)]
pub struct BiomeType(pub u8);

#[derive(Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub enum BiomeData {
Uniform(BiomeType),
Mixed(Box<[BiomeType]>),
Expand Down
4 changes: 2 additions & 2 deletions src/world/format/src/section/direct.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::section::paletted::PalettedSection;
use crate::section::uniform::UniformSection;
use crate::section::{AIR, CHUNK_SECTION_LENGTH};
use bitcode_derive::{Decode, Encode};
use deepsize::DeepSizeOf;
use serde_derive::{Deserialize, Serialize};
use temper_core::block_state_id::BlockStateId;
use type_hash::TypeHash;

Expand All @@ -11,7 +11,7 @@ type CompactBlockStateId = u16;

const AIR_COMPACT: CompactBlockStateId = AIR.raw() as CompactBlockStateId;

#[derive(Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub struct DirectSection(pub(crate) Box<[CompactBlockStateId]>, u16);

impl Default for DirectSection {
Expand Down
6 changes: 3 additions & 3 deletions src/world/format/src/section/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::section::direct::DirectSection;
use crate::section::paletted::{PalettedSection, PalettedSectionResult};
use crate::section::uniform::UniformSection;
use crate::vanilla_chunk_format::Section;
use bitcode_derive::{Decode, Encode};
use deepsize::DeepSizeOf;
use serde_derive::{Deserialize, Serialize};
use temper_core::block_state_id::BlockStateId;
use temper_core::pos::SectionBlockPos;
use temper_macros::block;
Expand All @@ -22,7 +22,7 @@ pub const CHUNK_SECTION_LENGTH: usize = 16 * 16 * 16;

pub(crate) const AIR: BlockStateId = block!("air");

#[derive(Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub(crate) enum ChunkSectionType {
Uniform(UniformSection),
Paletted(PalettedSection),
Expand Down Expand Up @@ -95,7 +95,7 @@ impl ChunkSectionType {
}
}

#[derive(Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub struct ChunkSection {
pub(crate) inner: ChunkSectionType,
pub(crate) light: SectionLightData,
Expand Down
4 changes: 2 additions & 2 deletions src/world/format/src/section/paletted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::BlockStateId;
use crate::palette::{BlockPalette, BlockPaletteResult, PaletteIndex};
use crate::section::uniform::UniformSection;
use crate::section::{AIR, CHUNK_SECTION_LENGTH};
use bitcode_derive::{Decode, Encode};
use deepsize::DeepSizeOf;
use serde_derive::{Deserialize, Serialize};
use std::num::NonZeroU16;
use type_hash::TypeHash;

Expand All @@ -13,7 +13,7 @@ pub enum PalettedSectionResult {
Shrink(BlockStateId),
}

#[derive(Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub struct PalettedSection {
pub(crate) palette: BlockPalette,
pub(crate) block_data: Box<[u64]>,
Expand Down
4 changes: 2 additions & 2 deletions src/world/format/src/section/uniform.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::BlockStateId;
use crate::section::AIR;
use bitcode_derive::{Decode, Encode};
use deepsize::DeepSizeOf;
use serde_derive::{Deserialize, Serialize};
use type_hash::TypeHash;

#[derive(Clone, DeepSizeOf, Encode, Decode, TypeHash)]
#[derive(Clone, DeepSizeOf, Serialize, Deserialize, TypeHash)]
pub struct UniformSection(BlockStateId);

impl UniformSection {
Expand Down
2 changes: 1 addition & 1 deletion src/world/src/benches/edit_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn get_rand_in_range(min: i32, max: i32) -> i32 {
#[expect(clippy::unit_arg)]
pub(crate) fn bench_edits(c: &mut Criterion) {
let chunk_data = include_bytes!("../../../../.etc/raw_chunk.dat");
let chunk: Chunk = bitcode::decode(chunk_data)
let chunk: Chunk = bitcode::deserialize(chunk_data)
.expect("If this fails, go run the dump_chunk test at src/lib/world/src/mod");

let mut read_group = c.benchmark_group("edit_read");
Expand Down
2 changes: 1 addition & 1 deletion src/world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ mod tests {
"Failed to load chunk. If it's a bitcode error, chances are the chunk format \
has changed since last generating a world so you'll need to regenerate",
);
let encoded = bitcode::encode(&*chunk);
let encoded = bitcode::serialize(&*chunk).unwrap();
std::fs::write("../../../.etc/raw_chunk.dat", encoded).unwrap();
}
}