Skip to content

Commit f839cd1

Browse files
committed
preserve editable files only when modpack actually changes
1 parent 6301c12 commit f839cd1

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

loader/core/src/main/java/pl/skidam/automodpack_loader_core/client/ModpackUtils.java

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static boolean isUpdate(Jsons.ModpackContentFields serverModpackContent,
4242
}
4343

4444
LOGGER.info("Indexing file system...");
45+
var start = System.currentTimeMillis();
4546

4647
Set<Path> existingFileTree;
4748
try (var stream = Files.walk(modpackDir)) {
@@ -87,7 +88,8 @@ public static boolean isUpdate(Jsons.ModpackContentFields serverModpackContent,
8788
}
8889
}
8990

90-
LOGGER.info("{} is up to date!", modpackDir);
91+
var end = System.currentTimeMillis();
92+
LOGGER.info("{} is up to date! Took {} ms", modpackDir, end - start);
9193
return false;
9294
}
9395

@@ -347,33 +349,45 @@ public static Path renameModpackDir(Jsons.ModpackContentFields serverModpackCont
347349

348350
// Returns true if value changed
349351
public static boolean selectModpack(Path modpackDirToSelect, Jsons.ModpackAddresses modpackAddresses, Set<String> newDownloadedFiles) {
350-
final String modpackToSelect = modpackDirToSelect.getFileName().toString();
351-
String selectedModpack = clientConfig.selectedModpack;
352-
353-
if (selectedModpack != null && !selectedModpack.isBlank()) {
354-
// Save current editable files
355-
Path selectedModpackDir = modpacksDir.resolve(selectedModpack);
356-
Path selectedModpackContentFile = selectedModpackDir.resolve(hostModpackContentFile.getFileName());
357-
Jsons.ModpackContentFields modpackContent = ConfigTools.loadModpackContent(selectedModpackContentFile);
358-
if (modpackContent != null) {
359-
Set<String> editableFiles = getEditableFiles(modpackContent.list);
360-
ModpackUtils.preserveEditableFiles(selectedModpackDir, editableFiles, newDownloadedFiles);
361-
}
352+
String newName = modpackDirToSelect.getFileName().toString();
353+
String oldName = clientConfig.selectedModpack;
354+
355+
// If nothing changed, update list only and return early to avoid I/O.
356+
if (Objects.equals(newName, oldName)) {
357+
ModpackUtils.addModpackToList(newName, modpackAddresses);
358+
return false;
362359
}
363360

364-
// Copy editable files from modpack to select
365-
Path modpackContentFile = modpackDirToSelect.resolve(hostModpackContentFile.getFileName());
366-
Jsons.ModpackContentFields modpackContentToSelect = ConfigTools.loadModpackContent(modpackContentFile);
367-
if (modpackContentToSelect != null) {
368-
Set<String> editableFiles = getEditableFiles(modpackContentToSelect.list);
369-
ModpackUtils.copyPreviousEditableFiles(modpackDirToSelect, editableFiles, newDownloadedFiles);
361+
LOGGER.info("Preserving editable files from old modpack and copying to new modpack...");
362+
363+
// 1. Preserve files from the OLD modpack (if it existed)
364+
if (oldName != null && !oldName.isBlank()) {
365+
processEditableFiles(modpacksDir.resolve(oldName), (dir, files) ->
366+
ModpackUtils.preserveEditableFiles(dir, files, newDownloadedFiles));
370367
}
371368

372-
clientConfig.selectedModpack = modpackToSelect;
369+
// 2. Restore/Copy files to the NEW modpack
370+
processEditableFiles(modpackDirToSelect, (dir, files) ->
371+
ModpackUtils.copyPreviousEditableFiles(dir, files, newDownloadedFiles));
372+
373+
// 3. Update Configuration and Save
374+
clientConfig.selectedModpack = newName;
373375
ConfigTools.save(clientConfigFile, clientConfig);
374-
ModpackUtils.addModpackToList(modpackToSelect, modpackAddresses);
376+
ModpackUtils.addModpackToList(newName, modpackAddresses);
375377

376-
return !Objects.equals(modpackToSelect, selectedModpack);
378+
LOGGER.info("Selected modpack: {}", newName);
379+
380+
return true;
381+
}
382+
383+
private static void processEditableFiles(Path modpackDir, java.util.function.BiConsumer<Path, Set<String>> action) {
384+
Path contentFile = modpackDir.resolve(hostModpackContentFile.getFileName());
385+
Jsons.ModpackContentFields content = ConfigTools.loadModpackContent(contentFile);
386+
387+
if (content != null) {
388+
Set<String> editableFiles = getEditableFiles(content.list);
389+
action.accept(modpackDir, editableFiles);
390+
}
377391
}
378392

379393
public static void removeModpackFromList(String modpackName) {

0 commit comments

Comments
 (0)