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 <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-08-30 23:21:58 -05:00
parent aed5163a83
commit e535a88840
3 changed files with 36 additions and 2 deletions

View File

@ -18,6 +18,8 @@
package itdelatrisu.opsu; package itdelatrisu.opsu;
import itdelatrisu.opsu.beatmap.BeatmapWatchService;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.util.ArrayList; import java.util.ArrayList;
@ -62,6 +64,9 @@ public class OszUnpacker {
} }
// unpack OSZs // unpack OSZs
BeatmapWatchService ws = (Options.isWatchServiceEnabled()) ? BeatmapWatchService.get() : null;
if (ws != null)
ws.pause();
for (File file : files) { for (File file : files) {
fileIndex++; fileIndex++;
String dirName = file.getName().substring(0, file.getName().lastIndexOf('.')); String dirName = file.getName().substring(0, file.getName().lastIndexOf('.'));
@ -73,6 +78,8 @@ public class OszUnpacker {
dirs.add(songDir); dirs.add(songDir);
} }
} }
if (ws != null)
ws.resume();
fileIndex = -1; fileIndex = -1;
files = null; files = null;

View File

@ -19,6 +19,7 @@
package itdelatrisu.opsu.beatmap; package itdelatrisu.opsu.beatmap;
import itdelatrisu.opsu.ErrorHandler; import itdelatrisu.opsu.ErrorHandler;
import itdelatrisu.opsu.Options;
import itdelatrisu.opsu.Utils; import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.audio.MusicController; import itdelatrisu.opsu.audio.MusicController;
import itdelatrisu.opsu.db.BeatmapDB; import itdelatrisu.opsu.db.BeatmapDB;
@ -199,11 +200,16 @@ public class BeatmapSetList {
BeatmapDB.delete(dir.getName()); BeatmapDB.delete(dir.getName());
// delete the associated directory // delete the associated directory
BeatmapWatchService ws = (Options.isWatchServiceEnabled()) ? BeatmapWatchService.get() : null;
if (ws != null)
ws.pause();
try { try {
Utils.deleteToTrash(dir); Utils.deleteToTrash(dir);
} catch (IOException e) { } catch (IOException e) {
ErrorHandler.error("Could not delete song group.", e, true); ErrorHandler.error("Could not delete song group.", e, true);
} }
if (ws != null)
ws.resume();
return true; return true;
} }
@ -247,11 +253,16 @@ public class BeatmapSetList {
BeatmapDB.delete(file.getParentFile().getName(), file.getName()); BeatmapDB.delete(file.getParentFile().getName(), file.getName());
// delete the associated file // delete the associated file
BeatmapWatchService ws = (Options.isWatchServiceEnabled()) ? BeatmapWatchService.get() : null;
if (ws != null)
ws.pause();
try { try {
Utils.deleteToTrash(file); Utils.deleteToTrash(file);
} catch (IOException e) { } catch (IOException e) {
ErrorHandler.error("Could not delete song.", e, true); ErrorHandler.error("Could not delete song.", e, true);
} }
if (ws != null)
ws.resume();
return true; return true;
} }

View File

@ -162,6 +162,9 @@ public class BeatmapWatchService {
/** The Executor. */ /** The Executor. */
private ExecutorService service; private ExecutorService service;
/** Whether the watch service is paused (i.e. does not fire events). */
private boolean paused = false;
/** /**
* Creates the WatchService. * Creates the WatchService.
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
@ -242,6 +245,7 @@ public class BeatmapWatchService {
if (dir == null) if (dir == null)
continue; continue;
boolean isPaused = paused;
for (WatchEvent<?> event : key.pollEvents()) { for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind(); WatchEvent.Kind<?> kind = event.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) if (kind == StandardWatchEventKinds.OVERFLOW)
@ -254,8 +258,10 @@ public class BeatmapWatchService {
//System.out.printf("%s: %s\n", kind.name(), child); //System.out.printf("%s: %s\n", kind.name(), child);
// fire listeners // fire listeners
if (!isPaused) {
for (BeatmapWatchServiceListener listener : listeners) for (BeatmapWatchServiceListener listener : listeners)
listener.eventReceived(kind, child); listener.eventReceived(kind, child);
}
// if directory is created, then register it and its sub-directories // if directory is created, then register it and its sub-directories
if (kind == StandardWatchEventKinds.ENTRY_CREATE) { 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; }
} }