fix: export cjm files WIP

This commit is contained in:
mikael.lantz 2021-06-01 16:38:44 +02:00
parent 40e2f2b42d
commit a1a01661ce
6 changed files with 242 additions and 71 deletions

View File

@ -24,6 +24,7 @@ import se.lantz.gui.dbbackup.BackupWorker;
import se.lantz.gui.dbrestore.RestoreDbDialog;
import se.lantz.gui.dbrestore.RestoreProgressDialog;
import se.lantz.gui.dbrestore.RestoreWorker;
import se.lantz.gui.exports.ExportFileLoaderWorker;
import se.lantz.gui.exports.ExportGamesDialog;
import se.lantz.gui.exports.ExportProgressDialog;
import se.lantz.gui.exports.ExportWorker;
@ -46,6 +47,7 @@ public class MenuManager
{
private JMenu fileMenu;
private JMenu importMenu;
private JMenu exportMenu;
private JMenu editMenu;
private JMenu toolsMenu;
private JMenu helpMenu;
@ -57,6 +59,7 @@ public class MenuManager
private JMenuItem importCarouselItem;
private JMenuItem importGamebaseItem;
private JMenuItem exportItem;
private JMenuItem exportFLItem;
private JMenuItem refreshItem;
private JMenuItem toggleFavorite1Item;
@ -131,7 +134,11 @@ public class MenuManager
fileMenu.add(importMenu);
importMenu.add(getImportCarouselItem());
importMenu.add(getImportGamebaseItem());
fileMenu.add(getExportItem());
exportMenu = new JMenu("Export");
exportMenu.setMnemonic('X');
exportMenu.add(getExportItem());
exportMenu.add(getExportFileLoaderItem());
fileMenu.add(exportMenu);
fileMenu.addSeparator();
fileMenu.add(getRefreshItem());
fileMenu.addSeparator();
@ -262,7 +269,7 @@ public class MenuManager
private JMenuItem getExportItem()
{
exportItem = new JMenuItem("Export games...");
exportItem = new JMenuItem("Export to Carousel...");
KeyStroke keyStrokeToExportGames = KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_DOWN_MASK);
exportItem.setAccelerator(keyStrokeToExportGames);
exportItem.setMnemonic('E');
@ -270,6 +277,16 @@ public class MenuManager
return exportItem;
}
private JMenuItem getExportFileLoaderItem()
{
exportFLItem = new JMenuItem("Export to File Loader...");
KeyStroke keyStrokeToExportGames = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_DOWN_MASK);
exportFLItem.setAccelerator(keyStrokeToExportGames);
exportFLItem.setMnemonic('L');
exportFLItem.addActionListener(e -> exportGamesToFileLoader());
return exportFLItem;
}
private JMenuItem getRefreshItem()
{
refreshItem = new JMenuItem("Reload current gamelist view");
@ -721,6 +738,28 @@ public class MenuManager
}
}
private void exportGamesToFileLoader()
{
final ExportGamesDialog exportSelectionDialog = new ExportGamesDialog(MainWindow.getInstance());
exportSelectionDialog.pack();
exportSelectionDialog.setLocationRelativeTo(this.mainWindow);
if (exportSelectionDialog.showDialog())
{
List<GameListData> gamesList = exportSelectionDialog.getSelectedGames();
if (!gamesList.isEmpty())
{
exportManager.setGamesToExport(gamesList);
exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(),
exportSelectionDialog.deleteBeforeExport(),
exportSelectionDialog.addGamesSubDirectory());
ExportProgressDialog dialog = new ExportProgressDialog(this.mainWindow);
ExportFileLoaderWorker worker = new ExportFileLoaderWorker(exportManager, dialog);
worker.execute();
dialog.setVisible(true);
}
}
}
private void reloadView()
{
this.mainWindow.reloadCurrentGameView();

View File

@ -0,0 +1,68 @@
package se.lantz.gui.exports;
import java.util.List;
import javax.swing.SwingWorker;
import se.lantz.manager.ExportManager;
import se.lantz.util.ExceptionHandler;
public class ExportFileLoaderWorker extends SwingWorker<Void, String>
{
private ExportManager exportManager;
private ExportProgressDialog dialog;
public ExportFileLoaderWorker(ExportManager importManager, ExportProgressDialog dialog)
{
this.exportManager = importManager;
this.dialog = dialog;
}
@Override
protected Void doInBackground() throws Exception
{
if (exportManager.isDeleteBeforeExport())
{
publish("Deleting existing games before exporting.\n");
}
publish("Exporting from db...");
StringBuilder infoBuilder = new StringBuilder();
exportManager.readFromDb(infoBuilder);
publish(infoBuilder.toString());
publish("Creating cjm files...");
infoBuilder = new StringBuilder();
exportManager.createGameInfoFiles(infoBuilder, true);
publish(infoBuilder.toString());
publish("Copy game files...");
infoBuilder = new StringBuilder();
exportManager.copyGamesForFileLoader(infoBuilder);
publish(infoBuilder.toString());
exportManager.clearAfterImport();
publish("Done!");
return null;
}
@Override
protected void process(List<String> chunks)
{
for (String value : chunks)
{
dialog.updateProgress(value + "\n");
}
}
@Override
protected void done()
{
try
{
get();
}
catch (Exception e)
{
ExceptionHandler.handleException(e, "Error during export");
}
dialog.finish();
}
}

View File

@ -32,7 +32,7 @@ public class ExportWorker extends SwingWorker<Void, String>
publish(infoBuilder.toString());
publish("Creating game info files...");
infoBuilder = new StringBuilder();
exportManager.createGameInfoFiles(infoBuilder);
exportManager.createGameInfoFiles(infoBuilder, false);
publish(infoBuilder.toString());
publish("Copy screenshots, covers and game files...");
infoBuilder = new StringBuilder();

View File

@ -105,11 +105,11 @@ public class ExportManager
gameDetailsList = uiModel.readGameDetailsForExport(infoBuilder, gamesList);
}
public void createGameInfoFiles(StringBuilder infoBuilder)
public void createGameInfoFiles(StringBuilder infoBuilder, boolean fileLoader)
{
for (GameDetails gameDetails : gameDetailsList)
{
uiModel.exportGameInfoFile(gameDetails, targetDir, infoBuilder);
uiModel.exportGameInfoFile(gameDetails, targetDir, infoBuilder, fileLoader);
}
}
@ -118,11 +118,8 @@ public class ExportManager
return deleteBeforeExport;
}
public void copyFiles(StringBuilder infoBuilder)
{
try
{
Path targetCoverPath = targetDir.toPath().resolve("covers");
@ -134,13 +131,10 @@ public class ExportManager
}
catch (IOException e)
{
infoBuilder
.append("ERROR: Could not create directories for covers, screens and games, " + e.getMessage() + "\n");
infoBuilder.append("ERROR: Could not create directories for covers, screens and games, " + e.getMessage() + "\n");
ExceptionHandler.handleException(e, " Could not create directories for covers, screens and games");
return;
}
for (GameDetails gameDetails : gameDetailsList)
{
Path coverPath = Paths.get("./covers/" + gameDetails.getCover());
@ -193,6 +187,45 @@ public class ExportManager
}
}
public void copyGamesForFileLoader(StringBuilder infoBuilder)
{
try
{
Path targetGamePath = targetDir.toPath();
Files.createDirectories(targetGamePath);
}
catch (IOException e)
{
infoBuilder.append("ERROR: Could not create directory for games, " + e.getMessage() + "\n");
ExceptionHandler.handleException(e, " Could not create directory for games");
return;
}
for (GameDetails gameDetails : gameDetailsList)
{
Path gamePath = Paths.get("./games/" + gameDetails.getGame());
//TODO: Unzip if needed, rename vsf files to prg according to Spannernick
Path targetGamePath = targetDir.toPath().resolve(gameDetails.getGame());
try
{
if (!gameDetails.getGame().isEmpty())
{
infoBuilder.append("Copying game file from ");
infoBuilder.append(gamePath.toString());
infoBuilder.append("\n");
Files.copy(gamePath, targetGamePath, StandardCopyOption.REPLACE_EXISTING);
}
}
catch (IOException e)
{
infoBuilder.append("ERROR: Could not copy files for " + gameDetails.getTitle() + ", " + e.getMessage() + "\n");
ExceptionHandler.handleException(e, "Could NOT copy files for: " + gameDetails.getTitle());
}
}
}
public void clearAfterImport()
{
gamesList.clear();

View File

@ -274,9 +274,9 @@ public class MainViewModel extends AbstractModel
return returnList;
}
public void exportGameInfoFile(GameDetails gameDetails, File targetDir, StringBuilder infoBuilder)
public void exportGameInfoFile(GameDetails gameDetails, File targetDir, StringBuilder infoBuilder, boolean fileLoader)
{
fileManager.exportGameInfoFile(gameDetails, targetDir, infoBuilder);
fileManager.exportGameInfoFile(gameDetails, targetDir, infoBuilder, fileLoader);
}
public InfoModel getInfoModel()

