From ff68145ba3b13135aac15655ca2bd2ea18035bdd Mon Sep 17 00:00:00 2001 From: yugecin Date: Sun, 18 Dec 2016 23:39:39 +0100 Subject: [PATCH] added BeatmapParser#parseOnlyTimingPoints --- .../opsu/beatmap/BeatmapParser.java | 82 +++++++++++++++---- 1 file changed, 67 insertions(+), 15 deletions(-) diff --git a/src/itdelatrisu/opsu/beatmap/BeatmapParser.java b/src/itdelatrisu/opsu/beatmap/BeatmapParser.java index c7004703..59d20639 100644 --- a/src/itdelatrisu/opsu/beatmap/BeatmapParser.java +++ b/src/itdelatrisu/opsu/beatmap/BeatmapParser.java @@ -212,6 +212,52 @@ public class BeatmapParser { return lastNode; } + /** + * Parses the timingpoints for a beatmap. + * @param map the map to parse timingpoints for + */ + public static void parseOnlyTimingPoints(Beatmap map) { + if (map == null || map.getFile() == null || !map.getFile().exists()) { + return; + } + if (map.timingPoints == null) { + map.timingPoints = new ArrayList(); + } + try ( + InputStream bis = new BufferedInputStream(new FileInputStream(map.getFile())); + MD5InputStreamWrapper md5stream = (!hasNoMD5Algorithm) ? new MD5InputStreamWrapper(bis) : null; + BufferedReader in = new BufferedReader(new InputStreamReader((md5stream != null) ? md5stream : bis, "UTF-8")); + ) { + String line; + boolean found = false; + while((line = in.readLine()) != null) { + line = line.trim(); + if(!isValidLine(line)) { + continue; + } + if ("[TimingPoints]".equals(line)) { + found = true; + continue; + } + if (found) { + if (line.startsWith("[")) { + break; + } + parseSectionTimingPoints(map, line); + } + } + map.timingPoints.trimToSize(); + } catch (IOException e) { + ErrorHandler.error(String.format("Failed to read file '%s'.", map.getFile().getAbsolutePath()), e, false); + } catch (NoSuchAlgorithmException e) { + ErrorHandler.error("Failed to get MD5 hash stream.", e, true); + + // retry without MD5 + hasNoMD5Algorithm = true; + parseOnlyTimingPoints(map); + } + } + /** * Parses a beatmap. * @param file the file to parse @@ -493,21 +539,7 @@ public class BeatmapParser { break; try { - // parse timing point - TimingPoint timingPoint = new TimingPoint(line); - - // calculate BPM - if (!timingPoint.isInherited()) { - int bpm = Math.round(60000 / timingPoint.getBeatLength()); - if (beatmap.bpmMin == 0) - beatmap.bpmMin = beatmap.bpmMax = bpm; - else if (bpm < beatmap.bpmMin) - beatmap.bpmMin = bpm; - else if (bpm > beatmap.bpmMax) - beatmap.bpmMax = bpm; - } - - beatmap.timingPoints.add(timingPoint); + parseSectionTimingPoints(beatmap, line); } catch (Exception e) { Log.warn(String.format("Failed to read timing point '%s' for file '%s'.", line, file.getAbsolutePath()), e); @@ -624,6 +656,26 @@ public class BeatmapParser { return beatmap; } + /** + * Parses a timing point in the timingpoints section of a beatmap file + * @param beatmap the beatmap to add the timingpoint to + * @param line the line with timingpoint to parse + */ + private static void parseSectionTimingPoints(Beatmap beatmap, String line) { + TimingPoint timingPoint = new TimingPoint(line); + if(!timingPoint.isInherited()) { + int bpm = Math.round(60000 / timingPoint.getBeatLength()); + if( beatmap.bpmMin == 0 ) { + beatmap.bpmMin = beatmap.bpmMax = bpm; + } else if( bpm < beatmap.bpmMin ) { + beatmap.bpmMin = bpm; + } else if( bpm > beatmap.bpmMax ) { + beatmap.bpmMax = bpm; + } + } + beatmap.timingPoints.add(timingPoint); + } + /** * Parses all hit objects in a beatmap. * @param beatmap the beatmap to parse