2014-06-30 04:17:04 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
2015-01-16 18:05:44 +01:00
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
2014-06-30 04:17:04 +02:00
|
|
|
*
|
|
|
|
* opsu! is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* opsu! is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package itdelatrisu.opsu;
|
|
|
|
|
2015-08-29 04:29:21 +02:00
|
|
|
import itdelatrisu.opsu.audio.MusicController;
|
|
|
|
import itdelatrisu.opsu.db.DBController;
|
|
|
|
import itdelatrisu.opsu.downloads.DownloadList;
|
|
|
|
import itdelatrisu.opsu.downloads.Updater;
|
|
|
|
import itdelatrisu.opsu.states.ButtonMenu;
|
|
|
|
import itdelatrisu.opsu.states.DownloadsMenu;
|
|
|
|
import itdelatrisu.opsu.states.Game;
|
|
|
|
import itdelatrisu.opsu.states.GamePauseMenu;
|
|
|
|
import itdelatrisu.opsu.states.GameRanking;
|
|
|
|
import itdelatrisu.opsu.states.MainMenu;
|
|
|
|
import itdelatrisu.opsu.states.OptionsMenu;
|
|
|
|
import itdelatrisu.opsu.states.SongMenu;
|
|
|
|
import itdelatrisu.opsu.states.Splash;
|
|
|
|
import itdelatrisu.opsu.ui.UI;
|
|
|
|
|
2015-08-27 21:14:04 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintStream;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.ServerSocket;
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
import org.newdawn.slick.GameContainer;
|
2015-08-28 18:07:03 +02:00
|
|
|
import org.newdawn.slick.Input;
|
2014-06-30 04:17:04 +02:00
|
|
|
import org.newdawn.slick.SlickException;
|
|
|
|
import org.newdawn.slick.state.StateBasedGame;
|
|
|
|
import org.newdawn.slick.state.transition.FadeInTransition;
|
2015-11-19 23:47:46 +01:00
|
|
|
import org.newdawn.slick.state.transition.EasedFadeOutTransition;
|
2014-06-30 04:17:04 +02:00
|
|
|
import org.newdawn.slick.util.DefaultLogSystem;
|
|
|
|
import org.newdawn.slick.util.FileSystemLocation;
|
|
|
|
import org.newdawn.slick.util.Log;
|
|
|
|
import org.newdawn.slick.util.ResourceLoader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main class.
|
|
|
|
* <p>
|
|
|
|
* Creates game container, adds all other states, and initializes song data.
|
|
|
|
*/
|
|
|
|
public class Opsu extends StateBasedGame {
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Game states. */
|
2014-06-30 04:17:04 +02:00
|
|
|
public static final int
|
2014-07-06 03:00:52 +02:00
|
|
|
STATE_SPLASH = 0,
|
2014-07-03 05:38:30 +02:00
|
|
|
STATE_MAINMENU = 1,
|
2015-02-13 08:43:34 +01:00
|
|
|
STATE_BUTTONMENU = 2,
|
2014-07-03 05:38:30 +02:00
|
|
|
STATE_SONGMENU = 3,
|
|
|
|
STATE_GAME = 4,
|
|
|
|
STATE_GAMEPAUSEMENU = 5,
|
|
|
|
STATE_GAMERANKING = 6,
|
2015-02-01 08:10:17 +01:00
|
|
|
STATE_OPTIONSMENU = 7,
|
|
|
|
STATE_DOWNLOADSMENU = 8;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Server socket for restricting the program to a single instance. */
|
2014-06-30 18:37:37 +02:00
|
|
|
private static ServerSocket SERVER_SOCKET;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* @param name the program name
|
|
|
|
*/
|
2014-06-30 04:17:04 +02:00
|
|
|
public Opsu(String name) {
|
|
|
|
super(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void initStatesList(GameContainer container) throws SlickException {
|
2014-07-06 03:00:52 +02:00
|
|
|
addState(new Splash(STATE_SPLASH));
|
2014-06-30 04:17:04 +02:00
|
|
|
addState(new MainMenu(STATE_MAINMENU));
|
2015-02-13 08:43:34 +01:00
|
|
|
addState(new ButtonMenu(STATE_BUTTONMENU));
|
2014-06-30 04:17:04 +02:00
|
|
|
addState(new SongMenu(STATE_SONGMENU));
|
|
|
|
addState(new Game(STATE_GAME));
|
|
|
|
addState(new GamePauseMenu(STATE_GAMEPAUSEMENU));
|
|
|
|
addState(new GameRanking(STATE_GAMERANKING));
|
2015-01-21 07:38:02 +01:00
|
|
|
addState(new OptionsMenu(STATE_OPTIONSMENU));
|
2015-02-01 08:10:17 +01:00
|
|
|
addState(new DownloadsMenu(STATE_DOWNLOADSMENU));
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
2015-01-11 20:17:33 +01:00
|
|
|
/**
|
|
|
|
* Launches opsu!.
|
|
|
|
*/
|
2014-06-30 04:17:04 +02:00
|
|
|
public static void main(String[] args) {
|
|
|
|
// log all errors to a file
|
|
|
|
Log.setVerbose(false);
|
|
|
|
try {
|
|
|
|
DefaultLogSystem.out = new PrintStream(new FileOutputStream(Options.LOG_FILE, true));
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
Log.error(e);
|
|
|
|
}
|
2016-12-09 02:00:14 +01:00
|
|
|
|
|
|
|
// set default exception handler
|
2014-06-30 04:17:04 +02:00
|
|
|
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
|
|
|
@Override
|
|
|
|
public void uncaughtException(Thread t, Throwable e) {
|
2015-02-13 21:03:17 +01:00
|
|
|
ErrorHandler.error("** Uncaught Exception! **", e, true);
|
2016-12-09 02:00:14 +01:00
|
|
|
System.exit(1);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-06-30 18:37:37 +02:00
|
|
|
// parse configuration file
|
|
|
|
Options.parseOptions();
|
|
|
|
|
|
|
|
// only allow a single instance
|
2016-10-14 23:54:06 +02:00
|
|
|
if (!Options.noSingleInstance()) {
|
|
|
|
try {
|
|
|
|
SERVER_SOCKET = new ServerSocket(Options.getPort(), 1, InetAddress.getLocalHost());
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
// shouldn't happen
|
|
|
|
} catch (IOException e) {
|
2016-12-10 01:19:31 +01:00
|
|
|
errorAndExit(
|
|
|
|
null,
|
|
|
|
String.format(
|
|
|
|
"opsu! could not be launched for one of these reasons:\n" +
|
|
|
|
"- An instance of opsu! is already running.\n" +
|
|
|
|
"- Another program is bound to port %d. " +
|
|
|
|
"You can change the port opsu! uses by editing the \"Port\" field in the configuration file.",
|
|
|
|
Options.getPort()
|
|
|
|
),
|
|
|
|
false
|
|
|
|
);
|
2016-10-14 23:54:06 +02:00
|
|
|
}
|
2014-06-30 18:37:37 +02:00
|
|
|
}
|
|
|
|
|
2016-12-09 02:00:14 +01:00
|
|
|
// load natives
|
2015-08-26 00:00:34 +02:00
|
|
|
File nativeDir;
|
2015-08-28 18:07:03 +02:00
|
|
|
if (!Utils.isJarRunning() && (
|
|
|
|
(nativeDir = new File("./target/natives/")).isDirectory() ||
|
|
|
|
(nativeDir = new File("./build/natives/")).isDirectory()))
|
2015-08-26 00:00:34 +02:00
|
|
|
;
|
|
|
|
else {
|
2015-08-30 21:31:01 +02:00
|
|
|
nativeDir = Options.NATIVE_DIR;
|
2015-08-26 00:00:34 +02:00
|
|
|
try {
|
2015-08-30 21:31:01 +02:00
|
|
|
new NativeLoader(nativeDir).loadNatives();
|
2015-08-26 00:00:34 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.error("Error loading natives.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.setProperty("org.lwjgl.librarypath", nativeDir.getAbsolutePath());
|
|
|
|
System.setProperty("java.library.path", nativeDir.getAbsolutePath());
|
|
|
|
try {
|
|
|
|
// Workaround for "java.library.path" property being read-only.
|
|
|
|
// http://stackoverflow.com/a/24988095
|
|
|
|
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
|
|
|
|
fieldSysPath.setAccessible(true);
|
|
|
|
fieldSysPath.set(null, null);
|
|
|
|
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
|
|
|
|
Log.warn("Failed to set 'sys_paths' field.", e);
|
|
|
|
}
|
|
|
|
|
2014-07-02 07:53:42 +02:00
|
|
|
// set the resource paths
|
2014-06-30 18:37:37 +02:00
|
|
|
ResourceLoader.addResourceLocation(new FileSystemLocation(new File("./res/")));
|
|
|
|
|
2015-03-05 03:03:06 +01:00
|
|
|
// initialize databases
|
2015-05-07 19:28:40 +02:00
|
|
|
try {
|
|
|
|
DBController.init();
|
|
|
|
} catch (UnsatisfiedLinkError e) {
|
2016-12-09 02:05:08 +01:00
|
|
|
errorAndExit(e, "The databases could not be initialized.", true);
|
2015-05-07 19:28:40 +02:00
|
|
|
}
|
2015-01-28 09:47:24 +01:00
|
|
|
|
2015-03-12 07:38:43 +01:00
|
|
|
// check if just updated
|
|
|
|
if (args.length >= 2)
|
|
|
|
Updater.get().setUpdateInfo(args[0], args[1]);
|
|
|
|
|
2015-03-07 10:17:19 +01:00
|
|
|
// check for updates
|
2015-07-08 02:03:54 +02:00
|
|
|
if (!Options.isUpdaterDisabled()) {
|
|
|
|
new Thread() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Updater.get().checkForUpdates();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.warn("Check for updates failed.", e);
|
|
|
|
}
|
2015-03-07 10:17:19 +01:00
|
|
|
}
|
2015-07-08 02:03:54 +02:00
|
|
|
}.start();
|
|
|
|
}
|
2015-03-07 10:17:19 +01:00
|
|
|
|
2015-08-28 18:07:03 +02:00
|
|
|
// disable jinput
|
|
|
|
Input.disableControllers();
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
// start the game
|
|
|
|
try {
|
2015-01-21 01:01:18 +01:00
|
|
|
// loop until force exit
|
|
|
|
while (true) {
|
|
|
|
Opsu opsu = new Opsu("opsu!");
|
|
|
|
Container app = new Container(opsu);
|
|
|
|
|
|
|
|
// basic game settings
|
|
|
|
Options.setDisplayMode(app);
|
|
|
|
String[] icons = { "icon16.png", "icon32.png" };
|
2016-12-10 14:46:46 +01:00
|
|
|
try {
|
|
|
|
app.setIcons(icons);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.error("could not set icon");
|
|
|
|
}
|
2015-01-21 01:01:18 +01:00
|
|
|
app.setForceExit(true);
|
|
|
|
|
|
|
|
app.start();
|
2015-03-07 10:17:19 +01:00
|
|
|
|
|
|
|
// run update if available
|
|
|
|
if (Updater.get().getStatus() == Updater.Status.UPDATE_FINAL) {
|
|
|
|
close();
|
|
|
|
Updater.get().runUpdate();
|
|
|
|
break;
|
|
|
|
}
|
2015-01-21 01:01:18 +01:00
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
} catch (SlickException e) {
|
2016-12-09 02:05:08 +01:00
|
|
|
errorAndExit(e, "An error occurred while creating the game container.", true);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean closeRequested() {
|
|
|
|
int id = this.getCurrentStateID();
|
|
|
|
|
|
|
|
// intercept close requests in game-related states and return to song menu
|
|
|
|
if (id == STATE_GAME || id == STATE_GAMEPAUSEMENU || id == STATE_GAMERANKING) {
|
|
|
|
// start playing track at preview position
|
2015-01-23 22:11:10 +01:00
|
|
|
SongMenu songMenu = (SongMenu) this.getState(Opsu.STATE_SONGMENU);
|
2015-01-29 02:57:43 +01:00
|
|
|
if (id == STATE_GAMERANKING) {
|
|
|
|
GameData data = ((GameRanking) this.getState(Opsu.STATE_GAMERANKING)).getGameData();
|
2015-08-21 05:58:45 +02:00
|
|
|
if (data != null && data.isGameplay())
|
2015-01-29 02:57:43 +01:00
|
|
|
songMenu.resetTrackOnLoad();
|
|
|
|
} else {
|
|
|
|
if (id == STATE_GAME) {
|
|
|
|
MusicController.pause();
|
2016-10-13 23:51:51 +02:00
|
|
|
MusicController.setPitch(1.0f);
|
2015-01-29 02:57:43 +01:00
|
|
|
MusicController.resume();
|
|
|
|
} else
|
|
|
|
songMenu.resetTrackOnLoad();
|
|
|
|
}
|
2016-12-20 00:33:06 +01:00
|
|
|
|
|
|
|
// reset game data
|
2015-08-24 03:41:09 +02:00
|
|
|
if (UI.getCursor().isBeatmapSkinned())
|
2015-05-29 10:48:03 +02:00
|
|
|
UI.getCursor().reset();
|
2015-08-21 05:58:45 +02:00
|
|
|
songMenu.resetGameDataOnLoad();
|
2016-12-20 00:33:06 +01:00
|
|
|
|
2015-11-19 23:47:46 +01:00
|
|
|
this.enterState(Opsu.STATE_SONGMENU, new EasedFadeOutTransition(), new FadeInTransition());
|
2014-06-30 04:17:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-01 08:10:17 +01:00
|
|
|
// show confirmation dialog if any downloads are active
|
2015-03-07 10:17:19 +01:00
|
|
|
if (DownloadList.get().hasActiveDownloads() &&
|
2015-08-26 00:00:34 +02:00
|
|
|
UI.showExitConfirmation(DownloadList.EXIT_CONFIRMATION))
|
2015-03-07 10:17:19 +01:00
|
|
|
return false;
|
2015-03-07 21:38:59 +01:00
|
|
|
if (Updater.get().getStatus() == Updater.Status.UPDATE_DOWNLOADING &&
|
2015-08-26 00:00:34 +02:00
|
|
|
UI.showExitConfirmation(Updater.EXIT_CONFIRMATION))
|
2015-02-01 08:10:17 +01:00
|
|
|
return false;
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-30 18:37:37 +02:00
|
|
|
|
|
|
|
/**
|
2015-03-07 10:17:19 +01:00
|
|
|
* Closes all resources.
|
2014-06-30 18:37:37 +02:00
|
|
|
*/
|
2015-03-07 10:17:19 +01:00
|
|
|
public static void close() {
|
2015-03-05 03:03:06 +01:00
|
|
|
// close databases
|
|
|
|
DBController.closeConnections();
|
2015-01-28 09:47:24 +01:00
|
|
|
|
2015-02-01 08:10:17 +01:00
|
|
|
// cancel all downloads
|
|
|
|
DownloadList.get().cancelAllDownloads();
|
|
|
|
|
2015-01-28 09:47:24 +01:00
|
|
|
// close server socket
|
2015-01-18 21:26:00 +01:00
|
|
|
if (SERVER_SOCKET != null) {
|
|
|
|
try {
|
|
|
|
SERVER_SOCKET.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
ErrorHandler.error("Failed to close server socket.", e, false);
|
|
|
|
}
|
2015-01-15 10:16:20 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-07 19:28:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2016-12-09 02:05:08 +01:00
|
|
|
* @param report whether to ask to report the error
|
2015-05-07 19:28:40 +02:00
|
|
|
*/
|
2016-12-09 02:05:08 +01:00
|
|
|
private static void errorAndExit(Throwable e, String message, boolean report) {
|
2015-05-07 19:28:40 +02:00
|
|
|
// JARs will not run properly inside directories containing '!'
|
|
|
|
// http://bugs.java.com/view_bug.do?bug_id=4523159
|
|
|
|
if (Utils.isJarRunning() && Utils.getRunningDirectory() != null &&
|
2015-08-26 00:00:34 +02:00
|
|
|
Utils.getRunningDirectory().getAbsolutePath().indexOf('!') != -1)
|
2015-05-07 19:28:40 +02:00
|
|
|
ErrorHandler.error("JARs cannot be run from some paths containing '!'. Please move or rename the file and try again.", null, false);
|
|
|
|
else
|
2016-12-09 02:05:08 +01:00
|
|
|
ErrorHandler.error(message, e, report);
|
2015-05-07 19:28:40 +02:00
|
|
|
System.exit(1);
|
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|