Skip to content

Commit

Permalink
Manually add maps
Browse files Browse the repository at this point in the history
Adds watcher for manually added maps
Fixes FAForever#1879
  • Loading branch information
1-alex98 authored and Chris Haggan committed Apr 15, 2022
1 parent 3b04c18 commit 979efac
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/main/java/com/faforever/client/map/MapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import static com.google.common.net.UrlEscapers.urlFragmentEscaper;
import static java.lang.String.format;
import static java.nio.file.Files.list;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.util.stream.Collectors.toCollection;

Expand Down Expand Up @@ -196,12 +197,24 @@ private void tryLoadMaps() {
private Thread startDirectoryWatcher(Path mapsDirectory) {
Thread thread = new Thread(() -> noCatch(() -> {
try (WatchService watcher = mapsDirectory.getFileSystem().newWatchService()) {
forgedAlliancePreferences.getCustomMapsDirectory().register(watcher, ENTRY_DELETE);
forgedAlliancePreferences.getCustomMapsDirectory().register(watcher, ENTRY_DELETE, ENTRY_CREATE);
while (!Thread.interrupted()) {
WatchKey key = watcher.take();
key.pollEvents().stream()
.filter(event -> event.kind() == ENTRY_DELETE)
.forEach(event -> removeMap(mapsDirectory.resolve((Path) event.context())));
.filter(event -> event.kind() == ENTRY_DELETE || event.kind() == ENTRY_CREATE)
.forEach(event -> {
if (event.kind() == ENTRY_DELETE) {
removeMap(mapsDirectory.resolve((Path) event.context()));
} else if (event.kind() == ENTRY_CREATE) {
Path mapPath = mapsDirectory.resolve((Path) event.context());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.debug("Thread interrupted ({})", e.getMessage());
}
addInstalledMap(mapPath);
}
});
key.reset();
}
} catch (InterruptedException e) {
Expand Down

0 comments on commit 979efac

Please sign in to comment.