Maraiah/src/main.rs

97 lines
2.4 KiB
Rust
Raw Normal View History

use maraiah::{durandal::{bin::*, chunk::*, err::*, image::*, text::*},
marathon::{machdr, map, pict, shp, term, wad}};
2019-02-08 21:53:27 -08:00
use std::{env, fs,
io::{self, Write}};
2019-02-08 20:21:22 -08:00
fn read_chunk(cid: &Ident, cnk: &[u8], eid: u16) -> ResultS<()>
2018-09-06 09:01:52 -07:00
{
2019-02-08 20:21:22 -08:00
match cid {
2019-02-08 21:53:27 -08:00
b"PICT" => {
let im = pict::load_pict(cnk)?;
println!("entry {} has PICT {}x{}", eid, im.w(), im.h());
let out = fs::File::create(&format!("out/{}.ppm", eid))?;
let mut out = io::BufWriter::new(out);
write_ppm(&mut out, &im)?;
2019-02-08 21:53:27 -08:00
}
b"Minf" => {
let minf = map::Minf::chunk(cnk)?;
println!("entry {} has {:#?}", eid, minf);
}
b"EPNT" => {
let epnt = map::Endpoint::chunk(cnk)?;
println!("entry {} has EPNT {:#?}", eid, epnt);
}
b"PNTS" => {
let epnt = map::Point::chunk(cnk)?;
println!("entry {} has PNTS {:#?}", eid, epnt);
}
b"LINS" => {
let line = map::Line::chunk(cnk)?;
println!("entry {} has LINS {:#?}", eid, line);
}
b"SIDS" => {
let line = map::Side::chunk(cnk)?;
println!("entry {} has SIDS {:#?}", eid, line);
}
b"term" => {
let term = term::Terminal::chunk(cnk)?;
println!("entry {} has term {:#?}", eid, term);
}
cid => {
let fname = format!("out/{:04}{}.bin", eid, mac_roman_conv(cid));
let out = fs::File::create(&fname)?;
let mut out = io::BufWriter::new(out);
out.write(cnk)?;
}
2019-02-08 20:21:22 -08:00
}
Ok(())
}
fn process_wad(b: &[u8]) -> ResultS<()>
{
let wad = wad::Wad::new(b)?;
2018-09-09 15:21:31 -07:00
2018-09-11 01:21:36 -07:00
println!("{:#?}", wad);
2018-09-09 15:21:31 -07:00
2019-02-08 20:21:22 -08:00
for (eid, ent) in wad.entries {
for (cid, cnk) in ent.chunks {
read_chunk(&cid, cnk, eid)?;
2018-09-09 15:21:31 -07:00
}
2019-02-08 20:21:22 -08:00
}
2019-02-08 20:21:22 -08:00
Ok(())
}
2019-02-08 20:21:22 -08:00
fn process_shp(b: &[u8]) -> ResultS<()>
{
2019-02-09 11:02:23 -08:00
shp::testfn_replaceme(b)
2018-09-06 09:01:52 -07:00
}
2019-02-08 20:21:22 -08:00
fn main() -> ResultS<()>
{
use memmap::Mmap;
for arg in env::args().skip(1) {
let (typ, fna) = if let Some(st) = arg.find(':') {
arg.split_at(st + 1)
} else {
("wad:", arg.as_str())
};
let fp = fs::File::open(fna)?;
2019-02-08 21:53:27 -08:00
let mm = unsafe {Mmap::map(&fp)?};
2019-02-08 20:21:22 -08:00
let b = &mm[machdr::try_mac_header(&mm)..];
match typ {
2019-02-08 21:53:27 -08:00
"wad:" => process_wad(b),
"shp:" => process_shp(b),
_ => Err(err_msg("invalid file type specified on commandline")),
2019-02-08 20:21:22 -08:00
}?;
}
Ok(())
}
2018-09-06 09:01:52 -07:00
// EOF