Skip to main content

RegionManager

Struct RegionManager 

Source
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: PathBuf

Base directory for region files (e.g., “world/region”).

§regions: AsyncRwLock<FxHashMap<RegionPos, RegionHandle>>

Open region file handles with their headers.

Implementations§

Source§

impl RegionManager

Source

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.
Source

fn region_path(&self, pos: RegionPos) -> PathBuf

Gets the file path for a region.

Source

async fn open_region(&self, pos: RegionPos) -> Result<RegionHandle>

Opens or creates a region file, loading only the header.

Source

async fn create_region(&self, pos: RegionPos) -> Result<RegionHandle>

Creates a new empty region file.

Source

async fn write_header(file: &mut File, header: &RegionHeader) -> Result<()>

Writes the header to disk.

Source

async fn read_chunk_data( file: &mut File, sector_offset: u32, size: u32, ) -> Result<Vec<u8>>

Reads a chunk’s compressed data from disk.

Source

fn validate_chunk_entry(entry: ChunkEntry, file_sectors: u32) -> Result<()>

Source

fn validate_region_entries( header: &RegionHeader, file_sectors: u32, ) -> Result<()>

Source

async fn clear_corrupt_chunk_if_unchanged( &self, region_pos: RegionPos, index: usize, expected_entry: ChunkEntry, ) -> Result<bool>

Source

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.

Source

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.

Source

fn encode_chunk(prepared: PreparedChunkSave) -> Result<Vec<u8>>

Source

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 position
  • min_y - The minimum Y coordinate of the world
  • height - The total height of the world
  • level - Weak reference to the world for Full chunk runtime access

The region must already be acquired via acquire_chunk before calling this.

Source

fn decode_chunk( compressed: Vec<u8>, pos: ChunkPos, status: ChunkStatus, min_y: i32, height: i32, level: Weak<World>, ) -> Result<LoadedChunk, CorruptChunkData>

Source

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.

Source

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.

Source

pub async fn chunk_exists(&self, pos: ChunkPos) -> Result<bool>

Checks if a chunk exists on disk without loading it.

Source

pub async fn flush_all(&self) -> Result<()>

Flushes all dirty headers to disk.

Source

pub async fn close_all(&self) -> Result<()>

Flushes all dirty headers and closes all region file handles.

This should be called during graceful shutdown after all chunks have been saved. It ensures all data is persisted and file handles are properly closed.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoShared for T

Source§

fn into_shared(self) -> Arc<Mutex<RawMutex, Self>>

Wraps this value in an Arc<SyncMutex<>>
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more