Save replays in a new thread.
Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
parent
712cf30e01
commit
c53679fe63
|
@ -230,84 +230,89 @@ public class Replay {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write file
|
// write file in new thread
|
||||||
File file = new File(dir, String.format("%s.osr", getReplayFilename()));
|
final File file = new File(dir, String.format("%s.osr", getReplayFilename()));
|
||||||
try (FileOutputStream out = new FileOutputStream(file)) {
|
new Thread() {
|
||||||
OsuWriter writer = new OsuWriter(out);
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try (FileOutputStream out = new FileOutputStream(file)) {
|
||||||
|
OsuWriter writer = new OsuWriter(out);
|
||||||
|
|
||||||
// header
|
// header
|
||||||
writer.write(mode);
|
writer.write(mode);
|
||||||
writer.write(version);
|
writer.write(version);
|
||||||
writer.write(beatmapHash);
|
writer.write(beatmapHash);
|
||||||
writer.write(playerName);
|
writer.write(playerName);
|
||||||
writer.write(replayHash);
|
writer.write(replayHash);
|
||||||
writer.write(hit300);
|
writer.write(hit300);
|
||||||
writer.write(hit100);
|
writer.write(hit100);
|
||||||
writer.write(hit50);
|
writer.write(hit50);
|
||||||
writer.write(geki);
|
writer.write(geki);
|
||||||
writer.write(katu);
|
writer.write(katu);
|
||||||
writer.write(miss);
|
writer.write(miss);
|
||||||
writer.write(score);
|
writer.write(score);
|
||||||
writer.write(combo);
|
writer.write(combo);
|
||||||
writer.write(perfect);
|
writer.write(perfect);
|
||||||
writer.write(mods);
|
writer.write(mods);
|
||||||
|
|
||||||
// life data
|
// life data
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
if (lifeFrames != null) {
|
if (lifeFrames != null) {
|
||||||
NumberFormat nf = new DecimalFormat("##.##");
|
NumberFormat nf = new DecimalFormat("##.##");
|
||||||
for (int i = 0; i < lifeFrames.length; i++) {
|
for (int i = 0; i < lifeFrames.length; i++) {
|
||||||
LifeFrame frame = lifeFrames[i];
|
LifeFrame frame = lifeFrames[i];
|
||||||
sb.append(String.format("%d|%s,",
|
sb.append(String.format("%d|%s,",
|
||||||
frame.getTime(), nf.format(frame.getPercentage())));
|
frame.getTime(), nf.format(frame.getPercentage())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writer.write(sb.toString());
|
||||||
|
|
||||||
|
// timestamp
|
||||||
|
writer.write(timestamp);
|
||||||
|
|
||||||
|
// LZMA-encoded replay data
|
||||||
|
if (frames != null && frames.length > 0) {
|
||||||
|
// build full frame string
|
||||||
|
NumberFormat nf = new DecimalFormat("###.#####");
|
||||||
|
sb = new StringBuilder();
|
||||||
|
for (int i = 0; i < frames.length; i++) {
|
||||||
|
ReplayFrame frame = frames[i];
|
||||||
|
sb.append(String.format("%d|%s|%s|%d,",
|
||||||
|
frame.getTimeDiff(), nf.format(frame.getRawX()),
|
||||||
|
nf.format(frame.getRawY()), frame.getKeys()));
|
||||||
|
}
|
||||||
|
sb.append(String.format("%s|0|0|%d", SEED_STRING, seed));
|
||||||
|
|
||||||
|
// get bytes from string
|
||||||
|
CharsetEncoder encoder = StandardCharsets.US_ASCII.newEncoder();
|
||||||
|
CharBuffer buffer = CharBuffer.wrap(sb);
|
||||||
|
byte[] bytes = encoder.encode(buffer).array();
|
||||||
|
|
||||||
|
// compress data
|
||||||
|
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||||
|
LzmaOutputStream compressedOut = new LzmaOutputStream.Builder(bout).useMediumDictionarySize().build();
|
||||||
|
try {
|
||||||
|
compressedOut.write(bytes);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// possible OOM: https://github.com/jponge/lzma-java/issues/9
|
||||||
|
ErrorHandler.error("LZMA compression failed (possible out-of-memory error).", e, true);
|
||||||
|
}
|
||||||
|
compressedOut.close();
|
||||||
|
bout.close();
|
||||||
|
|
||||||
|
// write to file
|
||||||
|
byte[] compressed = bout.toByteArray();
|
||||||
|
writer.write(compressed.length);
|
||||||
|
writer.write(compressed);
|
||||||
|
} else
|
||||||
|
writer.write(0);
|
||||||
|
|
||||||
|
writer.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
ErrorHandler.error("Could not save replay data.", e, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.write(sb.toString());
|
}.start();
|
||||||
|
|
||||||
// timestamp
|
|
||||||
writer.write(timestamp);
|
|
||||||
|
|
||||||
// LZMA-encoded replay data
|
|
||||||
if (frames != null && frames.length > 0) {
|
|
||||||
// build full frame string
|
|
||||||
NumberFormat nf = new DecimalFormat("###.#####");
|
|
||||||
sb = new StringBuilder();
|
|
||||||
for (int i = 0; i < frames.length; i++) {
|
|
||||||
ReplayFrame frame = frames[i];
|
|
||||||
sb.append(String.format("%d|%s|%s|%d,",
|
|
||||||
frame.getTimeDiff(), nf.format(frame.getRawX()),
|
|
||||||
nf.format(frame.getRawY()), frame.getKeys()));
|
|
||||||
}
|
|
||||||
sb.append(String.format("%s|0|0|%d", SEED_STRING, seed));
|
|
||||||
|
|
||||||
// get bytes from string
|
|
||||||
CharsetEncoder encoder = StandardCharsets.US_ASCII.newEncoder();
|
|
||||||
CharBuffer buffer = CharBuffer.wrap(sb);
|
|
||||||
byte[] bytes = encoder.encode(buffer).array();
|
|
||||||
|
|
||||||
// compress data
|
|
||||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
|
||||||
LzmaOutputStream compressedOut = new LzmaOutputStream.Builder(bout).useMediumDictionarySize().build();
|
|
||||||
try {
|
|
||||||
compressedOut.write(bytes);
|
|
||||||
} catch (IOException e) {
|
|
||||||
// possible OOM: https://github.com/jponge/lzma-java/issues/9
|
|
||||||
ErrorHandler.error("LZMA compression failed (possible out-of-memory error).", e, true);
|
|
||||||
}
|
|
||||||
compressedOut.close();
|
|
||||||
bout.close();
|
|
||||||
|
|
||||||
// write to file
|
|
||||||
byte[] compressed = bout.toByteArray();
|
|
||||||
writer.write(compressed.length);
|
|
||||||
writer.write(compressed);
|
|
||||||
} else
|
|
||||||
writer.write(0);
|
|
||||||
|
|
||||||
writer.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
ErrorHandler.error("Could not save replay data.", e, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user