2014-06-30 04:17:04 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
2015-01-16 18:05:44 +01:00
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
2014-06-30 04:17:04 +02:00
|
|
|
*
|
|
|
|
* opsu! is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* opsu! is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package itdelatrisu.opsu;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
2014-08-25 05:48:52 +02:00
|
|
|
import java.io.FileInputStream;
|
2014-06-30 04:17:04 +02:00
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.FilenameFilter;
|
|
|
|
import java.io.IOException;
|
2014-08-25 05:48:52 +02:00
|
|
|
import java.io.InputStreamReader;
|
2014-06-30 04:17:04 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
2015-01-15 03:48:24 +01:00
|
|
|
import java.util.HashMap;
|
2014-06-30 04:17:04 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
|
|
|
|
import org.newdawn.slick.Color;
|
|
|
|
import org.newdawn.slick.util.Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parser for OSU files.
|
|
|
|
*/
|
|
|
|
public class OsuParser {
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The current file being parsed. */
|
2014-07-06 03:00:52 +02:00
|
|
|
private static File currentFile;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The current directory number while parsing. */
|
2014-07-06 03:00:52 +02:00
|
|
|
private static int currentDirectoryIndex = -1;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The total number of directories to parse. */
|
2014-07-06 03:00:52 +02:00
|
|
|
private static int totalDirectories = -1;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The string lookup database. */
|
2015-01-15 03:48:24 +01:00
|
|
|
private static HashMap<String, String> stringdb = new HashMap<String, String>();
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
// This class should not be instantiated.
|
|
|
|
private OsuParser() {}
|
|
|
|
|
|
|
|
/**
|
2015-02-02 06:15:16 +01:00
|
|
|
* Invokes parser for each OSU file in a root directory and
|
|
|
|
* adds the OsuFiles to a new OsuGroupList.
|
2014-06-30 04:17:04 +02:00
|
|
|
* @param root the root directory (search has depth 1)
|
|
|
|
*/
|
2015-02-01 08:10:17 +01:00
|
|
|
public static void parseAllFiles(File root) {
|
2015-01-21 01:24:59 +01:00
|
|
|
// create a new OsuGroupList
|
|
|
|
OsuGroupList.create();
|
|
|
|
|
2015-02-02 06:15:16 +01:00
|
|
|
// parse all directories
|
|
|
|
parseDirectories(root.listFiles());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invokes parser for each directory in the given array and
|
|
|
|
* adds the OsuFiles to the existing OsuGroupList.
|
|
|
|
* @param dirs the array of directories to parse
|
|
|
|
* @return the last OsuGroupNode parsed
|
|
|
|
*/
|
|
|
|
public static OsuGroupNode parseDirectories(File[] dirs) {
|
2014-07-06 03:00:52 +02:00
|
|
|
// progress tracking
|
|
|
|
currentDirectoryIndex = 0;
|
2015-02-02 06:15:16 +01:00
|
|
|
totalDirectories = dirs.length;
|
2014-07-06 03:00:52 +02:00
|
|
|
|
2015-02-02 06:15:16 +01:00
|
|
|
// parse directories
|
|
|
|
OsuGroupNode lastNode = null;
|
|
|
|
for (File dir : dirs) {
|
2014-07-06 03:00:52 +02:00
|
|
|
currentDirectoryIndex++;
|
2015-02-02 06:15:16 +01:00
|
|
|
if (!dir.isDirectory())
|
2014-06-30 04:17:04 +02:00
|
|
|
continue;
|
2015-02-02 06:15:16 +01:00
|
|
|
|
|
|
|
// find all OSU files
|
|
|
|
File[] files = dir.listFiles(new FilenameFilter() {
|
2014-06-30 04:17:04 +02:00
|
|
|
@Override
|
|
|
|
public boolean accept(File dir, String name) {
|
|
|
|
return name.toLowerCase().endsWith(".osu");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (files.length < 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// create a new group entry
|
|
|
|
ArrayList<OsuFile> osuFiles = new ArrayList<OsuFile>();
|
|
|
|
for (File file : files) {
|
2014-07-06 03:00:52 +02:00
|
|
|
currentFile = file;
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
// Parse hit objects only when needed to save time/memory.
|
|
|
|
// Change boolean to 'true' to parse them immediately.
|
|
|
|
parseFile(file, osuFiles, false);
|
|
|
|
}
|
|
|
|
if (!osuFiles.isEmpty()) { // add entry if non-empty
|
2015-01-15 03:48:24 +01:00
|
|
|
osuFiles.trimToSize();
|
2014-06-30 04:17:04 +02:00
|
|
|
Collections.sort(osuFiles);
|
2015-02-02 06:15:16 +01:00
|
|
|
lastNode = OsuGroupList.get().addSongGroup(osuFiles);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
2015-01-18 21:26:00 +01:00
|
|
|
|
2015-02-02 06:15:16 +01:00
|
|
|
// stop parsing files (interrupted)
|
2015-01-18 21:26:00 +01:00
|
|
|
if (Thread.interrupted())
|
|
|
|
break;
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
2014-07-06 03:00:52 +02:00
|
|
|
|
2015-01-15 03:48:24 +01:00
|
|
|
// clear string DB
|
|
|
|
stringdb = new HashMap<String, String>();
|
|
|
|
|
2014-07-06 03:00:52 +02:00
|
|
|
currentFile = null;
|
|
|
|
currentDirectoryIndex = -1;
|
|
|
|
totalDirectories = -1;
|
2015-02-02 06:15:16 +01:00
|
|
|
return lastNode;
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses an OSU file.
|
|
|
|
* @param file the file to parse
|
|
|
|
* @param osuFiles the song group
|
|
|
|
* @param parseObjects if true, hit objects will be fully parsed now
|
|
|
|
* @return the new OsuFile object
|
|
|
|
*/
|
2014-12-21 00:17:04 +01:00
|
|
|
public static OsuFile parseFile(File file, ArrayList<OsuFile> osuFiles, boolean parseObjects) {
|
2014-06-30 04:17:04 +02:00
|
|
|
OsuFile osu = new OsuFile(file);
|
|
|
|
|
2014-08-25 05:48:52 +02:00
|
|
|
try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
// initialize timing point list
|
|
|
|
osu.timingPoints = new ArrayList<OsuTimingPoint>();
|
|
|
|
|
|
|
|
String line = in.readLine();
|
2014-07-18 03:16:15 +02:00
|
|
|
String tokens[] = null;
|
2014-06-30 04:17:04 +02:00
|
|
|
while (line != null) {
|
|
|
|
line = line.trim();
|
|
|
|
if (!isValidLine(line)) {
|
|
|
|
line = in.readLine();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (line) {
|
|
|
|
case "[General]":
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
line = line.trim();
|
|
|
|
if (!isValidLine(line))
|
|
|
|
continue;
|
|
|
|
if (line.charAt(0) == '[')
|
|
|
|
break;
|
2014-07-02 16:14:15 +02:00
|
|
|
if ((tokens = tokenize(line)) == null)
|
|
|
|
continue;
|
2014-07-24 19:38:59 +02:00
|
|
|
try {
|
|
|
|
switch (tokens[0]) {
|
|
|
|
case "AudioFilename":
|
2015-01-21 04:19:14 +01:00
|
|
|
File audioFileName = new File(file.getParent(), tokens[1]);
|
|
|
|
if (!osuFiles.isEmpty()) {
|
|
|
|
// if possible, reuse the same File object from another OsuFile in the group
|
|
|
|
File groupAudioFileName = osuFiles.get(0).audioFilename;
|
|
|
|
if (audioFileName.equals(groupAudioFileName))
|
|
|
|
audioFileName = groupAudioFileName;
|
|
|
|
}
|
|
|
|
osu.audioFilename = audioFileName;
|
2014-07-24 19:38:59 +02:00
|
|
|
break;
|
|
|
|
case "AudioLeadIn":
|
|
|
|
osu.audioLeadIn = Integer.parseInt(tokens[1]);
|
|
|
|
break;
|
|
|
|
// case "AudioHash": // deprecated
|
|
|
|
// osu.audioHash = tokens[1];
|
|
|
|
// break;
|
|
|
|
case "PreviewTime":
|
|
|
|
osu.previewTime = Integer.parseInt(tokens[1]);
|
|
|
|
break;
|
|
|
|
case "Countdown":
|
|
|
|
osu.countdown = Byte.parseByte(tokens[1]);
|
|
|
|
break;
|
|
|
|
case "SampleSet":
|
2015-01-15 03:48:24 +01:00
|
|
|
osu.sampleSet = getDBString(tokens[1]);
|
2014-07-24 19:38:59 +02:00
|
|
|
break;
|
|
|
|
case "StackLeniency":
|
|
|
|
osu.stackLeniency = Float.parseFloat(tokens[1]);
|
|
|
|
break;
|
|
|
|
case "Mode":
|
|
|
|
osu.mode = Byte.parseByte(tokens[1]);
|
|
|
|
|
|
|
|
/* Non-Opsu! standard files not implemented (obviously). */
|
|
|
|
if (osu.mode != 0)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case "LetterboxInBreaks":
|
|
|
|
osu.letterboxInBreaks = (Integer.parseInt(tokens[1]) == 1);
|
|
|
|
break;
|
|
|
|
case "WidescreenStoryboard":
|
|
|
|
osu.widescreenStoryboard = (Integer.parseInt(tokens[1]) == 1);
|
|
|
|
break;
|
|
|
|
case "EpilepsyWarning":
|
|
|
|
osu.epilepsyWarning = (Integer.parseInt(tokens[1]) == 1);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.warn(String.format("Failed to read line '%s' for file '%s'.",
|
|
|
|
line, file.getAbsolutePath()), e);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "[Editor]":
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
line = line.trim();
|
|
|
|
if (!isValidLine(line))
|
|
|
|
continue;
|
|
|
|
if (line.charAt(0) == '[')
|
|
|
|
break;
|
|
|
|
/* Not implemented. */
|
2014-07-02 16:14:15 +02:00
|
|
|
// if ((tokens = tokenize(line)) == null)
|
|
|
|
// continue;
|
2014-07-24 19:38:59 +02:00
|
|
|
// try {
|
|
|
|
// switch (tokens[0]) {
|
|
|
|
// case "Bookmarks":
|
|
|
|
// String[] bookmarks = tokens[1].split(",");
|
|
|
|
// osu.bookmarks = new int[bookmarks.length];
|
|
|
|
// for (int i = 0; i < bookmarks.length; i++)
|
|
|
|
// osu.bookmarks[i] = Integer.parseInt(bookmarks[i]);
|
|
|
|
// break;
|
|
|
|
// case "DistanceSpacing":
|
|
|
|
// osu.distanceSpacing = Float.parseFloat(tokens[1]);
|
|
|
|
// break;
|
|
|
|
// case "BeatDivisor":
|
|
|
|
// osu.beatDivisor = Byte.parseByte(tokens[1]);
|
|
|
|
// break;
|
|
|
|
// case "GridSize":
|
|
|
|
// osu.gridSize = Integer.parseInt(tokens[1]);
|
|
|
|
// break;
|
|
|
|
// case "TimelineZoom":
|
|
|
|
// osu.timelineZoom = Integer.parseInt(tokens[1]);
|
|
|
|
// break;
|
|
|
|
// default:
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// } catch (Exception e) {
|
|
|
|
// Log.warn(String.format("Failed to read editor line '%s' for file '%s'.",
|
|
|
|
// line, file.getAbsolutePath()), e);
|
2014-06-30 04:17:04 +02:00
|
|
|
// }
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "[Metadata]":
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
line = line.trim();
|
|
|
|
if (!isValidLine(line))
|
|
|
|
continue;
|
|
|
|
if (line.charAt(0) == '[')
|
|
|
|
break;
|
2014-07-02 16:14:15 +02:00
|
|
|
if ((tokens = tokenize(line)) == null)
|
|
|
|
continue;
|
2014-07-24 19:38:59 +02:00
|
|
|
try {
|
|
|
|
switch (tokens[0]) {
|
|
|
|
case "Title":
|
2015-01-15 03:48:24 +01:00
|
|
|
osu.title = getDBString(tokens[1]);
|
2014-07-24 19:38:59 +02:00
|
|
|
break;
|
|
|
|
case "TitleUnicode":
|
2015-01-15 03:48:24 +01:00
|
|
|
osu.titleUnicode = getDBString(tokens[1]);
|
2014-07-24 19:38:59 +02:00
|
|
|
break;
|
|
|
|
case "Artist":
|
2015-01-15 03:48:24 +01:00
|
|
|
osu.artist = getDBString(tokens[1]);
|
2014-07-24 19:38:59 +02:00
|
|
|
break;
|
|
|
|
case "ArtistUnicode":
|
2015-01-15 03:48:24 +01:00
|
|
|
osu.artistUnicode = getDBString(tokens[1]);
|
2014-07-24 19:38:59 +02:00
|
|
|
break;
|
|
|
|
case "Creator":
|
2015-01-15 03:48:24 +01:00
|
|
|
osu.creator = getDBString(tokens[1]);
|
2014-07-24 19:38:59 +02:00
|
|
|
break;
|
|
|
|
case "Version":
|
2015-01-15 03:48:24 +01:00
|
|
|
osu.version = getDBString(tokens[1]);
|
2014-07-24 19:38:59 +02:00
|
|
|
break;
|
|
|
|
case "Source":
|
2015-01-15 03:48:24 +01:00
|
|
|
osu.source = getDBString(tokens[1]);
|
2014-07-24 19:38:59 +02:00
|
|
|
break;
|
|
|
|
case "Tags":
|
2015-01-15 03:48:24 +01:00
|
|
|
osu.tags = getDBString(tokens[1].toLowerCase());
|
2014-07-24 19:38:59 +02:00
|
|
|
break;
|
|
|
|
case "BeatmapID":
|
|
|
|
osu.beatmapID = Integer.parseInt(tokens[1]);
|
|
|
|
break;
|
|
|
|
case "BeatmapSetID":
|
|
|
|
osu.beatmapSetID = Integer.parseInt(tokens[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.warn(String.format("Failed to read metadata '%s' for file '%s'.",
|
|
|
|
line, file.getAbsolutePath()), e);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "[Difficulty]":
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
line = line.trim();
|
|
|
|
if (!isValidLine(line))
|
|
|
|
continue;
|
|
|
|
if (line.charAt(0) == '[')
|
|
|
|
break;
|
2014-07-02 16:14:15 +02:00
|
|
|
if ((tokens = tokenize(line)) == null)
|
|
|
|
continue;
|
2014-07-24 19:38:59 +02:00
|
|
|
try {
|
|
|
|
switch (tokens[0]) {
|
|
|
|
case "HPDrainRate":
|
|
|
|
osu.HPDrainRate = Float.parseFloat(tokens[1]);
|
|
|
|
break;
|
|
|
|
case "CircleSize":
|
|
|
|
osu.circleSize = Float.parseFloat(tokens[1]);
|
|
|
|
break;
|
|
|
|
case "OverallDifficulty":
|
|
|
|
osu.overallDifficulty = Float.parseFloat(tokens[1]);
|
|
|
|
break;
|
|
|
|
case "ApproachRate":
|
|
|
|
osu.approachRate = Float.parseFloat(tokens[1]);
|
|
|
|
break;
|
|
|
|
case "SliderMultiplier":
|
|
|
|
osu.sliderMultiplier = Float.parseFloat(tokens[1]);
|
|
|
|
break;
|
|
|
|
case "SliderTickRate":
|
|
|
|
osu.sliderTickRate = Float.parseFloat(tokens[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.warn(String.format("Failed to read difficulty '%s' for file '%s'.",
|
|
|
|
line, file.getAbsolutePath()), e);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-30 10:03:39 +02:00
|
|
|
if (osu.approachRate == -1f) // not in old format
|
2014-06-30 04:17:04 +02:00
|
|
|
osu.approachRate = osu.overallDifficulty;
|
|
|
|
break;
|
|
|
|
case "[Events]":
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
line = line.trim();
|
|
|
|
if (!isValidLine(line))
|
|
|
|
continue;
|
|
|
|
if (line.charAt(0) == '[')
|
|
|
|
break;
|
|
|
|
tokens = line.split(",");
|
|
|
|
switch (tokens[0]) {
|
|
|
|
case "0": // background
|
|
|
|
tokens[2] = tokens[2].replaceAll("^\"|\"$", "");
|
|
|
|
String ext = OsuParser.getExtension(tokens[2]);
|
|
|
|
if (ext.equals("jpg") || ext.equals("png"))
|
2015-01-15 03:48:24 +01:00
|
|
|
osu.bg = getDBString(file.getParent() + File.separator + tokens[2]);
|
2014-06-30 04:17:04 +02:00
|
|
|
break;
|
|
|
|
case "2": // break periods
|
2014-07-24 19:38:59 +02:00
|
|
|
try {
|
|
|
|
if (osu.breaks == null) // optional, create if needed
|
|
|
|
osu.breaks = new ArrayList<Integer>();
|
|
|
|
osu.breaks.add(Integer.parseInt(tokens[1]));
|
|
|
|
osu.breaks.add(Integer.parseInt(tokens[2]));
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.warn(String.format("Failed to read break period '%s' for file '%s'.",
|
|
|
|
line, file.getAbsolutePath()), e);
|
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Not implemented. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 03:48:24 +01:00
|
|
|
if (osu.breaks != null)
|
|
|
|
osu.breaks.trimToSize();
|
2014-06-30 04:17:04 +02:00
|
|
|
break;
|
|
|
|
case "[TimingPoints]":
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
line = line.trim();
|
|
|
|
if (!isValidLine(line))
|
|
|
|
continue;
|
|
|
|
if (line.charAt(0) == '[')
|
|
|
|
break;
|
|
|
|
|
2014-07-24 19:38:59 +02:00
|
|
|
try {
|
|
|
|
// parse timing point
|
|
|
|
OsuTimingPoint timingPoint = new OsuTimingPoint(line);
|
|
|
|
|
|
|
|
// calculate BPM
|
|
|
|
if (!timingPoint.isInherited()) {
|
|
|
|
int bpm = Math.round(60000 / timingPoint.getBeatLength());
|
|
|
|
if (osu.bpmMin == 0)
|
|
|
|
osu.bpmMin = osu.bpmMax = bpm;
|
|
|
|
else if (bpm < osu.bpmMin)
|
|
|
|
osu.bpmMin = bpm;
|
|
|
|
else if (bpm > osu.bpmMax)
|
|
|
|
osu.bpmMax = bpm;
|
|
|
|
}
|
|
|
|
|
|
|
|
osu.timingPoints.add(timingPoint);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.warn(String.format("Failed to read timing point '%s' for file '%s'.",
|
|
|
|
line, file.getAbsolutePath()), e);
|
2014-07-09 19:36:42 +02:00
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
2015-01-15 03:48:24 +01:00
|
|
|
osu.timingPoints.trimToSize();
|
2014-06-30 04:17:04 +02:00
|
|
|
break;
|
|
|
|
case "[Colours]":
|
|
|
|
LinkedList<Color> colors = new LinkedList<Color>();
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
line = line.trim();
|
|
|
|
if (!isValidLine(line))
|
|
|
|
continue;
|
|
|
|
if (line.charAt(0) == '[')
|
|
|
|
break;
|
2014-07-02 16:14:15 +02:00
|
|
|
if ((tokens = tokenize(line)) == null)
|
|
|
|
continue;
|
2014-07-24 19:38:59 +02:00
|
|
|
try {
|
|
|
|
switch (tokens[0]) {
|
|
|
|
case "Combo1":
|
|
|
|
case "Combo2":
|
|
|
|
case "Combo3":
|
|
|
|
case "Combo4":
|
|
|
|
case "Combo5":
|
|
|
|
case "Combo6":
|
|
|
|
case "Combo7":
|
|
|
|
case "Combo8":
|
|
|
|
String[] rgb = tokens[1].split(",");
|
|
|
|
colors.add(new Color(
|
|
|
|
Integer.parseInt(rgb[0]),
|
|
|
|
Integer.parseInt(rgb[1]),
|
|
|
|
Integer.parseInt(rgb[2])
|
|
|
|
));
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.warn(String.format("Failed to read color '%s' for file '%s'.",
|
|
|
|
line, file.getAbsolutePath()), e);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!colors.isEmpty())
|
|
|
|
osu.combo = colors.toArray(new Color[colors.size()]);
|
|
|
|
break;
|
|
|
|
case "[HitObjects]":
|
2014-07-19 09:31:54 +02:00
|
|
|
int type = 0;
|
2014-06-30 04:17:04 +02:00
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
line = line.trim();
|
|
|
|
if (!isValidLine(line))
|
|
|
|
continue;
|
|
|
|
if (line.charAt(0) == '[')
|
|
|
|
break;
|
|
|
|
/* Only type counts parsed at this time. */
|
|
|
|
tokens = line.split(",");
|
2014-07-24 19:38:59 +02:00
|
|
|
try {
|
|
|
|
type = Integer.parseInt(tokens[3]);
|
|
|
|
if ((type & OsuHitObject.TYPE_CIRCLE) > 0)
|
|
|
|
osu.hitObjectCircle++;
|
|
|
|
else if ((type & OsuHitObject.TYPE_SLIDER) > 0)
|
|
|
|
osu.hitObjectSlider++;
|
|
|
|
else //if ((type & OsuHitObject.TYPE_SPINNER) > 0)
|
|
|
|
osu.hitObjectSpinner++;
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.warn(String.format("Failed to read hit object '%s' for file '%s'.",
|
|
|
|
line, file.getAbsolutePath()), e);
|
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
2014-07-18 03:16:15 +02:00
|
|
|
|
2014-07-24 19:38:59 +02:00
|
|
|
try {
|
|
|
|
// map length = last object end time (TODO: end on slider?)
|
|
|
|
if ((type & OsuHitObject.TYPE_SPINNER) > 0) {
|
|
|
|
// some 'endTime' fields contain a ':' character (?)
|
|
|
|
int index = tokens[5].indexOf(':');
|
|
|
|
if (index != -1)
|
|
|
|
tokens[5] = tokens[5].substring(0, index);
|
|
|
|
osu.endTime = Integer.parseInt(tokens[5]);
|
|
|
|
} else if (type != 0)
|
|
|
|
osu.endTime = Integer.parseInt(tokens[2]);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.warn(String.format("Failed to read hit object end time '%s' for file '%s'.",
|
|
|
|
line, file.getAbsolutePath()), e);
|
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
break;
|
|
|
|
default:
|
2014-07-04 00:50:44 +02:00
|
|
|
line = in.readLine();
|
2014-06-30 04:17:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2015-01-16 03:55:26 +01:00
|
|
|
ErrorHandler.error(String.format("Failed to read file '%s'.", file.getAbsolutePath()), e, false);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if no custom colors, use the default color scheme
|
|
|
|
if (osu.combo == null)
|
2014-07-02 01:32:03 +02:00
|
|
|
osu.combo = Utils.DEFAULT_COMBO;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
// parse hit objects now?
|
|
|
|
if (parseObjects)
|
|
|
|
parseHitObjects(osu);
|
|
|
|
|
|
|
|
// add OsuFile to song group
|
2014-12-21 00:17:04 +01:00
|
|
|
if (osuFiles != null)
|
|
|
|
osuFiles.add(osu);
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
return osu;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses all hit objects in an OSU file.
|
2014-07-18 21:11:57 +02:00
|
|
|
* @param osu the OsuFile to parse
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
|
|
|
public static void parseHitObjects(OsuFile osu) {
|
|
|
|
if (osu.objects != null) // already parsed
|
|
|
|
return;
|
|
|
|
|
|
|
|
osu.objects = new OsuHitObject[(osu.hitObjectCircle
|
|
|
|
+ osu.hitObjectSlider + osu.hitObjectSpinner)];
|
|
|
|
|
|
|
|
try (BufferedReader in = new BufferedReader(new FileReader(osu.getFile()))) {
|
|
|
|
String line = in.readLine();
|
|
|
|
while (line != null) {
|
|
|
|
line = line.trim();
|
2014-07-18 21:11:57 +02:00
|
|
|
if (!line.equals("[HitObjects]"))
|
2014-06-30 04:17:04 +02:00
|
|
|
line = in.readLine();
|
2014-07-18 21:11:57 +02:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (line == null) {
|
|
|
|
Log.warn(String.format("No hit objects found in OsuFile '%s'.", osu.toString()));
|
|
|
|
return;
|
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2014-07-18 21:11:57 +02:00
|
|
|
// combo info
|
|
|
|
int comboIndex = 0; // color index
|
|
|
|
int comboNumber = 1; // combo number
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2014-07-18 21:11:57 +02:00
|
|
|
int objectIndex = 0;
|
|
|
|
while ((line = in.readLine()) != null && objectIndex < osu.objects.length) {
|
|
|
|
line = line.trim();
|
|
|
|
if (!isValidLine(line))
|
|
|
|
continue;
|
|
|
|
if (line.charAt(0) == '[')
|
|
|
|
break;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2014-07-18 21:11:57 +02:00
|
|
|
// lines must have at minimum 5 parameters
|
|
|
|
int tokenCount = line.length() - line.replace(",", "").length();
|
|
|
|
if (tokenCount < 4)
|
|
|
|
continue;
|
|
|
|
|
2014-07-24 19:38:59 +02:00
|
|
|
try {
|
|
|
|
// create a new OsuHitObject for each line
|
|
|
|
OsuHitObject hitObject = new OsuHitObject(line);
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2014-07-24 19:38:59 +02:00
|
|
|
// set combo info
|
|
|
|
// - new combo: get next combo index, reset combo number
|
|
|
|
// - else: maintain combo index, increase combo number
|
2015-01-30 02:36:23 +01:00
|
|
|
if ((hitObject.isNewCombo() && !hitObject.isSpinner()) || objectIndex == 0) {
|
2014-07-24 19:38:59 +02:00
|
|
|
comboIndex = (comboIndex + 1) % osu.combo.length;
|
|
|
|
comboNumber = 1;
|
|
|
|
}
|
|
|
|
hitObject.setComboIndex(comboIndex);
|
|
|
|
hitObject.setComboNumber(comboNumber++);
|
2014-07-18 21:11:57 +02:00
|
|
|
|
2014-07-24 19:38:59 +02:00
|
|
|
osu.objects[objectIndex++] = hitObject;
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.warn(String.format("Failed to read hit object '%s' for OsuFile '%s'.",
|
|
|
|
line, osu.toString()), e);
|
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2015-01-16 03:55:26 +01:00
|
|
|
ErrorHandler.error(String.format("Failed to read file '%s'.", osu.getFile().getAbsolutePath()), e, false);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns false if the line is too short or commented.
|
|
|
|
*/
|
|
|
|
private static boolean isValidLine(String line) {
|
|
|
|
return (line.length() > 1 && !line.startsWith("//"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Splits line into two strings: tag, value.
|
2014-07-02 16:14:15 +02:00
|
|
|
* If no ':' character is present, null will be returned.
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
|
|
|
private static String[] tokenize(String line) {
|
2014-07-02 16:14:15 +02:00
|
|
|
int index = line.indexOf(':');
|
|
|
|
if (index == -1) {
|
|
|
|
Log.debug(String.format("Failed to tokenize line: '%s'.", line));
|
|
|
|
return null;
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
2014-07-02 16:14:15 +02:00
|
|
|
|
|
|
|
String[] tokens = new String[2];
|
|
|
|
tokens[0] = line.substring(0, index).trim();
|
|
|
|
tokens[1] = line.substring(index + 1).trim();
|
2014-06-30 04:17:04 +02:00
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the file extension of a file.
|
|
|
|
*/
|
|
|
|
public static String getExtension(String file) {
|
|
|
|
int i = file.lastIndexOf('.');
|
2014-07-02 16:14:15 +02:00
|
|
|
return (i != -1) ? file.substring(i + 1).toLowerCase() : "";
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
2014-07-06 03:00:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the current file being parsed, or null if none.
|
|
|
|
*/
|
|
|
|
public static String getCurrentFileName() {
|
|
|
|
return (currentFile != null) ? currentFile.getName() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the progress of file parsing, or -1 if not parsing.
|
|
|
|
* @return the completion percent [0, 100] or -1
|
|
|
|
*/
|
|
|
|
public static int getParserProgress() {
|
|
|
|
if (currentDirectoryIndex == -1 || totalDirectories == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return currentDirectoryIndex * 100 / totalDirectories;
|
|
|
|
}
|
2015-01-15 03:48:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the String object in the database for the given String.
|
|
|
|
* If none, insert the String into the database and return the original String.
|
|
|
|
* @param s the string to retrieve
|
|
|
|
* @return the string object
|
|
|
|
*/
|
|
|
|
private static String getDBString(String s) {
|
|
|
|
String DBString = stringdb.get(s);
|
|
|
|
if (DBString == null) {
|
|
|
|
stringdb.put(s, s);
|
|
|
|
return s;
|
|
|
|
} else
|
|
|
|
return DBString;
|
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|