Maraiah/src/main.rs

98 lines
2.3 KiB
Rust
Raw Normal View History

2018-09-06 09:01:52 -07:00
pub mod durandal;
pub mod marathon;
2018-12-11 00:08:23 -08:00
use crate::durandal::{err::*, image::Image, pict::load_pict};
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-10 23:33:38 -08:00
use std::{io, io::Write, fs, env};
#[derive(Debug)]
struct Minf
{
2018-12-10 23:32:59 -08:00
env_code: u16,
physi_id: u16,
music_id: u16,
msn_flag: u16,
env_flag: u16,
ent_flag: u32,
levelnam: 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::*;
2018-12-11 00:08:23 -08:00
if b.len() < 88 {return err_msg("not enough data for Minf")}
2018-12-10 23:32:59 -08:00
let env_code = b.c_u16b( 0)?;
let physi_id = b.c_u16b( 2)?;
let music_id = b.c_u16b( 4)?;
let msn_flag = b.c_u16b( 6)?;
let env_flag = b.c_u16b( 8)?;
let levelnam = mac_roman_conv(&b[18..84]);
let ent_flag = b.c_u32b(84)?;
2018-12-10 23:32:59 -08:00
Ok(Minf{env_code, physi_id, music_id, msn_flag, env_flag, ent_flag, levelnam})
}
}
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() {
2018-12-10 23:32:59 -08:00
for x in 0..im.w() {
2018-09-10 07:30:46 -07:00
let cr = &im[(x, y)];
write!(&mut out, "{} {} {} ", cr.r, cr.g, cr.b)?;
}
}
Ok(())
}
2018-09-06 09:01:52 -07:00
2018-12-11 00:08:23 -08:00
fn main() -> ResultS<()>
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-12-11 00:08:23 -08:00
let wad = wad::Wad::new(&mm)?;
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
2018-12-10 23:32:59 -08:00
for (id, ent) in wad.entries {
if let Some(b) = ent.chunks.get(b"PICT") {
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),
}
}
2018-12-10 23:32:59 -08:00
if let Some(b) = ent.chunks.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-12-10 23:32:59 -08:00
if let Some(b) = ent.chunks.get(b"term") {
match term::Terminal::chunk(b) {
Ok (c) => println!("entry {} has term {:#?}", id, c),
Err(e) => println!("entry {} has term (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