|
@@ -1,11 +1,16 @@
|
|
|
-use std::{fs::File, path::PathBuf, time::Instant};
|
|
|
-
|
|
|
-use binrw::{BinRead, BinWriterExt};
|
|
|
+use std::{
|
|
|
+ fs::File,
|
|
|
+ io::{Cursor, Read, Write},
|
|
|
+ path::PathBuf,
|
|
|
+ time::Instant,
|
|
|
+};
|
|
|
+
|
|
|
+use binrw::{BinRead, BinWrite, BinWriterExt};
|
|
|
use clap::Parser;
|
|
|
use clap_verbosity_flag::{InfoLevel, Verbosity};
|
|
|
use diff::Diff;
|
|
|
use ezcad::file::EzCadHeader;
|
|
|
-use log::{info, trace};
|
|
|
+use log::{info, trace, warn};
|
|
|
|
|
|
use crate::config::{Config, Operations};
|
|
|
|
|
@@ -42,15 +47,36 @@ fn main() {
|
|
|
.filter_level(cli.verbose.log_level_filter())
|
|
|
.init();
|
|
|
|
|
|
+ info!("Reading input file {}", cli.input.to_string_lossy());
|
|
|
let mut input: File = File::open(cli.input).expect("Failed to open input file");
|
|
|
|
|
|
+ // Deserialize to memory buffer for perf
|
|
|
+ let mut input_file: Cursor<Vec<u8>> = Cursor::new(vec![]);
|
|
|
+ input
|
|
|
+ .read_to_end(input_file.get_mut())
|
|
|
+ .expect("Failed to read input file");
|
|
|
+
|
|
|
+ // Deserialize as EZCAD format
|
|
|
+ let time: Instant = Instant::now();
|
|
|
+ let mut file: EzCadHeader =
|
|
|
+ EzCadHeader::read_le(&mut input_file).expect("Failed to parse input file as EZCAD format");
|
|
|
+ trace!("Input file decode time: {:?}", time.elapsed());
|
|
|
+
|
|
|
+ // Re-encode input file and check that it matches original
|
|
|
+ info!("Validating re-encoding of input file");
|
|
|
+ let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
|
|
|
+
|
|
|
let time: Instant = Instant::now();
|
|
|
- let mut input_file: EzCadHeader =
|
|
|
- EzCadHeader::read_le(&mut input).expect("Failed to parse input file as EZCAD format");
|
|
|
- info!("Input decode time: {:?}", time.elapsed());
|
|
|
+ file.write_le(&mut buffer)
|
|
|
+ .expect("Failed to reserialize input file as EZCAD format");
|
|
|
+ trace!("Input file re-encode time: {:?}", time.elapsed());
|
|
|
+
|
|
|
+ if buffer.into_inner().as_slice().ne(input_file.get_ref()) {
|
|
|
+ warn!("Re-encoded input file does not match original file!");
|
|
|
+ }
|
|
|
|
|
|
// Print info on pens with non-default settings
|
|
|
- for pen in input_file
|
|
|
+ for pen in file
|
|
|
.pens_offset
|
|
|
.data
|
|
|
.pens
|
|
@@ -61,12 +87,13 @@ fn main() {
|
|
|
}
|
|
|
|
|
|
// Print all objects
|
|
|
- for layer in input_file.layers_offset.iter() {
|
|
|
+ for layer in file.layers_offset.iter() {
|
|
|
for object in layer.objects.iter() {
|
|
|
trace!("{:#?}", object);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // Process diff
|
|
|
if let Some(diff) = cli.diff {
|
|
|
info!("Processing diff file {}", diff.to_string_lossy());
|
|
|
let mut diff: File = File::open(diff).expect("Failed to open diff file");
|
|
@@ -74,27 +101,47 @@ fn main() {
|
|
|
EzCadHeader::read_le(&mut diff).expect("Failed to parse diff file as EZCAD format");
|
|
|
|
|
|
// Diff pens
|
|
|
- info!("{:#?}", input_file.pens_offset.data.pens.diff(&diff_file.pens_offset.data.pens));
|
|
|
+ info!(
|
|
|
+ "{:#?}",
|
|
|
+ file.pens_offset
|
|
|
+ .data
|
|
|
+ .pens
|
|
|
+ .diff(&diff_file.pens_offset.data.pens)
|
|
|
+ );
|
|
|
|
|
|
// Diff objects
|
|
|
- info!("{:#?}", input_file.layers_offset.value.diff(&diff_file.layers_offset.value));
|
|
|
+ info!(
|
|
|
+ "{:#?}",
|
|
|
+ file.layers_offset
|
|
|
+ .value
|
|
|
+ .diff(&diff_file.layers_offset.value)
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
+ // Process config
|
|
|
if let Some(config) = cli.config {
|
|
|
info!("Processing config file {}", config.to_string_lossy());
|
|
|
let config: String = std::fs::read_to_string(config).expect("Failed to open config file");
|
|
|
let config: Config = serde_yaml::from_str(&config).expect("Failed to parse config file");
|
|
|
|
|
|
// Patch pen settings
|
|
|
- config.ops.apply(&mut input_file.pens_offset.data.pens);
|
|
|
+ config.ops.apply(&mut file.pens_offset.data.pens);
|
|
|
}
|
|
|
|
|
|
+ // Process output
|
|
|
if let Some(output) = cli.output {
|
|
|
- let mut output = File::create(output).expect("Failed to open output file");
|
|
|
+ // Serialize to memory buffer for perf
|
|
|
+ let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
|
|
|
let time: Instant = Instant::now();
|
|
|
+ buffer
|
|
|
+ .write_le(&file)
|
|
|
+ .expect("Failed to serialize contents as EZCAD format");
|
|
|
+ trace!("Output file encode time: {:?}", time.elapsed());
|
|
|
+
|
|
|
+ // Write buffer to output file
|
|
|
+ let mut output = File::create(output).expect("Failed to open output file");
|
|
|
output
|
|
|
- .write_le(&input_file)
|
|
|
- .expect("Failed to serialize contents in EZCAD format");
|
|
|
- info!("Output encode time: {:?}", time.elapsed());
|
|
|
+ .write_all(buffer.into_inner().as_slice())
|
|
|
+ .expect("Failed to write to output file");
|
|
|
}
|
|
|
}
|