[FEATURE][EXPERIMENTAL] Parse java args method

This commit is contained in:
Gravita 2024-01-07 20:15:29 +07:00
parent 90f6d002d1
commit 8e82f5cd84
2 changed files with 160 additions and 8 deletions

View file

@ -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<String, String> replaceMap, String arg) {
for(var e : replaceMap.entrySet()) {
arg = arg.replace(e.getKey(), e.getValue());
}
return arg;
}
public static List<String> replace(Map<String, String> replaceMap, List<String> args) {
List<String> 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<String> args) {
List<String> classpath = new ArrayList<>();
List<String> jvmArgs = new ArrayList<>();
List<String> 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<String> classpath, List<String> jvmArgs, String mainClass, String mainModule, String jarFile, List<String> 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<byte[]>, JsonDeserializer<byte[]> {
private static final ByteArrayToBase64TypeAdapter INSTANCE = new ByteArrayToBase64TypeAdapter();
private final Base64.Decoder decoder = Base64.getUrlDecoder();

View file

@ -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;