From 62e92764816dee939f27cb10806978545ae28a36 Mon Sep 17 00:00:00 2001 From: Gravita <12893402+gravit0@users.noreply.github.com> Date: Wed, 25 Oct 2023 22:41:06 +0700 Subject: [PATCH] [FEATURE] Update build pipeline, fix multi release jar with proguard --- .../launchserver/binary/BinaryPipeline.java | 20 ++++-- .../binary/EXEL4JLauncherBinary.java | 2 +- .../binary/EXELauncherBinary.java | 2 +- .../binary/JARLauncherBinary.java | 2 +- .../tasks/AdditionalFixesApplyTask.java | 5 -- .../binary/tasks/AttachJarsTask.java | 5 -- .../binary/tasks/CertificateAutogenTask.java | 5 -- .../binary/tasks/CompressBuildTask.java | 5 -- .../binary/tasks/LauncherBuildTask.java | 2 - .../binary/tasks/MainBuildTask.java | 7 +- .../binary/tasks/PrepareBuildTask.java | 5 -- .../binary/tasks/SignJarTask.java | 5 -- .../binary/tasks/exe/Launch4JTask.java | 5 -- .../components/ProGuardComponent.java | 68 ++++++++++++++++--- props.gradle | 2 +- 15 files changed, 78 insertions(+), 62 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/BinaryPipeline.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/BinaryPipeline.java index c3f8437b..dec8e0e2 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/BinaryPipeline.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/BinaryPipeline.java @@ -18,7 +18,6 @@ public class BinaryPipeline { public final List tasks = new ArrayList<>(); - public final AtomicLong count = new AtomicLong(0); public final Path buildDir; public final String nameFormat; private transient final Logger logger = LogManager.getLogger(); @@ -72,11 +71,20 @@ public Optional getTaskByClass(Class taskCla return tasks.stream().filter(taskClass::isInstance).map(taskClass::cast).findFirst(); } + public Optional getTaskBefore(Predicate pred) { + LauncherBuildTask last = null; + for(var e : tasks) { + if(pred.test(e)) { + return Optional.ofNullable(last); + } + last = e; + } + return Optional.empty(); + } + public void build(Path target, boolean deleteTempFiles) throws IOException { logger.info("Building launcher binary file"); - count.set(0); // set jar number Path thisPath = null; - boolean isNeedDelete = false; long time_start = System.currentTimeMillis(); long time_this = time_start; for (LauncherBuildTask task : tasks) { @@ -86,19 +94,17 @@ public void build(Path target, boolean deleteTempFiles) throws IOException { long time_task_end = System.currentTimeMillis(); long time_task = time_task_end - time_this; time_this = time_task_end; - if (isNeedDelete && deleteTempFiles && oldPath != thisPath) Files.deleteIfExists(oldPath); - isNeedDelete = task.allowDelete(); logger.info("Task {} processed from {} millis", task.getName(), time_task); } long time_end = System.currentTimeMillis(); - if (isNeedDelete && deleteTempFiles) IOHelper.move(thisPath, target); + if (deleteTempFiles) IOHelper.move(thisPath, target); else IOHelper.copy(thisPath, target); IOHelper.deleteDir(buildDir, false); logger.info("Build successful from {} millis", time_end - time_start); } public String nextName(String taskName) { - return nameFormat.formatted(taskName, count.getAndIncrement()); + return nameFormat.formatted(taskName); } public Path nextPath(String taskName) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/EXEL4JLauncherBinary.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/EXEL4JLauncherBinary.java index c9a0a3e2..26a2b457 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/EXEL4JLauncherBinary.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/EXEL4JLauncherBinary.java @@ -7,7 +7,7 @@ public final class EXEL4JLauncherBinary extends LauncherBinary { public EXEL4JLauncherBinary(LaunchServer server) { - super(server, LauncherBinary.resolve(server, ".exe"), "Launcher-%s-%d.exe"); + super(server, LauncherBinary.resolve(server, ".exe"), "Launcher-%s.exe"); } @Override diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/EXELauncherBinary.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/EXELauncherBinary.java index 2a33ba81..763d9912 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/EXELauncherBinary.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/EXELauncherBinary.java @@ -9,7 +9,7 @@ public class EXELauncherBinary extends LauncherBinary { public EXELauncherBinary(LaunchServer server) { - super(server, LauncherBinary.resolve(server, ".exe"), "Launcher-%s-%d.exe"); + super(server, LauncherBinary.resolve(server, ".exe"), "Launcher-%s.exe"); } @Override diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/JARLauncherBinary.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/JARLauncherBinary.java index 72f60338..b7bf3e0f 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/JARLauncherBinary.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/JARLauncherBinary.java @@ -23,7 +23,7 @@ public final class JARLauncherBinary extends LauncherBinary { public final Map files; public JARLauncherBinary(LaunchServer server) throws IOException { - super(server, resolve(server, ".jar"), "Launcher-%s-%d.jar"); + super(server, resolve(server, ".jar"), "Launcher-%s.jar"); count = new AtomicLong(0); runtimeDir = server.dir.resolve(Launcher.RUNTIME_DIR); buildDir = server.dir.resolve("build"); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/AdditionalFixesApplyTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/AdditionalFixesApplyTask.java index a94ac0c9..fdb2c3ee 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/AdditionalFixesApplyTask.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/AdditionalFixesApplyTask.java @@ -74,9 +74,4 @@ public Path process(Path inputFile) throws IOException { return out; } - @Override - public boolean allowDelete() { - return true; - } - } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/AttachJarsTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/AttachJarsTask.java index 4904283d..b5705fd9 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/AttachJarsTask.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/AttachJarsTask.java @@ -68,11 +68,6 @@ private boolean filter(String name) { return exclusions.stream().anyMatch(name::startsWith); } - @Override - public boolean allowDelete() { - return true; - } - public List getJars() { return jars; } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/CertificateAutogenTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/CertificateAutogenTask.java index 4ab793af..b78940a2 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/CertificateAutogenTask.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/CertificateAutogenTask.java @@ -81,9 +81,4 @@ public Path process(Path inputFile) throws IOException { } return inputFile; } - - @Override - public boolean allowDelete() { - return false; - } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/CompressBuildTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/CompressBuildTask.java index a53ee660..beb9f3b4 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/CompressBuildTask.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/CompressBuildTask.java @@ -43,9 +43,4 @@ public Path process(Path inputFile) throws IOException { } return output; } - - @Override - public boolean allowDelete() { - return true; - } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/LauncherBuildTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/LauncherBuildTask.java index 5642b002..9317a4a8 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/LauncherBuildTask.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/LauncherBuildTask.java @@ -7,6 +7,4 @@ public interface LauncherBuildTask { String getName(); Path process(Path inputFile) throws IOException; - - boolean allowDelete(); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/MainBuildTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/MainBuildTask.java index 6ee8b27c..0d1f6e85 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/MainBuildTask.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/MainBuildTask.java @@ -51,7 +51,7 @@ public String getName() { @Override public Path process(Path inputJar) throws IOException { - Path outputJar = server.launcherBinary.nextPath("main"); + Path outputJar = server.launcherBinary.nextPath(this); try (ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(outputJar))) { BuildContext context = new BuildContext(output, reader.getCp(), this); initProps(); @@ -161,11 +161,6 @@ public byte[] transformClass(byte[] bytes, String classname, BuildContext contex return result; } - @Override - public boolean allowDelete() { - return true; - } - @FunctionalInterface public interface Transformer { byte[] transform(byte[] input, String classname, BuildContext context); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/PrepareBuildTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/PrepareBuildTask.java index c5a63fbf..09473e54 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/PrepareBuildTask.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/PrepareBuildTask.java @@ -53,11 +53,6 @@ public Path process(Path inputFile) throws IOException { return result; } - @Override - public boolean allowDelete() { - return false; - } - public void tryUnpack() throws IOException { logger.info("Unpacking launcher native guard list and runtime"); UnpackHelper.unpackZipNoCheck("runtime.zip", server.launcherBinary.runtimeDir); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/SignJarTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/SignJarTask.java index 517adc48..3e349884 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/SignJarTask.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/SignJarTask.java @@ -104,9 +104,4 @@ private void autoSign(Path inputFile, Path signedFile) throws IOException { } } } - - @Override - public boolean allowDelete() { - return true; - } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/exe/Launch4JTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/exe/Launch4JTask.java index f5bbf4f5..400d529c 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/exe/Launch4JTask.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/exe/Launch4JTask.java @@ -59,11 +59,6 @@ public Path process(Path inputFile) throws IOException { return output; } - @Override - public boolean allowDelete() { - return true; - } - private Path setConfig() { Path path = server.launcherEXEBinary.nextPath(getName()); Config config = new Config(); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/ProGuardComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/ProGuardComponent.java index 78f63344..14efc8f7 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/ProGuardComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/ProGuardComponent.java @@ -15,9 +15,7 @@ import proguard.ConfigurationParser; import proguard.ProGuard; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; +import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -26,6 +24,9 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; public class ProGuardComponent extends Component implements AutoCloseable, Reconfigurable { private static final Logger logger = LogManager.getLogger(); @@ -36,6 +37,7 @@ public class ProGuardComponent extends Component implements AutoCloseable, Recon public transient ProguardConf proguardConf; private transient LaunchServer launchServer; private transient ProGuardBuildTask buildTask; + private transient ProGuardMultiReleaseFixer fixerTask; public static boolean checkFXJMods(Path path) { if (!IOHelper.exists(path.resolve("javafx.base.jmod"))) @@ -75,7 +77,9 @@ public void init(LaunchServer launchServer) { this.launchServer = launchServer; proguardConf = new ProguardConf(launchServer, this); this.buildTask = new ProGuardBuildTask(launchServer, proguardConf, this); + this.fixerTask = new ProGuardMultiReleaseFixer(launchServer, this, "ProGuard.".concat(componentName)); launchServer.launcherBinary.addAfter((v) -> v.getName().startsWith(modeAfter), buildTask); + launchServer.launcherBinary.addAfter((v) -> v.getName().equals("ProGuard.".concat(componentName)), fixerTask); } @Override @@ -111,6 +115,59 @@ public void invoke(String... args) throws Exception { return null; } + public static class ProGuardMultiReleaseFixer implements LauncherBuildTask { + private final LaunchServer server; + private final ProGuardComponent component; + private final String proguardTaskName; + + public ProGuardMultiReleaseFixer(LaunchServer server, ProGuardComponent component, String proguardTaskName) { + this.server = server; + this.component = component; + this.proguardTaskName = proguardTaskName; + } + + @Override + public String getName() { + return "ProGuardMultiReleaseFixer.".concat(component.componentName); + } + + @Override + public Path process(Path inputFile) throws IOException { + LauncherBuildTask task = server.launcherBinary.getTaskBefore((x) -> proguardTaskName.equals(x.getName())).get(); + Path lastPath = server.launcherBinary.nextPath(task); + if(Files.notExists(lastPath)) { + logger.error("{} not exist. Multi-Release JAR fix not applied!", lastPath); + return inputFile; + } + Path outputPath = server.launcherBinary.nextPath(this); + try(ZipOutputStream output = new ZipOutputStream(new FileOutputStream(outputPath.toFile()))) { + try(ZipInputStream input = new ZipInputStream(new FileInputStream(inputFile.toFile()))) { + ZipEntry entry = input.getNextEntry(); + while(entry != null) { + ZipEntry newEntry = new ZipEntry(entry.getName()); + output.putNextEntry(newEntry); + input.transferTo(output); + entry = input.getNextEntry(); + } + } + try(ZipInputStream input = new ZipInputStream(new FileInputStream(lastPath.toFile()))) { + ZipEntry entry = input.getNextEntry(); + while(entry != null) { + if(!entry.getName().startsWith("META-INF/versions")) { + entry = input.getNextEntry(); + continue; + } + ZipEntry newEntry = new ZipEntry(entry.getName()); + output.putNextEntry(newEntry); + input.transferTo(output); + entry = input.getNextEntry(); + } + } + } + return outputPath; + } + } + public static class ProGuardBuildTask implements LauncherBuildTask { private final LaunchServer server; private final ProGuardComponent component; @@ -157,11 +214,6 @@ public Path process(Path inputFile) throws IOException { IOHelper.copy(inputFile, outputJar); return outputJar; } - - @Override - public boolean allowDelete() { - return true; - } } public static class ProguardConf { diff --git a/props.gradle b/props.gradle index f3dc3530..d8072da7 100644 --- a/props.gradle +++ b/props.gradle @@ -14,6 +14,6 @@ verLog4j = '2.20.0' verMySQLConn = '8.1.0' verPostgreSQLConn = '42.6.0' - verProguard = '7.4.0-beta02' + verProguard = '7.4.0' verLaunch4j = '3.50' }