Skip to content

Commit

Permalink
Fix some edge cases with restoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
asvitkine committed Dec 27, 2021
1 parent 4e7ba96 commit 42eb46e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 49 deletions.
35 changes: 9 additions & 26 deletions src/impsave/InfoPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,14 @@ public void actionPerformed(ActionEvent event) {

// Copy over the selected file...
JButton button = (JButton) event.getSource();
String buttonText = button.getText();
String targetName = null;
for (int i = 0; i < 8; i++) {
if (buttonText.startsWith(getRestoreButtonNamePrefix(i))) {
targetName = saveDb.getFilename(info.fileNamePrefix, i);
}
}
String targetName = button.getName();
File file = getFileToRestore();
File parentDir = file.getParentFile();
String parentDirName = parentDir.getName();
System.out.printf("Activating [%s/%s]\n", parentDirName, file.getName());
if (targetName == null) {
// Either it's an autosave or the button text is out of sync with the database.
String suffix = " autosaves";
if (!parentDirName.endsWith(suffix)) {
JOptionPane.showMessageDialog(this, "Could not restore game.");
System.out.println("ERROR: Unexpected dir name: " + parentDir);
return;
}
targetName = parentDirName.substring(0, parentDirName.length() - suffix.length());

}
File dest = new File(parentDir.getParentFile().getAbsolutePath() + "/" + targetName);
System.out.printf("Activating [%s/%s]\n", file.getParentFile().getName(), file.getName());
try {
saveDb.activateFile(file, dest, gameName);
saveDb.activateFile(file, targetName, gameName);
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Could not restore game.");
System.err.println("Error activating: " + e.getMessage());
e.printStackTrace();
return;
}
System.out.println("Activated as: " + targetName);
Expand Down Expand Up @@ -185,6 +164,7 @@ public void setGameInfo(GameInfo info) {
BorderFactory.createEmptyBorder(2, 2, 2, 2)));
grid.setLayout(new GridLayout(5, 2));
JButton button = new JButton("- Autosave -");
button.setName(info.fileNamePrefix + "A.imp");
button.addActionListener(this);
grid.add(button);
grid.add(new JPanel());
Expand Down Expand Up @@ -215,6 +195,7 @@ public void itemStateChanged(ItemEvent unused) {
if (restoreButtons == null) {
return;
}
File fileToRestore = getFileToRestore();
int i = 0;
for (JButton button : restoreButtons) {
if (restoreButtons.length == 8) {
Expand All @@ -223,14 +204,16 @@ public void itemStateChanged(ItemEvent unused) {
} else {
button.setText(getRestoreButtonNamePrefix(i) + saveDb.getSoloSaveGameName(i));
}
button.setName(saveDb.getFilename(info.fileNamePrefix, i));
i++;
} else {
button.setText(" Select for Restore ");
String targetFile = Utils.truncateAtChar(fileToRestore.getParentFile().getName(), ' ');
button.setName(targetFile);
}
button.setEnabled(true);
}
TurnEntry entry = (TurnEntry) turnSelector.getSelectedItem();
gameNameField.setText(saveDb.getGameInfo(entry.file).getSavedGameName());
gameNameField.setText(saveDb.getGameInfo(fileToRestore).getSavedGameName());
}

private void add(JComponent c) {
Expand Down
58 changes: 35 additions & 23 deletions src/impsave/SaveDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ private void copyFileOrZipContents(File file, OutputStream out, String gameName)
if (gameName != null) {
byte[] header = new byte[SaveParser.SAVE_NAME_OFFSET + SaveParser.SAVE_NAME_LENGTH];
if (source.read(header) != header.length) {
if (zin != null) {
zin.closeEntry();
zin.close();
}
in.close();
throw new IOException("Couldn't read header!");
}
Expand All @@ -236,7 +240,11 @@ private void copyFileOrZipContents(File file, OutputStream out, String gameName)
in.close();
}

public synchronized void activateFile(File file, File dest, String gameName) throws IOException {
public synchronized void activateFile(File file, String targetSaveFileName, String gameName) throws IOException {
File dest = new File(saveFolder.getAbsolutePath() + "/" + targetSaveFileName);
if (dest.equals(file.getAbsoluteFile())) {
throw new IOException("Cannot restore file onto itself.");
}
FileOutputStream out = new FileOutputStream(dest);
try {
copyFileOrZipContents(file, out, gameName);
Expand Down Expand Up @@ -294,30 +302,34 @@ public synchronized void fileUpdated(File file, byte[] data) {
if (ignoreFile(file) || !file.getParentFile().equals(saveFolder))
return;

SaveDb.GameFileContents contents = parseGameInfo(file, data);
GameFileContents oldContents = getGameInfo(file);

String oldName = (oldContents != null ? oldContents.getSavedGameName() : "");
String newName = contents.getSavedGameName();
updateGameFileCache(contents);
if (contents.equals(oldContents)) {
return;
}
System.out.print("Updated data for: " + file.getName());
int index = savedGames.games.indexOf(oldContents);
if (index != -1) {
savedGames.games.set(index, contents);
} else {
savedGames.games.add(contents);
}
if (!oldName.equals(newName)) {
System.out.print(". Game name changed from \"" + oldName + "\" to \"" + newName + "\".");
for (Runnable listener : saveGameNameListeners) {
SwingUtilities.invokeLater(listener);
try {
SaveDb.GameFileContents contents = parseGameInfo(file, data);
GameFileContents oldContents = getGameInfo(file);

String oldName = (oldContents != null ? oldContents.getSavedGameName() : "");
String newName = contents.getSavedGameName();
updateGameFileCache(contents);
if (contents.equals(oldContents)) {
return;
}
System.out.print("Updated data for: " + file.getName());
int index = savedGames.games.indexOf(oldContents);
if (index != -1) {
savedGames.games.set(index, contents);
} else {
savedGames.games.add(contents);
}
if (!oldName.equals(newName)) {
System.out.print(". Game name changed from \"" + oldName + "\" to \"" + newName + "\".");
for (Runnable listener : saveGameNameListeners) {
SwingUtilities.invokeLater(listener);
}
}
System.out.println();
save();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println();
save();
}

private String generateBackupSuffix() {
Expand Down

0 comments on commit 42eb46e

Please sign in to comment.