diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index 061d8fc0..3ac18552 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -86,7 +86,7 @@ public final class LaunchServer implements Runnable { public static final class Config { - public int port; + public int port; private String address; @@ -131,7 +131,6 @@ public static final class Config { public String authRejectString; - public String whitelistRejectString; public boolean genMappings; @@ -140,6 +139,7 @@ public static final class Config { public boolean isWarningMissArchJava; public boolean enabledProGuard; + public boolean stripLineNumbers; public String startScript; @@ -577,8 +577,8 @@ private void generateConfigIfNotExists() throws IOException { newConfig.threadCoreCount = 0; // on your own newConfig.enabledProGuard = true; + newConfig.stripLineNumbers = false; newConfig.threadCount = JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() >= 4 ? JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() / 2 : JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors(); - // Set server address LogHelper.println("LaunchServer address: "); newConfig.setAddress(commandHandler.readLine()); 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 2e349a99..86bb6cb9 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java @@ -9,6 +9,7 @@ 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.UnpackBuildTask; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; @@ -22,6 +23,7 @@ public JARLauncherBinary(LaunchServer server) throws IOException { tasks.add(new UnpackBuildTask(server)); tasks.add(new MainBuildTask(server)); if(server.config.enabledProGuard) tasks.add(new ProGuardBuildTask(server)); + if(server.config.stripLineNumbers) tasks.add(new StripLineNumbersTask(server)); syncBinaryFile = server.dir.resolve(server.config.binaryName + ".jar"); } 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 new file mode 100644 index 00000000..b14d36a6 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/StripLineNumbersTask.java @@ -0,0 +1,71 @@ +package ru.gravit.launchserver.binary.tasks; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.file.Path; +import java.util.jar.JarFile; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; + +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; + +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 { + + private final LaunchServer server; + private final ClassMetadataReader reader; + + public StripLineNumbersTask(LaunchServer server) { + this.server = server; + reader = new ClassMetadataReader(); + } + + @Override + public String getName() { + return "Strip debug task"; + } + + @Override + public Path process(Path inputFile) throws IOException { + Path out = server.dir.resolve(server.config.projectName + "-stripped.jar"); + 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) { + String filename = e.getName(); + output.putNextEntry(IOHelper.newZipEntry(e)); + if (filename.endsWith(".class")) { + byte[] bytes = null; + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(2048)) { + IOHelper.transfer(input, outputStream); + bytes = outputStream.toByteArray(); + } + output.write(classFix(bytes, reader)); + } else + IOHelper.transfer(input, output); + e = input.getNextEntry(); + } + } + return out; + } + + private static byte[] classFix(byte[] bytes, ClassMetadataReader reader) { + ClassReader cr = new ClassReader(bytes); + ClassWriter cw = new SafeClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); + cr.accept(cw, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); + return cw.toByteArray(); + } + + @Override + public boolean allowDelete() { + return true; + } + +}