Added extra checks in the parser for invalid line.

Credits to xasuma for the report.
This commit is contained in:
Jeffrey Han 2014-07-02 10:14:15 -04:00
parent 56123363b3
commit 812b83af45
2 changed files with 23 additions and 14 deletions

View File

@ -120,7 +120,8 @@ public class OsuParser {
continue; continue;
if (line.charAt(0) == '[') if (line.charAt(0) == '[')
break; break;
tokens = tokenize(line); if ((tokens = tokenize(line)) == null)
continue;
switch (tokens[0]) { switch (tokens[0]) {
case "AudioFilename": case "AudioFilename":
osu.audioFilename = new File(file.getParent() + File.separator + tokens[1]); osu.audioFilename = new File(file.getParent() + File.separator + tokens[1]);
@ -172,7 +173,8 @@ public class OsuParser {
if (line.charAt(0) == '[') if (line.charAt(0) == '[')
break; break;
/* Not implemented. */ /* Not implemented. */
// tokens = tokenize(line); // if ((tokens = tokenize(line)) == null)
// continue;
// switch (tokens[0]) { // switch (tokens[0]) {
// case "Bookmarks": // case "Bookmarks":
// String[] bookmarks = tokens[1].split(","); // String[] bookmarks = tokens[1].split(",");
@ -204,7 +206,8 @@ public class OsuParser {
continue; continue;
if (line.charAt(0) == '[') if (line.charAt(0) == '[')
break; break;
tokens = tokenize(line); if ((tokens = tokenize(line)) == null)
continue;
switch (tokens[0]) { switch (tokens[0]) {
case "Title": case "Title":
osu.title = tokens[1]; osu.title = tokens[1];
@ -246,7 +249,8 @@ public class OsuParser {
continue; continue;
if (line.charAt(0) == '[') if (line.charAt(0) == '[')
break; break;
tokens = tokenize(line); if ((tokens = tokenize(line)) == null)
continue;
switch (tokens[0]) { switch (tokens[0]) {
case "HPDrainRate": case "HPDrainRate":
osu.HPDrainRate = Float.parseFloat(tokens[1]); osu.HPDrainRate = Float.parseFloat(tokens[1]);
@ -346,7 +350,8 @@ public class OsuParser {
continue; continue;
if (line.charAt(0) == '[') if (line.charAt(0) == '[')
break; break;
tokens = tokenize(line); if ((tokens = tokenize(line)) == null)
continue;
switch (tokens[0]) { switch (tokens[0]) {
case "Combo1": case "Combo1":
case "Combo2": case "Combo2":
@ -464,6 +469,8 @@ public class OsuParser {
break; break;
// create a new OsuHitObject for each line // create a new OsuHitObject for each line
tokens = line.split(","); tokens = line.split(",");
if (tokens.length < 5)
continue;
float scaledX = Integer.parseInt(tokens[0]) * xMultiplier + xOffset; float scaledX = Integer.parseInt(tokens[0]) * xMultiplier + xOffset;
float scaledY = Integer.parseInt(tokens[1]) * yMultiplier + yOffset; float scaledY = Integer.parseInt(tokens[1]) * yMultiplier + yOffset;
int type = Integer.parseInt(tokens[3]); int type = Integer.parseInt(tokens[3]);
@ -525,16 +532,18 @@ public class OsuParser {
/** /**
* Splits line into two strings: tag, value. * Splits line into two strings: tag, value.
* If no ':' character is present, null will be returned.
*/ */
private static String[] tokenize(String line) { private static String[] tokenize(String line) {
String[] tokens = new String[2]; int index = line.indexOf(':');
try { if (index == -1) {
int index = line.indexOf(':'); Log.debug(String.format("Failed to tokenize line: '%s'.", line));
tokens[0] = line.substring(0, index).trim(); return null;
tokens[1] = line.substring(index + 1).trim();
} catch (java.lang.StringIndexOutOfBoundsException e) {
Log.error(String.format("Failed to tokenize line: '%s'.", line), e);
} }
String[] tokens = new String[2];
tokens[0] = line.substring(0, index).trim();
tokens[1] = line.substring(index + 1).trim();
return tokens; return tokens;
} }
@ -543,6 +552,6 @@ public class OsuParser {
*/ */
public static String getExtension(String file) { public static String getExtension(String file) {
int i = file.lastIndexOf('.'); int i = file.lastIndexOf('.');
return (i > -1) ? file.substring(i + 1).toLowerCase() : ""; return (i != -1) ? file.substring(i + 1).toLowerCase() : "";
} }
} }

View File

@ -218,7 +218,7 @@ public class MainMenu extends BasicGameState {
Utils.drawFPS(); Utils.drawFPS();
Utils.drawCursor(); Utils.drawCursor();
} }
@Override @Override
public void update(GameContainer container, StateBasedGame game, int delta) public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException { throws SlickException {