diff --git a/Cargo.lock b/Cargo.lock index 7e9ffa5..37eff11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1111,6 +1111,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_arrays" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38636132857f68ec3d5f3eb121166d2af33cb55174c4d5ff645db6165cbef0fd" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.189" @@ -1191,6 +1200,7 @@ dependencies = [ "parquet", "rand", "serde", + "serde_arrays", "serde_json", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index fd7b310..8f4af9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,6 @@ clap = { version = "4.4.5", features = ["derive"] } parquet = "47.0.0" rand = "0.8.5" serde = { version = "1.0.189", features = ["derive"] } +serde_arrays = "0.1.0" serde_json = "1.0.107" tokio = { version = "1.32.0", features = ["macros", "rt-multi-thread"] } diff --git a/src/storage/disk_storage.rs b/src/storage/disk_storage.rs index c858265..95c6449 100644 --- a/src/storage/disk_storage.rs +++ b/src/storage/disk_storage.rs @@ -61,6 +61,7 @@ impl ChunkStorageCache { &self.cached_chunk_files[last_used_index] } + /// `fetch_chunk_by_pos` takes in the position of a chunk, and returns the /// data of the chunk from disk /// diff --git a/src/storage/world.rs b/src/storage/world.rs index 232d3c7..a2c8dd4 100644 --- a/src/storage/world.rs +++ b/src/storage/world.rs @@ -1,5 +1,6 @@ use core::fmt; use serde::ser; +use serde::ser::{SerializeSeq, SerializeStruct, Serializer}; use serde::Serialize; use std::{ cmp::{max, min}, @@ -31,12 +32,26 @@ impl ChunkPos { } } -#[derive(Debug, Serialize)] +#[derive(Debug)] pub struct ChunkData { pub pos: ChunkPos, pub sections: [ChunkSection; SECTIONS_PER_CHUNK], } +impl Serialize for ChunkData { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let mut seq = serializer.serialize_seq(Some(self.sections.len()))?; + + for section in self.sections { + seq.serialize_element(§ion)?; + } + seq.end() + } +} + impl ChunkData { pub fn new(pos: &ChunkPos) -> Self { ChunkData { @@ -59,7 +74,7 @@ impl ChunkData { } // https://wiki.vg/Chunk_Format -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Serialize)] pub struct ChunkSection { /// The number of non-empty blocks in the section. If completely full, the /// section contains a 16 x 16 x 16 cube of blocks = 4096 blocks @@ -68,6 +83,7 @@ pub struct ChunkSection { /// The data for all the blocks in the chunk /// The representation for this may be different based on the number of /// non-empty blocks + #[serde(with = "serde_arrays")] block_states: [BlockID; 16 * 16 * 16], }