88import net .minecraftforge .gradle .common .tasks .JarExec ;
99import net .minecraftforge .gradle .common .util .Utils ;
1010
11+ import net .minecraftforge .srgutils .IMappingFile ;
1112import org .apache .commons .io .FileUtils ;
1213import org .gradle .api .file .ConfigurableFileCollection ;
13- import org .gradle .api .file .Directory ;
1414import org .gradle .api .file .RegularFile ;
1515import org .gradle .api .file .RegularFileProperty ;
1616import org .gradle .api .provider .Provider ;
2626import java .util .List ;
2727
2828public abstract class RenameJarInPlace extends JarExec {
29- private final Provider <Directory > workDir = getProject (). getLayout (). getBuildDirectory (). dir ( getName ( ));
30- private final Provider <RegularFile > temp = workDir .map (s -> s .file ("output.jar " ));
29+ private final Provider <RegularFile > tempOutput = this . workDir . map ( s -> s . file ( "output.jar" ));
30+ private final Provider <RegularFile > tempMappings = this . workDir .map (s -> s .file ("mappings.tsrg " ));
3131
3232 public RenameJarInPlace () {
3333 getTool ().set (Utils .FART );
@@ -39,10 +39,9 @@ public RenameJarInPlace() {
3939 protected List <String > filterArgs (List <String > args ) {
4040 return replaceArgsMulti (args , ImmutableMap .of (
4141 "{input}" , getInput ().get ().getAsFile (),
42- "{output}" , temp .get ().getAsFile ()),
42+ "{output}" , tempOutput .get ().getAsFile (),
43+ "{mappings}" , this .tempMappings .get ().getAsFile ()),
4344 ImmutableMultimap .<String , Object >builder ()
44- .put ("{mappings}" , getMappings ().get ().getAsFile ())
45- .putAll ("{mappings}" , getExtraMappings ().getFiles ())
4645 .putAll ("{libraries}" , getLibraries ().getFiles ())
4746 .build ()
4847 );
@@ -51,14 +50,27 @@ protected List<String> filterArgs(List<String> args) {
5150 @ Override
5251 @ TaskAction
5352 public void apply () throws IOException {
54- File temp = this .temp .get ().getAsFile ();
55- if (temp .getParentFile () != null && !temp .getParentFile ().exists () && !temp .getParentFile ().mkdirs ()) {
56- getProject ().getLogger ().warn ("Could not create parent directories for temp dir '{}'" , temp .getAbsolutePath ());
53+ File tempJar = this .tempOutput .get ().getAsFile ();
54+ File tempMappings = this .tempMappings .get ().getAsFile ();
55+
56+ if (tempJar .getParentFile () != null && !tempJar .getParentFile ().exists () && !tempJar .getParentFile ().mkdirs ()) {
57+ getProject ().getLogger ().warn ("Could not create parent directories for temp dir '{}'" , tempJar .getAbsolutePath ());
58+ }
59+
60+ if (tempMappings .exists () && !tempMappings .delete ())
61+ throw new IllegalStateException ("Could not delete temp mappings file: " + tempMappings .getAbsolutePath ());
62+
63+ IMappingFile mappings = IMappingFile .load (getMappings ().get ().getAsFile ());
64+
65+ for (File file : getExtraMappings ().getFiles ()) {
66+ mappings = mappings .merge (IMappingFile .load (file ));
5767 }
5868
69+ mappings .write (tempMappings .toPath (), IMappingFile .Format .TSRG2 , false );
70+
5971 super .apply ();
6072
61- FileUtils .copyFile (temp , getInput ().get ().getAsFile ());
73+ FileUtils .copyFile (tempJar , getInput ().get ().getAsFile ());
6274 }
6375
6476 // TODO: Make this a ConfigurableFileCollection? (then remove getExtraMappings())
0 commit comments