Maraiah/src/main.rs

115 lines
2.4 KiB
Rust
Raw Normal View History

2018-09-06 09:01:52 -07:00
extern crate memmap;
2018-09-09 15:21:31 -07:00
extern crate generic_array;
2018-09-06 09:01:52 -07:00
pub mod durandal;
pub mod marathon;
2018-09-09 15:21:31 -07:00
use std::{io, fs, env};
use std::io::Write;
2018-09-06 09:01:52 -07:00
2018-12-08 14:54:42 -08:00
use crate::durandal::pict::load_pict;
use crate::durandal::image::Image;
2018-09-10 07:30:46 -07:00
2018-12-08 14:54:42 -08:00
use crate::marathon::{wad, term};
2018-09-11 01:21:36 -07:00
use memmap::Mmap;
2018-12-08 14:54:42 -08:00
use crate::durandal::bin::ResultS;
#[derive(Debug)]
struct Minf
{
env: u16,
phy: u16,
mus: u16,
mfl: u16,
efl: u16,
epf: u32,
bip: bool,
nam: String,
}
impl Minf
{
fn chunk(b: &[u8]) -> ResultS<Minf>
{
2018-12-08 14:54:42 -08:00
use crate::durandal::text::mac_roman_conv;
use crate::durandal::bin::*;
if b.len() < 88 {return Err("not enough data for Minf")}
let env = b.c_u16b( 0)?;
let phy = b.c_u16b( 2)?;
let mus = b.c_u16b( 4)?;
let mfl = b.c_u16b( 6)?;
let efl = b.c_u16b( 8)?;
let bip = b[10] != 0;
let nam = mac_roman_conv(&b[18..84]);
let epf = b.c_u32b(84)?;
Ok(Minf{env, phy, mus, mfl, efl, epf, bip, nam})
}
}
2018-09-10 07:30:46 -07:00
fn write_ppm(fname: &str, im: &Image) -> io::Result<()>
{
let out = fs::File::create(fname)?;
let mut out = io::BufWriter::new(out);
write!(&mut out, "P3\n{} {}\n255\n", im.w(), im.h())?;
for y in 0..im.h() {
for x in 0..im.w()
{
let cr = &im[(x, y)];
write!(&mut out, "{} {} {} ", cr.r, cr.g, cr.b)?;
}
}
Ok(())
}
2018-09-06 09:01:52 -07:00
2018-09-09 15:21:31 -07:00
fn main() -> io::Result<()>
2018-09-06 09:01:52 -07:00
{
2018-09-09 15:21:31 -07:00
let arg = env::args().nth(1).expect("need at least 1 argument");
let fp = fs::File::open(arg)?;
let mm = unsafe{Mmap::map(&fp)?};
2018-09-11 12:07:42 -07:00
let wad = wad::Wad::new(&mm).unwrap();
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
for (id, ent) in wad.ent
{
2018-09-11 01:21:36 -07:00
if let Some(b) = ent.map.get(b"PICT")
2018-09-09 15:21:31 -07:00
{
2018-09-11 12:07:42 -07:00
match load_pict(b) {
2018-09-09 15:21:31 -07:00
Ok(im) => {
println!("entry {} has PICT {}x{}", id, im.w(), im.h());
2018-09-10 07:30:46 -07:00
write_ppm(&format!("out_{}.ppm", id), &im)?;
2018-09-09 15:21:31 -07:00
},
Err(e) => println!("entry {} has PICT (invalid: {:?})", id, e),
}
}
if let Some(b) = ent.map.get(b"term")
2018-09-11 01:21:36 -07:00
{
match term::Terminal::chunk(b) {
Ok(c) => println!("entry {} has term {:#?}", id, c),
Err(e) => println!("entry {} has term (invalid: {:?})", id, e),
}
}
if let Some(b) = ent.map.get(b"Minf")
{
match Minf::chunk(b) {
Ok(c) => println!("entry {} has Minf {:#?}", id, c),
Err(e) => println!("entry {} has Minf (invalid: {:?})", id, e),
}
2018-09-11 01:21:36 -07:00
}
2018-09-06 09:01:52 -07:00
}
2018-09-09 15:21:31 -07:00
Ok(())
2018-09-06 09:01:52 -07:00
}
// EOF