Follow-up to #99.
- Many code style changes. - Don't increment combo if missing the last slider circle. - Added player name in ranking screen. - Don't show null/default player names. - Only import replays with .osr extension. - Display loading status for importing replays. - Moved MD5InputStreamWrapper to package "opsu.io". Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
@@ -56,6 +56,9 @@ public class Replay {
|
||||
/** The associated file. */
|
||||
private File file;
|
||||
|
||||
/** The associated score data. */
|
||||
private ScoreData scoreData;
|
||||
|
||||
/** Whether or not the replay data has been loaded from the file. */
|
||||
public boolean loaded = false;
|
||||
|
||||
@@ -104,8 +107,6 @@ public class Replay {
|
||||
/** Seed. (?) */
|
||||
public int seed;
|
||||
|
||||
private ScoreData scoreData;
|
||||
|
||||
/** Seed string. */
|
||||
private static final String SEED_STRING = "-12345";
|
||||
|
||||
@@ -136,46 +137,16 @@ public class Replay {
|
||||
reader.close();
|
||||
loaded = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads the replay header only.
|
||||
* @throws IOException failure to load the data
|
||||
*/
|
||||
public void loadHeader() throws IOException {
|
||||
OsuReader reader = new OsuReader(file);
|
||||
loadHeader(reader);
|
||||
reader.close();
|
||||
}
|
||||
/**
|
||||
* Returns a ScoreData object encapsulating all game data.
|
||||
* If score data already exists, the existing object will be returned
|
||||
* (i.e. this will not overwrite existing data).
|
||||
* @param osu the OsuFile
|
||||
* @return the ScoreData object
|
||||
*/
|
||||
public ScoreData getScoreData(Beatmap osu) {
|
||||
if (scoreData != null)
|
||||
return scoreData;
|
||||
|
||||
scoreData = new ScoreData();
|
||||
scoreData.timestamp = file.lastModified() / 1000L;
|
||||
scoreData.MID = osu.beatmapID;
|
||||
scoreData.MSID = osu.beatmapSetID;
|
||||
scoreData.title = osu.title;
|
||||
scoreData.artist = osu.artist;
|
||||
scoreData.creator = osu.creator;
|
||||
scoreData.version = osu.version;
|
||||
scoreData.hit300 = hit300;
|
||||
scoreData.hit100 = hit100;
|
||||
scoreData.hit50 = hit50;
|
||||
scoreData.geki = geki;
|
||||
scoreData.katu = katu;
|
||||
scoreData.miss = miss;
|
||||
scoreData.score = score;
|
||||
scoreData.combo = combo;
|
||||
scoreData.perfect = perfect;
|
||||
scoreData.mods = mods;
|
||||
scoreData.replayString = file!=null ? file.getName() : getReplayFilename();
|
||||
scoreData.playerName = playerName!=null ? playerName : "No Name";
|
||||
return scoreData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads the replay header data.
|
||||
@@ -260,6 +231,40 @@ public class Replay {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a ScoreData object encapsulating all replay data.
|
||||
* If score data already exists, the existing object will be returned
|
||||
* (i.e. this will not overwrite existing data).
|
||||
* @param beatmap the beatmap
|
||||
* @return the ScoreData object
|
||||
*/
|
||||
public ScoreData getScoreData(Beatmap beatmap) {
|
||||
if (scoreData != null)
|
||||
return scoreData;
|
||||
|
||||
scoreData = new ScoreData();
|
||||
scoreData.timestamp = file.lastModified() / 1000L;
|
||||
scoreData.MID = beatmap.beatmapID;
|
||||
scoreData.MSID = beatmap.beatmapSetID;
|
||||
scoreData.title = beatmap.title;
|
||||
scoreData.artist = beatmap.artist;
|
||||
scoreData.creator = beatmap.creator;
|
||||
scoreData.version = beatmap.version;
|
||||
scoreData.hit300 = hit300;
|
||||
scoreData.hit100 = hit100;
|
||||
scoreData.hit50 = hit50;
|
||||
scoreData.geki = geki;
|
||||
scoreData.katu = katu;
|
||||
scoreData.miss = miss;
|
||||
scoreData.score = score;
|
||||
scoreData.combo = combo;
|
||||
scoreData.perfect = perfect;
|
||||
scoreData.mods = mods;
|
||||
scoreData.replayString = getReplayFilename();
|
||||
scoreData.playerName = playerName;
|
||||
return scoreData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the replay data to a file in the replays directory.
|
||||
*/
|
||||
|
||||
@@ -1,49 +1,119 @@
|
||||
/*
|
||||
* opsu! - an open-source osu! client
|
||||
* Copyright (C) 2014, 2015 Jeffrey Han
|
||||
*
|
||||
* 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.replay;
|
||||
|
||||
import itdelatrisu.opsu.ErrorHandler;
|
||||
import itdelatrisu.opsu.Options;
|
||||
import itdelatrisu.opsu.ScoreData;
|
||||
import itdelatrisu.opsu.beatmap.Beatmap;
|
||||
import itdelatrisu.opsu.beatmap.BeatmapSetList;
|
||||
import itdelatrisu.opsu.db.ScoreDB;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.newdawn.slick.util.Log;
|
||||
|
||||
/**
|
||||
* Importer for replay files.
|
||||
*/
|
||||
public class ReplayImporter {
|
||||
/** The index of the current file being imported. */
|
||||
private static int fileIndex = -1;
|
||||
|
||||
/** The total number of replays to import. */
|
||||
private static File[] files;
|
||||
|
||||
// This class should not be instantiated.
|
||||
private ReplayImporter() {}
|
||||
|
||||
/**
|
||||
* Invokes the importer for each OSR file in a directory, adding the replay
|
||||
* to the score database and moving the file into the replay directory.
|
||||
* @param dir the directory
|
||||
*/
|
||||
public static void importAllReplaysFromDir(File dir) {
|
||||
for (File replayToImport : dir.listFiles()) {
|
||||
try {
|
||||
Replay r = new Replay(replayToImport);
|
||||
r.loadHeader();
|
||||
Beatmap oFile = BeatmapSetList.get().getFileFromBeatmapHash(r.beatmapHash);
|
||||
if(oFile != null){
|
||||
File replaydir = Options.getReplayDir();
|
||||
if (!replaydir.isDirectory()) {
|
||||
if (!replaydir.mkdir()) {
|
||||
ErrorHandler.error("Failed to create replay directory.", null, false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
//ErrorHandler.error("Importing"+replayToImport+" forBeatmap:"+oFile, null, false);
|
||||
ScoreData data = r.getScoreData(oFile);
|
||||
File moveToFile = new File(replaydir, replayToImport.getName());
|
||||
if(
|
||||
!replayToImport.renameTo(moveToFile)
|
||||
){
|
||||
Log.warn("Rename Failed "+moveToFile);
|
||||
}
|
||||
data.replayString = replayToImport.getName().substring(0, replayToImport.getName().length()-4);
|
||||
ScoreDB.addScore(data);;
|
||||
} else {
|
||||
Log.warn("Could not find beatmap for replay "+replayToImport);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.warn("Failed to import replays ",e);
|
||||
// find all OSR files
|
||||
files = dir.listFiles(new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.toLowerCase().endsWith(".osr");
|
||||
}
|
||||
|
||||
});
|
||||
if (files == null || files.length < 1) {
|
||||
files = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// get replay directory
|
||||
File replayDir = Options.getReplayDir();
|
||||
if (!replayDir.isDirectory()) {
|
||||
if (!replayDir.mkdir()) {
|
||||
ErrorHandler.error(String.format("Failed to create replay directory '%s'.", replayDir.getAbsolutePath()), null, false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// import OSRs
|
||||
for (File file : files) {
|
||||
fileIndex++;
|
||||
Replay r = new Replay(file);
|
||||
try {
|
||||
r.loadHeader();
|
||||
} catch (IOException e) {
|
||||
ErrorHandler.error(String.format("Failed to import replay '%s'. The replay file could not be parsed.", file.getName()), e, false);
|
||||
continue;
|
||||
}
|
||||
Beatmap beatmap = BeatmapSetList.get().getBeatmapFromHash(r.beatmapHash);
|
||||
if (beatmap != null) {
|
||||
File moveToFile = new File(replayDir, String.format("%s.osr", r.getReplayFilename()));
|
||||
if (!file.renameTo(moveToFile)) {
|
||||
ErrorHandler.error(String.format("Failed to import replay '%s'. The replay file could not be moved to the replay directory.", file.getName()), null, false);
|
||||
//continue;
|
||||
}
|
||||
ScoreDB.addScore(r.getScoreData(beatmap));
|
||||
} else {
|
||||
ErrorHandler.error(String.format("Failed to import replay '%s'. The associated beatmap could not be found.", file.getName()), null, false);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
fileIndex = -1;
|
||||
files = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the current file being imported, or null if none.
|
||||
*/
|
||||
public static String getCurrentFileName() {
|
||||
if (files == null || fileIndex == -1)
|
||||
return null;
|
||||
|
||||
return files[fileIndex].getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the progress of replay importing, or -1 if not importing.
|
||||
* @return the completion percent [0, 100] or -1
|
||||
*/
|
||||
public static int getLoadingProgress() {
|
||||
if (files == null || fileIndex == -1)
|
||||
return -1;
|
||||
|
||||
return (fileIndex + 1) * 100 / files.length;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user