From 8e82f5cd847d17b6ecd4941b93daa61b437e5751 Mon Sep 17 00:00:00 2001 From: Gravita <12893402+gravit0@users.noreply.github.com> Date: Sun, 7 Jan 2024 20:15:29 +0700 Subject: [PATCH] [FEATURE][EXPERIMENTAL] Parse java args method --- .../pro/gravit/utils/helper/CommonHelper.java | 163 +++++++++++++++++- .../gravit/utils/launch/LaunchOptions.java | 5 +- 2 files changed, 160 insertions(+), 8 deletions(-) diff --git a/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java b/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java index 1d5d7ac9..a286c248 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java @@ -2,13 +2,11 @@ import com.google.gson.*; import pro.gravit.utils.command.CommandException; +import pro.gravit.utils.launch.LaunchOptions; import javax.script.ScriptEngine; import java.lang.reflect.Type; -import java.util.Base64; -import java.util.Collection; -import java.util.LinkedList; -import java.util.Locale; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -56,6 +54,21 @@ public static String replace(String source, String... params) { return source; } + public static String replace(Map replaceMap, String arg) { + for(var e : replaceMap.entrySet()) { + arg = arg.replace(e.getKey(), e.getValue()); + } + return arg; + } + + public static List replace(Map replaceMap, List args) { + List updatedList = new ArrayList<>(args.size()); + for(var e : args) { + updatedList.add(replace(replaceMap, e)); + } + return updatedList; + } + public static String[] parseCommand(CharSequence line) throws CommandException { boolean quoted = false; boolean wasQuoted = false; @@ -111,6 +124,148 @@ public static GsonBuilder newBuilder() { ByteArrayToBase64TypeAdapter.INSTANCE); } + + public static ArgsParseResult parseJavaArgs(List args) { + List classpath = new ArrayList<>(); + List jvmArgs = new ArrayList<>(); + List runArgs = new ArrayList<>(); + String jarFile = null; + String mainClass = null; + String mainModule = null; + LaunchOptions.ModuleConf conf = new LaunchOptions.ModuleConf(); + var prevArgType = PrevArgType.NONE; + boolean runArgsBoolean = false; + boolean first = false; + for(var arg : args) { + if(runArgsBoolean) { + runArgs.add(arg); + continue; + } + if(!first) { + if(!arg.startsWith("-")) { + continue; + } + first = true; + } + switch (prevArgType) { + case NONE -> { + + } + case MODULE_PATH -> { + char c = ':'; + int i = arg.indexOf(c); + if(i<0) { + c = ';'; + } + String[] l = arg.split(Character.toString(c)); + conf.modulePath.addAll(Arrays.asList(l)); + prevArgType = PrevArgType.NONE; + continue; + } + case CLASSPATH -> { + char c = ':'; + int i = arg.indexOf(c); + if(i<0) { + c = ';'; + } + String[] l = arg.split(Character.toString(c)); + classpath.addAll(Arrays.asList(l)); + prevArgType = PrevArgType.POST_CLASSPATH; + continue; + } + case ADD_MODULES -> { + String[] l = arg.split(","); + conf.modules.addAll(Arrays.asList(l)); + prevArgType = PrevArgType.NONE; + continue; + } + case ADD_OPENS -> { + String[] l = arg.split("="); + conf.opens.put(l[0], l[1]); + prevArgType = PrevArgType.NONE; + continue; + } + case ADD_EXPORTS -> { + String[] l = arg.split("="); + conf.exports.put(l[0], l[1]); + prevArgType = PrevArgType.NONE; + continue; + } + case ADD_READS -> { + String[] l = arg.split("="); + if(l.length != 2) { + continue; + } + conf.reads.put(l[0], l[1]); + prevArgType = PrevArgType.NONE; + continue; + } + case MODULE -> { + String[] l = arg.split("/"); + mainModule = l[0]; + mainClass = l[1]; + runArgsBoolean = true; + prevArgType = PrevArgType.NONE; + continue; + } + case POST_CLASSPATH -> { + mainClass = arg; + runArgsBoolean = true; + prevArgType = PrevArgType.NONE; + continue; + } + case JAR -> { + jarFile = arg; + runArgsBoolean = true; + prevArgType = PrevArgType.NONE; + continue; + } + } + if(arg.equals("--module-path") || arg.equals("-p")) { + prevArgType = PrevArgType.MODULE_PATH; + continue; + } + if(arg.equals("--classpath") || arg.equals("-cp")) { + prevArgType = PrevArgType.CLASSPATH; + continue; + } + if(arg.equals("--add-modules")) { + prevArgType = PrevArgType.ADD_MODULES; + continue; + } + if(arg.equals("--add-opens")) { + prevArgType = PrevArgType.ADD_OPENS; + continue; + } + if(arg.equals("--add-exports")) { + prevArgType = PrevArgType.ADD_EXPORTS; + continue; + } + if(arg.equals("--add-reads")) { + prevArgType = PrevArgType.ADD_READS; + continue; + } + if(arg.equals("--module") || arg.equals("-m")) { + prevArgType = PrevArgType.MODULE; + continue; + } + if(arg.equals("-jar")) { + prevArgType = PrevArgType.JAR; + continue; + } + jvmArgs.add(arg); + } + return new ArgsParseResult(conf, classpath, jvmArgs, mainClass, mainModule, jarFile, args); + } + + public record ArgsParseResult(LaunchOptions.ModuleConf conf, List classpath, List jvmArgs, String mainClass, String mainModule, String jarFile, List args) { + + } + + private enum PrevArgType { + NONE, MODULE_PATH, ADD_MODULES, ADD_OPENS, ADD_EXPORTS, ADD_READS, CLASSPATH, POST_CLASSPATH, JAR, MAINCLASS, MODULE; + } + private static class ByteArrayToBase64TypeAdapter implements JsonSerializer, JsonDeserializer { private static final ByteArrayToBase64TypeAdapter INSTANCE = new ByteArrayToBase64TypeAdapter(); private final Base64.Decoder decoder = Base64.getUrlDecoder(); diff --git a/LauncherCore/src/main/java/pro/gravit/utils/launch/LaunchOptions.java b/LauncherCore/src/main/java/pro/gravit/utils/launch/LaunchOptions.java index fa95d0d8..ddabd56c 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/launch/LaunchOptions.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/launch/LaunchOptions.java @@ -1,9 +1,6 @@ package pro.gravit.utils.launch; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class LaunchOptions { public boolean enableHacks;