Class FreeColDirectories


  • public class FreeColDirectories
    extends java.lang.Object
    Simple container for the freecol file and directory structure model.
    • Field Detail

      • fileModificationComparator

        private static final java.util.Comparator<java.io.File> fileModificationComparator
      • fileNameComparator

        private static final java.util.Comparator<java.io.File> fileNameComparator
      • AUTOSAVE_DIRECTORY

        private static final java.lang.String AUTOSAVE_DIRECTORY
        See Also:
        Constant Field Values
      • CLASSIC_DIRECTORY

        private static final java.lang.String CLASSIC_DIRECTORY
        See Also:
        Constant Field Values
      • CONFIG_DIRS

        private static final java.lang.String[] CONFIG_DIRS
      • FREECOL_DIRECTORY

        private static final java.lang.String FREECOL_DIRECTORY
        See Also:
        Constant Field Values
      • LOG_COMMS_FILE_NAME

        private static final java.lang.String LOG_COMMS_FILE_NAME
        See Also:
        Constant Field Values
      • MESSAGE_FILE_PREFIX

        private static final java.lang.String MESSAGE_FILE_PREFIX
        See Also:
        Constant Field Values
      • MESSAGE_FILE_SUFFIX

        private static final java.lang.String MESSAGE_FILE_SUFFIX
        See Also:
        Constant Field Values
      • MOD_MESSAGE_FILE_PREFIX

        private static final java.lang.String MOD_MESSAGE_FILE_PREFIX
        See Also:
        Constant Field Values
      • PLURALS_FILE_NAME

        private static final java.lang.String PLURALS_FILE_NAME
        See Also:
        Constant Field Values
      • RESOURCE_FILE_PREFIX

        private static final java.lang.String RESOURCE_FILE_PREFIX
        See Also:
        Constant Field Values
      • RESOURCE_FILE_SUFFIX

        private static final java.lang.String RESOURCE_FILE_SUFFIX
        See Also:
        Constant Field Values
      • SPECIFICATION_FILE_NAME

        private static final java.lang.String SPECIFICATION_FILE_NAME
        See Also:
        Constant Field Values
      • SEPARATOR

        private static final java.lang.String SEPARATOR
      • USER_MAPS_DIRECTORY

        private static final java.lang.String USER_MAPS_DIRECTORY
        See Also:
        Constant Field Values
      • XDG_CONFIG_HOME_ENV

        private static final java.lang.String XDG_CONFIG_HOME_ENV
        See Also:
        Constant Field Values
      • XDG_CONFIG_HOME_DEFAULT

        private static final java.lang.String XDG_CONFIG_HOME_DEFAULT
        See Also:
        Constant Field Values
      • XDG_DATA_HOME_ENV

        private static final java.lang.String XDG_DATA_HOME_ENV
        See Also:
        Constant Field Values
      • XDG_DATA_HOME_DEFAULT

        private static final java.lang.String XDG_DATA_HOME_DEFAULT
        See Also:
        Constant Field Values
      • XDG_CACHE_HOME_ENV

        private static final java.lang.String XDG_CACHE_HOME_ENV
        See Also:
        Constant Field Values
      • XDG_CACHE_HOME_DEFAULT

        private static final java.lang.String XDG_CACHE_HOME_DEFAULT
        See Also:
        Constant Field Values
      • BASE_CLIENT_OPTIONS_FILE_NAME

        public static final java.lang.String BASE_CLIENT_OPTIONS_FILE_NAME
        See Also:
        Constant Field Values
      • CLIENT_OPTIONS_FILE_NAME

        public static final java.lang.String CLIENT_OPTIONS_FILE_NAME
        See Also:
        Constant Field Values
      • CUSTOM_DIFFICULTY_FILE_NAME

        public static final java.lang.String CUSTOM_DIFFICULTY_FILE_NAME
        See Also:
        Constant Field Values
      • GAME_OPTIONS_FILE_NAME

        public static final java.lang.String GAME_OPTIONS_FILE_NAME
        See Also:
        Constant Field Values
      • MAP_EDITOR_FILE_NAME

        public static final java.lang.String MAP_EDITOR_FILE_NAME
        See Also:
        Constant Field Values
      • MAP_GENERATOR_OPTIONS_FILE_NAME

        public static final java.lang.String MAP_GENERATOR_OPTIONS_FILE_NAME
        See Also:
        Constant Field Values
      • MOD_DESCRIPTOR_FILE_NAME

        public static final java.lang.String MOD_DESCRIPTOR_FILE_NAME
        See Also:
        Constant Field Values
      • modFileFilter

        private static final java.util.function.Predicate<java.io.File> modFileFilter
        Predicate to filter suitable candidates to be made into mods.
      • saveGameFilter

        public static final java.util.function.Predicate<java.io.File> saveGameFilter
        Predicate to select readable files that look like saved games. Public for SaveGameValidator.
      • mapFilter

        private static final java.util.function.Predicate<java.io.File> mapFilter
        Predicate to select readable files that look like maps.
      • tcFileFilter

        private static final java.util.function.Predicate<java.io.File> tcFileFilter
        Predicate to filter suitable candidates to be made into TCs.
      • mode0700

        private static final java.util.Set<java.nio.file.attribute.PosixFilePermission> mode0700
        Posix file mode 0700.
      • autosaveDirectory

        private static java.io.File autosaveDirectory
        The directory containing automatically created save games. At program start, the path of this directory is based on the path where to store regular save games. If the saved game is changed by the user during the game, then the value of autosaveDirectory will not change.
      • clientOptionsFile

        private static java.io.File clientOptionsFile
        A file containing the client options. Can be overridden at the command line.
      • dataDirectory

        private static java.io.File dataDirectory
        The directory where the standard freecol data is installed. Can be overridden at the command line. FIXME: defaults lamely to ./data. Do something better in the installer.
      • logFilePath

        private static java.lang.String logFilePath
        The path to the log file. Can be overridden at the command line.
      • saveDirectory

        private static java.io.File saveDirectory
        Where games are saved. Can be overridden in game or from the command line by specifying the save game file.
      • savegameFile

        private static java.io.File savegameFile
        The current save game file. Can be modified in game.
      • userCacheDirectory

        private static java.io.File userCacheDirectory
        The directory where freecol saves transient information.
      • userConfigDirectory

        private static java.io.File userConfigDirectory
        The directory where freecol saves user configuration. This will be set by default but can be overridden at the command line.
      • userDataDirectory

        private static java.io.File userDataDirectory
        The directory where freecol saves user data. This will be set by default but can be overridden at the command line.
      • userModsDirectory

        private static java.io.File userModsDirectory
        An optional directory containing user mods.
      • commsWriter

        private static java.util.concurrent.atomic.AtomicReference<java.io.Writer> commsWriter
        The comms writer.
    • Constructor Detail

      • FreeColDirectories

        public FreeColDirectories()
    • Method Detail

      • getUserDefaultDirectory

        private static java.io.File getUserDefaultDirectory()
        Get the user home directory.
        Returns:
        The user home directory.
      • checkDir

        public static java.lang.String checkDir​(java.io.File dir)
        Check a directory for read and write access.
        Parameters:
        dir - The File that must be a usable directory.
        Returns:
        Null on success, an error message key on failure.
      • isGoodDirectory

        private static boolean isGoodDirectory​(java.io.File f)
        Is the specified file a writable directory?
        Parameters:
        f - The File to check.
        Returns:
        True if the file is a writable directory.
      • requireDirectory

        private static java.io.File requireDirectory​(java.io.File dir)
        Create the given directory if it does not exist, otherwise expect it to be writable.
        Parameters:
        dir - The File specifying the required directory.
        Returns:
        The required directory, or null on failure.
      • badConfig

        private static StringTemplate badConfig​(java.io.File f)
      • badCache

        private static StringTemplate badCache​(java.io.File f)
      • getXDGDirs

        private static StringTemplate getXDGDirs​(java.io.File[] dirs)
        Get directories for XDG compliant systems. XDG say: If, when attempting to write a file, the destination directory is non-existant an attempt should be made to create it with permission 0700.
        Parameters:
        dirs - An array of File to be filled in with the XDG directory if it is present or created.
        Returns:
        Null on success, an error message on error.
      • getMacOSXDirs

        private static StringTemplate getMacOSXDirs​(java.io.File[] dirs)
        Get FreeCol directories for MacOSX. No separate cache directory here.
        Parameters:
        dirs - An array of File to be filled in with the MacOSX freecol directories.
        Returns:
        Null on success, an error message on failure.
      • getWindowsDirs

        private static StringTemplate getWindowsDirs​(java.io.File[] dirs)
        Get FreeCol directories for Windows. Simple case, everything is in the one directory. Result is: - Negative on failure. - Zero if a migration is needed. - Positive if no migration is needed.
        Parameters:
        dirs - An array of File to be filled in with the Windows freecol directories.
        Returns:
        Null on success, an error message on failure.
      • insistDirectory

        private static boolean insistDirectory​(java.io.File file)
        Insist that a directory either already exists, or is created.
        Parameters:
        file - A File specifying where to make the directory.
        Returns:
        True if the directory is now there.
      • deriveDirectory

        private static java.io.File deriveDirectory​(java.io.File root,
                                                    java.lang.String subdir)
        Safely derive a subdirectory of a root.
        Parameters:
        root - The root directory.
        subdir - The name of the subdirectory to find or create.
        Returns:
        The directory found, or null on error.
      • deriveAutosaveDirectory

        private static java.io.File deriveAutosaveDirectory()
        Derive the directory for the autosave files from the save directory.
        Returns:
        The new autosave directory, or null if not possible.
      • collectFiles

        private static java.util.List<java.io.File> collectFiles​(java.io.File dir,
                                                                 java.util.function.Predicate<java.io.File> pred)
        Collect files from a directory that match a predicate.
        Parameters:
        dir - The directory to load from.
        pred - A Predicate to match files with.
        Returns:
        A list of Files.
      • setDataDirectory

        public static java.lang.String setDataDirectory​(java.lang.String path)
        Sets the data directory. Insist that the base resources and i18n subdirectories are present.
        Parameters:
        path - The path to the new data directory, or null to apply the default.
        Returns:
        A (non-i18n) error message on failure, null on success.
      • setUserDirectories

        public static StringTemplate setUserDirectories()
        Set the user directories for the current user. - on XDG standard compliant Unixes: - config: ~/.config/freecol - data: ~/.local/share/freecol - logging: ~/.cache/freecol - on Mac: - config: ~/Library/Preferences/freecol - else: ~/Library/Application Support/freecol - on Windows: - everything in default directory/freecol Note: the freecol data directory is set independently and earlier in initialization than this routine. FIXME: Should the default location of the main user and data directories be determined by the installer?
        Returns:
        Null on success, otherwise an error message template.
      • sanitize

        private static java.lang.String sanitize​(java.lang.String fileName)
        Remove disallowed parts of a user supplied file name.
        Parameters:
        fileName - The input file name.
        Returns:
        A sanitized file name.
      • getAutosaveDirectory

        public static java.io.File getAutosaveDirectory()
        Gets the directory where the automatically saved games should be put.
        Returns:
        The autosave directory.
      • setAutosaveDirectory

        private static void setAutosaveDirectory​(java.io.File dir)
        Set the autosave directory.
        Parameters:
        dir - The new autosave directory.
      • getAutosaveFile

        public static java.io.File getAutosaveFile​(java.lang.String fileName)
        Get a specific autosave file.
        Parameters:
        fileName - The name of the file.
        Returns:
        The File found, or null on error.
      • getAutosaveFiles

        private static java.util.List<java.io.File> getAutosaveFiles​(java.lang.String prefix,
                                                                     java.util.function.Predicate<java.io.File> pred)
        Get the autosave files.
        Parameters:
        prefix - The autosave file prefix.
        pred - A Predicate to select files with.
        Returns:
        A list of of autosaved Files.
      • removeOutdatedAutosaves

        public static java.lang.String removeOutdatedAutosaves​(java.lang.String prefix,
                                                               java.util.List<java.lang.String> excludeSuffixes,
                                                               long validDays)
        Remove out of date autosaves. This only removed generic autosaves, not the last-turn autosave, which can be useful for continuing the game on the next play-session.
        Parameters:
        prefix - The autosave file prefix.
        excludeSuffixes - Only files not ending with any of these prefixes will be removed.
        validDays - Only files older than this amount of days will be removed.
        Returns:
        A suitable log message, or null if nothing was deleted.
      • removeAutosaves

        public static java.lang.String removeAutosaves​(java.lang.String prefix)
        Remove all autosave files.
        Parameters:
        prefix - The autosave file prefix.
        Returns:
        A suitable log message, or null if nothing was deleted.
      • getBaseDirectory

        public static java.io.File getBaseDirectory()
        Gets the base resources directory.
        Returns:
        The base resources directory.
      • getBaseClientOptionsFile

        public static java.io.File getBaseClientOptionsFile()
        Gets the base client options file.
        Returns:
        The base client options file.
      • getClientOptionsFile

        public static java.io.File getClientOptionsFile()
        Gets the file containing the client options.
        Returns:
        The client options file, if any.
      • setClientOptionsFile

        public static boolean setClientOptionsFile​(java.lang.String path)
        Sets the client options file.
        Parameters:
        path - The new client options file.
        Returns:
        True if the file was set successfully.
      • getCompatibilityFile

        public static java.io.File getCompatibilityFile​(java.lang.String fileName)
        Get a compatibility file. Not sanitizing the file name as it is fixed in all current uses.
        Parameters:
        fileName - The name of the compatibility file.
        Returns:
        The File found.
      • getDataDirectory

        public static java.io.File getDataDirectory()
        Gets the data directory.
        Returns:
        The directory where the data files are located.
      • getDebugRunSaveFile

        public static java.io.File getDebugRunSaveFile()
        Get the debug-run save file.
        Returns:
        The save File, if any.
      • getHighScoreFile

        public static java.io.File getHighScoreFile()
        Gets the high score file.
        Returns:
        The high score file, if it exists.
      • getI18nDirectory

        public static java.io.File getI18nDirectory()
        Gets the directory containing language property files.
        Returns:
        The FreeCol i18n directory.
      • getLogFileContents

        public static java.lang.String getLogFileContents()
        Get the contents of the log file.
        Returns:
        A string of the log file contents, or null on error.
      • getI18nMessageFileList

        public static java.util.List<java.io.File> getI18nMessageFileList​(java.util.Locale locale)
        Get a list of candidate message file names for a given locale.
        Parameters:
        locale - The Locale to generate file names for.
        Returns:
        A list of message Files.
      • getI18nPluralsFile

        public static java.io.File getI18nPluralsFile()
        Get the i18n plurals file.
        Returns:
        The plurals File.
      • getLanguageIdList

        public static java.util.List<java.lang.String> getLanguageIdList()
        Get a list of all the supported language identifiers.
        Returns:
        A list of language identifiers for which there is an i18n-message file.
      • getLanguageId

        public static java.lang.String getLanguageId​(java.io.File file)
        If this a messages file, work out which language identifier it belongs to.
        Parameters:
        file - The File to test.
        Returns:
        The language identifier found, or null on failure.
      • getLocaleFileNames

        public static java.util.List<java.lang.String> getLocaleFileNames​(java.lang.String prefix,
                                                                          java.lang.String suffix,
                                                                          java.util.Locale locale)
        Gets a list containing the names of all possible message files for a locale.
        Parameters:
        prefix - The file name prefix.
        suffix - The file name suffix.
        locale - The Locale to generate file names for.
        Returns:
        A list of candidate file names.
      • getLogFilePath

        public static java.lang.String getLogFilePath()
        Gets the log file path.
        Returns:
        The log file path.
      • setLogFilePath

        public static void setLogFilePath​(java.lang.String path)
        Sets the log file path.
        Parameters:
        path - The new log file path.
      • getLogWriter

        public static java.io.Writer getLogWriter()
                                           throws FreeColException
        Gets a new log writer.
        Returns:
        The log Writer.
        Throws:
        FreeColException - if there was a problem creating the writer.
      • getLogCommsWriter

        public static java.io.Writer getLogCommsWriter()
                                                throws FreeColException
        Get a writer for the comms log file.
        Returns:
        A suitable Writer.
        Throws:
        FreeColException - on error.
      • getMapsDirectory

        public static java.io.File getMapsDirectory()
        Gets the directory containing the predefined maps.
        Returns:
        The predefined maps.
      • getMapFileList

        public static java.util.List<java.io.File> getMapFileList()
        Get the map files.
        Returns:
        A list of map files which may be empty.
      • getMessageFileNameList

        public static java.util.List<java.lang.String> getMessageFileNameList​(java.util.Locale locale)
        Get the message file names for a given locale.
        Parameters:
        locale - The Locale to generate names for.
        Returns:
        A list of potential message file names.
      • getModMessageFileNames

        public static java.util.List<java.lang.String> getModMessageFileNames​(java.util.Locale locale)
        Get the mod message file names for a given locale.
        Parameters:
        locale - The Locale to generate names for.
        Returns:
        A list of potential mod message file names.
      • getModFileList

        public static java.util.List<java.io.File> getModFileList()
        Get a list of the standard and current user mod files.
        Returns:
        A list of mod Files.
      • getOptionsDirectory

        public static java.io.File getOptionsDirectory()
        Gets the directory where the user options are saved.
        Returns:
        The directory to save user options in.
      • getOptionsFile

        public static java.io.File getOptionsFile​(java.lang.String fileName)
        Get an options file from the options directory.
        Parameters:
        fileName - The name of the file within the options directory.
        Returns:
        The options file.
      • getResourceFileNames

        public static java.util.List<java.lang.String> getResourceFileNames()
        Get a list of candidate resource file names for a given locale.
        Returns:
        A list of resource file names.
      • getRulesClassicDirectory

        public static java.io.File getRulesClassicDirectory()
        Gets the directory containing the classic rules.
        Returns:
        The classic rules directory.
      • getRulesDirectory

        public static java.io.File getRulesDirectory()
        Gets the directory containing the various rulesets.
        Returns:
        The ruleset directory.
      • getSaveDirectory

        public static java.io.File getSaveDirectory()
        Gets the directory where the savegames should be put.
        Returns:
        The save directory.
      • getSavegameFile

        public static java.io.File getSavegameFile()
        Gets the save game file.
        Returns:
        The save game file.
      • getSavegameFiles

        public static java.util.stream.Stream<java.io.File> getSavegameFiles​(java.io.File directory)
        Gets the save game files in a given directory.
        Parameters:
        directory - The base directory, or the default locations if null.
        Returns:
        A stream of save game Files.
      • getSavegameFileList

        public static java.util.List<java.io.File> getSavegameFileList​(java.io.File directory)
        Gets the save game files in a given directory.
        Parameters:
        directory - The base directory, or the default locations if null.
        Returns:
        A list of save game Files.
      • setSavegameFile

        public static boolean setSavegameFile​(java.lang.String path)
        Sets the save game file.
        Parameters:
        path - The path to the new save game file.
        Returns:
        True if the setting succeeds.
      • getLastSaveGameFile

        public static java.io.File getLastSaveGameFile()
        Gets the most recently saved game file, or null. (This may be either from a recent arbitrary user operation or an autosave function.)
        Returns:
        The recent save game File, or null if not found.
      • getStandardModsDirectory

        public static java.io.File getStandardModsDirectory()
        Gets the standard mods directory.
        Returns:
        The directory where the standard mods are located.
      • getStartMapFile

        public static java.io.File getStartMapFile()
        Get the map file to start from, if any.
        Returns:
        The start map file, or null on error.
      • getTcFileList

        public static java.util.List<java.io.File> getTcFileList()
        Get all available rules files.
        Returns:
        A list of Files containing rulesets.
      • getUserCacheDirectory

        public static java.io.File getUserCacheDirectory()
        Gets the user cache directory, that is the directory under which the transient user files live.
        Returns:
        The user cache directory.
      • setUserCacheDirectory

        public static java.lang.String setUserCacheDirectory​(java.lang.String path)
        Sets the user cache directory, that is the directory under which the user-specific cache files live.
        Parameters:
        path - The path to the new user cache directory.
        Returns:
        Null on success, an error message key on failure.
      • getUserConfigDirectory

        public static java.io.File getUserConfigDirectory()
        Gets the user config directory, that is the directory under which the user-specific config files live.
        Returns:
        The user config directory.
      • setUserConfigDirectory

        public static java.lang.String setUserConfigDirectory​(java.lang.String path)
        Sets the user config directory, that is the directory under which the user-specific config files live.
        Parameters:
        path - The path to the new user config directory.
        Returns:
        Null on success, an error message key on failure.
      • getUserDataDirectory

        public static java.io.File getUserDataDirectory()
        Gets the user data directory, that is the directory under which the user-specific data lives.
        Returns:
        The user data directory.
      • setUserDataDirectory

        public static java.lang.String setUserDataDirectory​(java.lang.String path)
        Sets the main user data directory, creating it if necessary. If pre-existing, it must be a directory, readable and writable.
        Parameters:
        path - The path to the new main data user directory, or null to apply the default.
        Returns:
        Null on success, an error message key on failure.
      • getUserMapsDirectory

        public static java.io.File getUserMapsDirectory()
        Get the user maps directory.
        Returns:
        The directory to save user maps to, or null if none.
      • getUserModsDirectory

        public static java.io.File getUserModsDirectory()
        Gets the user mods directory.
        Returns:
        The directory where user mods are located, or null if none.