diff --git a/bindings/rustboyadvance-jni/src/emulator.rs b/bindings/rustboyadvance-jni/src/emulator.rs index 5a231e4..efcb877 100644 --- a/bindings/rustboyadvance-jni/src/emulator.rs +++ b/bindings/rustboyadvance-jni/src/emulator.rs @@ -224,14 +224,24 @@ impl EmulatorContext { pub fn native_open_saved_state( env: &JNIEnv, - state: jbyteArray, + bios: jbyteArray, + rom: jbyteArray, + savestate: jbyteArray, renderer_obj: JObject, audio_player: JObject, keypad_obj: JObject, ) -> Result { - let state = env - .convert_byte_array(state) - .map_err(|e| format!("could not get state buffer, error {}", e))?; + let bios = env + .convert_byte_array(savestate) + .map_err(|e| format!("could not get bios buffer, error {}", e))? + .into_boxed_slice(); + let rom = env + .convert_byte_array(savestate) + .map_err(|e| format!("could not get rom buffer, error {}", e))? + .into_boxed_slice(); + let savestate = env + .convert_byte_array(savestate) + .map_err(|e| format!("could not get savestate buffer, error {}", e))?; let renderer = Renderer::new(env, renderer_obj)?; @@ -240,13 +250,13 @@ impl EmulatorContext { audio_producer: None, key_state: 0xffff, })); - let gba = - GameBoyAdvance::from_saved_state(&state, hw.clone(), hw.clone()).map_err(|e| { - format!( - "failed to create GameBoyAdvance from saved state, error {:?}", - e - ) - })?; + let gba = GameBoyAdvance::from_saved_state(&savestate, bios, rom, hw.clone(), hw.clone()) + .map_err(|e| { + format!( + "failed to create GameBoyAdvance from saved savestate, error {:?}", + e + ) + })?; let keypad = Keypad::new(env, keypad_obj); diff --git a/bindings/rustboyadvance-jni/src/lib.rs b/bindings/rustboyadvance-jni/src/lib.rs index 9c7ef52..9a8a955 100644 --- a/bindings/rustboyadvance-jni/src/lib.rs +++ b/bindings/rustboyadvance-jni/src/lib.rs @@ -106,14 +106,18 @@ pub mod bindings { pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_openSavedState( env: JNIEnv, _obj: JClass, - state: jbyteArray, + bios: jbyteArray, + rom: jbyteArray, + savestate: jbyteArray, renderer_obj: JObject, audio_player_obj: JObject, keypad_obj: JObject, ) -> jlong { match EmulatorContext::native_open_saved_state( &env, - state, + bios, + rom, + savestate, renderer_obj, audio_player_obj, keypad_obj, @@ -287,7 +291,6 @@ pub mod bindings { _obj: JClass, ctx: jlong, ) { - let ctx = cast_ctx(ctx); - info!("CPU LOG: {:#x?}", ctx.gba.cpu); + info!("unimplemented") } } diff --git a/platform/android/.idea/gradle.xml b/platform/android/.idea/gradle.xml index 2a80f60..78708b3 100644 --- a/platform/android/.idea/gradle.xml +++ b/platform/android/.idea/gradle.xml @@ -1,15 +1,19 @@ + diff --git a/platform/android/app/src/main/java/com/mrmichel/rustboyadvance/EmulatorBindings.java b/platform/android/app/src/main/java/com/mrmichel/rustboyadvance/EmulatorBindings.java index ff36eb7..a447503 100644 --- a/platform/android/app/src/main/java/com/mrmichel/rustboyadvance/EmulatorBindings.java +++ b/platform/android/app/src/main/java/com/mrmichel/rustboyadvance/EmulatorBindings.java @@ -27,6 +27,8 @@ public class EmulatorBindings { /** * Open a new emulator context from a saved state buffer * + * @param bios bytearray of the GBA bios + * @param rom bytearray of the rom to run * @param savedState saved state buffer * @param renderer renderer instance * @param audioPlayer audio player instance @@ -34,7 +36,7 @@ public class EmulatorBindings { * @return * @throws NativeBindingException */ - public static native long openSavedState(byte[] savedState, IFrameRenderer renderer, IAudioPlayer audioPlayer, Keypad keypad) throws NativeBindingException; + public static native long openSavedState(byte[] bios, byte[] rom, byte[] savedState, IFrameRenderer renderer, IAudioPlayer audioPlayer, Keypad keypad) throws NativeBindingException; /** * Destroys the emulator instance diff --git a/platform/android/app/src/main/java/com/mrmichel/rustdroid_emu/core/Emulator.java b/platform/android/app/src/main/java/com/mrmichel/rustdroid_emu/core/Emulator.java index c670dfb..e169f1d 100644 --- a/platform/android/app/src/main/java/com/mrmichel/rustdroid_emu/core/Emulator.java +++ b/platform/android/app/src/main/java/com/mrmichel/rustdroid_emu/core/Emulator.java @@ -65,11 +65,11 @@ public class Emulator { return EmulatorBindings.saveState(this.ctx); } - public synchronized void loadState(byte[] state) throws EmulatorBindings.NativeBindingException { + public synchronized void loadState(byte[] state) throws EmulatorBindings.NativeBindingException, EmulatorException { if (ctx != -1) { EmulatorBindings.loadState(this.ctx, state); } else { - openSavedState(state); + throw new EmulatorException("Call open() first"); } } @@ -77,8 +77,8 @@ public class Emulator { this.ctx = EmulatorBindings.openEmulator(bios, rom, this.frameRenderer, this.audioPlayer, this.keypad, saveName, skipBios); } - public synchronized void openSavedState(byte[] savedState) throws EmulatorBindings.NativeBindingException { - this.ctx = EmulatorBindings.openSavedState(savedState, this.frameRenderer, this.audioPlayer, this.keypad); + public synchronized void openSavedState(byte[] bios, byte[] rom, byte[] savedState) throws EmulatorBindings.NativeBindingException { + this.ctx = EmulatorBindings.openSavedState(bios, rom, savedState, this.frameRenderer, this.audioPlayer, this.keypad); } public synchronized void close() { diff --git a/platform/android/app/src/main/java/com/mrmichel/rustdroid_emu/ui/EmulatorActivity.java b/platform/android/app/src/main/java/com/mrmichel/rustdroid_emu/ui/EmulatorActivity.java index 09cb024..ad15267 100644 --- a/platform/android/app/src/main/java/com/mrmichel/rustdroid_emu/ui/EmulatorActivity.java +++ b/platform/android/app/src/main/java/com/mrmichel/rustdroid_emu/ui/EmulatorActivity.java @@ -289,6 +289,7 @@ public class EmulatorActivity extends AppCompatActivity implements View.OnClickL fis.close(); outState.putString("saveFile", saveFile.getPath()); + outState.putInt("romId", this.romMetadata.getId()); outState.putBoolean("turbo", false); @@ -334,6 +335,7 @@ public class EmulatorActivity extends AppCompatActivity implements View.OnClickL if (null != savedInstanceState && (saveFilePath = savedInstanceState.getString("saveFile")) != null) { final EmulatorActivity thisActivity = this; + int romId = getIntent().getIntExtra("romId", -1); // busy wait until surface view is ready try { @@ -353,7 +355,18 @@ public class EmulatorActivity extends AppCompatActivity implements View.OnClickL saveFile.delete(); byte[] savedState = outputStream.toByteArray(); - emulator.openSavedState(savedState); + RomManager romManager = RomManager.getInstance(this); + romManager.updateLastPlayed(romId); + this.romMetadata = romManager.getRomMetadata(romId); + + byte[] romData; + try { + romData = Util.readFile(romMetadata.getRomFile()); + } catch (Exception e) { + Util.showAlertDialogAndExit(this, e); + return; + } + emulator.openSavedState(this.bios, romData, savedState); createThreads();