|
| 1 | +package com.dazednconfused.catalauncher.mod; |
| 2 | + |
| 3 | + |
| 4 | +import static com.dazednconfused.catalauncher.helper.Constants.CUSTOM_MODS_DIR; |
| 5 | + |
| 6 | +import io.vavr.control.Try; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.function.Consumer; |
| 14 | +import java.util.stream.Collectors; |
| 15 | + |
| 16 | +import org.apache.commons.io.FileUtils; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +public class ModManager { |
| 21 | + |
| 22 | + private static final Logger LOGGER = LoggerFactory.getLogger(ModManager.class); |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns all mods currently found in {@link com.dazednconfused.catalauncher.helper.Constants#CUSTOM_MODS_DIR}. |
| 26 | + * */ |
| 27 | + public static List<File> listAllMods() { |
| 28 | + LOGGER.debug("Listing all mods..."); |
| 29 | + return Arrays.stream(Objects.requireNonNull(getModsFolder().listFiles())) |
| 30 | + .filter(file -> !file.getName().equals(".DS_Store")) |
| 31 | + .collect(Collectors.toList()); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Deletes given {@code toBeDeleted} mod. |
| 36 | + * */ |
| 37 | + public static void deleteMod(File toBeDeleted) { |
| 38 | + LOGGER.info("Deleting mod [{}]...", toBeDeleted); |
| 39 | + Try.run(() -> FileUtils.deleteDirectory(toBeDeleted)).onFailure(t -> LOGGER.error("There was an error deleting mod [{}]", toBeDeleted, t)); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Installs given {@code toBeInstalled} mod inside {@link com.dazednconfused.catalauncher.helper.Constants#CUSTOM_MODS_DIR}. |
| 44 | + * */ |
| 45 | + public static void installMod(File toBeInstalled, Consumer<Path> onDoneCallback) { |
| 46 | + LOGGER.info("Installing mod [{}]...", toBeInstalled); |
| 47 | + File installInto = new File(getModsFolder().getPath() + "/" + toBeInstalled.getName()); |
| 48 | + |
| 49 | + Try.run(() -> { |
| 50 | + LOGGER.debug("Copying [{}] into [{}]...", toBeInstalled, installInto); |
| 51 | + FileUtils.copyDirectory(toBeInstalled, installInto); |
| 52 | + }).onFailure(t -> LOGGER.error("There was an error installing mod [{}]", toBeInstalled, t)).andThen(() -> onDoneCallback.accept(installInto.toPath())); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Retrieves the {@link com.dazednconfused.catalauncher.helper.Constants#CUSTOM_MODS_DIR} as a {@link File}. |
| 57 | + * */ |
| 58 | + private static File getModsFolder() { |
| 59 | + File modsPath = new File(CUSTOM_MODS_DIR); |
| 60 | + if (!modsPath.exists()) { |
| 61 | + LOGGER.debug("Mods folder [{}] not found. Creating...", modsPath); |
| 62 | + Try.of(modsPath::mkdirs).onFailure(t -> LOGGER.error("Could not create mods destination folder [{}]", modsPath, t)); |
| 63 | + } |
| 64 | + |
| 65 | + return modsPath; |
| 66 | + } |
| 67 | +} |
0 commit comments