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/ /Replays/
/Screenshots/ /Screenshots/
/Skins/ /Skins/

View File

@ -184,6 +184,13 @@ public class Options {
@Override @Override
public void read(String s) { replayDir = new File(s); } 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") { SKIN_DIRECTORY ("SkinDirectory") {
@Override @Override
public String write() { return getSkinRootDir().getAbsolutePath(); } public String write() { return getSkinRootDir().getAbsolutePath(); }

View File

@ -27,11 +27,18 @@ import itdelatrisu.opsu.db.ScoreDB;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.newdawn.slick.util.Log;
/** /**
* Importer for replay files. * Importer for replay files.
*/ */
public class ReplayImporter { 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. */ /** The index of the current file being imported. */
private static int fileIndex = -1; private static int fileIndex = -1;
@ -75,18 +82,24 @@ public class ReplayImporter {
try { try {
r.loadHeader(); r.loadHeader();
} catch (IOException e) { } 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); ErrorHandler.error(String.format("Failed to import replay '%s'. The replay file could not be parsed.", file.getName()), e, false);
continue; continue;
} }
Beatmap beatmap = BeatmapSetList.get().getBeatmapFromHash(r.beatmapHash); Beatmap beatmap = BeatmapSetList.get().getBeatmapFromHash(r.beatmapHash);
if (beatmap != null) { if (beatmap != null) {
File moveToFile = new File(replayDir, String.format("%s.osr", r.getReplayFilename())); // add score to database
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)); 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 { } else {
moveToFailedDirectory(file);
ErrorHandler.error(String.format("Failed to import replay '%s'. The associated beatmap could not be found.", file.getName()), null, false); ErrorHandler.error(String.format("Failed to import replay '%s'. The associated beatmap could not be found.", file.getName()), null, false);
continue; continue;
} }
@ -96,6 +109,21 @@ public class ReplayImporter {
files = null; 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. * Returns the name of the current file being imported, or null if none.
*/ */