| 1 | package com.rox; | |
| 2 | ||
| 3 | import com.rox.cartridge.Cartridge; | |
| 4 | import com.rox.save.BatterySaveManager; | |
| 5 | import com.rox.save.SavePaths; | |
| 6 | ||
| 7 | import javax.sound.sampled.LineUnavailableException; | |
| 8 | import java.nio.file.Path; | |
| 9 | ||
| 10 | /** | |
| 11 | * One loaded game: its cartridge, battery saver and {@link NES}, running on its own thread from | |
| 12 | * {@link #start} until {@link #close}. The game window and debugger outlive it, so changing ROM is one | |
| 13 | * close then one start - like swapping a cartridge with the power off. | |
| 14 | */ | |
| 15 | final class GameSession { | |
| 16 | /** Builds the {@link NES} for a cartridge. */ | |
| 17 | @FunctionalInterface | |
| 18 | interface NesFactory { | |
| 19 | NES create(Cartridge cartridge) throws LineUnavailableException; | |
| 20 | } | |
| 21 | ||
| 22 | private final Path romPath; | |
| 23 | private final NES nes; | |
| 24 | private final Thread nesThread; | |
| 25 | /** {@code null} unless the cartridge is battery-backed */ | |
| 26 | private final BatterySaveManager batterySaver; | |
| 27 | ||
| 28 | private GameSession(final Path romPath, final NES nes, final Thread nesThread, final BatterySaveManager batterySaver){ | |
| 29 | this.romPath = romPath; | |
| 30 | this.nes = nes; | |
| 31 | this.nesThread = nesThread; | |
| 32 | this.batterySaver = batterySaver; | |
| 33 | } | |
| 34 | ||
| 35 | /** | |
| 36 | * Loads any battery save into {@code cartridge}, builds its {@code NES} and powers it on, on a thread | |
| 37 | * of its own. | |
| 38 | */ | |
| 39 | static GameSession start(final Path romPath, final Cartridge cartridge, final NesFactory nesFactory) | |
| 40 | throws LineUnavailableException { | |
| 41 | //like inserting a cartridge whose battery was already charged - silent, no prompt | |
| 42 | final BatterySaveManager batterySaver; | |
| 43 |
1
1. start : negated conditional → KILLED |
if (cartridge.rom().hasBattery()){ |
| 44 | final Path batterySaveFile = SavePaths.batterySaveFile(romPath); | |
| 45 |
1
1. start : removed call to com/rox/save/BatterySaveManager::loadIfPresent → KILLED |
BatterySaveManager.loadIfPresent(batterySaveFile, cartridge); |
| 46 | batterySaver = BatterySaveManager.start(batterySaveFile, cartridge); | |
| 47 | } else { | |
| 48 | batterySaver = null; | |
| 49 | } | |
| 50 | ||
| 51 | final NES nes; | |
| 52 | try { | |
| 53 | nes = nesFactory.create(cartridge); | |
| 54 | } catch (LineUnavailableException | RuntimeException e){ | |
| 55 |
1
1. start : negated conditional → KILLED |
if (batterySaver != null){ |
| 56 |
1
1. start : removed call to com/rox/save/BatterySaveManager::stop → KILLED |
batterySaver.stop(); |
| 57 | } | |
| 58 | throw e; | |
| 59 | } | |
| 60 | final Thread nesThread = new Thread(nes::powerOn, "nes-" + romPath.getFileName()); | |
| 61 |
1
1. start : removed call to java/lang/Thread::start → TIMED_OUT |
nesThread.start(); |
| 62 |
1
1. start : replaced return value with null for com/rox/GameSession::start → KILLED |
return new GameSession(romPath, nes, nesThread, batterySaver); |
| 63 | } | |
| 64 | ||
| 65 | Path romPath(){ | |
| 66 |
1
1. romPath : replaced return value with null for com/rox/GameSession::romPath → KILLED |
return romPath; |
| 67 | } | |
| 68 | ||
| 69 | NES nes(){ | |
| 70 |
1
1. nes : replaced return value with null for com/rox/GameSession::nes → KILLED |
return nes; |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * Powers off and returns once the emulation thread has ended, then flushes the battery save - only | |
| 75 | * then can nothing write PRG-RAM any more. | |
| 76 | */ | |
| 77 | void close(){ | |
| 78 |
1
1. close : removed call to com/rox/NES::powerOff → TIMED_OUT |
nes.powerOff(); |
| 79 | boolean interrupted = false; | |
| 80 | //retry until nesThread has genuinely ended - an interrupted join() would otherwise return early | |
| 81 |
1
1. close : negated conditional → KILLED |
while (nesThread.isAlive()){ |
| 82 | try { | |
| 83 |
1
1. close : removed call to java/lang/Thread::join → TIMED_OUT |
nesThread.join(); |
| 84 | } catch (InterruptedException e){ | |
| 85 | interrupted = true; | |
| 86 | } | |
| 87 | } | |
| 88 |
1
1. close : negated conditional → KILLED |
if (batterySaver != null){ |
| 89 |
1
1. close : removed call to com/rox/save/BatterySaveManager::stop → KILLED |
batterySaver.stop(); |
| 90 | } | |
| 91 |
1
1. close : negated conditional → KILLED |
if (interrupted){ |
| 92 |
1
1. close : removed call to java/lang/Thread::interrupt → KILLED |
Thread.currentThread().interrupt(); |
| 93 | } | |
| 94 | } | |
| 95 | } | |
Mutations | ||
| 43 |
1.1 |
|
| 45 |
1.1 |
|
| 55 |
1.1 |
|
| 56 |
1.1 |
|
| 61 |
1.1 |
|
| 62 |
1.1 |
|
| 66 |
1.1 |
|
| 70 |
1.1 |
|
| 78 |
1.1 |
|
| 81 |
1.1 |
|
| 83 |
1.1 |
|
| 88 |
1.1 |
|
| 89 |
1.1 |
|
| 91 |
1.1 |
|
| 92 |
1.1 |