Maraiah/source/leela/main.rs

111 lines
2.7 KiB
Rust
Raw Normal View History

2019-05-30 00:11:05 -07:00
#![allow(clippy::unit_arg)]
2019-03-01 03:22:27 -08:00
use maraiah::{durandal::{err::*, file::*, image::*, sound::*},
2019-04-11 21:04:39 -07:00
marathon::{machdr, map, ppm, shp, snd, tga, wav}};
2019-03-29 11:32:17 -07:00
use std::{fs, io, slice::from_ref};
2019-05-30 00:11:05 -07:00
fn open(path: &str) -> ResultS<memmap::Mmap>
2019-02-10 02:31:57 -08:00
{
2019-05-30 00:11:05 -07:00
let fp = fs::File::open(path)?;
Ok(unsafe {memmap::Mmap::map(&fp)?})
}
2019-05-30 00:11:05 -07:00
fn file_read<T, F>(path: &str, f: F) -> ResultS<T>
where F: FnOnce(&[u8]) -> ResultS<T>
2019-03-13 09:56:39 -07:00
{
2019-05-30 00:11:05 -07:00
let mm = open(path)?;
let bp = &mm[machdr::try_mac_header(&mm)..];
2019-03-13 09:56:39 -07:00
2019-05-30 00:11:05 -07:00
f(bp)
2019-03-13 09:56:39 -07:00
}
2019-05-30 00:11:05 -07:00
fn exists(path: String) -> Result<(), String>
{
2019-05-30 00:11:05 -07:00
match std::fs::metadata(path) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
2019-02-08 20:21:22 -08:00
}
2019-05-30 00:11:05 -07:00
fn each_value<'a, F>(opt: &'a clap::ArgMatches<'a>, name: &str, mut f: F)
2019-04-11 21:04:39 -07:00
-> ResultS<()>
2019-05-30 00:11:05 -07:00
where F: FnMut(&'a str) -> ResultS<()>
2019-02-12 06:41:28 -08:00
{
2019-05-30 00:11:05 -07:00
if let Some(values) = opt.values_of(name) {
for value in values {
f(value)?;
2019-02-12 06:41:28 -08:00
}
}
Ok(())
}
2019-05-30 00:11:05 -07:00
fn dump_info(data: impl std::fmt::Debug)
{
2019-05-30 00:11:05 -07:00
println!("{:#?}", data);
}
2019-05-30 00:11:05 -07:00
fn sub_data_c<'a>(_opt: &clap::ArgMatches<'a>) -> ResultS<()>
2019-02-08 20:21:22 -08:00
{
2019-05-30 00:11:05 -07:00
unimplemented!();
2018-09-06 09:01:52 -07:00
}
2019-05-30 00:11:05 -07:00
fn sub_dump_c<'a>(_opt: &clap::ArgMatches<'a>) -> ResultS<()>
2019-02-17 18:17:35 -08:00
{
2019-05-30 00:11:05 -07:00
unimplemented!();
2019-02-17 18:17:35 -08:00
}
2019-05-30 00:11:05 -07:00
fn sub_info_c<'a>(opt: &clap::ArgMatches<'a>) -> ResultS<()>
2019-02-17 20:04:04 -08:00
{
2019-05-30 00:11:05 -07:00
each_value(opt, "map", |f| Ok(dump_info(file_read(f, map::read)?)))?;
each_value(opt, "shp", |f| Ok(dump_info(file_read(f, shp::read)?)))?;
each_value(opt, "snd", |f| Ok(dump_info(file_read(f, snd::read)?)))?;
2019-02-17 20:04:04 -08:00
Ok(())
}
2019-02-08 20:21:22 -08:00
fn main() -> ResultS<()>
{
2019-05-30 00:11:05 -07:00
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"));
let sub_info =
clap_app!(@subcommand info =>
(about: "Outputs debug info")
(@group files =>
(@attributes +required +multiple)
(@arg map: -m --map [file]... {exists} "Loads a Map file")
(@arg shp: -s --shp [file]... {exists} "Loads a Shapes file")
(@arg snd: -n --snd [file]... {exists} "Loads a Sounds file")));
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!(),
2019-02-08 20:21:22 -08:00
}
Ok(())
}
2018-09-06 09:01:52 -07:00
// EOF