view examples/dump_ecl.rs @ 725:fbf89cba04e2

ecl_vm: some work on spellcard 11
author Gauvain "GovanifY" Roussel-Tarbouriech <gauvain@govanify.com>
date Tue, 29 Oct 2019 15:41:23 +0100
parents 28e6332b088d
children eea03c9ca604
line wrap: on
line source

use touhou::th06::ecl::{Ecl, CallMain, CallSub, Rank};
use std::env;
use std::path::Path;

#[path = "common.rs"]
mod common;
use common::{load_file_into_vec};

fn format_rank(rank: &Rank) -> String {
    format!("{}{}{}{}", if rank.contains(Rank::EASY) { 'E' } else { ' ' },
                        if rank.contains(Rank::NORMAL) { 'N' } else { ' ' },
                        if rank.contains(Rank::HARD) { 'H' } else { ' ' },
                        if rank.contains(Rank::LUNATIC) { 'L' } else { ' ' })
}

fn print_sub_instruction(call: &CallSub) {
    let CallSub { time, rank_mask, param_mask: _, instr } = call;
    println!("    {:>5}: {}: {:?}", time, format_rank(rank_mask), instr);
}

fn print_main_instruction(call: &CallMain) {
    let CallMain { time, sub, instr } = call;
    println!("    {:>5}: sub {:>2}: {:?}", time, sub, instr);
}

fn main() {
    // Parse arguments.
    let args: Vec<_> = env::args().collect();
    if args.len() != 2 {
        eprintln!("Usage: {} <ECL file>", args[0]);
        return;
    }
    let ecl_filename = Path::new(&args[1]);

    // Open the ECL file.
    let buf = load_file_into_vec(ecl_filename);
    let (_, ecl) = Ecl::from_slice(&buf).unwrap();

    for (i, main) in ecl.mains.iter().enumerate() {
        println!("Main {} {{", i);
        for call in main.instructions.iter() {
            print_main_instruction(call);
        }
        println!("}}");
    }

    for (i, sub) in ecl.subs.iter().enumerate() {
        println!("Sub {} {{", i);
        for call in sub.instructions.iter() {
            print_sub_instruction(call);
        }
        println!("}}");
    }
}