2022-05-24 22:53:15 -04:00
|
|
|
use std::fs::File;
|
2022-05-29 10:22:34 -04:00
|
|
|
use std::marker::PhantomData;
|
2022-05-23 09:46:44 -04:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2022-05-24 22:53:15 -04:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-05-29 10:22:34 -04:00
|
|
|
use thiserror::Error;
|
2022-05-23 09:46:44 -04:00
|
|
|
|
2022-05-29 10:22:34 -04:00
|
|
|
use crate::rom::RomData;
|
|
|
|
|
|
|
|
pub mod bosses;
|
|
|
|
pub mod constants;
|
|
|
|
pub mod option_flags;
|
2022-05-23 09:46:44 -04:00
|
|
|
pub mod rom;
|
|
|
|
|
2022-05-29 10:22:34 -04:00
|
|
|
#[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)]
|
2022-05-23 09:46:44 -04:00
|
|
|
pub struct Patch {
|
|
|
|
pub address: usize,
|
|
|
|
pub patch_data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2022-05-24 22:53:15 -04:00
|
|
|
#[derive(Deserialize, Serialize)]
|
2022-05-23 09:46:44 -04:00
|
|
|
pub struct PatchSet {
|
|
|
|
filename: PathBuf,
|
2022-05-24 22:53:15 -04:00
|
|
|
patches: Vec<Patch>,
|
2022-05-23 09:46:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PatchSet {
|
2022-05-24 22:53:15 -04:00
|
|
|
pub fn load(filename: &Path) -> Result<PatchSet, anyhow::Error> {
|
2022-05-23 09:46:44 -04:00
|
|
|
let patches = {
|
2022-05-24 22:53:15 -04:00
|
|
|
let file = File::open(filename)?;
|
|
|
|
let buffer = std::io::BufReader::new(file);
|
2022-05-23 09:46:44 -04:00
|
|
|
serde_json::from_reader(buffer)?
|
|
|
|
};
|
2022-05-24 22:53:15 -04:00
|
|
|
Ok(PatchSet {
|
2022-05-23 09:46:44 -04:00
|
|
|
filename: filename.into(),
|
2022-05-24 22:53:15 -04:00
|
|
|
patches: patches,
|
|
|
|
})
|
2022-05-23 09:46:44 -04:00
|
|
|
}
|
|
|
|
|
2022-05-24 22:53:15 -04:00
|
|
|
pub fn filename(&self) -> &Path {
|
2022-05-23 09:46:44 -04:00
|
|
|
self.filename.as_path()
|
|
|
|
}
|
|
|
|
|
2022-05-24 22:53:15 -04:00
|
|
|
pub fn patch_rom(self, rom: &mut RomData) {
|
2022-05-23 09:46:44 -04:00
|
|
|
for patch in self.patches {
|
|
|
|
rom.patch_data(patch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|