-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add abstraction to support multiple world formats
- Loading branch information
Showing
8 changed files
with
219 additions
and
198 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ version = "0.1.0" | |
edition = "2021" | ||
|
||
[profile.release] | ||
debug = 1 | ||
lto = true | ||
codegen-units = 1 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
use std::{ | ||
fs::OpenOptions, | ||
io::{Read, Seek}, | ||
}; | ||
|
||
use flate2::bufread::{GzDecoder, ZlibDecoder}; | ||
use itertools::Itertools; | ||
|
||
use crate::level::SaveFile; | ||
|
||
use super::{ChunkData, ChunkReader, ChunkReadingError, CompressionError}; | ||
|
||
pub struct AnvilChunkReader {} | ||
|
||
impl Default for AnvilChunkReader { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl AnvilChunkReader { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub enum Compression { | ||
Gzip, | ||
Zlib, | ||
None, | ||
LZ4, | ||
} | ||
|
||
impl Compression { | ||
pub fn from_byte(byte: u8) -> Option<Self> { | ||
match byte { | ||
1 => Some(Self::Gzip), | ||
2 => Some(Self::Zlib), | ||
3 => Some(Self::None), | ||
4 => Some(Self::LZ4), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn decompress_data(&self, compressed_data: Vec<u8>) -> Result<Vec<u8>, CompressionError> { | ||
match self { | ||
Compression::Gzip => { | ||
let mut z = GzDecoder::new(&compressed_data[..]); | ||
let mut chunk_data = Vec::with_capacity(compressed_data.len()); | ||
z.read_to_end(&mut chunk_data) | ||
.map_err(CompressionError::GZipError)?; | ||
Ok(chunk_data) | ||
} | ||
Compression::Zlib => { | ||
let mut z = ZlibDecoder::new(&compressed_data[..]); | ||
let mut chunk_data = Vec::with_capacity(compressed_data.len()); | ||
z.read_to_end(&mut chunk_data) | ||
.map_err(CompressionError::ZlibError)?; | ||
Ok(chunk_data) | ||
} | ||
Compression::None => Ok(compressed_data), | ||
Compression::LZ4 => todo!(), | ||
} | ||
} | ||
} | ||
|
||
impl ChunkReader for AnvilChunkReader { | ||
fn read_chunk( | ||
&self, | ||
save_file: &SaveFile, | ||
at: pumpkin_core::math::vector2::Vector2<i32>, | ||
) -> Result<super::ChunkData, ChunkReadingError> { | ||
let region = ( | ||
((at.x as f32) / 32.0).floor() as i32, | ||
((at.z as f32) / 32.0).floor() as i32, | ||
); | ||
|
||
let mut region_file = OpenOptions::new() | ||
.read(true) | ||
.open( | ||
save_file | ||
.region_folder | ||
.join(format!("r.{}.{}.mca", region.0, region.1)), | ||
) | ||
.map_err(|err| match err.kind() { | ||
std::io::ErrorKind::NotFound => ChunkReadingError::ChunkNotExist, | ||
kind => ChunkReadingError::IoError(kind), | ||
})?; | ||
|
||
let mut location_table: [u8; 4096] = [0; 4096]; | ||
let mut timestamp_table: [u8; 4096] = [0; 4096]; | ||
|
||
// fill the location and timestamp tables | ||
region_file | ||
.read_exact(&mut location_table) | ||
.map_err(|err| ChunkReadingError::IoError(err.kind()))?; | ||
region_file | ||
.read_exact(&mut timestamp_table) | ||
.map_err(|err| ChunkReadingError::IoError(err.kind()))?; | ||
|
||
let modulus = |a: i32, b: i32| ((a % b) + b) % b; | ||
let chunk_x = modulus(at.x, 32) as u32; | ||
let chunk_z = modulus(at.z, 32) as u32; | ||
let table_entry = (chunk_x + chunk_z * 32) * 4; | ||
|
||
let mut offset = vec![0u8]; | ||
offset.extend_from_slice(&location_table[table_entry as usize..table_entry as usize + 3]); | ||
let offset = u32::from_be_bytes(offset.try_into().unwrap()) as u64 * 4096; | ||
let size = location_table[table_entry as usize + 3] as usize * 4096; | ||
|
||
if offset == 0 && size == 0 { | ||
return Err(ChunkReadingError::ChunkNotExist); | ||
} | ||
|
||
// Read the file using the offset and size | ||
let mut file_buf = { | ||
region_file | ||
.seek(std::io::SeekFrom::Start(offset)) | ||
.map_err(|_| ChunkReadingError::RegionIsInvalid)?; | ||
let mut out = vec![0; size]; | ||
region_file | ||
.read_exact(&mut out) | ||
.map_err(|_| ChunkReadingError::RegionIsInvalid)?; | ||
out | ||
}; | ||
|
||
// TODO: check checksum to make sure chunk is not corrupted | ||
let header = file_buf.drain(0..5).collect_vec(); | ||
|
||
let compression = Compression::from_byte(header[4]) | ||
.ok_or_else(|| ChunkReadingError::Compression(CompressionError::UnknownCompression))?; | ||
|
||
let size = u32::from_be_bytes(header[..4].try_into().unwrap()); | ||
|
||
// size includes the compression scheme byte, so we need to subtract 1 | ||
let chunk_data = file_buf.drain(0..size as usize - 1).collect_vec(); | ||
let decompressed_chunk = compression | ||
.decompress_data(chunk_data) | ||
.map_err(ChunkReadingError::Compression)?; | ||
|
||
ChunkData::from_bytes(decompressed_chunk, at).map_err(ChunkReadingError::ParsingError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.