Search in sources :

Example 1 with DownloaderException

use of kml.exceptions.DownloaderException in project Krothium-Launcher by DarkLBP.

the class MainFX method launchGame.

/**
 * Downloads and launches the game
 */
@FXML
public final void launchGame() {
    progressPane.setVisible(true);
    playPane.setVisible(false);
    progressBar.setProgress(0);
    progressText.setText("");
    final Downloader d = kernel.getDownloader();
    final GameLauncher gl = kernel.getGameLauncher();
    // Keep track of the progress
    final TimerTask progressTask = new TimerTask() {

        @Override
        public void run() {
            Platform.runLater(new Runnable() {

                @Override
                public void run() {
                    MainFX.this.progressBar.setProgress(d.getProgress());
                    MainFX.this.progressText.setText(Language.get(13) + ' ' + d.getCurrentFile() + "...");
                }
            });
        }
    };
    Thread runThread = new Thread(new Runnable() {

        @Override
        public void run() {
            // Begin download and game launch task
            try {
                Timer timer = new Timer();
                timer.schedule(progressTask, 0, 25);
                d.download();
                timer.cancel();
                timer.purge();
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        progressText.setText(Language.get(78));
                        progressBar.setProgress(ProgressIndicator.INDETERMINATE_PROGRESS);
                    }
                });
                gl.launch(MainFX.this);
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        progressPane.setVisible(false);
                        playPane.setVisible(true);
                        playButton.setText(Language.get(14));
                        playButton.setDisable(true);
                        profilePopupButton.setDisable(true);
                    }
                });
                if (!settings.getKeepLauncherOpen()) {
                    Platform.runLater(new Runnable() {

                        @Override
                        public void run() {
                            MainFX.this.stage.close();
                        }
                    });
                }
            } catch (DownloaderException e) {
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        kernel.showAlert(Alert.AlertType.ERROR, Language.get(83), Language.get(84));
                    }
                });
                console.print("Failed to perform game download task");
                e.printStackTrace(console.getWriter());
            } catch (GameLauncherException e) {
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        kernel.showAlert(Alert.AlertType.ERROR, Language.get(81), Language.get(82));
                    }
                });
                console.print("Failed to perform game launch task");
                e.printStackTrace(console.getWriter());
            }
        }
    });
    runThread.start();
}
Also used : DownloaderException(kml.exceptions.DownloaderException) Downloader(kml.game.download.Downloader) GameLauncher(kml.game.GameLauncher) GameLauncherException(kml.exceptions.GameLauncherException) FXML(javafx.fxml.FXML)

Example 2 with DownloaderException

use of kml.exceptions.DownloaderException in project Krothium-Launcher by DarkLBP.

the class Downloader method download.

/**
 * Downloads all requires game files
 * @throws DownloaderException If the download fails
 */
