From 063cb4374ad6776ad0f68ecf65cdecf1f304c888 Mon Sep 17 00:00:00 2001 From: Vivian Lim Date: Tue, 24 Dec 2019 14:43:11 -0800 Subject: [PATCH 01/13] WIP comms stuff and trying to make pokemon_rb serializable --- .vscode/launch.json | 4 +- Cargo.lock | 25 +++++++++++ Cargo.toml | 9 ++-- src/comms.rs | 37 ++++++++++++++++ examples/sdl2_emulator.rs => src/emulator.rs | 40 ++--------------- src/main.rs | 45 ++++++++++++++++++++ src/sync/pokemon_rb.rs | 14 ++++++ 7 files changed, 131 insertions(+), 43 deletions(-) create mode 100644 src/comms.rs rename examples/sdl2_emulator.rs => src/emulator.rs (95%) create mode 100644 src/main.rs diff --git a/.vscode/launch.json b/.vscode/launch.json index 39c6af3..9cab8e0 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "sync: pokeblue", "type": "cppvsdbg", "request": "launch", - "program": "target/debug/examples/sdl2_emulator.exe", + "program": "target/debug/ferretro-synced.exe", "args": ["--core", "./data/gambatte_libretro.dll", "--rom", "./data/pokeblue.gb", "--state", "./data/pkblue-route1.sav"], "stopAtEntry": false, "cwd": "${workspaceFolder}", @@ -22,7 +22,7 @@ "name": "sync: sonic2", "type": "cppvsdbg", "request": "launch", - "program": "target/debug/examples/sdl2_emulator.exe", + "program": "target/debug/ferretro-synced.exe", "args": ["--core", "./data/genesis_plus_gx_libretro.dll", "--rom", "./data/sonic2.bin"], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/Cargo.lock b/Cargo.lock index 88fc1e5..a330b24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,6 +151,8 @@ dependencies = [ "num-derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "sdl2 0.32.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_bytes 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -489,6 +491,27 @@ dependencies = [ name = "serde" version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_bytes" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "strsim" @@ -661,6 +684,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum sdl2 0.32.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d051a07231e303f5f719da78cb6f7394f6d5b54f733aef5b0b447804a83edd7b" "checksum sdl2-sys 0.32.6 (registry+https://github.com/rust-lang/crates.io-index)" = "34e71125077d297d57e4c1acfe8981b5bdfbf5a20e7b589abfdcb33bf1127f86" "checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449" +"checksum serde_bytes 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "325a073952621257820e7a3469f55ba4726d8b28657e7e36653d1c36dc2c84ae" +"checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum structopt 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "30b3a3e93f5ad553c38b3301c8a0a0cec829a36783f6a0c467fc4bf553a5f5bf" "checksum structopt-derive 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea692d40005b3ceba90a9fe7a78fa8d4b82b0ce627eebbffc329aab850f3410e" diff --git a/Cargo.toml b/Cargo.toml index 193f90d..25dc4b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,9 +14,10 @@ libloading = "^0.5" num = "0.2" num-derive = "0.3" num-traits = "0.2" - -[dev-dependencies] -# example: sdl2_emulator sdl2 = "^0.32" crossbeam-channel = "^0.4" -structopt = "^0.3" \ No newline at end of file +structopt = "^0.3" +serde = { version = "1.0.104", features = ["derive"] } +serde_bytes = "0.11" + +[dev-dependencies] \ No newline at end of file diff --git a/src/comms.rs b/src/comms.rs new file mode 100644 index 0000000..d1f6e54 --- /dev/null +++ b/src/comms.rs @@ -0,0 +1,37 @@ +use std::path::{Path, PathBuf}; +use std::sync::mpsc::{Sender, Receiver}; +use std::sync::mpsc; +use std::thread; +use serde::{Serialize, Deserialize}; + +pub struct Communication { + tx: Sender, + rx: Receiver, +} + +impl Communication { + pub fn new() -> Box { + let (to_thread, from_main): (Sender, Receiver) = mpsc::channel(); + let (to_main, from_thread): (Sender, Receiver) = mpsc::channel(); + + // let child = thread::spawn(move || { + // let client = WebsocketClient:: { + // thread_tx: to_main, + // thread_rx: from_main, + // }; + + // // todo: loop + // println!("listener thread finished"); + // }); + + Box::new(Communication { + tx: to_thread, + rx: from_thread, + }) + } +} + +struct WebsocketClient { + thread_tx: Sender, + thread_rx: Receiver, +} \ No newline at end of file diff --git a/examples/sdl2_emulator.rs b/src/emulator.rs similarity index 95% rename from examples/sdl2_emulator.rs rename to src/emulator.rs index 8aa6e5b..c79ce63 100644 --- a/examples/sdl2_emulator.rs +++ b/src/emulator.rs @@ -1,7 +1,3 @@ -extern crate crossbeam_channel; -extern crate ferretro; -extern crate sdl2; - use ferretro::retro; use ferretro::retro::ffi::{GameGeometry, SystemInfo, SystemAvInfo, MemoryMap}; use ferretro::retro::constants::{InputIndex, JoypadButton, AnalogAxis, DeviceType}; @@ -13,13 +9,11 @@ use ferretro_synced::sync::pokemon_rb::SyncedPokemonRedBlue; use std::ffi::CStr; use std::io::Read; -use std::path::{Path, PathBuf}; use std::pin::Pin; +use std::path::{Path, PathBuf}; use std::time::{Duration, Instant}; use std::convert::TryFrom; -use structopt::StructOpt; - use sdl2::audio::{AudioCallback, AudioFormat, AudioSpec, AudioSpecDesired, AudioDevice}; use sdl2::controller::{GameController, Button, Axis}; use sdl2::event::Event; @@ -27,7 +21,7 @@ use sdl2::keyboard::Keycode; use sdl2::rect::Rect; use sdl2::render::WindowCanvas; -struct MyEmulator { +pub struct MyEmulator { retro: retro::wrapper::LibretroWrapper, core_path: PathBuf, sys_path: Option, @@ -497,34 +491,6 @@ impl AudioCallback for MySdlAudio { } } -pub fn main() -> failure::Fallible<()> { - let opt: Opt = Opt::from_args(); - let mut emu = MyEmulator::new(&opt.core, &opt.system); - emu.load_game(&opt.rom); - match (&opt.state) { - Some(s) => emu.load_state(s), - None => () - } - emu.run(); - - Ok(()) -} - -#[derive(StructOpt)] -struct Opt { - /// Core module to use. - #[structopt(short, long, parse(from_os_str))] - core: PathBuf, - /// ROM to load using the core. - #[structopt(short, long, parse(from_os_str))] - rom: PathBuf, - /// System directory, often containing BIOS files - #[structopt(short, long, parse(from_os_str))] - system: Option, - /// Serialized game state to load - #[structopt(long, parse(from_os_str))] - state: Option, -} fn button_map(retro_button: &JoypadButton) -> Option