2525import com .google .common .collect .Maps ;
2626import com .google .common .io .Files ;
2727import com .google .common .io .LineProcessor ;
28- import de .oceanlabs .mcp .mcinjector .LVTNaming ;
29- import de .oceanlabs .mcp .mcinjector .MCInjectorImpl ;
28+
29+ import de .oceanlabs .mcp .mcinjector .lvt .LVTNaming ;
30+ import de .oceanlabs .mcp .mcinjector .MCInjector ;
31+
3032import groovy .lang .Closure ;
33+
3134import net .md_5 .specialsource .*;
3235import net .md_5 .specialsource .provider .JarProvider ;
3336import net .md_5 .specialsource .provider .JointProvider ;
37+
3438import net .minecraftforge .gradle .common .Constants ;
3539import net .minecraftforge .gradle .util .caching .Cached ;
3640import net .minecraftforge .gradle .util .caching .CachedTask ;
37- import net .minecraftforge .gradle .util .json .JsonFactory ;
38- import net .minecraftforge .gradle .util .json .MCInjectorStruct ;
39- import net .minecraftforge .gradle .util .json .MCInjectorStruct .InnerClass ;
41+
4042import org .gradle .api .file .FileCollection ;
4143import org .gradle .api .tasks .*;
42- import org .gradle .api .tasks .Optional ;
4344
4445import java .io .BufferedReader ;
4546import java .io .File ;
4647import java .io .IOException ;
47- import java .nio .charset .Charset ;
48- import java .util .*;
49- import java .util .zip .ZipFile ;
48+ import java .util .ArrayList ;
49+ import java .util .Collection ;
50+ import java .util .HashSet ;
51+ import java .util .Map ;
52+ import java .util .Set ;
5053
51- import static org .objectweb .asm .Opcodes .*;
5254
5355public class DeobfuscateJar extends CachedTask
5456{
@@ -69,10 +71,13 @@ public class DeobfuscateJar extends CachedTask
6971 private Object exceptorCfg ;
7072
7173 @ InputFile
72- private Object exceptorJson ;
73-
74- @ Input
75- private boolean applyMarkers = false ;
74+ private Object access ;
75+
76+ @ InputFile
77+ private Object constructors ;
78+
79+ @ InputFile
80+ private Object exceptions ;
7681
7782 @ Input
7883 private boolean failOnAtError = true ;
@@ -163,158 +168,22 @@ private void deobfJar(File inJar, File outJar, File srg, Collection<File> ats) t
163168 }
164169 }
165170
166- private int fixAccess (int access , String target )
167- {
168- int ret = access & ~7 ;
169- int t = 0 ;
170-
171- if (target .startsWith ("public" ))
172- t = ACC_PUBLIC ;
173- else if (target .startsWith ("private" ))
174- t = ACC_PRIVATE ;
175- else if (target .startsWith ("protected" ))
176- t = ACC_PROTECTED ;
177-
178- switch (access & 7 )
179- {
180- case ACC_PRIVATE :
181- ret |= t ;
182- break ;
183- case 0 :
184- ret |= (t != ACC_PRIVATE ? t : 0 );
185- break ;
186- case ACC_PROTECTED :
187- ret |= (t != ACC_PRIVATE && t != 0 ? t : ACC_PROTECTED );
188- break ;
189- case ACC_PUBLIC :
190- ret |= ACC_PUBLIC ;
191- break ;
192- }
193-
194- if (target .endsWith ("-f" ))
195- ret &= ~ACC_FINAL ;
196- else if (target .endsWith ("+f" ))
197- ret |= ACC_FINAL ;
198- return ret ;
199- }
200-
201171 public void applyExceptor (File inJar , File outJar , File config , File log , Set <File > ats ) throws IOException
202172 {
203- String json = null ;
204- File getJson = getExceptorJson ();
205- if (getJson != null )
206- {
207- final Map <String , MCInjectorStruct > struct = JsonFactory .loadMCIJson (getJson );
208- for (File at : ats )
209- {
210- getLogger ().info ("loading AT: " + at .getCanonicalPath ());
211-
212- Files .readLines (at , Charset .defaultCharset (), new LineProcessor <Object >()
213- {
214- @ Override
215- public boolean processLine (String line ) throws IOException
216- {
217- if (line .indexOf ('#' ) != -1 )
218- line = line .substring (0 , line .indexOf ('#' ));
219- line = line .trim ().replace ('.' , '/' );
220- if (line .isEmpty ())
221- return true ;
222-
223- String [] s = line .split (" " );
224- if (s .length == 2 && s [1 ].indexOf ('$' ) > 0 )
225- {
226- String parent = s [1 ].substring (0 , s [1 ].indexOf ('$' ));
227- for (MCInjectorStruct cls : new MCInjectorStruct [] { struct .get (parent ), struct .get (s [1 ]) })
228- {
229- if (cls != null && cls .innerClasses != null )
230- {
231- for (InnerClass inner : cls .innerClasses )
232- {
233- if (inner .inner_class .equals (s [1 ]))
234- {
235- int access = fixAccess (inner .getAccess (), s [0 ]);
236- inner .access = (access == 0 ? null : Integer .toHexString (access ));
237- }
238- }
239- }
240- }
241- }
242-
243- return true ;
244- }
245-
246- @ Override
247- public Object getResult ()
248- {
249- return null ;
250- }
251- });
252- }
253-
254- // Remove unknown classes from configuration
255- removeUnknownClasses (inJar , struct );
256-
257- File jsonTmp = new File (this .getTemporaryDir (), "transformed.json" );
258- json = jsonTmp .getCanonicalPath ();
259- Files .write (JsonFactory .GSON .toJson (struct ).getBytes (), jsonTmp );
260- }
261-
262173 getLogger ().debug ("INPUT: " + inJar );
263174 getLogger ().debug ("OUTPUT: " + outJar );
264175 getLogger ().debug ("CONFIG: " + config );
265- getLogger ().debug ("JSON: " + json );
176+ getLogger ().debug ("ACCESS: " + getAccessCfg ());
177+ getLogger ().debug ("CONSTRUCTOR: " + getConstructorCfg ());
178+ getLogger ().debug ("EXCEPTION: " + getExceptionsCfg ());
266179 getLogger ().debug ("LOG: " + log );
267180 getLogger ().debug ("PARAMS: true" );
268181
269- MCInjectorImpl .process (inJar .getCanonicalPath (),
270- outJar .getCanonicalPath (),
271- config .getCanonicalPath (),
272- log .getCanonicalPath (),
273- null ,
274- 0 ,
275- json ,
276- isApplyMarkers (),
277- true ,
278- LVTNaming .LVT
279- );
280- }
281-
282- private void removeUnknownClasses (File inJar , Map <String , MCInjectorStruct > config ) throws IOException
283- {
284- try (ZipFile zip = new ZipFile (inJar ))
285- {
286- Iterator <Map .Entry <String , MCInjectorStruct >> entries = config .entrySet ().iterator ();
287- while (entries .hasNext ())
288- {
289- Map .Entry <String , MCInjectorStruct > entry = entries .next ();
290- String className = entry .getKey ();
291-
292- // Verify the configuration contains only classes we actually have
293- if (zip .getEntry (className + ".class" ) == null )
294- {
295- getLogger ().info ("Removing unknown class {}" , className );
296- entries .remove ();
297- continue ;
298- }
299-
300- MCInjectorStruct struct = entry .getValue ();
301-
302- // Verify the inner classes in the configuration actually exist in our deobfuscated JAR file
303- if (struct .innerClasses != null )
304- {
305- Iterator <InnerClass > innerClasses = struct .innerClasses .iterator ();
306- while (innerClasses .hasNext ())
307- {
308- InnerClass innerClass = innerClasses .next ();
309- if (zip .getEntry (innerClass .inner_class + ".class" ) == null )
310- {
311- getLogger ().info ("Removing unknown inner class {} from {}" , innerClass .inner_class , className );
312- innerClasses .remove ();
313- }
314- }
315- }
316- }
317- }
182+ new MCInjector (inJar .toPath (), outJar .toPath ()).log (log .toPath ())
183+ .access (getAccessCfg ().toPath ())
184+ .constructors (getConstructorCfg ().toPath ())
185+ .exceptions (getExceptionsCfg ().toPath ())
186+ .lvt (LVTNaming .LVT ).process ();
318187 }
319188
320189 public File getExceptorCfg ()
@@ -327,27 +196,28 @@ public void setExceptorCfg(Object exceptorCfg)
327196 this .exceptorCfg = exceptorCfg ;
328197 }
329198
330- public File getExceptorJson ()
331- {
332- if (exceptorJson == null )
333- return null ;
334- else
335- return getProject ().file (exceptorJson );
199+ public File getAccessCfg () {
200+ return getProject ().file (access );
336201 }
337202
338- public void setExceptorJson (Object exceptorJson )
339- {
340- this .exceptorJson = exceptorJson ;
203+ public void setAccessCfg (Object accessCfg ) {
204+ access = accessCfg ;
341205 }
342206
343- public boolean isApplyMarkers ()
344- {
345- return applyMarkers ;
207+ public File getConstructorCfg () {
208+ return getProject ().file (constructors );
346209 }
347210
348- public void setApplyMarkers (boolean applyMarkers )
349- {
350- this .applyMarkers = applyMarkers ;
211+ public void setConstructorCfg (Object constructorCfg ) {
212+ constructors = constructorCfg ;
213+ }
214+
215+ public File getExceptionsCfg () {
216+ return getProject ().file (exceptions );
217+ }
218+
219+ public void setExceptionsCfg (Object exceptionCfg ) {
220+ exceptions = exceptionCfg ;
351221 }
352222
353223 public boolean isFailOnAtError ()
0 commit comments