mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-05 01:24:53 +01:00
main: iterate over regions of a Minecraft save dir
This commit is contained in:
parent
f3a6711458
commit
2d0f2fb865
1 changed files with 68 additions and 12 deletions
80
src/main.rs
80
src/main.rs
|
@ -1,23 +1,42 @@
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::{Context, Result};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
use minedmap::{resource, world};
|
use minedmap::{resource, world};
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
struct Args {
|
struct Args {
|
||||||
/// Filename to dump
|
/// Minecraft save directory
|
||||||
file: PathBuf,
|
savedir: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
/// Type with methods for processing the regions of a Minecraft save directory
|
||||||
let args = Args::parse();
|
struct RegionProcessor {
|
||||||
|
block_types: resource::BlockTypeMap,
|
||||||
|
}
|
||||||
|
|
||||||
let block_types = resource::block_types();
|
impl RegionProcessor {
|
||||||
|
fn new() -> Self {
|
||||||
|
RegionProcessor {
|
||||||
|
block_types: resource::block_types(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
minedmap::io::region::from_file(args.file.as_path())?.foreach_chunk(
|
/// Parses a filename in the format r.X.Z.mca into the contained X and Z values
|
||||||
|coords, data: world::de::Chunk| {
|
fn parse_region_filename(path: &Path) -> Option<(i32, i32)> {
|
||||||
|
let file_name = path.file_name()?.to_str()?;
|
||||||
|
let parts: Vec<_> = file_name.split('.').collect();
|
||||||
|
let &["r", x, z, "mca"] = parts.as_slice() else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
Some((x.parse().ok()?, z.parse().ok()?))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Processes a single region file
|
||||||
|
fn process_region(&self, path: &Path, _x: i32, _z: i32) -> Result<()> {
|
||||||
|
minedmap::io::region::from_file(path)?.foreach_chunk(|coords, data: world::de::Chunk| {
|
||||||
let chunk = match world::chunk::Chunk::new(&data) {
|
let chunk = match world::chunk::Chunk::new(&data) {
|
||||||
Ok(chunk) => chunk,
|
Ok(chunk) => chunk,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -26,10 +45,47 @@ fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match world::layer::top_layer(&chunk, &block_types) {
|
match world::layer::top_layer(&chunk, &self.block_types) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(err) => println!("{:?}", err),
|
Err(err) => println!("{:?}", err),
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
)
|
}
|
||||||
|
|
||||||
|
/// Iterates over all region files of a Minecraft save directory
|
||||||
|
fn process_region_dir(&self, regiondir: &Path) -> Result<()> {
|
||||||
|
let read_dir = regiondir
|
||||||
|
.read_dir()
|
||||||
|
.with_context(|| format!("Failed to read directory {}", regiondir.display()))?;
|
||||||
|
|
||||||
|
for entry in read_dir.filter_map(|entry| entry.ok()).filter(|entry| {
|
||||||
|
// We are only interested in regular files
|
||||||
|
entry
|
||||||
|
.file_type()
|
||||||
|
.map(|file_type| file_type.is_file())
|
||||||
|
.unwrap_or_default()
|
||||||
|
}) {
|
||||||
|
let path = entry.path();
|
||||||
|
let Some((x, z)) = Self::parse_region_filename(&path) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(err) = self.process_region(&path, x, z) {
|
||||||
|
eprintln!("Failed to process region r.{}.{}.mca: {}", x, z, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let args = Args::parse();
|
||||||
|
|
||||||
|
let regiondir: PathBuf = [&args.savedir, Path::new("region")].iter().collect();
|
||||||
|
|
||||||
|
let region_processor = RegionProcessor::new();
|
||||||
|
region_processor.process_region_dir(®iondir)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue