master
Ashley N. 2023-08-14 20:36:33 -04:00
parent aea97be053
commit d21d16605c
2 changed files with 69 additions and 65 deletions

View File

@ -1,17 +1,12 @@
use std::{error::Error, fs::File, io::Read, str::from_utf8, slice::Iter, convert::TryInto, collections::HashMap}; use std::{error::Error, fs::File, io::Read, convert::TryInto, collections::HashMap};
use flate2::read::ZlibDecoder; use flate2::read::ZlibDecoder;
use crate::reskit::utility::{Either, get_string, get_u8, skip, get_u32, get_i8, get_i32, get_u16, get_i16};
const DMF_MAGIC_NUMBER: &'static str = ".DelekDefleMask."; const DMF_MAGIC_NUMBER: &'static str = ".DelekDefleMask.";
const DMF_SUPPORTED_VERSION: u8 = 0x18; const DMF_SUPPORTED_VERSION: u8 = 0x18;
const DMF_MD: u8 = 0x02; const DMF_MD: u8 = 0x02;
const DMF_MD_ENHANCED_CH3: u8 = 0x42; const DMF_MD_ENHANCED_CH3: u8 = 0x42;
#[derive(Debug)]
pub enum Either< Type1, Type2 > {
Type1( Type1 ),
Type2( Type2 )
}
#[derive(Debug)] #[derive(Debug)]
pub enum FrameMode { pub enum FrameMode {
Ntsc, Ntsc,
@ -140,64 +135,6 @@ pub struct DmfModule {
samples: Vec<Sample> samples: Vec<Sample>
} }
fn get_string( bytes: &mut Iter<'_, u8>, take: usize ) -> Result<String, Box<dyn Error>> {
let took = bytes.take( take );
let took: Vec<u8> = took.cloned().collect();
let took = from_utf8( &took )?.to_string();
Ok( took )
}
fn get_u8( bytes: &mut Iter<'_, u8> ) -> Result<u8, Box<dyn Error>> {
let took = bytes.take( 1 ).cloned().next().ok_or( "invalid file: expected 1 byte" )?;
Ok( took )
}
fn get_i8( bytes: &mut Iter<'_, u8> ) -> Result<i8, Box<dyn Error>> {
let took = bytes.take( 1 ).cloned().next().ok_or( "invalid file: expected 1 byte" )?;
Ok( took as i8 )
}
fn get_u16( bytes: &mut Iter<'_, u8> ) -> Result<u16, Box<dyn Error>> {
let took = bytes.take( 2 );
let took: Vec<u8> = took.cloned().collect();
let took = u16::from_le_bytes( took[0..2].try_into()? );
Ok( took )
}
fn get_i16( bytes: &mut Iter<'_, u8> ) -> Result<i16, Box<dyn Error>> {
let took = bytes.take( 2 );
let took: Vec<u8> = took.cloned().collect();
let took = i16::from_le_bytes( took[0..2].try_into()? );
Ok( took )
}
fn get_u32( bytes: &mut Iter<'_, u8> ) -> Result<u32, Box<dyn Error>> {
let took = bytes.take( 4 );
let took: Vec<u8> = took.cloned().collect();
let took = u32::from_le_bytes( took[0..4].try_into()? );
Ok( took )
}
fn get_i32( bytes: &mut Iter<'_, u8> ) -> Result<i32, Box<dyn Error>> {
let took = bytes.take( 4 );
let took: Vec<u8> = took.cloned().collect();
let took = i32::from_le_bytes( took[0..4].try_into()? );
Ok( took )
}
fn skip( bytes: &mut Iter<'_, u8>, by: usize ) {
for _i in 0..by {
bytes.next();
}
}
impl DmfModule { impl DmfModule {
pub fn from_file( path: &str ) -> Result<DmfModule, Box<dyn Error>> { pub fn from_file( path: &str ) -> Result<DmfModule, Box<dyn Error>> {

View File

@ -1,3 +1,12 @@
use std::{slice::Iter, error::Error, str::from_utf8, convert::TryInto};
#[derive(Debug)]
pub enum Either< Type1, Type2 > {
Type1( Type1 ),
Type2( Type2 )
}
pub fn print_error( error_msg: &str ) { pub fn print_error( error_msg: &str ) {
red!( "fatal: " ); println!( "{}", error_msg ); red!( "fatal: " ); println!( "{}", error_msg );
} }
@ -5,3 +14,61 @@ pub fn print_error( error_msg: &str ) {
pub fn print_good( message: &str ) { pub fn print_good( message: &str ) {
green!( "success: " ); println!( "{}", message ); green!( "success: " ); println!( "{}", message );
} }
pub fn get_string( bytes: &mut Iter<'_, u8>, take: usize ) -> Result<String, Box<dyn Error>> {
let took = bytes.take( take );
let took: Vec<u8> = took.cloned().collect();
let took = from_utf8( &took )?.to_string();
Ok( took )
}
pub fn get_u8( bytes: &mut Iter<'_, u8> ) -> Result<u8, Box<dyn Error>> {
let took = bytes.take( 1 ).cloned().next().ok_or( "invalid file: expected 1 byte" )?;
Ok( took )
}
pub fn get_i8( bytes: &mut Iter<'_, u8> ) -> Result<i8, Box<dyn Error>> {
let took = bytes.take( 1 ).cloned().next().ok_or( "invalid file: expected 1 byte" )?;
Ok( took as i8 )
}
pub fn get_u16( bytes: &mut Iter<'_, u8> ) -> Result<u16, Box<dyn Error>> {
let took = bytes.take( 2 );
let took: Vec<u8> = took.cloned().collect();
let took = u16::from_le_bytes( took[0..2].try_into()? );
Ok( took )
}
pub fn get_i16( bytes: &mut Iter<'_, u8> ) -> Result<i16, Box<dyn Error>> {
let took = bytes.take( 2 );
let took: Vec<u8> = took.cloned().collect();
let took = i16::from_le_bytes( took[0..2].try_into()? );
Ok( took )
}
pub fn get_u32( bytes: &mut Iter<'_, u8> ) -> Result<u32, Box<dyn Error>> {
let took = bytes.take( 4 );
let took: Vec<u8> = took.cloned().collect();
let took = u32::from_le_bytes( took[0..4].try_into()? );
Ok( took )
}
pub fn get_i32( bytes: &mut Iter<'_, u8> ) -> Result<i32, Box<dyn Error>> {
let took = bytes.take( 4 );
let took: Vec<u8> = took.cloned().collect();
let took = i32::from_le_bytes( took[0..4].try_into()? );
Ok( took )
}
pub fn skip( bytes: &mut Iter<'_, u8>, by: usize ) {
for _i in 0..by {
bytes.next();
}
}