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-01-23 22:06:57 +01:00
|
|
|
import itdelatrisu.opsu.audio.MusicController;
|
2015-02-01 08:10:17 +01:00
|
|
|
import itdelatrisu.opsu.downloads.DownloadList;
|
2015-02-13 08:43:34 +01:00
|
|
|
import itdelatrisu.opsu.states.ButtonMenu;
|
2015-02-01 08:10:17 +01:00
|
|
|
import itdelatrisu.opsu.states.DownloadsMenu;
|
2014-06-30 04:17:04 +02:00
|
|
|
import itdelatrisu.opsu.states.Game;
|
|
|
|
import itdelatrisu.opsu.states.GamePauseMenu;
|
|
|
|
import itdelatrisu.opsu.states.GameRanking;
|
|
|
|
import itdelatrisu.opsu.states.MainMenu;
|
2015-01-21 05:56:10 +01:00
|
|
|
import itdelatrisu.opsu.states.OptionsMenu;
|
2014-06-30 04:17:04 +02:00
|
|
|
import itdelatrisu.opsu.states.SongMenu;
|
2014-07-06 03:00:52 +02:00
|
|
|
import itdelatrisu.opsu.states.Splash;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
2014-06-30 18:37:37 +02:00
|
|
|
import java.io.IOException;
|
2014-06-30 04:17:04 +02:00
|
|
|
import java.io.PrintStream;
|
2014-06-30 18:37:37 +02:00
|
|
|
import java.net.ServerSocket;
|
2015-01-15 08:47:48 +01:00
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
import org.newdawn.slick.Color;
|
|
|
|
import org.newdawn.slick.GameContainer;
|
|
|
|
import org.newdawn.slick.SlickException;
|
|
|
|
import org.newdawn.slick.state.StateBasedGame;
|
|
|
|
import org.newdawn.slick.state.transition.FadeInTransition;
|
|
|
|
import org.newdawn.slick.state.transition.FadeOutTransition;
|
2014-07-02 07:53:42 +02:00
|
|
|
import org.newdawn.slick.util.ClasspathLocation;
|
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);
|
|
|
|
}
|
|
|
|
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);
|
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
|
|
|
|
try {
|
|
|
|
SERVER_SOCKET = new ServerSocket(Options.getPort());
|
|
|
|
} catch (IOException e) {
|
2015-01-16 03:55:26 +01:00
|
|
|
ErrorHandler.error(String.format("Another program is already running on port %d.", Options.getPort()), e, false);
|
2014-06-30 18:37:37 +02:00
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set path for lwjgl natives - NOT NEEDED if using JarSplice
|
2015-01-15 08:47:48 +01:00
|
|
|
File nativeDir = new File("./target/natives/");
|
|
|
|
if (nativeDir.isDirectory())
|
|
|
|
System.setProperty("org.lwjgl.librarypath", nativeDir.getAbsolutePath());
|
2014-06-30 18:37:37 +02:00
|
|
|
|
2014-07-02 07:53:42 +02:00
|
|
|
// set the resource paths
|
|
|
|
ResourceLoader.removeAllResourceLocations();
|
|
|
|
ResourceLoader.addResourceLocation(new FileSystemLocation(Options.getSkinDir()));
|
|
|
|
ResourceLoader.addResourceLocation(new ClasspathLocation());
|
|
|
|
ResourceLoader.addResourceLocation(new FileSystemLocation(new File(".")));
|
2014-06-30 18:37:37 +02:00
|
|
|
ResourceLoader.addResourceLocation(new FileSystemLocation(new File("./res/")));
|
|
|
|
|
2015-01-28 09:47:24 +01:00
|
|
|
// initialize score database
|
2015-01-29 06:56:30 +01:00
|
|
|
ScoreDB.init();
|
2015-01-28 09:47:24 +01:00
|
|
|
|
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" };
|
|
|
|
app.setIcons(icons);
|
|
|
|
app.setForceExit(true);
|
|
|
|
|
|
|
|
app.start();
|
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
} catch (SlickException e) {
|
2014-07-19 19:01:23 +02:00
|
|
|
// JARs will not run properly inside directories containing '!'
|
|
|
|
// http://bugs.java.com/view_bug.do?bug_id=4523159
|
2014-07-20 19:21:13 +02:00
|
|
|
if (new File("").getAbsolutePath().indexOf('!') != -1)
|
2015-01-16 03:55:26 +01:00
|
|
|
ErrorHandler.error("Cannot run JAR from path containing '!'.", null, false);
|
2014-07-19 19:01:23 +02:00
|
|
|
else
|
2015-01-16 03:55:26 +01:00
|
|
|
ErrorHandler.error("Error while creating game container.", e, true);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
2015-01-27 11:49:58 +01:00
|
|
|
|
2015-01-28 09:47:24 +01:00
|
|
|
Opsu.exit();
|
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();
|
|
|
|
if (data != null && data.isGameplay()) {
|
|
|
|
songMenu.resetGameDataOnLoad();
|
|
|
|
songMenu.resetTrackOnLoad();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
songMenu.resetGameDataOnLoad();
|
|
|
|
if (id == STATE_GAME) {
|
|
|
|
MusicController.pause();
|
|
|
|
MusicController.resume();
|
|
|
|
} else
|
|
|
|
songMenu.resetTrackOnLoad();
|
|
|
|
}
|
2015-01-25 04:23:49 +01:00
|
|
|
Utils.resetCursor();
|
2014-06-30 04:17:04 +02:00
|
|
|
this.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-01 08:10:17 +01:00
|
|
|
// show confirmation dialog if any downloads are active
|
|
|
|
if (DownloadList.get().hasActiveDownloads() && DownloadList.showExitConfirmation())
|
|
|
|
return false;
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-30 18:37:37 +02:00
|
|
|
|
|
|
|
/**
|
2015-01-28 09:47:24 +01:00
|
|
|
* Closes all resources and exits the application.
|
2014-06-30 18:37:37 +02:00
|
|
|
*/
|
2015-01-28 09:47:24 +01:00
|
|
|
public static void exit() {
|
|
|
|
// close scores database
|
2015-01-29 06:56:30 +01:00
|
|
|
ScoreDB.closeConnection();
|
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-01-28 09:47:24 +01:00
|
|
|
|
|
|
|
System.exit(0);
|
2015-01-15 10:16:20 +01:00
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|