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 7c19e410..0af66163 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java @@ -1,9 +1,13 @@ package ru.gravit.launchserver.binary; import java.io.IOException; +import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; +import java.util.List; import java.util.concurrent.atomic.AtomicLong; import ru.gravit.launcher.Launcher; @@ -12,18 +16,34 @@ import ru.gravit.launchserver.binary.tasks.LauncherBuildTask; import ru.gravit.launchserver.binary.tasks.MainBuildTask; import ru.gravit.launchserver.binary.tasks.ProGuardBuildTask; -import ru.gravit.launchserver.binary.tasks.StripLineNumbersTask; +import ru.gravit.launchserver.binary.tasks.AdditionalFixesApplyTask; 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 { + private static final class ListFileVisitor extends SimpleFileVisitor { + private final List lst; + + private ListFileVisitor(List lst) { + this.lst = lst; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + if (file.toFile().getName().endsWith(".jar")) + lst.add(file); + return super.visitFile(file, attrs); + } + } + public final AtomicLong count; public final Path runtimeDir; public final Path guardDir; public final Path buildDir; - public ArrayList tasks; + public List tasks; + public List coreLibs; public JARLauncherBinary(LaunchServer server) throws IOException { super(server); @@ -33,6 +53,7 @@ public JARLauncherBinary(LaunchServer server) throws IOException { guardDir = server.dir.resolve(Launcher.GUARD_DIR); buildDir = server.dir.resolve("build"); tasks = new ArrayList<>(); + coreLibs = new ArrayList<>(); Files.createDirectory(buildDir); } @@ -40,15 +61,17 @@ public JARLauncherBinary(LaunchServer server) throws IOException { public void init() { tasks.add(new UnpackBuildTask(server)); tasks.add(new MainBuildTask(server)); - if(server.config.enabledProGuard) tasks.add(new ProGuardBuildTask(server)); + tasks.add(new ProGuardBuildTask(server)); tasks.add(new AttachJarsTask(server)); - if(server.config.stripLineNumbers) tasks.add(new StripLineNumbersTask(server)); + tasks.add(new AdditionalFixesApplyTask(server)); } @Override public void build() throws IOException { LogHelper.info("Building launcher binary file"); count.set(0); + coreLibs.clear(); + IOHelper.walk(server.launcherLibraries, new ListFileVisitor(coreLibs), true); Path thisPath = null; boolean isNeedDelete = false; long time_start = System.currentTimeMillis(); @@ -61,7 +84,7 @@ public void build() throws IOException { long time_task_end = System.currentTimeMillis(); long time_task = time_task_end - time_this; time_this = time_task_end; - if (isNeedDelete && server.config.deleteTempFiles) Files.delete(oldPath); + if (isNeedDelete && server.config.deleteTempFiles) Files.deleteIfExists(oldPath); isNeedDelete = task.allowDelete(); LogHelper.subInfo("Task %s processed from %d millis",task.getName(), time_task); } @@ -72,7 +95,7 @@ public void build() throws IOException { } public String nextName(String taskName) { - return String.format("%s-%s-%d.jar", server.config.projectName, taskName, count.getAndIncrement()); + return String.format("Launcher-%s-%d.jar", server.config.projectName, taskName, count.getAndIncrement()); } public Path nextPath(String taskName) { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/StripLineNumbersTask.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/AdditionalFixesApplyTask.java similarity index 74% rename from LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/StripLineNumbersTask.java rename to LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/AdditionalFixesApplyTask.java index 35b064ee..0b334cbd 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/StripLineNumbersTask.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/AdditionalFixesApplyTask.java @@ -10,33 +10,38 @@ import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.tree.ClassNode; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.asm.ClassMetadataReader; import ru.gravit.launchserver.asm.SafeClassWriter; import ru.gravit.utils.helper.IOHelper; -public class StripLineNumbersTask implements LauncherBuildTask { +public class AdditionalFixesApplyTask implements LauncherBuildTask { private final LaunchServer server; - public StripLineNumbersTask(LaunchServer server) { + public AdditionalFixesApplyTask(LaunchServer server) { this.server = server; } @Override public String getName() { - return "StripDebug"; + return "AdditionalFixesApply"; } @Override public Path process(Path inputFile) throws IOException { - Path out = server.launcherBinary.nextPath("stripped"); + Path out = server.launcherBinary.nextPath("post-fixed"); try (ClassMetadataReader reader = new ClassMetadataReader()) { reader.getCp().add(new JarFile(inputFile.toFile())); try (ZipInputStream input = IOHelper.newZipInput(inputFile); ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(out))) { ZipEntry e = input.getNextEntry(); while (e != null) { + if (e.isDirectory()) { + e = input.getNextEntry(); + continue; + } String filename = e.getName(); output.putNextEntry(IOHelper.newZipEntry(e)); if (filename.endsWith(".class")) { @@ -45,7 +50,7 @@ public Path process(Path inputFile) throws IOException { IOHelper.transfer(input, outputStream); bytes = outputStream.toByteArray(); } - output.write(classFix(bytes, reader)); + output.write(classFix(bytes, reader, server.config.stripLineNumbers)); } else IOHelper.transfer(input, output); e = input.getNextEntry(); @@ -55,10 +60,13 @@ public Path process(Path inputFile) throws IOException { return out; } - private static byte[] classFix(byte[] bytes, ClassMetadataReader reader) { + private static byte[] classFix(byte[] bytes, ClassMetadataReader reader, boolean stripNumbers) { ClassReader cr = new ClassReader(bytes); + ClassNode cn = new ClassNode(); + cr.accept(cn, stripNumbers ? (ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES) : ClassReader.SKIP_FRAMES); + ClassWriter cw = new SafeClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); - cr.accept(cw, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); + cn.accept(cw); return cw.toByteArray(); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/AttachJarsTask.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/AttachJarsTask.java index f7eea802..a46871be 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/AttachJarsTask.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/AttachJarsTask.java @@ -1,14 +1,9 @@ package ru.gravit.launchserver.binary.tasks; import java.io.IOException; -import java.lang.instrument.Instrumentation; -import java.nio.file.FileVisitResult; import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; 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; @@ -17,21 +12,6 @@ import ru.gravit.utils.helper.IOHelper; public class AttachJarsTask implements LauncherBuildTask { - private static final class ListFileVisitor extends SimpleFileVisitor { - private final List lst; - - private ListFileVisitor(List lst) { - this.lst = lst; - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (file.toFile().getName().endsWith(".jar")) - lst.add(file); - return super.visitFile(file, attrs); - } - } - private final LaunchServer srv; private final List jars; private final List exclusions; @@ -40,6 +20,7 @@ public AttachJarsTask(LaunchServer srv) { this.srv = srv; jars = new ArrayList<>(); exclusions = new ArrayList<>(); + exclusions.add("META-INF"); } @Override @@ -54,13 +35,15 @@ public Path process(Path inputFile) throws IOException { ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(outputFile))) { ZipEntry e = input.getNextEntry(); while (e != null) { + if (e.isDirectory() || srv.buildHookManager.isContainsBlacklist(e.getName())) { + e = input.getNextEntry(); + continue; + } output.putNextEntry(IOHelper.newZipEntry(e)); IOHelper.transfer(input, output); e = input.getNextEntry(); } - List coreAttach = new ArrayList<>(); - IOHelper.walk(srv.launcherLibraries, new ListFileVisitor(coreAttach), true); - attach(output, coreAttach); + attach(output, srv.launcherBinary.coreLibs); attach(output, jars); } return outputFile; @@ -72,7 +55,7 @@ private void attach(ZipOutputStream output, List lst) throws IOException { ZipEntry e = input.getNextEntry(); while (e != null) { String filename = e.getName(); - if (exclusions.stream().noneMatch(filename::startsWith)) { + if (exclusions.stream().noneMatch(filename::startsWith) && !srv.buildHookManager.isContainsBlacklist(filename)) { output.putNextEntry(IOHelper.newZipEntry(e)); IOHelper.transfer(input, output); } 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 82e90cbb..7583ec79 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 @@ -133,7 +133,7 @@ public Path process(Path inputJar) throws IOException { ZipEntry e = input.getNextEntry(); while (e != null) { String filename = e.getName(); - if (server.buildHookManager.isContainsBlacklist(filename)) { + if (server.buildHookManager.isContainsBlacklist(filename) || e.isDirectory()) { e = input.getNextEntry(); continue; } 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 7785d70b..9be2393d 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.IOHelper; import ru.gravit.utils.helper.LogHelper; public class ProGuardBuildTask implements LauncherBuildTask { @@ -24,18 +25,24 @@ public String getName() { @Override public Path process(Path inputFile) throws IOException { - 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 e) { - LogHelper.error(e); - } - return outputJar; + if (server.config.enabledProGuard) { + 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 e) { + LogHelper.error(e); + } + return outputJar; + } else { + Path outputJar = server.launcherBinary.nextPath("non-obf"); + IOHelper.copy(inputFile, outputJar); + return outputJar; + } } @Override 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/TaskUtil.java similarity index 84% rename from LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/api/TaskUtil.java rename to LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/TaskUtil.java index 644706b5..8175b7e1 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/api/TaskUtil.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/TaskUtil.java @@ -1,30 +1,28 @@ -package ru.gravit.launchserver.binary.tasks.api; +package ru.gravit.launchserver.binary.tasks; import java.util.ArrayList; import java.util.List; 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<>(); + List indexes = new ArrayList<>(); tasks.stream().filter(pred).forEach(e -> { - indexes.add(tasks.indexOf(e)+count); + indexes.add(e); }); indexes.forEach(e -> { - tasks.add(e, taskAdd); + tasks.add(tasks.indexOf(e)+count, taskAdd); }); } public static void replaceCounted(List tasks, int count, Predicate pred, LauncherBuildTask taskRep) { - List indexes = new ArrayList<>(); + List indexes = new ArrayList<>(); tasks.stream().filter(pred).forEach(e -> { - indexes.add(tasks.indexOf(e)+count); + indexes.add(e); }); indexes.forEach(e -> { - tasks.set(e, taskRep); + tasks.set(tasks.indexOf(e)+count, taskRep); }); }