88
99import java .io .IOException ;
1010import java .nio .file .Path ;
11+ import java .util .jar .JarFile ;
12+ import java .util .jar .JarEntry ;
13+ import java .util .jar .JarOutputStream ;
14+ import java .io .InputStream ;
15+ import java .nio .file .Files ;
1116
1217public class RemapService {
1318
1419 private final Path inputJar ;
1520 private final Path mappingsFile ;
1621 private final Path outputJar ;
22+ private final String fromNamespace ;
23+ private final String toNamespace ;
1724
18- public RemapService (Path inputJar , Path mappingsFile , Path outputJar ) {
25+ public RemapService (Path inputJar , Path mappingsFile , Path outputJar , String fromNamespace , String toNamespace ) {
1926 this .inputJar = inputJar ;
2027 this .mappingsFile = mappingsFile ;
2128 this .outputJar = outputJar ;
29+ this .fromNamespace = fromNamespace ;
30+ this .toNamespace = toNamespace ;
2231 }
2332
2433 public void run () throws IOException {
2534 System .out .println ("Loading mappings..." );
2635 MemoryMappingTree tree = new MemoryMappingTree ();
2736 MappingReader .read (mappingsFile , tree );
2837
29- String fromNamespace = "official" ;
30- String toNamespace = "named" ;
31-
3238 System .out .println ("Setting up TinyRemapper..." );
3339
3440 TinyRemapper remapper = TinyRemapper .newRemapper ()
@@ -39,12 +45,43 @@ public void run() throws IOException {
3945 .build ();
4046
4147 System .out .println ("Remapping jar..." );
42- try ( @ SuppressWarnings ("deprecation" ) OutputConsumerPath outputConsumer = new OutputConsumerPath (outputJar ) ) {
48+ try (@ SuppressWarnings ("deprecation" ) OutputConsumerPath outputConsumer = new OutputConsumerPath (outputJar )) {
4349 outputConsumer .addNonClassFiles (inputJar );
4450 remapper .readInputs (inputJar );
4551 remapper .apply (outputConsumer );
4652 }
4753
54+ if (!Files .exists (outputJar )) {
55+ throw new IOException ("Remapped output jar was not created: " + outputJar );
56+ }
57+
58+ Path tempJar = Files .createTempFile ("remapped-filtered" , ".jar" );
59+ try (
60+ JarFile jarFile = new JarFile (outputJar .toFile ());
61+ JarOutputStream jos = new JarOutputStream (Files .newOutputStream (tempJar ))
62+ ) {
63+ for (JarEntry entry : java .util .Collections .list (jarFile .entries ())) {
64+ String n = entry .getName ().replace ('\\' , '/' );
65+ if (n .startsWith ("META-INF/" ) && (n .endsWith (".SF" ) || n .endsWith (".DSA" ) || n .endsWith (".RSA" ))) {
66+ continue ;
67+ }
68+ JarEntry newEntry = new JarEntry (entry .getName ());
69+ if (entry .getMethod () == JarEntry .STORED ) {
70+ newEntry .setMethod (JarEntry .STORED );
71+ newEntry .setSize (entry .getSize ());
72+ newEntry .setCompressedSize (entry .getCompressedSize ());
73+ newEntry .setCrc (entry .getCrc ());
74+ }
75+ jos .putNextEntry (newEntry );
76+ try (InputStream is = jarFile .getInputStream (entry )) {
77+ is .transferTo (jos );
78+ }
79+ jos .closeEntry ();
80+ }
81+ }
82+
83+ Files .move (tempJar , outputJar , java .nio .file .StandardCopyOption .REPLACE_EXISTING );
84+
4885 remapper .finish ();
4986 System .out .println ("Remapped jar created at: " + outputJar .toAbsolutePath ());
5087 }
0 commit comments