Skip to content

Commit

Permalink
Primitive mixin plugin (#35)
Browse files Browse the repository at this point in the history
* First commit, needs testing

* ProcessResources task*
  • Loading branch information
Rongmario authored May 17, 2023
1 parent 6df87c5 commit e47f066
Showing 1 changed file with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.file.RegularFile;
import org.gradle.api.file.*;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.BasePluginExtension;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.SetProperty;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.language.jvm.tasks.ProcessResources;

import com.gtnewhorizons.retrofuturagradle.MinecraftExtension;
import com.gtnewhorizons.retrofuturagradle.ObfuscationAttribute;
import com.gtnewhorizons.retrofuturagradle.mcp.GenSrgMappingsTask;
import com.gtnewhorizons.retrofuturagradle.mcp.MCPTasks;
import com.gtnewhorizons.retrofuturagradle.mcp.ReobfuscatedJar;
import com.gtnewhorizons.retrofuturagradle.minecraft.MinecraftTasks;
import com.gtnewhorizons.retrofuturagradle.util.Utilities;

Expand All @@ -39,6 +44,7 @@ public class ModUtils {
private final MCPTasks mcpTasks;
private final ConfigurableFileCollection depFilesToDeobf;
private final SetProperty<String> depModulesToDeobf;
private final Property<String> mixinRefMap;

/**
* Attribute that controls running the deobfuscation artifact transform.
Expand All @@ -58,6 +64,7 @@ public ModUtils(Project project, MinecraftExtension mcExt, MinecraftTasks minecr

this.depFilesToDeobf = objects.fileCollection();
this.depModulesToDeobf = objects.setProperty(String.class);
this.mixinRefMap = objects.property(String.class);

project.getTasks().register("applyDecompilerCleanupToMain", ApplyDecompCleanupTask.class, task -> {
task.setGroup(TASK_GROUP_USER);
Expand Down Expand Up @@ -96,6 +103,30 @@ public ModUtils(Project project, MinecraftExtension mcExt, MinecraftTasks minecr
});
});

project.afterEvaluate(_p -> {
if (this.mixinRefMap.isPresent()) {
File tempMixinDir = FileUtils.getFile(project.getBuildDir(), "tmp", "mixins");
File mixinSrg = new File(tempMixinDir, "mixins.srg");
File mixinRefMapFile = new File(tempMixinDir, this.mixinRefMap.get());
TaskProvider<ReobfuscatedJar> reobfJarTask = project.getTasks()
.named("reobfJar", ReobfuscatedJar.class);
reobfJarTask.configure(task -> task.getExtraSrgFiles().from(mixinSrg));
project.getTasks().named("compileJava", JavaCompile.class).configure(task -> {
task.doFirst("createTempMixinDirectory", _t -> tempMixinDir.mkdirs());
List<String> compilerArgs = task.getOptions().getCompilerArgs();
compilerArgs.add(
"-AreobfSrgFile=" + reobfJarTask.map(ReobfuscatedJar::getSrg).map(RegularFileProperty::get)
.map(RegularFile::getAsFile));
compilerArgs.add("-AoutSrgFile=" + mixinSrg);
compilerArgs.add("-AoutRefMapFile=" + mixinRefMapFile);
});
project.getTasks().named("processResources", ProcessResources.class).configure(task -> {
task.from(mixinRefMapFile);
task.dependsOn("compileJava");
});
}
});

deps.getExtensions().add("rfg", new RfgDependencyExtension());
}

Expand Down Expand Up @@ -130,6 +161,22 @@ public Object deobfuscate(Object depSpec) {
return depSpec;
}

/**
* Wrap a mixin dependency specification with a call to this function to allow RFG to recognise the need for a
* refmap to be generated. The dependency should be declared as part of the annotationProcessor configuration
*/
public Object enableMixins(Object mixinSpec, String refMapName) {
mixinRefMap.set(refMapName);
return mixinSpec;
}

public Object enableMixins(Object mixinSpec) {
return enableMixins(
mixinSpec,
"mixins." + project.getExtensions().getByType(BasePluginExtension.class).getArchivesName()
+ ".refmap.json");
}

public class RfgDependencyExtension {

/**
Expand Down

0 comments on commit e47f066

Please sign in to comment.