From 495a7e7f8b01116307c81e3767d68e19c30a8361 Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Mon, 29 Jun 2015 20:18:28 -0500 Subject: [PATCH] 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 --- .gitignore | 1 + src/itdelatrisu/opsu/Options.java | 7 ++++ .../opsu/replay/ReplayImporter.java | 38 ++++++++++++++++--- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 1773d9fe..629e6a34 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/ReplayImport/ /Replays/ /Screenshots/ /Skins/ diff --git a/src/itdelatrisu/opsu/Options.java b/src/itdelatrisu/opsu/Options.java index b5072e0e..0f3f48d6 100644 --- a/src/itdelatrisu/opsu/Options.java +++ b/src/itdelatrisu/opsu/Options.java @@ -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(); } diff --git a/src/itdelatrisu/opsu/replay/ReplayImporter.java b/src/itdelatrisu/opsu/replay/ReplayImporter.java index 06d5a3df..22e4a7dd 100644 --- a/src/itdelatrisu/opsu/replay/ReplayImporter.java +++ b/src/itdelatrisu/opsu/replay/ReplayImporter.java @@ -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. */