[FEATURE] Arch/Debian openjfx search

This commit is contained in:
Gravita 2021-04-18 18:03:47 +07:00
parent 64661abf0f
commit 66db1eb861

View file

@ -1,5 +1,7 @@
package pro.gravit.launchserver.components; package pro.gravit.launchserver.components;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.Reconfigurable; import pro.gravit.launchserver.Reconfigurable;
import pro.gravit.launchserver.binary.tasks.LauncherBuildTask; import pro.gravit.launchserver.binary.tasks.LauncherBuildTask;
@ -26,6 +28,7 @@ public class ProGuardComponent extends Component implements AutoCloseable, Recon
public boolean enabled = true; public boolean enabled = true;
public boolean mappings = true; public boolean mappings = true;
public transient ProguardConf proguardConf; public transient ProguardConf proguardConf;
private transient static final Logger logger = LogManager.getLogger();
@Override @Override
public void init(LaunchServer launchServer) { public void init(LaunchServer launchServer) {
proguardConf = new ProguardConf(launchServer, this); proguardConf = new ProguardConf(launchServer, this);
@ -84,22 +87,21 @@ public Path process(Path inputFile) throws IOException {
Path outputJar = server.launcherBinary.nextLowerPath(this); Path outputJar = server.launcherBinary.nextLowerPath(this);
if (component.enabled) { if (component.enabled) {
Configuration proguard_cfg = new Configuration(); Configuration proguard_cfg = new Configuration();
ConfigurationParser parser = new ConfigurationParser(proguardConf.buildConfig(inputFile, outputJar), if(!checkJMods(IOHelper.JVM_DIR.resolve("jmods"))) {
proguardConf.proguard.toFile(), System.getProperties()); logger.error("Java path: {} is not JDK! Please install JDK", IOHelper.JVM_DIR);
if (JVMHelper.JVM_VERSION >= 9) { }
Path javaJModsPath = Paths.get(System.getProperty("java.home")).resolve("jmods"); Path jfxPath = tryFindOpenJFXPath(IOHelper.JVM_DIR);
if (!IOHelper.exists(javaJModsPath)) { if(checkFXJMods(IOHelper.JVM_DIR.resolve("jmods"))) {
LogHelper.warning("Directory %s not found. It is not good", javaJModsPath); logger.debug("JavaFX jmods resolved in JDK path");
jfxPath = null;
}
else if(jfxPath != null && checkFXJMods(jfxPath)) {
logger.debug("JMods resolved in {}", jfxPath.toString());
} else { } else {
//Find javaFX libraries logger.error("JavaFX jmods not found. May be install OpenJFX?");
if (!IOHelper.exists(javaJModsPath.resolve("javafx.base.jmod")))
LogHelper.error("javafx.base.jmod not found. Launcher can be assembled incorrectly. Maybe you need to install OpenJFX?");
if (!IOHelper.exists(javaJModsPath.resolve("javafx.graphics.jmod")))
LogHelper.error("javafx.graphics.jmod not found. Launcher can be assembled incorrectly. Maybe you need to install OpenJFX?");
if (!IOHelper.exists(javaJModsPath.resolve("javafx.controls.jmod")))
LogHelper.error("javafx.controls.jmod not found. Launcher can be assembled incorrectly. Maybe you need to install OpenJFX?");
}
} }
ConfigurationParser parser = new ConfigurationParser(proguardConf.buildConfig(inputFile, outputJar, jfxPath == null ? new Path[0] : new Path[]{jfxPath}),
proguardConf.proguard.toFile(), System.getProperties());
try { try {
parser.parse(proguard_cfg); parser.parse(proguard_cfg);
ProGuard proGuard = new ProGuard(proguard_cfg); ProGuard proGuard = new ProGuard(proguard_cfg);
@ -118,6 +120,39 @@ public boolean allowDelete() {
} }
} }
public static boolean checkFXJMods(Path path) {
if (!IOHelper.exists(path.resolve("javafx.base.jmod")))
return false;
if (!IOHelper.exists(path.resolve("javafx.graphics.jmod")))
return false;
return IOHelper.exists(path.resolve("javafx.controls.jmod"));
}
public static boolean checkJMods(Path path) {
return IOHelper.exists(path.resolve("java.base.jmod"));
}
public static Path tryFindOpenJFXPath(Path jvmDir) {
String dirName = jvmDir.getFileName().toString();
Path parent = jvmDir.getParent();
if(parent == null) return null;
Path archJFXPath = parent.resolve(dirName.replace("openjdk", "openjfx")).resolve("jmods");
if(Files.isDirectory(archJFXPath)) {
return archJFXPath;
}
Path arch2JFXPath = parent.resolve(dirName.replace("jdk", "openjfx")).resolve("jmods");
if(Files.isDirectory(arch2JFXPath)) {
return arch2JFXPath;
}
if(JVMHelper.OS_TYPE == JVMHelper.OS.LINUX) {
Path debianJfxPath = Paths.get("/usr/share/openjfx/jmods");
if(Files.isDirectory(debianJfxPath)) {
return debianJfxPath;
}
}
return null;
}
public static class ProguardConf { public static class ProguardConf {
public static final String[] JAVA9_OPTS = new String[]{ public static final String[] JAVA9_OPTS = new String[]{
"-libraryjars '<java.home>/jmods/'" "-libraryjars '<java.home>/jmods/'"
@ -155,7 +190,7 @@ private static String generateString(SecureRandom rand, String lowString, String
return sb.toString(); return sb.toString();
} }
public String[] buildConfig(Path inputJar, Path outputJar) { public String[] buildConfig(Path inputJar, Path outputJar, Path[] jfxPath) {
List<String> confStrs = new ArrayList<>(); List<String> confStrs = new ArrayList<>();
prepare(false); prepare(false);
if (component.mappings) if (component.mappings)
@ -163,13 +198,18 @@ public String[] buildConfig(Path inputJar, Path outputJar) {
confStrs.add("-obfuscationdictionary '" + words.toFile().getName() + "'"); confStrs.add("-obfuscationdictionary '" + words.toFile().getName() + "'");
confStrs.add("-injar '" + inputJar.toAbsolutePath() + "'"); confStrs.add("-injar '" + inputJar.toAbsolutePath() + "'");
confStrs.add("-outjar '" + outputJar.toAbsolutePath() + "'"); confStrs.add("-outjar '" + outputJar.toAbsolutePath() + "'");
Collections.addAll(confStrs, JVMHelper.JVM_VERSION >= 9 ? JAVA9_OPTS : JAVA8_OPTS); Collections.addAll(confStrs, JAVA9_OPTS);
if(jfxPath != null) {
for(Path path : jfxPath) {
confStrs.add(String.format("-libraryjars '%s'", path.toAbsolutePath()));
}
}
srv.launcherBinary.coreLibs.stream() srv.launcherBinary.coreLibs.stream()
.map(e -> "-libraryjars '" + e.toAbsolutePath().toString() + "'") .map(e -> "-libraryjars '" + e.toAbsolutePath() + "'")
.forEach(confStrs::add); .forEach(confStrs::add);
srv.launcherBinary.addonLibs.stream() srv.launcherBinary.addonLibs.stream()
.map(e -> "-libraryjars '" + e.toAbsolutePath().toString() + "'") .map(e -> "-libraryjars '" + e.toAbsolutePath() + "'")
.forEach(confStrs::add); .forEach(confStrs::add);
confStrs.add("-classobfuscationdictionary '" + words.toFile().getName() + "'"); confStrs.add("-classobfuscationdictionary '" + words.toFile().getName() + "'");
confStrs.add("@".concat(config.toFile().getName())); confStrs.add("@".concat(config.toFile().getName()));