diff --git a/src/itdelatrisu/opsu/Opsu.java b/src/itdelatrisu/opsu/Opsu.java index 078e791d..3524863b 100644 --- a/src/itdelatrisu/opsu/Opsu.java +++ b/src/itdelatrisu/opsu/Opsu.java @@ -135,7 +135,11 @@ public class Opsu extends StateBasedGame { ResourceLoader.addResourceLocation(new FileSystemLocation(new File("./res/"))); // initialize databases - DBController.init(); + try { + DBController.init(); + } catch (UnsatisfiedLinkError e) { + errorAndExit(e, "The databases could not be initialized."); + } // check if just updated if (args.length >= 2) @@ -176,12 +180,7 @@ public class Opsu extends StateBasedGame { } } } catch (SlickException e) { - // JARs will not run properly inside directories containing '!' - // http://bugs.java.com/view_bug.do?bug_id=4523159 - if (new File("").getAbsolutePath().indexOf('!') != -1) - ErrorHandler.error("Cannot run JAR from path containing '!'.", null, false); - else - ErrorHandler.error("Error while creating game container.", e, true); + errorAndExit(e, "An error occurred while creating the game container."); } } @@ -242,4 +241,20 @@ public class Opsu extends StateBasedGame { } } } + + /** + * Throws an error and exits the application with the given message. + * @param e the exception that caused the crash + * @param message the message to display + */ + private static void errorAndExit(Throwable e, String message) { + // JARs will not run properly inside directories containing '!' + // http://bugs.java.com/view_bug.do?bug_id=4523159 + if (Utils.isJarRunning() && Utils.getRunningDirectory() != null && + Utils.getRunningDirectory().getAbsolutePath().indexOf('!') != -1) + ErrorHandler.error("JARs cannot be run from some paths containing '!'. Please move or rename the file and try again.", null, false); + else + ErrorHandler.error(message, e, true); + System.exit(1); + } } diff --git a/src/itdelatrisu/opsu/Utils.java b/src/itdelatrisu/opsu/Utils.java index 7035825e..379ff08b 100644 --- a/src/itdelatrisu/opsu/Utils.java +++ b/src/itdelatrisu/opsu/Utils.java @@ -35,6 +35,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.SocketTimeoutException; +import java.net.URISyntaxException; import java.net.URL; import java.nio.ByteBuffer; import java.security.MessageDigest; @@ -633,4 +634,24 @@ public class Utils { return easeOut(t, a, b, d); return easeOut(d - t, a, b, d); } + + /** + * Returns whether or not the application is running within a JAR. + * @return true if JAR, false if file + */ + public static boolean isJarRunning() { + return Opsu.class.getResource(String.format("%s.class", Opsu.class.getSimpleName())).toString().startsWith("jar:"); + } + + /** + * Returns the directory where the application is being run. + */ + public static File getRunningDirectory() { + try { + return new File(Opsu.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); + } catch (URISyntaxException e) { + Log.error("Could not get the running directory.", e); + return null; + } + } }