v0.7.4b: refactored file handling to use String instead of File
This commit is contained in:
parent
7885da4b99
commit
785726f4f8
|
@ -24,9 +24,7 @@ package com.watabou.utils;
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.files.FileHandle;
|
import com.badlogic.gdx.files.FileHandle;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
@ -41,27 +39,10 @@ public class FileUtils {
|
||||||
return file.exists() && !file.isDirectory();
|
return file.exists() && !file.isDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File getFile( String name ){
|
|
||||||
return Gdx.files.local(name).file();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static File getFile( File base, String name ){
|
|
||||||
File file = new File(base, name);
|
|
||||||
if (!file.exists() || !file.isDirectory()){
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean deleteFile( String name ){
|
public static boolean deleteFile( String name ){
|
||||||
return Gdx.files.local(name).delete();
|
return Gdx.files.local(name).delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean deleteFile( File file ){
|
|
||||||
return !file.isDirectory() && file.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Directories
|
// Directories
|
||||||
|
|
||||||
public static boolean dirExists( String name ){
|
public static boolean dirExists( String name ){
|
||||||
|
@ -69,50 +50,26 @@ public class FileUtils {
|
||||||
return dir.exists() && dir.isDirectory();
|
return dir.exists() && dir.isDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
//base directory
|
|
||||||
public static File getDir( String name ){
|
|
||||||
return Gdx.files.local( name ).file();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static File getDir( File base, String name ){
|
|
||||||
File dir = new File(base, name);
|
|
||||||
if (!dir.exists() && dir.mkdirs()){
|
|
||||||
return dir;
|
|
||||||
} else if (dir.isDirectory()){
|
|
||||||
return dir;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean deleteDir( String name ){
|
public static boolean deleteDir( String name ){
|
||||||
return deleteDir(getDir(name));
|
FileHandle dir = Gdx.files.local( name );
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean deleteDir( File dir ){
|
|
||||||
if (dir == null || !dir.isDirectory()){
|
if (dir == null || !dir.isDirectory()){
|
||||||
return false;
|
return false;
|
||||||
|
} else {
|
||||||
|
return dir.deleteDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (File f : dir.listFiles()){
|
|
||||||
if (f.isDirectory()){
|
|
||||||
if (!deleteDir(f)) return false;
|
|
||||||
} else {
|
|
||||||
if (!deleteFile(f)) return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return dir.delete();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// bundle reading
|
// bundle reading
|
||||||
|
|
||||||
//only works for base path
|
//only works for base path
|
||||||
public static Bundle bundleFromFile( String fileName ) throws IOException{
|
public static Bundle bundleFromFile( String fileName ) throws IOException{
|
||||||
return bundleFromStream(Gdx.files.local(fileName).read());
|
FileHandle file = Gdx.files.local(fileName);
|
||||||
}
|
if (!file.exists()){
|
||||||
|
throw new FileNotFoundException("file not found: " + file.path());
|
||||||
public static Bundle bundleFromFile( File file ) throws IOException{
|
} else {
|
||||||
return bundleFromStream(new FileInputStream(file));
|
return bundleFromStream(file.read());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Bundle bundleFromStream( InputStream input ) throws IOException{
|
private static Bundle bundleFromStream( InputStream input ) throws IOException{
|
||||||
|
@ -128,10 +85,6 @@ public class FileUtils {
|
||||||
bundleToStream( Gdx.files.local(fileName).write(false), bundle);
|
bundleToStream( Gdx.files.local(fileName).write(false), bundle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void bundleToFile( File file, Bundle bundle ) throws IOException{
|
|
||||||
bundleToStream( new FileOutputStream(file), bundle);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void bundleToStream( OutputStream output, Bundle bundle ) throws IOException{
|
private static void bundleToStream( OutputStream output, Bundle bundle ) throws IOException{
|
||||||
Bundle.write( bundle, output );
|
Bundle.write( bundle, output );
|
||||||
output.close();
|
output.close();
|
||||||
|
|
|
@ -30,9 +30,7 @@ import android.os.ParcelFileDescriptor;
|
||||||
|
|
||||||
import com.shatteredpixel.shatteredpixeldungeon.Badges;
|
import com.shatteredpixel.shatteredpixeldungeon.Badges;
|
||||||
import com.shatteredpixel.shatteredpixeldungeon.Rankings;
|
import com.shatteredpixel.shatteredpixeldungeon.Rankings;
|
||||||
import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon;
|
|
||||||
import com.shatteredpixel.shatteredpixeldungeon.journal.Journal;
|
import com.shatteredpixel.shatteredpixeldungeon.journal.Journal;
|
||||||
import com.watabou.utils.FileUtils;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
|
@ -52,12 +50,29 @@ public class AndroidBackupHandler extends BackupAgent {
|
||||||
//does not backup runs in progress, to prevent cheating.
|
//does not backup runs in progress, to prevent cheating.
|
||||||
|
|
||||||
//store shared preferences
|
//store shared preferences
|
||||||
fullBackupFile(new File(getFilesDir().getParent() + "/shared_prefs/"+ ShatteredPixelDungeon.class.getCanonicalName() + ".xml"), data);
|
fullBackupFile(new File(getFilesDir().getParent() + "/shared_prefs/ShatteredPixelDungeon.xml"), data);
|
||||||
|
|
||||||
//store game data
|
//store game data
|
||||||
fullBackupFile(FileUtils.getFile( getFilesDir(), Rankings.RANKINGS_FILE ), data);
|
File file = getFile( getFilesDir(), Rankings.RANKINGS_FILE );
|
||||||
fullBackupFile(FileUtils.getFile( getFilesDir(), Badges.BADGES_FILE ), data);
|
if (file != null){
|
||||||
fullBackupFile(FileUtils.getFile( getFilesDir(), Journal.JOURNAL_FILE ), data);
|
fullBackupFile( file , data);
|
||||||
|
}
|
||||||
|
file = getFile( getFilesDir(), Badges.BADGES_FILE );
|
||||||
|
if (file != null){
|
||||||
|
fullBackupFile( file , data);
|
||||||
|
}
|
||||||
|
file = getFile( getFilesDir(), Journal.JOURNAL_FILE );
|
||||||
|
if (file != null){
|
||||||
|
fullBackupFile( file , data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static File getFile( File base, String name ){
|
||||||
|
File file = new File(base, name);
|
||||||
|
if (!file.exists() || !file.isDirectory()){
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,6 @@ import com.shatteredpixel.shatteredpixeldungeon.messages.Messages;
|
||||||
import com.watabou.utils.Bundle;
|
import com.watabou.utils.Bundle;
|
||||||
import com.watabou.utils.FileUtils;
|
import com.watabou.utils.FileUtils;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -53,16 +52,16 @@ public class GamesInProgress {
|
||||||
return FileUtils.dirExists(Messages.format(GAME_FOLDER, slot));
|
return FileUtils.dirExists(Messages.format(GAME_FOLDER, slot));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File gameFolder( int slot ){
|
public static String gameFolder( int slot ){
|
||||||
return FileUtils.getDir(Messages.format(GAME_FOLDER, slot));
|
return Messages.format(GAME_FOLDER, slot);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File gameFile( int slot ){
|
public static String gameFile( int slot ){
|
||||||
return FileUtils.getFile(gameFolder( slot ), GAME_FILE);
|
return gameFolder(slot) + "/" + GAME_FILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File depthFile( int slot, int depth ) {
|
public static String depthFile( int slot, int depth ) {
|
||||||
return FileUtils.getFile( gameFolder(slot), Messages.format(DEPTH_FILE, depth));
|
return gameFolder(slot) + "/" + Messages.format(DEPTH_FILE, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int firstEmpty(){
|
public static int firstEmpty(){
|
||||||
|
|
Loading…
Reference in New Issue
Block a user