Move the headless state handling mostly into FreeCol.

This commit is contained in:
Mike Pope 2019-02-27 09:43:14 +10:30
parent e6d5ef9ef3
commit 3e9df7d8be
5 changed files with 58 additions and 46 deletions

View File

@ -64,6 +64,7 @@ import static net.sf.freecol.common.util.CollectionUtils.*;
import net.sf.freecol.common.util.LogBuilder;
import net.sf.freecol.common.util.OSUtils;
import static net.sf.freecol.common.util.StringUtils.*;
import net.sf.freecol.common.util.Utils;
import net.sf.freecol.server.FreeColServer;
import net.sf.freecol.server.control.Controller;
@ -353,7 +354,11 @@ public final class FreeCol {
FreeColModFile.loadMods();
Messages.loadModMessageBundle(getLocale());
// Handle other special options
// Sort out the special graphics options before touching the GUI
// (which is initialized by FreeColClient). These options control
// graphics pipeline initialization, and are ineffective if twiddled
// after the first Java2D call is made.
headless |= Utils.isHeadless();
processSpecialOptions();
// Report on where we are.
@ -363,6 +368,12 @@ public final class FreeCol {
if (standAloneServer) {
startServer();
} else {
if (headless) {
if (!FreeColDebugger.isInDebugMode()
|| FreeColDebugger.getDebugRunTurns() <= 0) {
fatal(logger, Messages.message("client.headlessDebug"));
}
}
startClient();
}
}
@ -951,6 +962,27 @@ public final class FreeCol {
// Accessors, mutators and support for the cli variables.
/**
* Are we in headless mode, either externally true or explicitly
* from the command line.
*
* @return True if in headless mode.
*/
public static boolean getHeadless() {
return headless;
}
/**
* Set the headless state.
*
* Used by the test suite.
*
* @param newHeadless The new headless state.
*/
public static void setHeadless(boolean newHeadless) {
headless = newHeadless;
}
/**
* Gets the default advantages type.
*
@ -1501,14 +1533,8 @@ public final class FreeCol {
// savegame was specified on command line
}
// Sort out the special graphics options before touching the GUI
// (which is initialized by FreeColClient). These options control
// graphics pipeline initialization, and are ineffective if twiddled
// after the first Java2D call is made.
final FreeColClient freeColClient
= new FreeColClient(splashStream, fontName, guiScale, headless);
= new FreeColClient(splashStream, fontName, guiScale);
freeColClient.startClient(windowSize, null, sound, introVideo,
savegame, spec);
}

View File

@ -130,20 +130,12 @@ public final class FreeColClient {
*/
private boolean loggedIn = false;
/** Run in headless mode. */
private final boolean headless;
/** Cached value of server state. */
private ServerState cachedServerState = null;
/** Cached list of vacant players. */
private List<String> cachedVacantPlayerNames = new ArrayList<>();
public FreeColClient(final InputStream splashStream,
final String fontName) {
this(splashStream, fontName, FreeCol.GUI_SCALE_DEFAULT, true);
}
/**
* Creates a new {@code FreeColClient}. Creates the control
@ -152,22 +144,14 @@ public final class FreeColClient {
* @param splashStream A stream to read the splash image from.
* @param fontName An optional override of the main font.
* @param scale The scale factor for gui elements.
* @param headless Run in headless mode.
*/
public FreeColClient(final InputStream splashStream, final String fontName,
final float scale, boolean headless) {
final float scale) {
mapEditor = false;
this.headless = headless || Utils.isHeadless();
if (this.headless) {
if (!FreeColDebugger.isInDebugMode()
|| FreeColDebugger.getDebugRunTurns() <= 0) {
FreeCol.fatal(logger, Messages.message("client.headlessDebug"));
}
}
// Get the splash screen up early on to show activity.
gui = (this.headless) ? new GUI(this, scale)
: new SwingGUI(this, scale);
gui = (FreeCol.getHeadless()) ? new GUI(this, scale)
: new SwingGUI(this, scale);
gui.displaySplashScreen(splashStream);
// Look for base data directory. Failure is fatal.
@ -216,7 +200,7 @@ public final class FreeColClient {
ResourceManager.setTcMapping(tcData.getResourceMapping());
// Swing system and look-and-feel initialization.
if (!this.headless) {
if (!FreeCol.getHeadless()) {
try {
gui.installLookAndFeel(fontName);
} catch (Exception e) {
@ -228,6 +212,20 @@ public final class FreeColClient {
actionManager.initializeActions(inGameController, connectController);
}
/**
* Wrapper for the test suite to start a test client.
*
* @param spec The {@code Specification} to use in the new client.
* @return The new {@code FreeColClient}.
*/
public static FreeColClient startTestClient(Specification spec) {
FreeCol.setHeadless(true);
FreeColClient freeColClient
= new FreeColClient(null, null, FreeCol.GUI_SCALE_DEFAULT);
freeColClient.startClient(null, null, false, false, null, spec);
return freeColClient;
}
/**
* Handy utility to create a runnable to restart the main panel.
*
@ -263,7 +261,7 @@ public final class FreeColClient {
final boolean showOpeningVideo,
final File savedGame,
final Specification spec) {
if (headless && savedGame == null && spec == null) {
if (FreeCol.getHeadless() && savedGame == null && spec == null) {
FreeCol.fatal(logger, Messages.message("client.headlessRequires"));
}
@ -621,15 +619,6 @@ public final class FreeColClient {
return this.loggedIn;
}
/**
* Is the game in headless mode?
*
* @return a {@code boolean} value
*/
public boolean isHeadless() {
return this.headless;
}
/**
* Get the server state, or at least our most recently cached value.
*
@ -773,7 +762,7 @@ public final class FreeColClient {
private FreeColServer failToMain(Exception ex, StringTemplate template) {
GUI.ErrorJob ej = gui.errorJob(ex, template);
logger.log(Level.WARNING, Messages.message(template), ex);
if (isHeadless() // If this is a debug run, fail hard.
if (FreeCol.getHeadless() // If this is a debug run, fail hard.
|| FreeColDebugger.getDebugRunTurns() >= 0) {
FreeCol.fatal(logger, ej.toString());
}

View File

@ -52,8 +52,7 @@ public class FreeColMenuTest implements ActionListener, ItemListener {
private JMenuBar createMenuBar() {
JMenuBar menuBar = null;
FreeColClient client = new FreeColClient(null, null);
client.startClient(null, null, true, true, null, null);
FreeColClient client = FreeColClient.startTestClient(null);
//Create the menu bar.
menuBar = new InGameMenuBar(client, null);
@ -135,8 +134,7 @@ public class FreeColMenuTest implements ActionListener, ItemListener {
private static void createAndShowGUI() {
//Create and set up the window.
//JFrame frame = new JFrame("MenuDemo");
FreeColClient client = new FreeColClient(null, null);
client.startClient(null, null, true, true, null, null);
FreeColClient client = FreeColClient.startTestClient(null);
FreeColFrame frame = new FreeColFrame(client,
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(),
null, null, true, null);

View File

@ -173,7 +173,7 @@ public class ResourceManager {
* Create and start a new background preload thread.
*/
private static void startBackgroundPreloading() {
if (Utils.isHeadless()) return; // Do not preload in headless mode
if (FreeCol.getHeadless()) return; // Do not preload in headless mode
preloadThread = new Thread(FreeCol.CLIENT_THREAD + "-Resource loader") {
@Override

View File

@ -53,8 +53,7 @@ public class ClientTestHelper {
logger.info("Debug value: " + FreeColDebugger.isInDebugMode());
client = new FreeColClient(null, null);
client.startClient(null, null, false, false, null, specification);
client = FreeColClient.startTestClient(specification);
assertNotNull(client);
ConnectController connectController = client.getConnectController();