From 2bf997e882317ef1cddf02960c7b90dc6c9bbeba Mon Sep 17 00:00:00 2001 From: coolGi Date: Fri, 13 Oct 2023 00:06:57 +1030 Subject: [PATCH 1/4] Updater now works with spaces in file paths --- .../coreapi/util/jar/DeleteOnUnlock.java | 15 +++++----- .../core/jar/updater/SelfUpdater.java | 28 ++++++++----------- 2 files changed, 20 insertions(+), 23 deletions(-) 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 index 2ff7933c3..d3ecc5967 100644 --- 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 @@ -1,6 +1,7 @@ package com.seibel.distanthorizons.coreapi.util.jar; import java.io.File; +import java.net.URLDecoder; import java.nio.file.Files; import java.util.concurrent.TimeUnit; @@ -19,13 +20,18 @@ 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 + * @param args Takes whatever the first argument is, treats it as a file, and deletes it once a process lock is lifted from it
+ * Note: This argument should also be encoded in UTF-8 */ public static void main(String[] args) { - File file = new File(args[0]); try { + File file = new File( + URLDecoder.decode(args[0], "UTF-8") + ); + + for (int i = 0; i < (60 / ((float) attemptSpeed/1000) ) * timeout; i++) { if (file.renameTo(file)) // If it is able to be renamed, then it is unlocked and can be deleted @@ -41,10 +47,5 @@ public class DeleteOnUnlock 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 still exist. So it was not able to be deleted"); } } 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 05b28ded7..7d0307781 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 @@ -19,18 +19,17 @@ package com.seibel.distanthorizons.core.jar.updater; -import com.seibel.distanthorizons.api.enums.config.EUpdateBranch; +import com.seibel.distanthorizons.core.config.Config; +import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; 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; -import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; import com.seibel.distanthorizons.core.jar.installer.ModrinthGetter; import com.seibel.distanthorizons.core.jar.installer.WebDownloader; import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; import com.seibel.distanthorizons.core.wrapperInterfaces.IVersionConstants; +import com.seibel.distanthorizons.coreapi.ModInfo; import com.seibel.distanthorizons.coreapi.util.jar.DeleteOnUnlock; import org.apache.logging.log4j.Logger; @@ -38,12 +37,9 @@ import javax.swing.*; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.net.URL; -import java.net.URLClassLoader; +import java.net.URLEncoder; 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; @@ -315,15 +311,15 @@ public class SelfUpdater } 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() +" "+ DeleteOnUnlock.class.getCanonicalName() +" "+ JarUtils.jarFile.getAbsolutePath()); + Runtime.getRuntime().exec( + "java -cp "+ + newFileLocation.getAbsolutePath() + +" "+ + DeleteOnUnlock.class.getCanonicalName() + +" "+ + URLEncoder.encode(JarUtils.jarFile.getAbsolutePath(), "UTF-8") // Encode the file location so that it doesnt have any spaces + ); } } catch (Exception e) From ae65fb8e938bd710f0a8fa915600a5332921ec71 Mon Sep 17 00:00:00 2001 From: coolGi Date: Fri, 13 Oct 2023 02:00:12 +1030 Subject: [PATCH 2/4] Attempt to find Java's binary to use for the self updater on Windows --- .../distanthorizons/core/jar/updater/SelfUpdater.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 7d0307781..d1e3b2219 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 @@ -311,9 +311,13 @@ public class SelfUpdater } else { + // Gets the Java binary + String javaHome = System.getProperty("java.home"); + String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; + // Execute the new jar, to delete the old jar once it detects the lock has been lifted Runtime.getRuntime().exec( - "java -cp "+ + javaBin +" -cp "+ newFileLocation.getAbsolutePath() +" "+ DeleteOnUnlock.class.getCanonicalName() From 7f4e5d9e58246658cba0952008e2c6abe25b200c Mon Sep 17 00:00:00 2001 From: coolGi Date: Fri, 13 Oct 2023 02:54:29 +1030 Subject: [PATCH 3/4] Surrounded the Java location with quotations so paths with spaces work --- .../seibel/distanthorizons/core/jar/updater/SelfUpdater.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d1e3b2219..a2b4aa73f 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 @@ -317,7 +317,7 @@ public class SelfUpdater // Execute the new jar, to delete the old jar once it detects the lock has been lifted Runtime.getRuntime().exec( - javaBin +" -cp "+ + "\""+ javaBin +"\" -cp "+ newFileLocation.getAbsolutePath() +" "+ DeleteOnUnlock.class.getCanonicalName() From 7ce89e9903e2abee3367d485d62e969432a91eec Mon Sep 17 00:00:00 2001 From: coolGi Date: Thu, 12 Oct 2023 18:38:56 +0000 Subject: [PATCH 4/4] Fixed yet another issue relating to spaces in the path --- .../seibel/distanthorizons/core/jar/updater/SelfUpdater.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 a2b4aa73f..a5f5d0c7f 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 @@ -317,9 +317,9 @@ public class SelfUpdater // Execute the new jar, to delete the old jar once it detects the lock has been lifted Runtime.getRuntime().exec( - "\""+ javaBin +"\" -cp "+ + "\""+ javaBin +"\" -cp \""+ newFileLocation.getAbsolutePath() - +" "+ + +"\" "+ DeleteOnUnlock.class.getCanonicalName() +" "+ URLEncoder.encode(JarUtils.jarFile.getAbsolutePath(), "UTF-8") // Encode the file location so that it doesnt have any spaces