Skip to content
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

Parchment in NeoDev #224

Merged
merged 11 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Constants {
public static final String DEFAULT_PARCHMENT_GROUP = "org.parchmentmc.data"
public static final String DEFAULT_PARCHMENT_ARTIFACT_PREFIX = "parchment-"
public static final String DEFAULT_PARCHMENT_MAVEN_URL = "https://maven.parchmentmc.org/"
public static final String JST_TOOL_ARTIFACT = "net.neoforged.jst:jst-cli-bundle:1.0.43"
public static final String JST_TOOL_ARTIFACT = "net.neoforged.jst:jst-cli-bundle:1.0.55"
public static final String DEVLOGIN_TOOL_ARTIFACT = "net.covers1624:DevLogin:0.1.0.4"
public static final String DEVLOGIN_MAIN_CLASS = "net.covers1624.devlogin.DevLogin"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ protected void bakeDefinition(NeoFormRuntimeDefinition definition) {
}

neoFormRuntimeTaskProvider = createExecute(spec, step, function);

if (step.getType().equals("mergeMappings")) {
neoFormRuntimeTaskProvider.configure(tsk -> tsk.getOutputFileName().set("output.tsrg"));
}
}

Optional<TaskProvider<? extends WithOutput>> finalAdaptedInput = adaptedInput;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.neoforged.gradle.dsl.common.tasks.WithOutput;
import net.neoforged.gradle.neoform.runtime.definition.NeoFormRuntimeDefinition;
import net.neoforged.gradle.platform.runtime.runtime.specification.RuntimeDevRuntimeSpecification;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.tasks.TaskProvider;
import org.jetbrains.annotations.NotNull;

Expand All @@ -22,10 +21,12 @@
//TODO: Create DSL for runtime
public final class RuntimeDevRuntimeDefinition extends CommonRuntimeDefinition<RuntimeDevRuntimeSpecification> implements IDelegatingRuntimeDefinition<RuntimeDevRuntimeSpecification> {
private final NeoFormRuntimeDefinition joinedNeoFormRuntimeDefinition;

public RuntimeDevRuntimeDefinition(@NotNull RuntimeDevRuntimeSpecification specification, NeoFormRuntimeDefinition joinedNeoFormRuntimeDefinition, TaskProvider<? extends ArtifactProvider> sourcesProvider) {
private final TaskProvider<? extends WithOutput> patchBase;

public RuntimeDevRuntimeDefinition(@NotNull RuntimeDevRuntimeSpecification specification, NeoFormRuntimeDefinition joinedNeoFormRuntimeDefinition, TaskProvider<? extends ArtifactProvider> sourcesProvider, TaskProvider<? extends WithOutput> patchBase) {
super(specification, joinedNeoFormRuntimeDefinition.getTasks(), sourcesProvider, joinedNeoFormRuntimeDefinition.getRawJarTask(), joinedNeoFormRuntimeDefinition.getGameArtifactProvidingTasks(), joinedNeoFormRuntimeDefinition.getMinecraftDependenciesConfiguration(), joinedNeoFormRuntimeDefinition::configureAssociatedTask, joinedNeoFormRuntimeDefinition.getVersionJson());
this.joinedNeoFormRuntimeDefinition = joinedNeoFormRuntimeDefinition;
this.patchBase = patchBase;
}

public NeoFormRuntimeDefinition getJoinedNeoFormRuntimeDefinition() {
Expand Down Expand Up @@ -64,6 +65,10 @@ protected Map<String, String> buildRunInterpolationData(RunImpl run) {
return interpolationData;
}

public TaskProvider<? extends WithOutput> getPatchBase() {
return patchBase;
}

@Override
public Definition<?> getDelegate() {
return joinedNeoFormRuntimeDefinition;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package net.neoforged.gradle.platform.runtime.runtime.extension;

import io.codechicken.diffpatch.util.PatchMode;
import net.neoforged.gradle.common.runtime.definition.CommonRuntimeDefinition;
import net.neoforged.gradle.common.runtime.extensions.CommonRuntimeExtension;
import net.neoforged.gradle.dsl.common.extensions.Mappings;
import net.neoforged.gradle.dsl.common.extensions.Minecraft;
import net.neoforged.gradle.common.runtime.specification.CommonRuntimeSpecification;
import net.neoforged.gradle.common.runtime.tasks.DefaultExecute;
import net.neoforged.gradle.common.util.ToolUtilities;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Subsystems;
import net.neoforged.gradle.dsl.common.tasks.ArtifactProvider;
import net.neoforged.gradle.dsl.common.tasks.WithOutput;
import net.neoforged.gradle.dsl.common.tasks.specifications.ExecuteSpecification;
import net.neoforged.gradle.dsl.common.util.CommonRuntimeUtils;
import net.neoforged.gradle.neoform.runtime.definition.NeoFormRuntimeDefinition;
import net.neoforged.gradle.platform.runtime.runtime.definition.RuntimeDevRuntimeDefinition;
import net.neoforged.gradle.platform.runtime.runtime.specification.RuntimeDevRuntimeSpecification;
import net.neoforged.gradle.platform.runtime.runtime.tasks.ApplyPatches;
import org.gradle.api.Project;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.inject.Inject;
import java.io.File;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public abstract class RuntimeDevRuntimeExtension extends CommonRuntimeExtension<RuntimeDevRuntimeSpecification, RuntimeDevRuntimeSpecification.Builder, RuntimeDevRuntimeDefinition> {

Expand All @@ -30,29 +41,96 @@ public RuntimeDevRuntimeExtension(Project project) {

NeoFormRuntimeDefinition neoformRuntime = spec.getNeoFormRuntime();

final TaskProvider<? extends WithOutput> patchBase;
if (spec.getParchmentArtifact() != null) {
patchBase = applyParchment(
spec.getProject(),
"applyParchment",
getProject().provider(() -> ToolUtilities.resolveTool(getProject(), spec.getParchmentArtifact())),
getProject().provider(() -> "p_"),
neoformRuntime.getSourceJarTask().flatMap(WithOutput::getOutput).map(RegularFile::getAsFile),
true,
spec,
workingDirectory,
null
);
} else {
patchBase = neoformRuntime.getSourceJarTask();
}

final TaskProvider<ApplyPatches> patchApply = spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "applyPatches"), ApplyPatches.class, task -> {
task.getBase().set(neoformRuntime.getSourceJarTask().flatMap(ArtifactProvider::getOutput));
task.getBase().set(patchBase.flatMap(WithOutput::getOutput));
task.getPatches().set(spec.getPatchesDirectory());
task.getRejects().set(spec.getRejectsDirectory());
task.getPatchMode().set(spec.isUpdating() ? PatchMode.FUZZY : PatchMode.ACCESS);
task.getShouldFailOnPatchFailure().set(!spec.isUpdating());
configureCommonRuntimeTaskParameters(task, "applyPatches", spec, workingDirectory);
});

final TaskProvider<ArtifactProvider> sourcesProvider = spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "sourceFromAppliedPatches"), ArtifactProvider.class, task -> {
task.getInputFiles().from(patchApply.flatMap(ApplyPatches::getOutput));
task.getInputFiles().from(patchApply.flatMap(WithOutput::getOutput));
task.getOutput().set(new File(workingDirectory, "patched.jar"));
});

return new RuntimeDevRuntimeDefinition(
spec,
neoformRuntime,
sourcesProvider
);
sourcesProvider,
patchBase);
}

