81 lines
1.8 KiB
Rust
81 lines
1.8 KiB
Rust
use std::fs::File;
|
|
use std::marker::PhantomData;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
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 randomize;
|
|
pub mod rom;
|
|
|
|
#[derive(Debug, Error)]
|
|
#[error("Not a valid value for {0:?}")]
|
|
pub struct InvalidEnumError<T>(PhantomData<T>);
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct Patch {
|
|
pub address: usize,
|
|
pub patch_data: Vec<u8>,
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub struct PatchSet {
|
|
filename: PathBuf,
|
|
patches: Vec<Patch>,
|
|
}
|
|
|
|
impl PatchSet {
|
|
pub fn load(filename: &Path) -> Result<PatchSet, anyhow::Error> {
|
|
let patches = {
|
|
let file = File::open(filename)?;
|
|
let buffer = std::io::BufReader::new(file);
|
|
serde_json::from_reader(buffer)?
|
|
};
|
|
Ok(PatchSet {
|
|
filename: filename.into(),
|
|
patches: patches,
|
|
})
|
|
}
|
|
|
|
pub fn save(&self) -> anyhow::Result<()> {
|
|
let file = File::create(&self.filename)?;
|
|
let buffer = std::io::BufWriter::new(file);
|
|
serde_json::to_writer(buffer, &self.patches)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn save_to_file(&self, filename: &Path) -> anyhow::Result<()> {
|
|
let file = File::create(filename)?;
|
|
let buffer = std::io::BufWriter::new(file);
|
|
serde_json::to_writer(buffer, &self.patches)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|