Search in sources :

Example 6 with VersionMeta

use of kml.game.version.VersionMeta 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 VersionMeta

use of kml.game.version.VersionMeta 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)

Example 8 with VersionMeta

use of kml.game.version.VersionMeta in project Krothium-Launcher by DarkLBP.

the class Profiles method fetchProfiles.

/**
 * Loads the profiles from the launcher_profiles.json
 */
public final void fetchProfiles() {
    console.print("Fetching profiles.");
    Timestamp latestUsedMillis = new Timestamp(-1);
    JSONObject root = kernel.getLauncherProfiles();
    if (root != null) {
        try {
            JSONObject ples = root.getJSONObject("profiles");
            Set<String> keys = ples.keySet();
            Iterator<String> it = keys.iterator();
            Profile first = null, latestUsedID = null;
            while (it.hasNext()) {
                String key = it.next();
                JSONObject o = ples.getJSONObject(key);
                try {
                    if (key.length() != 32) {
                        key = UUID.randomUUID().toString().replace("-", "");
                    } else {
                        String uuid = key.replaceAll("(.{8})(.{4})(.{4})(.{4})(.+)", "$1-$2-$3-$4-$5");
                        UUID.fromString(uuid);
                    }
                } catch (IllegalArgumentException ex) {
                    key = UUID.randomUUID().toString().replace("-", "");
                }
                ProfileType type;
                String typeString = o.has("type") ? o.getString("type") : null;
                if (typeString != null) {
                    switch(typeString) {
                        case "latest-release":
                            type = ProfileType.RELEASE;
                            break;
                        case "latest-snapshot":
                            type = ProfileType.SNAPSHOT;
                            break;
                        default:
                            type = ProfileType.CUSTOM;
                    }
                } else {
                    type = ProfileType.CUSTOM;
                }
                String name;
                VersionMeta version;
                ProfileIcon icon;
                boolean latestRelease = false, latestSnapshot = false;
                if (type == ProfileType.CUSTOM) {
                    name = o.has("name") ? o.getString("name") : null;
                    String ver = o.has("lastVersionId") ? o.getString("lastVersionId") : null;
                    if (ver != null) {
                        switch(ver) {
                            case "latest-release":
                                latestRelease = true;
                                break;
                            case "latest-snapshot":
                                latestSnapshot = true;
                                break;
                        }
                    }
                    version = kernel.getVersions().getVersionMeta(ver);
                    try {
                        icon = o.has("icon") ? ProfileIcon.valueOf(o.getString("icon").toUpperCase(Locale.ENGLISH)) : null;
                    } catch (IllegalArgumentException ex) {
                        icon = null;
                        console.print("Invalid profile icon for profile " + key);
                    }
                } else {
                    name = null;
                    version = null;
                    icon = null;
                }
                String created = o.has("created") ? o.getString("created") : null;
                String lastUsed = o.has("lastUsed") ? o.getString("lastUsed") : null;
                String gameDir = o.has("gameDir") ? o.getString("gameDir") : null;
                String javaDir = o.has("javaDir") ? o.getString("javaDir") : null;
                String javaArgs = o.has("javaArgs") ? o.getString("javaArgs") : null;
                Map<String, Integer> resolution = new HashMap<>();
                if (o.has("resolution")) {
                    JSONObject res = o.getJSONObject("resolution");
                    if (res.has("width") && res.has("height")) {
                        resolution.put("width", res.getInt("width"));
                        resolution.put("height", res.getInt("height"));
                    } else {
                        console.print("Profile " + name != null ? name : "UNKNOWN" + " has an invalid resolution.");
                    }
                }
                Profile p = new Profile(key, name, type, created, lastUsed, version, gameDir, javaDir, javaArgs, resolution, icon, latestRelease, latestSnapshot);
                if (first == null) {
                    first = p;
                }
                if (!profiles.contains(p)) {
                    addProfile(p);
                    if (p.getLastUsed().compareTo(latestUsedMillis) > 0) {
                        latestUsedMillis = p.getLastUsed();
                        latestUsedID = p;
                    }
                    if (type == ProfileType.RELEASE && releaseProfile == null) {
                        releaseProfile = p;
                    } else if (type == ProfileType.SNAPSHOT && snapshotProfile == null) {
                        snapshotProfile = p;
                    }
                }
            }
            if (releaseProfile == null) {
                releaseProfile = new Profile(ProfileType.RELEASE);
                if (first == null) {
                    first = releaseProfile;
                }
                addProfile(releaseProfile);
            }
            if (snapshotProfile == null) {
                snapshotProfile = new Profile(ProfileType.SNAPSHOT);
                addProfile(snapshotProfile);
            }
            if (latestUsedID != null) {
                Settings settings = kernel.getSettings();
                if (latestUsedID.getType() == ProfileType.SNAPSHOT && !settings.getEnableSnapshots()) {
                    setSelectedProfile(releaseProfile);
                } else if (latestUsedID.getType() == ProfileType.CUSTOM) {
                    VersionType type = latestUsedID.getVersionID().getType();
                    if (type == VersionType.SNAPSHOT && !settings.getEnableSnapshots()) {
                        setSelectedProfile(releaseProfile);
                    } else if (type == VersionType.OLD_ALPHA && !settings.getEnableHistorical()) {
                        setSelectedProfile(releaseProfile);
                    } else if (type == VersionType.OLD_BETA && !settings.getEnableHistorical()) {
                        setSelectedProfile(releaseProfile);
                    } else {
                        setSelectedProfile(latestUsedID);
                    }
                } else {
                    setSelectedProfile(latestUsedID);
                }
            } else {
                console.print("No profile is selected! Using first loaded (" + first + ')');
                setSelectedProfile(first);
            }
        } catch (JSONException ex) {
            console.print("Failed to fetch profiles.");
            ex.printStackTrace(console.getWriter());
        }
    } else {
        console.print("No profiles to be loaded. Generating defaults.");
        if (releaseProfile == null) {
            releaseProfile = new Profile(ProfileType.RELEASE);
            addProfile(releaseProfile);
        }
        if (snapshotProfile == null) {
            snapshotProfile = new Profile(ProfileType.SNAPSHOT);
            addProfile(snapshotProfile);
        }
    }
    if (selected == null) {
        setSelectedProfile(releaseProfile);
    }
}
Also used : VersionMeta(kml.game.version.VersionMeta) JSONException(org.json.JSONException) Timestamp(java.sql.Timestamp) VersionType(kml.game.version.VersionType) JSONObject(org.json.JSONObject) Settings(kml.Settings)

Aggregations

VersionMeta (kml.game.version.VersionMeta)8 Profile (kml.game.profile.Profile)5 Versions (kml.game.version.Versions)3 JSONObject (org.json.JSONObject)3 Profiles (kml.game.profile.Profiles)2 Version (kml.game.version.Version)2 VersionType (kml.game.version.VersionType)2 AssetIndex (kml.game.version.asset.AssetIndex)2 Library (kml.game.version.library.Library)2 JSONException (org.json.JSONException)2 URISyntaxException (java.net.URISyntaxException)1 Timestamp (java.sql.Timestamp)1 FXML (javafx.fxml.FXML)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 Settings (kml.Settings)1 Authentication (kml.auth.Authentication)1