fix: various fixes for saved states handling

This commit is contained in:
lantzelot-swe 2021-11-11 21:49:18 +01:00
parent 7b044e96b2
commit 04ff9b9262
7 changed files with 259 additions and 161 deletions

View File

@ -768,7 +768,7 @@ public class MenuManager
{ {
savedStatesManager.setImportDirectory(importSavedStatesDialog.getTargetDirectory()); savedStatesManager.setImportDirectory(importSavedStatesDialog.getTargetDirectory());
savedStatesManager.setImportOverwrite(importSavedStatesDialog.isImportOverwrite()); savedStatesManager.setImportOverwrite(importSavedStatesDialog.isImportOverwrite());
ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Import saved states"); ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Import saved states", true);
ImportSavedStatesWorker worker = new ImportSavedStatesWorker(savedStatesManager, dialog); ImportSavedStatesWorker worker = new ImportSavedStatesWorker(savedStatesManager, dialog);
worker.execute(); worker.execute();
dialog.setVisible(true); dialog.setVisible(true);
@ -784,7 +784,7 @@ public class MenuManager
{ {
savedStatesManager.setExportDirectory(exportSavedStatesDialog.getTargetDirectory()); savedStatesManager.setExportDirectory(exportSavedStatesDialog.getTargetDirectory());
savedStatesManager.setExportOverwrite(exportSavedStatesDialog.isExportOverwrite()); savedStatesManager.setExportOverwrite(exportSavedStatesDialog.isExportOverwrite());
ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Export saved states"); ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Export saved states", false);
ExportSavedStatesWorker worker = new ExportSavedStatesWorker(savedStatesManager, dialog); ExportSavedStatesWorker worker = new ExportSavedStatesWorker(savedStatesManager, dialog);
worker.execute(); worker.execute();
dialog.setVisible(true); dialog.setVisible(true);
@ -806,7 +806,7 @@ public class MenuManager
exportManager.setGameViewsToExport(viewList); exportManager.setGameViewsToExport(viewList);
exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(), exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(),
exportSelectionDialog.deleteBeforeExport()); exportSelectionDialog.deleteBeforeExport());
ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Export games"); ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Export games", false);
ExportWorker worker = new ExportWorker(exportManager, dialog); ExportWorker worker = new ExportWorker(exportManager, dialog);
worker.execute(); worker.execute();
dialog.setVisible(true); dialog.setVisible(true);
@ -820,7 +820,7 @@ public class MenuManager
exportManager.setGamesToExport(gamesList); exportManager.setGamesToExport(gamesList);
exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(), exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(),
exportSelectionDialog.deleteBeforeExport()); exportSelectionDialog.deleteBeforeExport());
ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Export games"); ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Export games", false);
ExportWorker worker = new ExportWorker(exportManager, dialog); ExportWorker worker = new ExportWorker(exportManager, dialog);
worker.execute(); worker.execute();
dialog.setVisible(true); dialog.setVisible(true);
@ -844,7 +844,7 @@ public class MenuManager
exportManager.setGameViewsToExport(viewList); exportManager.setGameViewsToExport(viewList);
exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(), exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(),
exportSelectionDialog.deleteBeforeExport()); exportSelectionDialog.deleteBeforeExport());
ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Export games"); ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Export games", false);
ExportFileLoaderWorker worker = new ExportFileLoaderWorker(exportManager, dialog); ExportFileLoaderWorker worker = new ExportFileLoaderWorker(exportManager, dialog);
worker.execute(); worker.execute();
dialog.setVisible(true); dialog.setVisible(true);
@ -858,7 +858,7 @@ public class MenuManager
exportManager.setGamesToExport(gamesList); exportManager.setGamesToExport(gamesList);
exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(), exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(),
exportSelectionDialog.deleteBeforeExport()); exportSelectionDialog.deleteBeforeExport());
ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Export games"); ImportExportProgressDialog dialog = new ImportExportProgressDialog(this.mainWindow, "Export games", false);
ExportFileLoaderWorker worker = new ExportFileLoaderWorker(exportManager, dialog); ExportFileLoaderWorker worker = new ExportFileLoaderWorker(exportManager, dialog);
worker.execute(); worker.execute();
dialog.setVisible(true); dialog.setVisible(true);

