Skip to content

Commit

Permalink
Add Parchment support
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed May 25, 2024
1 parent f5858f1 commit e8d4110
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 83 deletions.
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,19 @@ idea {
project {
settings {
runConfigurations {
"Run Neoforge 1.20.6 (joined) + Parchment"(Application) {
mainClass = mainClassName
programParameters = "run --dist joined --neoforge net.neoforged:neoforge:20.6.72-beta:userdev --add-repository=https://maven.parchmentmc.org --parchment-data=org.parchmentmc.data:parchment-1.20.6:2024.05.01:checked@zip --write-result=compiled:build/minecraft.jar --write-result=clientResources:build/client-extra.jar --write-result=sources:build/minecraft-sources.jar"
moduleRef(project, sourceSets.main)
}
"Run Neoforge 1.20.6 (joined)"(Application) {
mainClass = mainClassName
programParameters = "run --dist joined --neoforge net.neoforged:neoforge:20.6.72-beta:userdev --write-result=compiled:build/minecraft.jar --write-result=clientResources:build/client-extra.jar --write-result=sources:build/minecraft-sources.jar"
moduleRef(project, sourceSets.main)
}
"Run Neoform 1.20.6 (joined)"(Application) {
mainClass = mainClassName
programParameters = "run --dist joined --neoforge net.neoforged:neoform:1.20.6-20240429.153634@zip --write-result=compiled:build/minecraft.jar --write-result=clientResources:build/client-extra.jar --write-result=sources:build/minecraft-sources.jar"
programParameters = "run --dist joined --neoform net.neoforged:neoform:1.20.6-20240429.153634@zip --write-result=compiled:build/minecraft.jar --write-result=clientResources:build/client-extra.jar --write-result=sources:build/minecraft-sources.jar"
moduleRef(project, sourceSets.main)
}
"Run clean-cache"(Application) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@
import net.neoforged.neoform.runtime.cache.CacheKeyBuilder;
import net.neoforged.neoform.runtime.engine.ProcessingEnvironment;
import net.neoforged.neoform.runtime.utils.MavenCoordinate;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.zip.ZipFile;

public class ApplySourceAccessTransformersAction extends ExternalJavaToolAction {
private static final MavenCoordinate JST_TOOL_COORDINATE = MavenCoordinate.parse("net.neoforged.jst:jst-cli-bundle:1.0.35");
private final String accessTransformersData;
private static final MavenCoordinate JST_TOOL_COORDINATE = MavenCoordinate.parse("net.neoforged.jst:jst-cli-bundle:1.0.37");

/**
* names of {@linkplain net.neoforged.neoform.runtime.engine.NeoFormEngine#addDataSource(String, ZipFile, String) data sources} containing
* access transformers to apply.
*/
private List<String> accessTransformersData = new ArrayList<>();

/**
* Additional paths to access transformers.
*/
private List<Path> additionalAccessTransformers = new ArrayList<>();

public ApplySourceAccessTransformersAction(String accessTransformersData) {
/**
* Path to a Parchment data archive.
*/
@Nullable
private Path parchmentData;

public ApplySourceAccessTransformersAction() {
super(JST_TOOL_COORDINATE);
this.accessTransformersData = accessTransformersData;
}

@Override
Expand All @@ -27,22 +43,32 @@ public void run(ProcessingEnvironment environment) throws IOException, Interrupt
Collections.addAll(args,
"--libraries-list", "{libraries}",
"--in-format", "ARCHIVE",
"--out-format", "ARCHIVE",
"--enable-accesstransformers"
"--out-format", "ARCHIVE"
);

var accessTransformers = environment.extractData(accessTransformersData);
if (!accessTransformersData.isEmpty() || !additionalAccessTransformers.isEmpty()) {
args.add("--enable-accesstransformers");

for (var dataId : accessTransformersData) {
var accessTransformers = environment.extractData(dataId);

try (var stream = Files.walk(accessTransformers)) {
stream.filter(Files::isRegularFile).forEach(path -> {
try (var stream = Files.walk(accessTransformers)) {
stream.filter(Files::isRegularFile).forEach(path -> {
args.add("--access-transformer");
args.add(environment.getWorkspace().relativize(path).toString());
});
}
}

for (var path : additionalAccessTransformers) {
args.add("--access-transformer");
args.add(environment.getWorkspace().relativize(path).toString());
});
}
}

for (var path : additionalAccessTransformers) {
args.add("--access-transformer");
args.add(environment.getWorkspace().relativize(path).toString());
if (parchmentData != null) {
args.add("--enable-parchment");
args.add("--parchment-mappings=" + environment.getPathArgument(parchmentData.toAbsolutePath()));
}

Collections.addAll(args, "{input}", "{output}");
Expand All @@ -54,7 +80,19 @@ public void run(ProcessingEnvironment environment) throws IOException, Interrupt
@Override
public void computeCacheKey(CacheKeyBuilder ck) {
super.computeCacheKey(ck);
ck.addStrings("access transformers data ids", accessTransformersData);
ck.addPaths("additional access transformers", additionalAccessTransformers);
if (parchmentData != null) {
ck.addPath("parchment data", parchmentData);
}
}

public List<String> getAccessTransformersData() {
return accessTransformersData;
}

public void setAccessTransformersData(List<String> accessTransformersData) {
this.accessTransformersData = List.copyOf(accessTransformersData);
}

public List<Path> getAdditionalAccessTransformers() {
Expand All @@ -64,4 +102,12 @@ public List<Path> getAdditionalAccessTransformers() {
public void setAdditionalAccessTransformers(List<Path> additionalAccessTransformers) {
this.additionalAccessTransformers = List.copyOf(additionalAccessTransformers);
}

public @Nullable Path getParchmentData() {
return parchmentData;
}

public void setParchmentData(@Nullable Path parchmentData) {
this.parchmentData = parchmentData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ public void copyTo(ZipOutputStream out) throws IOException {
try (var in = zf.getInputStream(entry)) {
// Relocate the entry
var copiedEntry = new ZipEntry(entry.getName().substring(sourcePath.length()));
if (copiedEntry.getName().isEmpty()) {
continue;
}
copiedEntry.setMethod(entry.getMethod());

out.putNextEntry(entry);
out.putNextEntry(copiedEntry);
in.transferTo(out);
out.closeEntry();
} catch (ZipException e) {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/net/neoforged/neoform/runtime/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

Expand All @@ -20,9 +21,12 @@ public class Main {
@Option(names = "--work-dir", scope = CommandLine.ScopeType.INHERIT, description = "Where temporary working directories are stored. Defaults to the subfolder 'work' in the NFRT home dir.")
Path workDir;

@Option(names = "--repository", arity = "*", scope = CommandLine.ScopeType.INHERIT, description = "Add a Maven repository for downloading artifacts.")
@Option(names = "--repository", arity = "*", scope = CommandLine.ScopeType.INHERIT, description = "Overriddes Maven repositories used for downloading artifacts.")
List<URI> repositories = List.of(URI.create("https://maven.neoforged.net/releases/"), Path.of(System.getProperty("user.home"), ".m2", "repository").toUri());

@Option(names = "--add-repository", arity = "*", scope = CommandLine.ScopeType.INHERIT, description = "Add Maven repositories for downloading artifacts.")
List<URI> additionalRepositories = new ArrayList<>();

@Option(names = "--artifact-manifest", scope = CommandLine.ScopeType.INHERIT)
Path artifactManifest;

Expand Down Expand Up @@ -56,4 +60,10 @@ public static void main(String... args) {
int exitCode = commandLine.execute(args);
System.exit(exitCode);
}

public List<URI> getEffectiveRepositories() {
var result = new ArrayList<>(repositories);
result.addAll(additionalRepositories);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public final Integer call() throws Exception {
}

lockManager.setVerbose(commonOptions.verbose);
var artifactManager = new ArtifactManager(commonOptions.repositories, cacheManager, downloadManager, lockManager, commonOptions.launcherManifestUrl);
var artifactManager = new ArtifactManager(commonOptions.getEffectiveRepositories(), cacheManager, downloadManager, lockManager, commonOptions.launcherManifestUrl);

if (commonOptions.artifactManifest != null) {
artifactManager.loadArtifactManifest(commonOptions.artifactManifest);
Expand Down
147 changes: 85 additions & 62 deletions src/main/java/net/neoforged/neoform/runtime/cli/RunNeoFormCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class RunNeoFormCommand extends NeoFormEngineCommand {
@CommandLine.Option(names = "--access-transformer", arity = "*")
List<String> additionalAccessTransformers = new ArrayList<>();

@CommandLine.Option(names = "--parchment-data", description = "Path or Maven coordinates of parchment data to use")
String parchmentData;

static class SourceArtifacts {
@CommandLine.Option(names = "--neoform")
String neoform;
Expand All @@ -60,79 +63,72 @@ static class SourceArtifacts {
protected void runWithNeoFormEngine(NeoFormEngine engine, List<AutoCloseable> closables) throws IOException, InterruptedException {
var artifactManager = engine.getArtifactManager();

List<GraphTransform> transforms = new ArrayList<>();
if (sourceArtifacts.neoforge != null) {
var neoforgeArtifact = artifactManager.get(sourceArtifacts.neoforge);
try (var neoforgeZipFile = new JarFile(neoforgeArtifact.path().toFile())) {
var neoforgeConfig = NeoForgeConfig.from(neoforgeZipFile);
MavenCoordinate neoformArtifact = MavenCoordinate.parse(neoforgeConfig.neoformArtifact());
// Allow it to be overridden
if (sourceArtifacts.neoform != null) {
LOG.println("Overriding NeoForm version " + neoformArtifact + " with CLI argument " + sourceArtifacts.neoform);
neoformArtifact = MavenCoordinate.parse(sourceArtifacts.neoform);
}

engine.loadNeoFormData(neoformArtifact, dist);

// Add NeoForge specific data sources
engine.addDataSource("neoForgeAccessTransformers", neoforgeZipFile, neoforgeConfig.accessTransformersFolder());

// Build the graph transformations needed to apply NeoForge to the NeoForm execution
var neoforgeZipFile = engine.addManagedResource(new JarFile(neoforgeArtifact.path().toFile()));
var neoforgeConfig = NeoForgeConfig.from(neoforgeZipFile);
var neoformArtifact = MavenCoordinate.parse(neoforgeConfig.neoformArtifact());
// Allow it to be overridden
if (sourceArtifacts.neoform != null) {
LOG.println("Overriding NeoForm version " + neoformArtifact + " with CLI argument " + sourceArtifacts.neoform);
neoformArtifact = MavenCoordinate.parse(sourceArtifacts.neoform);
}

// Add NeoForge libraries to the list of libraries
transforms.add(new ModifyAction<>(
"recompile",
RecompileSourcesAction.class,
action -> {
action.getClasspath().addMavenLibraries(neoforgeConfig.libraries());
}
));
engine.loadNeoFormData(neoformArtifact, dist);

// Also inject NeoForge sources, which we can get from the sources file
var neoforgeSources = artifactManager.get(neoforgeConfig.sourcesArtifact()).path();
var neoforgeSourcesZip = new ZipFile(neoforgeSources.toFile());
closables.add(neoforgeSourcesZip);

transforms.add(new ReplaceNodeOutput(
"patch",
"output",
"transformSources",
(builder, previousNodeOutput) -> {
builder.input("input", previousNodeOutput.asInput());
builder.inputFromNodeOutput("libraries", "listLibraries", "output");
var action = new ApplySourceAccessTransformersAction("neoForgeAccessTransformers");
action.setAdditionalAccessTransformers(additionalAccessTransformers.stream().map(Paths::get).toList());
builder.action(action);
return builder.output("output", NodeOutputType.ZIP, "Sources with additional transforms (ATs, Parchment) applied");
}
));
// Add NeoForge specific data sources
engine.addDataSource("neoForgeAccessTransformers", neoforgeZipFile, neoforgeConfig.accessTransformersFolder());

transforms.add(new ModifyAction<>(
"inject",
InjectZipContentAction.class,
action -> {
action.getInjectedSources().add(
new InjectFromZipFileSource(neoforgeSourcesZip, "/")
);
}
));
// Build the graph transformations needed to apply NeoForge to the NeoForm execution
List<GraphTransform> transforms = new ArrayList<>();

// Append a patch step to the NeoForge patches
transforms.add(new ReplaceNodeOutput("patch", "output", "applyNeoforgePatches",
(builder, previousOutput) -> {
return PatchActionFactory.makeAction(builder, neoforgeArtifact.path(), neoforgeConfig.patchesFolder(), previousOutput);
}
));
// Add NeoForge libraries to the list of libraries
transforms.add(new ModifyAction<>(
"recompile",
RecompileSourcesAction.class,
action -> {
action.getClasspath().addMavenLibraries(neoforgeConfig.libraries());
}
));

// Also inject NeoForge sources, which we can get from the sources file
var neoforgeSources = artifactManager.get(neoforgeConfig.sourcesArtifact()).path();
var neoforgeSourcesZip = new ZipFile(neoforgeSources.toFile());
closables.add(neoforgeSourcesZip);

var transformSources = getOrAddTransformSourcesNode(engine);
transformSources.setAccessTransformersData(List.of("neoForgeAccessTransformers"));
transformSources.setAdditionalAccessTransformers(additionalAccessTransformers.stream().map(Paths::get).toList());

transforms.add(new ModifyAction<>(
"inject",
InjectZipContentAction.class,
action -> {
action.getInjectedSources().add(
new InjectFromZipFileSource(neoforgeSourcesZip, "/")
);
}
));

engine.applyTransforms(transforms);
// Append a patch step to the NeoForge patches
transforms.add(new ReplaceNodeOutput("patch", "output", "applyNeoforgePatches",
(builder, previousOutput) -> {
return PatchActionFactory.makeAction(builder, neoforgeArtifact.path(), neoforgeConfig.patchesFolder(), previousOutput);
}
));

execute(engine);
}
engine.applyTransforms(transforms);
} else {
engine.loadNeoFormData(MavenCoordinate.parse(sourceArtifacts.neoform), dist);
}

execute(engine);
if (parchmentData != null) {
var transformSources = getOrAddTransformSourcesNode(engine);
var parchmentDataFile = artifactManager.get(parchmentData);
transformSources.setParchmentData(parchmentDataFile.path());
}

execute(engine);
}

private void execute(NeoFormEngine engine) throws InterruptedException, IOException {
Expand Down Expand Up @@ -179,5 +175,32 @@ private void execute(NeoFormEngine engine) throws InterruptedException, IOExcept
FileUtil.atomicMove(tmpFile, entry.getValue());
}
}
}

private static ApplySourceAccessTransformersAction getOrAddTransformSourcesNode(NeoFormEngine engine) {
var graph = engine.getGraph();
var transformNode = graph.getNode("transformSources");
if (transformNode != null) {
if (transformNode.action() instanceof ApplySourceAccessTransformersAction action) {
return action;
} else {
throw new IllegalStateException("Node transformSources has a different action type than expected. Expected: "
+ ApplySourceAccessTransformersAction.class + " but got " + transformNode.action().getClass());
}
}

new ReplaceNodeOutput(
"patch",
"output",
"transformSources",
(builder, previousNodeOutput) -> {
builder.input("input", previousNodeOutput.asInput());
builder.inputFromNodeOutput("libraries", "listLibraries", "output");
var action = new ApplySourceAccessTransformersAction();
builder.action(action);
return builder.output("output", NodeOutputType.ZIP, "Sources with additional transforms (ATs, Parchment) applied");
}
).apply(engine, graph);

return getOrAddTransformSourcesNode(engine);
}
}
Loading

0 comments on commit e8d4110

Please sign in to comment.