Skip to content

Commit

Permalink
try to correct modpack deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Skidamek committed Nov 16, 2024
1 parent 9cb2a2d commit b06b218
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,9 @@ private boolean applyModpack(Path modpackDir, Path modpackContentFile, String li
var dupeMods = ModpackUtils.getDupeMods(modpackDir, workaroundMods);
boolean needsRestart1 = ModpackUtils.removeDupeMods(dupeMods);

return needsRestart0 || needsRestart1;
boolean needsRestart2 = ModpackUtils.correctModpackDepsOnDefaultDir(modpackDir);

return needsRestart0 || needsRestart1 || needsRestart2;
}

private Set<String> getIgnoredFiles(Set<Jsons. ModpackContentFields. ModpackContentItem> modpackContentItems, Set<String> workaroundMods) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

import static pl.skidam.automodpack_core.config.ConfigTools.GSON;
import static pl.skidam.automodpack_core.GlobalVariables.*;
Expand Down Expand Up @@ -134,12 +135,46 @@ public static boolean correctFilesLocations(Path modpackDir, Jsons.ModpackConten
return needsRestart;
}

// Check if modpack mods are dependent on any mod in the default mods folder and if so, then check if the that dependency version is lower than required/provided by modpack mod.
// if so then force move that modpack mod to the default mods folder and delete the old one from the default mods folder.
public static boolean correctModpackDepsOnDefaultDir(Path modpackDir) throws IOException {
AtomicBoolean needsRestart = new AtomicBoolean(false);
List<LoaderService.Mod> standardModList = Files.list(MODS_DIR)
.map(LOADER_MANAGER::getMod)
.filter(Objects::nonNull)
.toList();
List<LoaderService.Mod> modpackModList = Files.list(modpackDir.resolve("mods"))
.map(LOADER_MANAGER::getMod)
.filter(Objects::nonNull)
.toList();

for (LoaderService.Mod modpackMod : modpackModList) {
for (String depId : modpackMod.dependencies()) {
standardModList.stream()
.filter(standardMod -> standardMod.modID().equals(depId) || standardMod.providesIDs().contains(depId))
.filter(standardMod -> standardMod.modVersion().compareTo(modpackMod.modVersion()) < 0)
.forEach(standardMod -> {
LOGGER.info("Moving {} mod to the default mods folder because it is dependent on {} mod and the version is lower", modpackMod.modID(), standardMod.modID());
try {
CustomFileUtils.copyFile(modpackMod.modPath(), MODS_DIR.resolve(modpackMod.modPath().getFileName()));
CustomFileUtils.forceDelete(standardMod.modPath());
needsRestart.set(true);
} catch (IOException e) {
e.printStackTrace();
}
});
}
}

return needsRestart.get();
}

// Checks if in standard mods folder are any mods that are in modpack
// Returns map of modpack mods and standard mods that have the same mod id they dont necessarily have to be the same*
public static Map<LoaderService.Mod, LoaderService.Mod> getDupeMods(Path modpackPath, Set<String> workaroundMods) throws IOException {
public static Map<LoaderService.Mod, LoaderService.Mod> getDupeMods(Path modpackDir, Set<String> workaroundMods) throws IOException {
// maybe also check subfolders...
final List<Path> standardMods = Files.list(MODS_DIR).toList();
final List<Path> modpackMods = Files.list(modpackPath.resolve("mods")).toList();
final List<Path> modpackMods = Files.list(modpackDir.resolve("mods")).toList();
final Collection<LoaderService.Mod> standardModList = standardMods.stream().map(modPath -> LOADER_MANAGER.getMod(modPath)).filter(Objects::nonNull).toList();
final Collection<LoaderService.Mod> modpackModList = modpackMods.stream().map(modPath -> LOADER_MANAGER.getMod(modPath)).filter(Objects::nonNull).toList();

Expand All @@ -150,7 +185,7 @@ public static Map<LoaderService.Mod, LoaderService.Mod> getDupeMods(Path modpack
for (LoaderService.Mod modpackMod : modpackModList) {
LoaderService.Mod standardMod = standardModList.stream().filter(mod -> mod.modID().equals(modpackMod.modID())).findFirst().orElse(null); // There might be super rare edge case if client would have for some reason more than one mod with the same mod id
if (standardMod != null) {
String formattedFile = CustomFileUtils.formatPath(modpackMod.modPath(), modpackPath);
String formattedFile = CustomFileUtils.formatPath(modpackMod.modPath(), modpackDir);
if (workaroundMods.contains(formattedFile)) continue;
duplicates.put(modpackMod, standardMod);
}
Expand Down

0 comments on commit b06b218

Please sign in to comment.