diff --git a/HMCL/build.gradle b/HMCL/build.gradle index 673386f29..9ac87a913 100644 --- a/HMCL/build.gradle +++ b/HMCL/build.gradle @@ -48,16 +48,16 @@ jar { from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } manifest { - attributes 'Created-By' : 'Copyright(c) 2013-2017 huangyuhui.', + attributes 'Created-By' : 'Copyright(c) 2013-2018 huangyuhui.', 'Main-Class' : mainClass, 'Multi-Release': "true" } doLast { - new File("build/signed").mkdirs() - ant.signjar(signedjar: archivePath, jar: archivePath, - keystore: "HMCL.keystore", storepass: "123456", - alias: "HMCL") + //new File("build/signed").mkdirs() + //ant.signjar(signedjar: archivePath, jar: archivePath, + // keystore: "HMCL.keystore", storepass: "123456", + // alias: "HMCL") def messageDigest = MessageDigest.getInstance("SHA1") archivePath.eachByte 1024 * 1024, { byte[] buf, int bytesRead -> diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java b/HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java index a78d93358..7b36c739f 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java @@ -71,7 +71,7 @@ public final class Launcher extends Application { public static void main(String[] args) { Thread.setDefaultUncaughtExceptionHandler(CRASH_REPORTER); - if (!LOG_DIRECTORY.mkdirs()) + if (!FileUtils.makeDirectory(LOG_DIRECTORY)) System.out.println("Unable to create log directory " + LOG_DIRECTORY + ", log files cannot be generated."); try { @@ -160,6 +160,7 @@ public final class Launcher extends Application { public static final IUpgrader UPGRADER = new AppDataUpgrader(); public static final CrashReporter CRASH_REPORTER = new CrashReporter(); - public static final String CONTACT = "http://huangyuhui.duapp.com/hmcl.php"; + public static final String UPDATE_SERVER = "http://47.101.47.110"; + public static final String CONTACT = UPDATE_SERVER + "/hmcl.php"; public static final String PUBLISH = "http://www.mcbbs.net/thread-142335-1-1.html"; } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/upgrade/AppDataUpgrader.java b/HMCL/src/main/java/org/jackhuang/hmcl/upgrade/AppDataUpgrader.java index 3b15ff533..1bb60a961 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/upgrade/AppDataUpgrader.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/upgrade/AppDataUpgrader.java @@ -33,10 +33,8 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.net.*; -import java.security.AccessController; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; import java.util.*; import java.util.concurrent.atomic.AtomicReference; import java.util.jar.JarFile; @@ -51,29 +49,24 @@ import java.util.zip.GZIPInputStream; */ public class AppDataUpgrader extends IUpgrader { - private boolean launchNewerVersion(List args, File jar) throws IOException, PrivilegedActionException { + private void launchNewerVersion(List args, File jar) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException { try (JarFile jarFile = new JarFile(jar)) { String mainClass = jarFile.getManifest().getMainAttributes().getValue("Main-Class"); - if (mainClass != null) { - ArrayList al = new ArrayList<>(args); - al.add("--noupdate"); - ClassLoader pre = Thread.currentThread().getContextClassLoader(); - try { - AccessController.doPrivileged((PrivilegedExceptionAction) () -> { - Logging.stop(); - ClassLoader now = new URLClassLoader(new URL[]{jar.toURI().toURL()}, ClassLoader.getSystemClassLoader().getParent()); - Thread.currentThread().setContextClassLoader(now); - now.loadClass(mainClass).getMethod("main", String[].class).invoke(null, new Object[]{al.toArray(new String[0])}); - return null; - }); - } finally { - Logging.start(Launcher.LOG_DIRECTORY); - Thread.currentThread().setContextClassLoader(pre); - } - return true; + if (mainClass == null) + throw new ClassNotFoundException("Main-Class not found in manifest"); + ArrayList al = new ArrayList<>(args); + al.add("--noupdate"); + ClassLoader pre = Thread.currentThread().getContextClassLoader(); + try { + Logging.stop(); + ClassLoader now = new URLClassLoader(new URL[]{jar.toURI().toURL()}, ClassLoader.getSystemClassLoader().getParent()); + Thread.currentThread().setContextClassLoader(now); + now.loadClass(mainClass).getMethod("main", String[].class).invoke(null, new Object[]{al.toArray(new String[0])}); + } finally { + Logging.start(Launcher.LOG_DIRECTORY); + Thread.currentThread().setContextClassLoader(pre); } } - return false; } @Override @@ -89,15 +82,18 @@ public class AppDataUpgrader extends IUpgrader { String j = m.get("loc"); if (j != null) { File jar = new File(j); - if (jar.exists() && launchNewerVersion(args, jar)) + if (jar.exists()) { + launchNewerVersion(args, jar); System.exit(0); + } } } } } catch (JsonParseException ex) { f.delete(); - } catch (IOException | PrivilegedActionException t) { + } catch (IOException | NoSuchMethodException | SecurityException | InvocationTargetException | IllegalAccessException | ClassNotFoundException t) { Logging.LOG.log(Level.SEVERE, "Unable to execute newer version application", t); + AppDataUpgraderPackGzTask.HMCL_VER_FILE.delete(); // delete version json, let HMCL re-download the newer version. } } @@ -109,60 +105,57 @@ public class AppDataUpgrader extends IUpgrader { checker.requestDownloadLink().then(Task.of(variables -> { Map map = variables.get(UpdateChecker.REQUEST_DOWNLOAD_LINK_ID); - if (MessageBox.confirm(Launcher.i18n("update.newest_version", version.toString()) + "\n" - + Launcher.i18n("update.should_open_link"), - MessageBox.YES_NO_OPTION) == MessageBox.YES_OPTION) - if (map != null && map.containsKey("jar") && !StringUtils.isBlank(map.get("jar"))) - try { - String hash = null; - if (map.containsKey("jarsha1")) - hash = map.get("jarsha1"); - Task task = new AppDataUpgraderJarTask(NetworkUtils.toURL(map.get("jar")), version.toString(), hash); - TaskExecutor executor = task.executor(); - AtomicReference region = new AtomicReference<>(); - JFXUtilities.runInFX(() -> region.set(Controllers.taskDialog(executor, Launcher.i18n("message.downloading"), "", null))); - if (executor.test()) { - new ProcessBuilder(JavaVersion.fromCurrentEnvironment().getBinary().getAbsolutePath(), "-jar", AppDataUpgraderJarTask.getSelf(version.toString()).getAbsolutePath()) - .directory(new File("").getAbsoluteFile()).start(); - System.exit(0); - } - JFXUtilities.runInFX(() -> Controllers.closeDialog(region.get())); - } catch (IOException ex) { - Logging.LOG.log(Level.SEVERE, "Failed to create upgrader", ex); - } - else if (map != null && map.containsKey("pack") && !StringUtils.isBlank(map.get("pack"))) - try { - String hash = null; - if (map.containsKey("packsha1")) - hash = map.get("packsha1"); - Task task = new AppDataUpgraderPackGzTask(NetworkUtils.toURL(map.get("pack")), version.toString(), hash); - TaskExecutor executor = task.executor(); - AtomicReference region = new AtomicReference<>(); - JFXUtilities.runInFX(() -> region.set(Controllers.taskDialog(executor, Launcher.i18n("message.downloading"), "", null))); - if (executor.test()) { - new ProcessBuilder(JavaVersion.fromCurrentEnvironment().getBinary().getAbsolutePath(), "-jar", AppDataUpgraderPackGzTask.getSelf(version.toString()).getAbsolutePath()) - .directory(new File("").getAbsoluteFile()).start(); - System.exit(0); - } - JFXUtilities.runInFX(() -> Controllers.closeDialog(region.get())); - } catch (IOException ex) { - Logging.LOG.log(Level.SEVERE, "Failed to create upgrader", ex); - } - else { - String url = Launcher.PUBLISH; - if (map != null) - if (map.containsKey(OperatingSystem.CURRENT_OS.getCheckedName())) - url = map.get(OperatingSystem.CURRENT_OS.getCheckedName()); - else if (map.containsKey(OperatingSystem.UNKNOWN.getCheckedName())) - url = map.get(OperatingSystem.UNKNOWN.getCheckedName()); - try { - java.awt.Desktop.getDesktop().browse(new URI(url)); - } catch (URISyntaxException | IOException e) { - Logging.LOG.log(Level.SEVERE, "Failed to browse uri: " + url, e); - OperatingSystem.setClipboard(url); - MessageBox.show(Launcher.i18n("update.no_browser")); + if (map != null && map.containsKey("jar") && !StringUtils.isBlank(map.get("jar"))) + try { + String hash = null; + if (map.containsKey("jarsha1")) + hash = map.get("jarsha1"); + Task task = new AppDataUpgraderJarTask(NetworkUtils.toURL(map.get("jar")), version.toString(), hash); + TaskExecutor executor = task.executor(); + AtomicReference region = new AtomicReference<>(); + JFXUtilities.runInFX(() -> region.set(Controllers.taskDialog(executor, Launcher.i18n("message.downloading"), "", null))); + if (executor.test()) { + new ProcessBuilder(JavaVersion.fromCurrentEnvironment().getBinary().getAbsolutePath(), "-jar", AppDataUpgraderJarTask.getSelf(version.toString()).getAbsolutePath()) + .directory(new File("").getAbsoluteFile()).start(); + System.exit(0); } + JFXUtilities.runInFX(() -> Controllers.closeDialog(region.get())); + } catch (IOException ex) { + Logging.LOG.log(Level.SEVERE, "Failed to create upgrader", ex); } + else if (map != null && map.containsKey("pack") && !StringUtils.isBlank(map.get("pack"))) + try { + String hash = null; + if (map.containsKey("packsha1")) + hash = map.get("packsha1"); + Task task = new AppDataUpgraderPackGzTask(NetworkUtils.toURL(map.get("pack")), version.toString(), hash); + TaskExecutor executor = task.executor(); + AtomicReference region = new AtomicReference<>(); + JFXUtilities.runInFX(() -> region.set(Controllers.taskDialog(executor, Launcher.i18n("message.downloading"), "", null))); + if (executor.test()) { + new ProcessBuilder(JavaVersion.fromCurrentEnvironment().getBinary().getAbsolutePath(), "-jar", AppDataUpgraderPackGzTask.getSelf(version.toString()).getAbsolutePath()) + .directory(new File("").getAbsoluteFile()).start(); + System.exit(0); + } + JFXUtilities.runInFX(() -> Controllers.closeDialog(region.get())); + } catch (IOException ex) { + Logging.LOG.log(Level.SEVERE, "Failed to create upgrader", ex); + } + else { + String url = Launcher.PUBLISH; + if (map != null) + if (map.containsKey(OperatingSystem.CURRENT_OS.getCheckedName())) + url = map.get(OperatingSystem.CURRENT_OS.getCheckedName()); + else if (map.containsKey(OperatingSystem.UNKNOWN.getCheckedName())) + url = map.get(OperatingSystem.UNKNOWN.getCheckedName()); + try { + java.awt.Desktop.getDesktop().browse(new URI(url)); + } catch (URISyntaxException | IOException e) { + Logging.LOG.log(Level.SEVERE, "Failed to browse uri: " + url, e); + OperatingSystem.setClipboard(url); + MessageBox.show(Launcher.i18n("update.no_browser")); + } + } })).start(); } @@ -200,7 +193,7 @@ public class AppDataUpgrader extends IUpgrader { if (!FileUtils.makeDirectory(f.getParentFile())) throw new IOException("Failed to make directories: " + f.getParent()); - for (int i = 0; f.exists(); i++) + for (int i = 0; f.exists() && !f.delete(); i++) f = new File(BASE_FOLDER, "HMCL-" + newestVersion + (i > 0 ? "-" + i : "") + ".jar"); if (!f.createNewFile()) throw new IOException("Failed to create new file: " + f); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/upgrade/UpdateChecker.java b/HMCL/src/main/java/org/jackhuang/hmcl/upgrade/UpdateChecker.java index 5c39949a7..658b1b8ac 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/upgrade/UpdateChecker.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/upgrade/UpdateChecker.java @@ -69,7 +69,7 @@ public final class UpdateChecker { */ public TaskResult process(final boolean showMessage) { return new TaskResult() { - GetTask http = new GetTask(NetworkUtils.toURL("https://huangyuhui.duapp.com/hmcl/update.php?version=" + Launcher.VERSION)); + GetTask http = new GetTask(NetworkUtils.toURL(Launcher.UPDATE_SERVER + "/hmcl/update.php?version=" + Launcher.VERSION)); @Override public Collection getDependents() { @@ -126,7 +126,7 @@ public final class UpdateChecker { public void execute() { if (download_link == null) try { - download_link = Constants.GSON.>fromJson(NetworkUtils.doGet(NetworkUtils.toURL("https://huangyuhui.duapp.com/hmcl/update_link.php")), Map.class); + download_link = Constants.GSON.>fromJson(NetworkUtils.doGet(NetworkUtils.toURL(Launcher.UPDATE_SERVER + "/hmcl/update_link.php")), Map.class); } catch (JsonSyntaxException | IOException e) { Logging.LOG.log(Level.SEVERE, "Failed to get update link.", e); }