pub struct Region<S> {/* private fields */ }
Expand description
A Minecraft Region, allowing reading and writing of chunk data to a stream (eg aFile). This does not concern itself with manipulating chunk data, users areexpected to usefastnbt
or other deserialization method to manipulate thechunk data itself.
If using a stream withRead
andSeek
you can only read chunk data. Ifyou want to be able to modify chunk data you also need a stream implementingWrite
.File
implements these, as doesCursor<Vec<u8>>
.
Implementations§
Source§impl<S>Region<S>
impl<S>Region<S>
Sourcepub fnfrom_stream(stream: S) ->Result<Self>
pub fnfrom_stream(stream: S) ->Result<Self>
Load a region from an existing stream, meaning something that implementsRead
andSeek
. This will assume a seek of zero is the start ofthe region. This does not load all region data into memory immediately.Chunks are read from the underlying stream when needed.
The most obvious ‘stream’ is a file:
letfile = File::open("foo.mca")?;letmutregion = Region::from_stream(file)?;// manipulate region
If you have raw data, this can also be used by wrapping it in aCursor
:
letdata: Vec<u8> =todo!("get data");letdata = Cursor::new(data);letmutregion = Region::from_stream(data)?;// manipulate region
Sourcepub fnread_chunk(&mut self, x:usize, z:usize) ->Result<Option<Vec<u8>>>
pub fnread_chunk(&mut self, x:usize, z:usize) ->Result<Option<Vec<u8>>>
Read the chunk located at the chunk coordindatesx
,z
. The chunkdata returned is uncompressed NBT.Ok(None)
means that the chunk doesnot exist, which will be the case if that chunk has not generated. Ifx
orz
are outside0..32
,Error::InvalidOffset
is returned.
usefastanvil::CurrentJavaChunk;letfile = File::open("foo.mca")?;letmutregion = Region::from_stream(file)?;// Get a chunk. May error for IO reasons, and may not be present, hence Result<Option>.letchunk = region.read_chunk(1,2).unwrap().unwrap();// Parse chunk data into a CurrentJavaChunk.letchunk: CurrentJavaChunk = fastnbt::from_bytes(&chunk).unwrap();
Sourcepub fninto_inner(self) ->Result<S>
pub fninto_inner(self) ->Result<S>
Return the inner buffer used. The buffer is rewound to the logical endof the region data. If the region does not contain any chunk the position is atthe end of the header. If saving to disk you should truncate the file tothis position to ensure the file is saved with the correct padding bytes.
§Examples
This can be used to truncate a region file after manipulating it to savedisk space.
letfile = File::open("foo.mca")?;letmutregion = Region::from_stream(file)?;// manipulate region// recover the file object in order to make sure the file is the correct// size on disk. The seek head is left at the exact end of the region data,// including required padding.letmutfile = region.into_inner()?;letlen = file.stream_position()?;file.set_len(len)?;
Sourcepub fniter(&mut self) ->RegionIter<'_, S>ⓘ
pub fniter(&mut self) ->RegionIter<'_, S>ⓘ
Create an iterator for the chunks of the region. Chunks not present inthe file are skipped.
Source§impl<S>Region<S>
impl<S>Region<S>
Sourcepub fnnew(stream: S) ->Result<Self>
pub fnnew(stream: S) ->Result<Self>
Create an new empty region.The provided stream will be overwritten, andwill assume a seek to 0 is the start of the region. The stream needsread, write, and seek, like a file provides.
Sourcepub fnwrite_chunk( &mut self, x:usize, z:usize, uncompressed_chunk: &[u8],) ->Result<()>
pub fnwrite_chunk( &mut self, x:usize, z:usize, uncompressed_chunk: &[u8],) ->Result<()>
Write the given uncompressed NBT chunk data to the chunk coordinates x,z.
The chunk data will be compressed with zlib by default. You can usewrite_compressed_chunk if you want more control. Ifx
orz
areoutside0..32
,Error::InvalidOffset
is returned.
Sourcepub fnwrite_compressed_chunk( &mut self, x:usize, z:usize, scheme:CompressionScheme, compressed_chunk: &[u8],) ->Result<()>
pub fnwrite_compressed_chunk( &mut self, x:usize, z:usize, scheme:CompressionScheme, compressed_chunk: &[u8],) ->Result<()>
Low level method to write the given compressed chunk data to the stream.
It is the callers responsibility to make sure the compression schemematches the compression used. Ifx
orz
are outside0..32
,Error::InvalidOffset
is returned.
Sourcepub fnremove_chunk(&mut self, x:usize, z:usize) ->Result<()>
pub fnremove_chunk(&mut self, x:usize, z:usize) ->Result<()>
Remove the chunk at the chunk location with the coordinates x and z.
If you are stripping chunks from regions to save disk space, you shouldinstead iterate through the chunks of the region, and write the desiredchunks to a new region, and write that to disk. This will ensure chunks arecompactly stored with no gaps.