Converted to Java 7, factored InstanceLock to own file.
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package mightypork.rogue;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@@ -27,6 +24,7 @@ import mightypork.rogue.events.ActionRequest.RequestType;
|
||||
import mightypork.rogue.screens.test_bouncyboxes.ScreenTestBouncy;
|
||||
import mightypork.rogue.screens.test_cat_sound.ScreenTestCat;
|
||||
import mightypork.rogue.screens.test_font.ScreenTestFont;
|
||||
import mightypork.utils.files.InstanceLock;
|
||||
import mightypork.utils.logging.Log;
|
||||
import mightypork.utils.logging.LogInstance;
|
||||
|
||||
@@ -266,13 +264,13 @@ public class App implements AppAccess {
|
||||
{
|
||||
if (!Config.SINGLE_INSTANCE) return;
|
||||
|
||||
if (!lockInstance()) {
|
||||
System.err.println("Working directory is locked.\nOnly one instance can run at a time.");
|
||||
if (!InstanceLock.onFile(Paths.LOCK)) {
|
||||
System.err.println("Could not obtain lock.\nOnly one instance can run at a time.");
|
||||
|
||||
//@formatter:off
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
"The game is already running.",
|
||||
"Another instance is already running.",
|
||||
"Instance error",
|
||||
JOptionPane.ERROR_MESSAGE
|
||||
);
|
||||
@@ -284,41 +282,6 @@ public class App implements AppAccess {
|
||||
}
|
||||
|
||||
|
||||
private static boolean lockInstance()
|
||||
{
|
||||
final File lockFile = new File(Paths.WORKDIR, ".lock");
|
||||
try {
|
||||
|
||||
final RandomAccessFile randomAccessFile = new RandomAccessFile(lockFile, "rw");
|
||||
|
||||
final FileLock fileLock = randomAccessFile.getChannel().tryLock();
|
||||
if (fileLock != null) {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try {
|
||||
fileLock.release();
|
||||
randomAccessFile.close();
|
||||
lockFile.delete();
|
||||
} catch (final Exception e) {
|
||||
System.err.println("Unable to remove lock file.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
} catch (final Exception e) {
|
||||
System.err.println("Unable to create and/or lock file.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return sound system of the running instance
|
||||
*/
|
||||
|
||||
@@ -9,11 +9,12 @@ import mightypork.utils.files.OsUtils;
|
||||
public class Paths {
|
||||
|
||||
private static final String APPDIR_NAME = "rogue";
|
||||
|
||||
|
||||
public static final File WORKDIR = OsUtils.getWorkDir(APPDIR_NAME);
|
||||
public static final File LOGS = OsUtils.getWorkDir(APPDIR_NAME, "logs");
|
||||
public static final File SCREENSHOTS = OsUtils.getWorkDir(APPDIR_NAME, "screenshots");
|
||||
public static final File CONFIG = new File(WORKDIR, "config.ini");
|
||||
public static final File LOCK = new File(WORKDIR, ".lock");
|
||||
|
||||
public static final String DIR_EFFECTS = "res/sounds/effects/";
|
||||
public static final String DIR_MUSIC = "res/sounds/music/";
|
||||
|
||||
@@ -15,7 +15,7 @@ import mightypork.utils.math.coord.Coord;
|
||||
public class LayerFps extends ScreenLayer {
|
||||
|
||||
TextPainter tp;
|
||||
private FontRenderer fr;
|
||||
private final FontRenderer fr;
|
||||
|
||||
|
||||
public LayerFps(Screen screen) {
|
||||
@@ -28,7 +28,7 @@ public class LayerFps extends ScreenLayer {
|
||||
@Override
|
||||
public void render()
|
||||
{
|
||||
Coord pos = new Coord(DisplaySystem.getWidth() - 8, 8);
|
||||
final Coord pos = new Coord(DisplaySystem.getWidth() - 8, 8);
|
||||
fr.draw(getDisplay().getFps() + " fps", pos, 32, Align.RIGHT);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import mightypork.utils.string.StringProvider;
|
||||
|
||||
public class LayerBouncyBoxes extends ScreenLayer {
|
||||
|
||||
List<BouncyBox> boxes = new ArrayList<BouncyBox>();
|
||||
List<BouncyBox> boxes = new ArrayList<>();
|
||||
private RowHolder layout;
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ public class LayerBouncyBoxes extends ScreenLayer {
|
||||
boxes.add(bbr);
|
||||
}
|
||||
|
||||
StringProvider sp = new StringProvider() {
|
||||
final StringProvider sp = new StringProvider() {
|
||||
|
||||
@Override
|
||||
public String getString()
|
||||
@@ -91,7 +91,7 @@ public class LayerBouncyBoxes extends ScreenLayer {
|
||||
bbr.goRight();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public int getPriority()
|
||||
|
||||
@@ -18,10 +18,10 @@ public class ScreenTestBouncy extends LayeredScreen {
|
||||
super(app);
|
||||
|
||||
layer = new LayerBouncyBoxes(this);
|
||||
|
||||
|
||||
addLayer(new LayerFps(this));
|
||||
|
||||
addLayer(layer);
|
||||
|
||||
addLayer(layer);
|
||||
|
||||
bindKeyStroke(new KeyStroke(Keys.KEY_C), new Runnable() {
|
||||
|
||||
@@ -33,6 +33,7 @@ public class ScreenTestBouncy extends LayeredScreen {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getId()
|
||||
{
|
||||
|
||||
@@ -15,7 +15,6 @@ import mightypork.gamecore.input.InputSystem;
|
||||
import mightypork.gamecore.input.KeyStroke;
|
||||
import mightypork.gamecore.input.Keys;
|
||||
import mightypork.gamecore.render.DisplaySystem;
|
||||
import mightypork.gamecore.render.fonts.FontRenderer;
|
||||
import mightypork.gamecore.render.fonts.FontRenderer.Align;
|
||||
import mightypork.rogue.Res;
|
||||
import mightypork.utils.math.animation.AnimDouble;
|
||||
@@ -106,6 +105,7 @@ public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButt
|
||||
text.render();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getPriority()
|
||||
{
|
||||
|
||||
@@ -14,12 +14,10 @@ import mightypork.rogue.screens.LayerFps;
|
||||
|
||||
public class ScreenTestCat extends LayeredScreen {
|
||||
|
||||
|
||||
|
||||
public ScreenTestCat(AppAccess app) {
|
||||
super(app);
|
||||
|
||||
addLayer(new LayerFps(this));
|
||||
addLayer(new LayerFps(this));
|
||||
addLayer(new LayerFlyingCat(this));
|
||||
|
||||
bindKeyStroke(new KeyStroke(Keys.KEY_ESCAPE), new Runnable() {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package mightypork.rogue.screens.test_font;
|
||||
|
||||
|
||||
import mightypork.gamecore.control.AppAccess;
|
||||
import static mightypork.utils.math.constraints.ConstraintFactory.*;
|
||||
import mightypork.gamecore.control.AppAccess;
|
||||
import mightypork.gamecore.gui.renderers.TextPainter;
|
||||
import mightypork.gamecore.gui.screens.Screen;
|
||||
import mightypork.gamecore.render.fonts.FontRenderer.Align;
|
||||
@@ -22,7 +22,7 @@ public class ScreenTestFont extends Screen {
|
||||
tp = new TextPainter(Res.getFont("default"), Align.CENTER, RGB.GREEN);
|
||||
tp.setText("Hello World!");
|
||||
|
||||
RectConstraint strbox = c_grow(c_center(this), 0, c_div(c_height(this), 10));
|
||||
final RectConstraint strbox = c_grow(c_center(this), 0, c_div(c_height(this), 10));
|
||||
|
||||
tp.setContext(strbox);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user