72 lines
2.4 KiB
Rust
72 lines
2.4 KiB
Rust
use std::path::Path;
|
|
use anyhow::Result;
|
|
use crate::model::patch::{Patch, PatchData, PatchList, PatchSelection, PatchSelectionData};
|
|
|
|
impl PatchSelection {
|
|
pub async fn render_to_file(
|
|
&self,
|
|
filename: &str,
|
|
patch_lists: &Vec<&PatchList>,
|
|
path: impl AsRef<Path>
|
|
) -> Result<()> {
|
|
let mut res = "".to_owned();
|
|
|
|
for list in patch_lists {
|
|
if list.filename != filename {
|
|
continue;
|
|
}
|
|
for patch in &list.patches {
|
|
if let Some(selection) = self.0.get(&patch.id) {
|
|
res += &Self::render(filename, patch, selection);
|
|
}
|
|
}
|
|
}
|
|
|
|
tokio::fs::write(path, res).await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn render(filename: &str, patch: &Patch, sel: &PatchSelectionData) -> String {
|
|
let mut res = "".to_owned();
|
|
match &patch.data {
|
|
PatchData::Normal(data) => {
|
|
for p in &data.patches {
|
|
res += &format!("{} F+{:X} ", filename, p.offset);
|
|
for on in &p.on {
|
|
res += &format!("{:02X}", on);
|
|
}
|
|
res += " ";
|
|
for off in &p.off {
|
|
res += &format!("{:02X}", off);
|
|
}
|
|
res += "\n";
|
|
}
|
|
},
|
|
PatchData::Number(data) => {
|
|
if let PatchSelectionData::Number(val) = sel {
|
|
let width = (data.size as usize) * 2usize;
|
|
res += &format!("{} F+{:X} {:0width$X} {:0width$X}", filename, data.offset, val, data.default, width = width);
|
|
} else {
|
|
log::error!("invalid number patch {:?}", patch);
|
|
}
|
|
},
|
|
PatchData::Hex(data) => {
|
|
if let PatchSelectionData::Hex(val) = sel {
|
|
res += &format!("{} F+{:X} ", filename, data.offset);
|
|
for byte in val {
|
|
res += &format!("{:02X}", byte);
|
|
}
|
|
res += " ";
|
|
for byte in &data.off {
|
|
res += &format!("{:02X}", byte);
|
|
}
|
|
} else {
|
|
log::error!("invalid number patch {:?}", patch);
|
|
}
|
|
|
|
}
|
|
}
|
|
format!("{}\n", res)
|
|
}
|
|
} |