Continue load old version if the newer version cannot launch

This commit is contained in:
huangyuhui
2018-06-10 00:10:17 +08:00
parent 38c6bce2c0
commit 630f164877
4 changed files with 80 additions and 86 deletions

View File

@@ -48,16 +48,16 @@ jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
manifest { manifest {
attributes 'Created-By' : 'Copyright(c) 2013-2017 huangyuhui.', attributes 'Created-By' : 'Copyright(c) 2013-2018 huangyuhui.',
'Main-Class' : mainClass, 'Main-Class' : mainClass,
'Multi-Release': "true" 'Multi-Release': "true"
} }
doLast { doLast {
new File("build/signed").mkdirs() //new File("build/signed").mkdirs()
ant.signjar(signedjar: archivePath, jar: archivePath, //ant.signjar(signedjar: archivePath, jar: archivePath,
keystore: "HMCL.keystore", storepass: "123456", // keystore: "HMCL.keystore", storepass: "123456",
alias: "HMCL") // alias: "HMCL")
def messageDigest = MessageDigest.getInstance("SHA1") def messageDigest = MessageDigest.getInstance("SHA1")
archivePath.eachByte 1024 * 1024, { byte[] buf, int bytesRead -> archivePath.eachByte 1024 * 1024, { byte[] buf, int bytesRead ->

View File

@@ -71,7 +71,7 @@ public final class Launcher extends Application {
public static void main(String[] args) { public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(CRASH_REPORTER); 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."); System.out.println("Unable to create log directory " + LOG_DIRECTORY + ", log files cannot be generated.");
try { try {
@@ -160,6 +160,7 @@ public final class Launcher extends Application {
public static final IUpgrader UPGRADER = new AppDataUpgrader(); public static final IUpgrader UPGRADER = new AppDataUpgrader();
public static final CrashReporter CRASH_REPORTER = new CrashReporter(); 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"; public static final String PUBLISH = "http://www.mcbbs.net/thread-142335-1-1.html";
} }

View File

@@ -33,10 +33,8 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.*; import java.net.*;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.jar.JarFile; import java.util.jar.JarFile;
@@ -51,30 +49,25 @@ import java.util.zip.GZIPInputStream;
*/ */
public class AppDataUpgrader extends IUpgrader { public class AppDataUpgrader extends IUpgrader {
private boolean launchNewerVersion(List<String> args, File jar) throws IOException, PrivilegedActionException { private void launchNewerVersion(List<String> args, File jar) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {
try (JarFile jarFile = new JarFile(jar)) { try (JarFile jarFile = new JarFile(jar)) {
String mainClass = jarFile.getManifest().getMainAttributes().getValue("Main-Class"); String mainClass = jarFile.getManifest().getMainAttributes().getValue("Main-Class");
if (mainClass != null) { if (mainClass == null)
throw new ClassNotFoundException("Main-Class not found in manifest");
ArrayList<String> al = new ArrayList<>(args); ArrayList<String> al = new ArrayList<>(args);
al.add("--noupdate"); al.add("--noupdate");
ClassLoader pre = Thread.currentThread().getContextClassLoader(); ClassLoader pre = Thread.currentThread().getContextClassLoader();
try { try {
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
Logging.stop(); Logging.stop();
ClassLoader now = new URLClassLoader(new URL[]{jar.toURI().toURL()}, ClassLoader.getSystemClassLoader().getParent()); ClassLoader now = new URLClassLoader(new URL[]{jar.toURI().toURL()}, ClassLoader.getSystemClassLoader().getParent());
Thread.currentThread().setContextClassLoader(now); Thread.currentThread().setContextClassLoader(now);
now.loadClass(mainClass).getMethod("main", String[].class).invoke(null, new Object[]{al.toArray(new String[0])}); now.loadClass(mainClass).getMethod("main", String[].class).invoke(null, new Object[]{al.toArray(new String[0])});
return null;
});
} finally { } finally {
Logging.start(Launcher.LOG_DIRECTORY); Logging.start(Launcher.LOG_DIRECTORY);
Thread.currentThread().setContextClassLoader(pre); Thread.currentThread().setContextClassLoader(pre);
} }
return true;
} }
} }
return false;
}
@Override @Override
public void parseArguments(VersionNumber nowVersion, List<String> args) { public void parseArguments(VersionNumber nowVersion, List<String> args) {
@@ -89,15 +82,18 @@ public class AppDataUpgrader extends IUpgrader {
String j = m.get("loc"); String j = m.get("loc");
if (j != null) { if (j != null) {
File jar = new File(j); File jar = new File(j);
if (jar.exists() && launchNewerVersion(args, jar)) if (jar.exists()) {
launchNewerVersion(args, jar);
System.exit(0); System.exit(0);
} }
} }
} }
}
} catch (JsonParseException ex) { } catch (JsonParseException ex) {
f.delete(); 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); 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,9 +105,6 @@ public class AppDataUpgrader extends IUpgrader {
checker.requestDownloadLink().then(Task.of(variables -> { checker.requestDownloadLink().then(Task.of(variables -> {
Map<String, String> map = variables.get(UpdateChecker.REQUEST_DOWNLOAD_LINK_ID); Map<String, String> 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"))) if (map != null && map.containsKey("jar") && !StringUtils.isBlank(map.get("jar")))
try { try {
String hash = null; String hash = null;
@@ -200,7 +193,7 @@ public class AppDataUpgrader extends IUpgrader {
if (!FileUtils.makeDirectory(f.getParentFile())) if (!FileUtils.makeDirectory(f.getParentFile()))
throw new IOException("Failed to make directories: " + f.getParent()); 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"); f = new File(BASE_FOLDER, "HMCL-" + newestVersion + (i > 0 ? "-" + i : "") + ".jar");
if (!f.createNewFile()) if (!f.createNewFile())
throw new IOException("Failed to create new file: " + f); throw new IOException("Failed to create new file: " + f);

View File

@@ -69,7 +69,7 @@ public final class UpdateChecker {
*/ */
public TaskResult<VersionNumber> process(final boolean showMessage) { public TaskResult<VersionNumber> process(final boolean showMessage) {
return new TaskResult<VersionNumber>() { return new TaskResult<VersionNumber>() {
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 @Override
public Collection<? extends Task> getDependents() { public Collection<? extends Task> getDependents() {
@@ -126,7 +126,7 @@ public final class UpdateChecker {
public void execute() { public void execute() {
if (download_link == null) if (download_link == null)
try { try {
download_link = Constants.GSON.<Map<String, String>>fromJson(NetworkUtils.doGet(NetworkUtils.toURL("https://huangyuhui.duapp.com/hmcl/update_link.php")), Map.class); download_link = Constants.GSON.<Map<String, String>>fromJson(NetworkUtils.doGet(NetworkUtils.toURL(Launcher.UPDATE_SERVER + "/hmcl/update_link.php")), Map.class);
} catch (JsonSyntaxException | IOException e) { } catch (JsonSyntaxException | IOException e) {
Logging.LOG.log(Level.SEVERE, "Failed to get update link.", e); Logging.LOG.log(Level.SEVERE, "Failed to get update link.", e);
} }