Skip to content

Commit

Permalink
spotlessApply
Browse files Browse the repository at this point in the history
  • Loading branch information
makamys committed Feb 16, 2024
1 parent 4954089 commit 262117c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.gtnewhorizons.retrofuturagradle.modutils;

import com.gtnewhorizons.retrofuturagradle.mcp.GenSrgMappingsTask;
import com.gtnewhorizons.retrofuturagradle.mcp.InjectTagsTask;
import com.gtnewhorizons.retrofuturagradle.util.Utilities;
import com.opencsv.CSVReader;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import javax.inject.Inject;

import org.cadixdev.lorenz.MappingSet;
import org.cadixdev.lorenz.io.MappingFormats;
import org.cadixdev.lorenz.io.MappingsWriter;
Expand All @@ -24,15 +28,13 @@
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.gradle.jvm.tasks.Jar;
import org.gradle.api.tasks.options.Option;
import org.gradle.jvm.tasks.Jar;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import com.gtnewhorizons.retrofuturagradle.mcp.GenSrgMappingsTask;
import com.gtnewhorizons.retrofuturagradle.mcp.InjectTagsTask;
import com.gtnewhorizons.retrofuturagradle.util.Utilities;
import com.opencsv.CSVReader;

public abstract class MigrateMappingsTask extends DefaultTask {

Expand All @@ -41,7 +43,9 @@ public abstract class MigrateMappingsTask extends DefaultTask {
@Optional
@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
@Option(option = "mcpDir", description = "The directory containing the mappings to migrate to, using MCP's fields.csv and methods.csv format.")
@Option(
option = "mcpDir",
description = "The directory containing the mappings to migrate to, using MCP's fields.csv and methods.csv format.")
public abstract DirectoryProperty getMcpDir();

@InputDirectory
Expand All @@ -58,13 +62,14 @@ public MigrateMappingsTask() {
getInputDir().convention(getProject().getLayout().getProjectDirectory().dir("src/main/java"));
getOutputDir().convention(getProject().getLayout().getProjectDirectory().dir("src/main/java"));
}

@TaskAction
public void migrateMappings() throws Exception {
if(!getMcpDir().isPresent()) {
if (!getMcpDir().isPresent()) {
throw new IllegalArgumentException("A target mapping must be set using --mcpDir.");
}
GenSrgMappingsTask genSrgMappings = getProject().getTasks().named("generateForgeSrgMappings", GenSrgMappingsTask.class).get();
GenSrgMappingsTask genSrgMappings = getProject().getTasks()
.named("generateForgeSrgMappings", GenSrgMappingsTask.class).get();
File currentFields = genSrgMappings.getFieldsCsv().getAsFile().get();
File currentMethods = genSrgMappings.getMethodsCsv().getAsFile().get();

Expand All @@ -73,35 +78,46 @@ public void migrateMappings() throws Exception {

MappingSet notchSrg = MappingFormats.SRG.read(srg.toPath());
MappingSet currentSrgMcp = createSrgMcpMappingSet(notchSrg, readCsv(currentFields), readCsv(currentMethods));
MappingSet targetSrgMcp = createSrgMcpMappingSet(notchSrg, readCsv(new File(target, "fields.csv")), readCsv(new File(target, "methods.csv")));
MappingSet targetSrgMcp = createSrgMcpMappingSet(
notchSrg,
readCsv(new File(target, "fields.csv")),
readCsv(new File(target, "methods.csv")));
MappingSet diffMcp = diff(currentSrgMcp, targetSrgMcp);

if(DEBUG_WRITE_DIFF) {
try (MappingsWriter w = MappingFormats.SRG.createWriter(new File(getProject().getRootDir(), "diff.srg").toPath())) {
if (DEBUG_WRITE_DIFF) {
try (MappingsWriter w = MappingFormats.SRG
.createWriter(new File(getProject().getRootDir(), "diff.srg").toPath())) {
w.write(diffMcp);
}
}

final Mercury mercury = new Mercury();

// There's probably a better way to do this
mercury.getClassPath().add(getProject().getTasks().named("packagePatchedMc", Jar.class).get().getArchiveFile().get().getAsFile().toPath());
mercury.getClassPath().add(getProject().getTasks().named("injectTags", InjectTagsTask.class).get().getOutputDir().getAsFile().get().toPath());
mercury.getClassPath().add(
getProject().getTasks().named("packagePatchedMc", Jar.class).get().getArchiveFile().get().getAsFile()
.toPath());
mercury.getClassPath().add(
getProject().getTasks().named("injectTags", InjectTagsTask.class).get().getOutputDir().getAsFile().get()
.toPath());
for (File file : getProject().getConfigurations().getByName("compileClasspath").getFiles()) {
mercury.getClassPath().add(file.toPath());
}
// Fixes some issues like broken javadoc in Forge, and JDT not understanding Hodgepodge's obfuscated voxelmap targets.
// Fixes some issues like broken javadoc in Forge, and JDT not understanding Hodgepodge's obfuscated voxelmap
// targets.
mercury.setGracefulClasspathChecks(true);

mercury.getProcessors().add(MixinRemapper.create(diffMcp));
mercury.getProcessors().add(MercuryRemapper.create(diffMcp));

mercury.setSourceCompatibility(getProject().getExtensions().getByType(JavaPluginExtension.class).getSourceCompatibility().toString());
mercury.setSourceCompatibility(
getProject().getExtensions().getByType(JavaPluginExtension.class).getSourceCompatibility().toString());

mercury.rewrite(getInputDir().getAsFile().get().toPath(), getOutputDir().getAsFile().get().toPath());
}

/** Returns the difference between two mappings of the same root.
/**
* Returns the difference between two mappings of the same root.
*
* @param mappingsA Mappings from O to A.
* @param mappingsB Mappings from O to B.
Expand All @@ -112,70 +128,64 @@ private MappingSet diff(MappingSet mappingsA, MappingSet mappingsB) {

forEachClassMapping(mappingsA, classA -> {
ClassMapping<?, ?> classB = mappingsB.getClassMapping(classA.getFullObfuscatedName()).get();
ClassMapping<?, ?> classDiff = diff.getOrCreateClassMapping(
classA.getFullDeobfuscatedName());
ClassMapping<?, ?> classDiff = diff.getOrCreateClassMapping(classA.getFullDeobfuscatedName());

for(FieldMapping fieldA : classA.getFieldMappings()) {
for (FieldMapping fieldA : classA.getFieldMappings()) {
FieldMapping fieldB = classB.getFieldMapping(fieldA.getObfuscatedName()).get();

classDiff.createFieldMapping(
fieldA.getDeobfuscatedName())
.setDeobfuscatedName(
fieldB.getDeobfuscatedName());
classDiff.createFieldMapping(fieldA.getDeobfuscatedName())
.setDeobfuscatedName(fieldB.getDeobfuscatedName());
}

for(MethodMapping methodA : classA.getMethodMappings()) {
MethodMapping methodB = classB.getMethodMapping(methodA.getObfuscatedName(), methodA.getObfuscatedDescriptor()).get();
for (MethodMapping methodA : classA.getMethodMappings()) {
MethodMapping methodB = classB
.getMethodMapping(methodA.getObfuscatedName(), methodA.getObfuscatedDescriptor()).get();

classDiff.createMethodMapping(
methodA.getDeobfuscatedName(),
methodA.getDeobfuscatedDescriptor())
.setDeobfuscatedName(
methodB.getDeobfuscatedName());
classDiff.createMethodMapping(methodA.getDeobfuscatedName(), methodA.getDeobfuscatedDescriptor())
.setDeobfuscatedName(methodB.getDeobfuscatedName());
}
});

return diff;
}

private void forEachClassMapping(MappingSet mappings, Consumer<ClassMapping<?, ?>> callback) {
for(TopLevelClassMapping topLevelClass : mappings.getTopLevelClassMappings()) {
for (TopLevelClassMapping topLevelClass : mappings.getTopLevelClassMappings()) {
callback.accept(topLevelClass);
for(InnerClassMapping inner : topLevelClass.getInnerClassMappings()) {
for (InnerClassMapping inner : topLevelClass.getInnerClassMappings()) {
forEachClassMappingInner(inner, callback);
}
}
}

private void forEachClassMappingInner(InnerClassMapping inner, Consumer<ClassMapping<?, ?>> callback) {
callback.accept(inner);
for(InnerClassMapping innerer : inner.getInnerClassMappings()) {
for (InnerClassMapping innerer : inner.getInnerClassMappings()) {
forEachClassMappingInner(innerer, callback);
}
}

private MappingSet createSrgMcpMappingSet(MappingSet notchSrg, Map<String, String> fields, Map<String, String> methods) {
private MappingSet createSrgMcpMappingSet(MappingSet notchSrg, Map<String, String> fields,
Map<String, String> methods) {
MappingSet srgMcp = MappingSet.create();

// In 1.7 everything is top level because proguard strips inner class info
for(TopLevelClassMapping notchSrgTopLevelClass : notchSrg.getTopLevelClassMappings()) {
ClassMapping<?, ?> srgMcpClass = srgMcp.getOrCreateClassMapping(notchSrgTopLevelClass.getFullDeobfuscatedName());

for(FieldMapping notchSrgField : notchSrgTopLevelClass.getFieldMappings()) {
srgMcpClass.createFieldMapping(
notchSrgField.getDeobfuscatedName())
.setDeobfuscatedName(fields.getOrDefault(
notchSrgField.getDeobfuscatedName(),
notchSrgField.getDeobfuscatedName()));
for (TopLevelClassMapping notchSrgTopLevelClass : notchSrg.getTopLevelClassMappings()) {
ClassMapping<?, ?> srgMcpClass = srgMcp
.getOrCreateClassMapping(notchSrgTopLevelClass.getFullDeobfuscatedName());

for (FieldMapping notchSrgField : notchSrgTopLevelClass.getFieldMappings()) {
srgMcpClass.createFieldMapping(notchSrgField.getDeobfuscatedName()).setDeobfuscatedName(
fields.getOrDefault(notchSrgField.getDeobfuscatedName(), notchSrgField.getDeobfuscatedName()));
}

for(MethodMapping notchSrgMethod : notchSrgTopLevelClass.getMethodMappings()) {
for (MethodMapping notchSrgMethod : notchSrgTopLevelClass.getMethodMappings()) {
srgMcpClass.createMethodMapping(
notchSrgMethod.getDeobfuscatedName(),
notchSrgMethod.getDeobfuscatedDescriptor())
.setDeobfuscatedName(methods.getOrDefault(
notchSrgMethod.getDeobfuscatedName(),
notchSrgMethod.getDeobfuscatedName()));
notchSrgMethod.getDeobfuscatedName(),
notchSrgMethod.getDeobfuscatedDescriptor()).setDeobfuscatedName(
methods.getOrDefault(
notchSrgMethod.getDeobfuscatedName(),
notchSrgMethod.getDeobfuscatedName()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ public ModUtils(Project project, MinecraftExtension mcExt, MinecraftTasks minecr

project.getTasks().register("migrateMappings", MigrateMappingsTask.class, task -> {
task.setGroup(TASK_GROUP_USER);
task.setDescription(
"Migrate main source set to a new set of mappings");
task.setDescription("Migrate main source set to a new set of mappings");
task.dependsOn("extractMcpData");
task.dependsOn("extractForgeUserdev");
task.dependsOn("packagePatchedMc");
Expand Down

0 comments on commit 262117c

Please sign in to comment.