View File

@ -367,15 +367,44 @@ public class FileManager
return newNameString;
}
public void exportGameInfoFile(GameDetails gameDetails, File targetDir, StringBuilder infoBuilder)
public static String generateFileNameFromTitleForFileLoader(String title, int duplicateIndex)
{
List<Character> forbiddenCharsList =
":,'!+*<>()/[]?|".chars().mapToObj(item -> (char) item).collect(Collectors.toList());
List<Character> 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)
{
//Just add the duplicate index if there are several games with the same name
newNameString = newNameString + "0" + duplicateIndex;
}
logger.debug("Game title: \"{}\" ---- New fileName: \"{}\"", title, newNameString);
return newNameString;
}
public void exportGameInfoFile(GameDetails gameDetails, File targetDir, StringBuilder infoBuilder, boolean fileLoader)
{
try
{
String filename = generateFileNameFromTitle(gameDetails.getTitle(), gameDetails.getDuplicateIndex());
String filename = "";
if (fileLoader)
{
infoBuilder.append("Creating cjm file for " + gameDetails.getTitle() + "\n");
filename = generateFileNameFromTitleForFileLoader(gameDetails.getTitle(), gameDetails.getDuplicateIndex()) + ".cjm";
}
else
{
infoBuilder.append("Creating game info file for " + gameDetails.getTitle() + "\n");
//Add -ms to comply with the maxi game tool.
filename = filename + "-ms.tsg";
writeGameInfoFile(filename, targetDir, gameDetails);
filename = generateFileNameFromTitle(gameDetails.getTitle(), gameDetails.getDuplicateIndex()) + "-ms.tsg";
}
writeGameInfoFile(filename, targetDir, gameDetails, fileLoader);
}
catch (Exception e)
{
@ -385,15 +414,16 @@ public class FileManager
}
}
public void writeGameInfoFile(String fileName, File targetDir, GameDetails gameDetails) throws IOException
public void writeGameInfoFile(String fileName, File targetDir, GameDetails gameDetails, boolean fileLoader) throws IOException
{
Path outDirPath = targetDir.toPath();
Path filePath = outDirPath.resolve(fileName);
filePath.toFile().createNewFile();
FileWriter fw = new FileWriter(filePath.toFile());
if (!fileLoader)
{
fw.write("T:" + gameDetails.getTitle() + "\n");
fw.write("X:" + gameDetails.getSystem() + "\n");
String description = replaceMinus(gameDetails.getDescription());
fw.write("D:en:" + description + "\n");
fw.write("D:de:" +
@ -425,7 +455,8 @@ public class FileManager
{
fw.write("G:" + "screens/" + gameDetails.getScreen2() + "\n");
}
}
fw.write("X:" + gameDetails.getSystem() + "\n");
if (!gameDetails.getJoy1().isEmpty())
{
fw.write(gameDetails.getJoy1() + "\n");