From 03d3950d119830edb65d35bafb90c330aa3a78d1 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Thu, 30 Dec 2021 00:15:18 +0000 Subject: [PATCH] Exclude client only libraries in server run task. --- .../loom/configuration/ide/RunConfig.java | 33 ++++++++++++++++++ .../configuration/ide/idea/IdeaSyncTask.java | 34 +++---------------- .../fabricmc/loom/task/AbstractRunTask.java | 21 +++++++++++- .../projects/runconfigs/build.gradle | 6 ++-- 4 files changed, 60 insertions(+), 34 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/configuration/ide/RunConfig.java b/src/main/java/net/fabricmc/loom/configuration/ide/RunConfig.java index 3706900..7979b1b 100644 --- a/src/main/java/net/fabricmc/loom/configuration/ide/RunConfig.java +++ b/src/main/java/net/fabricmc/loom/configuration/ide/RunConfig.java @@ -24,14 +24,18 @@ package net.fabricmc.loom.configuration.ide; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; import com.google.common.collect.ImmutableMap; import com.google.gson.JsonElement; @@ -46,6 +50,8 @@ import org.w3c.dom.Node; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.configuration.InstallerData; import net.fabricmc.loom.configuration.ide.idea.IdeaSyncTask; +import net.fabricmc.loom.configuration.providers.BundleMetadata; +import net.fabricmc.loom.util.Constants; public class RunConfig { public String configName; @@ -54,6 +60,7 @@ public class RunConfig { public String mainClass; public String runDirIdeaUrl; public String runDir; + public String environment; public List vmArgs = new ArrayList<>(); public List programArgs = new ArrayList<>(); public SourceSet sourceSet; @@ -163,6 +170,7 @@ public class RunConfig { runConfig.runDirIdeaUrl = "file://$PROJECT_DIR$/" + runDir; runConfig.runDir = runDir; runConfig.sourceSet = sourceSet; + runConfig.environment = environment; // Custom parameters runConfig.programArgs.addAll(settings.getProgramArgs()); @@ -251,6 +259,31 @@ public class RunConfig { return defaultMainClass; } + public List getExcludedLibraryPaths(Project project) { + if (!environment.equals("server")) { + return Collections.emptyList(); + } + + final BundleMetadata bundleMetadata = LoomGradleExtension.get(project).getMinecraftProvider().getServerBundleMetadata(); + + if (bundleMetadata == null) { + // Legacy version + return Collections.emptyList(); + } + + final Set allLibraries = project.getConfigurations().getByName(Constants.Configurations.MINECRAFT_DEPENDENCIES).getFiles(); + final Set serverLibraries = project.getConfigurations().getByName(Constants.Configurations.MINECRAFT_SERVER_DEPENDENCIES).getFiles(); + final List clientOnlyLibraries = new LinkedList<>(); + + for (File commonLibrary : allLibraries) { + if (!serverLibraries.contains(commonLibrary)) { + clientOnlyLibraries.add(commonLibrary.getAbsolutePath()); + } + } + + return clientOnlyLibraries; + } + private static String encodeEscaped(String s) { StringBuilder ret = new StringBuilder(); diff --git a/src/main/java/net/fabricmc/loom/configuration/ide/idea/IdeaSyncTask.java b/src/main/java/net/fabricmc/loom/configuration/ide/idea/IdeaSyncTask.java index ead3874..b2b2369 100644 --- a/src/main/java/net/fabricmc/loom/configuration/ide/idea/IdeaSyncTask.java +++ b/src/main/java/net/fabricmc/loom/configuration/ide/idea/IdeaSyncTask.java @@ -31,10 +31,7 @@ import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Collections; -import java.util.LinkedList; import java.util.List; -import java.util.Set; import javax.inject.Inject; import javax.xml.parsers.DocumentBuilder; @@ -57,9 +54,7 @@ import org.xml.sax.InputSource; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.configuration.ide.RunConfig; import net.fabricmc.loom.configuration.ide.RunConfigSettings; -import net.fabricmc.loom.configuration.providers.BundleMetadata; import net.fabricmc.loom.task.AbstractLoomTask; -import net.fabricmc.loom.util.Constants; public abstract class IdeaSyncTask extends AbstractLoomTask { @Inject @@ -90,8 +85,6 @@ public abstract class IdeaSyncTask extends AbstractLoomTask { runConfigsDir.mkdirs(); } - final List excludedServerLibraries = getExcludedServerLibraries(); - for (RunConfigSettings settings : extension.getRunConfigs()) { if (!settings.isIdeConfigGenerated()) { continue; @@ -109,9 +102,11 @@ public abstract class IdeaSyncTask extends AbstractLoomTask { settings.makeRunDir(); - if (settings.getEnvironment().equals("server") && !excludedServerLibraries.isEmpty()) { + final List excludedLibraryPaths = config.getExcludedLibraryPaths(getProject()); + + if (!excludedLibraryPaths.isEmpty()) { try { - setClasspathModifications(runConfigs.toPath(), excludedServerLibraries); + setClasspathModifications(runConfigs.toPath(), excludedLibraryPaths); } catch (Exception e) { getProject().getLogger().error("Failed to modify run configuration xml", e); } @@ -119,27 +114,6 @@ public abstract class IdeaSyncTask extends AbstractLoomTask { } } - private List getExcludedServerLibraries() { - final BundleMetadata bundleMetadata = getExtension().getMinecraftProvider().getServerBundleMetadata(); - - if (bundleMetadata == null) { - // Legacy version - return Collections.emptyList(); - } - - final Set allLibraries = getProject().getConfigurations().getByName(Constants.Configurations.MINECRAFT_DEPENDENCIES).getFiles(); - final Set serverLibraries = getProject().getConfigurations().getByName(Constants.Configurations.MINECRAFT_SERVER_DEPENDENCIES).getFiles(); - final List clientOnlyLibraries = new LinkedList<>(); - - for (File commonLibrary : allLibraries) { - if (!serverLibraries.contains(commonLibrary)) { - clientOnlyLibraries.add(commonLibrary.getAbsolutePath()); - } - } - - return clientOnlyLibraries; - } - private void setClasspathModifications(Path runConfig, List exclusions) throws IOException { if (!IdeaUtils.supportsCustomizableClasspath()) { return; diff --git a/src/main/java/net/fabricmc/loom/task/AbstractRunTask.java b/src/main/java/net/fabricmc/loom/task/AbstractRunTask.java index baa5c93..d88633f 100644 --- a/src/main/java/net/fabricmc/loom/task/AbstractRunTask.java +++ b/src/main/java/net/fabricmc/loom/task/AbstractRunTask.java @@ -31,6 +31,7 @@ import java.util.List; import java.util.function.Function; import org.gradle.api.Project; +import org.gradle.api.specs.Spec; import org.gradle.api.tasks.JavaExec; import net.fabricmc.loom.configuration.ide.RunConfig; @@ -44,7 +45,7 @@ public abstract class AbstractRunTask extends JavaExec { setGroup(Constants.TaskGroup.FABRIC); this.config = configProvider.apply(getProject()); - setClasspath(config.sourceSet.getRuntimeClasspath()); + setClasspath(config.sourceSet.getRuntimeClasspath().filter(new LibraryFilter())); args(config.programArgs); getMainClass().set(config.mainClass); } @@ -72,4 +73,22 @@ public abstract class AbstractRunTask extends JavaExec { args.addAll(config.vmArgs); return args; } + + private class LibraryFilter implements Spec { + private List excludedLibraryPaths = null; + + @Override + public boolean isSatisfiedBy(File element) { + if (excludedLibraryPaths == null) { + excludedLibraryPaths = config.getExcludedLibraryPaths(getProject()); + } + + if (excludedLibraryPaths.contains(element.getAbsolutePath())) { + getProject().getLogger().debug("Excluding library {} from {} run config", element.getName(), config.configName); + return false; + } + + return true; + } + } } diff --git a/src/test/resources/projects/runconfigs/build.gradle b/src/test/resources/projects/runconfigs/build.gradle index 9ed945c..a8e1f6b 100644 --- a/src/test/resources/projects/runconfigs/build.gradle +++ b/src/test/resources/projects/runconfigs/build.gradle @@ -31,7 +31,7 @@ archivesBaseName = "fabric-example-mod" version = "1.0.0" dependencies { - minecraft "com.mojang:minecraft:1.16.5" - mappings "net.fabricmc:yarn:1.16.5+build.5:v2" - modImplementation "net.fabricmc:fabric-loader:0.11.2" + minecraft "com.mojang:minecraft:1.18.1" + mappings "net.fabricmc:yarn:1.18.1+build.12:v2" + modImplementation "net.fabricmc:fabric-loader:0.12.12" } \ No newline at end of file