2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launcher;
|
2018-10-22 14:38:01 +03:00
|
|
|
|
2023-07-27 16:03:49 +03:00
|
|
|
import pro.gravit.launcher.client.RuntimeModuleManager;
|
2019-06-03 10:58:10 +03:00
|
|
|
import pro.gravit.launcher.client.DirBridge;
|
2019-12-18 19:24:07 +03:00
|
|
|
import pro.gravit.launcher.utils.DirWatcher;
|
2023-09-19 12:44:09 +03:00
|
|
|
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
|
|
|
|
2023-09-19 12:44:09 +03:00
|
|
|
import javax.swing.*;
|
2019-10-19 19:46:04 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Path;
|
2023-09-13 12:35:01 +03:00
|
|
|
import java.util.*;
|
2019-10-19 19:46:04 +03:00
|
|
|
|
2018-10-22 14:38:01 +03:00
|
|
|
public class ClientLauncherWrapper {
|
2019-04-03 16:27:40 +03:00
|
|
|
public static final String MAGIC_ARG = "-Djdk.attach.allowAttachSelf";
|
2019-04-24 11:49:01 +03:00
|
|
|
public static final String WAIT_PROCESS_PROPERTY = "launcher.waitProcess";
|
2020-06-17 11:32:30 +03:00
|
|
|
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);
|
2021-01-28 20:13:21 +03:00
|
|
|
@LauncherInject("launcher.memory")
|
|
|
|
public static int launcherMemoryLimit;
|
2022-03-11 11:55:54 +03:00
|
|
|
@LauncherInject("launcher.customJvmOptions")
|
|
|
|
public static List<String> customJvmOptions;
|
2023-07-27 16:03:49 +03:00
|
|
|
public static RuntimeModuleManager modulesManager;
|
2020-09-25 18:48:33 +03:00
|
|
|
|
2023-09-13 12:35:01 +03:00
|
|
|
public static boolean contains(String[] array, String value) {
|
|
|
|
for(String s : array) {
|
|
|
|
if(s.equals(value)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-22 14:38:01 +03:00
|
|
|
public static void main(String[] arguments) throws IOException, InterruptedException {
|
|
|
|
LogHelper.printVersion("Launcher");
|
2018-11-27 14:34:39 +03:00
|
|
|
LogHelper.printLicense("Launcher");
|
2018-10-22 14:38:01 +03:00
|
|
|
JVMHelper.checkStackTrace(ClientLauncherWrapper.class);
|
|
|
|
JVMHelper.verifySystemProperties(Launcher.class, true);
|
2018-10-26 17:42:20 +03:00
|
|
|
EnvHelper.checkDangerousParams();
|
2019-04-24 11:49:01 +03:00
|
|
|
LauncherConfig config = Launcher.getConfig();
|
2023-07-27 16:03:49 +03:00
|
|
|
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) {
|
2019-04-24 11:49:01 +03:00
|
|
|
LogHelper.warning("Found -Dlauncher.debug=true");
|
|
|
|
}
|
2019-05-15 14:11:22 +03:00
|
|
|
if (System.getProperty(LogHelper.STACKTRACE_PROPERTY) != null) {
|
2019-04-24 11:49:01 +03:00
|
|
|
LogHelper.warning("Found -Dlauncher.stacktrace=true");
|
|
|
|
}
|
|
|
|
LogHelper.info("Debug mode disabled (found env PRODUCTION)");
|
2019-05-15 14:11:22 +03:00
|
|
|
} else {
|
2019-04-24 11:49:01 +03:00
|
|
|
LogHelper.info("If need debug output use -Dlauncher.debug=true");
|
|
|
|
LogHelper.info("If need stacktrace output use -Dlauncher.stacktrace=true");
|
2023-09-13 12:35:01 +03:00
|
|
|
if(contains(arguments, "--debug")) {
|
|
|
|
LogHelper.setDebugEnabled(true);
|
|
|
|
LogHelper.setStacktraceEnabled(true);
|
|
|
|
}
|
2019-05-15 14:11:22 +03:00
|
|
|
if (LogHelper.isDebugEnabled()) waitProcess = true;
|
2019-04-24 11:49:01 +03:00
|
|
|
}
|
|
|
|
LogHelper.info("Restart Launcher with JavaAgent...");
|
2021-04-15 18:02:30 +03:00
|
|
|
ClientLauncherWrapperContext context = new ClientLauncherWrapperContext();
|
|
|
|
context.processBuilder = new ProcessBuilder();
|
|
|
|
if (waitProcess) context.processBuilder.inheritIO();
|
2020-06-17 11:32:30 +03:00
|
|
|
|
2021-05-31 01:51:40 +03:00
|
|
|
context.javaVersion = null;
|
2020-06-08 11:45:49 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 11:32:30 +03:00
|
|
|
} catch (Throwable e) {
|
2020-06-08 11:45:49 +03:00
|
|
|
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
|
|
|
}
|
2023-09-19 12:44:09 +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);
|
|
|
|
}
|
2020-06-17 11:32:30 +03:00
|
|
|
|
2021-04-15 18:02:30 +03:00
|
|
|
context.executePath = IOHelper.resolveJavaBin(context.javaVersion.jvmDir);
|
2023-07-27 16:03:49 +03:00
|
|
|
String pathLauncher = IOHelper.getCodeSource(ClientLauncherWrapper.class).toString();
|
|
|
|
context.mainClass = "pro.gravit.launcher.LauncherEngineWrapper";
|
2021-04-15 18:02:30 +03:00
|
|
|
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");
|
2021-04-15 18:02:30 +03:00
|
|
|
context.args.add(MAGIC_ARG);
|
|
|
|
context.args.add("-XX:+DisableAttachMechanism");
|
2023-09-13 12:35:01 +03:00
|
|
|
context.clientArgs.addAll(Arrays.asList(arguments));
|
2021-04-15 18:02:30 +03:00
|
|
|
EnvHelper.addEnv(context.processBuilder);
|
2023-07-27 16:03:49 +03:00
|
|
|
modulesManager.callWrapper(context);
|
2021-04-15 18:02:30 +03:00
|
|
|
// ---------
|
|
|
|
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) {
|
2021-04-15 18:02:30 +03:00
|
|
|
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) {
|
2021-04-15 18:02:30 +03:00
|
|
|
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(",");
|
2021-04-15 18:02:30 +03:00
|
|
|
modulesAdd.append(moduleName);
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 12:17:29 +03:00
|
|
|
if (modulesAdd.length() > 0) {
|
2020-01-04 17:54:17 +03:00
|
|
|
args.add("--add-modules");
|
2021-04-15 18:02:30 +03:00
|
|
|
args.add(modulesAdd.toString());
|
|
|
|
}
|
2021-05-25 12:17:29 +03:00
|
|
|
if (modulesPath.length() > 0) {
|
2019-11-28 20:12:40 +03:00
|
|
|
args.add("--module-path");
|
2021-04-15 18:02:30 +03:00
|
|
|
args.add(modulesPath.toString());
|
2019-09-18 00:11:13 +03:00
|
|
|
}
|
2019-09-17 23:32:57 +03:00
|
|
|
}
|
2021-04-15 18:02:30 +03:00
|
|
|
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));
|
|
|
|
}
|
2021-04-15 18:02:30 +03:00
|
|
|
args.add(context.mainClass);
|
|
|
|
args.addAll(context.clientArgs);
|
2018-10-24 16:40:51 +03:00
|
|
|
LogHelper.debug("Commandline: " + args);
|
2021-04-15 18:02:30 +03:00
|
|
|
context.processBuilder.command(args);
|
|
|
|
Process process = context.processBuilder.start();
|
2019-04-24 11:49:01 +03:00
|
|
|
if (!waitProcess) {
|
2018-10-22 14:38:01 +03:00
|
|
|
Thread.sleep(3000);
|
|
|
|
if (!process.isAlive()) {
|
2018-11-27 14:34:39 +03:00
|
|
|
int errorcode = process.exitValue();
|
2018-12-20 18:45:01 +03:00
|
|
|
if (errorcode != 0)
|
2019-03-10 15:01:14 +03:00
|
|
|
LogHelper.error("Process exit with error code: %d", errorcode);
|
2018-11-27 14:34:39 +03:00
|
|
|
else
|
2019-03-10 15:01:14 +03:00
|
|
|
LogHelper.info("Process exit with code 0");
|
2018-10-22 14:38:01 +03:00
|
|
|
} else {
|
|
|
|
LogHelper.debug("Process started success");
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
} else {
|
2018-10-22 14:38:01 +03:00
|
|
|
process.waitFor();
|
|
|
|
}
|
|
|
|
}
|
2019-10-19 19:46:04 +03:00
|
|
|
|
2021-05-25 12:17:29 +03:00
|
|
|
|
2021-04-15 18:02:30 +03:00
|
|
|
public static class ClientLauncherWrapperContext {
|
2021-06-18 07:34:32 +03:00
|
|
|
public JavaHelper.JavaVersion javaVersion;
|
2021-04-15 18:02:30 +03:00
|
|
|
public Path executePath;
|
|
|
|
public String mainClass;
|
|
|
|
public int memoryLimit;
|
2023-04-15 13:40:34 +03:00
|
|
|
public boolean useLegacyClasspathProperty;
|
2021-04-15 18:02:30 +03:00
|
|
|
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
|
|
|
|
2021-04-15 18:02:30 +03:00
|
|
|
public void addSystemProperty(String name) {
|
|
|
|
String property = System.getProperty(name);
|
|
|
|
if (property != null)
|
|
|
|
jvmProperties.put(name, property);
|
|
|
|
}
|
|
|
|
}
|
2018-10-22 14:38:01 +03:00
|
|
|
}
|