summaryrefslogtreecommitdiff
path: root/src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java
diff options
context:
space:
mode:
authorLoren <47842483+sornerol@users.noreply.github.com>2022-07-06 11:42:40 -0500
committerrafa_99 <raroma09@gmail.com>2022-07-10 21:24:13 +0100
commitb8f4accb0da92aa786c0da1d22fcf307aa11d618 (patch)
treeef13df016ed93fc0a9837110fea1816f29628a30 /src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java
parentc96c5028316877a8a764da7fdba4b56904e393d5 (diff)
Batch randomization (#477)
Diffstat (limited to 'src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java')
-rw-r--r--src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java162
1 files changed, 133 insertions, 29 deletions
diff --git a/src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java b/src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java
index 40c1313..82ca2c2 100644
--- a/src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java
+++ b/src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java
@@ -326,6 +326,7 @@ public class NewRandomizerGUI {
private JMenuItem customNamesEditorMenuItem;
private JMenuItem loadGetSettingsMenuItem;
private JMenuItem keepOrUnloadGameAfterRandomizingMenuItem;
+ private JMenuItem batchRandomizationMenuItem;
private ImageIcon emptyIcon = new ImageIcon(getClass().getResource("/com/sneed/pkrandom/newgui/emptyIcon.png"));
private boolean haveCheckedCustomNames, unloadGameOnSuccess;
@@ -336,6 +337,8 @@ public class NewRandomizerGUI {
private final int TRAINER_UNCHANGED = 0, TRAINER_RANDOM = 1, TRAINER_RANDOM_EVEN = 2, TRAINER_RANDOM_EVEN_MAIN = 3,
TRAINER_TYPE_THEMED = 4, TRAINER_TYPE_THEMED_ELITE4_GYMS = 5;
+ private BatchRandomizationSettings batchRandomizationSettings;
+
public NewRandomizerGUI() {
ToolTipManager.sharedInstance().setInitialDelay(400);
ToolTipManager.sharedInstance().setDismissDelay(Integer.MAX_VALUE);
@@ -520,6 +523,7 @@ public class NewRandomizerGUI {
enableOrDisableSubControls();
}
});
+ batchRandomizationMenuItem.addActionListener(e -> batchRandomizationSettingsDialog());
}
private void showInitialPopup() {
@@ -640,6 +644,10 @@ public class NewRandomizerGUI {
keepOrUnloadGameAfterRandomizingMenuItem.setText(bundle.getString("GUI.unloadGameAfterRandomizingMenuItem.text"));
}
settingsMenu.add(keepOrUnloadGameAfterRandomizingMenuItem);
+
+ batchRandomizationMenuItem = new JMenuItem();
+ batchRandomizationMenuItem.setText(bundle.getString("GUI.batchRandomizationMenuItem.text"));
+ settingsMenu.add(batchRandomizationMenuItem);
}
private void loadROM() {
@@ -723,6 +731,10 @@ public class NewRandomizerGUI {
if (romHandler == null) {
return; // none loaded
}
+ if (raceModeCheckBox.isSelected() && batchRandomizationSettings.isBatchRandomizationEnabled()) {
+ JOptionPane.showMessageDialog(frame, bundle.getString("GUI.batchRandomizationRequirements"));
+ return;
+ }
if (raceModeCheckBox.isSelected() && isTrainerSetting(TRAINER_UNCHANGED) && wpUnchangedRadioButton.isSelected()) {
JOptionPane.showMessageDialog(frame, bundle.getString("GUI.raceModeRequirements"));
return;
@@ -736,7 +748,10 @@ public class NewRandomizerGUI {
romSaveChooser.setSelectedFile(null);
boolean allowed = false;
File fh = null;
- if (outputType == SaveType.FILE) {
+ if (batchRandomizationSettings.isBatchRandomizationEnabled() && outputType != SaveType.INVALID) {
+ allowed = true;
+ }
+ else if (outputType == SaveType.FILE) {
romSaveChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = romSaveChooser.showSaveDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
@@ -764,19 +779,78 @@ public class NewRandomizerGUI {
}
if (allowed && fh != null) {
- // Get a seed
- long seed = RandomSource.pickSeed();
- // Apply it
- RandomSource.seed(seed);
- presetMode = false;
+ saveRandomizedRom(outputType, fh);
+ } else if (allowed && batchRandomizationSettings.isBatchRandomizationEnabled()) {
+ int numberOfRandomizedROMs = batchRandomizationSettings.getNumberOfRandomizedROMs();
+ int startingIndex = batchRandomizationSettings.getStartingIndex();
+ int endingIndex = startingIndex + numberOfRandomizedROMs;
+ final String progressTemplate = bundle.getString("GUI.batchRandomizationProgress");
+ OperationDialog batchProgressDialog = new OperationDialog(String.format(progressTemplate, 0, numberOfRandomizedROMs), frame, true);
+ SwingWorker swingWorker = new SwingWorker<Void, Void>() {
+ int i;
- try {
- CustomNamesSet cns = FileFunctions.getCustomNames();
- performRandomization(fh.getAbsolutePath(), seed, cns, outputType == SaveType.DIRECTORY);
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(frame, bundle.getString("GUI.cantLoadCustomNames"));
- }
+ @Override
+ protected Void doInBackground() {
+ frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
+ SwingUtilities.invokeLater(() -> batchProgressDialog.setVisible(true));
+ for (i = startingIndex; i < endingIndex; i++) {
+ String fileName = batchRandomizationSettings.getOutputDirectory() +
+ File.separator +
+ batchRandomizationSettings.getFileNamePrefix() +
+ i;
+ if (outputType == SaveType.FILE) {
+ fileName += '.' + romHandler.getDefaultExtension();
+ }
+ File rom = new File(fileName);
+ if (outputType == SaveType.DIRECTORY) {
+ rom.mkdirs();
+ }
+ int currentRomNumber = i - startingIndex + 1;
+
+ SwingUtilities.invokeLater(
+ () -> batchProgressDialog.setLoadingLabelText(String.format(progressTemplate,
+ currentRomNumber,
+ numberOfRandomizedROMs))
+ );
+ saveRandomizedRom(outputType, rom);
+ }
+ return null;
+ }
+
+ @Override
+ protected void done() {
+ super.done();
+ if (batchRandomizationSettings.shouldAutoAdvanceStartingIndex()) {
+ batchRandomizationSettings.setStartingIndex(i);
+ attemptWriteConfig();
+ }
+ SwingUtilities.invokeLater(() -> batchProgressDialog.setVisible(false));
+ JOptionPane.showMessageDialog(frame, bundle.getString("GUI.randomizationDone"));
+ if (unloadGameOnSuccess) {
+ romHandler = null;
+ initialState();
+ } else {
+ reinitializeRomHandler(false);
+ }
+ frame.setCursor(null);
+ }
+ };
+ swingWorker.execute();
+ }
+ }
+ private void saveRandomizedRom(SaveType outputType, File fh) {
+ // Get a seed
+ long seed = RandomSource.pickSeed();
+ // Apply it
+ RandomSource.seed(seed);
+ presetMode = false;
+
+ try {
+ CustomNamesSet cns = FileFunctions.getCustomNames();
+ performRandomization(fh.getAbsolutePath(), seed, cns, outputType == SaveType.DIRECTORY);
+ } catch (IOException ex) {
+ JOptionPane.showMessageDialog(frame, bundle.getString("GUI.cantLoadCustomNames"));
}
}
@@ -848,6 +922,7 @@ public class NewRandomizerGUI {
private void performRandomization(final String filename, final long seed, CustomNamesSet customNames, boolean saveAsDirectory) {
final Settings settings = createSettingsFromState(customNames);
final boolean raceMode = settings.isRaceMode();
+ final boolean batchRandomization = batchRandomizationSettings.isBatchRandomizationEnabled() && !presetMode;
// Setup verbose log
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream log;
@@ -863,7 +938,7 @@ public class NewRandomizerGUI {
final AtomicInteger finishedCV = new AtomicInteger(0);
opDialog = new OperationDialog(bundle.getString("GUI.savingText"), frame, true);
Thread t = new Thread(() -> {
- SwingUtilities.invokeLater(() -> opDialog.setVisible(true));
+ SwingUtilities.invokeLater(() -> opDialog.setVisible(!batchRandomization));
boolean succeededSave = false;
try {
romHandler.setLog(verboseLog);
@@ -893,19 +968,22 @@ public class NewRandomizerGUI {
JOptionPane.showMessageDialog(frame,
String.format(bundle.getString("GUI.raceModeCheckValuePopup"),
finishedCV.get()));
- } else {
+ } else if (batchRandomization && batchRandomizationSettings.shouldGenerateLogFile()) {
+ try {
+ saveLogFile(filename, out);
+ } catch (IOException e) {
+ JOptionPane.showMessageDialog(frame,
+ bundle.getString("GUI.logSaveFailed"));
+ return;
+ }
+ } else if (!batchRandomization) {
int response = JOptionPane.showConfirmDialog(frame,
bundle.getString("GUI.saveLogDialog.text"),
bundle.getString("GUI.saveLogDialog.title"),
JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.YES_OPTION) {
try {
- FileOutputStream fos = new FileOutputStream(filename + ".log");
- fos.write(0xEF);
- fos.write(0xBB);
- fos.write(0xBF);
- fos.write(out);
- fos.close();
+ saveLogFile(filename, out);
} catch (IOException e) {
JOptionPane.showMessageDialog(frame,
bundle.getString("GUI.logSaveFailed"));
@@ -923,9 +1001,9 @@ public class NewRandomizerGUI {
romHandler = null;
initialState();
} else {
- reinitializeRomHandler();
+ reinitializeRomHandler(false);
}
- } else {
+ } else if (!batchRandomization) {
// Compile a config string
try {
String configString = getCurrentSettings().toString();
@@ -941,7 +1019,7 @@ public class NewRandomizerGUI {
romHandler = null;
initialState();
} else {
- reinitializeRomHandler();
+ reinitializeRomHandler(false);
}
}
});
@@ -954,6 +1032,10 @@ public class NewRandomizerGUI {
}
});
t.start();
+ if (batchRandomization) {
+ t.join();
+ reinitializeRomHandler(true);
+ }
} catch (Exception ex) {
attemptToLogException(ex, "GUI.saveFailed", "GUI.saveFailedNoLog", settings.toString(), Long.toString(seed));
if (verboseLog != null) {
@@ -962,6 +1044,15 @@ public class NewRandomizerGUI {
}
}
+ private void saveLogFile(String filename, byte[] out) throws IOException {
+ FileOutputStream fos = new FileOutputStream(filename + ".log");
+ fos.write(0xEF);
+ fos.write(0xBB);
+ fos.write(0xBF);
+ fos.write(out);
+ fos.close();
+ }
+
private void presetLoader() {
PresetLoadDialog pld = new PresetLoadDialog(this,frame);
if (pld.isCompleted()) {
@@ -1209,17 +1300,24 @@ public class NewRandomizerGUI {
JOptionPane.showMessageDialog(frame, messages);
}
- // This is only intended to be used with the "Keep Game Loaded After Randomizing" setting; it assumes that
- // the game has already been loaded once, and we just need to reload the same game to reinitialize the
- // RomHandler. Don't use this for other purposes unless you know what you're doing.
- private void reinitializeRomHandler() {
+ private void batchRandomizationSettingsDialog() {
+ BatchRandomizationSettingsDialog dlg = new BatchRandomizationSettingsDialog(frame, batchRandomizationSettings);
+ batchRandomizationSettings = dlg.getCurrentSettings();
+ attemptWriteConfig();
+ }
+
+ // This is only intended to be used with the "Keep Game Loaded After Randomizing" setting or between randomization
+ // iterations when batch randomization is enabled. It assumes that the game has already been loaded once, and we just need
+ // to reload the same game to reinitialize the RomHandler. Don't use this for other purposes unless you know what
+ // you're doing.
+ private void reinitializeRomHandler(boolean batchRandomization) {
String currentFN = this.romHandler.loadedFilename();
for (RomHandler.Factory rhf : checkHandlers) {
if (rhf.isLoadable(currentFN)) {
this.romHandler = rhf.create(RandomSource.instance());
opDialog = new OperationDialog(bundle.getString("GUI.loadingText"), frame, true);
Thread t = new Thread(() -> {
- SwingUtilities.invokeLater(() -> opDialog.setVisible(true));
+ SwingUtilities.invokeLater(() -> opDialog.setVisible(!batchRandomization));
try {
this.romHandler.loadRom(currentFN);
if (gameUpdates.containsKey(this.romHandler.getROMCode())) {
@@ -1233,7 +1331,13 @@ public class NewRandomizerGUI {
});
});
t.start();
-
+ if (batchRandomization) {
+ try {
+ t.join();
+ } catch(InterruptedException ex) {
+ attemptToLogException(ex, "GUI.loadFailed", "GUI.loadFailedNoLog", null, null);
+ }
+ }
return;
}
}