Search in sources :

Example 1 with Profile

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

the class MainFX method validateSelectedProfile.

/**
 * Validates the selected profile according to the constraints
 */
private void validateSelectedProfile() {
    Profiles ps = kernel.getProfiles();
    // Check if selected profile passes the current settings
    Profile selected = ps.getSelectedProfile();
    VersionMeta selectedVersion = selected.getVersionID();
    if (selected.getType() == ProfileType.SNAPSHOT && !settings.getEnableSnapshots()) {
        ps.setSelectedProfile(ps.getReleaseProfile());
    } else if (selected.getType() == ProfileType.CUSTOM) {
        VersionType type = selectedVersion.getType();
        if (type == VersionType.SNAPSHOT && !settings.getEnableSnapshots()) {
            ps.setSelectedProfile(ps.getReleaseProfile());
        } else if (type == VersionType.OLD_ALPHA && !settings.getEnableHistorical()) {
            ps.setSelectedProfile(ps.getReleaseProfile());
        } else if (type == VersionType.OLD_BETA && !settings.getEnableHistorical()) {
            ps.setSelectedProfile(ps.getReleaseProfile());
        }
    }
    updateGameVersion();
}
Also used : VersionMeta(kml.game.version.VersionMeta) Profiles(kml.game.profile.Profiles) Profile(kml.game.profile.Profile) VersionType(kml.game.version.VersionType)

Example 2 with Profile

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

the class MainFX method saveProfile.

/**
 * Saves the profile data from the profile editor
 */
@FXML
public final void saveProfile() {
    Profile target;
    if (profileList.getSelectionModel().getSelectedIndex() == 0) {
        target = new Profile(ProfileType.CUSTOM);
        kernel.getProfiles().addProfile(target);
    } else {
        Label selectedElement = profileList.getSelectionModel().getSelectedItem();
        target = kernel.getProfiles().getProfile(selectedElement.getId());
    }
    if (target.getType() == ProfileType.CUSTOM) {
        if (!profileName.getText().isEmpty()) {
            target.setName(profileName.getText());
        } else {
            target.setName(null);
        }
        if (versionList.getSelectionModel().getSelectedIndex() == 0) {
            target.setVersionID(kernel.getVersions().getLatestRelease());
            target.setLatestRelease(true);
            target.setLatestSnapshot(false);
        } else if (versionList.getSelectionModel().getSelectedIndex() == 1 && settings.getEnableSnapshots()) {
            target.setVersionID(kernel.getVersions().getLatestSnapshot());
            target.setLatestRelease(false);
            target.setLatestSnapshot(true);
        } else {
            target.setVersionID(versionList.getSelectionModel().getSelectedItem());
            target.setLatestRelease(false);
            target.setLatestSnapshot(false);
        }
        try {
            target.setIcon(ProfileIcon.valueOf(profileIcon.getId()));
        } catch (IllegalArgumentException ex) {
            target.setIcon(null);
        }
    }
    if (!resW.isDisabled()) {
        try {
            int h = Integer.parseInt(resH.getText());
            int w = Integer.parseInt(resW.getText());
            target.setResolution(w, h);
        } catch (NumberFormatException ex) {
            console.print("Invalid resolution given.");
        }
    } else {
        target.setResolution(-1, -1);
    }
    if (!gameDir.isDisabled() && !gameDir.getText().isEmpty()) {
        target.setGameDir(new File(gameDir.getText()));
    } else {
        target.setGameDir(null);
    }
    if (settings.getEnableAdvanced()) {
        if (!javaExec.isDisabled() && !javaExec.getText().isEmpty()) {
            target.setJavaDir(new File(javaExec.getText()));
        } else {
            target.setJavaDir(null);
        }
        if (!javaArgs.isDisabled() && !javaArgs.getText().isEmpty()) {
            target.setJavaArgs(javaArgs.getText());
        } else {
            target.setJavaArgs(null);
        }
    }
    kernel.saveProfiles();
    if (kernel.getProfiles().getSelectedProfile() == target) {
        updateGameVersion();
    }
    kernel.showAlert(Alert.AlertType.INFORMATION, null, Language.get(57));
    profileListLoaded = false;
    profileListPopupLoaded = false;
    switchTab(launchOptionsLabel);
}
Also used : File(java.io.File) Profile(kml.game.profile.Profile) FXML(javafx.fxml.FXML)

Example 3 with Profile

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

the class GameLauncher method launch.

/**
 * Prepares and launcher the game
 * @throws GameLauncherException If an error has been thrown
 */
