[FEATURE] Пересмотр логики AttachJarsTask.

This commit is contained in:
zaxar163 2019-04-13 15:49:24 +03:00
parent ef89b3ce6e
commit da615fb11b
No known key found for this signature in database
GPG key ID: E3B309DD3852DE06
3 changed files with 33 additions and 21 deletions

View file

@ -11,7 +11,10 @@
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@ -31,13 +34,33 @@ public String getName() {
@Override @Override
public Path process(Path inputFile) throws IOException { public Path process(Path inputFile) throws IOException {
Path out = server.launcherBinary.nextPath("post-fixed"); Path out = server.launcherBinary.nextPath("post-fixed");
try (ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(out))) {
apply(inputFile, inputFile, output, server, (e) -> false);
}
return out;
}
public static void apply(Path inputFile, Path addFile, ZipOutputStream output, LaunchServer srv, Predicate<ZipEntry> excluder) throws IOException {
try (ClassMetadataReader reader = new ClassMetadataReader()) { try (ClassMetadataReader reader = new ClassMetadataReader()) {
reader.getCp().add(new JarFile(inputFile.toFile())); reader.getCp().add(new JarFile(inputFile.toFile()));
try (ZipInputStream input = IOHelper.newZipInput(inputFile); List<JarFile> libs = srv.launcherBinary.coreLibs.stream().map(e -> {
ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(out))) { try {
return new JarFile(e.toFile());
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}).collect(Collectors.toList());
libs.addAll(srv.launcherBinary.addonLibs.stream().map(e -> {
try {
return new JarFile(e.toFile());
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}).collect(Collectors.toList()));
try (ZipInputStream input = IOHelper.newZipInput(addFile)) {
ZipEntry e = input.getNextEntry(); ZipEntry e = input.getNextEntry();
while (e != null) { while (e != null) {
if (e.isDirectory()) { if (e.isDirectory() || excluder.test(e)) {
e = input.getNextEntry(); e = input.getNextEntry();
continue; continue;
} }
@ -49,14 +72,13 @@ public Path process(Path inputFile) throws IOException {
IOHelper.transfer(input, outputStream); IOHelper.transfer(input, outputStream);
bytes = outputStream.toByteArray(); bytes = outputStream.toByteArray();
} }
output.write(classFix(bytes, reader, server.config.stripLineNumbers)); output.write(classFix(bytes, reader, srv.config.stripLineNumbers));
} else } else
IOHelper.transfer(input, output); IOHelper.transfer(input, output);
e = input.getNextEntry(); e = input.getNextEntry();
} }
} }
} }
return out;
} }
private static byte[] classFix(byte[] bytes, ClassMetadataReader reader, boolean stripNumbers) { private static byte[] classFix(byte[] bytes, ClassMetadataReader reader, boolean stripNumbers) {

View file

@ -44,26 +44,16 @@ public Path process(Path inputFile) throws IOException {
IOHelper.transfer(input, output); IOHelper.transfer(input, output);
e = input.getNextEntry(); e = input.getNextEntry();
} }
attach(output, srv.launcherBinary.coreLibs); attach(output, inputFile, srv.launcherBinary.coreLibs);
attach(output, jars); attach(output, inputFile, jars);
} }
return outputFile; return outputFile;
} }
private void attach(ZipOutputStream output, List<Path> lst) throws IOException { private void attach(ZipOutputStream output, Path inputFile, List<Path> lst) throws IOException {
for (Path p : lst) { for (Path p : lst) {
LogHelper.debug("Attaching: " + p); LogHelper.debug("Attaching: " + p);
try (ZipInputStream input = IOHelper.newZipInput(p)) { AdditionalFixesApplyTask.apply(inputFile, p, output, srv, (e) -> exclusions.stream().anyMatch(e.getName()::startsWith));
ZipEntry e = input.getNextEntry();
while (e != null) {
String filename = e.getName();
if (exclusions.stream().noneMatch(filename::startsWith) && !e.isDirectory()) {
output.putNextEntry(IOHelper.newZipEntry(e));
IOHelper.transfer(input, output);
}
e = input.getNextEntry();
}
}
} }
} }

View file

@ -207,7 +207,7 @@ public Path process(Path inputJar) throws IOException {
jaConfigurator.compile(); jaConfigurator.compile();
output.write(jaConfigurator.getBytecode()); output.write(jaConfigurator.getBytecode());
} catch (CannotCompileException | NotFoundException e) { } catch (CannotCompileException | NotFoundException e) {
LogHelper.error(e); throw new IOException(e);
} }
reader.close(); reader.close();
return outputJar; return outputJar;