Skip to content

Commit 2576aa0

Browse files
committed
add command to combine 2 msts
1 parent 58bdfe3 commit 2576aa0

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

fang-cli/src/actions/mst/combine.rs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

fang-cli/src/actions/mst/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub use unpack::*;
1212
mod convert;
1313
pub use convert::*;
1414

15+
mod combine;
16+
pub use combine::*;
17+
1518
mod strip;
1619
pub use strip::*;
1720

@@ -31,6 +34,9 @@ pub enum Command {
3134
/// Read the file and write it back as a different version
3235
#[clap(about)]
3336
Convert(ConvertOpts),
37+
/// Combine two files and write them into a new file
38+
#[clap(about)]
39+
Combine(CombineOpts),
3440
/// Read the file and write it back without the content
3541
#[clap(about)]
3642
Strip(StripOpts),
@@ -43,6 +49,7 @@ impl Command {
4349
Command::List(opts) => list::list_mst(opts),
4450
Command::Unpack(opts) => unpack::unpack_mst(opts),
4551
Command::Convert(opts) => convert::convert_mst(opts),
52+
Command::Combine(opts) => combine::combine_mst(opts),
4653
Command::Strip(opts) => strip::strip_mst(opts),
4754
}
4855
}

0 commit comments

Comments
 (0)