Launcher/LauncherStart/src/main/java/pro/gravit/launcher/ClientLauncherWrapper.java

213 lines
9.7 KiB
Java
Raw Normal View History

package pro.gravit.launcher;
import pro.gravit.launcher.client.RuntimeModuleManager;
2019-06-03 10:58:10 +03:00
import pro.gravit.launcher.client.DirBridge;
import pro.gravit.launcher.utils.DirWatcher;
import pro.gravit.utils.Version;
2021-06-18 07:34:32 +03:00
import pro.gravit.utils.helper.*;
2019-06-03 10:58:10 +03:00
import javax.swing.*;
2019-10-19 19:46:04 +03:00
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
2019-10-19 19:46:04 +03:00
public class ClientLauncherWrapper {
2019-04-03 16:27:40 +03:00
public static final String MAGIC_ARG = "-Djdk.attach.allowAttachSelf";
public static final String WAIT_PROCESS_PROPERTY = "launcher.waitProcess";
public static final String NO_JAVA_CHECK_PROPERTY = "launcher.noJavaCheck";
public static boolean noJavaCheck = Boolean.getBoolean(NO_JAVA_CHECK_PROPERTY);
2020-04-05 10:27:04 +03:00
public static boolean waitProcess = Boolean.getBoolean(WAIT_PROCESS_PROPERTY);
@LauncherInject("launcher.memory")
public static int launcherMemoryLimit;
2022-03-11 11:55:54 +03:00
@LauncherInject("launcher.customJvmOptions")
public static List<String> customJvmOptions;
public static RuntimeModuleManager modulesManager;
2020-09-25 18:48:33 +03:00
public static boolean contains(String[] array, String value) {
for(String s : array) {
if(s.equals(value)) {
return true;
}
}
return false;
}
public static void main(String[] arguments) throws IOException, InterruptedException {
LogHelper.printVersion("Launcher");
LogHelper.printLicense("Launcher");
JVMHelper.checkStackTrace(ClientLauncherWrapper.class);
JVMHelper.verifySystemProperties(Launcher.class, true);
2018-10-26 17:42:20 +03:00
EnvHelper.checkDangerousParams();
LauncherConfig config = Launcher.getConfig();
modulesManager = new RuntimeModuleManager();
LauncherConfig.initModules(modulesManager);
2019-10-19 19:52:57 +03:00
LogHelper.info("Launcher for project %s", config.projectName);
2019-05-15 14:11:22 +03:00
if (config.environment.equals(LauncherConfig.LauncherEnvironment.PROD)) {
if (System.getProperty(LogHelper.DEBUG_PROPERTY) != null) {
LogHelper.warning("Found -Dlauncher.debug=true");
}
2019-05-15 14:11:22 +03:00
if (System.getProperty(LogHelper.STACKTRACE_PROPERTY) != null) {
LogHelper.warning("Found -Dlauncher.stacktrace=true");
}
LogHelper.info("Debug mode disabled (found env PRODUCTION)");
2019-05-15 14:11:22 +03:00
} else {
LogHelper.info("If need debug output use -Dlauncher.debug=true");
LogHelper.info("If need stacktrace output use -Dlauncher.stacktrace=true");
if(contains(arguments, "--debug")) {
LogHelper.setDebugEnabled(true);
LogHelper.setStacktraceEnabled(true);
}
2019-05-15 14:11:22 +03:00
if (LogHelper.isDebugEnabled()) waitProcess = true;
}
LogHelper.info("Restart Launcher with JavaAgent...");
ClientLauncherWrapperContext context = new ClientLauncherWrapperContext();
context.processBuilder = new ProcessBuilder();
if (waitProcess) context.processBuilder.inheritIO();
2021-05-31 01:51:40 +03:00
context.javaVersion = null;
try {
2021-05-28 23:14:03 +03:00
if (!noJavaCheck) {
2021-06-18 07:34:32 +03:00
List<JavaHelper.JavaVersion> javaVersions = JavaHelper.findJava();
for (JavaHelper.JavaVersion version : javaVersions) {
2021-05-31 04:25:33 +03:00
LogHelper.debug("Found Java %d b%d in %s javafx %s", version.version, version.build, version.jvmDir.toString(), version.enabledJavaFX ? "supported" : "not supported");
2021-05-28 23:14:03 +03:00
if (context.javaVersion == null) {
context.javaVersion = version;
continue;
}
if (version.enabledJavaFX && !context.javaVersion.enabledJavaFX) {
context.javaVersion = version;
continue;
}
2021-06-01 01:48:33 +03:00
if (version.enabledJavaFX == context.javaVersion.enabledJavaFX) {
2021-05-28 23:14:03 +03:00
if (context.javaVersion.version < version.version) {
context.javaVersion = version;
} else if (context.javaVersion.version == version.version && context.javaVersion.build < version.build) {
context.javaVersion = version;
}
}
}
}
} catch (Throwable e) {
LogHelper.error(e);
}
2021-05-31 04:25:33 +03:00
if (context.javaVersion == null) {
2021-06-18 07:34:32 +03:00
context.javaVersion = JavaHelper.JavaVersion.getCurrentJavaVersion();
2021-05-31 04:25:33 +03:00
}
if(context.javaVersion.version < 17) {
String message = String.format("GravitLauncher v%s required Java 17 or higher", Version.getVersion());
LogHelper.error(message);
JOptionPane.showMessageDialog(null, message, "GravitLauncher", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
context.executePath = IOHelper.resolveJavaBin(context.javaVersion.jvmDir);
String pathLauncher = IOHelper.getCodeSource(ClientLauncherWrapper.class).toString();
context.mainClass = "pro.gravit.launcher.LauncherEngineWrapper";
context.memoryLimit = launcherMemoryLimit;
context.classpath.add(pathLauncher);
context.jvmProperties.put(LogHelper.DEBUG_PROPERTY, Boolean.toString(LogHelper.isDebugEnabled()));
context.jvmProperties.put(LogHelper.STACKTRACE_PROPERTY, Boolean.toString(LogHelper.isStacktraceEnabled()));
context.jvmProperties.put(LogHelper.DEV_PROPERTY, Boolean.toString(LogHelper.isDevEnabled()));
context.addSystemProperty(DirBridge.CUSTOMDIR_PROPERTY);
context.addSystemProperty(DirBridge.USE_CUSTOMDIR_PROPERTY);
context.addSystemProperty(DirBridge.USE_OPTDIR_PROPERTY);
context.addSystemProperty(DirWatcher.IGN_OVERFLOW);
context.jvmModules.add("javafx.base");
context.jvmModules.add("javafx.graphics");
context.jvmModules.add("javafx.fxml");
context.jvmModules.add("javafx.controls");
context.jvmModules.add("javafx.swing");
2021-05-29 00:42:09 +03:00
context.jvmModules.add("javafx.media");
context.jvmModules.add("javafx.web");
context.args.add(MAGIC_ARG);
context.args.add("-XX:+DisableAttachMechanism");
context.clientArgs.addAll(Arrays.asList(arguments));
EnvHelper.addEnv(context.processBuilder);
modulesManager.callWrapper(context);
// ---------
List<String> args = new ArrayList<>(16);
args.add(context.executePath.toAbsolutePath().toString());
args.addAll(context.args);
2021-05-25 12:17:29 +03:00
context.jvmProperties.forEach((key, value) -> args.add(String.format("-D%s=%s", key, value)));
if (context.javaVersion.version >= 9) {
context.javaFXPaths.add(context.javaVersion.jvmDir);
context.javaFXPaths.add(context.javaVersion.jvmDir.resolve("jre"));
2021-06-18 07:34:32 +03:00
Path openjfxPath = JavaHelper.tryGetOpenJFXPath(context.javaVersion.jvmDir);
2021-05-25 12:17:29 +03:00
if (openjfxPath != null) {
context.javaFXPaths.add(openjfxPath);
}
StringBuilder modulesPath = new StringBuilder();
StringBuilder modulesAdd = new StringBuilder();
2021-05-25 12:17:29 +03:00
for (String moduleName : context.jvmModules) {
2021-06-18 07:34:32 +03:00
boolean success = JavaHelper.tryAddModule(context.javaFXPaths, moduleName, modulesPath);
2021-05-25 12:17:29 +03:00
if (success) {
if (modulesAdd.length() > 0) modulesAdd.append(",");
modulesAdd.append(moduleName);
}
}
2021-05-25 12:17:29 +03:00
if (modulesAdd.length() > 0) {
args.add("--add-modules");
args.add(modulesAdd.toString());
}
2021-05-25 12:17:29 +03:00
if (modulesPath.length() > 0) {
args.add("--module-path");
args.add(modulesPath.toString());
}
}
if (context.memoryLimit != 0) {
args.add(String.format("-Xmx%dM", context.memoryLimit));
2021-03-20 11:50:32 +03:00
}
2022-11-18 10:47:03 +03:00
if (customJvmOptions != null) {
2022-03-11 11:55:54 +03:00
args.addAll(customJvmOptions);
}
2023-04-15 13:40:34 +03:00
if(context.useLegacyClasspathProperty) {
args.add(String.format("-Djava.class.path=%s", String.join(IOHelper.PLATFORM_SEPARATOR, context.classpath)));
} else {
args.add("-cp");
args.add(String.join(IOHelper.PLATFORM_SEPARATOR, context.classpath));
}
args.add(context.mainClass);
args.addAll(context.clientArgs);
2018-10-24 16:40:51 +03:00
LogHelper.debug("Commandline: " + args);
context.processBuilder.command(args);
Process process = context.processBuilder.start();
if (!waitProcess) {
Thread.sleep(3000);
if (!process.isAlive()) {
int errorcode = process.exitValue();
2018-12-20 18:45:01 +03:00
if (errorcode != 0)
LogHelper.error("Process exit with error code: %d", errorcode);
else
LogHelper.info("Process exit with code 0");
} else {
LogHelper.debug("Process started success");
}
2018-11-08 15:30:16 +03:00
} else {
process.waitFor();
}
}
2019-10-19 19:46:04 +03:00
2021-05-25 12:17:29 +03:00
public static class ClientLauncherWrapperContext {
2021-06-18 07:34:32 +03:00
public JavaHelper.JavaVersion javaVersion;
public Path executePath;
public String mainClass;
public int memoryLimit;
2023-04-15 13:40:34 +03:00
public boolean useLegacyClasspathProperty;
public ProcessBuilder processBuilder;
public List<String> args = new ArrayList<>(8);
public Map<String, String> jvmProperties = new HashMap<>();
public List<String> classpath = new ArrayList<>();
public List<String> jvmModules = new ArrayList<>();
public List<String> clientArgs = new ArrayList<>();
public List<Path> javaFXPaths = new ArrayList<>();
2021-05-25 12:17:29 +03:00
public void addSystemProperty(String name) {
String property = System.getProperty(name);
if (property != null)
jvmProperties.put(name, property);
}
}
}