Search in sources :

Example 1 with ArgumentList

use of net.technicpack.minecraftcore.mojang.version.io.argument.ArgumentList in project LauncherV3 by TechnicPack.

the class MinecraftLauncher method buildCommands.

private List<String> buildCommands(ModpackModel pack, long memory, MojangVersion version, LaunchOptions options) {
    LaunchCommandCollector commands = new LaunchCommandCollector();
    // Wrapper command (optirun, etc)
    String wrapperCommand = options.getOptions().getWrapperCommand();
    if (StringUtils.isNotEmpty(wrapperCommand)) {
        commands.addRaw(wrapperCommand);
    }
    JavaVersion javaVersion = version.getJavaVersion();
    if (javaVersion != null && options.getOptions().shouldUseMojangJava()) {
        File runtimeRoot = new File(directories.getRuntimesDirectory(), javaVersion.getComponent());
        Path javaExecutable;
        if (OperatingSystem.getOperatingSystem() == OperatingSystem.WINDOWS) {
            javaExecutable = runtimeRoot.toPath().resolve("bin/javaw.exe");
        } else if (OperatingSystem.getOperatingSystem() == OperatingSystem.OSX) {
            javaExecutable = runtimeRoot.toPath().resolve("jre.bundle/Contents/Home/bin/java");
        } else {
            javaExecutable = runtimeRoot.toPath().resolve("bin/java");
        }
        commands.addRaw(javaExecutable.toString());
    } else {
        commands.addRaw(javaVersions.getSelectedPath());
    }
    OperatingSystem operatingSystem = OperatingSystem.getOperatingSystem();
    String nativesDir = new File(pack.getBinDir(), "natives").getAbsolutePath();
    String cpString = buildClassPath(pack, version);
    // build arg parameter map
    Map<String, String> params = new HashMap<String, String>();
    IUserType user = userModel.getCurrentUser();
    File gameDirectory = pack.getInstalledDirectory();
    ILaunchOptions launchOpts = options.getOptions();
    params.put("auth_username", user.getUsername());
    params.put("auth_session", user.getSessionId());
    params.put("auth_access_token", user.getAccessToken());
    params.put("auth_player_name", user.getDisplayName());
    params.put("auth_uuid", user.getId());
    params.put("profile_name", user.getDisplayName());
    params.put("version_name", version.getId());
    params.put("version_type", version.getType().getName());
    params.put("game_directory", gameDirectory.getAbsolutePath());
    params.put("natives_directory", nativesDir);
    params.put("classpath", cpString);
    params.put("resolution_width", Integer.toString(launchOpts.getCustomWidth()));
    params.put("resolution_height", Integer.toString(launchOpts.getCustomHeight()));
    StringSubstitutor paramDereferencer = new StringSubstitutor(params);
    String targetAssets = directories.getAssetsDirectory().getAbsolutePath();
    String assetsKey = version.getAssetsKey();
    if (assetsKey == null || assetsKey.isEmpty()) {
        assetsKey = "legacy";
    }
    if (version.getAreAssetsVirtual()) {
        targetAssets += File.separator + "virtual" + File.separator + assetsKey;
    } else if (version.getAssetsMapToResources()) {
        targetAssets = pack.getResourcesDir().getAbsolutePath();
    }
    params.put("game_assets", targetAssets);
    params.put("assets_root", targetAssets);
    params.put("assets_index_name", assetsKey);
    params.put("user_type", user.getMCUserType());
    params.put("user_properties", user.getUserProperties());
    params.put("launcher_name", "technic");
    params.put("launcher_version", "4." + buildNumber.getBuildNumber());
    // Prepend custom JVM arguments
    String customJvmArgs = options.getOptions().getJavaArgs();
    if (StringUtils.isNotEmpty(customJvmArgs)) {
        customJvmArgs = customJvmArgs.replaceAll("[\\r\\n]", " ");
        for (String customJvmArg : customJvmArgs.split("[ ]+")) {
            commands.addRaw(customJvmArg);
        }
    }
    // build jvm args
    String launchJavaVersion = javaVersions.getSelectedVersion().getVersionNumber();
    ArgumentList jvmArgs = version.getJavaArguments();
    if (jvmArgs != null) {
        for (String arg : jvmArgs.resolve(options.getOptions(), paramDereferencer)) {
            commands.add(arg);
        }
    }
    int permSize = 128;
    if (memory >= (1024 * 6)) {
        permSize = 512;
    } else if (memory >= 2048) {
        permSize = 256;
    }
    commands.addRaw("-Xms" + memory + "m");
    commands.addRaw("-Xmx" + memory + "m");
    if (!RunData.isJavaVersionAtLeast(launchJavaVersion, "1.8"))
        commands.add("-XX:MaxPermSize=" + permSize + "m");
    commands.addUnique("-Djava.library.path=" + nativesDir);
    // Tell forge 1.5 to download from our mirror instead
    // This can't be HTTPS because Forge 1.5 doesn't load up Let's Encrypt root certs (we use Let's Encrypt on the mirror)
    commands.addUnique("-Dfml.core.libraries.mirror=http://mirror.technicpack.net/Technic/lib/fml/%s");
    // This is required because we strip META-INF from the minecraft.jar
    commands.addUnique("-Dfml.ignoreInvalidMinecraftCertificates=true");
    commands.addUnique("-Dfml.ignorePatchDiscrepancies=true");
    // This is for ForgeWrapper >= 1.4.2
    if (MojangUtils.requiresForgeWrapper(version)) {
        commands.addUnique("-Dforgewrapper.librariesDir=" + directories.getCacheDirectory().getAbsolutePath());
        // The Forge installer jar is really the modpack.jar
        File modpackJar = new File(pack.getBinDir(), "modpack.jar");
        commands.addUnique("-Dforgewrapper.installer=" + modpackJar.getAbsolutePath());
        // We feed ForgeWrapper the unmodified Minecraft jar here
        String mcVersion = MojangUtils.getMinecraftVersion(version);
        File minecraftJar = new File(directories.getCacheDirectory(), "minecraft_" + mcVersion + ".jar");
        commands.addUnique("-Dforgewrapper.minecraft=" + minecraftJar.getAbsolutePath());
    }
    commands.addUnique("-Dminecraft.applet.TargetDirectory=" + pack.getInstalledDirectory().getAbsolutePath());
    commands.addUnique("-Duser.language=en");
    if (!options.getOptions().shouldUseStencilBuffer())
        commands.add("-Dforge.forceNoStencil=true");
    if (operatingSystem.equals(OperatingSystem.OSX)) {
        commands.add("-Xdock:icon=" + options.getIconPath());
        commands.add("-Xdock:name=" + pack.getDisplayName());
    } else if (operatingSystem.equals(OperatingSystem.WINDOWS)) {
        // I have no idea if this helps technic or not.
        commands.addUnique("-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump");
    }
    // build game args
    commands.addUnique("-cp", cpString);
    commands.addRaw(version.getMainClass());
    List<String> mcArgs = version.getMinecraftArguments().resolve(launchOpts, paramDereferencer);
    // We manually iterate over the arguments so we can add them in "--name value" pairs, and exclude duplicates
    // this way. For example: --username Foobar
    ListIterator<String> mcArgsIterator = mcArgs.listIterator();
    String current, next;
    while (mcArgsIterator.hasNext()) {
        current = mcArgsIterator.next();
        if (mcArgsIterator.hasNext()) {
            // We don't consume it so we can later add it to the commands if it turns out to not be a value
            // This allows us to keep the pair detection working correctly, and if we do end up using the value,
            // we just consume it from the iterator later on
            next = mcArgs.get(mcArgsIterator.nextIndex());
        } else {
            next = null;
        }
        // ${auth_player_name} ${auth_session} --gameDir ${game_directory} --assetsDir ${game_assets}
        if (current.startsWith("--")) {
            // This is an --argument, now we check if it has a value or not
            if (next != null && !next.startsWith("--")) {
                // LiteLoader to coexist
                if (current.equals("--tweakClass")) {
                    commands.add(current, next);
                } else {
                    commands.addUnique(current, next);
                }
                // Consume the next element in the iterator
                mcArgsIterator.next();
            } else {
                // This --argument doesn't have a value, so we just add the argument itself as a unique parameter
                commands.addUnique(current);
            }
        } else {
            // Doesn't start with --, so this is not a named argument, and since we have no way of knowing if this
            // is a duplicate or not, we just add it as a regular argument (no duplicate detection)
            commands.add(current);
        }
    }
    options.appendToCommands(commands);
    // TODO: Add all the other less important commands
    return commands.collect();
}
Also used : Path(java.nio.file.Path) OperatingSystem(net.technicpack.utilslib.OperatingSystem) HashMap(java.util.HashMap) JavaVersion(net.technicpack.minecraftcore.mojang.version.io.JavaVersion) ArgumentList(net.technicpack.minecraftcore.mojang.version.io.argument.ArgumentList) IUserType(net.technicpack.launchercore.auth.IUserType) StringSubstitutor(org.apache.commons.text.StringSubstitutor) File(java.io.File)

Aggregations

File (java.io.File)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 IUserType (net.technicpack.launchercore.auth.IUserType)1 JavaVersion (net.technicpack.minecraftcore.mojang.version.io.JavaVersion)1 ArgumentList (net.technicpack.minecraftcore.mojang.version.io.argument.ArgumentList)1 OperatingSystem (net.technicpack.utilslib.OperatingSystem)1 StringSubstitutor (org.apache.commons.text.StringSubstitutor)1