@@ -68,6 +68,7 @@ public static void patchFoxLoaderFinalJar(File foxloaderJar) throws IOException
6868 if (!foxloaderJar .renameTo (backup )) {
6969 throw new IOException ("Failed to rename loader jar" );
7070 }
71+ boolean patchedBuildConfig = false ;
7172 try (ZipInputStream zipInputStream = new ZipInputStream (
7273 new BufferedInputStream (Files .newInputStream (backup .toPath ())));
7374 ZipOutputStream zipOutputStream = new ZipOutputStream (
@@ -83,6 +84,7 @@ public static void patchFoxLoaderFinalJar(File foxloaderJar) throws IOException
8384 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
8485 IOUtils .copy (zipInputStream , byteArrayOutputStream );
8586 zipOutputStream .write (patchBuildConfig (byteArrayOutputStream .toByteArray ()));
87+ patchedBuildConfig = true ;
8688 } else {
8789 IOUtils .copy (zipInputStream , zipOutputStream );
8890 }
@@ -91,6 +93,9 @@ public static void patchFoxLoaderFinalJar(File foxloaderJar) throws IOException
9193 }
9294 zipOutputStream .finish ();
9395 }
96+ if (!patchedBuildConfig ) {
97+ throw new IOException ("Patching BuildConfig failed." );
98+ }
9499 if (backup .exists () && !backup .delete ()) {
95100 throw new IOException ("Failed to delete backup jar" );
96101 }
@@ -99,40 +104,10 @@ public static void patchFoxLoaderFinalJar(File foxloaderJar) throws IOException
99104 private static byte [] patchBuildConfig (byte [] buildConfig ) {
100105 ClassNode classNode = new ClassNode ();
101106 new ClassReader (buildConfig ).accept (classNode , 0 );
102- if (TransformerUtils .findMethod (classNode , "<clinit>" ) != null ) {
103- throw new RuntimeException ("<clinit>()V detected in BuildConfig!" );
104- }
105- MethodNode clInit = new MethodNode (ACC_STATIC , "<clinit>" , "()V" , null , null );
106- for (FieldNode fieldNode : classNode .fields ) {
107- if (fieldNode .access != (ACC_PUBLIC | ACC_STATIC | ACC_FINAL )) {
108- throw new RuntimeException ("Invalid field access: " + fieldNode .access );
109- }
110- AbstractInsnNode constInsn ;
111- switch (fieldNode .desc ) {
112- case "Ljava/lang/String;" :
113- constInsn = new LdcInsnNode (fieldNode .value );
114- break ;
115- case "I" :
116- case "Z" :
117- if (fieldNode .value instanceof Integer ) {
118- constInsn = TransformerUtils .getNumberInsn ((Integer ) fieldNode .value );
119- } else if (fieldNode .value instanceof Boolean ) {
120- constInsn = TransformerUtils .getBooleanInsn ((Boolean ) fieldNode .value );
121- } else {
122- throw new RuntimeException ("WTF???" );
123- }
124- break ;
125- default :
126- throw new RuntimeException ("Unsupported desc: \" " + fieldNode .desc + "\" " );
127- }
128- fieldNode .value = null ;
129- clInit .instructions .add (constInsn );
130- clInit .instructions .add (new FieldInsnNode (PUTSTATIC ,
131- "com/fox2code/foxloader/launcher/BuildConfig" ,
132- fieldNode .name , fieldNode .desc ));
107+ TransformerUtils .deInlineFieldConstants (classNode , true );
108+ if (classNode .methods .size () != 2 ) {
109+ throw new RuntimeException ("WHAT? " + classNode .methods .size ());
133110 }
134- clInit .instructions .add (new InsnNode (RETURN ));
135- classNode .methods .add (clInit );
136111 ClassWriter classWriter = new ClassWriter (ClassWriter .COMPUTE_MAXS );
137112 classNode .accept (classWriter );
138113 return classWriter .toByteArray ();
0 commit comments