Class FreeCol


  • public final class FreeCol
    extends java.lang.Object
    This class is responsible for handling the command-line arguments and starting either the stand-alone server or the client-GUI.
    See Also:
    FreeColClient, FreeColServer
    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • FREECOL_VERSION

        private static final java.lang.String FREECOL_VERSION
        The FreeCol release version number.
        See Also:
        Constant Field Values
      • FREECOL_PROTOCOL_VERSION

        private static final java.lang.String FREECOL_PROTOCOL_VERSION
        The FreeCol protocol version number.
        See Also:
        Constant Field Values
      • DIFFICULTIES

        private static final java.lang.String[] DIFFICULTIES
        The difficulty levels.
      • FREECOL_SAVE_EXTENSION

        public static final java.lang.String FREECOL_SAVE_EXTENSION
        The extension for FreeCol saved games.
        See Also:
        Constant Field Values
      • FREECOL_MAP_EXTENSION

        public static final java.lang.String FREECOL_MAP_EXTENSION
        The extension for FreeCol maps.
        See Also:
        Constant Field Values
      • JAVA_VERSION

        private static final java.lang.String JAVA_VERSION
        The Java version.
      • MEMORY_MAX

        private static final long MEMORY_MAX
        The maximum available memory.
      • METASERVER_THREAD

        public static final java.lang.String METASERVER_THREAD
        See Also:
        Constant Field Values
      • freeColRevision

        private static java.lang.String freeColRevision
        Specific revision number (currently the git tag of trunk at release)
      • locale

        private static java.util.Locale locale
        The locale, either default or command-line specified.
      • DIFFICULTY_DEFAULT

        private static final java.lang.String DIFFICULTY_DEFAULT
        See Also:
        Constant Field Values
      • LOGLEVEL_DEFAULT

        private static final java.util.logging.Level LOGLEVEL_DEFAULT
      • JAVA_VERSION_MIN

        private static final java.lang.String JAVA_VERSION_MIN
        See Also:
        Constant Field Values
      • META_SERVER_ADDRESS

        private static final java.lang.String META_SERVER_ADDRESS
        See Also:
        Constant Field Values
      • WINDOWSIZE_FALLBACK

        private static final java.awt.Dimension WINDOWSIZE_FALLBACK
      • checkIntegrity

        private static boolean checkIntegrity
      • consoleLogging

        private static boolean consoleLogging
      • debugStart

        private static boolean debugStart
      • fastStart

        private static boolean fastStart
      • headless

        private static boolean headless
      • introVideo

        private static boolean introVideo
      • javaCheck

        private static boolean javaCheck
      • memoryCheck

        private static boolean memoryCheck
      • publicServer

        private static boolean publicServer
      • sound

        private static boolean sound
      • standAloneServer

        private static boolean standAloneServer
      • difficulty

        private static java.lang.String difficulty
        The difficulty level id.
      • europeanCount

        private static int europeanCount
        The number of European nations to enable by default.
      • fontName

        private static java.lang.String fontName
        A font override.
      • metaServerAddress

        private static java.lang.String metaServerAddress
        Meta-server location.
      • metaServerPort

        private static int metaServerPort
      • name

        private static java.lang.String name
        The client player name.
      • serverAddress

        private static java.net.InetAddress serverAddress
        How to name and configure the server.
      • serverPort

        private static int serverPort
      • serverName

        private static java.lang.String serverName
      • splashStream

        private static java.io.InputStream splashStream
        A stream to get the splash image from.
      • tc

        private static java.lang.String tc
        The TotalConversion / ruleset in play, defaults to "freecol".
      • timeout

        private static long timeout
        The time out (seconds) for otherwise blocking commands.
      • windowSize

        private static java.awt.Dimension windowSize
        The size of window to create, defaults to impossible dimensions to require windowed mode with best determined screen size.
      • specialOptions

        private static java.util.Map<java.lang.String,​java.lang.String> specialOptions
        The special client options that must be processed early.
      • argDir

        private static java.lang.String argDir
        Definitions for all the options.
      • argFile

        private static java.lang.String argFile
      • optionsTable

        private static java.lang.String[][] optionsTable
    • Constructor Detail

      • FreeCol

        private FreeCol()
    • Method Detail

      • main

        public static void main​(java.lang.String[] args)
        The entrypoint.
        Parameters:
        args - The command-line arguments.
      • processSpecialOptions

        private static void processSpecialOptions()
        Handle the special options that need to be processed early.
      • getJarURLConnection

        private static java.net.JarURLConnection getJarURLConnection​(java.lang.Class c)
                                                              throws java.io.IOException
        Get the JarURLConnection from a class.
        Parameters:
        c - The Class to get the connection for.
        Returns:
        The JarURLConnection.
        Throws:
        java.io.IOException - if the connection fails to open.
      • readVersion

        private static java.lang.String readVersion​(java.net.JarURLConnection juc)
                                             throws java.io.IOException
        Extract the package version from the class.
        Parameters:
        juc - The JarURLConnection to extract from.
        Returns:
        A value of the package version attribute.
        Throws:
        java.io.IOException - if the manifest is not available.
      • getDefaultSplashStream

        private static java.io.InputStream getDefaultSplashStream​(java.net.JarURLConnection juc)
                                                           throws java.io.IOException
        Get a stream for the default splash file. Note: Not bothering to check for nulls as this is called in try block that ignores all exceptions.
        Parameters:
        juc - The JarURLConnection to extract from.
        Returns:
        A suitable InputStream, or null on error.
        Throws:
        java.io.IOException - if the connection fails to open.
      • quit

        public static void quit​(int status)
        Quit FreeCol. Route all exits through here.
        Parameters:
        status - Exit status.
      • fatal

        public static void fatal​(java.util.logging.Logger logger,
                                 java.lang.String err)
        Quit and exit with an error.
        Parameters:
        logger - An optional Logger to log to.
        err - The error message.
      • fatal

        private static void fatal​(StringTemplate template)
        Exit printing fatal error message.
        Parameters:
        template - A StringTemplate to print.
      • fatal

        private static void fatal​(java.lang.String err)
        Exit printing fatal error message.
        Parameters:
        err - The error message to print.
      • gripe

        private static void gripe​(StringTemplate template)
        Just gripe to System.err.
        Parameters:
        template - A StringTemplate to print.
      • gripe

        private static void gripe​(java.lang.String key)
        Just gripe to System.err.
        Parameters:
        key - A message key.
      • trace

        public static void trace​(java.util.logging.Logger logger,
                                 java.lang.String warn)
        Log a warning with a stack trace.
        Parameters:
        logger - The Logger to log to.
        warn - The warning message.
      • findArg

        private static java.lang.String findArg​(java.lang.String option,
                                                java.lang.String[] args)
        Find an option before the real option handling can get started. Takes care to use the *last* instance.
        Parameters:
        option - The option to find.
        args - The command-line arguments.
        Returns:
        The option's parameter.
      • handleArgs

        private static void handleArgs​(java.lang.String[] args)
        Processes the command-line arguments and takes appropriate actions for each of them.
        Parameters:
        args - The command-line arguments.
      • printUsage

        private static void printUsage​(org.apache.commons.cli.Options options,
                                       int status)
        Prints the usage message and exits.
        Parameters:
        options - The command line Options.
        status - The status to exit with.
      • loadSpecification

        public static Specification loadSpecification​(FreeColTcFile tcf,
                                                      NationOptions.Advantages advantages,
                                                      java.lang.String difficulty)
        Get the specification from a given TC file.
        Parameters:
        tcf - The FreeColTcFile to load.
        advantages - An optional Advantages setting.
        difficulty - An optional difficulty level.
        Returns:
        A Specification.
      • getTCSpecification

        private static Specification getTCSpecification()
        Get the specification from the specified TC.
        Returns:
        A Specification, quits on error.
      • getHeadless

        public static boolean getHeadless()
        Are we in headless mode, either externally true or explicitly from the command line.
        Returns:
        True if in headless mode.
      • setHeadless

        public static void setHeadless​(boolean newHeadless)
        Set the headless state. Used by the test suite.
        Parameters:
        newHeadless - The new headless state.
      • getAdvantages

        public static NationOptions.Advantages getAdvantages()
        Gets the default advantages type.
        Returns:
        Usually Advantages.SELECTABLE, but can be overridden at the command line.
      • selectAdvantages

        private static NationOptions.Advantages selectAdvantages​(java.lang.String as)
        Sets the advantages type. Called from NewPanel when a selection is made.
        Parameters:
        as - The name of the new advantages type.
        Returns:
        The type of advantages set, or null if none.
      • setAdvantages

        public static void setAdvantages​(NationOptions.Advantages advantages)
        Sets the advantages type.
        Parameters:
        advantages - The new Advantages type.
      • getValidAdvantages

        private static java.lang.String getValidAdvantages()
        Gets a comma separated list of localized advantage type names.
        Returns:
        A list of advantage types.
      • getAdvantagesDescription

        private static java.lang.String getAdvantagesDescription()
        Get a description for the advantages argument.
        Returns:
        A suitable description.
      • getDebugDescription

        private static java.lang.String getDebugDescription()
        Get a description for the debug argument.
        Returns:
        A suitable description.
      • getDifficulty

        public static java.lang.String getDifficulty()
        Gets the difficulty level.
        Returns:
        The name of a difficulty level.
      • selectDifficulty

        private static java.lang.String selectDifficulty​(java.lang.String arg)
        Selects a difficulty level.
        Parameters:
        arg - The supplied difficulty argument.
        Returns:
        The name of the selected difficulty, or null if none.
      • setDifficulty

        public static void setDifficulty​(OptionGroup difficulty)
        Sets the difficulty level.
        Parameters:
        difficulty - The actual OptionGroup containing the difficulty level.
      • setDifficulty

        public static void setDifficulty​(java.lang.String difficulty)
        Sets the difficulty level.
        Parameters:
        difficulty - The new difficulty.
      • getValidDifficulties

        public static java.lang.String getValidDifficulties()
        Gets the names of the valid difficulty levels.
        Returns:
        The valid difficulty levels, comma separated.
      • getEuropeanCount

        public static int getEuropeanCount()
        Get the number of European nations to enable by default.
        Returns:
        The default European nation count.
      • setEuropeanCount

        public static void setEuropeanCount​(int n)
        Sets the number of enabled European nations.
        Parameters:
        n - The number of nations to enable.
      • getValidGUIScales

        public static java.lang.String getValidGUIScales()
        Gets the valid scale factors for the GUI.
        Returns:
        A string containing these.
      • selectEuropeanCount

        private static int selectEuropeanCount​(java.lang.String arg)
        Selects a European nation count.
        Parameters:
        arg - The supplied count argument.
        Returns:
        A valid nation number, or negative on error.
      • getMetaServerAddress

        public static java.lang.String getMetaServerAddress()
        Get the meta-server address.
        Returns:
        The current meta-server address.
      • getMetaServerPort

        public static int getMetaServerPort()
        Get the meta-server port.
        Returns:
        The current meta-server port.
      • setMetaServer

        private static boolean setMetaServer​(java.lang.String arg)
        Set the meta-server location.
        Parameters:
        arg - The new meta-server location in HOST:PORT format.
        Returns:
        True if the location was set.
      • getName

        public static java.lang.String getName()
        Gets the user name.
        Returns:
        The user name, defaults to the user.name property, then to the "main.defaultPlayerName" message value.
      • setName

        public static void setName​(java.lang.String name)
        Sets the user name.
        Parameters:
        name - The new user name.
      • getLocale

        public static java.util.Locale getLocale()
        Get the selected locale.
        Returns:
        The Locale currently in use.
      • setLocale

        public static boolean setLocale​(java.lang.String localeArg)
        Set the locale. Public for the test suite.
        Parameters:
        localeArg - The locale specification, null implies the default locale.
        Returns:
        True if the Locale changed.
      • getRevision

        public static java.lang.String getRevision()
        Gets the current revision of game.
        Returns:
        The current version and SVN Revision of the game.
      • getServerHost

        public static java.lang.String getServerHost()
        Get the default server host name.
        Returns:
        The host name.
      • getServerPort

        public static int getServerPort()
        Gets the server network port.
        Returns:
        The port number the server will be listening on.
      • getServerName

        public static java.net.InetAddress getServerName()
        Gets the server address.
        Returns:
        The InetAddress the server will be listening on.
      • setServerPort

        private static boolean setServerPort​(java.lang.String arg)
        Sets the server port.
        Parameters:
        arg - The server port number.
        Returns:
        True if the port was set.
      • setServerAddress

        private static boolean setServerAddress​(java.lang.String arg)
        Sets the server address.
        Parameters:
        arg - The server address.
        Returns:
        True if the address was set.
      • getTC

        public static java.lang.String getTC()
        Gets the current Total-Conversion.
        Returns:
        Usually TC_DEFAULT, but can be overridden at the command line.
      • setTC

        public static void setTC​(java.lang.String tc)
        Sets the Total-Conversion. Called from NewPanel when a selection is made.
        Parameters:
        tc - The name of the new total conversion.
      • getTCFile

        public static FreeColTcFile getTCFile()
        Gets the FreeColTcFile for the current TC.
        Returns:
        The FreeColTcFile.
      • getTimeout

        public static long getTimeout​(boolean singlePlayer)
        Gets the timeout. Use the command line specified one if any, otherwise default to `infinite' in single player and the TIMEOUT_DEFAULT for multiplayer.
        Parameters:
        singlePlayer - True if this is a single player game.
        Returns:
        A suitable timeout value.
      • setTimeout

        public static void setTimeout​(java.lang.String timeout)
        Sets the timeout.
        Parameters:
        timeout - A string containing the new timeout.
      • getVersion

        public static java.lang.String getVersion()
        Gets the current version of game.
        Returns:
        The current version of the game using the format "x.y.z", where "x" is major, "y" is minor and "z" is revision.
      • getFreeColProtocolVersion

        public static java.lang.String getFreeColProtocolVersion()
        Gets the current version of the FreeCol protocol.
        Returns:
        The version of the FreeCol protocol.
      • setWindowSize

        private static void setWindowSize​(java.lang.String arg)
        Sets the window size. Does not fail because any empty or invalid value is interpreted as `windowed but use as much screen as possible'.
        Parameters:
        arg - The window size specification.
      • badFile

        public static StringTemplate badFile​(java.lang.String messageId,
                                             java.io.File file)
        Generate a failure message depending on a file parameter.
        Parameters:
        messageId - The failure message identifier.
        file - The File that caused the failure.
        Returns:
        A StringTemplate with the error message.
      • errorFromException

        public static StringTemplate errorFromException​(java.lang.Exception ex,
                                                        java.lang.String fallbackKey)
        Build an error template from an exception.
        Parameters:
        ex - The Exception to make an error from.
        fallbackKey - A message key to use to make a fallback message if the exception is unsuitable.
        Returns:
        An error StringTemplate.
      • errorFromException

        public static StringTemplate errorFromException​(java.lang.Exception ex,
                                                        StringTemplate fallback)
        Build an error template from an exception.
        Parameters:
        ex - The Exception to make an error from.
        fallback - A StringTemplate to use as a fall back if the exception is unsuitable.
        Returns:
        An error StringTemplate.
      • getConfiguration

        public static java.lang.StringBuilder getConfiguration()
        We get a lot of lame bug reports with insufficient configuration information. Get a buffer containing as much information as we can to embed in the log file and saved games.
        Returns:
        A StringBuilder full of configuration information.
      • startClient

        private static void startClient()
        Start a client.
      • createSplashScreen

        private static SplashScreen createSplashScreen()
      • startTestClient

        public static FreeColClient startTestClient​(Specification spec)
        Wrapper for the test suite to start a test client.
        Parameters:
        spec - The Specification to use in the new client.
        Returns:
        The new FreeColClient.
      • checkServerIntegrity

        private static void checkServerIntegrity​(FreeColServer freeColServer)
        Check the integrity of a FreeCol server.
        Parameters:
        freeColServer - The server to check.
      • startServer

        private static void startServer()
        Start the server.