Maraiah/src/main.rs

143 lines
3.7 KiB
Rust
Raw Normal View History

use maraiah::{durandal::{bin::*, chunk::*, err::*, image::*, text::*},
marathon::{machdr, map, pict, shp, term, wad}};
2019-02-12 09:53:25 -08:00
use std::{fs,
2019-02-08 21:53:27 -08:00
io::{self, Write}};
2019-02-10 02:31:57 -08:00
fn write_chunk(cid: &Ident, cnk: &[u8], eid: u16) -> ResultS<()>
{
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)?;
Ok(())
}
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)?;
2019-02-10 02:31:57 -08:00
eprintln!("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)?;
2019-02-10 02:31:57 -08:00
eprintln!("entry {} has {:#?}", eid, minf);
2019-02-08 21:53:27 -08:00
}
b"EPNT" => {
let epnt = map::Endpoint::chunk(cnk)?;
2019-02-10 02:31:57 -08:00
eprintln!("entry {} has EPNT {:#?}", eid, epnt);
2019-02-08 21:53:27 -08:00
}
b"PNTS" => {
let epnt = map::Point::chunk(cnk)?;
2019-02-10 02:31:57 -08:00
eprintln!("entry {} has PNTS {:#?}", eid, epnt);
2019-02-08 21:53:27 -08:00
}
b"LINS" => {
let line = map::Line::chunk(cnk)?;
2019-02-10 02:31:57 -08:00
eprintln!("entry {} has LINS {:#?}", eid, line);
2019-02-08 21:53:27 -08:00
}
b"SIDS" => {
let line = map::Side::chunk(cnk)?;
2019-02-10 02:31:57 -08:00
eprintln!("entry {} has SIDS {:#?}", eid, line);
2019-02-08 21:53:27 -08:00
}
b"term" => {
let term = term::Terminal::chunk(cnk)?;
2019-02-10 02:31:57 -08:00
eprintln!("entry {} has term {:#?}", eid, term);
2019-02-08 21:53:27 -08:00
}
cid => {
2019-02-10 02:31:57 -08:00
write_chunk(cid, cnk, eid)?;
2019-02-08 21:53:27 -08:00
}
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
2019-02-10 02:31:57 -08:00
eprintln!("{:#?}", 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-12 06:41:28 -08:00
fn dump_bitmaps(c: &shp::Collection, i: usize) -> ResultS<()>
{
for (j, bmp) in c.bmps.iter().enumerate() {
for (k, tab) in c.tabs.iter().enumerate() {
let fname = format!("out/shape{}_{}_{}.tga", i, j, k);
let out = fs::File::create(&fname)?;
let mut out = io::BufWriter::new(out);
write_tga(&mut out, &shp::ImageShp::new(bmp, &tab))?;
}
}
Ok(())
}
2019-02-08 20:21:22 -08:00
fn process_shp(b: &[u8]) -> ResultS<()>
{
2019-02-12 06:41:28 -08:00
for (i, cl) in shp::read_shapes(b)?.iter().enumerate() {
2019-02-12 02:31:20 -08:00
if let Some(cl) = &cl.0 {
2019-02-12 06:41:28 -08:00
dump_bitmaps(cl, i)?;
2019-02-12 03:32:10 -08:00
eprintln!("<{} lo> {:#?}\n{:#?}", i, cl.frms, cl.seqs);
2019-02-12 02:31:20 -08:00
}
if let Some(cl) = &cl.1 {
2019-02-12 06:41:28 -08:00
dump_bitmaps(cl, i + 100)?;
2019-02-12 03:32:10 -08:00
eprintln!("<{} hi> {:#?}\n{:#?}", i, cl.frms, cl.seqs);
2019-02-12 02:31:20 -08:00
}
}
Ok(())
2018-09-06 09:01:52 -07:00
}
2019-02-08 20:21:22 -08:00
fn main() -> ResultS<()>
{
2019-02-12 09:53:25 -08:00
use argparse::*;
2019-02-08 20:21:22 -08:00
use memmap::Mmap;
2019-02-12 09:53:25 -08:00
let mut args: Vec<String> = Vec::new();
{
let mut ap = ArgumentParser::new();
ap.set_description(env!("CARGO_PKG_DESCRIPTION"));
ap.add_option(&["-v", "--version"],
Print(format!("{} {}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"))),
"Show the version");
ap.refer(&mut args)
.add_argument("inputs", List, "Input files");
ap.parse_args_or_exit();
}
for arg in &args {
2019-02-08 20:21:22 -08:00
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-10 02:31:57 -08:00
let b = c_data(&mm, machdr::try_mac_header(&mm)..)?;
2019-02-08 20:21:22 -08:00
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