Support manual/automatic ROM reloading
This commit is contained in:
parent
2e474df3e8
commit
2034f7934b
|
|
@ -1449,6 +1449,15 @@ dependencies = [
|
||||||
"windows-sys 0.59.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fsevent-sys"
|
||||||
|
version = "4.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-channel"
|
name = "futures-channel"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
|
|
@ -2157,6 +2166,26 @@ version = "3.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc"
|
checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "kqueue"
|
||||||
|
version = "1.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "eac30106d7dce88daf4a3fcb4879ea939476d5074a9b7ddd0fb97fa4bed5596a"
|
||||||
|
dependencies = [
|
||||||
|
"kqueue-sys",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "kqueue-sys"
|
||||||
|
version = "1.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 1.3.2",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
|
|
@ -2189,6 +2218,7 @@ dependencies = [
|
||||||
"image",
|
"image",
|
||||||
"itertools 0.14.0",
|
"itertools 0.14.0",
|
||||||
"normpath",
|
"normpath",
|
||||||
|
"notify",
|
||||||
"num-derive",
|
"num-derive",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"object 0.37.3",
|
"object 0.37.3",
|
||||||
|
|
@ -2482,6 +2512,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c"
|
checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
|
"log",
|
||||||
"wasi 0.11.1+wasi-snapshot-preview1",
|
"wasi 0.11.1+wasi-snapshot-preview1",
|
||||||
"windows-sys 0.59.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
@ -2624,6 +2655,30 @@ dependencies = [
|
||||||
"windows-sys 0.59.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "notify"
|
||||||
|
version = "8.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4d3d07927151ff8575b7087f245456e549fea62edf0ec4e565a5ee50c8402bc3"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.9.3",
|
||||||
|
"fsevent-sys",
|
||||||
|
"inotify",
|
||||||
|
"kqueue",
|
||||||
|
"libc",
|
||||||
|
"log",
|
||||||
|
"mio",
|
||||||
|
"notify-types",
|
||||||
|
"walkdir",
|
||||||
|
"windows-sys 0.60.2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "notify-types"
|
||||||
|
version = "2.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5e0826a989adedc2a244799e823aece04662b66609d96af8dff7ac6df9a8925d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nu-ansi-term"
|
name = "nu-ansi-term"
|
||||||
version = "0.46.0"
|
version = "0.46.0"
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ hex = "0.4"
|
||||||
image = { version = "0.25", default-features = false, features = ["png"] }
|
image = { version = "0.25", default-features = false, features = ["png"] }
|
||||||
itertools = "0.14"
|
itertools = "0.14"
|
||||||
normpath = "1"
|
normpath = "1"
|
||||||
|
notify = "8"
|
||||||
num-derive = "0.4"
|
num-derive = "0.4"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
object = "0.37"
|
object = "0.37"
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ pub struct CliArgs {
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
pub object_data: bool,
|
pub object_data: bool,
|
||||||
/// Open worlds window
|
/// Open worlds window
|
||||||
#[arg(short, long)]
|
#[arg(long)]
|
||||||
pub worlds: bool,
|
pub worlds: bool,
|
||||||
/// Open frame buffers window
|
/// Open frame buffers window
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
|
|
@ -37,6 +37,9 @@ pub struct CliArgs {
|
||||||
/// Open terminal
|
/// Open terminal
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
pub terminal: bool,
|
pub terminal: bool,
|
||||||
|
/// Watch ROM files for changes, automatically reload
|
||||||
|
#[arg(short, long)]
|
||||||
|
pub watch: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const COLOR_PRESETS: [[Color32; 2]; 3] = [
|
pub const COLOR_PRESETS: [[Color32; 2]; 3] = [
|
||||||
|
|
|
||||||
113
src/emulator.rs
113
src/emulator.rs
|
|
@ -5,7 +5,7 @@ use std::{
|
||||||
sync::{
|
sync::{
|
||||||
Arc, Weak,
|
Arc, Weak,
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
mpsc::{self, RecvError, TryRecvError},
|
mpsc::{self, RecvTimeoutError, TryRecvError},
|
||||||
},
|
},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
@ -75,6 +75,7 @@ pub struct EmulatorBuilder {
|
||||||
audio_on: Arc<[AtomicBool; 2]>,
|
audio_on: Arc<[AtomicBool; 2]>,
|
||||||
linked: Arc<AtomicBool>,
|
linked: Arc<AtomicBool>,
|
||||||
start_paused: bool,
|
start_paused: bool,
|
||||||
|
watch_rom: Arc<[AtomicBool; 2]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EmulatorBuilder {
|
impl EmulatorBuilder {
|
||||||
|
|
@ -91,11 +92,13 @@ impl EmulatorBuilder {
|
||||||
audio_on: Arc::new([AtomicBool::new(true), AtomicBool::new(true)]),
|
audio_on: Arc::new([AtomicBool::new(true), AtomicBool::new(true)]),
|
||||||
linked: Arc::new(AtomicBool::new(false)),
|
linked: Arc::new(AtomicBool::new(false)),
|
||||||
start_paused: false,
|
start_paused: false,
|
||||||
|
watch_rom: Arc::new([AtomicBool::new(false), AtomicBool::new(false)]),
|
||||||
};
|
};
|
||||||
let client = EmulatorClient {
|
let client = EmulatorClient {
|
||||||
queue,
|
queue,
|
||||||
sim_state: builder.sim_state.clone(),
|
sim_state: builder.sim_state.clone(),
|
||||||
state: builder.state.clone(),
|
state: builder.state.clone(),
|
||||||
|
watch_rom: builder.watch_rom.clone(),
|
||||||
linked: builder.linked.clone(),
|
linked: builder.linked.clone(),
|
||||||
};
|
};
|
||||||
(builder, client)
|
(builder, client)
|
||||||
|
|
@ -121,12 +124,19 @@ impl EmulatorBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_watch_rom(self, p1: bool, p2: bool) -> Self {
|
||||||
|
self.watch_rom[0].store(p1, Ordering::Relaxed);
|
||||||
|
self.watch_rom[1].store(p2, Ordering::Relaxed);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn build(self) -> Result<Emulator> {
|
pub fn build(self) -> Result<Emulator> {
|
||||||
let mut emulator = Emulator::new(
|
let mut emulator = Emulator::new(
|
||||||
self.commands,
|
self.commands,
|
||||||
self.sim_state,
|
self.sim_state,
|
||||||
self.state,
|
self.state,
|
||||||
self.audio_on,
|
self.audio_on,
|
||||||
|
self.watch_rom,
|
||||||
self.linked,
|
self.linked,
|
||||||
)?;
|
)?;
|
||||||
if let Some(path) = self.rom {
|
if let Some(path) = self.rom {
|
||||||
|
|
@ -147,6 +157,7 @@ pub struct Emulator {
|
||||||
sim_state: Arc<[Atomic<SimState>; 2]>,
|
sim_state: Arc<[Atomic<SimState>; 2]>,
|
||||||
state: Arc<Atomic<EmulatorState>>,
|
state: Arc<Atomic<EmulatorState>>,
|
||||||
audio_on: Arc<[AtomicBool; 2]>,
|
audio_on: Arc<[AtomicBool; 2]>,
|
||||||
|
watch_rom: Arc<[AtomicBool; 2]>,
|
||||||
linked: Arc<AtomicBool>,
|
linked: Arc<AtomicBool>,
|
||||||
profilers: [Option<ProfileSender>; 2],
|
profilers: [Option<ProfileSender>; 2],
|
||||||
renderers: HashMap<SimId, TextureSink>,
|
renderers: HashMap<SimId, TextureSink>,
|
||||||
|
|
@ -165,6 +176,7 @@ impl Emulator {
|
||||||
sim_state: Arc<[Atomic<SimState>; 2]>,
|
sim_state: Arc<[Atomic<SimState>; 2]>,
|
||||||
state: Arc<Atomic<EmulatorState>>,
|
state: Arc<Atomic<EmulatorState>>,
|
||||||
audio_on: Arc<[AtomicBool; 2]>,
|
audio_on: Arc<[AtomicBool; 2]>,
|
||||||
|
watch_rom: Arc<[AtomicBool; 2]>,
|
||||||
linked: Arc<AtomicBool>,
|
linked: Arc<AtomicBool>,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
|
@ -175,6 +187,7 @@ impl Emulator {
|
||||||
sim_state,
|
sim_state,
|
||||||
state,
|
state,
|
||||||
audio_on,
|
audio_on,
|
||||||
|
watch_rom,
|
||||||
linked,
|
linked,
|
||||||
profilers: [None, None],
|
profilers: [None, None],
|
||||||
renderers: HashMap::new(),
|
renderers: HashMap::new(),
|
||||||
|
|
@ -188,9 +201,18 @@ impl Emulator {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reload_cart(&mut self, sim_id: SimId) -> Result<()> {
|
||||||
|
let Some(cart) = &self.carts[sim_id.to_index()] else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let path = cart.file_path.clone();
|
||||||
|
self.load_cart(sim_id, &path)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_cart(&mut self, sim_id: SimId, path: &Path) -> Result<()> {
|
pub fn load_cart(&mut self, sim_id: SimId, path: &Path) -> Result<()> {
|
||||||
let cart = Cart::load(path, sim_id)?;
|
let watch = self.watch_rom[sim_id.to_index()].load(Ordering::Acquire);
|
||||||
self.reset_sim(sim_id, Some(cart))?;
|
let cart = Cart::load(path, sim_id, watch)?;
|
||||||
|
self.try_reset_sim(sim_id, Some(cart))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -200,16 +222,26 @@ impl Emulator {
|
||||||
} else {
|
} else {
|
||||||
self.carts[0].as_ref().map(|c| c.file_path.clone())
|
self.carts[0].as_ref().map(|c| c.file_path.clone())
|
||||||
};
|
};
|
||||||
|
let watch = self.watch_rom[SimId::Player2.to_index()].load(Ordering::Acquire);
|
||||||
let cart = match file_path {
|
let cart = match file_path {
|
||||||
Some(rom_path) => Some(Cart::load(&rom_path, SimId::Player2)?),
|
Some(rom_path) => Some(Cart::load(&rom_path, SimId::Player2, watch)?),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
self.reset_sim(SimId::Player2, cart)?;
|
self.try_reset_sim(SimId::Player2, cart)?;
|
||||||
self.link_sims();
|
self.link_sims();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reset_sim(&mut self, sim_id: SimId, new_cart: Option<Cart>) -> Result<()> {
|
fn reset_sim(&mut self, sim_id: SimId, new_cart: Option<Cart>) -> bool {
|
||||||
|
if let Err(error) = self.try_reset_sim(sim_id, new_cart) {
|
||||||
|
self.report_error(sim_id, format!("Error resetting sim: {error}"));
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_reset_sim(&mut self, sim_id: SimId, new_cart: Option<Cart>) -> Result<()> {
|
||||||
self.save_sram(sim_id)?;
|
self.save_sram(sim_id)?;
|
||||||
|
|
||||||
let index = sim_id.to_index();
|
let index = sim_id.to_index();
|
||||||
|
|
@ -230,6 +262,10 @@ impl Emulator {
|
||||||
sim.load_cart(cart.rom.clone(), cart.sram.clone())?;
|
sim.load_cart(cart.rom.clone(), cart.sram.clone())?;
|
||||||
self.carts[index] = Some(cart);
|
self.carts[index] = Some(cart);
|
||||||
self.sim_state[index].store(SimState::Ready, Ordering::Release);
|
self.sim_state[index].store(SimState::Ready, Ordering::Release);
|
||||||
|
} else if let Some(cart) = self.carts[index].as_mut()
|
||||||
|
&& cart.is_watching()
|
||||||
|
{
|
||||||
|
cart.restart_watching();
|
||||||
}
|
}
|
||||||
let mut profiling = false;
|
let mut profiling = false;
|
||||||
if let Some(profiler) = self.profilers[sim_id.to_index()].as_ref()
|
if let Some(profiler) = self.profilers[sim_id.to_index()].as_ref()
|
||||||
|
|
@ -346,7 +382,7 @@ impl Emulator {
|
||||||
|
|
||||||
fn start_profiling(&mut self, sim_id: SimId, sender: ProfileSender) -> Result<()> {
|
fn start_profiling(&mut self, sim_id: SimId, sender: ProfileSender) -> Result<()> {
|
||||||
self.profilers[sim_id.to_index()] = Some(sender);
|
self.profilers[sim_id.to_index()] = Some(sender);
|
||||||
self.reset_sim(sim_id, None)
|
self.try_reset_sim(sim_id, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start_debugging(&mut self, sim_id: SimId, sender: DebugSender) {
|
fn start_debugging(&mut self, sim_id: SimId, sender: DebugSender) {
|
||||||
|
|
@ -422,11 +458,30 @@ impl Emulator {
|
||||||
let idle = self.tick();
|
let idle = self.tick();
|
||||||
if idle {
|
if idle {
|
||||||
// The game is paused, and we have output all the video/audio we have.
|
// The game is paused, and we have output all the video/audio we have.
|
||||||
// Block the thread until a new command comes in.
|
// Block the thread until a new command comes in, or a ROM changes.
|
||||||
match self.commands.recv() {
|
loop {
|
||||||
Ok(command) => self.handle_command(command),
|
match self.commands.recv_timeout(Duration::from_millis(250)) {
|
||||||
Err(RecvError) => {
|
Ok(command) => {
|
||||||
return;
|
self.handle_command(command);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Err(RecvTimeoutError::Timeout) => {
|
||||||
|
let mut changed = false;
|
||||||
|
for sim_id in SimId::values() {
|
||||||
|
let Some(cart) = self.carts[sim_id.to_index()].as_ref() else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
if cart.changed() {
|
||||||
|
changed |= self.reset_sim(sim_id, None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(RecvTimeoutError::Disconnected) => {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -441,6 +496,14 @@ impl Emulator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for sim_id in SimId::values() {
|
||||||
|
let Some(cart) = self.carts[sim_id.to_index()].as_ref() else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
if cart.changed() {
|
||||||
|
self.reset_sim(sim_id, None);
|
||||||
|
}
|
||||||
|
}
|
||||||
self.watched_regions.retain(|range, region| {
|
self.watched_regions.retain(|range, region| {
|
||||||
let Some(region) = region.upgrade() else {
|
let Some(region) = region.upgrade() else {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -607,6 +670,22 @@ impl Emulator {
|
||||||
self.report_error(sim_id, format!("Error loading rom: {error}"));
|
self.report_error(sim_id, format!("Error loading rom: {error}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
EmulatorCommand::ReloadRom(sim_id) => {
|
||||||
|
if let Err(error) = self.reload_cart(sim_id) {
|
||||||
|
self.report_error(sim_id, format!("Error loading rom: {error}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EmulatorCommand::WatchRom(sim_id, watch) => {
|
||||||
|
self.watch_rom[sim_id.to_index()].store(watch, Ordering::Release);
|
||||||
|
let Some(cart) = self.carts[sim_id.to_index()].as_mut() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if watch {
|
||||||
|
cart.restart_watching();
|
||||||
|
} else {
|
||||||
|
cart.stop_watching();
|
||||||
|
}
|
||||||
|
}
|
||||||
EmulatorCommand::StartSecondSim(path) => {
|
EmulatorCommand::StartSecondSim(path) => {
|
||||||
if let Err(error) = self.start_second_sim(path) {
|
if let Err(error) = self.start_second_sim(path) {
|
||||||
self.report_error(
|
self.report_error(
|
||||||
|
|
@ -730,9 +809,7 @@ impl Emulator {
|
||||||
self.unlink_sims();
|
self.unlink_sims();
|
||||||
}
|
}
|
||||||
EmulatorCommand::Reset(sim_id) => {
|
EmulatorCommand::Reset(sim_id) => {
|
||||||
if let Err(error) = self.reset_sim(sim_id, None) {
|
self.reset_sim(sim_id, None);
|
||||||
self.report_error(sim_id, format!("Error resetting sim: {error}"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
EmulatorCommand::SetKeys(sim_id, keys) => {
|
EmulatorCommand::SetKeys(sim_id, keys) => {
|
||||||
if let Some(sim) = self.sims.get_mut(sim_id.to_index()) {
|
if let Some(sim) = self.sims.get_mut(sim_id.to_index()) {
|
||||||
|
|
@ -774,6 +851,8 @@ impl Emulator {
|
||||||
pub enum EmulatorCommand {
|
pub enum EmulatorCommand {
|
||||||
ConnectToSim(SimId, TextureSink, mpsc::Sender<Toast>),
|
ConnectToSim(SimId, TextureSink, mpsc::Sender<Toast>),
|
||||||
LoadGame(SimId, PathBuf),
|
LoadGame(SimId, PathBuf),
|
||||||
|
ReloadRom(SimId),
|
||||||
|
WatchRom(SimId, bool),
|
||||||
StartSecondSim(Option<PathBuf>),
|
StartSecondSim(Option<PathBuf>),
|
||||||
StopSecondSim,
|
StopSecondSim,
|
||||||
Pause,
|
Pause,
|
||||||
|
|
@ -863,6 +942,7 @@ pub struct EmulatorClient {
|
||||||
sim_state: Arc<[Atomic<SimState>; 2]>,
|
sim_state: Arc<[Atomic<SimState>; 2]>,
|
||||||
state: Arc<Atomic<EmulatorState>>,
|
state: Arc<Atomic<EmulatorState>>,
|
||||||
linked: Arc<AtomicBool>,
|
linked: Arc<AtomicBool>,
|
||||||
|
watch_rom: Arc<[AtomicBool; 2]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EmulatorClient {
|
impl EmulatorClient {
|
||||||
|
|
@ -875,6 +955,9 @@ impl EmulatorClient {
|
||||||
pub fn are_sims_linked(&self) -> bool {
|
pub fn are_sims_linked(&self) -> bool {
|
||||||
self.linked.load(Ordering::Acquire)
|
self.linked.load(Ordering::Acquire)
|
||||||
}
|
}
|
||||||
|
pub fn is_rom_watched(&self, sim_id: SimId) -> bool {
|
||||||
|
self.watch_rom[sim_id.to_index()].load(Ordering::Acquire)
|
||||||
|
}
|
||||||
pub fn send_command(&self, command: EmulatorCommand) -> bool {
|
pub fn send_command(&self, command: EmulatorCommand) -> bool {
|
||||||
match self.queue.send(command) {
|
match self.queue.send(command) {
|
||||||
Ok(()) => true,
|
Ok(()) => true,
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use notify::Watcher;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::{
|
use std::{
|
||||||
fs::{self, File},
|
fs::{self, File},
|
||||||
io::{Read, Seek as _, SeekFrom, Write as _},
|
io::{Read, Seek as _, SeekFrom, Write as _},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::Arc,
|
sync::{Arc, atomic::AtomicBool},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::emulator::{SimId, game_info::GameInfo, shrooms_vb_util::rom_from_isx};
|
use crate::emulator::{SimId, game_info::GameInfo, shrooms_vb_util::rom_from_isx};
|
||||||
|
|
@ -15,10 +16,12 @@ pub struct Cart {
|
||||||
sram_file: File,
|
sram_file: File,
|
||||||
pub sram: Vec<u8>,
|
pub sram: Vec<u8>,
|
||||||
pub info: Arc<GameInfo>,
|
pub info: Arc<GameInfo>,
|
||||||
|
watcher: Option<notify::RecommendedWatcher>,
|
||||||
|
changed: Arc<AtomicBool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cart {
|
impl Cart {
|
||||||
pub fn load(file_path: &Path, sim_id: SimId) -> Result<Self> {
|
pub fn load(file_path: &Path, sim_id: SimId, watch: bool) -> Result<Self> {
|
||||||
let rom = fs::read(file_path)?;
|
let rom = fs::read(file_path)?;
|
||||||
let (rom, info) = try_parse_isx(file_path, &rom)
|
let (rom, info) = try_parse_isx(file_path, &rom)
|
||||||
.or_else(|| try_parse_elf(file_path, &rom))
|
.or_else(|| try_parse_elf(file_path, &rom))
|
||||||
|
|
@ -45,12 +48,21 @@ impl Cart {
|
||||||
sram
|
sram
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let changed = Arc::new(AtomicBool::new(false));
|
||||||
|
let watcher = if watch {
|
||||||
|
build_watcher(file_path, changed.clone())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Cart {
|
Ok(Cart {
|
||||||
file_path: file_path.to_path_buf(),
|
file_path: file_path.to_path_buf(),
|
||||||
rom,
|
rom,
|
||||||
sram_file,
|
sram_file,
|
||||||
sram,
|
sram,
|
||||||
info: Arc::new(info),
|
info: Arc::new(info),
|
||||||
|
watcher,
|
||||||
|
changed,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,6 +71,50 @@ impl Cart {
|
||||||
self.sram_file.write_all(&self.sram)?;
|
self.sram_file.write_all(&self.sram)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn changed(&self) -> bool {
|
||||||
|
self.changed.load(std::sync::atomic::Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_watching(&self) -> bool {
|
||||||
|
self.watcher.is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn restart_watching(&mut self) {
|
||||||
|
self.changed = Arc::new(AtomicBool::new(false));
|
||||||
|
if let Some(mut watcher) = self.watcher.take() {
|
||||||
|
let _ = watcher.unwatch(&self.file_path);
|
||||||
|
};
|
||||||
|
self.watcher = build_watcher(&self.file_path, self.changed.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stop_watching(&mut self) {
|
||||||
|
self.changed = Arc::new(AtomicBool::new(false));
|
||||||
|
self.watcher = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_watcher(file_path: &Path, changed: Arc<AtomicBool>) -> Option<notify::RecommendedWatcher> {
|
||||||
|
let file_path = file_path.to_path_buf();
|
||||||
|
let mut watcher =
|
||||||
|
notify::recommended_watcher(move |event: Result<notify::Event, notify::Error>| {
|
||||||
|
let Ok(e) = event else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let modified = !matches!(
|
||||||
|
e.kind,
|
||||||
|
notify::EventKind::Access(_)
|
||||||
|
| notify::EventKind::Modify(notify::event::ModifyKind::Metadata(_))
|
||||||
|
);
|
||||||
|
if modified {
|
||||||
|
changed.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.ok()?;
|
||||||
|
watcher
|
||||||
|
.watch(&file_path, notify::RecursiveMode::NonRecursive)
|
||||||
|
.ok()?;
|
||||||
|
Some(watcher)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_parse_isx(file_path: &Path, data: &[u8]) -> Option<(Vec<u8>, GameInfo)> {
|
fn try_parse_isx(file_path: &Path, data: &[u8]) -> Option<(Vec<u8>, GameInfo)> {
|
||||||
|
|
|
||||||
|
|
@ -460,6 +460,7 @@ struct PersistedGamepadMapping {
|
||||||
#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
OpenRom,
|
OpenRom,
|
||||||
|
ReloadRom,
|
||||||
Quit,
|
Quit,
|
||||||
FrameAdvance,
|
FrameAdvance,
|
||||||
FastForward(u32),
|
FastForward(u32),
|
||||||
|
|
@ -470,9 +471,10 @@ pub enum Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Command {
|
impl Command {
|
||||||
pub fn all() -> [Self; 7] {
|
pub fn all() -> [Self; 8] {
|
||||||
[
|
[
|
||||||
Self::OpenRom,
|
Self::OpenRom,
|
||||||
|
Self::ReloadRom,
|
||||||
Self::Quit,
|
Self::Quit,
|
||||||
Self::PauseResume,
|
Self::PauseResume,
|
||||||
Self::Reset,
|
Self::Reset,
|
||||||
|
|
@ -485,6 +487,7 @@ impl Command {
|
||||||
pub fn name(self) -> &'static str {
|
pub fn name(self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
Self::OpenRom => "Open ROM",
|
Self::OpenRom => "Open ROM",
|
||||||
|
Self::ReloadRom => "Reload ROM",
|
||||||
Self::Quit => "Exit",
|
Self::Quit => "Exit",
|
||||||
Self::PauseResume => "Pause/Resume",
|
Self::PauseResume => "Pause/Resume",
|
||||||
Self::Reset => "Reset",
|
Self::Reset => "Reset",
|
||||||
|
|
@ -516,6 +519,10 @@ impl Default for Shortcuts {
|
||||||
Command::OpenRom,
|
Command::OpenRom,
|
||||||
KeyboardShortcut::new(Modifiers::COMMAND, Key::O),
|
KeyboardShortcut::new(Modifiers::COMMAND, Key::O),
|
||||||
);
|
);
|
||||||
|
shortcuts.set(
|
||||||
|
Command::ReloadRom,
|
||||||
|
KeyboardShortcut::new(Modifiers::COMMAND | Modifiers::SHIFT, Key::F5),
|
||||||
|
);
|
||||||
shortcuts.set(
|
shortcuts.set(
|
||||||
Command::Quit,
|
Command::Quit,
|
||||||
KeyboardShortcut::new(Modifiers::COMMAND, Key::Q),
|
KeyboardShortcut::new(Modifiers::COMMAND, Key::Q),
|
||||||
|
|
|
||||||
11
src/main.rs
11
src/main.rs
|
|
@ -110,9 +110,14 @@ fn main() -> Result<()> {
|
||||||
if args.profile {
|
if args.profile {
|
||||||
builder = builder.start_paused(true)
|
builder = builder.start_paused(true)
|
||||||
}
|
}
|
||||||
let p1_audio_on = SimConfig::load(&persistence, SimId::Player1).audio_enabled;
|
let p1 = SimConfig::load(&persistence, SimId::Player1);
|
||||||
let p2_audio_on = SimConfig::load(&persistence, SimId::Player2).audio_enabled;
|
let p2 = SimConfig::load(&persistence, SimId::Player2);
|
||||||
builder = builder.with_audio_on(p1_audio_on, p2_audio_on);
|
|
||||||
|
let watch = args.watch;
|
||||||
|
|
||||||
|
builder = builder
|
||||||
|
.with_audio_on(p1.audio_enabled, p2.audio_enabled)
|
||||||
|
.with_watch_rom(watch, watch);
|
||||||
|
|
||||||
ThreadBuilder::default()
|
ThreadBuilder::default()
|
||||||
.name("Emulator".to_owned())
|
.name("Emulator".to_owned())
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,10 @@ impl GameWindow {
|
||||||
.send_command(EmulatorCommand::LoadGame(self.sim_id, path));
|
.send_command(EmulatorCommand::LoadGame(self.sim_id, path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Command::ReloadRom => {
|
||||||
|
self.client
|
||||||
|
.send_command(EmulatorCommand::ReloadRom(self.sim_id));
|
||||||
|
}
|
||||||
Command::Quit => {
|
Command::Quit => {
|
||||||
let _ = self.proxy.send_event(UserEvent::Quit(self.sim_id));
|
let _ = self.proxy.send_event(UserEvent::Quit(self.sim_id));
|
||||||
}
|
}
|
||||||
|
|
@ -137,6 +141,18 @@ impl GameWindow {
|
||||||
.send_command(EmulatorCommand::LoadGame(self.sim_id, path));
|
.send_command(EmulatorCommand::LoadGame(self.sim_id, path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ui
|
||||||
|
.add(self.button_for(ui.ctx(), "Reload ROM", Command::ReloadRom))
|
||||||
|
.clicked()
|
||||||
|
{
|
||||||
|
self.client
|
||||||
|
.send_command(EmulatorCommand::ReloadRom(self.sim_id));
|
||||||
|
}
|
||||||
|
let watch_rom = self.client.is_rom_watched(self.sim_id);
|
||||||
|
if ui.selectable_button(watch_rom, "Watch ROM").clicked() {
|
||||||
|
self.client
|
||||||
|
.send_command(EmulatorCommand::WatchRom(self.sim_id, !watch_rom));
|
||||||
|
}
|
||||||
if ui
|
if ui
|
||||||
.add(self.button_for(ui.ctx(), "Quit", Command::Quit))
|
.add(self.button_for(ui.ctx(), "Quit", Command::Quit))
|
||||||
.clicked()
|
.clicked()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue