Skip to content

Commit

Permalink
* Show save file in log if both backup and save file have the same time.
Browse files Browse the repository at this point in the history
* Always use save file time instead of remembering it separately.
  • Loading branch information
NathanSweet committed Aug 18, 2022
1 parent 253ef75 commit 2307830
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/com/esotericsoftware/darksoulssaver/DarkSoulsSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class DarkSoulsSaver {

File saveDir = new File("save"), backupDir = new File("backup");
final ArrayList<File> saveFiles, backupFiles;
long lastModified, lastBackupTime, skipBackupTime;
long lastModified, skipBackupTime;
Audio audio;

File saveFile;
Expand Down Expand Up @@ -187,13 +187,10 @@ synchronized void keyPressed (String key) {

// Last backup older than the current save file.
File last;
if (lastBackupTime == 0)
lastBackupTime = backupFiles.get(backupFiles.size() == 1 ? 0 : backupFiles.size() - 2).lastModified();
last = last(backupFiles, lastBackupTime);
last = last(backupFiles, saveFile.lastModified());
print("Replace with previous backup: " + fileNameAndDate(last));
audio.play(Sound.replace);
copy(last, saveFile);
lastBackupTime = last.lastModified();
skipBackup(20);

} else if (key.equals("restart")) {
Expand Down Expand Up @@ -224,14 +221,13 @@ synchronized void keyPressed (String key) {
}
if (!saveFiles.isEmpty()) {
File lastSave = last(saveFiles);
if (last == null || last.lastModified() < lastSave.lastModified()) {
if (last == null || last.lastModified() <= lastSave.lastModified()) {
last = lastSave;
type = "save";
}
}

print("Replace with last " + type + " and restart: " + fileNameAndDate(last));
lastBackupTime = last.lastModified();
stopGame();
audio.play(Sound.replace);
copy(last, saveFile);
Expand Down Expand Up @@ -302,7 +298,7 @@ void skipBackup (int seconds) {
skipBackupTime = System.currentTimeMillis() + seconds * 1000;
}

ArrayList<File> files (File dir, String prefix) {
static ArrayList<File> files (File dir, String prefix) {
ArrayList<File> prefixFiles = new ArrayList();
dir.mkdirs();
File[] files = dir.listFiles();
Expand All @@ -317,7 +313,7 @@ public int compare (File o1, File o2) {
return prefixFiles;
}

int suffix (File file, String prefix) {
static int suffix (File file, String prefix) {
String name = file.getName();
try {
return Integer.parseInt(name.substring(prefix.length(), name.length() - 4));
Expand All @@ -326,13 +322,13 @@ int suffix (File file, String prefix) {
}
}

File last (ArrayList<File> files) {
static File last (ArrayList<File> files) {
return files.get(files.size() - 1);
}

/** @param olderThan Return the newest file older than this, otherwise return the oldest file.
* @return May be null if files is empty. */
File last (ArrayList<File> files, long olderThan) {
static File last (ArrayList<File> files, long olderThan) {
File file = null;
for (int i = files.size() - 1; i >= 0; i--) {
file = files.get(i);
Expand All @@ -341,13 +337,13 @@ File last (ArrayList<File> files, long olderThan) {
return file;
}

int highestSuffix (ArrayList<File> files, String prefix) {
static int highestSuffix (ArrayList<File> files, String prefix) {
if (files.isEmpty()) return 1;
return suffix(last(files), prefix);
}

/** @return May be null. */
File backup (File from, File toDir, ArrayList<File> files, String prefix, int max) {
static File backup (File from, File toDir, ArrayList<File> files, String prefix, int max) {
if (!from.exists()) {
print("File does not exist: " + from.getAbsolutePath());
return null;
Expand All @@ -366,7 +362,7 @@ File backup (File from, File toDir, ArrayList<File> files, String prefix, int ma
}
}

boolean copy (File from, File to) {
static boolean copy (File from, File to) {
if (!from.exists()) {
print("File does not exist: " + from.getAbsolutePath());
return false;
Expand All @@ -383,7 +379,7 @@ boolean copy (File from, File to) {
}
}

void zzz (int millis) {
static void zzz (int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ignored) {
Expand All @@ -404,7 +400,7 @@ static void print (String message) {
System.out.println(buffer);
}

String fileNameAndDate (File file) {
static String fileNameAndDate (File file) {
return file.getName() + " (" + timestamp(file.lastModified()) + ')';
}

Expand Down

0 comments on commit 2307830

Please sign in to comment.