From 9875efa12804da2a8f36faebd2b8f12d8d7e3785 Mon Sep 17 00:00:00 2001 From: applenick Date: Sat, 9 Jul 2022 11:00:52 -0700 Subject: [PATCH 1/5] Reintroduce include statements Signed-off-by: applenick --- core/src/main/java/tc/oc/pgm/PGMConfig.java | 11 +++ core/src/main/java/tc/oc/pgm/PGMPlugin.java | 7 +- core/src/main/java/tc/oc/pgm/api/Config.java | 8 ++ .../oc/pgm/api/map/includes/MapInclude.java | 22 +++++ .../api/map/includes/MapIncludeProcessor.java | 26 +++++ .../java/tc/oc/pgm/map/MapFactoryImpl.java | 15 ++- .../java/tc/oc/pgm/map/MapLibraryImpl.java | 8 +- .../oc/pgm/map/includes/MapIncludeImpl.java | 47 +++++++++ .../map/includes/MapIncludeProcessorImpl.java | 98 +++++++++++++++++++ core/src/main/resources/config.yml | 3 + 10 files changed, 241 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java create mode 100644 core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java create mode 100644 core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java create mode 100644 core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java diff --git a/core/src/main/java/tc/oc/pgm/PGMConfig.java b/core/src/main/java/tc/oc/pgm/PGMConfig.java index a412ccb24c..786330c781 100644 --- a/core/src/main/java/tc/oc/pgm/PGMConfig.java +++ b/core/src/main/java/tc/oc/pgm/PGMConfig.java @@ -56,6 +56,7 @@ public final class PGMConfig implements Config { // map.* private final List mapSourceFactories; private final String mapPoolFile; + private final String includesDirectory; // countdown.* private final Duration startTime; @@ -159,6 +160,11 @@ public final class PGMConfig implements Config { mapPoolFile == null || mapPoolFile.isEmpty() ? null : new File(dataFolder, mapPoolFile).getAbsolutePath(); + final String includesDirectory = config.getString("map.includes"); + this.includesDirectory = + includesDirectory == null || includesDirectory.isEmpty() + ? null + : new File(dataFolder, includesDirectory).getAbsolutePath(); this.startTime = parseDuration(config.getString("countdown.start", "30s")); this.huddleTime = parseDuration(config.getString("countdown.huddle", "0s")); @@ -466,6 +472,11 @@ public String getMapPoolFile() { return mapPoolFile; } + @Override + public String getIncludesDirectory() { + return includesDirectory; + } + @Override public Duration getStartTime() { return startTime; diff --git a/core/src/main/java/tc/oc/pgm/PGMPlugin.java b/core/src/main/java/tc/oc/pgm/PGMPlugin.java index 6f27b0a109..5457751e66 100644 --- a/core/src/main/java/tc/oc/pgm/PGMPlugin.java +++ b/core/src/main/java/tc/oc/pgm/PGMPlugin.java @@ -36,6 +36,7 @@ import tc.oc.pgm.api.map.MapOrder; import tc.oc.pgm.api.map.exception.MapException; import tc.oc.pgm.api.map.factory.MapSourceFactory; +import tc.oc.pgm.api.map.includes.MapIncludeProcessor; import tc.oc.pgm.api.match.Match; import tc.oc.pgm.api.match.MatchManager; import tc.oc.pgm.api.module.Module; @@ -57,6 +58,7 @@ import tc.oc.pgm.listeners.ServerPingDataListener; import tc.oc.pgm.listeners.WorldProblemListener; import tc.oc.pgm.map.MapLibraryImpl; +import tc.oc.pgm.map.includes.MapIncludeProcessorImpl; import tc.oc.pgm.match.MatchManagerImpl; import tc.oc.pgm.match.NoopVanishManager; import tc.oc.pgm.namedecorations.ConfigDecorationProvider; @@ -85,6 +87,7 @@ public class PGMPlugin extends JavaPlugin implements PGM, Listener { private Logger gameLogger; private Datastore datastore; private MapLibrary mapLibrary; + private MapIncludeProcessor mapIncludeProcessor; private List mapSourceFactories; private MatchManager matchManager; private MatchTabManager matchTabManager; @@ -134,7 +137,8 @@ public void onEnable() { asyncExecutorService = new BukkitExecutorService(this, true); mapSourceFactories = new ArrayList<>(); - mapLibrary = new MapLibraryImpl(gameLogger, mapSourceFactories); + mapIncludeProcessor = new MapIncludeProcessorImpl(gameLogger); + mapLibrary = new MapLibraryImpl(gameLogger, mapSourceFactories, mapIncludeProcessor); saveDefaultConfig(); // Writes a config file, if one does not exist. reloadConfig(); // Populates "this.config", if there is an error, will be null @@ -261,6 +265,7 @@ public void reloadConfig() { mapSourceFactories.clear(); mapSourceFactories.addAll(config.getMapSourceFactories()); + mapIncludeProcessor.reload(config); if (mapOrder != null) { mapOrder.reload(); diff --git a/core/src/main/java/tc/oc/pgm/api/Config.java b/core/src/main/java/tc/oc/pgm/api/Config.java index c5d171c476..79bcc4f262 100644 --- a/core/src/main/java/tc/oc/pgm/api/Config.java +++ b/core/src/main/java/tc/oc/pgm/api/Config.java @@ -60,6 +60,14 @@ public interface Config { @Nullable String getMapPoolFile(); + /** + * Gets a path to the includes directory. + * + * @return A path to the includes directory, or null for none. + */ + @Nullable + String getIncludesDirectory(); + /** * Gets a duration to wait before starting a match. * diff --git a/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java b/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java new file mode 100644 index 0000000000..c9cbcbd928 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java @@ -0,0 +1,22 @@ +package tc.oc.pgm.api.map.includes; + +import java.util.Collection; +import org.jdom2.Content; + +/** Represents a snippet of XML that can be referenced for reuse * */ +public interface MapInclude { + + /** + * Get a unique id which identifies this MapInclude. + * + * @return A unique id + */ + String getId(); + + /** + * Get a collection of {@link Content} which can be merged into an existing {@link Document} + * + * @return a collection of {@link Content} + */ + Collection getContent(); +} diff --git a/core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java b/core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java new file mode 100644 index 0000000000..82e98e0341 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java @@ -0,0 +1,26 @@ +package tc.oc.pgm.api.map.includes; + +import java.util.Collection; +import org.jdom2.Document; +import tc.oc.pgm.api.Config; +import tc.oc.pgm.util.xml.InvalidXMLException; + +/** A processor to determine which {@link MapInclude}s should be included when loading a map * */ +public interface MapIncludeProcessor { + + /** + * Process the given {@link Document} and return a collection of {@link MapInclude}s. + * + * @param document A map document + * @return A collection of map includes, collection will be empty if none are found. + * @throws InvalidXMLException If the given document is not found or able to be parsed. + */ + Collection getMapIncludes(Document document) throws InvalidXMLException; + + /** + * Reload the processor to fetch new map includes or reload existing ones. + * + * @param config A configuration file. + */ + void reload(Config config); +} diff --git a/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java b/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java index eec262cdf7..9bcd177fb5 100644 --- a/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.InputStream; +import java.util.Collection; import java.util.logging.Logger; import org.jdom2.Document; import org.jdom2.JDOMException; @@ -19,6 +20,8 @@ import tc.oc.pgm.api.map.exception.MapMissingException; import tc.oc.pgm.api.map.factory.MapFactory; import tc.oc.pgm.api.map.factory.MapModuleFactory; +import tc.oc.pgm.api.map.includes.MapInclude; +import tc.oc.pgm.api.map.includes.MapIncludeProcessor; import tc.oc.pgm.api.module.ModuleGraph; import tc.oc.pgm.api.module.exception.ModuleLoadException; import tc.oc.pgm.features.FeatureDefinitionContext; @@ -49,6 +52,7 @@ public class MapFactoryImpl extends ModuleGraph mapIncludes = includes.getMapIncludes(document); + if (!mapIncludes.isEmpty()) { + for (MapInclude include : mapIncludes) { + document.getRootElement().addContent(0, include.getContent()); + } + } + info = new MapInfoImpl(document.getRootElement()); } diff --git a/core/src/main/java/tc/oc/pgm/map/MapLibraryImpl.java b/core/src/main/java/tc/oc/pgm/map/MapLibraryImpl.java index d4cd9826b5..0e7eb53027 100644 --- a/core/src/main/java/tc/oc/pgm/map/MapLibraryImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/MapLibraryImpl.java @@ -28,6 +28,7 @@ import tc.oc.pgm.api.map.exception.MapMissingException; import tc.oc.pgm.api.map.factory.MapFactory; import tc.oc.pgm.api.map.factory.MapSourceFactory; +import tc.oc.pgm.api.map.includes.MapIncludeProcessor; import tc.oc.pgm.util.StringUtils; import tc.oc.pgm.util.UsernameResolver; @@ -37,6 +38,7 @@ public class MapLibraryImpl implements MapLibrary { private final List factories; private final SortedMap maps; private final Set failed; + private final MapIncludeProcessor includes; private static class MapEntry { private final MapSource source; @@ -50,11 +52,13 @@ private MapEntry(MapSource source, MapInfo info, MapContext context) { } } - public MapLibraryImpl(Logger logger, List factories) { + public MapLibraryImpl( + Logger logger, List factories, MapIncludeProcessor includes) { this.logger = checkNotNull(logger); // Logger should be visible in-game this.factories = Collections.synchronizedList(checkNotNull(factories)); this.maps = Collections.synchronizedSortedMap(new ConcurrentSkipListMap<>()); this.failed = Collections.synchronizedSet(new HashSet<>()); + this.includes = includes; } @Override @@ -187,7 +191,7 @@ public CompletableFuture loadExistingMap(String id) { private MapContext loadMap(MapSource source, @Nullable String mapId) throws MapException { final MapContext context; - try (final MapFactory factory = new MapFactoryImpl(logger, source)) { + try (final MapFactory factory = new MapFactoryImpl(logger, source, includes)) { context = factory.load(); } catch (MapMissingException e) { failed.remove(source); diff --git a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java new file mode 100644 index 0000000000..8d9b8f1081 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java @@ -0,0 +1,47 @@ +package tc.oc.pgm.map.includes; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collection; +import java.util.Collections; +import org.jdom2.Content; +import org.jdom2.Document; +import org.jdom2.JDOMException; +import tc.oc.pgm.api.map.exception.MapMissingException; +import tc.oc.pgm.api.map.includes.MapInclude; + +public class MapIncludeImpl implements MapInclude { + + private final String id; + private final Collection content; + + public MapIncludeImpl(File file) throws MapMissingException, JDOMException, IOException { + try { + InputStream fileStream = new FileInputStream(file); + Document doc = MapIncludeProcessorImpl.DOCUMENT_FACTORY.get().build(fileStream); + this.id = file.getName().replace(".xml", ""); + this.content = Collections.unmodifiableCollection(doc.getRootElement().cloneContent()); + } catch (FileNotFoundException e) { + throw new MapMissingException(file.getPath(), "Unable to read map include document", e); + } + } + + @Override + public String getId() { + return id; + } + + @Override + public Collection getContent() { + return content; + } + + @Override + public boolean equals(Object other) { + if (other == null || !(other instanceof MapInclude)) return false; + return ((MapInclude) other).getId().equalsIgnoreCase(getId()); + } +} diff --git a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java new file mode 100644 index 0000000000..9752da7519 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java @@ -0,0 +1,98 @@ +package tc.oc.pgm.map.includes; + +import com.google.common.collect.Sets; +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.List; +import java.util.Set; +import java.util.logging.Logger; +import javax.annotation.Nullable; +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; +import tc.oc.pgm.api.Config; +import tc.oc.pgm.api.map.exception.MapMissingException; +import tc.oc.pgm.api.map.includes.MapInclude; +import tc.oc.pgm.api.map.includes.MapIncludeProcessor; +import tc.oc.pgm.util.xml.InvalidXMLException; +import tc.oc.pgm.util.xml.SAXHandler; +import tc.oc.pgm.util.xml.XMLUtils; + +public class MapIncludeProcessorImpl implements MapIncludeProcessor { + + private final Logger logger; + private final Set includes; + + protected static final ThreadLocal DOCUMENT_FACTORY = + ThreadLocal.withInitial( + () -> { + final SAXBuilder builder = new SAXBuilder(); + builder.setSAXHandlerFactory(SAXHandler.FACTORY); + return builder; + }); + + public MapIncludeProcessorImpl(Logger logger) { + this.logger = logger; + this.includes = Sets.newHashSet(); + } + + @Nullable + private MapInclude getIncludeById(String id) { + return includes.stream() + .filter(include -> include.getId().equalsIgnoreCase(id)) + .findAny() + .orElse(null); + } + + @Override + public Collection getMapIncludes(Document document) throws InvalidXMLException { + Set mapIncludes = Sets.newHashSet(); + List elements = document.getRootElement().getChildren("include"); + for (Element element : elements) { + + String legacy = XMLUtils.getNullableAttribute(element, "src"); + if (legacy != null) { + // Send a warning to legacy include statements without preventing them from loading + logger.warning( + "[" + + document.getBaseURI() + + "] " + + "Legacy include statements are no longer supported, please upgrade to the format."); + return Sets.newHashSet(); + } + + String id = XMLUtils.getRequiredAttribute(element, "id").getValue(); + MapInclude include = getIncludeById(id); + if (include == null) + throw new InvalidXMLException( + "The provided include id '" + id + "' could not be found!", element); + + mapIncludes.add(include); + } + return mapIncludes; + } + + @Override + public void reload(Config config) { + this.includes.clear(); + + if (config.getIncludesDirectory() == null) return; + + File includeFiles = new File(config.getIncludesDirectory()); + if (!includeFiles.isDirectory()) { + logger.warning(config.getIncludesDirectory() + " is not a directory!"); + return; + } + File[] files = includeFiles.listFiles(); + for (File file : files) { + try { + this.includes.add(new MapIncludeImpl(file)); + } catch (MapMissingException | JDOMException | IOException error) { + logger.info("Unable to load " + file.getName() + " include document"); + error.printStackTrace(); + } + } + } +} diff --git a/core/src/main/resources/config.yml b/core/src/main/resources/config.yml index 162d7e7a38..bbc139cd72 100644 --- a/core/src/main/resources/config.yml +++ b/core/src/main/resources/config.yml @@ -31,6 +31,9 @@ map: # A path to a map pools file, or empty to disable map pools. pools: "map-pools.yml" + + # A path to the includes folder, or empty to disable map includes. + includes: "includes" # Sets the duration of various countdowns. # From 172b0761f2ca8bfc7b5618dea16dc46a42ac9e7d Mon Sep 17 00:00:00 2001 From: applenick Date: Sat, 9 Jul 2022 15:00:52 -0700 Subject: [PATCH 2/5] Add global.xml support Signed-off-by: applenick --- .../oc/pgm/api/map/includes/MapIncludeProcessor.java | 9 +++++++++ core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java | 11 +++++++---- .../oc/pgm/map/includes/MapIncludeProcessorImpl.java | 5 +++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java b/core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java index 82e98e0341..9955526691 100644 --- a/core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java +++ b/core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java @@ -1,6 +1,7 @@ package tc.oc.pgm.api.map.includes; import java.util.Collection; +import javax.annotation.Nullable; import org.jdom2.Document; import tc.oc.pgm.api.Config; import tc.oc.pgm.util.xml.InvalidXMLException; @@ -8,6 +9,14 @@ /** A processor to determine which {@link MapInclude}s should be included when loading a map * */ public interface MapIncludeProcessor { + /** + * Get a {@link MapInclude} which should be included on all maps or null if none. + * + * @return a {@link MapInclude} + */ + @Nullable + MapInclude getGlobalInclude(); + /** * Process the given {@link Document} and return a collection of {@link MapInclude}s. * diff --git a/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java b/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java index 9bcd177fb5..6ba13b7bd4 100644 --- a/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java @@ -87,12 +87,15 @@ private void preLoad() document.setBaseURI(source.getId()); } + // Add global include file to the document if present + if (includes.getGlobalInclude() != null) { + document.getRootElement().addContent(0, includes.getGlobalInclude().getContent()); + } + // Check for any included map sources, appending them to the document if present Collection mapIncludes = includes.getMapIncludes(document); - if (!mapIncludes.isEmpty()) { - for (MapInclude include : mapIncludes) { - document.getRootElement().addContent(0, include.getContent()); - } + for (MapInclude include : mapIncludes) { + document.getRootElement().addContent(0, include.getContent()); } info = new MapInfoImpl(document.getRootElement()); diff --git a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java index 9752da7519..9ff3f80c12 100644 --- a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java @@ -46,6 +46,11 @@ private MapInclude getIncludeById(String id) { .orElse(null); } + @Override + public MapInclude getGlobalInclude() { + return getIncludeById("global"); + } + @Override public Collection getMapIncludes(Document document) throws InvalidXMLException { Set mapIncludes = Sets.newHashSet(); From cb23d5309dceb91b67240c495fd9d03024c79a5a Mon Sep 17 00:00:00 2001 From: applenick Date: Sat, 9 Jul 2022 23:47:24 -0700 Subject: [PATCH 3/5] Clone MapInclude content instead of storing as a collection Signed-off-by: applenick --- .../main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java index 8d9b8f1081..9277520bbf 100644 --- a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java @@ -6,7 +6,6 @@ import java.io.IOException; import java.io.InputStream; import java.util.Collection; -import java.util.Collections; import org.jdom2.Content; import org.jdom2.Document; import org.jdom2.JDOMException; @@ -16,14 +15,13 @@ public class MapIncludeImpl implements MapInclude { private final String id; - private final Collection content; + private final Document source; public MapIncludeImpl(File file) throws MapMissingException, JDOMException, IOException { try { InputStream fileStream = new FileInputStream(file); - Document doc = MapIncludeProcessorImpl.DOCUMENT_FACTORY.get().build(fileStream); this.id = file.getName().replace(".xml", ""); - this.content = Collections.unmodifiableCollection(doc.getRootElement().cloneContent()); + this.source = MapIncludeProcessorImpl.DOCUMENT_FACTORY.get().build(fileStream); } catch (FileNotFoundException e) { throw new MapMissingException(file.getPath(), "Unable to read map include document", e); } @@ -36,7 +34,7 @@ public String getId() { @Override public Collection getContent() { - return content; + return source.getRootElement().cloneContent(); } @Override From 42d9abd3500c9b2963ef9f04364fa660e28caab9 Mon Sep 17 00:00:00 2001 From: applenick Date: Sun, 17 Jul 2022 00:14:29 -0700 Subject: [PATCH 4/5] Handle detecting changes to include files Signed-off-by: applenick --- .../java/tc/oc/pgm/api/map/MapLibrary.java | 8 +++++ .../java/tc/oc/pgm/api/map/MapSource.java | 11 +++++++ .../oc/pgm/api/map/includes/MapInclude.java | 7 ++++ .../api/map/includes/MapIncludeProcessor.java | 17 +++++----- .../api/map/includes/StoredMapInclude.java | 22 +++++++++++++ .../java/tc/oc/pgm/map/MapFactoryImpl.java | 13 +++++--- .../java/tc/oc/pgm/map/MapLibraryImpl.java | 5 +++ .../oc/pgm/map/includes/MapIncludeImpl.java | 9 ++++++ .../map/includes/MapIncludeProcessorImpl.java | 24 ++++++++------ .../map/includes/StoredMapIncludeImpl.java | 25 +++++++++++++++ .../map/source/SystemMapSourceFactory.java | 32 ++++++++++++++++++- 11 files changed, 148 insertions(+), 25 deletions(-) create mode 100644 core/src/main/java/tc/oc/pgm/api/map/includes/StoredMapInclude.java create mode 100644 core/src/main/java/tc/oc/pgm/map/includes/StoredMapIncludeImpl.java diff --git a/core/src/main/java/tc/oc/pgm/api/map/MapLibrary.java b/core/src/main/java/tc/oc/pgm/api/map/MapLibrary.java index c985038b35..5161d8742b 100644 --- a/core/src/main/java/tc/oc/pgm/api/map/MapLibrary.java +++ b/core/src/main/java/tc/oc/pgm/api/map/MapLibrary.java @@ -3,6 +3,7 @@ import java.util.Iterator; import java.util.concurrent.CompletableFuture; import javax.annotation.Nullable; +import tc.oc.pgm.api.map.includes.MapIncludeProcessor; /** A library of {@link MapInfo}s and {@link MapContext}s. */ public interface MapLibrary { @@ -45,4 +46,11 @@ public interface MapLibrary { * @return A {@link MapContext}. */ CompletableFuture loadExistingMap(String id); + + /** + * Get the {@link MapIncludeProcessor}. + * + * @return A {@link MapIncludeProcessor} + */ + MapIncludeProcessor getIncludeProcessor(); } diff --git a/core/src/main/java/tc/oc/pgm/api/map/MapSource.java b/core/src/main/java/tc/oc/pgm/api/map/MapSource.java index f65c4fd22c..a3e583e791 100644 --- a/core/src/main/java/tc/oc/pgm/api/map/MapSource.java +++ b/core/src/main/java/tc/oc/pgm/api/map/MapSource.java @@ -3,6 +3,7 @@ import java.io.File; import java.io.InputStream; import tc.oc.pgm.api.map.exception.MapMissingException; +import tc.oc.pgm.api.map.includes.StoredMapInclude; /** A source where {@link MapInfo} documents and files are downloaded. */ public interface MapSource { @@ -38,4 +39,14 @@ public interface MapSource { * @throws MapMissingException If the document can no longer be found. */ boolean checkForUpdates() throws MapMissingException; + + /** + * Adds a {@link StoredMapInclude} which holds information about a {@link MapInclude} + * + * @param include The {@link StoredMapInclude} + */ + void addMapInclude(StoredMapInclude include); + + /** Remove all associated {@link StoredMapInclude}, used when reloading document. */ + void clearIncludes(); } diff --git a/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java b/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java index c9cbcbd928..4447f71285 100644 --- a/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java +++ b/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java @@ -13,6 +13,13 @@ public interface MapInclude { */ String getId(); + /** + * Get the system file time from when this MapInclude file was last modified. + * + * @return Time of last file modification + */ + long getLastModified(); + /** * Get a collection of {@link Content} which can be merged into an existing {@link Document} * diff --git a/core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java b/core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java index 9955526691..58d5658154 100644 --- a/core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java +++ b/core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java @@ -1,7 +1,6 @@ package tc.oc.pgm.api.map.includes; import java.util.Collection; -import javax.annotation.Nullable; import org.jdom2.Document; import tc.oc.pgm.api.Config; import tc.oc.pgm.util.xml.InvalidXMLException; @@ -9,14 +8,6 @@ /** A processor to determine which {@link MapInclude}s should be included when loading a map * */ public interface MapIncludeProcessor { - /** - * Get a {@link MapInclude} which should be included on all maps or null if none. - * - * @return a {@link MapInclude} - */ - @Nullable - MapInclude getGlobalInclude(); - /** * Process the given {@link Document} and return a collection of {@link MapInclude}s. * @@ -26,6 +17,14 @@ public interface MapIncludeProcessor { */ Collection getMapIncludes(Document document) throws InvalidXMLException; + /** + * Get a {@link MapInclude} by its id + * + * @param includeId ID of the map include + * @return A {@link MapInclude} + */ + MapInclude getMapIncludeById(String includeId); + /** * Reload the processor to fetch new map includes or reload existing ones. * diff --git a/core/src/main/java/tc/oc/pgm/api/map/includes/StoredMapInclude.java b/core/src/main/java/tc/oc/pgm/api/map/includes/StoredMapInclude.java new file mode 100644 index 0000000000..d020446059 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/api/map/includes/StoredMapInclude.java @@ -0,0 +1,22 @@ +package tc.oc.pgm.api.map.includes; + +/** + * A snapshot of {@link MapInclude} info, used to determine when include files have been updated. * + */ +public interface StoredMapInclude { + + /** + * Gets the unique include id, used to reference a {@link MapInclude}. + * + * @return A unique include id + */ + String getIncludeId(); + + /** + * Gets whether the associated {@link MapInclude} files have changed since last loading. + * + * @param time The current system time + * @return True if given time is newer than last modified time + */ + boolean hasBeenModified(long time); +} diff --git a/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java b/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java index 6ba13b7bd4..e34d17ab9b 100644 --- a/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java @@ -31,6 +31,7 @@ import tc.oc.pgm.kits.FeatureKitParser; import tc.oc.pgm.kits.KitParser; import tc.oc.pgm.kits.LegacyKitParser; +import tc.oc.pgm.map.includes.StoredMapIncludeImpl; import tc.oc.pgm.regions.FeatureRegionParser; import tc.oc.pgm.regions.LegacyRegionParser; import tc.oc.pgm.regions.RegionParser; @@ -76,26 +77,28 @@ protected MapModule createModule(MapModuleFactory factory) throws ModuleLoadExce } } + private void storeInclude(MapInclude include) { + this.source.addMapInclude(new StoredMapIncludeImpl(include.getId(), include.getLastModified())); + } + private void preLoad() throws IOException, JDOMException, InvalidXMLException, MapMissingException { if (document != null && !source.checkForUpdates()) { return; // If a document is present and there are no updates, skip loading again } + source.clearIncludes(); + try (final InputStream stream = source.getDocument()) { document = DOCUMENT_FACTORY.get().build(stream); document.setBaseURI(source.getId()); } - // Add global include file to the document if present - if (includes.getGlobalInclude() != null) { - document.getRootElement().addContent(0, includes.getGlobalInclude().getContent()); - } - // Check for any included map sources, appending them to the document if present Collection mapIncludes = includes.getMapIncludes(document); for (MapInclude include : mapIncludes) { document.getRootElement().addContent(0, include.getContent()); + storeInclude(include); } info = new MapInfoImpl(document.getRootElement()); diff --git a/core/src/main/java/tc/oc/pgm/map/MapLibraryImpl.java b/core/src/main/java/tc/oc/pgm/map/MapLibraryImpl.java index 0e7eb53027..6df17ed67a 100644 --- a/core/src/main/java/tc/oc/pgm/map/MapLibraryImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/MapLibraryImpl.java @@ -83,6 +83,11 @@ public long getSize() { return maps.size(); } + @Override + public MapIncludeProcessor getIncludeProcessor() { + return includes; + } + private void logMapError(MapException err) { logger.log(Level.WARNING, err.getMessage(), err); } diff --git a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java index 9277520bbf..7896f56dc4 100644 --- a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.Collection; +import java.util.concurrent.atomic.AtomicLong; import org.jdom2.Content; import org.jdom2.Document; import org.jdom2.JDOMException; @@ -16,6 +17,7 @@ public class MapIncludeImpl implements MapInclude { private final String id; private final Document source; + private final AtomicLong lastModified; public MapIncludeImpl(File file) throws MapMissingException, JDOMException, IOException { try { @@ -24,6 +26,8 @@ public MapIncludeImpl(File file) throws MapMissingException, JDOMException, IOEx this.source = MapIncludeProcessorImpl.DOCUMENT_FACTORY.get().build(fileStream); } catch (FileNotFoundException e) { throw new MapMissingException(file.getPath(), "Unable to read map include document", e); + } finally { + lastModified = new AtomicLong(file.lastModified()); } } @@ -42,4 +46,9 @@ public boolean equals(Object other) { if (other == null || !(other instanceof MapInclude)) return false; return ((MapInclude) other).getId().equalsIgnoreCase(getId()); } + + @Override + public long getLastModified() { + return lastModified.get(); + } } diff --git a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java index 9ff3f80c12..6cd973a634 100644 --- a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Set; import java.util.logging.Logger; -import javax.annotation.Nullable; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; @@ -38,22 +37,27 @@ public MapIncludeProcessorImpl(Logger logger) { this.includes = Sets.newHashSet(); } - @Nullable - private MapInclude getIncludeById(String id) { - return includes.stream() - .filter(include -> include.getId().equalsIgnoreCase(id)) - .findAny() - .orElse(null); + public MapInclude getGlobalInclude() { + return getMapIncludeById("global"); } @Override - public MapInclude getGlobalInclude() { - return getIncludeById("global"); + public MapInclude getMapIncludeById(String includeId) { + return includes.stream() + .filter(include -> include.getId().equalsIgnoreCase(includeId)) + .findAny() + .orElse(null); } @Override public Collection getMapIncludes(Document document) throws InvalidXMLException { Set mapIncludes = Sets.newHashSet(); + + // Always add global include if present + if (getGlobalInclude() != null) { + mapIncludes.add(getGlobalInclude()); + } + List elements = document.getRootElement().getChildren("include"); for (Element element : elements) { @@ -69,7 +73,7 @@ public Collection getMapIncludes(Document document) throws InvalidXM } String id = XMLUtils.getRequiredAttribute(element, "id").getValue(); - MapInclude include = getIncludeById(id); + MapInclude include = getMapIncludeById(id); if (include == null) throw new InvalidXMLException( "The provided include id '" + id + "' could not be found!", element); diff --git a/core/src/main/java/tc/oc/pgm/map/includes/StoredMapIncludeImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/StoredMapIncludeImpl.java new file mode 100644 index 0000000000..42a824e460 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/map/includes/StoredMapIncludeImpl.java @@ -0,0 +1,25 @@ +package tc.oc.pgm.map.includes; + +import java.util.concurrent.atomic.AtomicLong; +import tc.oc.pgm.api.map.includes.StoredMapInclude; + +public class StoredMapIncludeImpl implements StoredMapInclude { + + private final String includeId; + private final AtomicLong lastModified; + + public StoredMapIncludeImpl(String includeId, long lastModified) { + this.includeId = includeId; + this.lastModified = new AtomicLong(lastModified); + } + + @Override + public String getIncludeId() { + return includeId; + } + + @Override + public boolean hasBeenModified(long time) { + return time > lastModified.get(); + } +} diff --git a/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java b/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java index 755ac89b01..18182139c0 100644 --- a/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java +++ b/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java @@ -2,6 +2,7 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.collect.Sets; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -10,11 +11,16 @@ import java.nio.file.FileVisitOption; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Set; import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Stream; import org.apache.commons.lang3.builder.ToStringBuilder; +import tc.oc.pgm.api.PGM; import tc.oc.pgm.api.map.MapSource; import tc.oc.pgm.api.map.exception.MapMissingException; +import tc.oc.pgm.api.map.includes.MapInclude; +import tc.oc.pgm.api.map.includes.MapIncludeProcessor; +import tc.oc.pgm.api.map.includes.StoredMapInclude; import tc.oc.pgm.util.FileUtils; public class SystemMapSourceFactory extends PathMapSourceFactory { @@ -44,10 +50,14 @@ protected static class SystemMapSource implements MapSource { private final String dir; private final AtomicLong modified; + private final Set storedIncludes; + private final MapIncludeProcessor includes; private SystemMapSource(String dir) { this.dir = checkNotNull(dir); this.modified = new AtomicLong(-1); + this.storedIncludes = Sets.newHashSet(); + this.includes = PGM.get().getMapLibrary().getIncludeProcessor(); } private File getDirectory() throws MapMissingException { @@ -118,7 +128,7 @@ public boolean checkForUpdates() throws MapMissingException { final File file = getFile(); final long mod = modified.get(); - return mod > 0 && file.lastModified() > mod; + return (mod > 0 && file.lastModified() > mod) || checkForIncludeUpdates(); } @Override @@ -139,5 +149,25 @@ public String toString() { .append("modified", modified.get()) .build(); } + + @Override + public void addMapInclude(StoredMapInclude include) { + this.storedIncludes.add(include); + } + + @Override + public void clearIncludes() { + this.storedIncludes.clear(); + } + + private boolean checkForIncludeUpdates() { + for (StoredMapInclude stored : storedIncludes) { + MapInclude include = includes.getMapIncludeById(stored.getIncludeId()); + if (stored.hasBeenModified(include.getLastModified())) { + return true; + } + } + return false; + } } } From 4fa2ed4c4e5011870ae2ac3079a91d095645682f Mon Sep 17 00:00:00 2001 From: applenick Date: Thu, 11 Aug 2022 18:48:50 -0700 Subject: [PATCH 5/5] Remove StoredMapInclude and other small fixes Signed-off-by: applenick --- core/src/main/java/tc/oc/pgm/PGMConfig.java | 3 ++- .../java/tc/oc/pgm/api/map/MapSource.java | 10 ++++---- .../oc/pgm/api/map/includes/MapInclude.java | 8 ++++++ .../api/map/includes/StoredMapInclude.java | 22 ---------------- .../java/tc/oc/pgm/map/MapFactoryImpl.java | 3 +-- .../oc/pgm/map/includes/MapIncludeImpl.java | 5 ++++ .../map/includes/MapIncludeProcessorImpl.java | 20 +++++++-------- .../map/includes/StoredMapIncludeImpl.java | 25 ------------------- .../map/source/SystemMapSourceFactory.java | 14 +++-------- 9 files changed, 35 insertions(+), 75 deletions(-) delete mode 100644 core/src/main/java/tc/oc/pgm/api/map/includes/StoredMapInclude.java delete mode 100644 core/src/main/java/tc/oc/pgm/map/includes/StoredMapIncludeImpl.java diff --git a/core/src/main/java/tc/oc/pgm/PGMConfig.java b/core/src/main/java/tc/oc/pgm/PGMConfig.java index 786330c781..bfed54c0ba 100644 --- a/core/src/main/java/tc/oc/pgm/PGMConfig.java +++ b/core/src/main/java/tc/oc/pgm/PGMConfig.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.TreeSet; import java.util.logging.Level; +import javax.annotation.Nullable; import net.kyori.adventure.text.Component; import org.bukkit.ChatColor; import org.bukkit.configuration.ConfigurationSection; @@ -473,7 +474,7 @@ public String getMapPoolFile() { } @Override - public String getIncludesDirectory() { + public @Nullable String getIncludesDirectory() { return includesDirectory; } diff --git a/core/src/main/java/tc/oc/pgm/api/map/MapSource.java b/core/src/main/java/tc/oc/pgm/api/map/MapSource.java index a3e583e791..4a96615342 100644 --- a/core/src/main/java/tc/oc/pgm/api/map/MapSource.java +++ b/core/src/main/java/tc/oc/pgm/api/map/MapSource.java @@ -3,7 +3,7 @@ import java.io.File; import java.io.InputStream; import tc.oc.pgm.api.map.exception.MapMissingException; -import tc.oc.pgm.api.map.includes.StoredMapInclude; +import tc.oc.pgm.api.map.includes.MapInclude; /** A source where {@link MapInfo} documents and files are downloaded. */ public interface MapSource { @@ -41,12 +41,12 @@ public interface MapSource { boolean checkForUpdates() throws MapMissingException; /** - * Adds a {@link StoredMapInclude} which holds information about a {@link MapInclude} + * Adds an associated {@link MapInclude} * - * @param include The {@link StoredMapInclude} + * @param include The {@link MapInclude} */ - void addMapInclude(StoredMapInclude include); + void addMapInclude(MapInclude include); - /** Remove all associated {@link StoredMapInclude}, used when reloading document. */ + /** Remove all associated {@link MapInclude}, used when reloading document. */ void clearIncludes(); } diff --git a/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java b/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java index 4447f71285..e9a3df7e82 100644 --- a/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java +++ b/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java @@ -20,6 +20,14 @@ public interface MapInclude { */ long getLastModified(); + /** + * Gets whether the associated {@link MapInclude} files have changed since last loading. + * + * @param time The current system time + * @return True if given time is newer than last modified time + */ + boolean hasBeenModified(long time); + /** * Get a collection of {@link Content} which can be merged into an existing {@link Document} * diff --git a/core/src/main/java/tc/oc/pgm/api/map/includes/StoredMapInclude.java b/core/src/main/java/tc/oc/pgm/api/map/includes/StoredMapInclude.java deleted file mode 100644 index d020446059..0000000000 --- a/core/src/main/java/tc/oc/pgm/api/map/includes/StoredMapInclude.java +++ /dev/null @@ -1,22 +0,0 @@ -package tc.oc.pgm.api.map.includes; - -/** - * A snapshot of {@link MapInclude} info, used to determine when include files have been updated. * - */ -public interface StoredMapInclude { - - /** - * Gets the unique include id, used to reference a {@link MapInclude}. - * - * @return A unique include id - */ - String getIncludeId(); - - /** - * Gets whether the associated {@link MapInclude} files have changed since last loading. - * - * @param time The current system time - * @return True if given time is newer than last modified time - */ - boolean hasBeenModified(long time); -} diff --git a/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java b/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java index e34d17ab9b..efcce812df 100644 --- a/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java @@ -31,7 +31,6 @@ import tc.oc.pgm.kits.FeatureKitParser; import tc.oc.pgm.kits.KitParser; import tc.oc.pgm.kits.LegacyKitParser; -import tc.oc.pgm.map.includes.StoredMapIncludeImpl; import tc.oc.pgm.regions.FeatureRegionParser; import tc.oc.pgm.regions.LegacyRegionParser; import tc.oc.pgm.regions.RegionParser; @@ -78,7 +77,7 @@ protected MapModule createModule(MapModuleFactory factory) throws ModuleLoadExce } private void storeInclude(MapInclude include) { - this.source.addMapInclude(new StoredMapIncludeImpl(include.getId(), include.getLastModified())); + this.source.addMapInclude(include); } private void preLoad() diff --git a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java index 7896f56dc4..d608e47f4a 100644 --- a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java @@ -51,4 +51,9 @@ public boolean equals(Object other) { public long getLastModified() { return lastModified.get(); } + + @Override + public boolean hasBeenModified(long time) { + return time > lastModified.get(); + } } diff --git a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java index 6cd973a634..269bbaf6c7 100644 --- a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java @@ -1,10 +1,12 @@ package tc.oc.pgm.map.includes; +import com.google.common.collect.Maps; import com.google.common.collect.Sets; import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.logging.Logger; import org.jdom2.Document; @@ -16,13 +18,14 @@ import tc.oc.pgm.api.map.includes.MapInclude; import tc.oc.pgm.api.map.includes.MapIncludeProcessor; import tc.oc.pgm.util.xml.InvalidXMLException; +import tc.oc.pgm.util.xml.Node; import tc.oc.pgm.util.xml.SAXHandler; import tc.oc.pgm.util.xml.XMLUtils; public class MapIncludeProcessorImpl implements MapIncludeProcessor { private final Logger logger; - private final Set includes; + private final Map includes; protected static final ThreadLocal DOCUMENT_FACTORY = ThreadLocal.withInitial( @@ -34,7 +37,7 @@ public class MapIncludeProcessorImpl implements MapIncludeProcessor { public MapIncludeProcessorImpl(Logger logger) { this.logger = logger; - this.includes = Sets.newHashSet(); + this.includes = Maps.newHashMap(); } public MapInclude getGlobalInclude() { @@ -43,10 +46,7 @@ public MapInclude getGlobalInclude() { @Override public MapInclude getMapIncludeById(String includeId) { - return includes.stream() - .filter(include -> include.getId().equalsIgnoreCase(includeId)) - .findAny() - .orElse(null); + return includes.get(includeId); } @Override @@ -61,15 +61,14 @@ public Collection getMapIncludes(Document document) throws InvalidXM List elements = document.getRootElement().getChildren("include"); for (Element element : elements) { - String legacy = XMLUtils.getNullableAttribute(element, "src"); - if (legacy != null) { + if (Node.fromAttr(element, "src") != null) { // Send a warning to legacy include statements without preventing them from loading logger.warning( "[" + document.getBaseURI() + "] " + "Legacy include statements are no longer supported, please upgrade to the format."); - return Sets.newHashSet(); + continue; } String id = XMLUtils.getRequiredAttribute(element, "id").getValue(); @@ -97,7 +96,8 @@ public void reload(Config config) { File[] files = includeFiles.listFiles(); for (File file : files) { try { - this.includes.add(new MapIncludeImpl(file)); + MapIncludeImpl include = new MapIncludeImpl(file); + this.includes.put(include.getId(), include); } catch (MapMissingException | JDOMException | IOException error) { logger.info("Unable to load " + file.getName() + " include document"); error.printStackTrace(); diff --git a/core/src/main/java/tc/oc/pgm/map/includes/StoredMapIncludeImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/StoredMapIncludeImpl.java deleted file mode 100644 index 42a824e460..0000000000 --- a/core/src/main/java/tc/oc/pgm/map/includes/StoredMapIncludeImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -package tc.oc.pgm.map.includes; - -import java.util.concurrent.atomic.AtomicLong; -import tc.oc.pgm.api.map.includes.StoredMapInclude; - -public class StoredMapIncludeImpl implements StoredMapInclude { - - private final String includeId; - private final AtomicLong lastModified; - - public StoredMapIncludeImpl(String includeId, long lastModified) { - this.includeId = includeId; - this.lastModified = new AtomicLong(lastModified); - } - - @Override - public String getIncludeId() { - return includeId; - } - - @Override - public boolean hasBeenModified(long time) { - return time > lastModified.get(); - } -} diff --git a/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java b/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java index 18182139c0..cd4f5da1e0 100644 --- a/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java +++ b/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java @@ -15,12 +15,9 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Stream; import org.apache.commons.lang3.builder.ToStringBuilder; -import tc.oc.pgm.api.PGM; import tc.oc.pgm.api.map.MapSource; import tc.oc.pgm.api.map.exception.MapMissingException; import tc.oc.pgm.api.map.includes.MapInclude; -import tc.oc.pgm.api.map.includes.MapIncludeProcessor; -import tc.oc.pgm.api.map.includes.StoredMapInclude; import tc.oc.pgm.util.FileUtils; public class SystemMapSourceFactory extends PathMapSourceFactory { @@ -50,14 +47,12 @@ protected static class SystemMapSource implements MapSource { private final String dir; private final AtomicLong modified; - private final Set storedIncludes; - private final MapIncludeProcessor includes; + private final Set storedIncludes; private SystemMapSource(String dir) { this.dir = checkNotNull(dir); this.modified = new AtomicLong(-1); this.storedIncludes = Sets.newHashSet(); - this.includes = PGM.get().getMapLibrary().getIncludeProcessor(); } private File getDirectory() throws MapMissingException { @@ -151,7 +146,7 @@ public String toString() { } @Override - public void addMapInclude(StoredMapInclude include) { + public void addMapInclude(MapInclude include) { this.storedIncludes.add(include); } @@ -161,9 +156,8 @@ public void clearIncludes() { } private boolean checkForIncludeUpdates() { - for (StoredMapInclude stored : storedIncludes) { - MapInclude include = includes.getMapIncludeById(stored.getIncludeId()); - if (stored.hasBeenModified(include.getLastModified())) { + for (MapInclude include : storedIncludes) { + if (include.hasBeenModified(include.getLastModified())) { return true; } }