Continue load old version if the newer version cannot launch
This commit is contained in:
@@ -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 ->
|
||||||
|
|||||||
@@ -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";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,29 +49,24 @@ 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)
|
||||||
ArrayList<String> al = new ArrayList<>(args);
|
throw new ClassNotFoundException("Main-Class not found in manifest");
|
||||||
al.add("--noupdate");
|
ArrayList<String> al = new ArrayList<>(args);
|
||||||
ClassLoader pre = Thread.currentThread().getContextClassLoader();
|
al.add("--noupdate");
|
||||||
try {
|
ClassLoader pre = Thread.currentThread().getContextClassLoader();
|
||||||
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
|
try {
|
||||||
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 {
|
||||||
});
|
Logging.start(Launcher.LOG_DIRECTORY);
|
||||||
} finally {
|
Thread.currentThread().setContextClassLoader(pre);
|
||||||
Logging.start(Launcher.LOG_DIRECTORY);
|
|
||||||
Thread.currentThread().setContextClassLoader(pre);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -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,60 +105,57 @@ 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"
|
if (map != null && map.containsKey("jar") && !StringUtils.isBlank(map.get("jar")))
|
||||||
+ Launcher.i18n("update.should_open_link"),
|
try {
|
||||||
MessageBox.YES_NO_OPTION) == MessageBox.YES_OPTION)
|
String hash = null;
|
||||||
if (map != null && map.containsKey("jar") && !StringUtils.isBlank(map.get("jar")))
|
if (map.containsKey("jarsha1"))
|
||||||
try {
|
hash = map.get("jarsha1");
|
||||||
String hash = null;
|
Task task = new AppDataUpgraderJarTask(NetworkUtils.toURL(map.get("jar")), version.toString(), hash);
|
||||||
if (map.containsKey("jarsha1"))
|
TaskExecutor executor = task.executor();
|
||||||
hash = map.get("jarsha1");
|
AtomicReference<Region> region = new AtomicReference<>();
|
||||||
Task task = new AppDataUpgraderJarTask(NetworkUtils.toURL(map.get("jar")), version.toString(), hash);
|
JFXUtilities.runInFX(() -> region.set(Controllers.taskDialog(executor, Launcher.i18n("message.downloading"), "", null)));
|
||||||
TaskExecutor executor = task.executor();
|
if (executor.test()) {
|
||||||
AtomicReference<Region> region = new AtomicReference<>();
|
new ProcessBuilder(JavaVersion.fromCurrentEnvironment().getBinary().getAbsolutePath(), "-jar", AppDataUpgraderJarTask.getSelf(version.toString()).getAbsolutePath())
|
||||||
JFXUtilities.runInFX(() -> region.set(Controllers.taskDialog(executor, Launcher.i18n("message.downloading"), "", null)));
|
.directory(new File("").getAbsoluteFile()).start();
|
||||||
if (executor.test()) {
|
System.exit(0);
|
||||||
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> 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"));
|
|
||||||
}
|
}
|
||||||
|
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> 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();
|
})).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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);
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user