Class SourceBooks

java.lang.Object
megamek.common.SourceBooks

public class SourceBooks extends Object
This class manages sourcebook information, usually loaded from the data/sourcebooks folder. Sourcebooks are not automatically loaded into memory when instantiating this class but are loaded on-demand only.
  • Constructor Details

    • SourceBooks

      public SourceBooks(String directoryPath)
      Creates a sourcebooks manager for any sourcebooks in the given directory. This constructor is for testing/debugging.
      Parameters:
      directoryPath - The sourcebooks path to use
    • SourceBooks

      public SourceBooks()
      Creates a sourcebooks manager for sourcebooks in the standard sourcebooks folder. This constructor does not load anything and is a cheap operation; sourcebooks are only individually loaded on demand.
  • Method Details

    • getStandardSourceBooks

      public static SourceBooks getStandardSourceBooks()
    • availableSourcebooks

      public List<String> availableSourcebooks()
      Returns a list of available sourcebook filenames (with the ".yaml" extension removed). The returned values can directly be used to obtain a book. The returned list is empty if there are no source books in the folder or if there is some error (IOExceptions are caught in this method).
      
      
       SourceBooks sourceBooks = new SourceBooks();
       List<String> allBooks = sourceBooks.availableSourcebooks();
       String firstBook = allBooks.getFirst() // assuming there are sourcebooks
       Optional<SourceBook> book = sourceBooks.loadSourceBook(firstBook);
      
       
      Returns:
      A list of available sourcebooks
      See Also:
    • loadSourceBook

      public Optional<SourceBook> loadSourceBook(String fileName)
      Tries to read a sourcebook file of the given name. The filename can include the .yaml extension but does not have to.
      Parameters:
      fileName - The filename of the sourcebook to load
      Returns:
      The book, if it can be loaded.
    • saveSourceBook

      public void saveSourceBook(String fileName, SourceBook sourceBook) throws IOException
      Saves a sourcebook as a YAML file in the sourcebook directory.
      Parameters:
      fileName - The sourcebook filename or key. The .yaml extension is optional.
      sourceBook - The sourcebook data to save.
      Throws:
      IOException - if the sourcebook cannot be written.
    • clearCaches

      public static void clearCaches()
    • sourceBookFile

      public File sourceBookFile(String fileName)
    • sourceBookKey

      public String sourceBookKey(String fileName)
    • loadSourceBooks

      public List<SourceBook> loadSourceBooks(String sourceList)
      Loads any sourcebooks listed in a comma-separated source field.
      Parameters:
      sourceList - A comma-separated source list, such as "TR:3039,RG29"
      Returns:
      The sourcebooks that could be loaded, in source-list order
    • isNonCanonBySource

      public boolean isNonCanonBySource(String source, String published)
      Returns:
      true when no sourcebook entries are present or all listed sourcebooks are non-canon or cannot be loaded.
    • splitSourceList

      public static List<String> splitSourceList(String sourceList)
    • normalizeSourceList

      public static String normalizeSourceList(String sourceList)
    • formatSourceList

      public static String formatSourceList(Collection<String> sources)