sync and debounce working good

This commit is contained in:
Vivian Lim 2020-03-08 21:30:41 -07:00
parent 6d66f5e80b
commit ccc3f16491
5 changed files with 59 additions and 24 deletions

View File

@ -5,6 +5,7 @@ use ferretro::retro::wrapped_types::{ControllerDescription2, InputDescriptor2, I
use ferretro::retro::wrapper::LibretroWrapper;
use crate::sync::game::{SyncableGame, KnownGames, read_game_name, create_game};
use crate::emulator::libretro_memory_map::LibRetroMemoryMap;
use std::ffi::CStr;
use std::io::Read;
@ -47,7 +48,7 @@ pub struct MyEmulator {
gamepads: Vec<GameController>,
pressed_keys: Vec<Keycode>,
pub memory_map: Option<MemoryMap>,
pub memory_map: Option<LibRetroMemoryMap>,
synced_game: Option<Box<dyn SyncableGame>>,
}
@ -175,7 +176,7 @@ impl MyEmulator {
match g.handle_inbound_messages() {
Ok(_) => (),
Err(e) => {
println!("Error while handling inbound messages: {:?}", e);
println!("Error while handling inbound messages");
}
}
},
@ -431,8 +432,8 @@ impl retro::wrapper::Handler for MyEmulator {
eprint!("[{:?}] {}", level, msg);
}
fn set_memory_maps(&mut self, memory_map: MemoryMap) -> bool {
self.memory_map = Some(memory_map);
fn set_memory_maps(&mut self, ffi_memory_map: MemoryMap) -> bool {
self.memory_map = Some(LibRetroMemoryMap::create(&ffi_memory_map));
true
}
}

View File

@ -18,8 +18,7 @@ pub fn main() -> failure::Fallible<()> {
emu.load_game(&opt.rom);
// memory map should be ready after init & loading game.
let ffi_memory_map = emu.memory_map.as_ref().expect("Memory map was not set by emulator core, cannot continue.");
let memory_map = LibRetroMemoryMap::create(ffi_memory_map);
let memory_map = emu.memory_map.as_ref().expect("Memory map was not set by emulator core, cannot continue.");
match (&opt.state) {
Some(s) => emu.load_state(s),

View File

@ -1,5 +1,5 @@
use std::path::{Path, PathBuf};
use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc::{Sender, Receiver, TryRecvError};
use std::sync::mpsc;
use std::thread;
use std::thread::JoinHandle;
@ -125,7 +125,16 @@ impl<T> Communication<T> where T: std::marker::Send, T: Serialize, T: Deserializ
Ok(())
}
pub fn try_recv(&mut self) -> Result<T, std::sync::mpsc::TryRecvError> {
self.rx.try_recv()
pub fn receive_if_any(&mut self) -> Option<T> {
match self.rx.try_recv() {
Err(e) => match e {
TryRecvError::Empty => None,
TryRecvError::Disconnected => {
println!("Disconnected when trying to receive.");
None
}
}
Ok(msg) => Some(msg)
}
}
}

View File

@ -21,6 +21,42 @@ impl MemorySliceHandle<'_> {
}
}
pub fn write_to_slice(&mut self, data: Vec<u8>) -> Result<(), failure::Error> {
match &mut self.slice {
Some(slice) => {
if data.len() != slice.len() {
return Err(failure::err_msg("message size and slice size differ."));
}
// if last_read_value is set, update it as the same time that we write back to the emulator.
match &mut self.last_read_value {
None => {
let mut index: usize = 0;
for value in data.into_iter(){
slice[index] = value;
index += 1;
}
},
Some(last_read_value) => {
let mut index: usize = 0;
for value in data.into_iter(){
slice[index] = value;
last_read_value[index] = value;
index += 1;
}
}
}
Ok(())
},
None => {
println!("slice doesn't exist to write to.");
Ok(())
}
}
}
pub fn get_copy_if_changed_since_last_read(&mut self) -> Option<Vec<u8>> {
match &mut self.slice {
None => None,

View File

@ -61,8 +61,10 @@ impl SyncableGame for SyncedPokemonRedBlue {
}
fn handle_inbound_messages(&mut self) -> Result<(), failure::Error>{
let in_msg = self.comms.try_recv()?;
self.handle_message(in_msg)
match self.comms.receive_if_any() {
Some(msg) => self.handle_message(msg),
None => Ok(())
}
}
}
@ -103,19 +105,7 @@ impl SyncedPokemonRedBlue {
match msg.active_pkmn {
Some(src) => {
println!("message contains a new active pokemon. data: {:?}", &src);
let target_slice = self.memory_handles.active_pokemon.slice.as_mut().ok_or(failure::err_msg("can't write message back to None memory slice"))?;
if src.len() != target_slice.len() {
return Err(failure::err_msg("message size and slice size differ."));
}
let mut index: usize = 0;
for value in src.into_iter(){
target_slice[index] = value;
index += 1;
}
Ok(())
self.memory_handles.active_pokemon.write_to_slice(src)
}
None => Ok(())
}