Struct File

Source
pub struct File { /* private fields */ }
Available on crate features je and je-region only.
Expand description

An object for handling region files with any NBT content, works with both .mcr and .mca files.

Allows for random access reading to any chunk in the file using serde::Deserialize implementations and writing with serde::Serialize implementations.

§Examples

Copying the entities from one chunk to another:

use flate2::{read::ZlibDecoder, write::ZlibEncoder};
 
let file = OpenOptions::new()
   .read(true)
   .write(true)
   .open("test/r.0.0.mca")?;
let mut region = RegionFile::new(file)?;

struct CompHandler {}
impl CompressionHandler for CompHandler {
  fn decompress<'r, R: Read + 'r>(read: R, c: Compression) -> Box<dyn Read + 'r> {
       match c {
           Compression::Zlib => Box::new(ZlibDecoder::new(read)),
           _ => panic!("Should of been zlib compressed")
       }
   }
 
   fn compress<'w, W: Write + 'w>(write: W) -> (Box<dyn Write + 'w>, Compression) {
       (
           Box::new(ZlibEncoder::new(write, flate2::Compression::default())),
           Compression::Zlib
       )
   }
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all="PascalCase")]
#[serde(deny_unknown_fields)]
struct EntityChunk {
   data_version: i32,
   #[serde(with="nbt::int_array")]
   position: [i32; 2],
   entities: Vec<nbt::Compound>
}
match region.read_chunk::<EntityChunk, CompHandler>(0, 0)? {
   Some(chunk) => {
       let mut new_chunk = chunk.clone();
       new_chunk.position = [1, 0];
       let mut i = 0;
       for entity in &mut new_chunk.entities {
           match entity.get_mut("Pos") {
               Some(tag) => {
                   match tag {
                       nbt::Tag::List(nbt::List::Double(pos)) => {
                           pos[0] += 16.0;
                       }
                       _ => {}
                   }
               },
               None => {}
           }

           match entity.get_mut("UUID") {
               Some(uuid) => {
                   *uuid = nbt::Tag::IntArray(vec![0, 0, 0, i]);
               },
               None => {}
           }
           i += 1;
       }

       region.write_chunk::<EntityChunk, CompHandler>(1, 0, &new_chunk, "")?;
   },
   None => println!("No chunk data")
}

Implementations§

Source§

impl File

Source

pub fn new(file: File) -> Result<Self>

Creates a new region File object from rust’s std::fs::File object

Makes the underlying rust file a multiple of 4KiB as per specification

Source

pub fn dealloc_chunk(&mut self, chunk: u16) -> Result<()>

Deallocates the chunk with id chunk

This method only frees up the space in the file taken up by the chunk to be used by future chunk writes that fit in the space, it doesn’t remove the data.

Source

pub fn read_chunk<'de, T: Deserialize<'de>, C: CompressionHandler>( &mut self, chunk: u16, ) -> Result<Option<T>>

Attempts to read the chunk with table id chunk as T, C turns the compressed data from the file and turns it into decompressed data depending on the compression type passed in.

If there is no data for the chunk it returns None, if it fails to deserialize into T it returns an Err.

Source

pub fn write_chunk<T: Serialize, C: CompressionHandler>( &mut self, chunk: u16, v: &T, name: &'static str, ) -> Result<()>

Writes the chunk data v to the chunk with id chunk with the root name name, C turns the uncompressed data into compressed data before writing it to the file.

Source

pub fn flush(&mut self) -> Result<()>

Makes sure any internal state of the region file is written

Source

pub fn chunks(&self) -> &[u16]

Returns a slice of chunk ids that have chunk data

Source

pub fn sync_all(&mut self) -> Result<()>

Runs File::sync_all on the underlying file

Can be used to block code execution until all io operations on the file are finished

Source

pub fn normalize_size(&mut self) -> Result<u64>

Ensures that the file is a multiple of 4KiB as per specification.

Returns the new file size in bytes.

Auto Trait Implementations§

§

impl Freeze for File

§

impl RefUnwindSafe for File

§

impl Send for File

§

impl Sync for File

§

impl Unpin for File

§

impl UnwindSafe for File

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.

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