opsu-dance/src/yugecin/opsudance/OpsuDance.java

197 lines
5.2 KiB
Java
Raw Normal View History

2017-01-07 14:02:33 +01:00
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2017 yugecin
*
* opsu!dance 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!dance 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!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance;
2017-01-12 12:45:08 +01:00
import itdelatrisu.opsu.Utils;
2017-01-20 00:18:25 +01:00
import itdelatrisu.opsu.beatmap.BeatmapWatchService;
2017-01-12 12:45:08 +01:00
import itdelatrisu.opsu.db.DBController;
import itdelatrisu.opsu.downloads.DownloadList;
import itdelatrisu.opsu.downloads.Updater;
2017-01-17 23:18:12 +01:00
import itdelatrisu.opsu.states.Splash;
2017-01-12 12:45:08 +01:00
import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.DisplayContainer;
2017-01-16 21:53:48 +01:00
import yugecin.opsudance.core.errorhandling.ErrorHandler;
2017-03-26 22:57:10 +02:00
import yugecin.opsudance.core.inject.Inject;
import yugecin.opsudance.options.Configuration;
import yugecin.opsudance.options.OptionsService;
2017-01-07 14:02:33 +01:00
2017-01-12 12:45:08 +01:00
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
2017-01-16 21:53:48 +01:00
import static yugecin.opsudance.core.Entrypoint.sout;
2017-03-26 22:57:10 +02:00
import static yugecin.opsudance.options.Options.*;
2017-01-09 23:10:51 +01:00
2017-01-12 12:45:08 +01:00
/*
* loosely based on itdelatrisu.opsu.Opsu
*/
2017-01-07 14:02:33 +01:00
public class OpsuDance {
2017-03-26 22:57:10 +02:00
@Inject
private DisplayContainer container;
@Inject
private OptionsService optionsService;
@Inject
private Configuration config;
@Inject
private Updater updater;
2017-01-07 14:02:33 +01:00
2017-01-12 12:45:08 +01:00
private ServerSocket singleInstanceSocket;
2017-03-26 22:57:10 +02:00
@Inject
public OpsuDance() {
2017-01-07 14:02:33 +01:00
}
2017-01-12 12:45:08 +01:00
public void start(String[] args) {
2017-01-15 00:21:52 +01:00
try {
sout("initialized");
2017-01-12 12:45:08 +01:00
2017-01-15 00:21:52 +01:00
checkRunningDirectory();
2017-03-26 22:57:10 +02:00
optionsService.loadOptions();
2017-01-15 00:21:52 +01:00
ensureSingleInstance();
sout("prechecks done and options parsed");
2017-01-12 12:45:08 +01:00
2017-01-15 00:21:52 +01:00
initDatabase();
initUpdater(args);
sout("database & updater initialized");
2017-01-12 12:45:08 +01:00
2017-01-17 23:18:12 +01:00
container.init(Splash.class);
2017-01-15 00:21:52 +01:00
} catch (Exception e) {
errorAndExit("startup failure", e);
}
2017-01-12 12:45:08 +01:00
2017-01-11 20:41:13 +01:00
while (rungame());
2017-01-18 19:22:14 +01:00
container.teardownAL();
2017-01-12 12:45:08 +01:00
2017-03-26 22:57:10 +02:00
optionsService.saveOptions();
2017-01-12 12:45:08 +01:00
closeSingleInstanceSocket();
DBController.closeConnections();
DownloadList.get().cancelAllDownloads();
2017-03-26 22:57:10 +02:00
Utils.deleteDirectory(config.TEMP_DIR);
if (!OPTION_ENABLE_WATCH_SERVICE.state) {
2017-01-20 00:18:25 +01:00
BeatmapWatchService.destroy();
}
2017-01-11 20:41:13 +01:00
}
private boolean rungame() {
2017-01-07 14:02:33 +01:00
try {
2017-01-11 20:41:13 +01:00
container.setup();
2017-01-18 19:22:14 +01:00
container.resume();
2017-01-15 00:21:52 +01:00
} catch (Exception e) {
ErrorHandler.error("could not initialize GL", e).allowTerminate().preventContinue().show();
return false;
2017-01-11 20:41:13 +01:00
}
Exception caughtException = null;
try {
container.run();
} catch (Exception e) {
caughtException = e;
2017-01-07 14:02:33 +01:00
}
2017-01-11 20:41:13 +01:00
container.teardown();
2017-01-18 19:22:14 +01:00
container.pause();
return caughtException != null && ErrorHandler.error("update/render error", caughtException).allowTerminate().show().shouldIgnoreAndContinue();
2017-01-07 14:02:33 +01:00
}
2017-01-12 12:45:08 +01:00
private void initDatabase() {
try {
2017-03-26 22:57:10 +02:00
DBController.init(config);
2017-01-12 12:45:08 +01:00
} catch (UnsatisfiedLinkError e) {
errorAndExit("Could not initialize database.", e);
}
}
private void initUpdater(String[] args) {
// check if just updated
2017-03-26 22:57:10 +02:00
if (args.length >= 2) {
updater.setUpdateInfo(args[0], args[1]);
}
2017-01-12 12:45:08 +01:00
// check for updates
2017-03-26 22:57:10 +02:00
if (OPTION_DISABLE_UPDATER.state) {
2017-01-12 12:45:08 +01:00
return;
}
new Thread() {
@Override
public void run() {
try {
2017-03-26 22:57:10 +02:00
updater.checkForUpdates();
2017-01-12 12:45:08 +01:00
} catch (IOException e) {
Log.warn("updatecheck failed.", e);
}
}
}.start();
}
private void checkRunningDirectory() {
if (!Utils.isJarRunning()) {
return;
}
File runningDir = Utils.getRunningDirectory();
if (runningDir == null) {
return;
}
if (runningDir.getAbsolutePath().indexOf('!') == -1) {
return;
}
errorAndExit("Cannot run from a path that contains a '!'. Please move or rename the jar and try again.");
}
private void ensureSingleInstance() {
2017-03-26 22:57:10 +02:00
if (OPTION_NOSINGLEINSTANCE.state) {
2017-01-12 12:45:08 +01:00
return;
}
try {
2017-03-26 22:57:10 +02:00
singleInstanceSocket = new ServerSocket(OPTION_PORT.val, 1, InetAddress.getLocalHost());
2017-01-12 12:45:08 +01:00
} catch (UnknownHostException e) {
// shouldn't happen
} catch (IOException e) {
errorAndExit(String.format(
"Could not launch. Either opsu! is already running or a different program uses port %d.\n" +
"You can change the port opsu! uses by editing the 'Port' field in the .opsu.cfg configuration file.\n" +
2017-03-26 22:57:10 +02:00
"If that still does not resolve the problem, you can set 'NoSingleInstance' to 'true', but this is not recommended.", OPTION_PORT.val), e);
2017-01-12 12:45:08 +01:00
}
}
private void closeSingleInstanceSocket() {
if (singleInstanceSocket == null) {
return;
}
try {
singleInstanceSocket.close();
} catch (IOException e) {
Log.error("Single instance socket was not closed!", e);
}
}
private void errorAndExit(String errstr) {
ErrorHandler.error(errstr, new Throwable()).allowTerminate().preventContinue().show();
2017-01-12 12:45:08 +01:00
System.exit(1);
}
private void errorAndExit(String errstr, Throwable cause) {
ErrorHandler.error(errstr, cause).preventContinue().show();
System.exit(1);
}
2017-01-07 14:02:33 +01:00
}