From 91b30c4d29bae8bec7b48dd57644e974fb63b5e2 Mon Sep 17 00:00:00 2001 From: Skidam Date: Wed, 31 Jul 2024 18:50:38 +0200 Subject: [PATCH] Editable files are now preserved between modpack switches #257 bump to beta 12 --- .../skidam/automodpack_core/config/Jsons.java | 2 +- .../utils/FileInspection.java | 8 +- gradle.properties | 2 +- .../automodpack_loader_core/Preload.java | 5 ++ .../client/ModpackUpdater.java | 23 +++--- .../client/ModpackUtils.java | 75 +++++++++++++++++-- 6 files changed, 89 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/pl/skidam/automodpack_core/config/Jsons.java b/core/src/main/java/pl/skidam/automodpack_core/config/Jsons.java index d6da9e8c..b61b2292 100644 --- a/core/src/main/java/pl/skidam/automodpack_core/config/Jsons.java +++ b/core/src/main/java/pl/skidam/automodpack_core/config/Jsons.java @@ -11,7 +11,7 @@ public class Jsons { public static class ClientConfigFields { public int DO_NOT_CHANGE_IT = 1; // file version public String selectedModpack = ""; // modpack name - public Map installedModpacks = Map.of(); // modpack name, link + public Map installedModpacks; // modpack name, link public boolean selfUpdater = false; } diff --git a/core/src/main/java/pl/skidam/automodpack_core/utils/FileInspection.java b/core/src/main/java/pl/skidam/automodpack_core/utils/FileInspection.java index bad41394..7a632897 100644 --- a/core/src/main/java/pl/skidam/automodpack_core/utils/FileInspection.java +++ b/core/src/main/java/pl/skidam/automodpack_core/utils/FileInspection.java @@ -487,10 +487,9 @@ public static LoaderService.EnvironmentType getModEnvironment(Path file) { return environmentType; } - public static boolean isInValidFileName(String fileName) { - // Define a list of characters that are not allowed in file names - String forbiddenChars = "\\/:*?\"<>|"; + private static final String forbiddenChars = "\\/:*\"<>|!?."; + public static boolean isInValidFileName(String fileName) { // Check for each forbidden character in the file name for (char c : forbiddenChars.toCharArray()) { if (fileName.indexOf(c) != -1) { @@ -503,9 +502,6 @@ public static boolean isInValidFileName(String fileName) { } public static String fixFileName(String fileName) { - // Define a list of characters that are not allowed in file names - String forbiddenChars = "\\/:*?\"<>|"; - // Replace forbidden characters with underscores for (char c : forbiddenChars.toCharArray()) { fileName = fileName.replace(c, '-'); diff --git a/gradle.properties b/gradle.properties index 5199a9b4..aa71530e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,6 +9,6 @@ mixin_extras = 0.3.6 mod_id = automodpack mod_name = AutoModpack -mod_version = 4.0.0-beta11 +mod_version = 4.0.0-beta12 mod_group = pl.skidam.automodpack mod_description = Enjoy a seamless modpack installation process and effortless updates with a user-friendly solution that simplifies management, making your gaming experience a breeze. \ No newline at end of file diff --git a/loader/core/src/main/java/pl/skidam/automodpack_loader_core/Preload.java b/loader/core/src/main/java/pl/skidam/automodpack_loader_core/Preload.java index b9e9b5e7..826850bb 100644 --- a/loader/core/src/main/java/pl/skidam/automodpack_loader_core/Preload.java +++ b/loader/core/src/main/java/pl/skidam/automodpack_loader_core/Preload.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.nio.file.*; +import java.util.HashMap; import java.util.List; import static pl.skidam.automodpack_core.GlobalVariables.*; @@ -127,6 +128,10 @@ private void loadConfigs() { LOGGER.info("Updated client config version to {}", clientConfig.DO_NOT_CHANGE_IT); } + if (clientConfig.installedModpacks == null) { + clientConfig.installedModpacks = new HashMap<>(); + } + // Save changes ConfigTools.save(clientConfigFile, clientConfig); } diff --git a/loader/core/src/main/java/pl/skidam/automodpack_loader_core/client/ModpackUpdater.java b/loader/core/src/main/java/pl/skidam/automodpack_loader_core/client/ModpackUpdater.java index 6e2d8f86..91ac167d 100644 --- a/loader/core/src/main/java/pl/skidam/automodpack_loader_core/client/ModpackUpdater.java +++ b/loader/core/src/main/java/pl/skidam/automodpack_loader_core/client/ModpackUpdater.java @@ -47,7 +47,7 @@ public void startModpackUpdate(Jsons.ModpackContentFields serverModpackContent, // Handle the case where serverModpackContent is null if (serverModpackContent == null) { - handleOfflineMode(modpackDir, modpackContentFile); + handleOfflineMode(modpackDir, modpackContentFile, link); return; } @@ -68,7 +68,7 @@ public void startModpackUpdate(Jsons.ModpackContentFields serverModpackContent, // Check if an update is needed if (!ModpackUtils.isUpdate(serverModpackContent, modpackDir)) { LOGGER.info("Modpack is up to date"); - CheckAndLoadModpack(modpackDir, modpackContentFile); + CheckAndLoadModpack(modpackDir, modpackContentFile, link); return; } } else if (!preload) { @@ -88,7 +88,7 @@ public void startModpackUpdate(Jsons.ModpackContentFields serverModpackContent, } } - private void handleOfflineMode(Path modpackDir, Path modpackContentFile) throws Exception { + private void handleOfflineMode(Path modpackDir, Path modpackContentFile, String link) throws Exception { if (!Files.exists(modpackContentFile)) { return; } @@ -99,13 +99,13 @@ private void handleOfflineMode(Path modpackDir, Path modpackContentFile) throws } LOGGER.warn("Server is down, or you don't have access to internet, but we still want to load selected modpack"); - CheckAndLoadModpack(modpackDir, modpackContentFile); + CheckAndLoadModpack(modpackDir, modpackContentFile, link); } - public void CheckAndLoadModpack(Path modpackDir, Path modpackContentFile) throws Exception { + public void CheckAndLoadModpack(Path modpackDir, Path modpackContentFile, String link) throws Exception { - boolean requiresRestart = finishModpackUpdate(modpackDir, modpackContentFile); + boolean requiresRestart = applyModpack(modpackDir, modpackContentFile, link); if (requiresRestart) { LOGGER.info("Modpack is not loaded"); @@ -321,13 +321,11 @@ public void ModpackUpdaterMain(String link, Path modpackDir, Path modpackContent Path cwd = Path.of(System.getProperty("user.dir")); CustomFileUtils.deleteDummyFiles(cwd, serverModpackContent.list); - ModpackUtils.selectModpack(modpackDir, link); - if (preload) { LOGGER.info("Update completed! Took: {}ms", System.currentTimeMillis() - start); - CheckAndLoadModpack(modpackDir, modpackContentFile); + CheckAndLoadModpack(modpackDir, modpackContentFile, link); } else { - finishModpackUpdate(modpackDir, modpackContentFile); + applyModpack(modpackDir, modpackContentFile, link); if (!failedDownloads.isEmpty()) { StringBuilder failedFiles = new StringBuilder(); for (var download : failedDownloads.entrySet()) { @@ -360,7 +358,10 @@ public void ModpackUpdaterMain(String link, Path modpackDir, Path modpackContent } // returns true if restart is required - private boolean finishModpackUpdate(Path modpackDir, Path modpackContentFile) throws Exception { + private boolean applyModpack(Path modpackDir, Path modpackContentFile, String link) throws Exception { + + ModpackUtils.selectModpack(modpackDir, link); + Jsons.ModpackContentFields modpackContent = ConfigTools.loadModpackContent(modpackContentFile); if (modpackContent == null) { diff --git a/loader/core/src/main/java/pl/skidam/automodpack_loader_core/client/ModpackUtils.java b/loader/core/src/main/java/pl/skidam/automodpack_loader_core/client/ModpackUtils.java index 0d499fac..274885f2 100644 --- a/loader/core/src/main/java/pl/skidam/automodpack_loader_core/client/ModpackUtils.java +++ b/loader/core/src/main/java/pl/skidam/automodpack_loader_core/client/ModpackUtils.java @@ -14,6 +14,7 @@ import java.io.*; import java.net.*; import java.nio.charset.StandardCharsets; +import java.nio.file.DirectoryNotEmptyException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -119,12 +120,13 @@ public static boolean correctFilesLocations(Path modpackDir, Jsons.ModpackConten CustomFileUtils.copyFile(runFile, modpackFile); needsReCheck = false; } else if (!modpackFileExists) { - LOGGER.error("File {} doesn't exist!?", formattedFile); + LOGGER.error("File {} doesn't exist!? If you see this please report this to the automodpack repo and attach this log https://github.com/Skidamek/AutoModpack/issues", formattedFile); + Thread.dumpStack(); } // we need to update run file and we assume that modpack file is up to date if (needsReCheck && Files.exists(runFile) && !Objects.equals(contentItem.sha1, CustomFileUtils.getHash(runFile, "sha1").orElse(null))) { - LOGGER.info("File {} is not up to date, copying from modpack", formattedFile); + LOGGER.info("Overwriting {} file to the modpack version", formattedFile); CustomFileUtils.copyFile(modpackFile, runFile); } } @@ -240,20 +242,22 @@ public static Path renameModpackDir(Jsons.ModpackContentFields serverModpackCont if (!serverModpackName.equals(installedModpackName) && !serverModpackName.isEmpty()) { - Path newModpackDir = Path.of(modpackDir.getParent() + File.separator + serverModpackName); + Path newModpackDir = modpackDir.getParent().resolve(serverModpackName); try { Files.move(modpackDir, newModpackDir, StandardCopyOption.REPLACE_EXISTING); removeModpackFromList(installedModpackName); - selectModpack(newModpackDir, installedModpackLink); LOGGER.info("Changed modpack name of {} to {}", modpackDir.getFileName().toString(), serverModpackName); - - return newModpackDir; + } catch (DirectoryNotEmptyException ignored) { } catch (IOException e) { e.printStackTrace(); } + + selectModpack(newModpackDir, installedModpackLink); + + return newModpackDir; } return modpackDir; @@ -261,9 +265,28 @@ public static Path renameModpackDir(Jsons.ModpackContentFields serverModpackCont // Returns true if value changed public static boolean selectModpack(Path modpackDirToSelect, String modpackLinkToSelect) { - String modpackToSelect = modpackDirToSelect.getFileName().toString(); + final String modpackToSelect = modpackDirToSelect.getFileName().toString(); + String selectedModpack = clientConfig.selectedModpack; String selectedModpackLink = clientConfig.installedModpacks.get(selectedModpack); + + // Save current editable files + Path selectedModpackDir = modpacksDir.resolve(selectedModpack); + Path selectedModpackContentFile = selectedModpackDir.resolve(hostModpackContentFile.getFileName()); + Jsons.ModpackContentFields modpackContent = ConfigTools.loadModpackContent(selectedModpackContentFile); + if (modpackContent != null) { + Set editableFiles = getEditableFiles(modpackContent.list); + ModpackUtils.preserveEditableFiles(selectedModpackDir, editableFiles); + } + + // Copy editable files from modpack to select + Path modpackContentFile = modpackDirToSelect.resolve(hostModpackContentFile.getFileName()); + Jsons.ModpackContentFields modpackContentToSelect = ConfigTools.loadModpackContent(modpackContentFile); + if (modpackContentToSelect != null) { + Set editableFiles = getEditableFiles(modpackContentToSelect.list); + ModpackUtils.copyPreviousEditableFiles(modpackDirToSelect, editableFiles); + } + clientConfig.selectedModpack = modpackToSelect; ConfigTools.save(clientConfigFile, clientConfig); ModpackUtils.addModpackToList(modpackToSelect, modpackLinkToSelect); @@ -468,4 +491,42 @@ public static boolean potentiallyMalicious(Jsons.ModpackContentFields serverModp return false; } + + public static void preserveEditableFiles(Path modpackDir, Set editableFiles) { + for (String file : editableFiles) { + Path path = Path.of("." + file); + if (Files.exists(path)) { + try { + CustomFileUtils.copyFile(path, Path.of(modpackDir + file)); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + public static void copyPreviousEditableFiles(Path modpackDir, Set editableFiles) { + for (String file : editableFiles) { + Path path = Path.of(modpackDir + file); + if (Files.exists(path)) { + try { + CustomFileUtils.copyFile(path, Path.of("." + file)); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + static Set getEditableFiles(Set modpackContentItems) { + Set editableFiles = new HashSet<>(); + + for (Jsons.ModpackContentFields.ModpackContentItem modpackContentItem : modpackContentItems) { + if (modpackContentItem.editable) { + editableFiles.add(modpackContentItem.file); + } + } + + return editableFiles; + } }