prc/
lib.rs

1mod asm;
2mod disasm;
3mod param;
4pub mod prc_trait;
5#[cfg(feature = "xml-feat")]
6pub mod xml;
7
8#[cfg(test)]
9mod tests;
10
11use std::fs::{read, write};
12use std::io::{Cursor, Error, Read, Seek, Write};
13use std::path::Path;
14
15pub use hash40;
16pub use param::*;
17pub use prc_rs_derive::Prc;
18pub use prc_trait::Prc;
19
20pub(crate) type RefTable = Vec<(u32, u32)>;
21
22/// Attempts to read a param file from the given reader (requires [Seek]).
23/// The reader should be positioned at the header of the filetype.
24/// Returns a [ParamStruct] if successful, otherwise an [Error].
25pub fn read_stream<R>(reader: &mut R) -> std::result::Result<param::ParamStruct, Error>
26where
27    R: Read + Seek,
28{
29    disasm::disassemble(reader)
30}
31
32/// Attempts to write a param file into the given writer (requires [Seek]).
33/// Returns nothing if successful, otherwise an [Error].
34pub fn write_stream<W>(
35    writer: &mut W,
36    param_struct: &param::ParamStruct,
37) -> std::result::Result<(), Error>
38where
39    W: Write + Seek,
40{
41    asm::assemble(writer, param_struct)
42}
43
44/// Attempts to read a param file from the given filepath.
45/// Returns a [ParamStruct] if successful, otherwise an [Error].
46pub fn open<P: AsRef<Path>>(filepath: P) -> std::result::Result<param::ParamStruct, Error> {
47    let buf = read(filepath)?;
48    disasm::disassemble(&mut Cursor::new(buf))
49}
50
51/// Attempts to write a param file into the given filepath.
52/// Returns nothing if successful, otherwise an [Error].
53pub fn save<P: AsRef<Path>>(
54    filepath: P,
55    param: &param::ParamStruct,
56) -> std::result::Result<(), Error> {
57    let mut writer = Cursor::new(Vec::<u8>::new());
58    asm::assemble(&mut writer, param)?;
59    write(filepath, &writer.into_inner())
60}