Skip to content

Commit

Permalink
Editable files are now preserved between modpack switches #257
Browse files Browse the repository at this point in the history
bump to beta 12
  • Loading branch information
Skidamek committed Jul 31, 2024
1 parent 07b20b9 commit 91b30c4
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> installedModpacks = Map.of(); // modpack name, link
public Map<String, String> installedModpacks; // modpack name, link
public boolean selfUpdater = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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, '-');
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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");
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -240,30 +242,51 @@ 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;
}

// 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<String> 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<String> editableFiles = getEditableFiles(modpackContentToSelect.list);
ModpackUtils.copyPreviousEditableFiles(modpackDirToSelect, editableFiles);
}

clientConfig.selectedModpack = modpackToSelect;
ConfigTools.save(clientConfigFile, clientConfig);
ModpackUtils.addModpackToList(modpackToSelect, modpackLinkToSelect);
Expand Down Expand Up @@ -468,4 +491,42 @@ public static boolean potentiallyMalicious(Jsons.ModpackContentFields serverModp

return false;
}

public static void preserveEditableFiles(Path modpackDir, Set<String> 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<String> 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<String> getEditableFiles(Set<Jsons.ModpackContentFields.ModpackContentItem> modpackContentItems) {
Set<String> editableFiles = new HashSet<>();

for (Jsons.ModpackContentFields.ModpackContentItem modpackContentItem : modpackContentItems) {
if (modpackContentItem.editable) {
editableFiles.add(modpackContentItem.file);
}
}

return editableFiles;
}
}

0 comments on commit 91b30c4

Please sign in to comment.