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