1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
mod asm;
mod disasm;
mod param;
pub mod prc_trait;
#[cfg(feature = "xml-feat")]
pub mod xml;

#[cfg(test)]
mod tests;

use std::fs::{read, write};
use std::io::{Cursor, Error, Read, Seek, Write};
use std::path::Path;

pub use hash40;
pub use param::*;
pub use prc_rs_derive::Prc;
pub use prc_trait::Prc;

pub(crate) type RefTable = Vec<(u32, u32)>;

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

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

/// Attempts to read a param file from the given filepath.
/// Returns a [ParamStruct] if successful, otherwise an [Error].
pub fn open<P: AsRef<Path>>(filepath: P) -> std::result::Result<param::ParamStruct, Error> {
    let buf = read(filepath)?;
    disasm::disassemble(&mut Cursor::new(buf))
}

/// Attempts to write a param file into the given filepath.
/// Returns nothing if successful, otherwise an [Error].
pub fn save<P: AsRef<Path>>(
    filepath: P,
    param: &param::ParamStruct,
) -> std::result::Result<(), Error> {
    let mut writer = Cursor::new(Vec::<u8>::new());
    asm::assemble(&mut writer, param)?;
    write(filepath, &writer.into_inner())
}