View File

@ -30,6 +30,8 @@ import se.lantz.util.FileManager;
public class SelectDirPanel extends JPanel public class SelectDirPanel extends JPanel
{ {
private static final String THEC64SAVE = ".THEC64SAVE";
public enum Mode public enum Mode
{ {
CAROUSEL_IMPORT, GB_IMPORT, CAROUSEL_EXPORT, FILELOADER_EXPORT, SAVEDSTATES_IMPORT, SAVEDSTATES_EXPORT CAROUSEL_IMPORT, GB_IMPORT, CAROUSEL_EXPORT, FILELOADER_EXPORT, SAVEDSTATES_IMPORT, SAVEDSTATES_EXPORT
@ -276,13 +278,27 @@ public class SelectDirPanel extends JPanel
private void selectSavedStatesImportDirectory() private void selectSavedStatesImportDirectory()
{ {
final JFileChooser fileChooser = new JFileChooser(); final JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Select the \".THEC64SAVE\" directory to import from"); fileChooser.setDialogTitle("Select the \"" + THEC64SAVE + "\" directory to import from");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setCurrentDirectory(new File(configuredDir)); fileChooser.setCurrentDirectory(new File(configuredDir));
int value = fileChooser.showDialog(this, "OK"); int value = fileChooser.showDialog(this, "OK");
if (value == JFileChooser.APPROVE_OPTION) if (value == JFileChooser.APPROVE_OPTION)
{ {
targetDirectory = fileChooser.getSelectedFile(); targetDirectory = fileChooser.getSelectedFile();
if (!targetDirectory.getName().toUpperCase().equals(THEC64SAVE))
{
String message = "<html>You have not selected a \"" + THEC64SAVE +
"\" directory.<br> Are you sure you want to import from the selected directory?</html>";
int choice = JOptionPane.showConfirmDialog(SwingUtilities.getAncestorOfClass(JDialog.class, this),
message,
"Folder name",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (choice == JOptionPane.NO_OPTION)
{
selectSavedStatesImportDirectory();
}
}
configuredDir = targetDirectory.toPath().toString(); configuredDir = targetDirectory.toPath().toString();
FileManager.getConfiguredProperties().put(SAVEDSTATES_IMPORT_DIR_PROPERTY, configuredDir); FileManager.getConfiguredProperties().put(SAVEDSTATES_IMPORT_DIR_PROPERTY, configuredDir);
getDirTextField().setText(configuredDir); getDirTextField().setText(configuredDir);
@ -301,17 +317,17 @@ public class SelectDirPanel extends JPanel
return dlg; return dlg;
} }
}; };
fileChooser.setDialogTitle("Select the \".THEC64SAVE\" directory on your PCUAE USB stick"); fileChooser.setDialogTitle("Select the \"" + THEC64SAVE + "\" directory on your PCUAE USB stick");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setCurrentDirectory(new File(configuredDir)); fileChooser.setCurrentDirectory(new File(configuredDir));
int value = fileChooser.showDialog(this, "OK"); int value = fileChooser.showDialog(this, "OK");
if (value == JFileChooser.APPROVE_OPTION) if (value == JFileChooser.APPROVE_OPTION)
{ {
targetDirectory = fileChooser.getSelectedFile(); targetDirectory = fileChooser.getSelectedFile();
if (!targetDirectory.getName().toUpperCase().equals(".THEC64SAVE")) if (!targetDirectory.getName().toUpperCase().equals(THEC64SAVE))
{ {
String message = String message = "<html>You have not selected a \"" + THEC64SAVE +
"<html>You have not selected the \".THEC64SAVE\" directory.<br> Are you sure you want to export to the selected directory?</html>"; "\" directory.<br> Are you sure you want to export to the selected directory?</html>";
int choice = JOptionPane.showConfirmDialog(SwingUtilities.getAncestorOfClass(JDialog.class, this), int choice = JOptionPane.showConfirmDialog(SwingUtilities.getAncestorOfClass(JDialog.class, this),
message, message,
"Folder name", "Folder name",

View File

@ -1,18 +1,17 @@
package se.lantz.gui.exports; package se.lantz.gui.exports;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.Dimension;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import se.lantz.gui.SelectDirPanel; import java.awt.GridBagLayout;
import se.lantz.gui.SelectDirPanel.Mode;
import java.awt.Insets; import java.awt.Insets;
import java.io.File; import java.io.File;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup; import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import se.lantz.gui.SelectDirPanel;
import se.lantz.gui.SelectDirPanel.Mode;
public class ExportSavedStatesPanel extends JPanel public class ExportSavedStatesPanel extends JPanel
{ {
@ -23,7 +22,9 @@ public class ExportSavedStatesPanel extends JPanel
private JRadioButton overwriteRadioButton; private JRadioButton overwriteRadioButton;
private JRadioButton keepRadioButton; private JRadioButton keepRadioButton;
private final ButtonGroup buttonGroup = new ButtonGroup(); private final ButtonGroup buttonGroup = new ButtonGroup();
public ExportSavedStatesPanel() {
public ExportSavedStatesPanel()
{
GridBagLayout gridBagLayout = new GridBagLayout(); GridBagLayout gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout); setLayout(gridBagLayout);
GridBagConstraints gbc_infoLabel = new GridBagConstraints(); GridBagConstraints gbc_infoLabel = new GridBagConstraints();
@ -53,63 +54,80 @@ public class ExportSavedStatesPanel extends JPanel
add(getChoicePanel(), gbc_choicePanel); add(getChoicePanel(), gbc_choicePanel);
} }
private JLabel getInfoLabel() { private JLabel getInfoLabel()
if (infoLabel == null) { {
infoLabel = new JLabel("Select the \".THEC64SAVE\" folder in the root of your PCUAE USB stick:"); if (infoLabel == null)
{
infoLabel = new JLabel("Select the \".THEC64SAVE\" folder in the root of your PCUAE USB stick:");
} }
return infoLabel; return infoLabel;
} }
private SelectDirPanel getSelectDirPanel() {
if (selectDirPanel == null) { private SelectDirPanel getSelectDirPanel()
selectDirPanel = new SelectDirPanel(Mode.SAVEDSTATES_EXPORT); {
if (selectDirPanel == null)
{
selectDirPanel = new SelectDirPanel(Mode.SAVEDSTATES_EXPORT);
} }
return selectDirPanel; return selectDirPanel;
} }
private JLabel getChoiceLabel() {
if (choiceLabel == null) { private JLabel getChoiceLabel()
choiceLabel = new JLabel("Select how to handle already available saved states in the export folder:"); {
if (choiceLabel == null)
{
choiceLabel = new JLabel("Select how to handle already available saved states in the export folder:");
} }
return choiceLabel; return choiceLabel;
} }
private JPanel getChoicePanel() {
if (choicePanel == null) { private JPanel getChoicePanel()
choicePanel = new JPanel(); {
GridBagLayout gbl_choicePanel = new GridBagLayout(); if (choicePanel == null)
choicePanel.setLayout(gbl_choicePanel); {
GridBagConstraints gbc_overwriteRadioButton = new GridBagConstraints(); choicePanel = new JPanel();
gbc_overwriteRadioButton.anchor = GridBagConstraints.WEST; GridBagLayout gbl_choicePanel = new GridBagLayout();
gbc_overwriteRadioButton.gridx = 0; choicePanel.setLayout(gbl_choicePanel);
gbc_overwriteRadioButton.gridy = 0; GridBagConstraints gbc_overwriteRadioButton = new GridBagConstraints();
choicePanel.add(getOverwriteRadioButton(), gbc_overwriteRadioButton); gbc_overwriteRadioButton.anchor = GridBagConstraints.WEST;
GridBagConstraints gbc_keepRadioButton = new GridBagConstraints(); gbc_overwriteRadioButton.gridx = 0;
gbc_keepRadioButton.anchor = GridBagConstraints.WEST; gbc_overwriteRadioButton.gridy = 0;
gbc_keepRadioButton.gridx = 0; choicePanel.add(getOverwriteRadioButton(), gbc_overwriteRadioButton);
gbc_keepRadioButton.gridy = 1; GridBagConstraints gbc_keepRadioButton = new GridBagConstraints();
choicePanel.add(getKeepRadioButton(), gbc_keepRadioButton); gbc_keepRadioButton.anchor = GridBagConstraints.WEST;
gbc_keepRadioButton.gridx = 0;
gbc_keepRadioButton.gridy = 1;
choicePanel.add(getKeepRadioButton(), gbc_keepRadioButton);
} }
return choicePanel; return choicePanel;
} }
private JRadioButton getOverwriteRadioButton() {
if (overwriteRadioButton == null) { private JRadioButton getOverwriteRadioButton()
overwriteRadioButton = new JRadioButton("Overwrite"); {
buttonGroup.add(overwriteRadioButton); if (overwriteRadioButton == null)
overwriteRadioButton.setSelected(true); {
overwriteRadioButton = new JRadioButton("Overwrite");
buttonGroup.add(overwriteRadioButton);
overwriteRadioButton.setSelected(true);
} }
return overwriteRadioButton; return overwriteRadioButton;
} }
private JRadioButton getKeepRadioButton() {
if (keepRadioButton == null) { private JRadioButton getKeepRadioButton()
keepRadioButton = new JRadioButton("Skip from export\r\n"); {
buttonGroup.add(keepRadioButton); if (keepRadioButton == null)
{
keepRadioButton = new JRadioButton("Skip from export\r\n");
buttonGroup.add(keepRadioButton);
} }
return keepRadioButton; return keepRadioButton;
} }
File getTargetDirectory() File getTargetDirectory()
{ {
return getSelectDirPanel().getTargetDirectory(); return getSelectDirPanel().getTargetDirectory();
} }
boolean isExportOverwrite() boolean isExportOverwrite()
{ {
return getOverwriteRadioButton().isSelected(); return getOverwriteRadioButton().isSelected();

View File

@ -11,9 +11,12 @@ public class ImportExportProgressDialog extends JDialog
private ImportExportProgressPanel panel; private ImportExportProgressPanel panel;
public ImportExportProgressDialog(Frame frame, String title) private final boolean isImport;
public ImportExportProgressDialog(Frame frame, String title, boolean isImport)
{ {
super(frame, title, true); super(frame, title, true);
this.isImport = isImport;
this.add(getExportProgressPanel()); this.add(getExportProgressPanel());
setSize(900, 600); setSize(900, 600);
setLocationRelativeTo(frame); setLocationRelativeTo(frame);
@ -28,7 +31,7 @@ public class ImportExportProgressDialog extends JDialog
public void finish() public void finish()
{ {
getExportProgressPanel().finish(); getExportProgressPanel().finish(isImport);
} }
public ImportExportProgressPanel getExportProgressPanel() public ImportExportProgressPanel getExportProgressPanel()

View File

@ -88,7 +88,7 @@ public class ImportExportProgressPanel extends JPanel
getTextArea().append(infoText); getTextArea().append(infoText);
} }
public void finish() public void finish(boolean isImport)
{ {
getCloseButton().setEnabled(true); getCloseButton().setEnabled(true);
getProgressBar().setIndeterminate(false); getProgressBar().setIndeterminate(false);
@ -96,13 +96,21 @@ public class ImportExportProgressPanel extends JPanel
//Check for errors //Check for errors
String text = getTextArea().getText(); String text = getTextArea().getText();
int count = text.length() - text.replace("ERROR:", "").length(); int count = text.length() - text.replace("ERROR:", "").length();
if (count > 0) if (isImport)
{ {
getTextArea().append("\nExport ended with " + count/6 + " errors. See pcusb.log for details."); getTextArea().append("\nImport ");
} }
else else
{ {
getTextArea().append("\nExport completed successfully."); getTextArea().append("\nExport ");
}
if (count > 0)
{
getTextArea().append("ended with " + count/6 + " errors. See pcusb.log for details.");
}
else
{
getTextArea().append("completed successfully.");
} }
} }
} }

View File

@ -1,18 +1,17 @@
package se.lantz.gui.imports; package se.lantz.gui.imports;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.Dimension;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import se.lantz.gui.SelectDirPanel; import java.awt.GridBagLayout;
import se.lantz.gui.SelectDirPanel.Mode;
import java.awt.Insets; import java.awt.Insets;
import java.io.File; import java.io.File;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup; import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import se.lantz.gui.SelectDirPanel;
import se.lantz.gui.SelectDirPanel.Mode;
public class ImportSavedStatesPanel extends JPanel public class ImportSavedStatesPanel extends JPanel
{ {
@ -24,7 +23,9 @@ public class ImportSavedStatesPanel extends JPanel
private JRadioButton keepRadioButton; private JRadioButton keepRadioButton;
private JLabel info2Label; private JLabel info2Label;
private final ButtonGroup buttonGroup = new ButtonGroup(); private final ButtonGroup buttonGroup = new ButtonGroup();
public ImportSavedStatesPanel() {
public ImportSavedStatesPanel()
{
GridBagLayout gridBagLayout = new GridBagLayout(); GridBagLayout gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout); setLayout(gridBagLayout);
GridBagConstraints gbc_infoLabel = new GridBagConstraints(); GridBagConstraints gbc_infoLabel = new GridBagConstraints();
@ -60,69 +61,90 @@ public class ImportSavedStatesPanel extends JPanel
add(getInfo2Label(), gbc_info2Label); add(getInfo2Label(), gbc_info2Label);
} }
private JLabel getInfoLabel() { private JLabel getInfoLabel()
if (infoLabel == null) { {
infoLabel = new JLabel("Select the \".THEC64SAVE\" folder in the root of your PCUAE USB stick:"); if (infoLabel == null)
{
infoLabel = new JLabel("Select the \".THEC64SAVE\" folder in the root of your PCUAE USB stick:");
} }
return infoLabel; return infoLabel;
} }
private SelectDirPanel getSelectDirPanel() {
if (selectDirPanel == null) { private SelectDirPanel getSelectDirPanel()
selectDirPanel = new SelectDirPanel(Mode.SAVEDSTATES_IMPORT); {
if (selectDirPanel == null)
{
selectDirPanel = new SelectDirPanel(Mode.SAVEDSTATES_IMPORT);
} }
return selectDirPanel; return selectDirPanel;
} }
private JLabel getChoiceLabel() {
if (choiceLabel == null) { private JLabel getChoiceLabel()
choiceLabel = new JLabel("Select how to handle already available saved states in the \"saves\" folder:"); {
if (choiceLabel == null)
{
choiceLabel = new JLabel("Select how to handle already available saved states in the \"saves\" folder:");
} }
return choiceLabel; return choiceLabel;
} }
private JPanel getChoicePanel() {
if (choicePanel == null) { private JPanel getChoicePanel()
choicePanel = new JPanel(); {
GridBagLayout gbl_choicePanel = new GridBagLayout(); if (choicePanel == null)
choicePanel.setLayout(gbl_choicePanel); {
GridBagConstraints gbc_overwriteRadioButton = new GridBagConstraints(); choicePanel = new JPanel();
gbc_overwriteRadioButton.anchor = GridBagConstraints.WEST; GridBagLayout gbl_choicePanel = new GridBagLayout();
gbc_overwriteRadioButton.gridx = 0; choicePanel.setLayout(gbl_choicePanel);
gbc_overwriteRadioButton.gridy = 0; GridBagConstraints gbc_overwriteRadioButton = new GridBagConstraints();
choicePanel.add(getOverwriteRadioButton(), gbc_overwriteRadioButton); gbc_overwriteRadioButton.anchor = GridBagConstraints.WEST;
GridBagConstraints gbc_keepRadioButton = new GridBagConstraints(); gbc_overwriteRadioButton.gridx = 0;
gbc_keepRadioButton.anchor = GridBagConstraints.WEST; gbc_overwriteRadioButton.gridy = 0;
gbc_keepRadioButton.gridx = 0; choicePanel.add(getOverwriteRadioButton(), gbc_overwriteRadioButton);
gbc_keepRadioButton.gridy = 1; GridBagConstraints gbc_keepRadioButton = new GridBagConstraints();
choicePanel.add(getKeepRadioButton(), gbc_keepRadioButton); gbc_keepRadioButton.anchor = GridBagConstraints.WEST;
gbc_keepRadioButton.gridx = 0;
gbc_keepRadioButton.gridy = 1;
choicePanel.add(getKeepRadioButton(), gbc_keepRadioButton);
} }
return choicePanel; return choicePanel;
} }
private JRadioButton getOverwriteRadioButton() {
if (overwriteRadioButton == null) { private JRadioButton getOverwriteRadioButton()
overwriteRadioButton = new JRadioButton("Overwrite"); {
buttonGroup.add(overwriteRadioButton); if (overwriteRadioButton == null)
overwriteRadioButton.setSelected(true); {
overwriteRadioButton = new JRadioButton("Overwrite");
buttonGroup.add(overwriteRadioButton);
overwriteRadioButton.setSelected(true);
} }
return overwriteRadioButton; return overwriteRadioButton;
} }
private JRadioButton getKeepRadioButton() {
if (keepRadioButton == null) { private JRadioButton getKeepRadioButton()
keepRadioButton = new JRadioButton("Skip from import"); {
buttonGroup.add(keepRadioButton); if (keepRadioButton == null)
{
keepRadioButton = new JRadioButton("Skip from import");
buttonGroup.add(keepRadioButton);
} }
return keepRadioButton; return keepRadioButton;
} }
private JLabel getInfo2Label() {
if (info2Label == null) { private JLabel getInfo2Label()
info2Label = new JLabel("<html>Saved states available in the import folder that doesn't match any games in the database will still be copied to the game manager's \"saves\" folder. They will also be copied when exporting.</html>"); {
if (info2Label == null)
{
info2Label =
new JLabel("<html>Saved states available in the import folder that doesn't match any games in the database will still be copied to the game manager's \"saves\" folder. They will also be copied when exporting.</html>");
} }
return info2Label; return info2Label;
} }
File getTargetDirectory() File getTargetDirectory()
{ {
return getSelectDirPanel().getTargetDirectory(); return getSelectDirPanel().getTargetDirectory();
} }
boolean isImportOverwrite() boolean isImportOverwrite()
{ {
return getOverwriteRadioButton().isSelected(); return getOverwriteRadioButton().isSelected();

View File

@ -5,8 +5,10 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.file.FileSystems;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@ -60,49 +62,56 @@ public class SavedStatesManager
public void saveSavedStates() public void saveSavedStates()
{ {
//TODO How to handle when the title (and game name) is changed? //If the game has been renamed, make sure to rename the saves folder also
if (model.getInfoModel().isTitleChanged())
{
String oldFileName = model.getInfoModel().getOldGamesFile();
File oldSaveFolder = new File(SAVES + oldFileName);
if (oldSaveFolder.exists())
{
//Rename old folder to new name
oldSaveFolder.renameTo(new File(SAVES + model.getInfoModel().getGamesFile()));
}
}
String fileName = model.getInfoModel().getGamesFile(); String fileName = model.getInfoModel().getGamesFile();
Path saveFolder = new File(SAVES + fileName).toPath(); Path saveFolder = new File(SAVES + fileName).toPath();
if (Files.exists(saveFolder)) //Check which ones are available
Path mta0Path = saveFolder.resolve(MTA0);
Path vsz0Path = saveFolder.resolve(VSZ0);
Path png0Path = saveFolder.resolve(PNG0);
if (Files.exists(mta0Path) || savedStatesModel.getState1Path() != null)
{ {
//Check which ones are available storePlayTime(mta0Path, savedStatesModel.getState1time());
Path mta0Path = saveFolder.resolve(MTA0); copyVsfFile(vsz0Path, savedStatesModel.getState1Path());
Path vsz0Path = saveFolder.resolve(VSZ0); copyPngFile(png0Path, savedStatesModel.getState1PngImage());
Path png0Path = saveFolder.resolve(PNG0); }
if (Files.exists(mta0Path) || savedStatesModel.getState1Path() != null) Path mta1Path = saveFolder.resolve(MTA1);
{ Path vsz1Path = saveFolder.resolve(VSZ1);
storePlayTime(mta0Path, savedStatesModel.getState1time()); Path png1Path = saveFolder.resolve(PNG1);
copyVsfFile(vsz0Path, savedStatesModel.getState1Path()); if (Files.exists(mta1Path) || savedStatesModel.getState2Path() != null)
copyPngFile(png0Path, savedStatesModel.getState1PngImage()); {
} storePlayTime(mta1Path, savedStatesModel.getState2time());
Path mta1Path = saveFolder.resolve(MTA1); copyVsfFile(vsz1Path, savedStatesModel.getState2Path());
Path vsz1Path = saveFolder.resolve(VSZ1); copyPngFile(png1Path, savedStatesModel.getState2PngImage());
Path png1Path = saveFolder.resolve(PNG1); }
if (Files.exists(mta1Path) || savedStatesModel.getState2Path() != null) Path mta2Path = saveFolder.resolve(MTA2);
{ Path vsz2Path = saveFolder.resolve(VSZ2);
storePlayTime(mta1Path, savedStatesModel.getState2time()); Path png2Path = saveFolder.resolve(PNG2);
copyVsfFile(vsz1Path, savedStatesModel.getState2Path()); if (Files.exists(mta2Path) || savedStatesModel.getState3Path() != null)
copyPngFile(png1Path, savedStatesModel.getState2PngImage()); {
} storePlayTime(mta2Path, savedStatesModel.getState3time());
Path mta2Path = saveFolder.resolve(MTA2); copyVsfFile(vsz2Path, savedStatesModel.getState3Path());
Path vsz2Path = saveFolder.resolve(VSZ2); copyPngFile(png2Path, savedStatesModel.getState3PngImage());
Path png2Path = saveFolder.resolve(PNG2); }
if (Files.exists(mta2Path) || savedStatesModel.getState3Path() != null) Path mta3Path = saveFolder.resolve(MTA3);
{ Path vsz3Path = saveFolder.resolve(VSZ3);
storePlayTime(mta2Path, savedStatesModel.getState3time()); Path png3Path = saveFolder.resolve(PNG3);
copyVsfFile(vsz2Path, savedStatesModel.getState3Path()); if (Files.exists(mta3Path) || savedStatesModel.getState4Path() != null)
copyPngFile(png2Path, savedStatesModel.getState3PngImage()); {
} storePlayTime(mta3Path, savedStatesModel.getState4time());
Path mta3Path = saveFolder.resolve(MTA3); copyVsfFile(vsz3Path, savedStatesModel.getState4Path());
Path vsz3Path = saveFolder.resolve(VSZ3); copyPngFile(png3Path, savedStatesModel.getState4PngImage());
Path png3Path = saveFolder.resolve(PNG3);
if (Files.exists(mta3Path) || savedStatesModel.getState4Path() != null)
{
storePlayTime(mta3Path, savedStatesModel.getState4time());
copyVsfFile(vsz3Path, savedStatesModel.getState4Path());
copyPngFile(png3Path, savedStatesModel.getState4PngImage());
}
} }
} }
@ -262,13 +271,12 @@ public class SavedStatesManager
{ {
return this.exportOverwrite; return this.exportOverwrite;
} }
public void setImportDirectory(File importDir) public void setImportDirectory(File importDir)
{ {
this.importDir = importDir; this.importDir = importDir;
} }
public void setImportOverwrite(boolean importOverwrite) public void setImportOverwrite(boolean importOverwrite)
{ {
this.importOverwrite = importOverwrite; this.importOverwrite = importOverwrite;
@ -292,8 +300,14 @@ public class SavedStatesManager
if (sourcePath.equals(saveFolder.toPath().toAbsolutePath())) if (sourcePath.equals(saveFolder.toPath().toAbsolutePath()))
{ {
return; return;
} }
Path destinationPath = exportDir.toPath().resolve(saveFolder.toPath().toAbsolutePath().relativize(sourcePath)); if (!isValidSaveStatePath(sourcePath))
{
infoBuilder.append("Skipping " + sourcePath + " (not a valid save state file)\n");
return;
}
Path destinationPath =
exportDir.toPath().resolve(saveFolder.toPath().toAbsolutePath().relativize(sourcePath));
//Ignore already existing directories: Files.copy() throws DirectoryNotEmptyException for them //Ignore already existing directories: Files.copy() throws DirectoryNotEmptyException for them
if (destinationPath.toFile().exists() && destinationPath.toFile().isDirectory()) if (destinationPath.toFile().exists() && destinationPath.toFile().isDirectory())
{ {
@ -301,13 +315,16 @@ public class SavedStatesManager
} }
if (!this.exportOverwrite && destinationPath.toFile().exists()) if (!this.exportOverwrite && destinationPath.toFile().exists())
{ {
infoBuilder.append("Skipping " + sourcePath + "\n"); infoBuilder.append("Skipping " + sourcePath + " (already exists)\n");
} }
else else
{ {
infoBuilder.append("Copying " + sourcePath + "\n"); infoBuilder.append("Copying " + sourcePath + "\n");
Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING); Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
noFilesCopied++; if (!sourcePath.toFile().isDirectory())
{
noFilesCopied++;
}
} }
} }
catch (Exception e) catch (Exception e)
@ -322,7 +339,7 @@ public class SavedStatesManager
ExceptionHandler.handleException(e1, "Could not export saved states folder."); ExceptionHandler.handleException(e1, "Could not export saved states folder.");
} }
} }
public void importSavedStates(StringBuilder infoBuilder) public void importSavedStates(StringBuilder infoBuilder)
{ {
noFilesCopied = 0; noFilesCopied = 0;
@ -332,12 +349,18 @@ public class SavedStatesManager
stream.forEachOrdered(sourcePath -> { stream.forEachOrdered(sourcePath -> {
try try
{ {
//Ignore first folder //Ignore first folder or any files that are not save state files
if (sourcePath.equals(importDir.toPath().toAbsolutePath())) if (sourcePath.equals(importDir.toPath().toAbsolutePath()))
{ {
return; return;
} }
Path destinationPath = saveFolder.toPath().resolve(importDir.toPath().toAbsolutePath().relativize(sourcePath)); if (!isValidSaveStatePath(sourcePath))
{
infoBuilder.append("Skipping " + sourcePath + " (not a valid save state file)\n");
return;
}
Path destinationPath =
saveFolder.toPath().resolve(importDir.toPath().toAbsolutePath().relativize(sourcePath));
//Ignore already existing directories: Files.copy() throws DirectoryNotEmptyException for them //Ignore already existing directories: Files.copy() throws DirectoryNotEmptyException for them
if (destinationPath.toFile().exists() && destinationPath.toFile().isDirectory()) if (destinationPath.toFile().exists() && destinationPath.toFile().isDirectory())
{ {
@ -345,13 +368,16 @@ public class SavedStatesManager
} }
if (!this.importOverwrite && destinationPath.toFile().exists()) if (!this.importOverwrite && destinationPath.toFile().exists())
{ {
infoBuilder.append("Skipping " + sourcePath + "\n"); infoBuilder.append("Skipping " + sourcePath + " (already exists)\n");
} }
else else
{ {
infoBuilder.append("Copying " + sourcePath + "\n"); infoBuilder.append("Copying " + sourcePath + "\n");
Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING); Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
noFilesCopied++; if (!sourcePath.toFile().isDirectory())
{
noFilesCopied++;
}
} }
} }
catch (Exception e) catch (Exception e)
@ -366,8 +392,13 @@ public class SavedStatesManager
ExceptionHandler.handleException(e1, "Could not import to saved folder."); ExceptionHandler.handleException(e1, "Could not import to saved folder.");
} }
} }
private boolean isValidSaveStatePath(Path path)
{
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.{mta,png,vsz}");
return matcher.matches(path) || path.toFile().isDirectory();
}
public int getNumberOfFilesCopied() public int getNumberOfFilesCopied()
{ {
return noFilesCopied; return noFilesCopied;