diff --git a/src/main/java/se/lantz/db/DbConnector.java b/src/main/java/se/lantz/db/DbConnector.java index b9fd8f9..0289b88 100644 --- a/src/main/java/se/lantz/db/DbConnector.java +++ b/src/main/java/se/lantz/db/DbConnector.java @@ -954,6 +954,7 @@ public class DbConnector String sql = "SELECT * FROM gameinfo WHERE rowid = " + gameId; logger.debug("Generated SELECT String:\n{}", sql); GameDetails returnValue = new GameDetails(); + returnValue.setGameId(gameId); try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) { ResultSet rs = pstmt.executeQuery(); diff --git a/src/main/java/se/lantz/manager/ExportManager.java b/src/main/java/se/lantz/manager/ExportManager.java index e4c39b9..c988312 100644 --- a/src/main/java/se/lantz/manager/ExportManager.java +++ b/src/main/java/se/lantz/manager/ExportManager.java @@ -257,7 +257,7 @@ public class ExportManager } } } - + public void copyGamesForFileLoader(PublishWorker worker) { if (gameViewMode) @@ -306,17 +306,14 @@ public class ExportManager } zipped = true; } - String gameFileNameWithoutExtension = FileManager.generateFileNameFromTitleForFileLoader(gameDetails.getTitle(), - gameDetails.getDuplicateIndex()); - - Path targetGamePath = targetPath - .resolve(gameFileNameWithoutExtension + "." + extension); + String gameFileNameWithoutExtension = FileManager.generateFileNameFromTitleForFileLoader(gameDetails + .getGameId(), gameDetails.getTitle(), gameDetails.getAuthor(), gameDetails.getDuplicateIndex()); + + Path targetGamePath = targetPath.resolve(gameFileNameWithoutExtension + "." + extension); if (FileManager.hasExtraDisks(gameDetails)) { - targetGamePath = targetPath - .resolve(gameFileNameWithoutExtension + " (disk 1)." + extension); + targetGamePath = targetPath.resolve(gameFileNameWithoutExtension + " (disk 1)." + extension); } - try { @@ -347,27 +344,32 @@ public class ExportManager copyExtraDiskForFileLoader(targetPath, gameFileNameWithoutExtension, gameDetails.getDisk6(), 6, worker); } } - - private void copyExtraDiskForFileLoader(Path targetPath, String gameFileNameWithoutExtension, String extraDiskName, int diskIndex, PublishWorker worker) + + private void copyExtraDiskForFileLoader(Path targetPath, + String gameFileNameWithoutExtension, + String extraDiskName, + int diskIndex, + PublishWorker worker) { if (extraDiskName != null && !extraDiskName.isEmpty()) { String extension = FilenameUtils.getExtension(extraDiskName); - - Path targetGamePath = targetPath - .resolve(gameFileNameWithoutExtension + " (disk " + diskIndex + ")_CD." + extension); - + + Path targetGamePath = + targetPath.resolve(gameFileNameWithoutExtension + " (disk " + diskIndex + ")_CD." + extension); + Path extraDiskPath = Paths.get("./extradisks/" + extraDiskName); - + try { - - worker.publishMessage("Copying extra disk file from " + extraDiskPath.toString() + " to " + targetGamePath); - Files.copy(extraDiskPath, targetGamePath, StandardCopyOption.REPLACE_EXISTING); + + worker.publishMessage("Copying extra disk file from " + extraDiskPath.toString() + " to " + targetGamePath); + Files.copy(extraDiskPath, targetGamePath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { - worker.publishMessage("ERROR: Could not copy files for " + gameFileNameWithoutExtension + ", " + e.getMessage()); + worker + .publishMessage("ERROR: Could not copy files for " + gameFileNameWithoutExtension + ", " + e.getMessage()); ExceptionHandler.handleException(e, "Could NOT copy files for: " + gameFileNameWithoutExtension); } } diff --git a/src/main/java/se/lantz/model/MainViewModel.java b/src/main/java/se/lantz/model/MainViewModel.java index ca15262..a78e01d 100644 --- a/src/main/java/se/lantz/model/MainViewModel.java +++ b/src/main/java/se/lantz/model/MainViewModel.java @@ -104,6 +104,7 @@ public class MainViewModel extends AbstractModel .setPrimaryWithoutListenerNotification(!Boolean.valueOf(e.getActionCommand()))); resetDataChangedAfterInit(); + fileManager.setDbConnector(dbConnector); } public DbConnector getDbConnector() diff --git a/src/main/java/se/lantz/model/data/GameDetails.java b/src/main/java/se/lantz/model/data/GameDetails.java index 9f3e118..90518f0 100644 --- a/src/main/java/se/lantz/model/data/GameDetails.java +++ b/src/main/java/se/lantz/model/data/GameDetails.java @@ -36,6 +36,8 @@ public class GameDetails private String disk5 = ""; private String disk6 = ""; + private String gameId = ""; + public GameDetails() { @@ -295,4 +297,14 @@ public class GameDetails { this.disk6 = disk6 == null ? "" : disk6; } + + public String getGameId() + { + return gameId; + } + + public void setGameId(String gameId) + { + this.gameId = gameId; + } } diff --git a/src/main/java/se/lantz/util/FileManager.java b/src/main/java/se/lantz/util/FileManager.java index 8faa4f1..b0db6cb 100644 --- a/src/main/java/se/lantz/util/FileManager.java +++ b/src/main/java/se/lantz/util/FileManager.java @@ -99,6 +99,7 @@ public class FileManager private static List compressedDiskFilesEndingList = Arrays.asList("d64", "d71", "x64", "g64"); + private static DbConnector dbconnector; static { try @@ -123,6 +124,11 @@ public class FileManager this.savedStatesModel = model.getSavedStatesModel(); this.model = model; } + + public void setDbConnector(DbConnector ref) + { + dbconnector = ref; + } public static InputStream getMissingC64GameFile() throws URISyntaxException { @@ -715,18 +721,27 @@ public class FileManager return newNameString; } - public static String generateFileNameFromTitleForFileLoader(String title, int duplicateIndex) + public static String generateFileNameFromTitleForFileLoader(String gameId, String title, String author, int duplicateIndex) { + String correctedAuthor = author.replace("\\", " "); + correctedAuthor = author.replace("/", " "); List forbiddenCharsList = ":,'ï¿!+*<>/?|\"".chars().mapToObj(item -> (char) item).collect(Collectors.toList()); List newName = title.chars().mapToObj(item -> (char) item) .filter(character -> !forbiddenCharsList.contains(character)).collect(Collectors.toList()); String newNameString = newName.stream().map(String::valueOf).collect(Collectors.joining()); - if (duplicateIndex > 0) + if (duplicateIndex > 0 || (!gameId.isEmpty() && dbconnector.getGameDuplicateIndexToUse(title, gameId) > 1)) { - //Just add the duplicate index if there are several games with the same name - newNameString = newNameString + " (" + duplicateIndex + ")"; + //Add author and the duplicate index if larger than 0 if there are several games with the same name + if (duplicateIndex == 0) + { + newNameString = newNameString + " (" + correctedAuthor + ")"; + } + else + { + newNameString = newNameString + " (" + correctedAuthor + ")("+ duplicateIndex +")"; + } } newNameString = newNameString.trim(); @@ -749,13 +764,20 @@ public class FileManager newNameString = newNameString.replaceAll("\\[", "("); newNameString = newNameString.replaceAll("\\]", ")"); + newNameString = newNameString.replace("((", "("); + newNameString = newNameString.replace("))", ")"); + newNameString = newNameString.replace(">", ""); + newNameString = newNameString.replace("<", ""); + newNameString = newNameString.replace("?", ""); + newNameString = newNameString.replace("*", ""); + logger.debug("Game title: \"{}\" ---- New fileName: \"{}\"", title, newNameString); return newNameString; } public static String generateSavedStatesFolderNameForFileLoader(String title, int duplicateIndex) { - String name = generateFileNameFromTitleForFileLoader(title, duplicateIndex); + String name = generateFileNameFromTitleForFileLoader("", title, "", duplicateIndex); //Special handling of titles where dots occur, e.g. G.A.C.C.R.R. or H.E.R.O. int dotCount = (int) name.chars().filter(ch -> ch == '.').count(); if (dotCount > 4) @@ -786,10 +808,10 @@ public class FileManager } worker.publishMessage("Creating cjm file for " + gameDetails.getTitle()); filename = - generateFileNameFromTitleForFileLoader(gameDetails.getTitle(), gameDetails.getDuplicateIndex()) + ".cjm"; + generateFileNameFromTitleForFileLoader(gameDetails.getGameId(), gameDetails.getTitle(), gameDetails.getAuthor(), gameDetails.getDuplicateIndex()) + ".cjm"; if (hasExtraDisks(gameDetails)) { - filename = generateFileNameFromTitleForFileLoader(gameDetails.getTitle(), gameDetails.getDuplicateIndex()) + + filename = generateFileNameFromTitleForFileLoader(gameDetails.getGameId(), gameDetails.getTitle(), gameDetails.getAuthor(), gameDetails.getDuplicateIndex()) + " (disk 1).cjm"; } }