WIP local save file component
This commit is contained in:
parent
ed3b78eaa1
commit
56729dac15
|
@ -92,6 +92,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
save_path: Some(std::env::temp_dir()),
|
save_path: Some(std::env::temp_dir()),
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
emu.register_component(LocalFileSaveComponent::default())?;
|
||||||
|
|
||||||
emu.init()?;
|
emu.init()?;
|
||||||
emu.load_game(&opt.rom)?;
|
emu.load_game(&opt.rom)?;
|
||||||
if let Some(state) = opt.state {
|
if let Some(state) = opt.state {
|
||||||
|
|
|
@ -61,6 +61,7 @@ pub trait RetroComponent: RetroCallbacks + Any {
|
||||||
fn post_load_game(&mut self, retro: &mut LibretroWrapper, rom: &Path) -> Result<()> { Ok(()) }
|
fn post_load_game(&mut self, retro: &mut LibretroWrapper, rom: &Path) -> Result<()> { Ok(()) }
|
||||||
fn pre_run(&mut self, retro: &mut LibretroWrapper) -> ControlFlow { ControlFlow::Continue }
|
fn pre_run(&mut self, retro: &mut LibretroWrapper) -> ControlFlow { ControlFlow::Continue }
|
||||||
fn post_run(&mut self, retro: &mut LibretroWrapper) -> ControlFlow { ControlFlow::Continue }
|
fn post_run(&mut self, retro: &mut LibretroWrapper) -> ControlFlow { ControlFlow::Continue }
|
||||||
|
fn pre_drop(&mut self, retro: &mut LibretroWrapper) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RetroComponentBase {
|
impl RetroComponentBase {
|
||||||
|
@ -608,6 +609,9 @@ impl RetroCallbacks for RetroComponentBase {
|
||||||
|
|
||||||
impl Drop for RetroComponentBase {
|
impl Drop for RetroComponentBase {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
|
for comp in &mut self.components {
|
||||||
|
comp.pre_drop(&mut self.retro);
|
||||||
|
}
|
||||||
ferretro_base::retro::unset_handler(self.handler_id.unwrap()).unwrap();
|
ferretro_base::retro::unset_handler(self.handler_id.unwrap()).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ mod input;
|
||||||
mod logs;
|
mod logs;
|
||||||
mod memory;
|
mod memory;
|
||||||
mod paths;
|
mod paths;
|
||||||
|
mod saves;
|
||||||
mod variables;
|
mod variables;
|
||||||
|
|
||||||
pub use camera::CameraInfoComponent;
|
pub use camera::CameraInfoComponent;
|
||||||
|
@ -16,4 +17,5 @@ pub use logs::StderrCallTraceComponent;
|
||||||
pub use logs::StderrLogComponent;
|
pub use logs::StderrLogComponent;
|
||||||
pub use logs::StderrSysInfoLogComponent;
|
pub use logs::StderrSysInfoLogComponent;
|
||||||
pub use paths::PathBufComponent;
|
pub use paths::PathBufComponent;
|
||||||
|
pub use saves::LocalFileSaveComponent;
|
||||||
pub use variables::VariableStoreComponent;
|
pub use variables::VariableStoreComponent;
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use crate::prelude::{LibretroWrapper, RetroCallbacks, RetroComponent};
|
||||||
|
use ferretro_base::retro::ffi::MEMORY_SAVE_RAM;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct LocalFileSaveComponent {
|
||||||
|
rom_path: PathBuf,
|
||||||
|
initial_sram: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LocalFileSaveComponent {
|
||||||
|
fn save_path(&self) -> PathBuf {
|
||||||
|
self.rom_path.with_extension(".sav")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RetroComponent for LocalFileSaveComponent {
|
||||||
|
fn post_load_game(&mut self, retro: &mut LibretroWrapper, rom: &Path) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
|
self.rom_path = rom.to_path_buf();
|
||||||
|
let save = self.save_path();
|
||||||
|
if save.is_file() {
|
||||||
|
File::open(save)?.read(retro.mut_memory(MEMORY_SAVE_RAM))?;
|
||||||
|
}
|
||||||
|
self.initial_sram = retro.get_memory(MEMORY_SAVE_RAM).to_vec();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pre_drop(&mut self, retro: &mut LibretroWrapper) {
|
||||||
|
let final_sram = retro.get_memory(MEMORY_SAVE_RAM);
|
||||||
|
if &self.initial_sram != final_sram {
|
||||||
|
let save = self.save_path();
|
||||||
|
if let Ok(mut f) = File::create(save) {
|
||||||
|
let _ = f.write(final_sram);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RetroCallbacks for LocalFileSaveComponent {}
|
Loading…
Reference in New Issue