public final void download() throws DownloaderException {
    // Initial values
    downloading = true;
    downloaded = 0;
    validated = 0;
    total = 0;
    int tries;
    console.print("Download work has started.");
    if (Kernel.USE_LOCAL) {
        console.print("You are in offline mode.");
        downloading = false;
        return;
    }
    // Fetch version used by profile
    Profile p = kernel.getProfiles().getSelectedProfile();
    Versions versions = kernel.getVersions();
    VersionMeta verID;
    switch(p.getType()) {
        case CUSTOM:
            verID = p.hasVersion() ? p.getVersionID() : versions.getLatestRelease();
            break;
        case RELEASE:
            verID = versions.getLatestRelease();
            break;
        default:
            verID = versions.getLatestSnapshot();
            break;
    }
    if (verID == null) {
        downloading = false;
        throw new DownloaderException("Version ID is null.");
    }
    console.print("Using version ID: " + verID);
    Version v = versions.getVersion(verID);
    if (v == null) {
        downloading = false;
        throw new DownloaderException("Version info could not be obtained.");
    }
    // Get required files to be downloaded
    Set<Downloadable> urls = new HashSet<>();
    // Fetch assets
    console.print("Fetching asset urls..");
    AssetIndex index = v.getAssetIndex();
    File indexJSON = new File(Kernel.APPLICATION_WORKING_DIR, "assets" + File.separator + "indexes" + File.separator + index.getID() + ".json");
    tries = 0;
    if (!Utils.verifyChecksum(indexJSON, index.getSHA1(), "SHA-1")) {
        while (tries < DOWNLOAD_TRIES) {
            try {
                Utils.downloadFile(index.getURL(), indexJSON);
                break;
            } catch (IOException ex) {
                console.print("Failed to download file " + indexJSON.getName() + " (try " + tries + ')');
                ex.printStackTrace(console.getWriter());
                tries++;
            }
        }
    }
    if (tries == DOWNLOAD_TRIES) {
        console.print("Failed to download asset index for version " + index.getID());
    } else {
        // Load assets
        try {
            JSONObject root;
            try {
                root = new JSONObject(new String(Files.readAllBytes(indexJSON.toPath()), StandardCharsets.UTF_8));
            } catch (JSONException | IOException ex) {
                downloading = false;
                throw new DownloaderException("Failed to read asset index json file.");
            }
            JSONObject objects = root.getJSONObject("objects");
            Set<String> keys = objects.keySet();
            Collection<String> processedHashes = new ArrayList<>();
            File objectsRoot = new File("assets" + File.separator + "objects");
            for (String key : keys) {
                JSONObject o = objects.getJSONObject(key);
                String hash = o.getString("hash");
                long size = o.getLong("size");
                String downloadURL = "http://resources.download.minecraft.net/" + hash.substring(0, 2) + '/' + hash;
                File relPath = new File(objectsRoot, hash.substring(0, 2) + File.separator + hash);
                File fullPath = new File(Kernel.APPLICATION_WORKING_DIR + File.separator + relPath);
                if (!processedHashes.contains(hash)) {
                    total += size;
                    processedHashes.add(hash);
                    if (!Utils.verifyChecksum(fullPath, hash, "SHA-1")) {
                        Downloadable d = new Downloadable(downloadURL, size, relPath, hash, key);
                        urls.add(d);
                    } else {
                        validated += size;
                    }
                }
            }
        } catch (JSONException ex) {
            console.print("Failed to parse asset index.");
        }
    }
    // Fetch version
    console.print("Fetching version urls..");
    Map<String, Downloadable> downloads = v.getDownloads();
    if (downloads.containsKey("client")) {
        Downloadable d = downloads.get("client");
        if (d.hasURL()) {
            long jarSize = d.getSize();
            String jarSHA1 = d.getHash();
            total += d.getSize();
            File destPath = new File(Kernel.APPLICATION_WORKING_DIR + File.separator + v.getRelativeJar());
            File jsonFile = new File(Kernel.APPLICATION_WORKING_DIR + File.separator + v.getRelativeJSON());
            tries = 0;
            while (tries < DOWNLOAD_TRIES) {
                try {
                    Utils.downloadFile(v.getJSONURL(), jsonFile);
                    break;
                } catch (IOException ex) {
                    console.print("Failed to download file " + jsonFile.getName() + " (try " + tries + ')');
                    ex.printStackTrace(console.getWriter());
                    tries++;
                }
            }
            if (tries == DOWNLOAD_TRIES) {
                console.print("Failed to download version index " + destPath.getName());
            }
            if (!Utils.verifyChecksum(destPath, jarSHA1, "SHA-1")) {
                urls.add(d);
            } else {
                validated += jarSize;
            }
        } else {
            console.print("Incompatible version downloadable.");
        }
    } else if (v.hasJar()) {
        String jar = v.getJar();
        File relPath = v.getRelativeJar();
        console.print("Found legacy version " + jar);
        if (!relPath.exists()) {
            Downloadable d = new Downloadable("https://s3.amazonaws.com/Minecraft.Download/versions/" + jar + "/" + jar + ".jar", -1, v.getRelativeJar(), null, null);
            urls.add(d);
        } else {
            console.print("Legacy version file found. Assuming is valid.");
        }
    } else {
        console.print("Version file from " + v.getID() + " has no compatible downloadable objects.");
    }
    // Fetch libraries and natives
    console.print("Fetching library and native urls..");
    List<Library> libs = v.getLibraries();
    for (Library lib : libs) {
        if (lib.isCompatible()) {
            // Standard download
            if (lib.hasArtifactDownload()) {
                Downloadable a = lib.getArtifactDownload();
                File completePath = new File(Kernel.APPLICATION_WORKING_DIR + File.separator + a.getRelativePath());
                if (completePath.isFile() && a.getHash() == null) {
                    console.print("File " + completePath + " has no hash. So let's assume the local one is valid.");
                } else {
                    total += a.getSize();
                    if (Utils.verifyChecksum(completePath, a.getHash(), "SHA-1")) {
                        validated += a.getSize();
                    } else {
                        urls.add(a);
                    }
                }
            }
            // Native download
            if (lib.hasClassifierDownload()) {
                Downloadable c = lib.getClassifierDownload();
                File completePath = new File(Kernel.APPLICATION_WORKING_DIR + File.separator + c.getRelativePath());
                total += c.getSize();
                if (completePath.isFile() && c.getHash() == null) {
                    console.print("File " + completePath + " has no hash. So let's assume the local one is valid.");
                } else {
                    if (Utils.verifyChecksum(completePath, c.getHash(), "SHA-1")) {
                        validated += c.getSize();
                    } else {
                        urls.add(c);
                    }
                }
            }
        }
    }
    console.print("Downloading required game files...");
    if (urls.isEmpty()) {
        console.print("Nothing to download.");
    } else {
        // Download required files
        downloadFiles(urls);
    }
    downloading = false;
}
Also used : VersionMeta(kml.game.version.VersionMeta) DownloaderException(kml.exceptions.DownloaderException) AssetIndex(kml.game.version.asset.AssetIndex) JSONException(org.json.JSONException) Profile(kml.game.profile.Profile) Versions(kml.game.version.Versions) JSONObject(org.json.JSONObject) Version(kml.game.version.Version) Library(kml.game.version.library.Library)

Aggregations

DownloaderException (kml.exceptions.DownloaderException)2 FXML (javafx.fxml.FXML)1 GameLauncherException (kml.exceptions.GameLauncherException)1 GameLauncher (kml.game.GameLauncher)1 Downloader (kml.game.download.Downloader)1 Profile (kml.game.profile.Profile)1 Version (kml.game.version.Version)1 VersionMeta (kml.game.version.VersionMeta)1 Versions (kml.game.version.Versions)1 AssetIndex (kml.game.version.asset.AssetIndex)1 Library (kml.game.version.library.Library)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1