v0.8.0: improved file handling and added proper file location support to desktop

This commit is contained in:
Evan Debenham
2019-10-27 19:08:21 -04:00
parent b3d4515b32
commit addb991631
4 changed files with 75 additions and 14 deletions

View File

@@ -21,6 +21,7 @@
package com.watabou.utils;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.GdxRuntimeException;
@@ -32,26 +33,61 @@ import java.io.OutputStream;
public class FileUtils {
// Helper methods for setting/using a default base path and file address mode
private static Files.FileType defaultFileType = null;
private static String defaultPath = "";
public static void setDefaultFileProperties( Files.FileType type, String path ){
defaultFileType = type;
defaultPath = path;
}
public static FileHandle getFileHandle( String name ){
return getFileHandle( defaultFileType, defaultPath, name );
}
public static FileHandle getFileHandle( Files.FileType type, String name ){
return getFileHandle( type, "", name );
}
public static FileHandle getFileHandle( Files.FileType type, String basePath, String name ){
switch (type){
case Classpath:
return Gdx.files.classpath( basePath + name );
case Internal:
return Gdx.files.internal( basePath + name );
case External:
return Gdx.files.external( basePath + name );
case Absolute:
return Gdx.files.absolute( basePath + name );
case Local:
return Gdx.files.local( basePath + name );
default:
return null;
}
}
// Files
public static boolean fileExists( String name ){
FileHandle file = Gdx.files.local(name);
FileHandle file = getFileHandle( name );
return file.exists() && !file.isDirectory();
}
public static boolean deleteFile( String name ){
return Gdx.files.local(name).delete();
return getFileHandle( name ).delete();
}
// Directories
public static boolean dirExists( String name ){
FileHandle dir = Gdx.files.local( name );
FileHandle dir = getFileHandle( name );
return dir.exists() && dir.isDirectory();
}
public static boolean deleteDir( String name ){
FileHandle dir = Gdx.files.local( name );
FileHandle dir = getFileHandle( name );
if (dir == null || !dir.isDirectory()){
return false;
@@ -64,7 +100,7 @@ public class FileUtils {
//only works for base path
public static Bundle bundleFromFile( String fileName ) throws IOException{
FileHandle file = Gdx.files.local(fileName);
FileHandle file = getFileHandle( fileName );
if (!file.exists()){
throw new FileNotFoundException("file not found: " + file.path());
} else {
@@ -83,7 +119,7 @@ public class FileUtils {
//only works for base path
public static void bundleToFile( String fileName, Bundle bundle ) throws IOException{
try {
bundleToStream(Gdx.files.local(fileName).write(false), bundle);
bundleToStream(getFileHandle( fileName ).write(false), bundle);
} catch (GdxRuntimeException e){
if (e.getCause() instanceof IOException){
//we want to throw the underlying IOException, not the GdxRuntimeException