Skip to content

Commit

Permalink
dont log if chunk does not exist and tweak help functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kralverde committed Mar 1, 2025
1 parent 1cbaad0 commit 0082888
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
21 changes: 12 additions & 9 deletions pumpkin-world/src/chunk/format/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub const REGION_SIZE: usize = 32;
/// The number of bits that identify two chunks in the same region
pub const SUBREGION_BITS: u8 = pumpkin_util::math::ceil_log2(REGION_SIZE as u32);

pub const SUBREGION_AND: i32 = i32::pow(2, SUBREGION_BITS as u32) - 1;

/// The number of chunks in a region
pub const CHUNK_COUNT: usize = REGION_SIZE * REGION_SIZE;

Expand Down Expand Up @@ -57,7 +59,7 @@ pub enum Compression {

#[derive(Default)]
pub struct AnvilChunkData {
length: u32,
length: usize,
compression: Option<Compression>,
compressed_data: Box<[u8]>,
}
Expand Down Expand Up @@ -187,7 +189,7 @@ impl AnvilChunkData {
.map_err(|_| ChunkReadingError::Compression(CompressionError::UnknownCompression))?;

Ok(AnvilChunkData {
length,
length: length as usize,
compression,
compressed_data: buffer.to_vec().into_boxed_slice(),
})
Expand All @@ -201,7 +203,7 @@ impl AnvilChunkData {

let mut bytes = Vec::with_capacity(padded_size);

bytes.put_u32(self.length);
bytes.put_u32(self.length as u32);
bytes.put_u8(
self.compression
.map_or(Compression::NO_COMPRESSION, |c| c as u8),
Expand All @@ -214,7 +216,7 @@ impl AnvilChunkData {

fn to_chunk(&self, pos: Vector2<i32>) -> Result<ChunkData, ChunkReadingError> {
// -1 for the padding/align of the *compression* byte
let bytes = &self.compressed_data[..self.length as usize - 1];
let bytes = &self.compressed_data[..self.length];

let chunk = if let Some(compression) = self.compression {
let decompress_bytes = compression
Expand All @@ -240,7 +242,7 @@ impl AnvilChunkData {
.map_err(ChunkWritingError::Compression)?;

Ok(AnvilChunkData {
length: compressed_data.len() as u32 + 1,
length: compressed_data.len(),
compression: Some(compression),
compressed_data: compressed_data.into_boxed_slice(),
})
Expand All @@ -253,10 +255,11 @@ impl AnvilChunkFile {
(at.x >> SUBREGION_BITS, at.z >> SUBREGION_BITS)
}

const fn get_chunk_index(pos: &Vector2<i32>) -> usize {
let local_x = (pos.x & 31) as usize;
let local_z = (pos.z & 31) as usize;
(local_z << 5) + local_x
pub const fn get_chunk_index(pos: &Vector2<i32>) -> usize {
let local_x = pos.x & SUBREGION_AND;
let local_z = pos.z & SUBREGION_AND;
let index = (local_z << SUBREGION_BITS) + local_x;
index as usize
}
}

Expand Down
10 changes: 3 additions & 7 deletions pumpkin-world/src/chunk/format/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use pumpkin_util::math::vector2::Vector2;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use zstd::zstd_safe::WriteBuf;

use super::anvil::{CHUNK_COUNT, SUBREGION_BITS, chunk_to_bytes};
use super::anvil::{CHUNK_COUNT, chunk_to_bytes};

/// The signature of the linear file format
/// used as a header and footer described in https://gist.github.com/Aaron2550/5701519671253d4c6190bde6706f9f98
Expand Down Expand Up @@ -135,13 +135,9 @@ impl LinearFileHeader {

impl LinearFile {
const fn get_chunk_index(at: &Vector2<i32>) -> usize {
// we need only the 5 last bits of the x and z coordinates
let decode_x = at.x - ((at.x >> SUBREGION_BITS) << SUBREGION_BITS);
let decode_z = at.z - ((at.z >> SUBREGION_BITS) << SUBREGION_BITS);

// we calculate the index of the chunk in the region file
((decode_z << SUBREGION_BITS) + decode_x) as usize
AnvilChunkFile::get_chunk_index(at)
}

fn check_signature(bytes: &[u8]) -> Result<(), ChunkReadingError> {
if bytes[0..8] != SIGNATURE {
error!("Signature at the start of the file is invalid");
Expand Down
12 changes: 4 additions & 8 deletions pumpkin-world/src/chunk/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,10 @@ impl ChunkData {
.map_err(|e| ChunkParsingError::ErrorDeserializingChunk(e.to_string()))?;

if chunk_data.x_pos != position.x || chunk_data.z_pos != position.z {
log::error!(
"Expected chunk at {}:{}, but got {}:{}",
position.x,
position.z,
chunk_data.x_pos,
chunk_data.z_pos
);
// lets still continue
return Err(ChunkParsingError::ErrorDeserializingChunk(format!(
"Expected data for chunk {},{} but got it for {},{}!",
position.x, position.z, chunk_data.x_pos, chunk_data.z_pos,
)));
}

// this needs to be boxed, otherwise it will cause a stack-overflow
Expand Down
15 changes: 10 additions & 5 deletions pumpkin-world/src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,16 @@ impl Level {
LoadedData::Loaded(chunk) => (chunk.position, Some(chunk)),
LoadedData::Missing(pos) => (pos, None),
LoadedData::Error((position, error)) => {
log::error!(
"Failed to load chunk at {:?}: {} (regenerating)",
position,
error
);
match error {
ChunkReadingError::ChunkNotExist => {}
error => {
log::error!(
"Failed to load chunk at {:?}: {} (regenerating)",
position,
error
);
}
}
(position, None)
}
};
Expand Down

0 comments on commit 0082888

Please sign in to comment.