Skip to content

Commit

Permalink
Add methods to read and write from streams
Browse files Browse the repository at this point in the history
  • Loading branch information
benhall-7 committed Nov 1, 2022
1 parent 5ea1745 commit a9f8828
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "motion_list_rs"
description = "Library for working with motion_list.bin files in Smash Ultimate"
repository = "https://github.com/ultimate-research/motion_lib"
license = "MIT"
version = "1.4.2"
version = "1.4.3"
authors = ["BenHall-7 <[email protected]>"]
edition = "2021"

Expand Down
9 changes: 5 additions & 4 deletions src/asm.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::mlist::*;
use byteorder::{LittleEndian, WriteBytesExt};
use hash40::*;
use std::io::{Cursor, Error, ErrorKind};
use std::io::{Error, ErrorKind, Seek, SeekFrom, Write};

pub fn assemble(cursor: &mut Cursor<Vec<u8>>, mlist: &MList) -> Result<(), Error> {
pub fn assemble<W: Write + Seek>(cursor: &mut W, mlist: &MList) -> Result<(), Error> {
cursor.write_hash40::<LittleEndian>(MAGIC)?;
cursor.write_hash40::<LittleEndian>(mlist.motion_path)?;
cursor.write_u64::<LittleEndian>(mlist.list.len() as u64)?;
Expand All @@ -24,7 +24,7 @@ pub fn assemble(cursor: &mut Cursor<Vec<u8>>, mlist: &MList) -> Result<(), Error
Ok(())
}

fn write_motion(cursor: &mut Cursor<Vec<u8>>, motion: &Motion) -> Result<(), Error> {
fn write_motion<W: Write + Seek>(cursor: &mut W, motion: &Motion) -> Result<(), Error> {
let anm_cnt = motion.animations.len();
if anm_cnt > 3 {
return Err(Error::new(
Expand All @@ -51,7 +51,8 @@ fn write_motion(cursor: &mut Cursor<Vec<u8>>, motion: &Motion) -> Result<(), Err
//align
const ALIGN: u64 = 4;
const ALIGN_MASK: u64 = ALIGN - 1;
cursor.set_position((cursor.position() + ALIGN_MASK) & !ALIGN_MASK);
let pos = cursor.seek(SeekFrom::Current(0))?;
cursor.seek(SeekFrom::Start((pos + ALIGN_MASK) & !ALIGN_MASK))?;

for script in motion.scripts.iter() {
cursor.write_hash40::<LittleEndian>(*script)?;
Expand Down
10 changes: 5 additions & 5 deletions src/disasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use crate::mlist::*;
use byteorder::{LittleEndian, ReadBytesExt};
use hash40::*;
use indexmap::IndexMap;
use std::io::{Cursor, Error, ErrorKind};
use std::io::{Error, ErrorKind, Read, Seek, SeekFrom};

pub fn disassemble(cursor: &mut Cursor<Vec<u8>>) -> Result<MList, Error> {
cursor.set_position(0);
pub fn disassemble<R: Read + Seek>(cursor: &mut R) -> Result<MList, Error> {
if MAGIC != cursor.read_hash40::<LittleEndian>()? {
return Err(Error::new(
ErrorKind::InvalidData,
Expand All @@ -28,7 +27,7 @@ pub fn disassemble(cursor: &mut Cursor<Vec<u8>>) -> Result<MList, Error> {
})
}

fn read_motion(cursor: &mut Cursor<Vec<u8>>) -> Result<Motion, Error> {
fn read_motion<R: Read + Seek>(cursor: &mut R) -> Result<Motion, Error> {
let game_script = cursor.read_hash40::<LittleEndian>()?;
let flags = cursor.read_u16::<LittleEndian>()?.into();
let blend_frames = cursor.read_u8()?;
Expand Down Expand Up @@ -56,7 +55,8 @@ fn read_motion(cursor: &mut Cursor<Vec<u8>>) -> Result<Motion, Error> {
//align by 4
const ALIGN: u64 = 4;
const ALIGN_MASK: u64 = ALIGN - 1;
cursor.set_position((cursor.position() + ALIGN_MASK) & !ALIGN_MASK);
let pos = cursor.seek(SeekFrom::Current(0))?;
cursor.seek(SeekFrom::Start((pos + ALIGN_MASK) & !ALIGN_MASK))?;

let scripts = (0..size / 8)
.map(|_| cursor.read_hash40::<LittleEndian>())
Expand Down
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ use std::fs::{read, File};
use std::io::{prelude::*, Cursor, Error};
use std::path::Path;

pub use hash40;
pub use diff;
pub use hash40;

/// Attempts to read a [MList] from the given reader (requires [Seek]).
/// The reader should be positioned at the start of the filetype.
/// Returns a [MList] if successful, otherwise an [Error].
pub fn read_stream<R: Read + Seek>(reader: &mut R) -> Result<MList, Error> {
disasm::disassemble(reader)
}

/// Attempts to write a [MList] into the given writer (requires [Seek]).
/// Returns nothing if successful, otherwise an [Error].
pub fn write_stream<W: Write + Seek>(writer: &mut W, mlist: &MList) -> Result<(), Error> {
asm::assemble(writer, mlist)
}

/// Attempts to read a [MList] from a file path
pub fn open<P: AsRef<Path>>(file: P) -> Result<MList, Error> {
disasm::disassemble(&mut Cursor::new(read(file)?))
}

/// Attempts to write a [MList] to a file path
pub fn save<P: AsRef<Path>>(path: P, mlist: &MList) -> Result<(), Error> {
let mut file = File::create(path)?;
let mut cursor = Cursor::new(Vec::<u8>::new());
Expand Down

0 comments on commit a9f8828

Please sign in to comment.