annotate formats/src/bin/dump_dat.rs @ 769:cae5f15ca5ed

formats: Fix possible panic in PBG3 checksum check
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 30 Aug 2022 18:22:53 +0200
parents ccb04468c5fa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
759
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 use touhou_formats::th06::pbg3::PBG3;
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use std::env;
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use std::path::Path;
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use std::fs::{File, create_dir_all};
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 use std::io::{self, BufReader, Write};
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 fn main() -> io::Result<()> {
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 // Parse arguments.
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 let args: Vec<_> = env::args().collect();
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 if args.len() != 3 {
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 eprintln!("Usage: {} <DAT file> <output dir>", args[0]);
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 std::process::exit(1);
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 }
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 let filename = Path::new(&args[1]);
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 let output_filename = Path::new(&args[2]);
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 let file = File::open(filename)?;
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 let file = BufReader::new(file);
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 let mut pbg3 = PBG3::from_file(file)?;
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 let list = pbg3.list_files().cloned().collect::<Vec<_>>();
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 create_dir_all(output_filename)?;
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 for filename in list {
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 let data = pbg3.get_file(&filename, true)?;
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 let mut output = File::create(output_filename.join(filename))?;
767
ccb04468c5fa formats: Use Read::read_exact() and Write::write_all()
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 759
diff changeset
27 output.write_all(&data)?;
759
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 }
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 Ok(())
abcb586566e6 formats: Add a dump_dat binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 }