From 56f02af579f9f782f9bceb07f1a87994f6b24173 Mon Sep 17 00:00:00 2001 From: coolGi Date: Fri, 28 Oct 2022 10:07:46 +1030 Subject: [PATCH] Finished the self updater --- .../com/seibel/lod/core/config/Config.java | 2 +- .../core/jar/installer/ModrinthGetter.java | 7 ++-- .../lod/core/jar/updater/SelfUpdater.java | 34 ++++++++++++------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/com/seibel/lod/core/config/Config.java b/core/src/main/java/com/seibel/lod/core/config/Config.java index 7c38b4fc7..c36dcafd3 100644 --- a/core/src/main/java/com/seibel/lod/core/config/Config.java +++ b/core/src/main/java/com/seibel/lod/core/config/Config.java @@ -873,7 +873,7 @@ public class Config public static class AutoUpdater { public static ConfigEntry enableAutoUpdater = new ConfigEntry.Builder() - .set(false) // Keep this as false for development + .set(false) // Keep this as false for development but should be set to true when released .comment("Automatically checks for updates on game launch") .build(); diff --git a/core/src/main/java/com/seibel/lod/core/jar/installer/ModrinthGetter.java b/core/src/main/java/com/seibel/lod/core/jar/installer/ModrinthGetter.java index a2f2df3d6..475dae491 100644 --- a/core/src/main/java/com/seibel/lod/core/jar/installer/ModrinthGetter.java +++ b/core/src/main/java/com/seibel/lod/core/jar/installer/ModrinthGetter.java @@ -84,9 +84,10 @@ public class ModrinthGetter { } public static String getLatestShaForVersion(String mcVer) { return ((Config) - ((ArrayList) projectRelease.get(Integer.parseInt(mcVersions.indexOf(mcVer) + ".files"))) - .get(0)) + ((ArrayList) projectRelease.get( + mcVersions.indexOf(mcVer) + ).get("files")).get(0)) .get("hashes.sha1") - .toString(); + .toString(); } } diff --git a/core/src/main/java/com/seibel/lod/core/jar/updater/SelfUpdater.java b/core/src/main/java/com/seibel/lod/core/jar/updater/SelfUpdater.java index 34c7638f2..9f2ece7d0 100644 --- a/core/src/main/java/com/seibel/lod/core/jar/updater/SelfUpdater.java +++ b/core/src/main/java/com/seibel/lod/core/jar/updater/SelfUpdater.java @@ -12,7 +12,6 @@ import org.apache.logging.log4j.Logger; import java.nio.file.Files; import java.security.MessageDigest; -import java.util.Objects; /** * Used to update the mod automatically @@ -50,18 +49,10 @@ public class SelfUpdater { LOGGER.info("New version ("+ModrinthGetter.getLatestNameForVersion(mcVersion)+") of "+ ModInfo.READABLE_NAME+" is available"); if (!Config.Client.AutoUpdater.promptForUpdate.get()) { // Auto-update mod - try { - LOGGER.info("Attempting to auto update "+ModInfo.READABLE_NAME); - WebDownloader.downloadAsFile(ModrinthGetter.getLatestDownloadForVersion(mcVersion), JarUtils.jarFile.getParentFile().toPath().resolve(ModInfo.NAME+"-"+ModrinthGetter.getLatestNameForVersion(mcVersion)+".jar").toFile()); - deleteOldOnClose = true; - LOGGER.info(ModInfo.READABLE_NAME+" successfully updated. It will apply on game's relaunch"); - } catch (Exception e) { - LOGGER.info("Failed to update "+ModInfo.READABLE_NAME+" to version "+ModrinthGetter.getLatestNameForVersion(mcVersion)); - e.printStackTrace(); - } + updateMod(mcVersion); return false; - } - return false; + } // else + return true; } /** @@ -78,4 +69,23 @@ public class SelfUpdater { } } } + + public static boolean updateMod() { + return updateMod( + SingletonInjector.INSTANCE.get(IVersionConstants.class).getMinecraftVersion() + ); + } + public static boolean updateMod(String minecraftVersion) { + try { + LOGGER.info("Attempting to auto update " + ModInfo.READABLE_NAME); + WebDownloader.downloadAsFile(ModrinthGetter.getLatestDownloadForVersion(minecraftVersion), JarUtils.jarFile.getParentFile().toPath().resolve(ModInfo.NAME + "-" + ModrinthGetter.getLatestNameForVersion(minecraftVersion) + ".jar").toFile()); + deleteOldOnClose = true; + LOGGER.info(ModInfo.READABLE_NAME + " successfully updated. It will apply on game's relaunch"); + return true; + } catch (Exception e) { + LOGGER.info("Failed to update "+ModInfo.READABLE_NAME+" to version "+ModrinthGetter.getLatestNameForVersion(minecraftVersion)); + e.printStackTrace(); + return false; + } + } }