Mercurial > touhou
annotate formats/src/bin/dump_ecl.rs @ 768:dbe2ddca4a8b
Rust: Bump nom and image to their latest version
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 30 Aug 2022 17:09:04 +0200 |
parents | 21b186be2590 |
children |
rev | line source |
---|---|
757
21b186be2590
Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
756
diff
changeset
|
1 use touhou_formats::th06::ecl::{Ecl, CallMain, CallSub, Rank}; |
719
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; |
757
21b186be2590
Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
756
diff
changeset
|
4 use std::fs::File; |
21b186be2590
Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
756
diff
changeset
|
5 use std::io::{self, BufReader, Read}; |
719
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 |
757
21b186be2590
Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
756
diff
changeset
|
7 pub fn load_file_into_vec<P: AsRef<Path>>(filename: P) -> io::Result<Vec<u8>> { |
21b186be2590
Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
756
diff
changeset
|
8 let file = File::open(filename)?; |
21b186be2590
Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
756
diff
changeset
|
9 let mut file = BufReader::new(file); |
21b186be2590
Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
756
diff
changeset
|
10 let mut buf = Vec::new(); |
21b186be2590
Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
756
diff
changeset
|
11 file.read_to_end(&mut buf)?; |
21b186be2590
Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
756
diff
changeset
|
12 Ok(buf) |
21b186be2590
Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
756
diff
changeset
|
13 } |
719
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 fn format_rank(rank: &Rank) -> String { |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 format!("{}{}{}{}", if rank.contains(Rank::EASY) { 'E' } else { ' ' }, |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 if rank.contains(Rank::NORMAL) { 'N' } else { ' ' }, |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 if rank.contains(Rank::HARD) { 'H' } else { ' ' }, |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 if rank.contains(Rank::LUNATIC) { 'L' } else { ' ' }) |
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 |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 fn print_sub_instruction(call: &CallSub) { |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 let CallSub { time, rank_mask, param_mask: _, instr } = call; |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 println!(" {:>5}: {}: {:?}", time, format_rank(rank_mask), instr); |
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 |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 fn print_main_instruction(call: &CallMain) { |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 let CallMain { time, sub, instr } = call; |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 println!(" {:>5}: sub {:>2}: {:?}", time, sub, instr); |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 } |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 fn main() { |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 // Parse arguments. |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 let args: Vec<_> = env::args().collect(); |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 if args.len() != 2 { |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 eprintln!("Usage: {} <ECL file>", args[0]); |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 return; |
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 let ecl_filename = Path::new(&args[1]); |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 // Open the ECL file. |
756
4d91790cf8ab
Fix dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
727
diff
changeset
|
42 let buf = load_file_into_vec(ecl_filename).unwrap(); |
719
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 let (_, ecl) = Ecl::from_slice(&buf).unwrap(); |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 for (i, main) in ecl.mains.iter().enumerate() { |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 println!("Main {} {{", i); |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 for call in main.instructions.iter() { |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 print_main_instruction(call); |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 } |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 println!("}}"); |
727
eea03c9ca604
dump_ecl: Add newlines between blocks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
719
diff
changeset
|
51 println!(); |
719
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 } |
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 for (i, sub) in ecl.subs.iter().enumerate() { |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 println!("Sub {} {{", i); |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
56 for call in sub.instructions.iter() { |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 print_sub_instruction(call); |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 } |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 println!("}}"); |
727
eea03c9ca604
dump_ecl: Add newlines between blocks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
719
diff
changeset
|
60 println!(); |
719
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 } |
28e6332b088d
Add a dump_ecl example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
62 } |