diff --git a/launcher/src/main/java/com/skcraft/launcher/install/FileVerify.java b/launcher/src/main/java/com/skcraft/launcher/install/FileVerify.java new file mode 100644 index 0000000..d8a313a --- /dev/null +++ b/launcher/src/main/java/com/skcraft/launcher/install/FileVerify.java @@ -0,0 +1,42 @@ +package com.skcraft.launcher.install; + +import com.skcraft.launcher.Launcher; +import com.skcraft.launcher.LauncherException; +import com.skcraft.launcher.util.FileUtils; +import lombok.RequiredArgsConstructor; +import lombok.extern.java.Log; + +import java.io.File; + +@RequiredArgsConstructor +@Log +public class FileVerify implements InstallTask { + private final File target; + private final String name; + private final String hash; + + @Override + public void execute(Launcher launcher) throws Exception { + log.info("Verifying file " + name); + + String actualHash = FileUtils.getShaHash(target); + if (!actualHash.equals(hash)) { + String message = String.format( + "File %s (%s) is corrupt (invalid hash)\n" + + "Expected '%s'\nGot '%s'", + name, target.getAbsolutePath(), hash, actualHash); + + throw new LauncherException(message, message); + } + } + + @Override + public double getProgress() { + return -1; + } + + @Override + public String getStatus() { + return "Verifying " + name; + } +} diff --git a/launcher/src/main/java/com/skcraft/launcher/model/minecraft/Library.java b/launcher/src/main/java/com/skcraft/launcher/model/minecraft/Library.java index 5ebc43c..8c48832 100644 --- a/launcher/src/main/java/com/skcraft/launcher/model/minecraft/Library.java +++ b/launcher/src/main/java/com/skcraft/launcher/model/minecraft/Library.java @@ -145,6 +145,7 @@ public class Library { private String path; private String url; private String sha1; + private int size; } @Data diff --git a/launcher/src/main/java/com/skcraft/launcher/update/BaseUpdater.java b/launcher/src/main/java/com/skcraft/launcher/update/BaseUpdater.java index 2af4d07..81a939b 100644 --- a/launcher/src/main/java/com/skcraft/launcher/update/BaseUpdater.java +++ b/launcher/src/main/java/com/skcraft/launcher/update/BaseUpdater.java @@ -147,14 +147,19 @@ public abstract class BaseUpdater { } protected void installJar(@NonNull Installer installer, + @NonNull VersionManifest.Artifact artifact, @NonNull File jarFile, @NonNull URL url) throws InterruptedException { // If the JAR does not exist, install it if (!jarFile.exists()) { - List targets = new ArrayList(); + long size = artifact.getSize(); + if (size <= 0) size = JAR_SIZE_ESTIMATE; - File tempFile = installer.getDownloader().download(url, "", JAR_SIZE_ESTIMATE, jarFile.getName()); + File tempFile = installer.getDownloader().download(url, "", size, jarFile.getName()); installer.queue(new FileMover(tempFile, jarFile)); + if (artifact.getHash() != null) { + installer.queue(new FileVerify(jarFile, jarFile.getName(), artifact.getHash())); + } log.info("Installing " + jarFile.getName() + " from " + url); } } @@ -217,7 +222,12 @@ public abstract class BaseUpdater { if (library.matches(environment)) { checkInterrupted(); - String path = library.getPath(environment); + Library.Artifact artifact = library.getArtifact(environment); + String path = artifact.getPath(); + + long size = artifact.getSize(); + if (size <= 0) size = LIBRARY_SIZE_ESTIMATE; + File targetFile = new File(librariesDir, path); if (!targetFile.exists()) { @@ -230,10 +240,13 @@ public abstract class BaseUpdater { } } - File tempFile = installer.getDownloader().download(urls, "", LIBRARY_SIZE_ESTIMATE, + File tempFile = installer.getDownloader().download(urls, "", size, library.getName() + ".jar"); log.info("Fetching " + path + " from " + urls); - installer.queue(new FileMover( tempFile, targetFile)); + installer.queue(new FileMover(tempFile, targetFile)); + if (artifact.getSha1() != null) { + installer.queue(new FileVerify(targetFile, library.getName(), artifact.getSha1())); + } } } } diff --git a/launcher/src/main/java/com/skcraft/launcher/update/Updater.java b/launcher/src/main/java/com/skcraft/launcher/update/Updater.java index 083dfde..745c20a 100644 --- a/launcher/src/main/java/com/skcraft/launcher/update/Updater.java +++ b/launcher/src/main/java/com/skcraft/launcher/update/Updater.java @@ -176,9 +176,10 @@ public class Updater extends BaseUpdater implements Callable, Progress // Install the .jar File jarPath = launcher.getJarPath(version); - URL jarSource = url(version.getDownloads().get("client").getUrl()); + VersionManifest.Artifact clientJar = version.getDownloads().get("client"); + URL jarSource = url(clientJar.getUrl()); log.info("JAR at " + jarPath.getAbsolutePath() + ", fetched from " + jarSource); - installJar(installer, jarPath, jarSource); + installJar(installer, clientJar, jarPath, jarSource); // Download libraries log.info("Enumerating libraries to download...");