|
| 1 | +use std::fs::File; |
| 2 | + |
| 3 | +use clap::Parser; |
| 4 | + |
| 5 | +use libycresources::formats::{frm, pal}; |
| 6 | + |
| 7 | +pub mod render; |
| 8 | + |
| 9 | +#[derive(Parser)] |
| 10 | +#[clap(name = "frameview", version)] |
| 11 | +struct Options { |
| 12 | + /// Path to the input frame file (.frm, .fr0-5) |
| 13 | + #[clap(short, long)] |
| 14 | + input: String, |
| 15 | + #[clap(subcommand)] |
| 16 | + action: Action, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Parser)] |
| 20 | +enum Action { |
| 21 | + /// Prints summary info about contents of the file |
| 22 | + Info, |
| 23 | + /// Renders frame contents into .bmp file(s) in specified directory |
| 24 | + Render(Render), |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Parser)] |
| 28 | +struct Render { |
| 29 | + /// Path to the input palette file (.pal) |
| 30 | + #[clap(short, long)] |
| 31 | + palette: String, |
| 32 | + /// Output directory path |
| 33 | + #[clap(short, long)] |
| 34 | + directory: String, |
| 35 | +} |
| 36 | + |
| 37 | +fn main() { |
| 38 | + let options = Options::parse(); |
| 39 | + |
| 40 | + let file = match File::open(&options.input) { |
| 41 | + Err(error) => { |
| 42 | + eprintln!("Couldn't open input file: {:?}", error); |
| 43 | + return; |
| 44 | + } |
| 45 | + Ok(value) => value, |
| 46 | + }; |
| 47 | + |
| 48 | + let mut reader = std::io::BufReader::with_capacity(1 * 1024 * 1024, file); |
| 49 | + |
| 50 | + let sprite = match frm::parse::sprite(&mut reader) { |
| 51 | + Err(error) => { |
| 52 | + eprintln!("Error occurred: {:?}", error); |
| 53 | + return; |
| 54 | + } |
| 55 | + Ok(value) => value, |
| 56 | + }; |
| 57 | + |
| 58 | + match options.action { |
| 59 | + Action::Info => { |
| 60 | + println!("FPS: {:}.", sprite.fps); |
| 61 | + println!("Frames per orientation: {:}.", sprite.count); |
| 62 | + println!("Unique animations: {:}.", sprite.animations.len()); |
| 63 | + println!("Orientated animations: {:?}.", sprite.orientations); |
| 64 | + |
| 65 | + println!(); |
| 66 | + |
| 67 | + for (index, animation) in sprite.animations.iter().enumerate() { |
| 68 | + println!( |
| 69 | + "Animation {:} has {:} frame(s).", |
| 70 | + index, |
| 71 | + animation.frames.len() |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + Action::Render(arguments) => { |
| 76 | + let output = std::path::Path::new(&arguments.directory); |
| 77 | + |
| 78 | + if !output.exists() { |
| 79 | + eprintln!("Output path does not exist. Aborting."); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if !output.is_dir() { |
| 84 | + eprintln!("Output path is not a directory. Aborting."); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + let directory_name = match std::path::Path::new(&options.input).file_stem() { |
| 89 | + Some(value) => value, |
| 90 | + None => { |
| 91 | + eprintln!("Couldn't determine frame output filename."); |
| 92 | + return; |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + let path = output.join(directory_name); |
| 97 | + |
| 98 | + let mut reader = std::io::BufReader::with_capacity( |
| 99 | + 1 * 1024 * 1024, |
| 100 | + match File::open(arguments.palette) { |
| 101 | + Err(error) => { |
| 102 | + eprintln!("Couldn't open palette file: {:?}", error); |
| 103 | + return; |
| 104 | + } |
| 105 | + Ok(value) => value, |
| 106 | + }, |
| 107 | + ); |
| 108 | + |
| 109 | + let palette = match pal::parse::palette(&mut reader) { |
| 110 | + Err(error) => { |
| 111 | + eprintln!("Error occurred: {:?}", error); |
| 112 | + return; |
| 113 | + } |
| 114 | + Ok(value) => value, |
| 115 | + }; |
| 116 | + |
| 117 | + for (index, animation) in sprite.animations.iter().enumerate() { |
| 118 | + let animation_dir = path.join(index.to_string()); |
| 119 | + |
| 120 | + match std::fs::create_dir_all(&animation_dir) { |
| 121 | + Err(error) => { |
| 122 | + eprintln!( |
| 123 | + "Couldn't create animation output directory tree: {:?}, error: {:}", |
| 124 | + animation_dir, error |
| 125 | + ); |
| 126 | + return; |
| 127 | + } |
| 128 | + Ok(_) => (), |
| 129 | + }; |
| 130 | + |
| 131 | + for (index, frame) in animation.frames.iter().enumerate() { |
| 132 | + let frame_path = animation_dir.join(index.to_string()).with_extension("bmp"); |
| 133 | + |
| 134 | + match render::frame(&frame, &palette) { |
| 135 | + Ok(image) => { |
| 136 | + match image.save(frame_path) { |
| 137 | + Ok(_) => {} |
| 138 | + Err(error) => { |
| 139 | + eprintln!("Couldn't write output file: {:}", error); |
| 140 | + return; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + Err(error) => { |
| 145 | + eprintln!("Couldn't write output file: {:?}", error); |
| 146 | + return; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments