59 lines
1.8 KiB
Rust
59 lines
1.8 KiB
Rust
use anvil_region::AnvilChunkProvider;
|
|
use std::path::PathBuf;
|
|
use crate::backup::uncompress_backup;
|
|
use std::error;
|
|
use std::fs::remove_dir_all;
|
|
|
|
struct ChunkAccess {
|
|
src_path: PathBuf,
|
|
dest_path: PathBuf,
|
|
}
|
|
|
|
impl ChunkAccess {
|
|
pub fn new(world_name: &str, src_path: &PathBuf, dest_path: &PathBuf) -> Result<Self, std::io::Error> {
|
|
let src_path = uncompress_backup(src_path)?.join(world_name).join("region");
|
|
let dest_path= dest_path.join("region");
|
|
|
|
Ok(ChunkAccess {
|
|
src_path,
|
|
dest_path,
|
|
})
|
|
}
|
|
|
|
pub fn copy_chunk(&self, x:i32, z:i32) {
|
|
let src_provider = AnvilChunkProvider::new(self.src_path.to_str().unwrap());
|
|
let dest_provider = AnvilChunkProvider::new(self.dest_path.to_str().unwrap());
|
|
|
|
let chunk = src_provider.load_chunk(x, z).expect("Unable to load chunk");
|
|
dest_provider.save_chunk(x, z, chunk).expect("Unable to save chunk");
|
|
}
|
|
|
|
pub fn cleanup(self) -> Result<(), std::io::Error> {
|
|
remove_dir_all("tmp")
|
|
}
|
|
|
|
}
|
|
|
|
pub fn restore_range_from_backup(world_name: &str, lower_x: i32, upper_x: i32, lower_z: i32, upper_z: i32, backup_path: &PathBuf, minecraft_dir: &PathBuf) -> Result<u64, Box<dyn error::Error>> {
|
|
let chunk_access = ChunkAccess::new(world_name, backup_path, minecraft_dir)?;
|
|
let mut count = 0;
|
|
|
|
for x in lower_x..upper_x {
|
|
for z in lower_z..upper_z {
|
|
chunk_access.copy_chunk(x, z);
|
|
count += 1;
|
|
}
|
|
}
|
|
|
|
chunk_access.cleanup()?;
|
|
Ok(count)
|
|
}
|
|
|
|
pub fn restore_chunk_from_backup(world_name: &str, x: i32, z: i32, backup_path: &PathBuf, minecraft_dir: &PathBuf) -> Result<(), Box<dyn error::Error>> {
|
|
let chunk_access = ChunkAccess::new(world_name, backup_path, minecraft_dir)?;
|
|
chunk_access.copy_chunk(x, z);
|
|
|
|
chunk_access.cleanup()?;
|
|
Ok(())
|
|
}
|