comparison examples/dump_ecl.rs @ 719:28e6332b088d

Add a dump_ecl example.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 30 Oct 2019 10:28:52 +0100
parents
children eea03c9ca604
comparison
equal deleted inserted replaced
718:c187e0a6b751 719:28e6332b088d
1 use touhou::th06::ecl::{Ecl, CallMain, CallSub, Rank};
2 use std::env;
3 use std::path::Path;
4
5 #[path = "common.rs"]
6 mod common;
7 use common::{load_file_into_vec};
8
9 fn format_rank(rank: &Rank) -> String {
10 format!("{}{}{}{}", if rank.contains(Rank::EASY) { 'E' } else { ' ' },
11 if rank.contains(Rank::NORMAL) { 'N' } else { ' ' },
12 if rank.contains(Rank::HARD) { 'H' } else { ' ' },
13 if rank.contains(Rank::LUNATIC) { 'L' } else { ' ' })
14 }
15
16 fn print_sub_instruction(call: &CallSub) {
17 let CallSub { time, rank_mask, param_mask: _, instr } = call;
18 println!(" {:>5}: {}: {:?}", time, format_rank(rank_mask), instr);
19 }
20
21 fn print_main_instruction(call: &CallMain) {
22 let CallMain { time, sub, instr } = call;
23 println!(" {:>5}: sub {:>2}: {:?}", time, sub, instr);
24 }
25
26 fn main() {
27 // Parse arguments.
28 let args: Vec<_> = env::args().collect();
29 if args.len() != 2 {
30 eprintln!("Usage: {} <ECL file>", args[0]);
31 return;
32 }
33 let ecl_filename = Path::new(&args[1]);
34
35 // Open the ECL file.
36 let buf = load_file_into_vec(ecl_filename);
37 let (_, ecl) = Ecl::from_slice(&buf).unwrap();
38
39 for (i, main) in ecl.mains.iter().enumerate() {
40 println!("Main {} {{", i);
41 for call in main.instructions.iter() {
42 print_main_instruction(call);
43 }
44 println!("}}");
45 }
46
47 for (i, sub) in ecl.subs.iter().enumerate() {
48 println!("Sub {} {{", i);
49 for call in sub.instructions.iter() {
50 print_sub_instruction(call);
51 }
52 println!("}}");
53 }
54 }