enemize-rs/enemize/src/lib.rs

63 lines
1.3 KiB
Rust
Raw Normal View History

2022-05-24 22:53:15 -04:00
use std::fs::File;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
2022-05-24 22:53:15 -04:00
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::rom::RomData;
pub mod asar;
pub mod bosses;
pub mod constants;
pub mod option_flags;
pub mod rom;
#[derive(Debug, Error)]
#[error("Not a valid value for {0:?}")]
pub struct InvalidEnumError<T>(PhantomData<T>);
2022-05-24 22:53:15 -04:00
#[derive(Deserialize, Serialize)]
pub struct Patch {
pub address: usize,
pub patch_data: Vec<u8>,
}
2022-05-24 22:53:15 -04:00
#[derive(Deserialize, Serialize)]
pub struct PatchSet {
filename: PathBuf,
2022-05-24 22:53:15 -04:00
patches: Vec<Patch>,
}
impl PatchSet {
2022-05-24 22:53:15 -04:00
pub fn load(filename: &Path) -> Result<PatchSet, anyhow::Error> {
let patches = {
2022-05-24 22:53:15 -04:00
let file = File::open(filename)?;
let buffer = std::io::BufReader::new(file);
serde_json::from_reader(buffer)?
};
2022-05-24 22:53:15 -04:00
Ok(PatchSet {
filename: filename.into(),
2022-05-24 22:53:15 -04:00
patches: patches,
})
}
pub fn add_patch(&mut self, patch: Patch) {
self.patches.push(patch);
}
pub fn add_patches(&mut self, patches: Vec<Patch>) {
self.patches.extend(patches);
}
2022-05-24 22:53:15 -04:00
pub fn filename(&self) -> &Path {
self.filename.as_path()
}
pub fn patch_rom(&self, rom: &mut RomData) {
for patch in self.patches.iter() {
rom.patch_data(patch);
}
}
}