diff --git a/api/src/main/java/com/seibel/distanthorizons/coreapi/util/jar/DeleteOnUnlock.java b/api/src/main/java/com/seibel/distanthorizons/coreapi/util/jar/DeleteOnUnlock.java new file mode 100644 index 000000000..e9d80a10b --- /dev/null +++ b/api/src/main/java/com/seibel/distanthorizons/coreapi/util/jar/DeleteOnUnlock.java @@ -0,0 +1,44 @@ +package com.seibel.distanthorizons.coreapi.util.jar; + +import java.io.File; +import java.nio.file.Files; +import java.util.concurrent.TimeUnit; + +/** + * Deletes the first file in the arguments when a lock is lifted from it (for Windows)
+ * DON'T MOVE: If this class is moved, then the updater will no longer work on Windows + * + * @author coolgi + */ +public class DeleteOnUnlock +{ + /** + * @param args Takes whatever the first argument is, treats it as a file, and deletes it once a process lock is lifted from it + */ + public static void main(String[] args) + { + File file = new File(args[0]); + try + { + for (int i = 0; i < 600; i++) // As it rests for around 0.1 second each loop, this should last a minute + { + if (file.renameTo(file)) // If it is able to be renamed, then it is unlocked and can be deleted + { + Files.delete(file.toPath()); + break; + } + TimeUnit.MILLISECONDS.sleep(100); + } + } + catch (Exception e) + { + e.printStackTrace(); + throw new RuntimeException("Deletion failed"); + } + + + // If it isn't deleted by the end, crash + if (Files.exists(file.toPath())) + throw new RuntimeException("File was not able to be deleted"); + } +} diff --git a/core/src/main/java/com/seibel/distanthorizons/core/jar/JarUtils.java b/core/src/main/java/com/seibel/distanthorizons/core/jar/JarUtils.java index 0d4cc63cc..9f007adc4 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/jar/JarUtils.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/jar/JarUtils.java @@ -34,7 +34,13 @@ import java.util.Objects; */ public class JarUtils { - public static final File jarFile = new File(JarUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath()); + public static File jarFile = null; + + static { + try { + jarFile = new File(JarUtils.class.getProtectionDomain().getCodeSource().getLocation().toURI()); // Always safe + } catch (Exception e) { e.printStackTrace(); } + } /** @@ -95,7 +101,7 @@ public class JarUtils /** * Checks the checksum of a file given an algorithm * - * @param digest What algorithem to use
+ * @param digest What algorithm to use
* Eg.
* MessageDigest.getInstance("MD5")
* MessageDigest.getInstance("SHA-256")
diff --git a/core/src/main/java/com/seibel/distanthorizons/core/jar/updater/SelfUpdater.java b/core/src/main/java/com/seibel/distanthorizons/core/jar/updater/SelfUpdater.java index 885cb0659..f9d590bf3 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/jar/updater/SelfUpdater.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/jar/updater/SelfUpdater.java @@ -22,6 +22,7 @@ package com.seibel.distanthorizons.core.jar.updater; import com.seibel.distanthorizons.api.enums.config.EUpdateBranch; import com.seibel.distanthorizons.core.jar.JarUtils; import com.seibel.distanthorizons.core.jar.ModGitInfo; +import com.seibel.distanthorizons.core.jar.Platform; import com.seibel.distanthorizons.core.jar.installer.GitlabGetter; import com.seibel.distanthorizons.coreapi.ModInfo; import com.seibel.distanthorizons.core.config.Config; @@ -37,9 +38,11 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.net.URL; +import java.net.URLClassLoader; import java.nio.file.Files; import java.nio.file.Path; import java.security.MessageDigest; +import java.util.jar.JarFile; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -128,9 +131,21 @@ public class SelfUpdater return false; com.electronwill.nightconfig.core.Config pipeline = GitlabGetter.INSTANCE.projectPipelines.get(0); + if (!pipeline.get("ref").equals(ModGitInfo.Git_Main_Branch)) + { + //LOGGER.warn("Latest pipeline was found for branch ["+ pipeline.get("ref") +"], but we are on branch ["+ ModGitInfo.Git_Main_Branch +"]."); + return false; + } + + if (!pipeline.get("status").equals("success")) + { + LOGGER.warn("Pipeline for branch ["+ ModGitInfo.Git_Main_Branch +"], commit ["+ pipeline.get("id") +"], has either failed to build, or still building."); + return false; + } + if (!GitlabGetter.INSTANCE.getDownloads(pipeline.get("id")).containsKey(mcVersion)) { - LOGGER.warn("Minecraft version ["+ mcVersion +"] is not findable on Gitlab, findable versions are ["+ GitlabGetter.INSTANCE.getDownloads(pipeline.get("id")).keySet().toArray().toString() +"]"); + LOGGER.warn("Minecraft version ["+ mcVersion +"] is not findable on Gitlab, findable versions are ["+ GitlabGetter.INSTANCE.getDownloads(pipeline.get("id")).keySet().toArray().toString() +"]."); return false; } @@ -151,35 +166,8 @@ public class SelfUpdater return true; } - /** - * Should be called when the game is closed. - * This is ued to delete the previous file if it is required at the end. - */ - public static void onClose() - { - if (deleteOldOnClose) - { - try - { - Files.move(newFileLocation.toPath(), JarUtils.jarFile.getParentFile().toPath().resolve(newFileLocation.getName())); - Files.delete(newFileLocation.getParentFile().toPath()); - } - catch (Exception e) - { - LOGGER.warn("Failed to move updated fire from [" + newFileLocation.getAbsolutePath() + "] to [" + JarUtils.jarFile.getParentFile().getAbsolutePath() + "], please move it manually"); - e.printStackTrace(); - } - try - { - Files.delete(JarUtils.jarFile.toPath()); - } - catch (Exception e) - { - LOGGER.warn("Failed to delete previous " + ModInfo.READABLE_NAME + " file, please delete it manually at [" + JarUtils.jarFile + "]"); - e.printStackTrace(); - } - } - } + + public static boolean updateMod() { @@ -295,4 +283,53 @@ public class SelfUpdater return false; } } + + + + + + /** + * Should be called when the game is closed. + * This is ued to delete the previous file if it is required at the end. + */ + public static void onClose() + { + if (deleteOldOnClose) + { + try + { + Files.move(newFileLocation.toPath(), JarUtils.jarFile.getParentFile().toPath().resolve(newFileLocation.getName())); + Files.delete(newFileLocation.getParentFile().toPath()); + } + catch (Exception e) + { + LOGGER.warn("Failed to move updated fire from [" + newFileLocation.getAbsolutePath() + "] to [" + JarUtils.jarFile.getParentFile().getAbsolutePath() + "], please move it manually"); + e.printStackTrace(); + } + try + { + if (Platform.get() != Platform.WINDOWS) + { + Files.delete(JarUtils.jarFile.toPath()); + } + else + { + /* // If we want the user to delete it manually + System.setProperty("java.awt.headless", "false"); // Required to make it work + JOptionPane.showMessageDialog(null, "As you are on Windows, DH can not update fully by itself\nPlease delete ["+ JarUtils.jarFile.getAbsolutePath() +"] manually\nClick OK once ready.", ModInfo.READABLE_NAME, JOptionPane.INFORMATION_MESSAGE); + + Runtime.getRuntime().exec("explorer.exe /select," + JarUtils.jarFile.getAbsolutePath()); + */ + + // Execute the new jar, to delete the old jar once it detects the lock has been lifted + Runtime.getRuntime().exec("java -cp "+ newFileLocation.getAbsolutePath() +" com.seibel.distanthorizons.coreapi.util.jar.DeleteOnUnlock "+ JarUtils.jarFile.getAbsolutePath()); + } + } + catch (Exception e) + { + LOGGER.warn("Failed to delete previous " + ModInfo.READABLE_NAME + " file, please delete it manually at [" + JarUtils.jarFile + "]"); + e.printStackTrace(); + } + } + } } diff --git a/core/src/main/resources/assets/distanthorizons/textures/gui/button.png b/core/src/main/resources/assets/distanthorizons/textures/gui/button.png index b30919388..ed4a3a871 100644 Binary files a/core/src/main/resources/assets/distanthorizons/textures/gui/button.png and b/core/src/main/resources/assets/distanthorizons/textures/gui/button.png differ diff --git a/core/src/main/resources/assets/distanthorizons/textures/gui/changelog.png b/core/src/main/resources/assets/distanthorizons/textures/gui/changelog.png index 09e3e8614..bd7c50d0d 100644 Binary files a/core/src/main/resources/assets/distanthorizons/textures/gui/changelog.png and b/core/src/main/resources/assets/distanthorizons/textures/gui/changelog.png differ diff --git a/core/src/main/resources/assets/distanthorizons/textures/gui/changelog_button_icon.png b/core/src/main/resources/assets/distanthorizons/textures/gui/changelog_button_icon.png deleted file mode 100644 index bd7c50d0d..000000000 Binary files a/core/src/main/resources/assets/distanthorizons/textures/gui/changelog_button_icon.png and /dev/null differ diff --git a/core/src/main/resources/assets/distanthorizons/textures/gui/options_button_icon.png b/core/src/main/resources/assets/distanthorizons/textures/gui/options_button_icon.png deleted file mode 100644 index ed4a3a871..000000000 Binary files a/core/src/main/resources/assets/distanthorizons/textures/gui/options_button_icon.png and /dev/null differ diff --git a/core/src/main/resources/assets/distanthorizons/textures/gui/raw_button_background.png b/core/src/main/resources/assets/distanthorizons/textures/gui/raw_button_background.png deleted file mode 100644 index 2aa2a826c..000000000 Binary files a/core/src/main/resources/assets/distanthorizons/textures/gui/raw_button_background.png and /dev/null differ