|
| 1 | +use clap::Parser; |
| 2 | +use fang::{ |
| 3 | + mst::{builder::MstBuilder, entry::Entry, Mst}, |
| 4 | + BinReaderExt, |
| 5 | +}; |
| 6 | +use std::{ |
| 7 | + fs::File, |
| 8 | + io::{BufReader, BufWriter}, |
| 9 | + path::Path, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(Parser, Debug)] |
| 13 | +pub struct CombineOpts { |
| 14 | + /// Path to first MST |
| 15 | + #[clap(short = 'i', long)] |
| 16 | + input1_path: String, |
| 17 | + /// Path to second MST |
| 18 | + #[clap(short = 'j', long)] |
| 19 | + input2_path: String, |
| 20 | + /// Path to output MST |
| 21 | + #[clap(short = 'o', long)] |
| 22 | + output_path: Option<String>, |
| 23 | +} |
| 24 | + |
| 25 | +pub fn combine_mst(opts: CombineOpts) -> anyhow::Result<()> { |
| 26 | + // Parse the source Mst from input1 |
| 27 | + let mut in_file = BufReader::new(File::open(&opts.input1_path)?); |
| 28 | + let mst1 = in_file.read_le::<Mst>()?; |
| 29 | + |
| 30 | + // Parse the source Mst from input2 |
| 31 | + let mut in_file = BufReader::new(File::open(&opts.input2_path)?); |
| 32 | + let mst2 = in_file.read_le::<Mst>()?; |
| 33 | + |
| 34 | + // Prepare a new Mst, copying the versions and platform from input1 |
| 35 | + let mut mst_builder = MstBuilder::from_mst_empty(&mst1)?; |
| 36 | + |
| 37 | + // Add all the entries from the first source Mst as references |
| 38 | + for entry in mst1.collect_entries() { |
| 39 | + mst_builder.add_entry_file( |
| 40 | + entry.filename().to_string(), |
| 41 | + opts.input1_path.clone(), |
| 42 | + entry.offset(), |
| 43 | + entry.size(), |
| 44 | + Some(entry.timestamp().timestamp() as u32), |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + // Add all the entries from the second source Mst as references |
| 49 | + for entry in mst2.collect_entries() { |
| 50 | + mst_builder.add_entry_file( |
| 51 | + entry.filename().to_string(), |
| 52 | + opts.input2_path.clone(), |
| 53 | + entry.offset(), |
| 54 | + entry.size(), |
| 55 | + Some(entry.timestamp().timestamp() as u32), |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + // Finalize and write the Mst with context to specified output path or input1_path.combined.mst |
| 60 | + let out_path = match opts.output_path { |
| 61 | + None => Path::new(&opts.input1_path).with_extension("combined.mst"), |
| 62 | + Some(output_path) => Path::new(&output_path).to_path_buf(), |
| 63 | + }; |
| 64 | + let mut out_file = BufWriter::new(File::create(&out_path)?); |
| 65 | + |
| 66 | + mst_builder.write(&mut out_file)?; |
| 67 | + |
| 68 | + Ok(()) |
| 69 | +} |
0 commit comments