Skip to content

Commit

Permalink
Changed behavior of the "Delete" and "Restore backup: operations
Browse files Browse the repository at this point in the history
- Delete: also removes available backups of the resource
- Restore backup: Removes overridden copy if the resource is biffed,
overriden and no backup is available
  • Loading branch information
Argent77 committed Jan 25, 2015
1 parent a6cc6a5 commit c7c023a
Showing 1 changed file with 56 additions and 22 deletions.
78 changes: 56 additions & 22 deletions src/infinity/gui/ResourceTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ static void deleteResource(ResourceEntry entry)
return;
NearInfinity.getInstance().removeViewable();
ResourceFactory.getInstance().getResources().removeResourceEntry(entry);
File bakFile = getBackupFile(entry);
if (bakFile != null) {
bakFile.delete();
}
((FileResourceEntry)entry).deleteFile();
}
else if (entry instanceof BIFFResourceEntry) {
Expand All @@ -274,6 +278,10 @@ else if (entry instanceof BIFFResourceEntry) {
JOptionPane.WARNING_MESSAGE, null, options, options[0]) != 0)
return;
NearInfinity.getInstance().removeViewable();
File bakFile = getBackupFile(entry);
if (bakFile != null) {
bakFile.delete();
}
((BIFFResourceEntry)entry).deleteOverride();
}
}
Expand All @@ -287,30 +295,30 @@ static void restoreResource(ResourceEntry entry)
"Are you sure you want to restore " + entry + " with a previous version?",
"Restore backup", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]) == JOptionPane.YES_OPTION) {
File curFile = entry.getActualFile();
NearInfinity.getInstance().removeViewable();
File curFile = getCurrentFile(entry);
File bakFile = getBackupFile(entry);
File tmpFile = getTempFile(curFile);
if (curFile != null && curFile.isFile() && bakFile != null && bakFile.isFile()) {
if (curFile.renameTo(tmpFile)) {
if (bakFile.renameTo(curFile)) {
tmpFile.delete();
JOptionPane.showMessageDialog(NearInfinity.getInstance(),
"Backup has been restored successfully.\n" +
"(You may need to reload the current resource\n" +
"to make changes visible.)",
"Restore backup", JOptionPane.INFORMATION_MESSAGE);
return;
} else {
if (!tmpFile.renameTo(curFile)) {
if (bakFile != null && (bakFile.getName().toLowerCase(Locale.ENGLISH).endsWith(".bak"))) {
// .bak available -> restore .bak version
File tmpFile = getTempFile(curFile);
if (curFile != null && curFile.isFile() && bakFile != null && bakFile.isFile()) {
if (curFile.renameTo(tmpFile)) {
if (bakFile.renameTo(curFile)) {
tmpFile.delete();
JOptionPane.showMessageDialog(NearInfinity.getInstance(),
"Backup has been restored successfully.",
"Restore backup", JOptionPane.INFORMATION_MESSAGE);
return;
} else if (!tmpFile.renameTo(curFile)) {
// Worst possible scenario: failed restore operation can't restore original resource
String path = tmpFile.getParent();
String tmp = tmpFile.getName();
String cur = curFile.getName();
JOptionPane.showMessageDialog(NearInfinity.getInstance(),
"Error while restoring resource.\n" +
"Near Infinity is unable to recover from the restore operation.\n" +
"Please manually rename the file \"" + tmp + "\" into \"" + cur +
"\", located in \n\"" + path + "\".",
String.format("Please manually rename the file \"%1$s\" into \"%2$s\", located in \n + \"%3$s\"",
tmp, cur, path),
"Critical Error", JOptionPane.ERROR_MESSAGE);
return;
}
Expand All @@ -319,6 +327,13 @@ static void restoreResource(ResourceEntry entry)
JOptionPane.showMessageDialog(NearInfinity.getInstance(),
"Error while restoring resource.\nRestore operation has been cancelled.",
"Error", JOptionPane.ERROR_MESSAGE);
} else if (entry instanceof BIFFResourceEntry && entry.hasOverride()) {
// Biffed and no .bak available -> delete overridden copy
((BIFFResourceEntry)entry).deleteOverride();
JOptionPane.showMessageDialog(NearInfinity.getInstance(),
"Backup has been restored successfully.",
"Restore backup", JOptionPane.INFORMATION_MESSAGE);
return;
}
}
}
Expand Down Expand Up @@ -347,28 +362,47 @@ private static void processAllNodes(JTree tree, TreePath parent, boolean expand)
}
}

/** Returns whether a backup exists in the same folder as the specified resource entry. */
/**
* Returns whether a backup exists in the same folder as the specified resource entry
* or a biffed file has been overriden. */
static boolean isBackupAvailable(ResourceEntry entry)
{
File bakFile = getBackupFile(entry);
return (bakFile != null);
if (entry != null) {
return (getBackupFile(entry) != null ||
(entry instanceof BIFFResourceEntry && entry.hasOverride()));
}
return false;
}

// Returns the backup file of the specified resource entry if available or null.
// A backup file is either a *.bak file or a biffed file which has been overridden.
private static File getBackupFile(ResourceEntry entry)
{
if (entry != null) {
File file = entry.getActualFile();
File file = getCurrentFile(entry);
if (entry instanceof FileResourceEntry || (entry instanceof BIFFResourceEntry && entry.hasOverride())) {
if (file != null) {
File bakFile = new FileNI(file.getPath() + ".bak");
if (bakFile.isFile()) {
return bakFile;
}
}
} else if (entry instanceof BIFFResourceEntry) {
return file;
}
return null;
}

// Returns the actual physical file of the given resource entry or null.
private static File getCurrentFile(ResourceEntry entry)
{
if (entry instanceof FileResourceEntry ||
(entry instanceof BIFFResourceEntry && entry.hasOverride())) {
return entry.getActualFile();
} else {
return null;
}
}

// Returns an unoccupied filename based on 'file'.
private static File getTempFile(File file)
{
Expand Down Expand Up @@ -523,7 +557,7 @@ public void show(Component invoker, int x, int y)
mi_rename.setEnabled(tree.getLastSelectedPathComponent() instanceof FileResourceEntry);
if (tree.getLastSelectedPathComponent() instanceof ResourceEntry) {
ResourceEntry entry = (ResourceEntry)tree.getLastSelectedPathComponent();
mi_delete.setEnabled(entry.hasOverride() || entry instanceof FileResourceEntry);
mi_delete.setEnabled(entry != null && entry.hasOverride() || entry instanceof FileResourceEntry);
mi_restore.setEnabled(isBackupAvailable(entry));
}
else {
Expand Down

0 comments on commit c7c023a

Please sign in to comment.