diff --git a/src/main/java/se/lantz/gui/MenuManager.java b/src/main/java/se/lantz/gui/MenuManager.java index 765c2f6..03150fd 100644 --- a/src/main/java/se/lantz/gui/MenuManager.java +++ b/src/main/java/se/lantz/gui/MenuManager.java @@ -768,7 +768,7 @@ public class MenuManager { savedStatesManager.setImportDirectory(importSavedStatesDialog.getTargetDirectory()); 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); worker.execute(); dialog.setVisible(true); @@ -784,7 +784,7 @@ public class MenuManager { savedStatesManager.setExportDirectory(exportSavedStatesDialog.getTargetDirectory()); 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); worker.execute(); dialog.setVisible(true); @@ -806,7 +806,7 @@ public class MenuManager exportManager.setGameViewsToExport(viewList); exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(), 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); worker.execute(); dialog.setVisible(true); @@ -820,7 +820,7 @@ public class MenuManager exportManager.setGamesToExport(gamesList); exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(), 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); worker.execute(); dialog.setVisible(true); @@ -844,7 +844,7 @@ public class MenuManager exportManager.setGameViewsToExport(viewList); exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(), 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); worker.execute(); dialog.setVisible(true); @@ -858,7 +858,7 @@ public class MenuManager exportManager.setGamesToExport(gamesList); exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(), 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); worker.execute(); dialog.setVisible(true); diff --git a/src/main/java/se/lantz/gui/SelectDirPanel.java b/src/main/java/se/lantz/gui/SelectDirPanel.java index eeebf39..dba0d0b 100644 --- a/src/main/java/se/lantz/gui/SelectDirPanel.java +++ b/src/main/java/se/lantz/gui/SelectDirPanel.java @@ -30,6 +30,8 @@ import se.lantz.util.FileManager; public class SelectDirPanel extends JPanel { + private static final String THEC64SAVE = ".THEC64SAVE"; + public enum Mode { CAROUSEL_IMPORT, GB_IMPORT, CAROUSEL_EXPORT, FILELOADER_EXPORT, SAVEDSTATES_IMPORT, SAVEDSTATES_EXPORT @@ -276,13 +278,27 @@ public class SelectDirPanel extends JPanel private void selectSavedStatesImportDirectory() { 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.setCurrentDirectory(new File(configuredDir)); int value = fileChooser.showDialog(this, "OK"); if (value == JFileChooser.APPROVE_OPTION) { targetDirectory = fileChooser.getSelectedFile(); + if (!targetDirectory.getName().toUpperCase().equals(THEC64SAVE)) + { + String message = "You have not selected a \"" + THEC64SAVE + + "\" directory.
Are you sure you want to import from the selected directory?"; + 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(); FileManager.getConfiguredProperties().put(SAVEDSTATES_IMPORT_DIR_PROPERTY, configuredDir); getDirTextField().setText(configuredDir); @@ -301,17 +317,17 @@ public class SelectDirPanel extends JPanel 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.setCurrentDirectory(new File(configuredDir)); int value = fileChooser.showDialog(this, "OK"); if (value == JFileChooser.APPROVE_OPTION) { targetDirectory = fileChooser.getSelectedFile(); - if (!targetDirectory.getName().toUpperCase().equals(".THEC64SAVE")) + if (!targetDirectory.getName().toUpperCase().equals(THEC64SAVE)) { - String message = - "You have not selected the \".THEC64SAVE\" directory.
Are you sure you want to export to the selected directory?"; + String message = "You have not selected a \"" + THEC64SAVE + + "\" directory.
Are you sure you want to export to the selected directory?"; int choice = JOptionPane.showConfirmDialog(SwingUtilities.getAncestorOfClass(JDialog.class, this), message, "Folder name", diff --git a/src/main/java/se/lantz/gui/exports/ExportSavedStatesPanel.java b/src/main/java/se/lantz/gui/exports/ExportSavedStatesPanel.java index 6cb4ec6..0e2af1f 100644 --- a/src/main/java/se/lantz/gui/exports/ExportSavedStatesPanel.java +++ b/src/main/java/se/lantz/gui/exports/ExportSavedStatesPanel.java @@ -1,18 +1,17 @@ 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 se.lantz.gui.SelectDirPanel; -import se.lantz.gui.SelectDirPanel.Mode; +import java.awt.GridBagLayout; import java.awt.Insets; import java.io.File; -import javax.swing.JRadioButton; 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 { @@ -23,7 +22,9 @@ public class ExportSavedStatesPanel extends JPanel private JRadioButton overwriteRadioButton; private JRadioButton keepRadioButton; private final ButtonGroup buttonGroup = new ButtonGroup(); - public ExportSavedStatesPanel() { + + public ExportSavedStatesPanel() + { GridBagLayout gridBagLayout = new GridBagLayout(); setLayout(gridBagLayout); GridBagConstraints gbc_infoLabel = new GridBagConstraints(); @@ -53,63 +54,80 @@ public class ExportSavedStatesPanel extends JPanel add(getChoicePanel(), gbc_choicePanel); } - private JLabel getInfoLabel() { - if (infoLabel == null) { - infoLabel = new JLabel("Select the \".THEC64SAVE\" folder in the root of your PCUAE USB stick:"); + private JLabel getInfoLabel() + { + if (infoLabel == null) + { + infoLabel = new JLabel("Select the \".THEC64SAVE\" folder in the root of your PCUAE USB stick:"); } return infoLabel; } - private SelectDirPanel getSelectDirPanel() { - if (selectDirPanel == null) { - selectDirPanel = new SelectDirPanel(Mode.SAVEDSTATES_EXPORT); + + private SelectDirPanel getSelectDirPanel() + { + if (selectDirPanel == null) + { + selectDirPanel = new SelectDirPanel(Mode.SAVEDSTATES_EXPORT); } return selectDirPanel; } - private JLabel getChoiceLabel() { - if (choiceLabel == null) { - choiceLabel = new JLabel("Select how to handle already available saved states in the export folder:"); + + private JLabel getChoiceLabel() + { + if (choiceLabel == null) + { + choiceLabel = new JLabel("Select how to handle already available saved states in the export folder:"); } return choiceLabel; } - private JPanel getChoicePanel() { - if (choicePanel == null) { - choicePanel = new JPanel(); - GridBagLayout gbl_choicePanel = new GridBagLayout(); - choicePanel.setLayout(gbl_choicePanel); - GridBagConstraints gbc_overwriteRadioButton = new GridBagConstraints(); - gbc_overwriteRadioButton.anchor = GridBagConstraints.WEST; - gbc_overwriteRadioButton.gridx = 0; - gbc_overwriteRadioButton.gridy = 0; - choicePanel.add(getOverwriteRadioButton(), gbc_overwriteRadioButton); - GridBagConstraints gbc_keepRadioButton = new GridBagConstraints(); - gbc_keepRadioButton.anchor = GridBagConstraints.WEST; - gbc_keepRadioButton.gridx = 0; - gbc_keepRadioButton.gridy = 1; - choicePanel.add(getKeepRadioButton(), gbc_keepRadioButton); + + private JPanel getChoicePanel() + { + if (choicePanel == null) + { + choicePanel = new JPanel(); + GridBagLayout gbl_choicePanel = new GridBagLayout(); + choicePanel.setLayout(gbl_choicePanel); + GridBagConstraints gbc_overwriteRadioButton = new GridBagConstraints(); + gbc_overwriteRadioButton.anchor = GridBagConstraints.WEST; + gbc_overwriteRadioButton.gridx = 0; + gbc_overwriteRadioButton.gridy = 0; + choicePanel.add(getOverwriteRadioButton(), gbc_overwriteRadioButton); + GridBagConstraints gbc_keepRadioButton = new GridBagConstraints(); + gbc_keepRadioButton.anchor = GridBagConstraints.WEST; + gbc_keepRadioButton.gridx = 0; + gbc_keepRadioButton.gridy = 1; + choicePanel.add(getKeepRadioButton(), gbc_keepRadioButton); } return choicePanel; } - private JRadioButton getOverwriteRadioButton() { - if (overwriteRadioButton == null) { - overwriteRadioButton = new JRadioButton("Overwrite"); - buttonGroup.add(overwriteRadioButton); - overwriteRadioButton.setSelected(true); + + private JRadioButton getOverwriteRadioButton() + { + if (overwriteRadioButton == null) + { + overwriteRadioButton = new JRadioButton("Overwrite"); + buttonGroup.add(overwriteRadioButton); + overwriteRadioButton.setSelected(true); } return overwriteRadioButton; } - private JRadioButton getKeepRadioButton() { - if (keepRadioButton == null) { - keepRadioButton = new JRadioButton("Skip from export\r\n"); - buttonGroup.add(keepRadioButton); + + private JRadioButton getKeepRadioButton() + { + if (keepRadioButton == null) + { + keepRadioButton = new JRadioButton("Skip from export\r\n"); + buttonGroup.add(keepRadioButton); } return keepRadioButton; } - + File getTargetDirectory() { return getSelectDirPanel().getTargetDirectory(); } - + boolean isExportOverwrite() { return getOverwriteRadioButton().isSelected(); diff --git a/src/main/java/se/lantz/gui/exports/ImportExportProgressDialog.java b/src/main/java/se/lantz/gui/exports/ImportExportProgressDialog.java index 3ac225e..6a5ac76 100644 --- a/src/main/java/se/lantz/gui/exports/ImportExportProgressDialog.java +++ b/src/main/java/se/lantz/gui/exports/ImportExportProgressDialog.java @@ -11,9 +11,12 @@ public class ImportExportProgressDialog extends JDialog 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); + this.isImport = isImport; this.add(getExportProgressPanel()); setSize(900, 600); setLocationRelativeTo(frame); @@ -28,7 +31,7 @@ public class ImportExportProgressDialog extends JDialog public void finish() { - getExportProgressPanel().finish(); + getExportProgressPanel().finish(isImport); } public ImportExportProgressPanel getExportProgressPanel() diff --git a/src/main/java/se/lantz/gui/exports/ImportExportProgressPanel.java b/src/main/java/se/lantz/gui/exports/ImportExportProgressPanel.java index 4d42f9f..e835881 100644 --- a/src/main/java/se/lantz/gui/exports/ImportExportProgressPanel.java +++ b/src/main/java/se/lantz/gui/exports/ImportExportProgressPanel.java @@ -88,7 +88,7 @@ public class ImportExportProgressPanel extends JPanel getTextArea().append(infoText); } - public void finish() + public void finish(boolean isImport) { getCloseButton().setEnabled(true); getProgressBar().setIndeterminate(false); @@ -96,13 +96,21 @@ public class ImportExportProgressPanel extends JPanel //Check for errors String text = getTextArea().getText(); 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 { - 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."); } } } diff --git a/src/main/java/se/lantz/gui/imports/ImportSavedStatesPanel.java b/src/main/java/se/lantz/gui/imports/ImportSavedStatesPanel.java index c4ff6ae..564f695 100644 --- a/src/main/java/se/lantz/gui/imports/ImportSavedStatesPanel.java +++ b/src/main/java/se/lantz/gui/imports/ImportSavedStatesPanel.java @@ -1,18 +1,17 @@ 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 se.lantz.gui.SelectDirPanel; -import se.lantz.gui.SelectDirPanel.Mode; +import java.awt.GridBagLayout; import java.awt.Insets; import java.io.File; -import javax.swing.JRadioButton; 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 { @@ -24,7 +23,9 @@ public class ImportSavedStatesPanel extends JPanel private JRadioButton keepRadioButton; private JLabel info2Label; private final ButtonGroup buttonGroup = new ButtonGroup(); - public ImportSavedStatesPanel() { + + public ImportSavedStatesPanel() + { GridBagLayout gridBagLayout = new GridBagLayout(); setLayout(gridBagLayout); GridBagConstraints gbc_infoLabel = new GridBagConstraints(); @@ -60,69 +61,90 @@ public class ImportSavedStatesPanel extends JPanel add(getInfo2Label(), gbc_info2Label); } - private JLabel getInfoLabel() { - if (infoLabel == null) { - infoLabel = new JLabel("Select the \".THEC64SAVE\" folder in the root of your PCUAE USB stick:"); + private JLabel getInfoLabel() + { + if (infoLabel == null) + { + infoLabel = new JLabel("Select the \".THEC64SAVE\" folder in the root of your PCUAE USB stick:"); } return infoLabel; } - private SelectDirPanel getSelectDirPanel() { - if (selectDirPanel == null) { - selectDirPanel = new SelectDirPanel(Mode.SAVEDSTATES_IMPORT); + + private SelectDirPanel getSelectDirPanel() + { + if (selectDirPanel == null) + { + selectDirPanel = new SelectDirPanel(Mode.SAVEDSTATES_IMPORT); } return selectDirPanel; } - private JLabel getChoiceLabel() { - if (choiceLabel == null) { - choiceLabel = new JLabel("Select how to handle already available saved states in the \"saves\" folder:"); + + private JLabel getChoiceLabel() + { + if (choiceLabel == null) + { + choiceLabel = new JLabel("Select how to handle already available saved states in the \"saves\" folder:"); } return choiceLabel; } - private JPanel getChoicePanel() { - if (choicePanel == null) { - choicePanel = new JPanel(); - GridBagLayout gbl_choicePanel = new GridBagLayout(); - choicePanel.setLayout(gbl_choicePanel); - GridBagConstraints gbc_overwriteRadioButton = new GridBagConstraints(); - gbc_overwriteRadioButton.anchor = GridBagConstraints.WEST; - gbc_overwriteRadioButton.gridx = 0; - gbc_overwriteRadioButton.gridy = 0; - choicePanel.add(getOverwriteRadioButton(), gbc_overwriteRadioButton); - GridBagConstraints gbc_keepRadioButton = new GridBagConstraints(); - gbc_keepRadioButton.anchor = GridBagConstraints.WEST; - gbc_keepRadioButton.gridx = 0; - gbc_keepRadioButton.gridy = 1; - choicePanel.add(getKeepRadioButton(), gbc_keepRadioButton); + + private JPanel getChoicePanel() + { + if (choicePanel == null) + { + choicePanel = new JPanel(); + GridBagLayout gbl_choicePanel = new GridBagLayout(); + choicePanel.setLayout(gbl_choicePanel); + GridBagConstraints gbc_overwriteRadioButton = new GridBagConstraints(); + gbc_overwriteRadioButton.anchor = GridBagConstraints.WEST; + gbc_overwriteRadioButton.gridx = 0; + gbc_overwriteRadioButton.gridy = 0; + choicePanel.add(getOverwriteRadioButton(), gbc_overwriteRadioButton); + GridBagConstraints gbc_keepRadioButton = new GridBagConstraints(); + gbc_keepRadioButton.anchor = GridBagConstraints.WEST; + gbc_keepRadioButton.gridx = 0; + gbc_keepRadioButton.gridy = 1; + choicePanel.add(getKeepRadioButton(), gbc_keepRadioButton); } return choicePanel; } - private JRadioButton getOverwriteRadioButton() { - if (overwriteRadioButton == null) { - overwriteRadioButton = new JRadioButton("Overwrite"); - buttonGroup.add(overwriteRadioButton); - overwriteRadioButton.setSelected(true); + + private JRadioButton getOverwriteRadioButton() + { + if (overwriteRadioButton == null) + { + overwriteRadioButton = new JRadioButton("Overwrite"); + buttonGroup.add(overwriteRadioButton); + overwriteRadioButton.setSelected(true); } return overwriteRadioButton; } - private JRadioButton getKeepRadioButton() { - if (keepRadioButton == null) { - keepRadioButton = new JRadioButton("Skip from import"); - buttonGroup.add(keepRadioButton); + + private JRadioButton getKeepRadioButton() + { + if (keepRadioButton == null) + { + keepRadioButton = new JRadioButton("Skip from import"); + buttonGroup.add(keepRadioButton); } return keepRadioButton; } - private JLabel getInfo2Label() { - if (info2Label == null) { - info2Label = new JLabel("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."); + + private JLabel getInfo2Label() + { + if (info2Label == null) + { + info2Label = + new JLabel("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."); } return info2Label; } - + File getTargetDirectory() { return getSelectDirPanel().getTargetDirectory(); } - + boolean isImportOverwrite() { return getOverwriteRadioButton().isSelected(); diff --git a/src/main/java/se/lantz/manager/SavedStatesManager.java b/src/main/java/se/lantz/manager/SavedStatesManager.java index 722646a..f829eed 100644 --- a/src/main/java/se/lantz/manager/SavedStatesManager.java +++ b/src/main/java/se/lantz/manager/SavedStatesManager.java @@ -5,8 +5,10 @@ import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.PathMatcher; import java.nio.file.StandardCopyOption; import java.text.SimpleDateFormat; import java.util.Date; @@ -60,49 +62,56 @@ public class SavedStatesManager 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(); 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 - Path mta0Path = saveFolder.resolve(MTA0); - Path vsz0Path = saveFolder.resolve(VSZ0); - Path png0Path = saveFolder.resolve(PNG0); - if (Files.exists(mta0Path) || savedStatesModel.getState1Path() != null) - { - storePlayTime(mta0Path, savedStatesModel.getState1time()); - copyVsfFile(vsz0Path, savedStatesModel.getState1Path()); - copyPngFile(png0Path, savedStatesModel.getState1PngImage()); - } - Path mta1Path = saveFolder.resolve(MTA1); - Path vsz1Path = saveFolder.resolve(VSZ1); - Path png1Path = saveFolder.resolve(PNG1); - if (Files.exists(mta1Path) || savedStatesModel.getState2Path() != null) - { - storePlayTime(mta1Path, savedStatesModel.getState2time()); - copyVsfFile(vsz1Path, savedStatesModel.getState2Path()); - copyPngFile(png1Path, savedStatesModel.getState2PngImage()); - } - Path mta2Path = saveFolder.resolve(MTA2); - Path vsz2Path = saveFolder.resolve(VSZ2); - Path png2Path = saveFolder.resolve(PNG2); - if (Files.exists(mta2Path) || savedStatesModel.getState3Path() != null) - { - storePlayTime(mta2Path, savedStatesModel.getState3time()); - copyVsfFile(vsz2Path, savedStatesModel.getState3Path()); - copyPngFile(png2Path, savedStatesModel.getState3PngImage()); - } - Path mta3Path = saveFolder.resolve(MTA3); - Path vsz3Path = saveFolder.resolve(VSZ3); - Path png3Path = saveFolder.resolve(PNG3); - if (Files.exists(mta3Path) || savedStatesModel.getState4Path() != null) - { - storePlayTime(mta3Path, savedStatesModel.getState4time()); - copyVsfFile(vsz3Path, savedStatesModel.getState4Path()); - copyPngFile(png3Path, savedStatesModel.getState4PngImage()); - } + storePlayTime(mta0Path, savedStatesModel.getState1time()); + copyVsfFile(vsz0Path, savedStatesModel.getState1Path()); + copyPngFile(png0Path, savedStatesModel.getState1PngImage()); + } + Path mta1Path = saveFolder.resolve(MTA1); + Path vsz1Path = saveFolder.resolve(VSZ1); + Path png1Path = saveFolder.resolve(PNG1); + if (Files.exists(mta1Path) || savedStatesModel.getState2Path() != null) + { + storePlayTime(mta1Path, savedStatesModel.getState2time()); + copyVsfFile(vsz1Path, savedStatesModel.getState2Path()); + copyPngFile(png1Path, savedStatesModel.getState2PngImage()); + } + Path mta2Path = saveFolder.resolve(MTA2); + Path vsz2Path = saveFolder.resolve(VSZ2); + Path png2Path = saveFolder.resolve(PNG2); + if (Files.exists(mta2Path) || savedStatesModel.getState3Path() != null) + { + storePlayTime(mta2Path, savedStatesModel.getState3time()); + copyVsfFile(vsz2Path, savedStatesModel.getState3Path()); + copyPngFile(png2Path, savedStatesModel.getState3PngImage()); + } + Path mta3Path = saveFolder.resolve(MTA3); + Path vsz3Path = saveFolder.resolve(VSZ3); + 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; } - + public void setImportDirectory(File importDir) { this.importDir = importDir; } - public void setImportOverwrite(boolean importOverwrite) { this.importOverwrite = importOverwrite; @@ -292,8 +300,14 @@ public class SavedStatesManager if (sourcePath.equals(saveFolder.toPath().toAbsolutePath())) { 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 if (destinationPath.toFile().exists() && destinationPath.toFile().isDirectory()) { @@ -301,13 +315,16 @@ public class SavedStatesManager } if (!this.exportOverwrite && destinationPath.toFile().exists()) { - infoBuilder.append("Skipping " + sourcePath + "\n"); + infoBuilder.append("Skipping " + sourcePath + " (already exists)\n"); } else { infoBuilder.append("Copying " + sourcePath + "\n"); Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING); - noFilesCopied++; + if (!sourcePath.toFile().isDirectory()) + { + noFilesCopied++; + } } } catch (Exception e) @@ -322,7 +339,7 @@ public class SavedStatesManager ExceptionHandler.handleException(e1, "Could not export saved states folder."); } } - + public void importSavedStates(StringBuilder infoBuilder) { noFilesCopied = 0; @@ -332,12 +349,18 @@ public class SavedStatesManager stream.forEachOrdered(sourcePath -> { try { - //Ignore first folder + //Ignore first folder or any files that are not save state files if (sourcePath.equals(importDir.toPath().toAbsolutePath())) { 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 if (destinationPath.toFile().exists() && destinationPath.toFile().isDirectory()) { @@ -345,13 +368,16 @@ public class SavedStatesManager } if (!this.importOverwrite && destinationPath.toFile().exists()) { - infoBuilder.append("Skipping " + sourcePath + "\n"); + infoBuilder.append("Skipping " + sourcePath + " (already exists)\n"); } else { infoBuilder.append("Copying " + sourcePath + "\n"); Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING); - noFilesCopied++; + if (!sourcePath.toFile().isDirectory()) + { + noFilesCopied++; + } } } catch (Exception e) @@ -366,8 +392,13 @@ public class SavedStatesManager 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() { return noFilesCopied;