From cfddbfc65026aed97b1187236e73fbab989e3a9b Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Tue, 22 Mar 2022 13:57:09 -0400 Subject: [PATCH] v1.2.0: fixed errors with new save deletion logic --- .../main/java/com/watabou/utils/FileUtils.java | 16 ++++++++++++++-- .../shatteredpixeldungeon/Dungeon.java | 5 +---- .../shatteredpixeldungeon/GamesInProgress.java | 4 ++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/SPD-classes/src/main/java/com/watabou/utils/FileUtils.java b/SPD-classes/src/main/java/com/watabou/utils/FileUtils.java index 72b426c8a..f1968facc 100644 --- a/SPD-classes/src/main/java/com/watabou/utils/FileUtils.java +++ b/SPD-classes/src/main/java/com/watabou/utils/FileUtils.java @@ -118,13 +118,25 @@ public class FileUtils { FileHandle file = getFileHandle( name ); return file.exists() && !file.isDirectory() && file.length() > 0; } + + //returns length of a file in bytes, or 0 if file does not exist + public static long fileLength( String name ){ + FileHandle file = getFileHandle( name ); + if (!file.exists() || file.isDirectory()){ + return 0; + } else { + return file.length(); + } + } public static boolean deleteFile( String name ){ return getFileHandle( name ).delete(); } - public static void setFileEmpty(String name ){ - getFileHandle( name ).writeBytes(new byte[0], true); + //replaces a file with zeroes, for as many bytes as given + //This is helpful as some cloud sync systems do not persist deleted or empty files + public static void zeroFile( String name, int bytes ){ + getFileHandle( name ).writeBytes(new byte[bytes], false); } // Directories diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java index ee583bf43..169031125 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java @@ -672,12 +672,9 @@ public class Dungeon { FileUtils.deleteFile(folder + "/" + file); } } - ArrayList files = FileUtils.filesInDir(GamesInProgress.gameFolder(save)); - FileUtils.deleteDir(GamesInProgress.gameFolder(save)); } - //we empty this file instead of delete due to steam cloud only persisting file deletions locally - FileUtils.setFileEmpty(GamesInProgress.gameFile(save)); + FileUtils.zeroFile(GamesInProgress.gameFile(save), 1); GamesInProgress.delete( save ); } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/GamesInProgress.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/GamesInProgress.java index 6a72080cb..8cc3b6718 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/GamesInProgress.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/GamesInProgress.java @@ -50,7 +50,7 @@ public class GamesInProgress { public static boolean gameExists( int slot ){ return FileUtils.dirExists(gameFolder(slot)) - && FileUtils.fileExists(gameFile(slot)); + && FileUtils.fileLength(gameFile(slot)) > 1; } public static String gameFolder( int slot ){ @@ -74,7 +74,7 @@ public class GamesInProgress { public static ArrayList checkAll(){ ArrayList result = new ArrayList<>(); - for (int i = 0; i <= MAX_SLOTS; i++){ + for (int i = 1; i <= MAX_SLOTS; i++){ Info curr = check(i); if (curr != null) result.add(curr); }