Exclude client only libraries in server run task.

dev/0.11
modmuss50 2021-12-30 00:15:18 +00:00
parent 15afb45769
commit 03d3950d11
4 changed files with 60 additions and 34 deletions

View File

@ -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<String> vmArgs = new ArrayList<>();
public List<String> 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<String> 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<File> allLibraries = project.getConfigurations().getByName(Constants.Configurations.MINECRAFT_DEPENDENCIES).getFiles();
final Set<File> serverLibraries = project.getConfigurations().getByName(Constants.Configurations.MINECRAFT_SERVER_DEPENDENCIES).getFiles();
final List<String> 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();

View File

@ -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<String> 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<String> 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<String> getExcludedServerLibraries() {
final BundleMetadata bundleMetadata = getExtension().getMinecraftProvider().getServerBundleMetadata();
if (bundleMetadata == null) {
// Legacy version
return Collections.emptyList();
}
final Set<File> allLibraries = getProject().getConfigurations().getByName(Constants.Configurations.MINECRAFT_DEPENDENCIES).getFiles();
final Set<File> serverLibraries = getProject().getConfigurations().getByName(Constants.Configurations.MINECRAFT_SERVER_DEPENDENCIES).getFiles();
final List<String> clientOnlyLibraries = new LinkedList<>();
for (File commonLibrary : allLibraries) {
if (!serverLibraries.contains(commonLibrary)) {
clientOnlyLibraries.add(commonLibrary.getAbsolutePath());
}
}
return clientOnlyLibraries;
}
private void setClasspathModifications(Path runConfig, List<String> exclusions) throws IOException {
if (!IdeaUtils.supportsCustomizableClasspath()) {
return;

View File

@ -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<File> {
private List<String> 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;
}
}
}

View File

@ -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"
}