@Override
protected RuntimeDevRuntimeSpecification.Builder createBuilder() {
return RuntimeDevRuntimeSpecification.Builder.from(getProject());
}

public static TaskProvider<DefaultExecute> applyParchment(Project project,
String name,
Provider<File> mappingsFile,
Provider<String> conflictPrefix,
Provider<File> input,
boolean inputFile,
CommonRuntimeSpecification spec,
File workingDirectory,
@Nullable TaskProvider<? extends WithOutput> extraClasspath) {
return project.getTasks().register(CommonRuntimeUtils.buildTaskName(spec, name), DefaultExecute.class, task -> {
File toolExecutable = ToolUtilities.resolveTool(project, project.getExtensions().getByType(Subsystems.class).getTools().getJST().get());

task.getArguments().putFile("mappings", mappingsFile);
if (inputFile) {
task.getArguments().putFile("input", input);
} else {
task.getArguments().putDirectoryFile("input", input);
}

task.getExecutingJar().set(toolExecutable);
task.getProgramArguments().add("--enable-parchment");
task.getProgramArguments().add("--no-parchment-javadoc");
task.getProgramArguments().add("--parchment-mappings");
task.getProgramArguments().add("{mappings}");
task.getProgramArguments().add("--in-format=" + (inputFile ? "archive" : "folder"));
task.getProgramArguments().add("--out-format=archive");
if (conflictPrefix.isPresent() && !conflictPrefix.get().isBlank()) {
task.getProgramArguments().add("--parchment-conflict-prefix=%s".formatted(conflictPrefix.get()));
}
final StringBuilder builder = new StringBuilder();
project.getExtensions().getByType(SourceSetContainer.class).getByName("main").getCompileClasspath().forEach(f -> {
if (!builder.isEmpty()) {
builder.append(File.pathSeparator);
}
builder.append(f.getAbsolutePath());
});
if (extraClasspath != null) {
builder.append(File.pathSeparator).append(extraClasspath.get().getOutput().get().getAsFile().getAbsolutePath());
task.dependsOn(extraClasspath);
}
task.getProgramArguments().add("--classpath=" + builder);

task.getLogLevel().set(ExecuteSpecification.LogLevel.DISABLED);
task.getProgramArguments().add("{input}");
task.getProgramArguments().add("{output}");

CommonRuntimeExtension.configureCommonRuntimeTaskParameters(task, name, spec, workingDirectory);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.gradle.api.file.Directory;
import org.gradle.api.provider.Provider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

Expand All @@ -24,14 +25,16 @@ public final class RuntimeDevRuntimeSpecification extends CommonRuntimeSpecifica
private final Directory patchesDirectory;
private final Directory rejectsDirectory;
private final boolean isUpdating;
private final String parchmentArtifact;

public RuntimeDevRuntimeSpecification(NeoFormRuntimeDefinition neoFormRuntime,
Multimap<String, TaskTreeAdapter> preTaskTypeAdapters,
Multimap<String, TaskTreeAdapter> postTypeAdapters,
Multimap<String, TaskCustomizer<? extends Task>> taskCustomizers,
Directory patchesDirectory,
Directory rejectsDirectory,
boolean isUpdating) {
boolean isUpdating,
String parchmentArtifact) {
super(neoFormRuntime.getSpecification().getProject(),
"platform",
neoFormRuntime.getSpecification().getVersion(),
Expand All @@ -44,6 +47,7 @@ public RuntimeDevRuntimeSpecification(NeoFormRuntimeDefinition neoFormRuntime,
this.patchesDirectory = patchesDirectory;
this.rejectsDirectory = rejectsDirectory;
this.isUpdating = isUpdating;
this.parchmentArtifact = parchmentArtifact;
}

public NeoFormRuntimeDefinition getNeoFormRuntime() {
Expand Down Expand Up @@ -72,6 +76,11 @@ public Directory getRejectsDirectory() {
}
return rejectsDirectory;
}

@Nullable
public String getParchmentArtifact() {
return parchmentArtifact;
}

public boolean isUpdating() {
return isUpdating;
Expand All @@ -97,6 +106,7 @@ public static final class Builder extends CommonRuntimeSpecification.Builder<Run
private Provider<Directory> patchesDirectory;
private Provider<Directory> rejectsDirectory;
private Provider<Boolean> isUpdating;
private Provider<String> parchment;

private Builder(Project project) {
super(project);
Expand Down Expand Up @@ -155,6 +165,11 @@ public Builder isUpdating(final Boolean isUpdating) {
return isUpdating(project.provider(() -> isUpdating));
}

public Builder withParchment(final Provider<String> parchment) {
this.parchment = parchment;
return getThis();
}

public @NotNull RuntimeDevRuntimeSpecification build() {
if (neoFormRuntimeDefinition == null) {
throw new IllegalStateException("Setting a neoFormRuntimeDefinition is required");
Expand All @@ -177,7 +192,7 @@ public Builder isUpdating(final Boolean isUpdating) {
taskCustomizers,
patchesDirectory.get(),
rejectsDirectory.get(),
isUpdating.get());
isUpdating.get(), parchment.getOrNull());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.neoforged.gradle.platform.tasks;

import net.minecraftforge.srgutils.IMappingBuilder;
import net.minecraftforge.srgutils.IMappingFile;
import net.neoforged.gradle.common.runtime.tasks.DefaultRuntime;
import net.neoforged.gradle.dsl.common.tasks.WithOutput;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;

import javax.inject.Inject;
import java.io.IOException;

@CacheableTask
public abstract class OfficialMappingsJustParameters extends DefaultRuntime implements WithOutput {
@Inject
public OfficialMappingsJustParameters() {
getOutputFileName().set("output.tsrg");
}

@InputFile
@PathSensitive(PathSensitivity.NONE)
public abstract RegularFileProperty getInput();

@TaskAction
void exec() throws IOException {
var source = IMappingFile.load(getInput().getAsFile().get());
var builder = IMappingBuilder.create();
source.getClasses().forEach(cls -> {
var c = builder.addClass(cls.getMapped(), cls.getMapped());
cls.getMethods().forEach(mtd -> {
if (mtd.getParameters().isEmpty()) return;

var m = c.method(mtd.getMappedDescriptor(), mtd.getMapped(), mtd.getMapped());
mtd.getParameters().forEach(par -> m.parameter(par.getIndex(), par.getOriginal(), par.getMapped()));
});
});
builder.build().write(getOutput().get().getAsFile().toPath(), IMappingFile.Format.TSRG2);
}
}
Loading