public final void launch(final MainFX mainFX) throws GameLauncherException {
    console.print("Game launch work has started.");
    Profile p = kernel.getProfiles().getSelectedProfile();
    if (isRunning()) {
        throw new GameLauncherException("Game is already started!");
    }
    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) {
        throw new GameLauncherException("Version ID is null.");
    }
    Version ver = versions.getVersion(verID);
    if (ver == null) {
        throw new GameLauncherException("Version info could not be obtained.");
    }
    File workingDir = Kernel.APPLICATION_WORKING_DIR;
    console.print("Deleting old natives.");
    File nativesRoot = new File(workingDir + File.separator + "versions" + File.separator + ver.getID());
    if (nativesRoot.isDirectory()) {
        File[] files = nativesRoot.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory() && f.getName().contains("natives")) {
                    Utils.deleteDirectory(f);
                }
            }
        }
    }
    final File nativesDir = new File(workingDir, "versions" + File.separator + ver.getID() + File.separator + ver.getID() + "-natives-" + System.nanoTime());
    if (!nativesDir.isDirectory()) {
        nativesDir.mkdirs();
    }
    console.print("Launching Minecraft " + ver.getID() + " on " + workingDir.getAbsolutePath());
    console.print("Using natives dir: " + nativesDir);
    console.print("Extracting natives.");
    List<String> gameArgs = new ArrayList<>();
    if (p.hasJavaDir()) {
        gameArgs.add(p.getJavaDir().getAbsolutePath());
    } else {
        gameArgs.add(Utils.getJavaDir());
    }
    if (!p.hasJavaArgs()) {
        if (Utils.getOSArch() == OSArch.OLD) {
            gameArgs.add("-Xmx1G");
        } else {
            gameArgs.add("-Xmx2G");
        }
        gameArgs.add("-Xmn128M");
    } else {
        String javaArgs = p.getJavaArgs();
        String[] args = javaArgs.split(" ");
        Collections.addAll(gameArgs, args);
    }
    gameArgs.add("-Djava.library.path=" + nativesDir.getAbsolutePath());
    gameArgs.add("-cp");
    StringBuilder libraries = new StringBuilder();
    List<Library> libs = ver.getLibraries();
    String separator = System.getProperty("path.separator");
    Authentication a = kernel.getAuthentication();
    User u = a.getSelectedUser();
    if (u.getType() == UserType.KROTHIUM) {
        try {
            File launchPath = new File(GameLauncher.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
            libraries.append(launchPath.getAbsolutePath()).append(separator);
        } catch (URISyntaxException ex) {
            console.print("Failed to load GameStarter.");
        }
    }
    for (Library lib : libs) {
        if (!lib.isCompatible()) {
            continue;
        }
        if (lib.isNative()) {
            try {
                File completePath = new File(Kernel.APPLICATION_WORKING_DIR + File.separator + lib.getRelativeNativePath());
                FileInputStream input = new FileInputStream(completePath);
                Utils.decompressZIP(input, nativesDir, lib.getExtractExclusions());
            } catch (IOException ex) {
                console.print("Failed to extract native: " + lib.getName());
                ex.printStackTrace(console.getWriter());
            }
        } else {
            File completePath = new File(Kernel.APPLICATION_WORKING_DIR + File.separator + lib.getRelativePath());
            libraries.append(completePath.getAbsolutePath()).append(separator);
        }
    }
    console.print("Preparing game args.");
    File verPath = new File(Kernel.APPLICATION_WORKING_DIR + File.separator + ver.getRelativeJar());
    libraries.append(verPath.getAbsolutePath());
    File assetsDir;
    AssetIndex index = ver.getAssetIndex();
    File assetsRoot = new File(workingDir, "assets");
    if ("legacy".equals(index.getID())) {
        assetsDir = new File(assetsRoot, "virtual" + File.separator + "legacy");
        if (!assetsDir.isDirectory()) {
            assetsDir.mkdirs();
        }
        console.print("Building virtual asset folder.");
        File indexJSON = new File(assetsRoot, "indexes" + File.separator + index.getID() + ".json");
        try {
            JSONObject o = new JSONObject(new String(Files.readAllBytes(indexJSON.toPath()), "ISO-8859-1"));
            JSONObject objects = o.getJSONObject("objects");
            Set s = objects.keySet();
            for (Object value : s) {
                String name = value.toString();
                File assetFile = new File(assetsDir, name);
                JSONObject asset = objects.getJSONObject(name);
                String sha = asset.getString("hash");
                if (!Utils.verifyChecksum(assetFile, sha, "SHA-1")) {
                    File objectFile = new File(assetsRoot, "objects" + File.separator + sha.substring(0, 2) + File.separator + sha);
                    if (assetFile.getParentFile() != null) {
                        assetFile.getParentFile().mkdirs();
                    }
                    Files.copy(objectFile.toPath(), assetFile.toPath());
                }
            }
        } catch (Exception ex) {
            console.print("Failed to create virtual asset folder.");
            ex.printStackTrace(console.getWriter());
        }
    } else {
        assetsDir = assetsRoot;
    }
    gameArgs.add(libraries.toString());
    if (u.getType() == UserType.KROTHIUM) {
        gameArgs.add("kml.game.GameStarter");
    }
    gameArgs.add(ver.getMainClass());
    console.print("Full game launcher parameters: ");
    String[] versionArgs = ver.getMinecraftArguments().split(" ");
    for (int i = 0; i < versionArgs.length; i++) {
        if (versionArgs[i].startsWith("$")) {
            switch(versionArgs[i]) {
                case "${auth_player_name}":
                    versionArgs[i] = versionArgs[i].replace("${auth_player_name}", u.getDisplayName());
                    break;
                case "${version_name}":
                    versionArgs[i] = versionArgs[i].replace("${version_name}", ver.getID());
                    break;
                case "${game_directory}":
                    if (p.hasGameDir()) {
                        File gameDir = p.getGameDir();
                        if (!gameDir.isDirectory()) {
                            gameDir.mkdirs();
                        }
                        versionArgs[i] = versionArgs[i].replace("${game_directory}", gameDir.getAbsolutePath());
                    } else {
                        versionArgs[i] = versionArgs[i].replace("${game_directory}", workingDir.getAbsolutePath());
                    }
                    break;
                case "${assets_root}":
                    versionArgs[i] = versionArgs[i].replace("${assets_root}", assetsDir.getAbsolutePath());
                    break;
                case "${game_assets}":
                    versionArgs[i] = versionArgs[i].replace("${game_assets}", assetsDir.getAbsolutePath());
                    break;
                case "${assets_index_name}":
                    versionArgs[i] = versionArgs[i].replace("${assets_index_name}", index.getID());
                    break;
                case "${auth_uuid}":
                    versionArgs[i] = versionArgs[i].replace("${auth_uuid}", u.getSelectedProfile());
                    break;
                case "${auth_access_token}":
                    versionArgs[i] = versionArgs[i].replace("${auth_access_token}", u.getAccessToken());
                    break;
                case "${version_type}":
                    versionArgs[i] = versionArgs[i].replace("${version_type}", ver.getType().name());
                    break;
                case "${user_properties}":
                    versionArgs[i] = versionArgs[i].replace("${user_properties}", "{}");
                    break;
                case "${user_type}":
                    versionArgs[i] = versionArgs[i].replace("${user_type}", "mojang");
                    break;
                case "${auth_session}":
                    versionArgs[i] = versionArgs[i].replace("${auth_session}", "token:" + u.getAccessToken() + ':' + u.getSelectedProfile().replace("-", ""));
                    break;
            }
        }
    }
    Collections.addAll(gameArgs, versionArgs);
    if (p.hasResolution()) {
        gameArgs.add("--width");
        gameArgs.add(String.valueOf(p.getResolutionWidth()));
        gameArgs.add("--height");
        gameArgs.add(String.valueOf(p.getResolutionHeight()));
    }
    for (String arg : gameArgs) {
        console.print(arg);
    }
    ProcessBuilder pb = new ProcessBuilder(gameArgs);
    pb.directory(workingDir);
    try {
        process = pb.start();
        if (kernel.getSettings().getShowGameLog()) {
            Platform.runLater(new Runnable() {

                @Override
                public void run() {
                    FXMLLoader loader = new FXMLLoader();
                    loader.setLocation(GameLauncher.this.getClass().getResource("/kml/gui/fxml/Output.fxml"));
                    Parent parent;
                    try {
                        parent = loader.load();
                    } catch (IOException e) {
                        parent = null;
                        console.print("Failed to initialize Output GUI!");
                        e.printStackTrace(console.getWriter());
                    }
                    Stage stage = new Stage();
                    stage.getIcons().add(Kernel.APPLICATION_ICON);
                    stage.setTitle("Krothium Minecraft Launcher - " + Language.get(69));
                    stage.setScene(new Scene(parent));
                    stage.setResizable(true);
                    stage.setMaximized(false);
                    stage.show();
                    output = loader.getController();
                    outputGUI = stage;
                }
            });
        }
        Thread log_info = new Thread(new Runnable() {

            @Override
            public void run() {
                GameLauncher.this.pipeOutput(process.getInputStream());
            }
        });
        log_info.start();
        Thread log_error = new Thread(new Runnable() {

            @Override
            public void run() {
                GameLauncher.this.pipeOutput(process.getErrorStream());
            }
        });
        log_error.start();
        final Timer timer = new Timer();
        TimerTask process_status = new TimerTask() {

            @Override
            public void run() {
                if (!GameLauncher.this.isRunning()) {
                    boolean error;
                    if (GameLauncher.this.process.exitValue() != 0) {
                        error = true;
                        GameLauncher.this.console.print("Game stopped unexpectedly.");
                    } else {
                        error = false;
                    }
                    GameLauncher.this.console.print("Deleteting natives dir.");
                    Utils.deleteDirectory(nativesDir);
                    timer.cancel();
                    timer.purge();
                    mainFX.gameEnded(error);
                }
            }
        };
        timer.schedule(process_status, 0, 25);
    } catch (IOException ex) {
        ex.printStackTrace(console.getWriter());
        throw new GameLauncherException("Game returned an error code.");
    }
}
Also used : User(kml.auth.user.User) Parent(javafx.scene.Parent) URISyntaxException(java.net.URISyntaxException) FXMLLoader(javafx.fxml.FXMLLoader) Profile(kml.game.profile.Profile) Version(kml.game.version.Version) Stage(javafx.stage.Stage) VersionMeta(kml.game.version.VersionMeta) AssetIndex(kml.game.version.asset.AssetIndex) Scene(javafx.scene.Scene) URISyntaxException(java.net.URISyntaxException) GameLauncherException(kml.exceptions.GameLauncherException) Versions(kml.game.version.Versions) JSONObject(org.json.JSONObject) Authentication(kml.auth.Authentication) JSONObject(org.json.JSONObject) GameLauncherException(kml.exceptions.GameLauncherException) Library(kml.game.version.library.Library)

