Search in sources :

Example 6 with DownloadException

use of net.technicpack.launchercore.exception.DownloadException in project LauncherV3 by TechnicPack.

the class HandleVersionFileTask method runTask.

@Override
public void runTask(InstallTasksQueue queue) throws IOException, InterruptedException {
    MojangVersion version = versionBuilder.buildVersionFromKey(null);
    if (version == null) {
        throw new DownloadException("The version.json file was invalid.");
    }
    // if MC < 1.6, we inject LegacyWrapper
    // HACK
    boolean isLegacy = MojangUtils.isLegacyVersion(version.getId());
    if (isLegacy) {
        Library legacyWrapper = new Library();
        legacyWrapper.setName("net.technicpack:legacywrapper:1.2.1");
        legacyWrapper.setUrl("https://mirror.technicpack.net/Technic/lib/");
        version.addLibrary(legacyWrapper);
        version.setMainClass("net.technicpack.legacywrapper.Launch");
    }
    // In Forge 1.13+ and 1.12.2 > 2847, there's an installer jar, and the universal jar can't be used since it
    // doesn't have the required dependencies to actually launch the game.
    // So, for Forge 1.13+ we need to use ForgeWrapper to install Forge (it performs deobfuscation at install time).
    // For Forge 1.12.2 it launches directly, as long as we inject the universal jar (it won't launch at all if
    // you try running it through ForgeWrapper).
    final boolean hasModernForge = MojangUtils.hasModernForge(version);
    if (hasModernForge) {
        File profileJson = new File(pack.getBinDir(), "install_profile.json");
        ZipFileRetriever zipVersionRetriever = new ZipFileRetriever(new File(pack.getBinDir(), "modpack.jar"));
        MojangVersion profileVersion = new FileVersionBuilder(profileJson, zipVersionRetriever, null).buildVersionFromKey("install_profile");
        final String[] versionIdParts = version.getId().split("-", 3);
        final boolean is1_12_2 = versionIdParts[0].equals("1.12.2");
        for (Library library : profileVersion.getLibrariesForOS()) {
            if (library.isForge()) {
                // For Forge 1.12.2 > 2847, we have to inject the universal jar as a dependency
                if (is1_12_2) {
                    library.setName(library.getName() + ":universal");
                    library.setUrl("https://files.minecraftforge.net/maven/");
                    // Add the mutated library
                    version.addLibrary(library);
                    checkLibraryQueue.addTask(new InstallVersionLibTask(library, checkNonMavenLibsQueue, downloadLibraryQueue, copyLibraryQueue, pack, directories));
                }
                // We normally skip Forge because Forge specifies itself as a dependency, for some reason
                continue;
            }
            checkLibraryQueue.addTask(new InstallVersionLibTask(library, checkNonMavenLibsQueue, downloadLibraryQueue, copyLibraryQueue, pack, directories));
        }
        // For Forge 1.13+, we inject our ForgeWrapper as a dependency and launch it through that
        if (!is1_12_2) {
            Library forgeWrapper = new Library();
            // TODO: add hash validation
            forgeWrapper.setName("io.github.zekerzhayard:ForgeWrapper:1.5.1");
            version.addLibrary(forgeWrapper);
            version.setMainClass("io.github.zekerzhayard.forgewrapper.installer.Main");
            for (Library library : version.getLibrariesForOS()) {
                if (library.isForge()) {
                    Library forgeLauncher = new Library();
                    forgeLauncher.setName(library.getName() + ":launcher");
                    forgeLauncher.setUrl("https://files.minecraftforge.net/maven/");
                    version.addLibrary(forgeLauncher);
                    checkLibraryQueue.addTask(new InstallVersionLibTask(forgeLauncher, checkNonMavenLibsQueue, downloadLibraryQueue, copyLibraryQueue, pack, directories));
                    Library forgeUniversal = new Library();
                    forgeUniversal.setName(library.getName() + ":universal");
                    forgeUniversal.setUrl("https://files.minecraftforge.net/maven/");
                    checkLibraryQueue.addTask(new InstallVersionLibTask(forgeUniversal, checkNonMavenLibsQueue, downloadLibraryQueue, copyLibraryQueue, pack, directories));
                    break;
                }
            }
        }
    }
    for (Library library : version.getLibrariesForOS()) {
        // or at least only do it if the users are sticking with modpack.jar
        if (library.isForge()) {
            continue;
        }
        if (isLegacy && library.getName().startsWith("net.minecraft:launchwrapper")) {
            continue;
        }
        // And version 2.16.0 for >= 1.12
        if (library.isLog4j()) {
            final String[] libNameParts = library.getName().split(":");
            // org.apache.logging.log4j:log4j-core:2.0-beta9
            // org.apache.logging.log4j    log4j-core        2.0-beta9
            // Determine what version we need
            String log4jVersion = "2.16.0";
            if (libNameParts[2].equals("2.0-beta9")) {
                log4jVersion = "2.0-beta9-fixed";
            }
            String sha1;
            int size;
            String artifactName = libNameParts[1];
            switch(log4jVersion) {
                case "2.16.0":
                    switch(artifactName) {
                        case "log4j-api":
                            sha1 = "f821a18687126c2e2f227038f540e7953ad2cc8c";
                            size = 301892;
                            break;
                        case "log4j-core":
                            sha1 = "539a445388aee52108700f26d9644989e7916e7c";
                            size = 1789565;
                            break;
                        case "log4j-slf4j18-impl":
                            sha1 = "0c880a059056df5725f5d8d1035276d9749eba6d";
                            size = 21249;
                            break;
                        default:
                            throw new RuntimeException("Unknown log4j artifact " + artifactName + ", cannot continue");
                    }
                    break;
                case "2.0-beta9-fixed":
                    switch(artifactName) {
                        case "log4j-api":
                            sha1 = "b61eaf2e64d8b0277e188262a8b771bbfa1502b3";
                            size = 107347;
                            break;
                        case "log4j-core":
                            sha1 = "677991ea2d7426f76309a73739cecf609679492c";
                            size = 677588;
                            break;
                        default:
                            throw new RuntimeException("Unknown log4j artifact " + artifactName + ", cannot continue");
                    }
                    break;
                default:
                    throw new RuntimeException("Unknown log4j version " + log4jVersion + ", cannot continue");
            }
            String url = String.format("https://mirror.technicpack.net/Technic/lib/org/apache/logging/log4j/%1$s/%2$s/%1$s-%2$s.jar", artifactName, log4jVersion);
            Library fixedLog4j = new Library();
            fixedLog4j.setName("org.apache.logging.log4j:" + artifactName + ":" + log4jVersion);
            Artifact artifact = new Artifact(url, sha1, size);
            Downloads downloads = new Downloads();
            downloads.setArtifact(artifact);
            fixedLog4j.setDownloads(downloads);
            // Add fixed lib
            version.addLibrary(fixedLog4j);
            checkLibraryQueue.addTask(new InstallVersionLibTask(fixedLog4j, checkNonMavenLibsQueue, downloadLibraryQueue, copyLibraryQueue, pack, directories));
            // Remove unpatched lib
            version.removeLibrary(library.getName());
            continue;
        }
        checkLibraryQueue.addTask(new InstallVersionLibTask(library, checkNonMavenLibsQueue, downloadLibraryQueue, copyLibraryQueue, pack, directories));
    }
    queue.setMetadata(version);
}
Also used : FileVersionBuilder(net.technicpack.minecraftcore.mojang.version.builder.FileVersionBuilder) MojangVersion(net.technicpack.minecraftcore.mojang.version.MojangVersion) Artifact(net.technicpack.minecraftcore.mojang.version.io.Artifact) ZipFileRetriever(net.technicpack.minecraftcore.mojang.version.builder.retrievers.ZipFileRetriever) DownloadException(net.technicpack.launchercore.exception.DownloadException) Library(net.technicpack.minecraftcore.mojang.version.io.Library) Downloads(net.technicpack.minecraftcore.mojang.version.io.Downloads) File(java.io.File)

