Class ResourceManager


  • public class ResourceManager
    extends java.lang.Object
    Class for getting resources (images, audio etc).
    • Field Detail

      • logger

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

        private static java.lang.Thread preloadThread
        The thread that handles preloading of resources.
      • preloadDone

        private static volatile boolean preloadDone
        Flag to inform the preload thead that all mappings are queued.
      • mergedContainer

        private static ResourceMapping mergedContainer
        All the mappings are merged in order into this single ResourceMapping.
      • modResourceMappings

        private static java.util.List<ResourceMapping> modResourceMappings
      • savegameResourceMapping

        private static ResourceMapping savegameResourceMapping
    • Constructor Detail

      • ResourceManager

        public ResourceManager()
    • Method Detail

      • setBaseData

        public static void setBaseData​(FreeColDataFile baseDataFile)
      • setMods

        public static <T extends FreeColDataFile> void setMods​(java.util.List<T> mods)
      • prepare

        public static void prepare()
      • reload

        public static void reload()
        Clear all caches and
      • waitForPreloadingToStop

        private static void waitForPreloadingToStop()
      • startPreloading

        public static void startPreloading​(java.lang.Runnable afterPreloadHasCompleted)
        Create and start a new background preload thread. Synchronization protects preloadThread. The thread is the only place mergedContainer is written.
        Parameters:
        afterPreloadHasCompleted - A Runnable to run when it says.
      • finishPreloading

        public static void finishPreloading()
        Signal to the preload thread that no further mappings need to be loaded.
      • getAudioResource

        public static AudioResource getAudioResource​(java.lang.String key,
                                                     boolean warn)
        Get an audio resource.
        Parameters:
        key - The resource to get.
        warn - Log a warning if the resource is not found.
        Returns:
        The resource if there is one with the given resource key and type, or else null.
      • getColorResource

        public static ColorResource getColorResource​(java.lang.String key,
                                                     boolean warn)
        Get a color resource.
        Parameters:
        key - The resource to get.
        warn - Log a warning if the resource is not found.
        Returns:
        The resource if there is one with the given resource key and type, or else null.
      • getFAFileResource

        public static FAFileResource getFAFileResource​(java.lang.String key,
                                                       boolean warn)
        Get a FAFile resource.
        Parameters:
        key - The resource to get.
        warn - Log a warning if the resource is not found.
        Returns:
        The resource if there is one with the given resource key and type, or else null.
      • getFontResource

        public static FontResource getFontResource​(java.lang.String key,
                                                   boolean warn)
        Get a font resource.
        Parameters:
        key - The resource to get.
        warn - Log a warning if the resource is not found.
        Returns:
        The resource if there is one with the given resource key and type, or else null.
      • getImageResource

        public static ImageResource getImageResource​(java.lang.String key,
                                                     boolean warn)
        Get an image resource.
        Parameters:
        key - The resource to get.
        warn - Log a warning if the resource is not found.
        Returns:
        The resource if there is one with the given resource key and type, or else null.
      • getStringResource

        public static StringResource getStringResource​(java.lang.String key,
                                                       boolean warn)
        Get a string resource.
        Parameters:
        key - The resource to get.
        warn - Log a warning if the resource is not found.
        Returns:
        The resource if there is one with the given resource key and type, or else null.
      • getSZAResource

        public static SZAResource getSZAResource​(java.lang.String key,
                                                 boolean warn)
        Get a SZA resource.
        Parameters:
        key - The resource to get.
        warn - Log a warning if the resource is not found.
        Returns:
        The resource if there is one with the given resource key and type, or else null.
      • getVideoResource

        public static VideoResource getVideoResource​(java.lang.String key,
                                                     boolean warn)
        Get a video resource.
        Parameters:
        key - The resource to get.
        warn - Log a warning if the resource is not found.
        Returns:
        The resource if there is one with the given resource key and type, or else null.
      • getAudio

        public static java.io.File getAudio​(java.lang.String key)
        Gets an audio resource with the given name. This can return null as there as not all necessary sounds have been added to the game. FIXME: Change calling code to check using hasResource, then replace null return with calling FreeColClient.fatal on error.
        Parameters:
        key - The name of the resource to query.
        Returns:
        A File containing the audio data.
      • getColor

        public static java.awt.Color getColor​(java.lang.String key,
                                              java.awt.Color replacement)
        Gets a color resource with the given name.
        Parameters:
        key - The name of the resource to query.
        replacement - A fallback color.
        Returns:
        The Color found, or if not found, the replacement color, or finally the generic replacement color.
      • getFAFile

        public static FAFile getFAFile​(java.lang.String key)
        Gets a FAFile resource with the given name. This can return null as there is only one FAFile in FreeCol. FIXME: Consider calling FreeColClient.fatal on error.
        Parameters:
        key - The name of the resource to query.
        Returns:
        The FAFile found in a FAFileResource.
      • getFont

        public static java.awt.Font getFont​(java.lang.String key)
        Gets the font with the given name.
        Parameters:
        key - The name of the resource to query.
        Returns:
        The Font found in a FontResource, or the default Java font if not found.
      • getImage

        public static java.awt.image.BufferedImage getImage​(java.lang.String key)
        Get the image specified by the given key.
        Parameters:
        key - The name of the resource to return.
        Returns:
        The image identified by resource.
      • getImage

        public static java.awt.image.BufferedImage getImage​(java.lang.String key,
                                                            java.awt.Dimension size,
                                                            boolean grayscale)
        Get the image specified by the given name, size and grayscale.
        Parameters:
        key - The name of the resource to return.
        size - The size of the requested image. Rescaling will be performed if necessary.
        grayscale - If true return a grayscale image.
        Returns:
        The image identified by resource.
      • getImageKeys

        public static java.util.List<java.lang.String> getImageKeys​(java.lang.String prefix)
        Get a list of all image keys starting with the given prefix.
        Parameters:
        prefix - The prefix.
        Returns:
        A list of all image resource keys starting with the prefix.
      • getString

        public static java.lang.String getString​(java.lang.String key)
        Gets a string resource with the given name. Trying to get a nonexisting string is an error, but returns a replacement string to prevent crashes.
        Parameters:
        key - The name of the resource to query.
        Returns:
        The string value.
      • getSZA

        public static SimpleZippedAnimation getSZA​(java.lang.String key)
        Returns the animation specified by the given name. As the artwork is still incomplete and animations exist only for some military units, null can still be returned in many cases. FIXME: Check using hasResource before calling this, then replace null return with calling FreeColClient.fatal on error.
        Parameters:
        key - The name of the resource to return.
        Returns:
        The animation identified by resource or null if there is no animation identified by that name.
      • getSZA

        public static SimpleZippedAnimation getSZA​(java.lang.String key,
                                                   float scale)
        Returns the animation specified by the given name.
        Parameters:
        key - The name of the resource to return.
        scale - The size of the requested animation (with 1 being normal size, 2 twice the size, 0.5 half the size etc). Rescaling will be performed unless using 1.
        Returns:
        The animation identified by resource or null if there is no animation identified by that name.
      • getVideo

        public static Video getVideo​(java.lang.String key)
        Gets the Video represented by the given resource. This can return null as there is only one video in FreeCol. FIXME: Consider calling FreeColClient.fatal on error.
        Parameters:
        key - The name of the resource to return.
        Returns:
        The Video in it's original size.
      • summarizeImageResources

        public static void summarizeImageResources​(java.lang.StringBuilder sb)
        Summarize the image resources.
        Parameters:
        sb - A StringBuilder to summarize to.