Skip to content

Commit

Permalink
Beatmap parser fixes.
Browse files Browse the repository at this point in the history
- Fixed hit object 'addition' field parsing. (Still not sure what the fields do, but the types should be correct now...)
- Fixed a careless error causing a potential null pointer exception. (blame: 0b33fed)
- Show an error if parseHitObjects() parses a different amount of objects than expected.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
  • Loading branch information
itdelatrisu committed Sep 2, 2015
1 parent fdd70a8 commit 6c956e9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
31 changes: 19 additions & 12 deletions src/itdelatrisu/opsu/beatmap/BeatmapParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,19 @@ public boolean accept(File dir, String name) {

// check if beatmap is cached
String path = String.format("%s/%s", dir.getName(), file.getName());
Long lastModified = map.get(path);
if (map != null && lastModified != null) {
// check last modified times
if (lastModified == file.lastModified()) {
// add to cached beatmap list
Beatmap beatmap = new Beatmap(file);
beatmaps.add(beatmap);
cachedBeatmaps.add(beatmap);
continue;
} else
BeatmapDB.delete(dir.getName(), file.getName());
if (map != null) {
Long lastModified = map.get(path);
if (lastModified != null) {
// check last modified times
if (lastModified == file.lastModified()) {
// add to cached beatmap list
Beatmap beatmap = new Beatmap(file);
beatmaps.add(beatmap);
cachedBeatmaps.add(beatmap);
continue;
} else
BeatmapDB.delete(dir.getName(), file.getName());
}
}

// Parse hit objects only when needed to save time/memory.
Expand Down Expand Up @@ -686,10 +688,15 @@ public static void parseHitObjects(Beatmap beatmap) {

beatmap.objects[objectIndex++] = hitObject;
} catch (Exception e) {
Log.warn(String.format("Failed to read hit object '%s' for Beatmap '%s'.",
Log.warn(String.format("Failed to read hit object '%s' for beatmap '%s'.",
line, beatmap.toString()), e);
}
}

// check that all objects were parsed
if (objectIndex != beatmap.objects.length)
ErrorHandler.error(String.format("Parsed %d objects for beatmap '%s', %d objects expected.",
objectIndex, beatmap.toString(), beatmap.objects.length), null, true);
} catch (IOException e) {
ErrorHandler.error(String.format("Failed to read file '%s'.", beatmap.getFile().getAbsolutePath()), e, false);
}
Expand Down
47 changes: 41 additions & 6 deletions src/itdelatrisu/opsu/beatmap/HitObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,18 @@ public class HitObject {
/** Hit sound type (SOUND_* bitmask). */
private byte hitSound;

/** Hit sound addition (sampleSet, AdditionSampleSet, ?, ...). */
/** Hit sound addition (sampleSet, AdditionSampleSet). */
private byte[] addition;

/** Addition custom sample index. */
private byte additionCustomSampleIndex;

/** Addition hit sound volume. */
private int additionHitSoundVolume;

/** Addition hit sound file. */
private String additionHitSound;

/** Slider curve type (SLIDER_* constant). */
private char sliderType;

Expand Down Expand Up @@ -250,9 +259,17 @@ else if ((type & HitObject.TYPE_SLIDER) > 0) {
// addition
if (tokens.length > additionIndex) {
String[] additionTokens = tokens[additionIndex].split(":");
this.addition = new byte[additionTokens.length];
for (int j = 0; j < additionTokens.length; j++)
this.addition[j] = Byte.parseByte(additionTokens[j]);
if (additionTokens.length > 1) {
this.addition = new byte[2];
addition[0] = Byte.parseByte(additionTokens[0]);
addition[1] = Byte.parseByte(additionTokens[1]);
}
if (additionTokens.length > 2)
this.additionCustomSampleIndex = Byte.parseByte(additionTokens[2]);
if (additionTokens.length > 3)
this.additionHitSoundVolume = Integer.parseInt(additionTokens[3]);
if (additionTokens.length > 4)
this.additionHitSound = additionTokens[4];
}
}

Expand Down Expand Up @@ -471,6 +488,21 @@ public byte getAdditionSampleSet(int index) {
return 0;
}

/**
* Returns the custom sample index (addition).
*/
public byte getCustomSampleIndex() { return additionCustomSampleIndex; }

/**
* Returns the hit sound volume (addition).
*/
public int getHitSoundVolume() { return additionHitSoundVolume; }

/**
* Returns the hit sound file (addition).
*/
public String getHitSoundFile() { return additionHitSound; }

/**
* Sets the hit object index in the current stack.
* @param stack index in the stack
Expand Down Expand Up @@ -529,9 +561,12 @@ else if (isSlider()) {
// addition
if (addition != null) {
for (int i = 0; i < addition.length; i++) {
sb.append(addition[i]);
sb.append(':');
sb.append(addition[i]); sb.append(':');
}
sb.append(additionCustomSampleIndex); sb.append(':');
sb.append(additionHitSoundVolume); sb.append(':');
if (additionHitSound != null)
sb.append(additionHitSound);
} else
sb.setLength(sb.length() - 1);

Expand Down

0 comments on commit 6c956e9

Please # to comment.