Example 4 with Profile

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

the class MainFX method getProfileList.

/**
 * Generates an ObservableList of Labels representing each profile
 * @return The profiles ObservableList
 */
private ObservableList<Label> getProfileList() {
    ObservableList<Label> profileListItems = FXCollections.observableArrayList();
    Profiles ps = kernel.getProfiles();
    Label l;
    ImageView iv;
    String text;
    for (Profile p : ps.getProfiles()) {
        if (p.getType() == ProfileType.SNAPSHOT && !settings.getEnableSnapshots()) {
            continue;
        }
        switch(p.getType()) {
            case RELEASE:
                iv = new ImageView(kernel.getProfileIcon(ProfileIcon.GRASS));
                text = Language.get(59);
                break;
            case SNAPSHOT:
                iv = new ImageView(kernel.getProfileIcon(ProfileIcon.CRAFTING_TABLE));
                text = Language.get(60);
                break;
            default:
                text = p.hasName() ? p.getName() : Language.get(70);
                ProfileIcon pi = p.hasIcon() ? p.getIcon() : ProfileIcon.FURNACE;
                iv = new ImageView(kernel.getProfileIcon(pi));
                break;
        }
        iv.setFitWidth(68);
        iv.setFitHeight(68);
        l = new Label(text, iv);
        // Fetch Minecraft version used by the profile
        VersionMeta verID;
        switch(p.getType()) {
            case CUSTOM:
                Versions versions = kernel.getVersions();
                verID = p.hasVersion() ? p.getVersionID() : versions.getLatestRelease();
                break;
            case RELEASE:
                verID = kernel.getVersions().getLatestRelease();
                break;
            default:
                verID = kernel.getVersions().getLatestSnapshot();
                break;
        }
        l.setId(p.getID());
        if (verID != null) {
            // If profile has any known version just show it below the profile name
            if (verID.getType() == VersionType.SNAPSHOT && !settings.getEnableSnapshots()) {
                continue;
            }
            if ((verID.getType() == VersionType.OLD_ALPHA || verID.getType() == VersionType.OLD_BETA) && !settings.getEnableHistorical()) {
                continue;
            }
            l.setText(l.getText() + '\n' + verID.getID());
        }
        if (ps.getSelectedProfile().equals(p)) {
            l.getStyleClass().add("selectedProfile");
        }
        profileListItems.add(l);
    }
    return profileListItems;
}
Also used : Versions(kml.game.version.Versions) VersionMeta(kml.game.version.VersionMeta) Profiles(kml.game.profile.Profiles) ProfileIcon(kml.game.profile.ProfileIcon) ImageView(javafx.scene.image.ImageView) Profile(kml.game.profile.Profile)

Example 5 with Profile

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

the class MainFX method deleteProfile.

/**
 * Deletes the profile loaded by the profile editor
 */
@FXML
public final void deleteProfile() {
    int result = kernel.showAlert(Alert.AlertType.CONFIRMATION, null, Language.get(61));
    if (result == 1) {
        Label selectedElement = profileList.getSelectionModel().getSelectedItem();
        Profile p = kernel.getProfiles().getProfile(selectedElement.getId());
        if (kernel.getProfiles().deleteProfile(p)) {
            kernel.saveProfiles();
            updateGameVersion();
            kernel.showAlert(Alert.AlertType.INFORMATION, null, Language.get(56));
        } else {
            kernel.showAlert(Alert.AlertType.ERROR, null, Language.get(58));
        }
        loadProfileList();
        switchTab(launchOptionsLabel);
    }
}
Also used : Profile(kml.game.profile.Profile) FXML(javafx.fxml.FXML)

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