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

View File

@ -21,27 +21,26 @@
package com.watabou.utils;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
public class GameSettings {
//TODO might want to rename this file. this is the auto-generated name for android atm
public static final String PREFS_FILE = "ShatteredPixelDungeon";
public static final String DEFAULT_PREFS_FILE = "ShatteredPixelDungeon";
private static Preferences prefs;
private static Preferences get() {
if (prefs == null) {
prefs = Gdx.app.getPreferences(PREFS_FILE);
prefs = Gdx.app.getPreferences( DEFAULT_PREFS_FILE );
}
return prefs;
}
//allows setting up of preferences without Gdx.app being initialized
public static void setPrefsFromInstance (Application instance){
prefs = instance.getPreferences(PREFS_FILE);
//allows setting up of preferences directly during game initialization
public static void set( Preferences prefs ){
GameSettings.prefs = prefs;
}
public static boolean contains( String key ){

View File

@ -30,12 +30,14 @@ import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.shatteredpixel.shatteredpixeldungeon.SPDSettings;
import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon;
import com.watabou.noosa.Game;
import com.watabou.noosa.audio.Music;
import com.watabou.utils.FileUtils;
public class AndroidGame extends AndroidApplication {
@ -60,10 +62,12 @@ public class AndroidGame extends AndroidApplication {
} catch (PackageManager.NameNotFoundException e) {
Game.versionCode = 0;
}
FileUtils.setDefaultFileProperties( Files.FileType.Local, "" );
// grab preferences directly using our instance first
// so that we don't need to rely on Gdx.app, which isn't initialized yet.
SPDSettings.setPrefsFromInstance(instance);
SPDSettings.set(instance.getPreferences("ShatteredPixelDungeon"));
//set desired orientation (if it exists) before initializing the app.
if (SPDSettings.landscapeFromSettings() != null) {

View File

@ -21,11 +21,16 @@
package com.shatteredpixel.shatteredpixeldungeon.desktop;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.backends.lwjgl.LwjglPreferences;
import com.badlogic.gdx.utils.SharedLibraryLoader;
import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon;
import com.watabou.noosa.Game;
import com.watabou.utils.FileUtils;
import com.watabou.utils.GameSettings;
import java.io.PrintWriter;
import java.io.StringWriter;
@ -74,6 +79,23 @@ public class DesktopLauncher {
config.foregroundFPS = 0;
config.backgroundFPS = -1;
//TODO this is currently the same location and filenames as the old desktop codebase
// If I want to move it now would be the time
if (SharedLibraryLoader.isWindows) {
if (System.getProperties().getProperty("os.name").equals("Windows XP")) {
config.preferencesDirectory = "Application Data/.shatteredpixel/Shattered Pixel Dungeon/";
} else {
config.preferencesDirectory = "AppData/Roaming/.shatteredpixel/Shattered Pixel Dungeon/";
}
} else if (SharedLibraryLoader.isMac) {
config.preferencesDirectory = "Library/Application Support/Shattered Pixel Dungeon/";
} else if (SharedLibraryLoader.isLinux) {
config.preferencesDirectory = ".shatteredpixel/shattered-pixel-dungeon/";
}
GameSettings.set( new LwjglPreferences( "pd-prefs", config.preferencesDirectory) );
FileUtils.setDefaultFileProperties( Files.FileType.External, config.preferencesDirectory );
new LwjglApplication(new ShatteredPixelDungeon(new DesktopPlatformSupport()), config);
}
}