use std::fs;
use std::path::Path;
use nucleation::{BlockState, litematic, schematic, UniversalSchematic};
fn litematic_to_schem_conversion(name: &str) {
let input_path_str = format!("tests/samples/{}.litematic", name);
let litematic_path = Path::new(&input_path_str);
assert!(litematic_path.exists(), "Sample .litematic file not found");
let litematic_data = fs::read(litematic_path).expect(format!("Failed to read {}", input_path_str).as_str());
let mut schematic = litematic::from_litematic(&litematic_data).expect("Failed to parse litematic");
let schem_data = schematic::to_schematic(&schematic).expect("Failed to convert to schem");
let output_schem_path = format!("tests/output/{}.schem", name);
let schem_path = Path::new(&output_schem_path);
fs::write(schem_path, &schem_data).expect("Failed to write schem file");
let litematic_data = litematic::to_litematic(&schematic).expect("Failed to convert to litematic");
let output_litematic_path = format!("tests/output/{}.litematic", name);
let litematic_path = Path::new(&output_litematic_path);
fs::write(litematic_path, &litematic_data).expect("Failed to write litematic file");
let read_back_data = fs::read(schem_path).expect("Failed to read back schem file");
let read_back_schematic = schematic::from_schematic(&read_back_data).expect("Failed to parse schem");
assert_eq!(schematic.metadata.name, read_back_schematic.metadata.name);
println!("Successfully converted sample.litematic to .schem format and verified the contents.");
}
fn schema_to_litematic_conversion(name: &str) {
let input_path_str = format!("tests/samples/{}.schem", name);
let schem_path = Path::new(&input_path_str);
assert!(schem_path.exists(), "Sample .schem file not found");
let schem_data = fs::read(schem_path).expect(format!("Failed to read {}", input_path_str).as_str());
let schematic = schematic::from_schematic(&schem_data).expect("Failed to parse schem");
let block_entities = schematic.get_block_entities_as_list();
println!("{:?}", block_entities);
let litematic_data = litematic::to_litematic(&schematic).expect("Failed to convert to litematic");
let output_litematic_path = format!("tests/output/{}.litematic", name);
let litematic_path = Path::new(&output_litematic_path);
fs::write(litematic_path, &litematic_data).expect("Failed to write litematic file");
}
#[test]
fn load_evaluator_schematic(){
let input_path_str = format!("tests/samples/Evaluator.schem");
let schem_path = Path::new(&input_path_str);
let schem_data = fs::read(schem_path).expect(format!("Failed to read {}", input_path_str).as_str());
let schematic = schematic::from_schematic(&schem_data).expect("Failed to parse schem");
let block_entities = schematic.get_block_entities_as_list();
let entities = schematic.get_entities_as_list();
println!("{:?}", block_entities);
println!("{:?}", entities);
}
#[test]
fn test_cube_schematic() {
let input_path_str = format!("tests/samples/test_cube.schem");
let schem_path = Path::new(&input_path_str);
let schem_data = fs::read(schem_path).expect(format!("Failed to read {}", input_path_str).as_str());
let schematic = schematic::from_schematic(&schem_data).expect("Failed to parse schem");
let litematic_data = litematic::to_litematic(&schematic).expect("Failed to convert to litematic");
let output_litematic_path = format!("tests/output/test_cube.litematic");
let litematic_path = Path::new(&output_litematic_path);
fs::write
(litematic_path, &litematic_data).expect("Failed to write litematic file");
}
fn time_load_large_schematic() {
let input_path_str = format!("tests/samples/large_schematic.schem");
let schem_path = Path::new(&input_path_str);
let schem_data = fs::read(schem_path).expect(format!("Failed to read {}", input_path_str).as_str());
let start_time = std::time::Instant::now();
let _schematic = schematic::from_schematic(&schem_data).expect("Failed to parse schem");
let elapsed_time = start_time.elapsed();
println!("Time taken to load large schematic: {:?}", elapsed_time);
}
#[test]
fn test_expand_schematic() {
let mut schematic = UniversalSchematic::new("Default".to_string());
println!("Initial dimensions: {:?}", schematic.get_dimensions());
println!("Initial bbox: {:?}", schematic.get_bounding_box());
let result = schematic.set_block(4, 4, 4, BlockState::new("minecraft:sea_lantern".to_string()));
println!("Block set result: {}", result);
if let Some(region) = schematic.get_region("Main") {
println!("Region position: {:?}", region.position);
println!("Region size: {:?}", region.size);
println!("Region bbox: {:?}", region.get_bounding_box());
}
let block = schematic.get_block(4, 4, 4);
println!("Block at (4,4,4): {:?}", block.map(|b| b.get_name()));
println!("Final dimensions: {:?}", schematic.get_dimensions());
println!("Final bbox: {:?}", schematic.get_bounding_box());
assert_eq!(schematic.get_dimensions(), (1, 1, 1));
}
#[test]
fn test_litematic_to_schem_conversion() {
let test_files = list_test_file("litematic");
for file_name in test_files {
if file_name == "litematic-rose-farm" {
litematic_to_schem_conversion(&file_name);
}
}
println!("Successfully converted litematic-rose-farm.litematic to .schem format and verified the contents.");
}
struct TestFiles<'a> {
extension: &'a str,
reader: fs::ReadDir,
}
impl<'a> Iterator for TestFiles<'a> {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
while let Some(entry) = self.reader.next() {
if let Ok(entry) = entry {
let path = entry.path();
if path.is_file() && path.extension().and_then(|ext| ext.to_str()) == Some(self.extension) {
if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
return Some(stem.to_string());
}
}
}
}
None
}
}
fn list_test_file(extension: &str) -> TestFiles {
const DIR_PATH: &str = "./tests/samples";
TestFiles { extension, reader: fs::read_dir(DIR_PATH).unwrap() }
}