Example 7 with DownloadException

use of net.technicpack.launchercore.exception.DownloadException in project LauncherV3 by TechnicPack.

the class InstallJavaRuntimeTask method runTask.

@Override
public void runTask(InstallTasksQueue queue) throws IOException {
    String json = FileUtils.readFileToString(runtimeManifestFile, StandardCharsets.UTF_8);
    JavaRuntimeManifest manifest = MojangUtils.getGson().fromJson(json, JavaRuntimeManifest.class);
    if (manifest == null) {
        throw new DownloadException("The Java runtime manifest is invalid.");
    }
    // TODO: Check runtime isn't downloaded/installed outside of its directory
    // Create the runtime directory if it doesn't exist
    File runtimeRoot = new File(runtimesDirectory, runtimeName);
    runtimeRoot.mkdirs();
    // First, create the dirs
    manifest.getFiles().forEach((path, runtimeFile) -> {
        // We're only interested in the dirs for now
        if (runtimeFile.getType() == JavaRuntimeFileType.DIRECTORY) {
            File dir = new File(runtimeRoot, path);
            dir.mkdirs();
        }
    });
    // Then, download the files
    manifest.getFiles().forEach((path, runtimeFile) -> {
        // We're only interested in the files right now
        if (runtimeFile.getType() == JavaRuntimeFileType.FILE) {
            File target = new File(runtimeRoot, path);
            // Apparently the Mac Java 8 JRE spec doesn't have any directory entries, so we have to create them regardless
            target.getParentFile().mkdirs();
            Download download = runtimeFile.getDownloads().getRaw();
            IFileVerifier verifier = new SHA1FileVerifier(download.getSha1());
            EnsureFileTask ensureFileTask = new EnsureFileTask(target, verifier, null, download.getUrl(), downloadJavaQueue, null);
            ensureFileTask.setExecutable(runtimeFile.isExecutable());
            examineJavaQueue.addTask(ensureFileTask);
        }
    });
    // Then, create the links
    manifest.getFiles().forEach((path, runtimeFile) -> {
        // We're only interested in links right now
        if (runtimeFile.getType() == JavaRuntimeFileType.LINK) {
            File link = new File(runtimeRoot, path);
            File target = new File(link, runtimeFile.getTarget());
            // We add it to the download queue so it runs after all the files exist
            downloadJavaQueue.addTask(new EnsureLinkedFileTask(link, target));
        }
    });
}
Also used : EnsureFileTask(net.technicpack.launchercore.install.tasks.EnsureFileTask) EnsureLinkedFileTask(net.technicpack.launchercore.install.tasks.EnsureLinkedFileTask) DownloadException(net.technicpack.launchercore.exception.DownloadException) IFileVerifier(net.technicpack.launchercore.install.verifiers.IFileVerifier) JavaRuntimeManifest(net.technicpack.minecraftcore.mojang.java.JavaRuntimeManifest) SHA1FileVerifier(net.technicpack.launchercore.install.verifiers.SHA1FileVerifier) File(java.io.File) Download(net.technicpack.minecraftcore.mojang.version.io.Download)

