From 494d83ae92d2e3b81a25ee8f24bb7ce041022643 Mon Sep 17 00:00:00 2001 From: zaxar163 Date: Tue, 8 Jan 2019 17:36:05 +0400 Subject: [PATCH] Binary logic changes. --- .../binary/JARLauncherBinary.java | 24 ++++++ .../launchserver/binary/ProguardConf.java | 10 +-- .../binary/tasks/MainBuildTask.java | 17 ++-- .../binary/tasks/ProGuardBuildTask.java | 13 ++-- .../binary/tasks/StripLineNumbersTask.java | 4 +- .../binary/tasks/api/AttachJarsTask.java | 78 +++++++++++++++++++ .../binary/tasks/api/TaskUtil.java | 62 +++++++++++++++ .../java/ru/gravit/utils/HTTPRequest.java | 6 +- .../java/ru/gravit/utils/HttpDownloader.java | 2 +- .../java/ru/gravit/utils/NativeJVMHalt.java | 2 +- .../main/java/ru/gravit/utils/Version.java | 2 +- .../ru/gravit/utils/helper/EnvHelper.java | 2 +- .../ru/gravit/utils/helper/UnpackHelper.java | 2 +- 13 files changed, 194 insertions(+), 30 deletions(-) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/api/AttachJarsTask.java create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/api/TaskUtil.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java index a0caa387..31ba7dc9 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java @@ -4,6 +4,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Locale; +import java.util.concurrent.atomic.AtomicLong; import ru.gravit.launcher.Launcher; import ru.gravit.launchserver.LaunchServer; @@ -12,13 +14,16 @@ import ru.gravit.launchserver.binary.tasks.ProGuardBuildTask; import ru.gravit.launchserver.binary.tasks.StripLineNumbersTask; import ru.gravit.launchserver.binary.tasks.UnpackBuildTask; +import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; public final class JARLauncherBinary extends LauncherBinary { public ArrayList tasks; + public final AtomicLong count; public final Path runtimeDir; public final Path guardDir; + public final Path buildDir; public JARLauncherBinary(LaunchServer server) throws IOException { super(server); @@ -27,14 +32,17 @@ public JARLauncherBinary(LaunchServer server) throws IOException { tasks.add(new MainBuildTask(server)); if(server.config.enabledProGuard) tasks.add(new ProGuardBuildTask(server)); if(server.config.stripLineNumbers) tasks.add(new StripLineNumbersTask(server)); + count = new AtomicLong(0); syncBinaryFile = server.dir.resolve(server.config.binaryName + ".jar"); runtimeDir = server.dir.resolve(Launcher.RUNTIME_DIR); guardDir = server.dir.resolve(Launcher.GUARD_DIR); + buildDir = server.dir.resolve("build"); } @Override public void build() throws IOException { LogHelper.info("Building launcher binary file"); + count.set(0); Path thisPath = null; boolean isNeedDelete = false; long time_start = System.currentTimeMillis(); @@ -55,4 +63,20 @@ public void build() throws IOException { IOHelper.move(thisPath, syncBinaryFile); LogHelper.info("Build successful from %d millis",time_end - time_start); } + + public String nextName(String taskName) { + return String.format("%s-%s-%d.jar", server.config.projectName, taskName, count.getAndIncrement()); + } + + public Path nextPath(String taskName) { + return buildDir.resolve(nextName(taskName)); + } + + public Path nextPath(LauncherBuildTask task) { + return nextPath(task.getName()); + } + + public Path nextLowerPath(LauncherBuildTask task) { + return nextPath(CommonHelper.low(task.getName())); + } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/ProguardConf.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/ProguardConf.java index df293074..62b11ea6 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/ProguardConf.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/ProguardConf.java @@ -9,6 +9,7 @@ import java.nio.file.Path; import java.security.SecureRandom; import java.util.ArrayList; +import java.util.List; import ru.gravit.launchserver.LaunchServer; import ru.gravit.utils.helper.IOHelper; @@ -30,8 +31,6 @@ private static String generateString(SecureRandom rand, int il) { public final Path config; public final Path mappings; public final Path words; - public final Path outputJar; - public final ArrayList confStrs; public transient final LaunchServer srv; public ProguardConf(LaunchServer srv) { @@ -39,13 +38,11 @@ public ProguardConf(LaunchServer srv) { config = proguard.resolve("proguard.config"); mappings = proguard.resolve("mappings.pro"); words = proguard.resolve("random.pro"); - outputJar = srv.dir.resolve(srv.config.binaryName + "-obfPre.jar"); - confStrs = new ArrayList<>(); this.srv = srv; } - public void buildConfig(Path inputJar) + public String[] buildConfig(Path inputJar, Path outputJar) { - confStrs.clear(); + List confStrs = new ArrayList<>(); prepare(false); if (srv.config.genMappings) confStrs.add("-printmapping \'" + mappings.toFile().getName() + "\'"); confStrs.add("-obfuscationdictionary \'" + words.toFile().getName() + "\'"); @@ -53,6 +50,7 @@ public void buildConfig(Path inputJar) confStrs.add("-outjar \'" + outputJar.toAbsolutePath() + "\'"); confStrs.add("-classobfuscationdictionary \'" + words.toFile().getName() + "\'"); confStrs.add(readConf()); + return confStrs.toArray(new String[0]); } private void genConfig(boolean force) throws IOException { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/MainBuildTask.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/MainBuildTask.java index 27d7f654..82e90cbb 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/MainBuildTask.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/MainBuildTask.java @@ -31,8 +31,6 @@ import ru.gravit.utils.helper.SecurityHelper; public class MainBuildTask implements LauncherBuildTask { - public final Path binaryFile; - public Path cleanJar; private final LaunchServer server; public final ClassMetadataReader reader; private final class RuntimeDirVisitor extends SimpleFileVisitor { @@ -105,7 +103,6 @@ private static ZipEntry newGuardEntry(String fileName) { public MainBuildTask(LaunchServer srv) { server = srv; - binaryFile = server.dir.resolve(server.config.binaryName + "-main.jar"); reader = new ClassMetadataReader(); } @@ -115,11 +112,11 @@ public String getName() { } @Override - public Path process(Path cleanJar) throws IOException { - this.cleanJar = cleanJar; - try (ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(binaryFile)); + public Path process(Path inputJar) throws IOException { + Path outputJar = server.launcherBinary.nextPath("main"); + try (ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(outputJar)); JAConfigurator jaConfigurator = new JAConfigurator(AutogenConfig.class.getName(), this)) { - jaConfigurator.pool.insertClassPath(cleanJar.toFile().getAbsolutePath()); + jaConfigurator.pool.insertClassPath(inputJar.toFile().getAbsolutePath()); BuildContext context = new BuildContext(output, jaConfigurator, this); server.buildHookManager.hook(context); jaConfigurator.setAddress(server.config.getAddress()); @@ -131,8 +128,8 @@ public Path process(Path cleanJar) throws IOException { jaConfigurator.setDownloadJava(server.config.isDownloadJava); jaConfigurator.setEnv(server.config.env); server.buildHookManager.registerAllClientModuleClass(jaConfigurator); - reader.getCp().add(new JarFile(cleanJar.toFile())); - try (ZipInputStream input = new ZipInputStream(IOHelper.newInput(cleanJar))) { + reader.getCp().add(new JarFile(inputJar.toFile())); + try (ZipInputStream input = new ZipInputStream(IOHelper.newInput(inputJar))) { ZipEntry e = input.getNextEntry(); while (e != null) { String filename = e.getName(); @@ -196,7 +193,7 @@ public Path process(Path cleanJar) throws IOException { LogHelper.error(e); } reader.close(); - return binaryFile; + return outputJar; } @Override diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/ProGuardBuildTask.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/ProGuardBuildTask.java index 72366ea2..7785d70b 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/ProGuardBuildTask.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/ProGuardBuildTask.java @@ -8,6 +8,7 @@ import proguard.ParseException; import proguard.ProGuard; import ru.gravit.launchserver.LaunchServer; +import ru.gravit.utils.helper.LogHelper; public class ProGuardBuildTask implements LauncherBuildTask { private final LaunchServer server; @@ -23,18 +24,18 @@ public String getName() { @Override public Path process(Path inputFile) throws IOException { - Configuration proguard_cfg = new Configuration(); - server.proguardConf.buildConfig(inputFile); - ConfigurationParser parser = new ConfigurationParser(server.proguardConf.confStrs.toArray(new String[0]), + Path outputJar = server.launcherBinary.nextLowerPath(this); + Configuration proguard_cfg = new Configuration(); + ConfigurationParser parser = new ConfigurationParser(server.proguardConf.buildConfig(inputFile, outputJar), server.proguardConf.proguard.toFile(), System.getProperties()); try { parser.parse(proguard_cfg); ProGuard proGuard = new ProGuard(proguard_cfg); proGuard.execute(); - } catch (ParseException e1) { - e1.printStackTrace(); + } catch (ParseException e) { + LogHelper.error(e); } - return server.proguardConf.outputJar; + return outputJar; } @Override diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/StripLineNumbersTask.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/StripLineNumbersTask.java index d8baa571..35b064ee 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/StripLineNumbersTask.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/StripLineNumbersTask.java @@ -25,12 +25,12 @@ public StripLineNumbersTask(LaunchServer server) { @Override public String getName() { - return "Strip debug task"; + return "StripDebug"; } @Override public Path process(Path inputFile) throws IOException { - Path out = server.dir.resolve(server.config.projectName + "-stripped.jar"); + Path out = server.launcherBinary.nextPath("stripped"); try (ClassMetadataReader reader = new ClassMetadataReader()) { reader.getCp().add(new JarFile(inputFile.toFile())); try (ZipInputStream input = IOHelper.newZipInput(inputFile); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/api/AttachJarsTask.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/api/AttachJarsTask.java new file mode 100644 index 00000000..cbbb302a --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/api/AttachJarsTask.java @@ -0,0 +1,78 @@ +package ru.gravit.launchserver.binary.tasks.api; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.jar.JarFile; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; + +import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.binary.tasks.LauncherBuildTask; +import ru.gravit.utils.helper.IOHelper; + +public class AttachJarsTask implements LauncherBuildTask { + + private final LaunchServer srv; + private final List jars; + private final List exclusions; + + public AttachJarsTask(LaunchServer srv) { + this.srv = srv; + jars = new ArrayList<>(); + exclusions = new ArrayList<>(); + } + + @Override + public String getName() { + return "AttachJars"; + } + + @Override + public Path process(Path inputFile) throws IOException { + Path outputFile = srv.launcherBinary.nextPath("attached"); + try (ZipInputStream input = IOHelper.newZipInput(inputFile); + ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(outputFile))) { + ZipEntry e = input.getNextEntry(); + while (e != null) { + output.putNextEntry(IOHelper.newZipEntry(e)); + IOHelper.transfer(input, output); + e = input.getNextEntry(); + } + attach(output); + } + return outputFile; + } + + private void attach(ZipOutputStream output) throws IOException { + for (Path p : jars) { + try (ZipInputStream input = IOHelper.newZipInput(p)) { + ZipEntry e = input.getNextEntry(); + while (e != null) { + String filename = e.getName(); + if (exclusions.stream().noneMatch(exc -> filename.startsWith(exc))) { + output.putNextEntry(IOHelper.newZipEntry(e)); + IOHelper.transfer(input, output); + } + e = input.getNextEntry(); + } + } + } + } + + @Override + public boolean allowDelete() { + return true; + } + + public List getJars() { + return jars; + } + + public List getExclusions() { + return exclusions; + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/api/TaskUtil.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/api/TaskUtil.java new file mode 100644 index 00000000..0bc81a6e --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/api/TaskUtil.java @@ -0,0 +1,62 @@ +package ru.gravit.launchserver.binary.tasks.api; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +import ru.gravit.launchserver.binary.tasks.LauncherBuildTask; + +public final class TaskUtil { + public static void addCounted(List tasks, int count, Predicate pred, LauncherBuildTask taskAdd) { + List indexes = new ArrayList<>(); + tasks.stream().filter(pred).forEach(e -> { + indexes.add(tasks.indexOf(e)+count); + }); + indexes.forEach(e -> { + tasks.add(e, taskAdd); + }); + } + + public static void replaceCounted(List tasks, int count, Predicate pred, LauncherBuildTask taskRep) { + List indexes = new ArrayList<>(); + tasks.stream().filter(pred).forEach(e -> { + indexes.add(tasks.indexOf(e)+count); + }); + indexes.forEach(e -> { + tasks.set(e, taskRep); + }); + } + + public static void addPre(List tasks, Predicate pred, LauncherBuildTask taskAdd) { + addCounted(tasks, -1, pred, taskAdd); + } + + public static void add(List tasks, Predicate pred, LauncherBuildTask taskAdd) { + addCounted(tasks, 0, pred, taskAdd); + } + + public static void addAfter(List tasks, Predicate pred, LauncherBuildTask taskAdd) { + addCounted(tasks, 1, pred, taskAdd); + } + + public static void replacePre(List tasks, Predicate pred, LauncherBuildTask taskRep) { + replaceCounted(tasks, -1, pred, taskRep); + } + + public static void replace(List tasks, Predicate pred, LauncherBuildTask taskRep) { + replaceCounted(tasks, 0, pred, taskRep); + } + + public static void replaceAfter(List tasks, Predicate pred, LauncherBuildTask taskRep) { + replaceCounted(tasks, 1, pred, taskRep); + } + + public static List getTaskByClass(List tasks, Class taskClass) { + return tasks.stream().filter(taskClass::isInstance).map(taskClass::cast).collect(Collectors.toList()); + } + + private TaskUtil() { + } +} diff --git a/libLauncher/src/main/java/ru/gravit/utils/HTTPRequest.java b/libLauncher/src/main/java/ru/gravit/utils/HTTPRequest.java index 868de75c..55627a3a 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/HTTPRequest.java +++ b/libLauncher/src/main/java/ru/gravit/utils/HTTPRequest.java @@ -13,7 +13,7 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -public class HTTPRequest { +public final class HTTPRequest { private static final int TIMEOUT = 10000; private static final JsonParser parser = new JsonParser(); @@ -61,4 +61,8 @@ public static JsonElement jsonRequest(JsonElement request, URL url) throws IOExc JsonElement content = parser.parse(reader); return content; } + + private HTTPRequest() { + } +} } diff --git a/libLauncher/src/main/java/ru/gravit/utils/HttpDownloader.java b/libLauncher/src/main/java/ru/gravit/utils/HttpDownloader.java index a73bca13..504f8f3f 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/HttpDownloader.java +++ b/libLauncher/src/main/java/ru/gravit/utils/HttpDownloader.java @@ -13,7 +13,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -public class HttpDownloader extends Observable { +public final class HttpDownloader extends Observable { public static final int BUFER_SIZE = 8192; public static final int INTERVAL = 300; public AtomicInteger writed = new AtomicInteger(0); diff --git a/libLauncher/src/main/java/ru/gravit/utils/NativeJVMHalt.java b/libLauncher/src/main/java/ru/gravit/utils/NativeJVMHalt.java index 9d067daf..728e82be 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/NativeJVMHalt.java +++ b/libLauncher/src/main/java/ru/gravit/utils/NativeJVMHalt.java @@ -2,7 +2,7 @@ import ru.gravit.utils.helper.LogHelper; -public class NativeJVMHalt { +public final class NativeJVMHalt { public NativeJVMHalt(int haltCode) { this.haltCode = haltCode; LogHelper.error("JVM exit code %d", haltCode); diff --git a/libLauncher/src/main/java/ru/gravit/utils/Version.java b/libLauncher/src/main/java/ru/gravit/utils/Version.java index 2d189c7e..5795a7a8 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/Version.java +++ b/libLauncher/src/main/java/ru/gravit/utils/Version.java @@ -4,7 +4,7 @@ import java.util.Objects; -public class Version { +public final class Version { @LauncherAPI public final int major; @LauncherAPI diff --git a/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java b/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java index 175cae96..dfe0b35f 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java +++ b/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java @@ -3,7 +3,7 @@ import java.util.Locale; import java.util.Map; -public class EnvHelper { +public final class EnvHelper { public static final String[] toTest = {"_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS"}; public static void addEnv(ProcessBuilder builder) { diff --git a/libLauncher/src/main/java/ru/gravit/utils/helper/UnpackHelper.java b/libLauncher/src/main/java/ru/gravit/utils/helper/UnpackHelper.java index 40dc0212..460630cd 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/helper/UnpackHelper.java +++ b/libLauncher/src/main/java/ru/gravit/utils/helper/UnpackHelper.java @@ -10,7 +10,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -public class UnpackHelper { +public final class UnpackHelper { public static boolean unpack(URL resource, Path target) throws IOException { if (IOHelper.isFile(target)) { if (matches(target, resource)) return false;