From e535a8884081a780ff704aa5ae0d90f5392c3421 Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Sun, 30 Aug 2015 23:21:58 -0500 Subject: [PATCH] Stop firing BeatmapWatchService events for intended file modifications. Specifically, during OSZ unpacking and for file deletion through the song menu. Triggering the beatmap reload event in these scenarios would be redundant. Signed-off-by: Jeffrey Han --- src/itdelatrisu/opsu/OszUnpacker.java | 7 +++++++ .../opsu/beatmap/BeatmapSetList.java | 11 ++++++++++ .../opsu/beatmap/BeatmapWatchService.java | 20 +++++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/itdelatrisu/opsu/OszUnpacker.java b/src/itdelatrisu/opsu/OszUnpacker.java index b8bf1460..e1eb36ba 100644 --- a/src/itdelatrisu/opsu/OszUnpacker.java +++ b/src/itdelatrisu/opsu/OszUnpacker.java @@ -18,6 +18,8 @@ package itdelatrisu.opsu; +import itdelatrisu.opsu.beatmap.BeatmapWatchService; + import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; @@ -62,6 +64,9 @@ public class OszUnpacker { } // unpack OSZs + BeatmapWatchService ws = (Options.isWatchServiceEnabled()) ? BeatmapWatchService.get() : null; + if (ws != null) + ws.pause(); for (File file : files) { fileIndex++; String dirName = file.getName().substring(0, file.getName().lastIndexOf('.')); @@ -73,6 +78,8 @@ public class OszUnpacker { dirs.add(songDir); } } + if (ws != null) + ws.resume(); fileIndex = -1; files = null; diff --git a/src/itdelatrisu/opsu/beatmap/BeatmapSetList.java b/src/itdelatrisu/opsu/beatmap/BeatmapSetList.java index c146ee67..c37b4dd1 100644 --- a/src/itdelatrisu/opsu/beatmap/BeatmapSetList.java +++ b/src/itdelatrisu/opsu/beatmap/BeatmapSetList.java @@ -19,6 +19,7 @@ package itdelatrisu.opsu.beatmap; import itdelatrisu.opsu.ErrorHandler; +import itdelatrisu.opsu.Options; import itdelatrisu.opsu.Utils; import itdelatrisu.opsu.audio.MusicController; import itdelatrisu.opsu.db.BeatmapDB; @@ -199,11 +200,16 @@ public class BeatmapSetList { BeatmapDB.delete(dir.getName()); // delete the associated directory + BeatmapWatchService ws = (Options.isWatchServiceEnabled()) ? BeatmapWatchService.get() : null; + if (ws != null) + ws.pause(); try { Utils.deleteToTrash(dir); } catch (IOException e) { ErrorHandler.error("Could not delete song group.", e, true); } + if (ws != null) + ws.resume(); return true; } @@ -247,11 +253,16 @@ public class BeatmapSetList { BeatmapDB.delete(file.getParentFile().getName(), file.getName()); // delete the associated file + BeatmapWatchService ws = (Options.isWatchServiceEnabled()) ? BeatmapWatchService.get() : null; + if (ws != null) + ws.pause(); try { Utils.deleteToTrash(file); } catch (IOException e) { ErrorHandler.error("Could not delete song.", e, true); } + if (ws != null) + ws.resume(); return true; } diff --git a/src/itdelatrisu/opsu/beatmap/BeatmapWatchService.java b/src/itdelatrisu/opsu/beatmap/BeatmapWatchService.java index a287e198..e63482cf 100644 --- a/src/itdelatrisu/opsu/beatmap/BeatmapWatchService.java +++ b/src/itdelatrisu/opsu/beatmap/BeatmapWatchService.java @@ -162,6 +162,9 @@ public class BeatmapWatchService { /** The Executor. */ private ExecutorService service; + /** Whether the watch service is paused (i.e. does not fire events). */ + private boolean paused = false; + /** * Creates the WatchService. * @throws IOException if an I/O error occurs @@ -242,6 +245,7 @@ public class BeatmapWatchService { if (dir == null) continue; + boolean isPaused = paused; for (WatchEvent event : key.pollEvents()) { WatchEvent.Kind kind = event.kind(); if (kind == StandardWatchEventKinds.OVERFLOW) @@ -254,8 +258,10 @@ public class BeatmapWatchService { //System.out.printf("%s: %s\n", kind.name(), child); // fire listeners - for (BeatmapWatchServiceListener listener : listeners) - listener.eventReceived(kind, child); + if (!isPaused) { + for (BeatmapWatchServiceListener listener : listeners) + listener.eventReceived(kind, child); + } // if directory is created, then register it and its sub-directories if (kind == StandardWatchEventKinds.ENTRY_CREATE) { @@ -272,4 +278,14 @@ public class BeatmapWatchService { } } } + + /** + * Stops listener events from being fired. + */ + public void pause() { paused = true; } + + /** + * Resumes firing listener events. + */ + public void resume() { paused = false; } }