Example 8 with DownloadException

use of net.technicpack.launchercore.exception.DownloadException in project LauncherV3 by TechnicPack.

the class Library method getDownloadUrl.

public String getDownloadUrl(String path) throws DownloadException {
    Set<String> possibleUrls = new LinkedHashSet<>(8);
    // It acts as a Maven root URL
    if (this.url != null) {
        possibleUrls.add(this.url + path);
    }
    // This is a fully specified URL
    if (!hasNatives()) {
        String artifactUrl = null;
        if (downloads != null) {
            Artifact artifact = downloads.getArtifact();
            if (artifact != null)
                artifactUrl = artifact.getUrl();
        }
        if (artifactUrl != null && !artifactUrl.isEmpty()) {
            // Check if this URL is in Minecraft Forge's Maven repo and add ours as a primary mirror
            Matcher m = FORGE_MAVEN_ROOT.matcher(artifactUrl);
            if (m.matches())
                possibleUrls.add(TechnicConstants.technicForgeRepo + m.group(1));
            possibleUrls.add(artifactUrl);
        }
    }
    // These also work as a Maven root URL
    for (String string : FALLBACK) {
        possibleUrls.add(string + path);
    }
    for (String possibleUrl : possibleUrls) {
        if (Utils.pingHttpURL(possibleUrl)) {
            return possibleUrl;
        }
    }
    throw new DownloadException("Failed to download library " + path + ": no mirror found");
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Matcher(java.util.regex.Matcher) DownloadException(net.technicpack.launchercore.exception.DownloadException)

Example 9 with DownloadException

use of net.technicpack.launchercore.exception.DownloadException in project LauncherV3 by TechnicPack.

the class Utils method downloadFile.

public static Download downloadFile(String url, String name, String output, File cache, IFileVerifier verifier, DownloadListener listener) throws IOException, InterruptedException {
    int tries = DOWNLOAD_RETRIES;
    File outputFile = null;
    Download download = null;
    while (tries > 0) {
        getLogger().info("Starting download of " + url + ", with " + tries + " tries remaining");
        tries--;
        download = new Download(getFullUrl(url), name, output);
        download.setListener(listener);
        download.run();
        if (download.getResult() != Download.Result.SUCCESS) {
            if (download.getOutFile() != null) {
                download.getOutFile().delete();
            }
            if (Thread.interrupted())
                throw new InterruptedException();
            System.err.println("Download of " + url + " Failed!");
            if (listener != null) {
                listener.stateChanged("Download failed, retries remaining: " + tries, 0F);
            }
        } else {
            if (download.getOutFile().exists() && (verifier == null || verifier.isFileValid(download.getOutFile()))) {
                outputFile = download.getOutFile();
                break;
            }
        }
    }
    if (outputFile == null) {
        throw new DownloadException("Failed to download " + url, download != null ? download.getException() : null);
    }
    if (cache != null) {
        FileUtils.copyFile(outputFile, cache);
    }
    return download;
}
Also used : DownloadException(net.technicpack.launchercore.exception.DownloadException) Download(net.technicpack.launchercore.mirror.download.Download)

Example 10 with DownloadException

use of net.technicpack.launchercore.exception.DownloadException in project LauncherV3 by TechnicPack.

the class Utils method pingHttpURL.

/**
 * Opens an HTTP connection to a web URL and tests that the response is a valid 200-level code
 * and we can successfully open a stream to the content.
 *
 * @param urlLoc The HTTP URL indicating the location of the content.
 * @return True if the content can be accessed successfully, false otherwise.
 */
public static boolean pingHttpURL(String urlLoc) {
    try {
        final URL url = getFullUrl(urlLoc);
        HttpURLConnection conn = openHttpConnection(url);
        conn.setRequestMethod("HEAD");
        int responseCode = conn.getResponseCode();
        int responseFamily = responseCode / 100;
        // System.out.println(responseCode + " " + urlLoc);
        if (responseFamily == 3) {
            String newUrl = conn.getHeaderField("Location");
            URL redirectUrl = null;
            try {
                redirectUrl = new URL(newUrl);
            } catch (MalformedURLException ex) {
                throw new DownloadException("Invalid Redirect URL: " + url, ex);
            }
            conn = openHttpConnection(redirectUrl);
            responseCode = conn.getResponseCode();
            responseFamily = responseCode / 100;
        }
        if (responseFamily == 2) {
            try (InputStream stream = conn.getInputStream()) {
                return true;
            }
        } else {
            return false;
        }
    } catch (IOException ex) {
        logger.log(Level.SEVERE, "Got an error when pinging " + urlLoc, ex);
        return false;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) DownloadException(net.technicpack.launchercore.exception.DownloadException) URL(java.net.URL)

Aggregations

DownloadException (net.technicpack.launchercore.exception.DownloadException)11 File (java.io.File)5 MojangVersion (net.technicpack.minecraftcore.mojang.version.MojangVersion)4 IFileVerifier (net.technicpack.launchercore.install.verifiers.IFileVerifier)3 InstallTasksQueue (net.technicpack.launchercore.install.InstallTasksQueue)2 DownloadFileTask (net.technicpack.launchercore.install.tasks.DownloadFileTask)2 EnsureFileTask (net.technicpack.launchercore.install.tasks.EnsureFileTask)2 SHA1FileVerifier (net.technicpack.launchercore.install.verifiers.SHA1FileVerifier)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1