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

Transform decompile step into a task that is customizable via specific settings #46

Merged
merged 5 commits into from
Dec 1, 2023
Merged
Changes from 1 commit
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
Next Next commit
Transform decompile step into a task that is customizable via specifi…
…c settings.
shartte committed Nov 30, 2023
commit 0ccc934f784f4fa557cae5b72b74bd48007b74ce
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import net.neoforged.gradle.common.extensions.dependency.creation.ProjectBasedDependencyCreator;
import net.neoforged.gradle.common.extensions.dependency.replacement.DependencyReplacementsExtension;
import net.neoforged.gradle.common.extensions.repository.IvyDummyRepositoryExtension;
import net.neoforged.gradle.common.extensions.subsystems.SubsystemsExtension;
import net.neoforged.gradle.common.runs.ide.IdeRunIntegrationManager;
import net.neoforged.gradle.common.runs.run.RunImpl;
import net.neoforged.gradle.common.runtime.definition.CommonRuntimeDefinition;
@@ -20,6 +21,7 @@
import net.neoforged.gradle.dsl.common.extensions.*;
import net.neoforged.gradle.dsl.common.extensions.dependency.replacement.DependencyReplacement;
import net.neoforged.gradle.dsl.common.extensions.repository.Repository;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Subsystems;
import net.neoforged.gradle.dsl.common.runs.run.Run;
import net.neoforged.gradle.dsl.common.runs.type.RunType;
import net.neoforged.gradle.dsl.common.util.NamingConstants;
@@ -74,6 +76,7 @@ public void apply(Project project) {

extensionManager.registerExtension("minecraft", Minecraft.class, (p) -> p.getObjects().newInstance(MinecraftExtension.class, p));
extensionManager.registerExtension("mappings", Mappings.class, (p) -> p.getObjects().newInstance(MappingsExtension.class, p));
extensionManager.registerExtension("subsystems", Subsystems.class, (p) -> p.getObjects().newInstance(SubsystemsExtension.class, p));

OfficialNamingChannelConfigurator.getInstance().configure(project);

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.neoforged.gradle.common.extensions.subsystems;

import net.minecraftforge.gdi.ConfigurableDSLElement;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Subsystems;
import org.gradle.api.Project;

import javax.inject.Inject;

public abstract class SubsystemsExtension implements ConfigurableDSLElement<Subsystems>, Subsystems {
private final Project project;

@Inject
public SubsystemsExtension(Project project) {
this.project = project;
}

@Override
public Project getProject() {
return project;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net.neoforged.gradle.dsl.common.extensions.subsystems

import groovy.transform.CompileStatic
import net.minecraftforge.gdi.ConfigurableDSLElement
import net.minecraftforge.gdi.annotations.DSLProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional

/**
* Allows configuration of the decompiler used by NeoGradle.
*/
@CompileStatic
interface Decompiler extends ConfigurableDSLElement<Decompiler> {

/**
* Allows the maximum memory provided to the decompiler to be overridden. Must be specified
* in the "123g" or "123m" form.
*/
@Input
@Optional
@DSLProperty
Property<String> getMaxMemory();

/**
* Allows the maximum number of threads used by the decompiler to be constrained. By default, it will
* use all available threads.
*/
@Input
@Optional
@DSLProperty
Property<Integer> getMaxThreads();

/**
* The log-level to use for the decompiler. Supported values: info, debug, warn, error.
shartte marked this conversation as resolved.
Show resolved Hide resolved
* Defaults to info.
*/
@Input
@Optional
@DSLProperty
Property<String> getLogLevel();
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved

/**
* Allows additional JVM arguments to be added to the decompiler invocation.
*/
@Input
@Optional
@DSLProperty
ListProperty<String> getJvmArgs();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.neoforged.gradle.dsl.common.extensions.subsystems

import groovy.transform.CompileStatic
import net.minecraftforge.gdi.BaseDSLElement
import net.minecraftforge.gdi.annotations.DSLProperty
import org.gradle.api.tasks.Nested

/**
* Allows configuration of various NeoGradle subsystems.
*/
@CompileStatic
interface Subsystems extends BaseDSLElement<Subsystems> {

/**
* @return Settings for the decompiler subsystem.
shartte marked this conversation as resolved.
Show resolved Hide resolved
*/
@Nested
@DSLProperty
Decompiler getDecompiler();

}
Original file line number Diff line number Diff line change
@@ -129,8 +129,6 @@ interface Execute extends WithWorkspace, WithOutput, WithJavaVersion, ExecuteSpe
java.setWorkingDir(me.getOutputDirectory().get())
java.getMainClass().set(mainClass)
java.setStandardOutput(log_out)

java.setMaxHeapSize(String.format("%dm", Runtime.getRuntime().maxMemory().intdiv(1024 * 1024)))
}).rethrowFailure().assertNormalExitValue()
}
}
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraftforge.gdi.ConfigurableDSLElement;
import net.neoforged.gradle.common.runtime.extensions.CommonRuntimeExtension;
import net.neoforged.gradle.common.runtime.tasks.Execute;
import net.neoforged.gradle.common.runtime.tasks.ListLibraries;
@@ -11,14 +12,20 @@
import net.neoforged.gradle.dsl.common.extensions.Mappings;
import net.neoforged.gradle.dsl.common.extensions.Minecraft;
import net.neoforged.gradle.dsl.common.extensions.MinecraftArtifactCache;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Decompiler;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Subsystems;
import net.neoforged.gradle.dsl.common.runtime.naming.TaskBuildingContext;
import net.neoforged.gradle.dsl.common.runtime.tasks.Runtime;
import net.neoforged.gradle.dsl.common.runtime.tasks.RuntimeArguments;
import net.neoforged.gradle.dsl.common.runtime.tasks.tree.TaskTreeAdapter;
import net.neoforged.gradle.dsl.common.tasks.ArtifactProvider;
import net.neoforged.gradle.dsl.common.tasks.WithOutput;
import net.neoforged.gradle.dsl.common.tasks.specifications.OutputSpecification;
import net.neoforged.gradle.dsl.common.util.*;
import net.neoforged.gradle.dsl.common.util.CommonRuntimeUtils;
import net.neoforged.gradle.dsl.common.util.ConfigurationUtils;
import net.neoforged.gradle.dsl.common.util.DistributionType;
import net.neoforged.gradle.dsl.common.util.GameArtifact;
import net.neoforged.gradle.dsl.common.util.NamingConstants;
import net.neoforged.gradle.dsl.neoform.configuration.NeoFormConfigConfigurationSpecV1;
import net.neoforged.gradle.dsl.neoform.configuration.NeoFormConfigConfigurationSpecV2;
import net.neoforged.gradle.neoform.runtime.definition.NeoFormRuntimeDefinition;
@@ -43,11 +50,17 @@
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@SuppressWarnings({"OptionalUsedAsFieldOrParameterType", "unused"}) // API Design
public abstract class NeoFormRuntimeExtension extends CommonRuntimeExtension<NeoFormRuntimeSpecification, NeoFormRuntimeSpecification.Builder, NeoFormRuntimeDefinition> {
public abstract class NeoFormRuntimeExtension extends CommonRuntimeExtension<NeoFormRuntimeSpecification, NeoFormRuntimeSpecification.Builder, NeoFormRuntimeDefinition> implements ConfigurableDSLElement<NeoFormRuntimeExtension> {

@javax.inject.Inject
public NeoFormRuntimeExtension(Project project) {
@@ -67,6 +80,8 @@ private static void configureMcpRuntimeTaskWithDefaults(NeoFormRuntimeSpecificat
@Nullable
private static TaskProvider<? extends WithOutput> createBuiltIn(final NeoFormRuntimeSpecification spec, NeoFormConfigConfigurationSpecV2 neoFormConfigV2, NeoFormConfigConfigurationSpecV1.Step step, final Map<String, TaskProvider<? extends WithOutput>> tasks, final Map<GameArtifact, TaskProvider<? extends WithOutput>> gameArtifactTaskProviders, final Optional<TaskProvider<? extends WithOutput>> adaptedInput) {
switch (step.getType()) {
case "decompile":
return createDecompile(spec, step, neoFormConfigV2);
case "downloadManifest":
return gameArtifactTaskProviders.computeIfAbsent(GameArtifact.LAUNCHER_MANIFEST, a -> {
throw new IllegalStateException("Launcher Manifest is required for this step, but was not provided");
@@ -117,7 +132,48 @@ private static TaskProvider<? extends WithOutput> createBuiltIn(final NeoFormRun
return null;
}

private static TaskProvider<? extends Runtime> createExecute(final NeoFormRuntimeSpecification spec, final NeoFormConfigConfigurationSpecV1.Step step, final NeoFormConfigConfigurationSpecV1.Function function) {
@NotNull
private static TaskProvider<? extends Runtime> createDecompile(NeoFormRuntimeSpecification spec, NeoFormConfigConfigurationSpecV1.Step step, NeoFormConfigConfigurationSpecV2 neoFormConfig) {
NeoFormConfigConfigurationSpecV1.Function function = neoFormConfig.getFunction(step.getType());
if (function == null) {
throw new IllegalArgumentException(String.format("Invalid NeoForm Config, Unknown function step type: %s File: %s", step.getType(), neoFormConfig));
}

// Filter out decompiler arguments that aren't related to its output (log-level and thread-count)
List<String> decompilerArgs = new ArrayList<>(function.getArgs());
decompilerArgs.removeIf(arg -> arg.startsWith("-log=") || arg.startsWith("-thr="));

// Retrieve the default memory size from the JVM arguments configured in NeoForm
String defaultMaxMemory = "4g";
List<String> jvmArgs = new ArrayList<>(function.getJvmArgs());
for (int i = jvmArgs.size() - 1; i >= 0; i--) {
if (jvmArgs.get(i).startsWith("-Xmx")) {
defaultMaxMemory = jvmArgs.get(i).substring("-Xmx".length());
jvmArgs.remove(i);
}
}

// Consider user-settings
Decompiler settings = spec.getProject().getExtensions().getByType(Subsystems.class).getDecompiler();
String maxMemory = settings.getMaxMemory().getOrElse(defaultMaxMemory);
int maxThreads = settings.getMaxThreads().getOrElse(0);
String logLevel = settings.getLogLevel().getOrElse("INFO");

jvmArgs.addAll(settings.getJvmArgs().get());
jvmArgs.add("-Xmx" + maxMemory);
if (maxThreads > 0) {
decompilerArgs.add(0, "-thr=" + maxThreads);
}
decompilerArgs.add(0, "-log=" + logLevel);

return spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, step.getName()), Execute.class, task -> {
task.getExecutingJar().set(ToolUtilities.resolveTool(task.getProject(), function.getVersion()));
task.getJvmArguments().addAll(jvmArgs);
task.getProgramArguments().addAll(decompilerArgs);
});
}

private TaskProvider<? extends Runtime> createExecute(final NeoFormRuntimeSpecification spec, final NeoFormConfigConfigurationSpecV1.Step step, final NeoFormConfigConfigurationSpecV1.Function function) {
return spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, step.getName()), Execute.class, task -> {
task.getExecutingJar().set(ToolUtilities.resolveTool(task.getProject(), function.getVersion()));
task.getJvmArguments().addAll(function.getJvmArgs());