v1.2.0: deleted games now empty the main file instead of deleting it

This commit is contained in:
Evan Debenham 2022-03-21 21:19:56 -04:00
parent c35e750ff0
commit 077bdafe41
4 changed files with 31 additions and 9 deletions

View File

@ -29,6 +29,7 @@ import com.badlogic.gdx.utils.GdxRuntimeException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
public class FileUtils {
@ -115,12 +116,16 @@ public class FileUtils {
public static boolean fileExists( String name ){
FileHandle file = getFileHandle( name );
return file.exists() && !file.isDirectory();
return file.exists() && !file.isDirectory() && file.length() > 0;
}
public static boolean deleteFile( String name ){
return getFileHandle( name ).delete();
}
public static void setFileEmpty(String name ){
getFileHandle( name ).writeBytes(new byte[0], true);
}
// Directories
@ -138,6 +143,17 @@ public class FileUtils {
return dir.deleteDirectory();
}
}
public static ArrayList<String> filesInDir( String name ){
FileHandle dir = getFileHandle( name );
ArrayList result = new ArrayList();
if (dir != null && dir.isDirectory()){
for (FileHandle file : dir.list()){
result.add(file.name());
}
}
return result;
}
// bundle reading

View File

@ -37,7 +37,6 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.npcs.Blacksmith;
import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.npcs.Ghost;
import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.npcs.Imp;
import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.npcs.Wandmaker;
import com.shatteredpixel.shatteredpixeldungeon.items.Ankh;
import com.shatteredpixel.shatteredpixeldungeon.items.Generator;
import com.shatteredpixel.shatteredpixeldungeon.items.Heap;
import com.shatteredpixel.shatteredpixeldungeon.items.Item;
@ -53,7 +52,6 @@ import com.shatteredpixel.shatteredpixeldungeon.levels.CityLevel;
import com.shatteredpixel.shatteredpixeldungeon.levels.DeadEndLevel;
import com.shatteredpixel.shatteredpixeldungeon.levels.HallsLevel;
import com.shatteredpixel.shatteredpixeldungeon.levels.LastLevel;
import com.shatteredpixel.shatteredpixeldungeon.levels.LastShopLevel;
import com.shatteredpixel.shatteredpixeldungeon.levels.Level;
import com.shatteredpixel.shatteredpixeldungeon.levels.CavesBossLevel;
import com.shatteredpixel.shatteredpixeldungeon.levels.CityBossLevel;
@ -666,12 +664,20 @@ public class Dungeon {
}
public static void deleteGame( int save, boolean deleteLevels ) {
FileUtils.deleteFile(GamesInProgress.gameFile(save));
if (deleteLevels) {
String folder = GamesInProgress.gameFolder(save);
for (String file : FileUtils.filesInDir(folder)){
if (file.contains("depth")){
FileUtils.deleteFile(folder + "/" + file);
}
}
ArrayList<String> 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));
GamesInProgress.delete( save );
}

View File

@ -49,7 +49,8 @@ public class GamesInProgress {
private static final String DEPTH_FILE = "depth%d.dat";
public static boolean gameExists( int slot ){
return FileUtils.dirExists(Messages.format(GAME_FOLDER, slot));
return FileUtils.dirExists(gameFolder(slot))
&& FileUtils.fileExists(gameFile(slot));
}
public static String gameFolder( int slot ){

View File

@ -149,8 +149,7 @@ public class WndGameInProgress extends Window {
@Override
protected void onSelect( int index ) {
if (index == 0) {
FileUtils.deleteDir(GamesInProgress.gameFolder(slot));
GamesInProgress.setUnknown(slot);
Dungeon.deleteGame(slot, true);
ShatteredPixelDungeon.switchNoFade(StartScene.class);
}
}