#![allow(clippy::unit_arg)] use maraiah::{err::*, file::*, image::*, sound::*, machdr, map, ppm, shp, snd, tga, wav}; use std::{collections::HashSet, fs, io, slice::from_ref}; fn open(path: &str) -> ResultS { let fp = fs::File::open(path)?; Ok(unsafe {memmap::Mmap::map(&fp)?}) } fn file_read(path: &str, f: F) -> ResultS where F: FnOnce(&[u8]) -> ResultS { let mm = open(path)?; let bp = &mm[machdr::try_mac_header(&mm)..]; f(bp) } fn exists(path: String) -> Result<(), String> { match std::fs::metadata(path) { Ok(_) => Ok(()), Err(e) => Err(e.to_string()), } } fn each_value(opt: &clap::ArgMatches<'_>, name: &str, mut f: F) -> ResultS<()> where F: FnMut(&str) -> ResultS<()> { if let Some(values) = opt.values_of(name) { for value in values { f(value)?; } } Ok(()) } fn dbg_info(data: impl std::fmt::Debug) { println!("{:#?}", data); } fn dump_map(opt: &clap::ArgMatches<'_>, f: &str) -> ResultS<()> { let mut cnks = HashSet::new(); if let Some(opt_cnks) = opt.values_of("chunks") { for typ in opt_cnks { cnks.insert(typ); } } Ok(()) } fn dump_shp(opt: &clap::ArgMatches<'_>, f: &str) -> ResultS<()> { unimplemented!(); } fn dump_snd(opt: &clap::ArgMatches<'_>, f: &str) -> ResultS<()> { unimplemented!(); } fn sub_data_c(_opt: &clap::ArgMatches<'_>) -> ResultS<()> { unimplemented!(); } fn sub_dump_c(opt: &clap::ArgMatches<'_>) -> ResultS<()> { each_value(opt, "map", |f| dump_map(opt, f))?; each_value(opt, "shp", |f| dump_shp(opt, f))?; each_value(opt, "snd", |f| dump_snd(opt, f))?; Ok(()) } fn sub_info_c(opt: &clap::ArgMatches<'_>) -> ResultS<()> { each_value(opt, "map", |f| Ok(dbg_info(file_read(f, map::read)?)))?; each_value(opt, "shp", |f| Ok(dbg_info(file_read(f, shp::read)?)))?; each_value(opt, "snd", |f| Ok(dbg_info(file_read(f, snd::read)?)))?; Ok(()) } fn main() -> ResultS<()> { use clap::clap_app; let sub_data = clap_app!(@subcommand data => (about: "Dumps data into a discrete folder/YAML format")); let sub_dump = clap_app!(@subcommand dump => (about: "Dumps particular parts of data") (@arg chunks: -c --chunks [name]... "Dumps named chunks from an entry") (@group files => (@attributes +required +multiple) (@arg map: -m --map [file]... {exists} "Loads Map files") (@arg shp: -s --shp [file]... {exists} "Loads Shapes files") (@arg snd: -n --snd [file]... {exists} "Loads Sounds files"))); let sub_info = clap_app!(@subcommand info => (about: "Outputs debug info") (@group files => (@attributes +required +multiple) (@arg map: -m --map [file]... {exists} "Loads Map files") (@arg shp: -s --shp [file]... {exists} "Loads Shapes files") (@arg snd: -n --snd [file]... {exists} "Loads Sounds files"))); let opt = clap_app!((env!("CARGO_PKG_NAME")) => (version: env!("CARGO_PKG_VERSION")) (author: env!("CARGO_PKG_AUTHORS")) (about: env!("CARGO_PKG_DESCRIPTION")) (setting: clap::AppSettings::SubcommandRequiredElseHelp) (subcommand: sub_data) (subcommand: sub_dump) (subcommand: sub_info)); let opt = opt.get_matches(); match opt.subcommand() { ("data", Some(opt)) => sub_data_c(opt)?, ("dump", Some(opt)) => sub_dump_c(opt)?, ("info", Some(opt)) => sub_info_c(opt)?, _ => unreachable!(), } Ok(()) } // EOF