fix: renaming a game will rename files properly also if they have non-standard names after file import

This commit is contained in:
lantzelot-swe 2021-01-20 22:50:14 +01:00
parent 9fe2fbb482
commit 0f84d48758
3 changed files with 62 additions and 20 deletions

View File

@ -28,6 +28,11 @@ public class InfoModel extends AbstractModel
private BufferedImage screen2Image;
private Path gamesPath;
private String oldGamesFile = "";
private String oldCoverFile = "";
private String oldScreens1File = "";
private String oldScreens2File = "";
public String getTitle()
{
@ -264,10 +269,16 @@ public class InfoModel extends AbstractModel
}
}
public void updateFileNames()
public boolean updateFileNames()
{
if (isNewGame() || isTitleChanged())
{
//Keep track of the old names, used when renaming files when saving
oldCoverFile = getCoverFile();
oldScreens1File = getScreens1File();
oldScreens2File = getScreens2File();
oldGamesFile = getGamesFile();
disableChangeNotification(true);
String fileName = FileManager.generateFileNameFromTitle(this.title);
if (!getCoverFile().isEmpty() || getCoverImage() != null)
@ -288,7 +299,9 @@ public class InfoModel extends AbstractModel
setGamesFile(fileName + fileEnding);
}
disableChangeNotification(false);
return true;
}
return false;
}
public boolean isNewGame()
@ -302,11 +315,35 @@ public class InfoModel extends AbstractModel
}
public void resetImages()
public void resetImagesAndOldFileNames()
{
this.coverImage = null;
this.screen1Image = null;
this.screen2Image = null;
this.gamesPath = null;
this.oldGamesFile = "";
this.oldCoverFile = "";
this.oldScreens1File = "";
this.oldScreens2File = "";
}
public String getOldGamesFile()
{
return oldGamesFile;
}
public String getOldCoverFile()
{
return oldCoverFile;
}
public String getOldScreens1File()
{
return oldScreens1File;
}
public String getOldScreens2File()
{
return oldScreens2File;
}
}

View File

@ -151,7 +151,7 @@ public class MainViewModel extends AbstractModel
infoModel.setScreens1File(details.getScreen1());
infoModel.setScreens2File(details.getScreen2());
//Reset and images that where added previously
infoModel.resetImages();
infoModel.resetImagesAndOldFileNames();
joy1Model.setConfigStringFromDb(details.getJoy1());
@ -364,7 +364,7 @@ public class MainViewModel extends AbstractModel
fileManager.saveFiles();
//Reset and images that where added previously
infoModel.resetImages();
infoModel.resetImagesAndOldFileNames();
//Reset all models
infoModel.resetDataChanged();
joy1Model.resetDataChanged();

View File

@ -12,7 +12,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -27,10 +26,10 @@ import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import javax.imageio.ImageIO;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -199,11 +198,12 @@ public class FileManager
private void renameFiles()
{
String oldTitle = generateFileNameFromTitle(infoModel.getTitleInDb());
if (!infoModel.getCoverFile().isEmpty())
String coverFile = infoModel.getCoverFile();
String oldCoverFile = infoModel.getOldCoverFile();
if (!coverFile.isEmpty() && !oldCoverFile.isEmpty())
{
File oldCover = new File(COVERS + oldTitle + "-cover.png");
File newCover = new File(COVERS + infoModel.getCoverFile());
File oldCover = new File(COVERS + oldCoverFile);
File newCover = new File(COVERS + coverFile);
if (oldCover.renameTo(newCover))
{
logger.debug("Renamed cover {} to {}", oldCover.getName(), newCover.getName());
@ -213,10 +213,12 @@ public class FileManager
logger.debug("Could NOT rename cover {} to {}", oldCover.getName(), newCover.getName());
}
}
if (!infoModel.getScreens1File().isEmpty())
String screens1File = infoModel.getScreens1File();
String oldScreens1File = infoModel.getOldScreens1File();
if (!screens1File.isEmpty() && !oldScreens1File.isEmpty())
{
File oldScreen1 = new File(SCREENS + oldTitle + "-00.png");
File newScreen1 = new File(SCREENS + infoModel.getScreens1File());
File oldScreen1 = new File(SCREENS + oldScreens1File);
File newScreen1 = new File(SCREENS + screens1File);
if (oldScreen1.renameTo(newScreen1))
{
logger.debug("Renamed screen1 {} to {}", oldScreen1.getName(), newScreen1.getName());
@ -226,10 +228,12 @@ public class FileManager
logger.debug("Could NOT rename screen1 {} to {}", oldScreen1.getName(), newScreen1.getName());
}
}
if (!infoModel.getScreens2File().isEmpty())
String screens2File = infoModel.getScreens2File();
String oldScreens2File = infoModel.getOldScreens2File();
if (!screens2File.isEmpty() && !oldScreens2File.isEmpty())
{
File oldScreen2 = new File(SCREENS + oldTitle + "-01.png");
File newScreen2 = new File(SCREENS + infoModel.getScreens2File());
File oldScreen2 = new File(SCREENS + oldScreens2File);
File newScreen2 = new File(SCREENS + screens2File);
if (oldScreen2.renameTo(newScreen2))
{
logger.debug("Renamed screen2 {} to {}", oldScreen2.getName(), newScreen2.getName());
@ -239,11 +243,12 @@ public class FileManager
logger.debug("Could NOT rename screen2 {} to {}", oldScreen2.getName(), newScreen2.getName());
}
}
if (!infoModel.getGamesFile().isEmpty())
String gamesFile = infoModel.getGamesFile();
String oldGamesFile = infoModel.getOldGamesFile();
if (!gamesFile.isEmpty() && !oldGamesFile.isEmpty())
{
String fileEnding = infoModel.getGamesFile().substring(infoModel.getGamesFile().indexOf("."));
File oldGame = new File(GAMES + oldTitle + fileEnding);
File newGame = new File(GAMES + infoModel.getGamesFile());
File oldGame = new File(GAMES + oldGamesFile);
File newGame = new File(GAMES + gamesFile);
if (oldGame.renameTo(newGame))
{
logger.debug("Renamed game {} to {}", oldGame.getName(), newGame.getName());