Skip to content

Commit c645e48

Browse files
committed
Add optional mappings to MCPTask
Must be enabled with the `--mappings` flag. Also added the `--parchment` flag because dear god, we need parameter names in ForgeDev soon.
1 parent 51241b7 commit c645e48

File tree

4 files changed

+76
-21
lines changed

4 files changed

+76
-21
lines changed

src/main/java/net/minecraftforge/mcmaven/cli/MCPTask.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@
88
import java.util.Set;
99

1010
import joptsimple.OptionParser;
11+
import net.minecraftforge.mcmaven.impl.MinecraftMaven;
1112
import net.minecraftforge.mcmaven.impl.cache.Cache;
13+
import net.minecraftforge.mcmaven.impl.mappings.Mappings;
14+
import net.minecraftforge.mcmaven.impl.mappings.ParchmentMappings;
1215
import net.minecraftforge.mcmaven.impl.repo.forge.Patcher;
1316
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MCPConfigRepo;
17+
import net.minecraftforge.mcmaven.impl.tasks.RenameTask;
1418
import net.minecraftforge.mcmaven.impl.util.Artifact;
1519
import net.minecraftforge.mcmaven.impl.util.Task;
1620
import net.minecraftforge.mcmaven.impl.util.Util;
1721
import net.minecraftforge.util.hash.HashFunction;
1822
import net.minecraftforge.util.hash.HashStore;
1923
import net.minecraftforge.util.logging.Log;
2024

