mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-10 12:26:16 -04:00
support jar upgrading
This commit is contained in:
parent
4b435d4ac3
commit
6e685a2aa1
@ -137,6 +137,15 @@ task makeExecutable(dependsOn: jar) doLast {
|
|||||||
fos.write(bytes, 0, read);
|
fos.write(bytes, 0, read);
|
||||||
is.close()
|
is.close()
|
||||||
fos.close()
|
fos.close()
|
||||||
|
|
||||||
|
def messageDigest = MessageDigest.getInstance("SHA1")
|
||||||
|
makeExecutableoutjar.eachByte 1024 * 1024, { byte[] buf, int bytesRead ->
|
||||||
|
messageDigest.update(buf, 0, bytesRead);
|
||||||
|
}
|
||||||
|
def sha1Hex = new BigInteger(1, messageDigest.digest()).toString(16).padLeft(40, '0')
|
||||||
|
def fileEx = new File(project.buildDir, "libs/" + makeExecutableoutjar.getName().substring(0, makeExecutableoutjar.getName().length()-4)+".jar.sha1")
|
||||||
|
if (!fileEx.exists()) fileEx.createNewFile()
|
||||||
|
fileEx.append sha1Hex
|
||||||
}
|
}
|
||||||
|
|
||||||
task makePackGZ(dependsOn: jar) doLast {
|
task makePackGZ(dependsOn: jar) doLast {
|
||||||
|
@ -46,6 +46,7 @@ import org.jackhuang.hmcl.util.MessageBox;
|
|||||||
import org.jackhuang.hmcl.util.UpdateChecker;
|
import org.jackhuang.hmcl.util.UpdateChecker;
|
||||||
import org.jackhuang.hmcl.util.Utils;
|
import org.jackhuang.hmcl.util.Utils;
|
||||||
import org.jackhuang.hmcl.api.VersionNumber;
|
import org.jackhuang.hmcl.api.VersionNumber;
|
||||||
|
import org.jackhuang.hmcl.util.StrUtils;
|
||||||
import org.jackhuang.hmcl.util.sys.FileUtils;
|
import org.jackhuang.hmcl.util.sys.FileUtils;
|
||||||
import org.jackhuang.hmcl.util.sys.IOUtils;
|
import org.jackhuang.hmcl.util.sys.IOUtils;
|
||||||
import org.jackhuang.hmcl.util.sys.OS;
|
import org.jackhuang.hmcl.util.sys.OS;
|
||||||
@ -78,7 +79,7 @@ public class AppDataUpgrader extends IUpgrader {
|
|||||||
public void parseArguments(VersionNumber nowVersion, String[] args) {
|
public void parseArguments(VersionNumber nowVersion, String[] args) {
|
||||||
if (!ArrayUtils.contains(args, "--noupdate"))
|
if (!ArrayUtils.contains(args, "--noupdate"))
|
||||||
try {
|
try {
|
||||||
File f = AppDataUpgraderTask.HMCL_VER_FILE;
|
File f = AppDataUpgraderPackGzTask.HMCL_VER_FILE;
|
||||||
if (f.exists()) {
|
if (f.exists()) {
|
||||||
Map<String, String> m = C.GSON.fromJson(FileUtils.read(f), Map.class);
|
Map<String, String> m = C.GSON.fromJson(FileUtils.read(f), Map.class);
|
||||||
String s = m.get("ver");
|
String s = m.get("ver");
|
||||||
@ -103,13 +104,25 @@ public class AppDataUpgrader extends IUpgrader {
|
|||||||
if (MessageBox.show(C.i18n("update.newest_version") + version.firstVer + "." + version.secondVer + "." + version.thirdVer + "\n"
|
if (MessageBox.show(C.i18n("update.newest_version") + version.firstVer + "." + version.secondVer + "." + version.thirdVer + "\n"
|
||||||
+ C.i18n("update.should_open_link"),
|
+ C.i18n("update.should_open_link"),
|
||||||
MessageBox.YES_NO_OPTION) == MessageBox.YES_OPTION)
|
MessageBox.YES_NO_OPTION) == MessageBox.YES_OPTION)
|
||||||
if (map != null && map.containsKey("pack"))
|
if (map != null && map.containsKey("jar") && !StrUtils.isBlank(map.get("jar")))
|
||||||
|
try {
|
||||||
|
String hash = null;
|
||||||
|
if (map.containsKey("jarsha1"))
|
||||||
|
hash = map.get("jarsha1");
|
||||||
|
if (TaskWindow.factory().append(new AppDataUpgraderJarTask(map.get("jar"), version.version, hash)).execute()) {
|
||||||
|
new ProcessBuilder(new String[] { IOUtils.getJavaDir(), "-jar", AppDataUpgraderJarTask.getSelf(version.version).getAbsolutePath() }).directory(new File("").getAbsoluteFile()).start();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
HMCLog.err("Failed to create upgrader", ex);
|
||||||
|
}
|
||||||
|
else if (map != null && map.containsKey("pack") && !StrUtils.isBlank(map.get("pack")))
|
||||||
try {
|
try {
|
||||||
String hash = null;
|
String hash = null;
|
||||||
if (map.containsKey("packsha1"))
|
if (map.containsKey("packsha1"))
|
||||||
hash = map.get("packsha1");
|
hash = map.get("packsha1");
|
||||||
if (TaskWindow.factory().append(new AppDataUpgraderTask(map.get("pack"), version.version, hash)).execute()) {
|
if (TaskWindow.factory().append(new AppDataUpgraderPackGzTask(map.get("pack"), version.version, hash)).execute()) {
|
||||||
new ProcessBuilder(new String[] { IOUtils.getJavaDir(), "-jar", AppDataUpgraderTask.getSelf(version.version).getAbsolutePath() }).directory(new File("").getAbsoluteFile()).start();
|
new ProcessBuilder(new String[] { IOUtils.getJavaDir(), "-jar", AppDataUpgraderPackGzTask.getSelf(version.version).getAbsolutePath() }).directory(new File("").getAbsoluteFile()).start();
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
@ -135,7 +148,7 @@ public class AppDataUpgrader extends IUpgrader {
|
|||||||
}).execute();
|
}).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class AppDataUpgraderTask extends Task {
|
public static class AppDataUpgraderPackGzTask extends Task {
|
||||||
|
|
||||||
public static final File BASE_FOLDER = MCUtils.getWorkingDirectory("hmcl");
|
public static final File BASE_FOLDER = MCUtils.getWorkingDirectory("hmcl");
|
||||||
public static final File HMCL_VER_FILE = new File(BASE_FOLDER, "hmclver.json");
|
public static final File HMCL_VER_FILE = new File(BASE_FOLDER, "hmclver.json");
|
||||||
@ -147,7 +160,7 @@ public class AppDataUpgrader extends IUpgrader {
|
|||||||
private final String downloadLink, newestVersion, expectedHash;
|
private final String downloadLink, newestVersion, expectedHash;
|
||||||
File tempFile;
|
File tempFile;
|
||||||
|
|
||||||
public AppDataUpgraderTask(String downloadLink, String newestVersion, String hash) throws IOException {
|
public AppDataUpgraderPackGzTask(String downloadLink, String newestVersion, String hash) throws IOException {
|
||||||
this.downloadLink = downloadLink;
|
this.downloadLink = downloadLink;
|
||||||
this.newestVersion = newestVersion;
|
this.newestVersion = newestVersion;
|
||||||
this.expectedHash = hash;
|
this.expectedHash = hash;
|
||||||
@ -190,4 +203,50 @@ public class AppDataUpgrader extends IUpgrader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class AppDataUpgraderJarTask extends Task {
|
||||||
|
|
||||||
|
public static final File BASE_FOLDER = MCUtils.getWorkingDirectory("hmcl");
|
||||||
|
public static final File HMCL_VER_FILE = new File(BASE_FOLDER, "hmclver.json");
|
||||||
|
|
||||||
|
public static File getSelf(String ver) {
|
||||||
|
return new File(BASE_FOLDER, "HMCL-" + ver + ".jar");
|
||||||
|
}
|
||||||
|
|
||||||
|
private final String downloadLink, newestVersion, expectedHash;
|
||||||
|
File tempFile;
|
||||||
|
|
||||||
|
public AppDataUpgraderJarTask(String downloadLink, String newestVersion, String hash) throws IOException {
|
||||||
|
this.downloadLink = downloadLink;
|
||||||
|
this.newestVersion = newestVersion;
|
||||||
|
this.expectedHash = hash;
|
||||||
|
tempFile = File.createTempFile("hmcl", ".jar");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Task> getDependTasks() {
|
||||||
|
return Arrays.asList(new FileDownloadTask(downloadLink, tempFile, expectedHash));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeTask(boolean areDependTasksSucceeded) throws Exception {
|
||||||
|
if (!areDependTasksSucceeded) {
|
||||||
|
tempFile.delete();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
HashMap<String, String> json = new HashMap<>();
|
||||||
|
File f = getSelf(newestVersion);
|
||||||
|
FileUtils.copyFile(tempFile, f);
|
||||||
|
json.put("ver", newestVersion);
|
||||||
|
json.put("loc", f.getAbsolutePath());
|
||||||
|
String result = C.GSON.toJson(json);
|
||||||
|
FileUtils.write(HMCL_VER_FILE, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getInfo() {
|
||||||
|
return "Upgrade";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,7 +205,7 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
|||||||
|
|
||||||
// Check hash code
|
// Check hash code
|
||||||
String hashCode = String.format("%1$040x", new BigInteger(1, digest.digest()));
|
String hashCode = String.format("%1$040x", new BigInteger(1, digest.digest()));
|
||||||
if (expectedHash != null && !expectedHash.equals(hashCode))
|
if (expectedHash != null && !expectedHash.equalsIgnoreCase(hashCode))
|
||||||
throw new IllegalStateException("Unexpected hash code: " + hashCode + ", expected: " + expectedHash);
|
throw new IllegalStateException("Unexpected hash code: " + hashCode + ", expected: " + expectedHash);
|
||||||
|
|
||||||
if (ppl != null)
|
if (ppl != null)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user