Follow-up to d860a30: replay importing fixes.

- Move failed imports to a "failed" subdirectory so that errors aren't generated each time the program is launched.
- Importing now overwrites files.
- Add "ReplayImportDirectory" option to config file.
- Fixed a bug with scores not being properly added.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-06-29 20:18:28 -05:00
parent d860a30aed
commit 495a7e7f8b
3 changed files with 41 additions and 5 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/ReplayImport/
/Replays/
/Screenshots/
/Skins/

View File

@ -184,6 +184,13 @@ public class Options {
@Override
public void read(String s) { replayDir = new File(s); }
},
REPLAY_IMPORT_DIRECTORY ("ReplayImportDirectory") {
@Override
public String write() { return getReplayImportDir().getAbsolutePath(); }
@Override
public void read(String s) { replayImportDir = new File(s); }
},
SKIN_DIRECTORY ("SkinDirectory") {
@Override
public String write() { return getSkinRootDir().getAbsolutePath(); }

View File

@ -27,11 +27,18 @@ import itdelatrisu.opsu.db.ScoreDB;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.newdawn.slick.util.Log;
/**
* Importer for replay files.
*/
public class ReplayImporter {
/** The subdirectory (within the replay import directory) to move replays that could not be imported. */
private static final String FAILED_IMPORT_DIR = "failed";
/** The index of the current file being imported. */
private static int fileIndex = -1;
@ -75,18 +82,24 @@ public class ReplayImporter {
try {
r.loadHeader();
} catch (IOException e) {
moveToFailedDirectory(file);
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;
}
// add score to database
ScoreDB.addScore(r.getScoreData(beatmap));
// move to replay directory
File moveToFile = new File(replayDir, String.format("%s.osr", r.getReplayFilename()));
try {
Files.move(file.toPath(), moveToFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
Log.warn(String.format("Failed to move replay '%s' to the replay directory '%s'.", file, replayDir), e);
}
} else {
moveToFailedDirectory(file);
ErrorHandler.error(String.format("Failed to import replay '%s'. The associated beatmap could not be found.", file.getName()), null, false);
continue;
}
@ -96,6 +109,21 @@ public class ReplayImporter {
files = null;
}
/**
* Moves a replay file into the failed import directory.
* @param file the file to move
*/
private static void moveToFailedDirectory(File file) {
File dir = new File(Options.getReplayImportDir(), FAILED_IMPORT_DIR);
dir.mkdir();
File moveToFile = new File(dir, file.getName());
try {
Files.move(file.toPath(), moveToFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
Log.warn(String.format("Failed to move replay '%s' to the failed import directory '%s'.", file, dir), e);
}
}
/**
* Returns the name of the current file being imported, or null if none.
*/