Search in sources :

Example 1 with Settings

use of kml.Settings 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

Timestamp (java.sql.Timestamp)1 Settings (kml.Settings)1 VersionMeta (kml.game.version.VersionMeta)1 VersionType (kml.game.version.VersionType)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1