22
33import static org .mcphackers .mcp .MCPPaths .*;
44
5+ import java .io .IOException ;
56import java .nio .file .Files ;
67import java .nio .file .Path ;
8+ import java .util .HashMap ;
9+ import java .util .HashSet ;
710import java .util .List ;
11+ import java .util .Map ;
12+ import java .util .Set ;
13+ import java .util .function .Predicate ;
14+ import java .util .regex .Pattern ;
815
916import org .mcphackers .mcp .MCP ;
1017import org .mcphackers .mcp .MCPPaths ;
1118import org .mcphackers .mcp .tasks .mode .TaskParameter ;
1219import org .mcphackers .mcp .tools .FileUtil ;
20+ import org .mcphackers .mcp .tools .Util ;
21+ import org .mcphackers .mcp .tools .injector .ExcludingStorageWriter ;
22+ import org .mcphackers .mcp .tools .injector .SourceFileTransformer ;
23+ import org .mcphackers .mcp .tools .mappings .MappingUtil ;
24+ import org .mcphackers .rdi .injector .data .Mappings ;
25+ import org .mcphackers .rdi .nio .RDInjector ;
26+ import org .objectweb .asm .ClassWriter ;
1327
1428public class TaskBuild extends TaskStaged {
1529 /*
@@ -26,35 +40,41 @@ public TaskBuild(Side side, MCP instance) {
2640 @ Override
2741 protected Stage [] setStages () {
2842 Path bin = MCPPaths .get (mcp , BIN , side );
43+ boolean fullBuild = mcp .getOptions ().getBooleanParameter (TaskParameter .FULL_BUILD );
44+
2945 return new Stage []{
3046 stage (getLocalizedStage ("recompile" ), 0 ,
3147 () -> new TaskRecompile (side , mcp , this ).doTask ()),
32- stage (getLocalizedStage ("reobf" ), 50 ,
33- () -> new TaskReobfuscate (side , mcp , this ).doTask ()),
48+ stage (getLocalizedStage ("gathermd5" ), 25 ,
49+ () -> new TaskUpdateMD5 (side , mcp , this ).updateMD5 (true )),
50+ stage (getLocalizedStage ("reobf" ), 50 , this ::reobfuscate ),
3451 stage (getLocalizedStage ("build" ), 70 ,
3552 () -> {
3653 Side [] sides = side == Side .MERGED ? new Side []{Side .CLIENT , Side .SERVER } : new Side []{side };
3754 for (Side localSide : sides ) {
3855 Path originalJar = MCPPaths .get (mcp , JAR_ORIGINAL , localSide );
39- Path reobfDir = MCPPaths .get (mcp , REOBF_SIDE , localSide );
40- Path buildJar = MCPPaths .get (mcp , BUILD_JAR , localSide );
41- Path buildZip = MCPPaths .get (mcp , BUILD_ZIP , localSide );
56+ Path reobfJar = MCPPaths .get (mcp , REOBF_JAR , localSide );
4257 FileUtil .createDirectories (MCPPaths .get (mcp , BUILD ));
43- if (mcp .getOptions ().getBooleanParameter (TaskParameter .FULL_BUILD )) {
58+ Predicate <Path > walkFilter = path -> !Files .isDirectory (path ) && !path .getFileName ().toString ().endsWith (".class" ) && !path .getFileName ().toString ().endsWith (".DS_Store" );
59+
60+ if (fullBuild ) {
61+ Path buildJar = MCPPaths .get (mcp , BUILD_JAR , localSide );
62+
4463 Files .deleteIfExists (buildJar );
4564 Files .copy (originalJar , buildJar );
46- List <Path > reobfClasses = FileUtil .walkDirectory (reobfDir , path -> !Files .isDirectory (path ));
47- FileUtil .packFilesToZip (buildJar , reobfClasses , reobfDir );
48- List <Path > assets = FileUtil .walkDirectory (bin , path -> !Files .isDirectory (path ) && !path .getFileName ().toString ().endsWith (".class" ));
65+ FileUtil .copyZipContentsIntoZip (reobfJar , buildJar );
66+ List <Path > assets = FileUtil .walkDirectory (bin , walkFilter );
4967 FileUtil .packFilesToZip (buildJar , assets , bin );
5068 FileUtil .deleteFileInAZip (buildJar , "/META-INF/MOJANG_C.DSA" );
5169 FileUtil .deleteFileInAZip (buildJar , "/META-INF/MOJANG_C.SF" );
5270 FileUtil .deleteFileInAZip (buildJar , "/META-INF/CODESIGN.DSA" );
5371 FileUtil .deleteFileInAZip (buildJar , "/META-INF/CODESIGN.SF" );
5472 } else {
73+ Path buildZip = MCPPaths .get (mcp , BUILD_ZIP , localSide );
74+
5575 Files .deleteIfExists (buildZip );
56- FileUtil . compress ( reobfDir , buildZip );
57- List <Path > assets = FileUtil .walkDirectory (bin , path -> ! Files . isDirectory ( path ) && ! path . getFileName (). toString (). endsWith ( ".class" ) );
76+ Files . copy ( reobfJar , buildZip );
77+ List <Path > assets = FileUtil .walkDirectory (bin , walkFilter );
5878 FileUtil .packFilesToZip (buildZip , assets , bin );
5979 }
6080 }
@@ -80,4 +100,65 @@ public void setProgress(int progress) {
80100 break ;
81101 }
82102 }
103+
104+ private void reobfuscate () throws IOException {
105+ final Path reobfBin = MCPPaths .get (mcp , BIN , side );
106+ final boolean stripSourceFile = mcp .getOptions ().getBooleanParameter (TaskParameter .STRIP_SOURCE_FILE );
107+ final boolean fullBuild = mcp .getOptions ().getBooleanParameter (TaskParameter .FULL_BUILD );
108+
109+ Side [] sides = side == Side .MERGED ? new Side []{Side .CLIENT , Side .SERVER } : new Side []{side };
110+
111+ Map <String , String > originalHashes = Util .gatherMD5Hashes (mcp , side , false );
112+ Map <String , String > recompHashes = Util .gatherMD5Hashes (mcp , side ,true );
113+
114+ for (Side localSide : sides ) {
115+ final Path reobfJar = MCPPaths .get (mcp , REOBF_JAR , localSide );
116+ if (!Files .exists (reobfJar .getParent ())) {
117+ Files .createDirectories (reobfJar .getParent ());
118+ }
119+ Files .deleteIfExists (reobfJar );
120+ RDInjector injector = new RDInjector (reobfBin );
121+ Mappings mappings = MappingUtil .getMappings (mcp , injector .getStorage (), localSide );
122+ if (mappings != null ) {
123+ injector .applyMappings (mappings );
124+ }
125+ if (stripSourceFile ) {
126+ injector .addTransform (SourceFileTransformer ::removeSourceFileAttributes );
127+ }
128+ injector .transform ();
129+
130+ Map <String , String > reversedNames = new HashMap <>();
131+ if (mappings != null ) {
132+ for (Map .Entry <String , String > entry : mappings .classes .entrySet ()) {
133+ reversedNames .put (entry .getValue (), entry .getKey ());
134+ }
135+ }
136+ Pattern regexPattern = Pattern .compile (mcp .getOptions ().getStringParameter (TaskParameter .EXCLUDED_CLASSES ));
137+
138+ // Exclude all unchanged classes if full build is OFF
139+ Set <String > excludes = new HashSet <>();
140+ if (!fullBuild ) {
141+ for (String className : injector .getStorage ().getAllClasses ()) {
142+ int index = className .indexOf ('$' );
143+ if (index != -1 ) {
144+ className = className .substring (0 , index );
145+ }
146+ String deobfName = reversedNames .get (className );
147+ if (deobfName == null ) {
148+ deobfName = className ;
149+ }
150+ String hash = originalHashes .get (deobfName );
151+ String hashModified = recompHashes .get (deobfName );
152+ boolean extract = (hash == null ) || !hash .equals (hashModified ); //&& !regexPattern.matcher(deobfName).matches();
153+ if (!extract ) {
154+ excludes .add (className );
155+ } else {
156+ System .out .println (reversedNames .get (className ) + " : " + className );
157+ }
158+ }
159+ }
160+
161+ new ExcludingStorageWriter (injector .getStorage (), ClassWriter .COMPUTE_MAXS , excludes ).write (Files .newOutputStream (reobfJar ));
162+ }
163+ }
83164}
0 commit comments