25+
// TODO [Mavenizer][MCPTask] Cleanup. Works well but is a mess.
2126
public class MCPTask {
2227
public static void run(String[] args) throws Exception {
2328
// TODO [MCMavenizer] Make this into a --log [level] option
@@ -61,11 +66,18 @@ public static void run(String[] args) throws Exception {
6166

6267
var atO = parser.accepts("at",
6368
"Access Transformer config file to apply")
64-
.withRequiredArg().ofType(File.class);
69+
.withOptionalArg().ofType(File.class);
6570

6671
var sasO = parser.accepts("sas",
6772
"Side Annotation Stripper confg file to apply")
68-
.withRequiredArg().ofType(File.class);
73+
.withOptionalArg().ofType(File.class);
74+
75+
var mappingsO = parser.accepts("mappings",
76+
"Use to enable using official mappings");
77+
78+
var parchmentO = parser.accepts("parchment",
79+
"Version of parchment mappings to use, snapshots are not supported")
80+
.availableIf(mappingsO).withRequiredArg();
6981
//@formatter:on
7082

7183
var options = parser.parse(args);
@@ -130,17 +142,36 @@ public static void run(String[] args) throws Exception {
130142
}
131143

132144
File sources = null;
133-
Log.info("Creating MCP Source Jar");
134-
var indent = Log.push();
135-
try {
136-
sources = sourcesTask.get();
137-
} finally {
138-
Log.pop(indent);
145+
{
146+
Log.info("Creating MCP Source Jar");
147+
var indent = Log.push();
148+
try {
149+
sources = sourcesTask.execute();
150+
} finally {
151+
Log.pop(indent);
152+
}
139153
}
140154

141155
var cache = HashStore.fromFile(output)
142156
.add("sources", sources);
143157

158+
if (options.has(mappingsO)) {
159+
Log.info("Renaming MCP Source Jar");
160+
var indent = Log.push();
161+
try {
162+
var mappings = options.has(parchmentO)
163+
? new ParchmentMappings(options.valueOf(parchmentO))
164+
: new Mappings("official", null).withMCVersion(MinecraftMaven.mcpToMcVersion(artifact.getVersion()));
165+
166+
var renameTask = new RenameTask(side.getBuildFolder(), pipeline, side, Task.existing("sources", sources), mappings).get();
167+
sources = renameTask.execute();
168+
} finally {
169+
Log.pop(indent);
170+
}
171+
172+
cache.add("renamed", sources);
173+
}
174+
144175
if (!output.exists() || !cache.isSame()) {
145176
try {
146177
org.apache.commons.io.FileUtils.copyFile(sources, output);

src/main/java/net/minecraftforge/mcmaven/impl/MinecraftMaven.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static String forgeToMcVersion(String version) {
101101
return version.substring(0, idx).replace('_', '-');
102102
}
103103

104-
private static String mcpToMcVersion(String version) {
104+
public static String mcpToMcVersion(String version) {
105105
// MCP names can either be {MCVersion} or {MCVersion}-{Timestamp}, EXA: 1.21.1-20240808.132146
106106
// So lets see if the thing following the last - matches a timestamp
107107
int idx = version.lastIndexOf('-');

src/main/java/net/minecraftforge/mcmaven/impl/tasks/RenameTask.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* Applies mappings provided by a ziped csv file to them and returns the new sources.
3232
*/
3333
public final class RenameTask implements Supplier<Task> {
34-
private final Artifact name;
34+
private final String name;
3535
private final MCPSide side;
3636
private final Task task;
3737

@@ -43,6 +43,17 @@ public final class RenameTask implements Supplier<Task> {
4343
* @param sources The task that creates the unnamed sources
4444
*/
4545
public RenameTask(File build, Artifact name, MCPSide side, Task sources, Mappings mappings) {
46+
this(build, name.getName(), side, sources, mappings);
47+
}
48+
49+
/**
50+
* Creates a new renamer for the given patcher.
51+
*
52+
* @param build The directory where the output will be stored
53+
* @param name The development artifact, only used for the task name
54+
* @param sources The task that creates the unnamed sources
55+
*/
56+
public RenameTask(File build, String name, MCPSide side, Task sources, Mappings mappings) {
4657
this.name = name;
4758
this.side = side;
4859
this.task = this.remapSources(sources, mappings.getFolder(build), mappings);
@@ -56,7 +67,7 @@ public Task get() {
5667
private Task remapSources(Task input, File outputDir, Mappings provider) {
5768
var output = new File(outputDir, "remapped.jar");
5869
var mappings = provider.getCsvZip(side);
59-
return Task.named("remap[" + this.name.getName() + "][" + provider + ']',
70+
return Task.named("remap[" + this.name + "][" + provider + ']',
6071
Set.of(input, mappings),
6172
() -> remapSourcesImpl(input, mappings, output)
6273
);

src/main/java/net/minecraftforge/mcmaven/impl/util/Task.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@
1010
import java.util.Arrays;
1111
import java.util.Iterator;
1212
import java.util.Set;
13+
import java.util.concurrent.Callable;
1314
import java.util.function.Supplier;
1415

1516
/** Represents a task that can be executed. Tasks in this tool <strong>will always</strong> provide a file. */
16-
public sealed interface Task extends Supplier<File> permits Task.Simple {
17-
static Task named(String name, ThrowingSupplier<File> supplier) {
17+
public sealed interface Task extends Supplier<File> permits Task.Existing, Task.Simple {
18+
static Task existing(String name, File file) {
19+
return new Existing(name, file);
20+
}
21+
22+
static Task named(String name, Callable<File> supplier) {
1823
return named(name, Set.of(), supplier);
1924
}
2025

21-
static Task named(String name, Iterable<? extends Task> deps, ThrowingSupplier<File> supplier) {
26+
static Task named(String name, Iterable<? extends Task> deps, Callable<File> supplier) {
2227
return new Simple(name, deps, supplier);
2328
}
2429

@@ -66,17 +71,13 @@ default File get() {
6671
return this.execute();
6772
}
6873

69-
interface ThrowingSupplier<T> {
70-
T get() throws Exception;
71-
}
72-
7374
final class Simple implements Task {
7475
private final String name;
7576
private final Iterable<? extends Task> deps;
76-
private final ThrowingSupplier<File> supplier;
77+
private final Callable<File> supplier;
7778
private File file;
7879

79-
private Simple(String name, Iterable<? extends Task> deps, ThrowingSupplier<File> supplier) {
80+
private Simple(String name, Iterable<? extends Task> deps, Callable<File> supplier) {
8081
this.name = name;
8182
this.deps = deps;
8283
this.supplier = supplier;
@@ -90,7 +91,7 @@ public File execute() {
9091
Log.info(name);
9192
Log.push();
9293
try {
93-
this.file = supplier.get();
94+
this.file = supplier.call();
9495
} catch (Exception e) {
9596
Util.sneak(e);
9697
}
@@ -116,4 +117,16 @@ public String toString() {
116117
return "Task[" + this.name + ']';
117118
}
118119
}
120+
121+
record Existing(String name, File execute) implements Task {
122+
@Override
123+
public boolean resolved() {
124+
return true;
125+
}
126+
127+
@Override
128+
public String toString() {
129+
return "Task[" + this.name + ']';
130+
}
131+
}
119132
}

0 commit comments

Comments
 (0)