2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launcher;
|
2018-10-22 14:38:01 +03:00
|
|
|
|
2019-06-03 10:58:10 +03:00
|
|
|
import pro.gravit.launcher.client.ClientLauncher;
|
2019-09-17 23:25:59 +03:00
|
|
|
import pro.gravit.launcher.client.ClientModuleManager;
|
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;
|
2019-06-03 10:58:10 +03:00
|
|
|
import pro.gravit.utils.helper.EnvHelper;
|
|
|
|
import pro.gravit.utils.helper.IOHelper;
|
|
|
|
import pro.gravit.utils.helper.JVMHelper;
|
|
|
|
import pro.gravit.utils.helper.LogHelper;
|
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
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";
|
2019-09-17 23:32:57 +03:00
|
|
|
public static final String NO_JAVA9_CHECK_PROPERTY = "launcher.noJava9Check";
|
2019-04-24 11:49:01 +03:00
|
|
|
public static boolean waitProcess = Boolean.getBoolean(WAIT_PROCESS_PROPERTY);
|
2019-10-19 19:43:25 +03:00
|
|
|
public static final boolean noJava9check = Boolean.getBoolean(NO_JAVA9_CHECK_PROPERTY);
|
2019-04-03 16:27:40 +03:00
|
|
|
|
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();
|
2019-09-17 23:25:59 +03:00
|
|
|
LauncherEngine.modulesManager = new ClientModuleManager();
|
2020-01-31 21:21:24 +03:00
|
|
|
LauncherConfig.initModules(LauncherEngine.modulesManager);
|
2019-09-17 23:25:59 +03:00
|
|
|
|
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");
|
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...");
|
2018-10-22 14:38:01 +03:00
|
|
|
ProcessBuilder processBuilder = new ProcessBuilder();
|
2019-05-15 14:11:22 +03:00
|
|
|
if (waitProcess) processBuilder.inheritIO();
|
2018-10-22 14:38:01 +03:00
|
|
|
Path javaBin = IOHelper.resolveJavaBin(Paths.get(System.getProperty("java.home")));
|
|
|
|
List<String> args = new LinkedList<>();
|
|
|
|
args.add(javaBin.toString());
|
|
|
|
String pathLauncher = IOHelper.getCodeSource(ClientLauncher.class).toString();
|
2018-10-24 16:40:51 +03:00
|
|
|
args.add(JVMHelper.jvmProperty(LogHelper.DEBUG_PROPERTY, Boolean.toString(LogHelper.isDebugEnabled())));
|
2019-01-03 17:26:54 +03:00
|
|
|
args.add(JVMHelper.jvmProperty(LogHelper.STACKTRACE_PROPERTY, Boolean.toString(LogHelper.isStacktraceEnabled())));
|
2019-04-24 11:49:01 +03:00
|
|
|
args.add(JVMHelper.jvmProperty(LogHelper.DEV_PROPERTY, Boolean.toString(LogHelper.isDevEnabled())));
|
2019-03-12 11:24:48 +03:00
|
|
|
JVMHelper.addSystemPropertyToArgs(args, DirBridge.CUSTOMDIR_PROPERTY);
|
|
|
|
JVMHelper.addSystemPropertyToArgs(args, DirBridge.USE_CUSTOMDIR_PROPERTY);
|
|
|
|
JVMHelper.addSystemPropertyToArgs(args, DirBridge.USE_OPTDIR_PROPERTY);
|
2019-12-18 19:24:07 +03:00
|
|
|
JVMHelper.addSystemPropertyToArgs(args, DirWatcher.IGN_OVERFLOW);
|
2019-10-19 19:46:04 +03:00
|
|
|
if (!noJava9check && !System.getProperty("java.version").startsWith("1.8")) {
|
2019-09-17 23:32:57 +03:00
|
|
|
LogHelper.debug("Found Java 9+ ( %s )", System.getProperty("java.version"));
|
2019-09-18 00:11:13 +03:00
|
|
|
Path jvmDir = Paths.get(System.getProperty("java.home"));
|
|
|
|
String pathToFx = System.getenv("PATH_TO_FX");
|
|
|
|
Path fxPath = pathToFx == null ? null : Paths.get(pathToFx);
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
Path[] findPath = new Path[]{jvmDir, fxPath};
|
|
|
|
tryAddModule(findPath, "javafx.base", builder);
|
|
|
|
tryAddModule(findPath, "javafx.graphics", builder);
|
|
|
|
tryAddModule(findPath, "javafx.fxml", builder);
|
|
|
|
tryAddModule(findPath, "javafx.controls", builder);
|
|
|
|
String modulePath = builder.toString();
|
2019-10-19 19:46:04 +03:00
|
|
|
if (!modulePath.isEmpty()) {
|
2020-01-04 17:54:17 +03:00
|
|
|
args.add("--add-modules");
|
|
|
|
args.add("javafx.base,javafx.fxml,javafx.controls,jdk.unsupported");
|
2019-11-28 20:12:40 +03:00
|
|
|
args.add("--module-path");
|
|
|
|
args.add(modulePath);
|
2019-09-18 00:11:13 +03:00
|
|
|
}
|
2019-09-17 23:32:57 +03:00
|
|
|
}
|
2019-11-28 20:12:40 +03:00
|
|
|
args.add(MAGIC_ARG);
|
|
|
|
args.add("-XX:+DisableAttachMechanism");
|
|
|
|
args.add("-Xmx256M");
|
2019-11-10 03:00:22 +03:00
|
|
|
//Collections.addAll(args, "-javaagent:".concat(pathLauncher));
|
2019-11-28 20:12:40 +03:00
|
|
|
args.add("-cp");
|
|
|
|
args.add(pathLauncher);
|
|
|
|
args.add(LauncherEngine.class.getName());
|
2019-09-17 23:25:59 +03:00
|
|
|
LauncherEngine.modulesManager.callWrapper(processBuilder, args);
|
2018-10-22 14:38:01 +03:00
|
|
|
EnvHelper.addEnv(processBuilder);
|
2018-10-24 16:40:51 +03:00
|
|
|
LogHelper.debug("Commandline: " + args);
|
2018-10-22 14:38:01 +03:00
|
|
|
processBuilder.command(args);
|
|
|
|
Process process = 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
|
|
|
|
|
|
|
public static Path tryFindModule(Path path, String moduleName) {
|
2019-09-18 00:11:13 +03:00
|
|
|
Path result = path.resolve(moduleName.concat(".jar"));
|
|
|
|
LogHelper.dev("Try resolve %s", result.toString());
|
2019-10-19 19:46:04 +03:00
|
|
|
if (!IOHelper.isFile(result))
|
2019-09-18 00:11:13 +03:00
|
|
|
result = path.resolve("lib").resolve(moduleName.concat(".jar"));
|
|
|
|
else return result;
|
2019-10-19 19:46:04 +03:00
|
|
|
if (!IOHelper.isFile(result))
|
2019-09-18 00:11:13 +03:00
|
|
|
return null;
|
|
|
|
else return result;
|
|
|
|
}
|
2019-10-19 19:46:04 +03:00
|
|
|
|
|
|
|
public static boolean tryAddModule(Path[] paths, String moduleName, StringBuilder args) {
|
|
|
|
for (Path path : paths) {
|
|
|
|
if (path == null) continue;
|
2019-09-18 00:11:13 +03:00
|
|
|
Path result = tryFindModule(path, moduleName);
|
2019-10-19 19:46:04 +03:00
|
|
|
if (result != null) {
|
|
|
|
if (args.length() != 0) args.append(File.pathSeparatorChar);
|
2019-09-18 00:11:13 +03:00
|
|
|
args.append(result.toAbsolutePath().toString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-22 14:38:01 +03:00
|
|
|
}
|