comparison formats/src/bin/dump_ecl.rs @ 757:21b186be2590

Split the Rust version into multiple crates.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 05 Jan 2021 02:16:32 +0100
parents examples/dump_ecl.rs@4d91790cf8ab
children
comparison
equal deleted inserted replaced
756:4d91790cf8ab 757:21b186be2590
1 use touhou_formats::th06::ecl::{Ecl, CallMain, CallSub, Rank};
2 use std::env;
3 use std::path::Path;
4 use std::fs::File;
5 use std::io::{self, BufReader, Read};
6
7 pub fn load_file_into_vec<P: AsRef<Path>>(filename: P) -> io::Result<Vec<u8>> {
8 let file = File::open(filename)?;
9 let mut file = BufReader::new(file);
10 let mut buf = Vec::new();
11 file.read_to_end(&mut buf)?;
12 Ok(buf)
13 }
14
15 fn format_rank(rank: &Rank) -> String {
16 format!("{}{}{}{}", if rank.contains(Rank::EASY) { 'E' } else { ' ' },
17 if rank.contains(Rank::NORMAL) { 'N' } else { ' ' },
18 if rank.contains(Rank::HARD) { 'H' } else { ' ' },
19 if rank.contains(Rank::LUNATIC) { 'L' } else { ' ' })
20 }
21
22 fn print_sub_instruction(call: &CallSub) {
23 let CallSub { time, rank_mask, param_mask: _, instr } = call;
24 println!(" {:>5}: {}: {:?}", time, format_rank(rank_mask), instr);
25 }
26
27 fn print_main_instruction(call: &CallMain) {
28 let CallMain { time, sub, instr } = call;
29 println!(" {:>5}: sub {:>2}: {:?}", time, sub, instr);
30 }
31
32 fn main() {
33 // Parse arguments.
34 let args: Vec<_> = env::args().collect();
35 if args.len() != 2 {
36 eprintln!("Usage: {} <ECL file>", args[0]);
37 return;
38 }
39 let ecl_filename = Path::new(&args[1]);
40
41 // Open the ECL file.
42 let buf = load_file_into_vec(ecl_filename).unwrap();
43 let (_, ecl) = Ecl::from_slice(&buf).unwrap();
44
45 for (i, main) in ecl.mains.iter().enumerate() {
46 println!("Main {} {{", i);
47 for call in main.instructions.iter() {
48 print_main_instruction(call);
49 }
50 println!("}}");
51 println!();
52 }
53
54 for (i, sub) in ecl.subs.iter().enumerate() {
55 println!("Sub {} {{", i);
56 for call in sub.instructions.iter() {
57 print_sub_instruction(call);
58 }
59 println!("}}");
60 println!();
61 }
62 }