From 25356ce74d4aa99718d4a0a2830524ba360dd9d0 Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Thu, 2 Feb 2017 15:30:01 -0500 Subject: [PATCH] Reloading beatmaps now preserves non-parsed beatmap fields. Signed-off-by: Jeffrey Han --- src/itdelatrisu/opsu/beatmap/Beatmap.java | 15 ++++++- .../opsu/beatmap/BeatmapParser.java | 39 ++++++++++++++++--- src/itdelatrisu/opsu/states/SongMenu.java | 2 +- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/itdelatrisu/opsu/beatmap/Beatmap.java b/src/itdelatrisu/opsu/beatmap/Beatmap.java index b6b3156b..c8291823 100644 --- a/src/itdelatrisu/opsu/beatmap/Beatmap.java +++ b/src/itdelatrisu/opsu/beatmap/Beatmap.java @@ -548,4 +548,17 @@ public void incrementPlayCounter() { this.playCount++; this.lastPlayed = System.currentTimeMillis(); } -} \ No newline at end of file + + /** + * Copies non-parsed fields from this beatmap into another beatmap. + * @param target the target beatmap + */ + public void copyAdditionalFields(Beatmap target) { + target.starRating = starRating; + target.dateAdded = dateAdded; + target.favorite = favorite; + target.playCount = playCount; + target.lastPlayed = lastPlayed; + target.localMusicOffset = localMusicOffset; + } +} diff --git a/src/itdelatrisu/opsu/beatmap/BeatmapParser.java b/src/itdelatrisu/opsu/beatmap/BeatmapParser.java index 2eb33688..b96b91e3 100644 --- a/src/itdelatrisu/opsu/beatmap/BeatmapParser.java +++ b/src/itdelatrisu/opsu/beatmap/BeatmapParser.java @@ -80,8 +80,16 @@ private BeatmapParser() {} * adds the beatmaps to a new BeatmapSetList. * @param root the root directory (search has depth 1) */ - public static void parseAllFiles(File root) { - // create a new BeatmapSetList + public static void parseAllFiles(File root) { parseAllFiles(root, null); } + + /** + * Invokes parser for each OSU file in a root directory and + * adds the beatmaps to a new BeatmapSetList. + * @param root the root directory (search has depth 1) + * @param oldBeatmapList the old beatmap list to copy non-parsed fields from + */ + public static void parseAllFiles(File root, BeatmapSetList oldBeatmapList) { + // create a new beatmap list BeatmapSetList.create(); // create a new watch service @@ -89,7 +97,7 @@ public static void parseAllFiles(File root) { BeatmapWatchService.create(); // parse all directories - parseDirectories(root.listFiles()); + parseDirectories(root.listFiles(), oldBeatmapList); } /** @@ -99,6 +107,17 @@ public static void parseAllFiles(File root) { * @return the last BeatmapSetNode parsed, or null if none */ public static BeatmapSetNode parseDirectories(File[] dirs) { + return parseDirectories(dirs, null); + } + + /** + * Invokes parser for each directory in the given array and + * adds the beatmaps to the existing BeatmapSetList. + * @param dirs the array of directories to parse + * @param oldBeatmapList the old beatmap list to copy non-parsed fields from + * @return the last BeatmapSetNode parsed, or null if none + */ + public static BeatmapSetNode parseDirectories(File[] dirs, BeatmapSetList oldBeatmapList) { if (dirs == null) return null; @@ -173,9 +192,19 @@ public boolean accept(File dir, String name) { // add to parsed beatmap list if (beatmap != null) { - beatmap.dateAdded = timestamp; - if (beatmap.mode == Beatmap.MODE_OSU) // only support standard mode + // copy non-parsed fields + Beatmap oldBeatmap; + if (oldBeatmapList != null && (oldBeatmap = oldBeatmapList.getBeatmapFromHash(beatmap.md5Hash)) != null) + oldBeatmap.copyAdditionalFields(beatmap); + + // add timestamp + if (beatmap.dateAdded < 1) + beatmap.dateAdded = timestamp; + + // only support standard mode + if (beatmap.mode == Beatmap.MODE_OSU) beatmaps.add(beatmap); + parsedBeatmaps.add(beatmap); } } diff --git a/src/itdelatrisu/opsu/states/SongMenu.java b/src/itdelatrisu/opsu/states/SongMenu.java index 3b3efb6e..fae66823 100644 --- a/src/itdelatrisu/opsu/states/SongMenu.java +++ b/src/itdelatrisu/opsu/states/SongMenu.java @@ -256,7 +256,7 @@ private void reloadBeatmaps() { } // invoke parser - BeatmapParser.parseAllFiles(beatmapDir); + BeatmapParser.parseAllFiles(beatmapDir, BeatmapSetList.get()); } }