-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic mapping migration #57
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4853450
Add migrateMappings foundation
makamys 3aae5a4
Use paths from configuration
makamys 1e792f0
Only write diff to disk if debug flag is enabled, use project dir
makamys 525eeeb
Actually remap sources
makamys 3899d35
Use project's source compatibility
makamys 1e933ad
Merge service files
makamys aadef57
Fix inner classes not getting remapped
makamys 62ac0ae
Set task dependencies
makamys e61186c
Make paths relative to project dir
makamys 22d159c
Fix broken things
makamys 858b7d9
Remove duplicate files from eclipse dependencies
makamys 9bf3cbb
Avoid calling getByName
makamys 4954089
Use DirectoryProperties
makamys 262117c
spotlessApply
makamys 2ea7e62
Add source mappings and classpath as task inputs
makamys 73a8d5b
Clean up classpath configuration
makamys 7f297bc
Use MappingIO and move CSV loading to Utilities
makamys ac3e154
Remove redundant Project#files call
makamys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
plugin/src/main/java/com/gtnewhorizons/retrofuturagradle/modutils/MigrateMappingsTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package com.gtnewhorizons.retrofuturagradle.modutils; | ||
|
||
import java.io.CharArrayReader; | ||
import java.io.CharArrayWriter; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Collections; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.inject.Inject; | ||
|
||
import net.fabricmc.mappingio.adapter.MappingNsRenamer; | ||
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch; | ||
import net.fabricmc.mappingio.format.srg.SrgFileReader; | ||
import net.fabricmc.mappingio.format.srg.SrgFileWriter; | ||
import net.fabricmc.mappingio.tree.MemoryMappingTree; | ||
import net.fabricmc.mappingio.tree.VisitableMappingTree; | ||
|
||
import org.cadixdev.lorenz.MappingSet; | ||
import org.cadixdev.lorenz.io.MappingFormats; | ||
import org.cadixdev.lorenz.io.MappingsReader; | ||
import org.cadixdev.lorenz.io.MappingsWriter; | ||
import org.cadixdev.mercury.Mercury; | ||
import org.cadixdev.mercury.mixin.MixinRemapper; | ||
import org.cadixdev.mercury.remapper.MercuryRemapper; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.file.ConfigurableFileCollection; | ||
import org.gradle.api.file.DirectoryProperty; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.plugins.JavaPluginExtension; | ||
import org.gradle.api.tasks.Classpath; | ||
import org.gradle.api.tasks.InputDirectory; | ||
import org.gradle.api.tasks.InputFile; | ||
import org.gradle.api.tasks.InputFiles; | ||
import org.gradle.api.tasks.Optional; | ||
import org.gradle.api.tasks.OutputDirectory; | ||
import org.gradle.api.tasks.PathSensitive; | ||
import org.gradle.api.tasks.PathSensitivity; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.gradle.api.tasks.options.Option; | ||
|
||
import com.gtnewhorizons.retrofuturagradle.util.Utilities; | ||
|
||
public abstract class MigrateMappingsTask extends DefaultTask { | ||
|
||
private static final boolean DEBUG_WRITE_DIFF = Boolean.parseBoolean(System.getenv("RFG_DEBUG_WRITE_MAPPING_DIFF")); | ||
|
||
@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.") | ||
public abstract DirectoryProperty getMcpDir(); | ||
|
||
@InputDirectory | ||
@PathSensitive(PathSensitivity.RELATIVE) | ||
@Option(option = "inputDir", description = "The directory containing the source code to migrate.") | ||
public abstract DirectoryProperty getInputDir(); | ||
|
||
@OutputDirectory | ||
@Option(option = "outputDir", description = "The directory the migrated source code should be written to.") | ||
public abstract DirectoryProperty getOutputDir(); | ||
|
||
@InputFile | ||
@PathSensitive(PathSensitivity.NONE) | ||
public abstract RegularFileProperty getSourceSrg(); | ||
|
||
@InputFile | ||
@PathSensitive(PathSensitivity.NONE) | ||
public abstract RegularFileProperty getSourceFieldsCsv(); | ||
|
||
@InputFile | ||
@PathSensitive(PathSensitivity.NONE) | ||
public abstract RegularFileProperty getSourceMethodsCsv(); | ||
|
||
@InputFiles | ||
@Classpath | ||
public abstract ConfigurableFileCollection getCompileClasspath(); | ||
|
||
@Inject | ||
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()) { | ||
throw new IllegalArgumentException("A target mapping must be set using --mcpDir."); | ||
} | ||
File sourceFields = getSourceFieldsCsv().getAsFile().get(); | ||
File sourceMethods = getSourceMethodsCsv().getAsFile().get(); | ||
|
||
File target = getMcpDir().get().getAsFile(); | ||
File srg = getSourceSrg().getAsFile().get(); | ||
|
||
MemoryMappingTree notchSrg = new MemoryMappingTree(); | ||
SrgFileReader.read(Files.newBufferedReader(srg.toPath()), "official", "srg", notchSrg); | ||
|
||
MemoryMappingTree sourceSrgMcp = new MemoryMappingTree(); | ||
Utilities.loadSrgMcpMappings(sourceSrgMcp, notchSrg, sourceMethods, sourceFields, null, null); | ||
|
||
MemoryMappingTree targetSrgMcp = new MemoryMappingTree(); | ||
Utilities.loadSrgMcpMappings( | ||
targetSrgMcp, | ||
notchSrg, | ||
new File(target, "methods.csv"), | ||
new File(target, "fields.csv"), | ||
null, | ||
null); | ||
|
||
MemoryMappingTree joinedSrgMcp = new MemoryMappingTree(); | ||
joinedSrgMcp.setSrcNamespace("srg"); | ||
sourceSrgMcp.accept(new MappingNsRenamer(joinedSrgMcp, Collections.singletonMap("mcp", "mcpSource"))); | ||
targetSrgMcp.accept(new MappingNsRenamer(joinedSrgMcp, Collections.singletonMap("mcp", "mcpTarget"))); | ||
|
||
MemoryMappingTree diffMcp = new MemoryMappingTree(); | ||
diffMcp.setSrcNamespace("mcpSource"); | ||
joinedSrgMcp.accept(new MappingSourceNsSwitch(diffMcp, "mcpSource")); | ||
diffMcp.setDstNamespaces(Collections.singletonList("mcpTarget")); | ||
|
||
MappingSet diffMcpLorenz = mappingIoToLorenz(diffMcp); | ||
|
||
if (DEBUG_WRITE_DIFF) { | ||
try (MappingsWriter w = MappingFormats.SRG | ||
.createWriter(new File(getProject().getRootDir(), "diff.srg").toPath())) { | ||
w.write(diffMcpLorenz); | ||
} | ||
} | ||
|
||
final Mercury mercury = new Mercury(); | ||
|
||
mercury.getClassPath().addAll( | ||
getCompileClasspath().getFiles().stream().map(File::toPath).filter(Files::exists) | ||
.collect(Collectors.toList())); | ||
|
||
// 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(diffMcpLorenz)); | ||
mercury.getProcessors().add(MercuryRemapper.create(diffMcpLorenz)); | ||
|
||
mercury.setSourceCompatibility( | ||
getProject().getExtensions().getByType(JavaPluginExtension.class).getSourceCompatibility().toString()); | ||
|
||
mercury.rewrite(getInputDir().getAsFile().get().toPath(), getOutputDir().getAsFile().get().toPath()); | ||
} | ||
|
||
/** Converts a MappingIO mapping to Lorenz's type. May not preserve parameter names and comments. */ | ||
private static MappingSet mappingIoToLorenz(VisitableMappingTree mappingIoMappings) throws IOException { | ||
CharArrayWriter writer = new CharArrayWriter(); | ||
mappingIoMappings.accept(new SrgFileWriter(writer, false)); | ||
CharArrayReader reader = new CharArrayReader(writer.toCharArray()); | ||
MappingSet lorenzMappings = MappingSet.create(); | ||
|
||
try (final MappingsReader mappingsReader = MappingFormats.SRG.createReader(reader)) { | ||
mappingsReader.read(lorenzMappings); | ||
} | ||
return lorenzMappings; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not entirely sure if I'm supposed to use
.named
here, when I tried using it it gave me an inscrutable compiler error so I went with getting it directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConfigurableFileCollection#from
evaluates whatever is passed as-if they're passed toProject#files
already, no need to also callProject#files
here.