11package org .sinytra .connector .transformer .transform ;
22
3+ import com .mojang .logging .LogUtils ;
34import net .minecraftforge .fart .api .Transformer ;
5+ import org .slf4j .Logger ;
46
57import java .io .*;
8+ import java .nio .file .FileSystem ;
9+ import java .nio .file .FileSystems ;
10+ import java .nio .file .Files ;
11+ import java .nio .file .Path ;
12+ import java .util .jar .Attributes ;
613import java .util .jar .Manifest ;
714
815public class JarSignatureStripper implements Transformer {
16+ private static final Logger LOGGER = LogUtils .getLogger ();
917
1018 @ Override
1119 public ResourceEntry process (ResourceEntry entry ) {
@@ -18,12 +26,38 @@ public ManifestEntry process(ManifestEntry entry) {
1826 Manifest manifest = new Manifest ();
1927 try (InputStream is = new ByteArrayInputStream (entry .getData ())) {
2028 manifest .read (is );
21- manifest . getEntries (). clear ( );
29+ processManifest ( manifest );
2230 ByteArrayOutputStream byteStream = new ByteArrayOutputStream ();
2331 manifest .write (byteStream );
2432 return ManifestEntry .create (entry .getTime (), byteStream .toByteArray ());
2533 } catch (IOException e ) {
2634 throw new UncheckedIOException ("Error writing manifest" , e );
2735 }
2836 }
37+
38+ // Remove Automatic Module Name from libraries to force giving them a unique module name
39+ // and avoid FML silently ignoring those with duplicate names
40+ public static void processJarInPlace (Path path ) {
41+ try (FileSystem fs = FileSystems .newFileSystem (path )) {
42+ Path mfPath = fs .getPath ("META-INF/MANIFEST.MF" );
43+ if (!Files .exists (mfPath )) return ;
44+
45+ Manifest manifest = new Manifest ();
46+ try (InputStream ins = Files .newInputStream (mfPath )) {
47+ manifest .read (ins );
48+ }
49+ processManifest (manifest );
50+
51+ try (OutputStream os = Files .newOutputStream (mfPath )) {
52+ manifest .write (os );
53+ }
54+ } catch (Exception e ) {
55+ LOGGER .error ("Error stripping jar signature from {}" , path , e );
56+ }
57+ }
58+
59+ private static void processManifest (Manifest manifest ) {
60+ manifest .getEntries ().clear ();
61+ manifest .getMainAttributes ().remove (new Attributes .Name ("Automatic-Module-Name" ));
62+ }
2963}
0 commit comments