pub struct RegionManager {
base_path: PathBuf,
regions: AsyncRwLock<FxHashMap<RegionPos, RegionHandle>>,
}Expand description
Manages region files with seek-based chunk access.
Only keeps region headers (8KB each) in memory, not chunk data. Chunks are loaded on-demand and converted directly to runtime format.
Fields§
§base_path: PathBufBase directory for region files (e.g., “world/region”).
regions: AsyncRwLock<FxHashMap<RegionPos, RegionHandle>>Open region file handles with their headers.
Implementations§
Source§impl RegionManager
impl RegionManager
Sourcepub fn new(base_path: impl Into<PathBuf>) -> Self
pub fn new(base_path: impl Into<PathBuf>) -> Self
Creates a new region manager.
§Arguments
base_path- Directory where region files are stored.registry- The registry for block state and biome conversions.
Sourcefn region_path(&self, pos: RegionPos) -> PathBuf
fn region_path(&self, pos: RegionPos) -> PathBuf
Gets the file path for a region.
Sourceasync fn open_region(&self, pos: RegionPos) -> Result<RegionHandle>
async fn open_region(&self, pos: RegionPos) -> Result<RegionHandle>
Opens or creates a region file, loading only the header.
Sourceasync fn create_region(&self, pos: RegionPos) -> Result<RegionHandle>
async fn create_region(&self, pos: RegionPos) -> Result<RegionHandle>
Creates a new empty region file.
Sourceasync fn write_header(file: &mut File, header: &RegionHeader) -> Result<()>
async fn write_header(file: &mut File, header: &RegionHeader) -> Result<()>
Writes the header to disk.
Sourceasync fn read_chunk_data(
file: &mut File,
sector_offset: u32,
size: u32,
) -> Result<Vec<u8>>
async fn read_chunk_data( file: &mut File, sector_offset: u32, size: u32, ) -> Result<Vec<u8>>
Reads a chunk’s compressed data from disk.
fn validate_chunk_entry(entry: ChunkEntry, file_sectors: u32) -> Result<()>
fn validate_region_entries( header: &RegionHeader, file_sectors: u32, ) -> Result<()>
async fn clear_corrupt_chunk_if_unchanged( &self, region_pos: RegionPos, index: usize, expected_entry: ChunkEntry, ) -> Result<bool>
Sourceasync fn write_chunk_data(
file: &mut File,
sector_offset: u32,
data: &[u8],
file_sectors: &mut u32,
) -> Result<()>
async fn write_chunk_data( file: &mut File, sector_offset: u32, data: &[u8], file_sectors: &mut u32, ) -> Result<()>
Writes chunk data to disk at the specified sector offset.
Sourcepub async fn save_chunk_data(
&self,
prepared: PreparedChunkSave,
thread_pool: &ThreadPool,
) -> Result<bool>
pub async fn save_chunk_data( &self, prepared: PreparedChunkSave, thread_pool: &ThreadPool, ) -> Result<bool>
Saves prepared chunk data to disk after the snapshot-preparation phase has ended.
fn encode_chunk(prepared: PreparedChunkSave) -> Result<Vec<u8>>
Sourcepub async fn load_chunk(
&self,
pos: ChunkPos,
min_y: i32,
height: i32,
level: Weak<World>,
thread_pool: &ThreadPool,
) -> Result<Option<LoadedChunk>>
pub async fn load_chunk( &self, pos: ChunkPos, min_y: i32, height: i32, level: Weak<World>, thread_pool: &ThreadPool, ) -> Result<Option<LoadedChunk>>
Loads a chunk from the appropriate region.
Automatically opens the region if not already open. The region’s reference
count is incremented, so you must call release_chunk when done with the chunk.
Returns Ok(None) if the chunk doesn’t exist on disk.
§Arguments
pos- The chunk positionmin_y- The minimum Y coordinate of the worldheight- The total height of the worldlevel- Weak reference to the world for Full chunk runtime access
The region must already be acquired via acquire_chunk before calling this.
fn decode_chunk( compressed: Vec<u8>, pos: ChunkPos, status: ChunkStatus, min_y: i32, height: i32, level: Weak<World>, ) -> Result<LoadedChunk, CorruptChunkData>
Sourcepub async fn acquire_chunk(&self, pos: ChunkPos) -> Result<bool>
pub async fn acquire_chunk(&self, pos: ChunkPos) -> Result<bool>
Acquires a chunk, incrementing the region’s reference count.
This opens or creates the region file. Call this before loading or
generating a chunk, and call release_chunk when done with the chunk.
Returns Ok(true) if the chunk exists on disk, Ok(false) if it doesn’t.
Sourcepub async fn release_chunk(&self, pos: ChunkPos) -> Result<()>
pub async fn release_chunk(&self, pos: ChunkPos) -> Result<()>
Releases a loaded chunk, decrementing the region’s reference count.
When all chunks from a region are released, the header is saved (if dirty) and the file handle is closed.
This must be called for each chunk returned by load_chunk.
Sourcepub async fn chunk_exists(&self, pos: ChunkPos) -> Result<bool>
pub async fn chunk_exists(&self, pos: ChunkPos) -> Result<bool>
Checks if a chunk exists on disk without loading it.
Auto Trait Implementations§
impl !Freeze for RegionManager
impl !RefUnwindSafe for RegionManager
impl Send for RegionManager
impl Sync for RegionManager
impl Unpin for RegionManager
impl UnsafeUnpin for RegionManager
impl UnwindSafe for RegionManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreArc<SyncMutex<>>