From ed2d3058ba660be7657a5a1b0cbac94cc05c68ff Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Fri, 13 Feb 2015 15:03:17 -0500 Subject: [PATCH] Merge pull request #16 from SuperNascher/master Implemented XDG base directory support for Unix-like operating systems. This is disabled by default. To use these directories, uncomment the line in pom.xml that sets the "XDG" system property when packaging opsu!. Signed-off-by: Jeffrey Han --- pom.xml | 2 ++ src/itdelatrisu/opsu/Opsu.java | 4 +-- src/itdelatrisu/opsu/Options.java | 48 ++++++++++++++++++++++++++----- src/itdelatrisu/opsu/ScoreDB.java | 2 +- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index c7cec84f..b1a006aa 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,8 @@ -Dinput=opsu-${project.version}.jar -Dmain=itdelatrisu.opsu.Opsu -Doutput=opsu-${project.version}-runnable.jar + + ${basedir}/tools/JarSplicePlus.jar diff --git a/src/itdelatrisu/opsu/Opsu.java b/src/itdelatrisu/opsu/Opsu.java index e1a7b01f..1a81726c 100644 --- a/src/itdelatrisu/opsu/Opsu.java +++ b/src/itdelatrisu/opsu/Opsu.java @@ -105,9 +105,7 @@ public class Opsu extends StateBasedGame { Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { - if (!(e instanceof ThreadDeath)) { // TODO: see MusicController - ErrorHandler.error("** Uncaught Exception! **", e, true); - } + ErrorHandler.error("** Uncaught Exception! **", e, true); } }); diff --git a/src/itdelatrisu/opsu/Options.java b/src/itdelatrisu/opsu/Options.java index 3b1aa1c2..ef2a446e 100644 --- a/src/itdelatrisu/opsu/Options.java +++ b/src/itdelatrisu/opsu/Options.java @@ -42,21 +42,27 @@ import org.newdawn.slick.util.Log; * Handles all user options. */ public class Options { + /** The config directory. */ + private static final File CONFIG_DIR = getXDGBaseDir("XDG_CONFIG_HOME", ".config"); + + /** The data directory. */ + private static final File DATA_DIR = getXDGBaseDir("XDG_DATA_HOME", ".local/share"); + /** File for logging errors. */ - public static final File LOG_FILE = new File(".opsu.log"); + public static final File LOG_FILE = new File(CONFIG_DIR, ".opsu.log"); /** File for storing user options. */ - private static final File OPTIONS_FILE = new File(".opsu.cfg"); + private static final File OPTIONS_FILE = new File(CONFIG_DIR, ".opsu.cfg"); /** Beatmap directories (where to search for files). */ private static final String[] BEATMAP_DIRS = { "C:/Program Files (x86)/osu!/Songs/", "C:/Program Files/osu!/Songs/", - "Songs/" + new File(DATA_DIR, "Songs/").getPath() }; /** Score database name. */ - public static final String SCORE_DB = ".opsu_scores.db"; + public static final File SCORE_DB = new File(DATA_DIR, ".opsu_scores.db"); /** Font file name. */ public static final String FONT_NAME = "kochi-gothic.ttf"; @@ -88,6 +94,34 @@ public class Options { /** The current skin directory (for user skins). */ private static File skinDir; + /** + * Returns the directory based on the XDG base directory specification for + * Unix-like operating systems, only if the system property "XDG" has been defined. + * @param env the environment variable to check (XDG_*_*) + * @param fallback the fallback directory relative to ~home + * @return the XDG base directory, or the working directory if unavailable + */ + private static File getXDGBaseDir(String env, String fallback) { + if (System.getProperty("XDG") == null) + return new File("./"); + + String OS = System.getProperty("os.name").toLowerCase(); + if (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0) { + String rootPath = System.getenv(env); + if (rootPath == null) { + String home = System.getProperty("user.home"); + if (home == null) + return new File("./"); + rootPath = String.format("%s/%s", home, fallback); + } + File dir = new File(rootPath, "opsu"); + if (!dir.isDirectory()) + dir.mkdir(); + return dir; + } else + return new File("./"); + } + /** * The theme song string: * {@code filename,title,artist,length(ms)} @@ -803,7 +837,7 @@ public class Options { if (oszDir != null && oszDir.isDirectory()) return oszDir; - oszDir = new File("SongPacks/"); + oszDir = new File(DATA_DIR, "SongPacks/"); oszDir.mkdir(); return oszDir; } @@ -817,7 +851,7 @@ public class Options { if (screenshotDir != null && screenshotDir.isDirectory()) return screenshotDir; - screenshotDir = new File("Screenshots/"); + screenshotDir = new File(DATA_DIR, "Screenshots/"); return screenshotDir; } @@ -830,7 +864,7 @@ public class Options { if (skinDir != null && skinDir.isDirectory()) return skinDir; - skinDir = new File("Skins/"); + skinDir = new File(DATA_DIR, "Skins/"); skinDir.mkdir(); return skinDir; } diff --git a/src/itdelatrisu/opsu/ScoreDB.java b/src/itdelatrisu/opsu/ScoreDB.java index 77e44980..b7fc6aca 100644 --- a/src/itdelatrisu/opsu/ScoreDB.java +++ b/src/itdelatrisu/opsu/ScoreDB.java @@ -63,7 +63,7 @@ public class ScoreDB { // create a database connection try { - connection = DriverManager.getConnection(String.format("jdbc:sqlite:%s", Options.SCORE_DB)); + connection = DriverManager.getConnection(String.format("jdbc:sqlite:%s", Options.SCORE_DB.getPath())); } catch (SQLException e) { // if the error message is "out of memory", it probably means no database file is found ErrorHandler.error("Could not connect to score database.", e, true);