Search in sources :

Example 6 with Profile

use of kml.game.profile.Profile in project Krothium-Launcher by DarkLBP.

the class MainFX method updateGameVersion.

/**
 * Updates the selected minecraft version indicator
 */
private void updateGameVersion() {
    Profile p = kernel.getProfiles().getSelectedProfile();
    if (p != null) {
        switch(p.getType()) {
            case RELEASE:
                gameVersion.setText(Language.get(26));
                break;
            case SNAPSHOT:
                gameVersion.setText(Language.get(32));
                break;
            default:
                if (p.isLatestRelease()) {
                    gameVersion.setText(Language.get(26));
                } else if (p.isLatestSnapshot()) {
                    gameVersion.setText(Language.get(32));
                } else if (p.hasVersion()) {
                    VersionMeta version = p.getVersionID();
                    gameVersion.setText("Minecraft " + version.getID());
                }
                break;
        }
    } else {
        gameVersion.setText("");
    }
}
Also used : VersionMeta(kml.game.version.VersionMeta) Profile(kml.game.profile.Profile)

Example 7 with Profile

use of kml.game.profile.Profile in project Krothium-Launcher by DarkLBP.

the class MainFX method loadEditor.

/**
 * Prepares the editor with the selected profile or with a new one
 */
@FXML
public final void loadEditor() {
    if (profileList.getSelectionModel().getSelectedIndex() == -1) {
        // Nothing has been selected
        return;
    }
    if (!versionListLoaded) {
        loadVersionList();
    }
    if (profileList.getSelectionModel().getSelectedIndex() == 0) {
        profileName.setEditable(true);
        profileName.setText("");
        deleteButton.setVisible(false);
        versionBlock.setVisible(true);
        versionBlock.setManaged(true);
        iconBlock.setVisible(true);
        iconBlock.setManaged(true);
        versionList.getSelectionModel().select(0);
        profileIcon.setImage(kernel.getProfileIcon(ProfileIcon.FURNACE));
        if (settings.getEnableAdvanced()) {
            javaExecBlock.setVisible(true);
            javaExecBlock.setManaged(true);
            javaArgsBlock.setVisible(true);
            javaArgsBlock.setManaged(true);
            toggleEditorOption(javaExecLabel, false);
            javaExec.setText(Utils.getJavaDir());
            toggleEditorOption(javaArgsLabel, false);
            StringBuilder jA = new StringBuilder(15);
            if (Utils.getOSArch() == OSArch.OLD) {
                jA.append("-Xmx1G");
            } else {
                jA.append("-Xmx2G");
            }
            jA.append(" -Xmn128M");
            javaArgs.setText(jA.toString());
        } else {
            javaExecBlock.setVisible(false);
            javaExecBlock.setManaged(false);
            javaArgsBlock.setVisible(false);
            javaArgsBlock.setManaged(false);
        }
        toggleEditorOption(resolutionLabel, false);
        resW.setText(String.valueOf(854));
        resH.setText(String.valueOf(480));
        toggleEditorOption(gameDirLabel, false);
        gameDir.setText(Utils.getWorkingDirectory().getAbsolutePath());
    } else {
        Label selectedElement = profileList.getSelectionModel().getSelectedItem();
        if (selectedElement != null) {
            Profile p = kernel.getProfiles().getProfile(selectedElement.getId());
            if (p.getType() != ProfileType.CUSTOM) {
                profileName.setEditable(false);
                deleteButton.setVisible(false);
                if (p.getType() == ProfileType.RELEASE) {
                    profileName.setText(Language.get(59));
                    profileIcon.setImage(kernel.getProfileIcon(ProfileIcon.GRASS));
                } else {
                    profileName.setText(Language.get(60));
                    profileIcon.setImage(kernel.getProfileIcon(ProfileIcon.CRAFTING_TABLE));
                }
                versionBlock.setVisible(false);
                versionBlock.setManaged(false);
                iconBlock.setVisible(false);
                iconBlock.setManaged(false);
            } else {
                if (p.hasIcon()) {
                    profileIcon.setImage(kernel.getProfileIcon(p.getIcon()));
                    profileIcon.setId(p.getIcon().name());
                } else {
                    profileIcon.setImage(kernel.getProfileIcon(ProfileIcon.FURNACE));
                }
                profileName.setEditable(true);
                deleteButton.setVisible(true);
                if (p.hasName()) {
                    profileName.setText(p.getName());
                } else {
                    profileName.setText("");
                }
                versionBlock.setVisible(true);
                versionBlock.setManaged(true);
                iconBlock.setVisible(true);
                iconBlock.setManaged(true);
                if (p.hasVersion()) {
                    if (p.isLatestRelease()) {
                        versionList.getSelectionModel().select(0);
                    } else if (p.isLatestSnapshot() && settings.getEnableSnapshots()) {
                        versionList.getSelectionModel().select(1);
                    } else if (versionList.getItems().contains(p.getVersionID())) {
                        versionList.getSelectionModel().select(p.getVersionID());
                    } else {
                        versionList.getSelectionModel().select(0);
                    }
                } else {
                    versionList.getSelectionModel().select(0);
                }
            }
            if (p.hasResolution()) {
                toggleEditorOption(resolutionLabel, true);
                resH.setText(String.valueOf(p.getResolutionHeight()));
                resW.setText(String.valueOf(p.getResolutionWidth()));
            } else {
                toggleEditorOption(resolutionLabel, false);
                resW.setText(String.valueOf(854));
                resH.setText(String.valueOf(480));
            }
            if (p.hasGameDir()) {
                toggleEditorOption(gameDirLabel, true);
                gameDir.setText(p.getGameDir().getAbsolutePath());
            } else {
                toggleEditorOption(gameDirLabel, false);
                gameDir.setText(Utils.getWorkingDirectory().getAbsolutePath());
            }
            if (settings.getEnableAdvanced()) {
                javaExecBlock.setVisible(true);
                javaExecBlock.setManaged(true);
                javaArgsBlock.setVisible(true);
                javaArgsBlock.setManaged(true);
                if (p.hasJavaDir()) {
                    toggleEditorOption(javaExecLabel, true);
                    javaExec.setText(p.getJavaDir().getAbsolutePath());
                } else {
                    toggleEditorOption(javaExecLabel, false);
                    javaExec.setText(Utils.getJavaDir());
                }
                if (p.hasJavaArgs()) {
                    toggleEditorOption(javaArgsLabel, true);
                    javaArgs.setText(p.getJavaArgs());
                } else {
                    toggleEditorOption(javaArgsLabel, false);
                    StringBuilder jA = new StringBuilder(15);
                    if (Utils.getOSArch() == OSArch.OLD) {
                        jA.append("-Xmx1G");
                    } else {
                        jA.append("-Xmx2G");
                    }
                    jA.append(" -Xmn128M");
                    javaArgs.setText(jA.toString());
                }
            } else {
                javaExecBlock.setVisible(false);
                javaExecBlock.setManaged(false);
                javaArgsBlock.setVisible(false);
                javaArgsBlock.setManaged(false);
            }
        }
    }
    switchTab(profileEditorTab);
}
Also used : Profile(kml.game.profile.Profile) FXML(javafx.fxml.FXML)

Example 8 with Profile

use of kml.game.profile.Profile 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

Profile (kml.game.profile.Profile)8 VersionMeta (kml.game.version.VersionMeta)5 FXML (javafx.fxml.FXML)3 Versions (kml.game.version.Versions)3 Profiles (kml.game.profile.Profiles)2 Version (kml.game.version.Version)2 AssetIndex (kml.game.version.asset.AssetIndex)2 Library (kml.game.version.library.Library)2 JSONObject (org.json.JSONObject)2 File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 FXMLLoader (javafx.fxml.FXMLLoader)1 Parent (javafx.scene.Parent)1 Scene (javafx.scene.Scene)1 ImageView (javafx.scene.image.ImageView)1 Stage (javafx.stage.Stage)1 Authentication (kml.auth.Authentication)1 User (kml.auth.user.User)1 DownloaderException (kml.exceptions.DownloaderException)1 GameLauncherException (kml.exceptions.GameLauncherException)1