-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathMinecraftLibraryProvider.java
More file actions
200 lines (165 loc) · 8.79 KB
/
MinecraftLibraryProvider.java
File metadata and controls
200 lines (165 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2018-2023 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.fabricmc.loom.configuration.providers.minecraft;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.gradle.api.JavaVersion;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ExternalModuleDependency;
import org.gradle.api.artifacts.ModuleDependency;
import org.gradle.api.provider.Provider;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.configuration.providers.BundleMetadata;
import net.fabricmc.loom.configuration.providers.minecraft.library.Library;
import net.fabricmc.loom.configuration.providers.minecraft.library.LibraryContext;
import net.fabricmc.loom.configuration.providers.minecraft.library.LibraryProcessorManager;
import net.fabricmc.loom.configuration.providers.minecraft.library.MinecraftLibraryHelper;
import net.fabricmc.loom.configuration.providers.minecraft.library.processors.RuntimeLog4jLibraryProcessor;
import net.fabricmc.loom.configuration.providers.minecraft.library.processors.RuntimeLwjglGraphicsLibraryProcessor;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.Platform;
import net.fabricmc.loom.util.gradle.GradleUtils;
public class MinecraftLibraryProvider {
private static final Platform platform = Platform.CURRENT;
private final Project project;
private final MinecraftProvider minecraftProvider;
private final LibraryProcessorManager processorManager;
public MinecraftLibraryProvider(MinecraftProvider minecraftProvider, Project project) {
this.project = project;
this.minecraftProvider = minecraftProvider;
this.processorManager = new LibraryProcessorManager(platform, project.getRepositories(), LoomGradleExtension.get(project).getLibraryProcessors().get(), getEnabledProcessors());
}
private List<String> getEnabledProcessors() {
final LoomGradleExtension extension = LoomGradleExtension.get(project);
var enabledProcessors = new ArrayList<String>();
if (extension.getRuntimeOnlyLog4j().get()) {
enabledProcessors.add(RuntimeLog4jLibraryProcessor.class.getSimpleName());
}
if (extension.getRuntimeOnlyLwjglGraphics().get()) {
enabledProcessors.add(RuntimeLwjglGraphicsLibraryProcessor.class.getSimpleName());
}
final Provider<String> libraryProcessorsProperty = project.getProviders().gradleProperty(Constants.Properties.LIBRARY_PROCESSORS);
if (libraryProcessorsProperty.isPresent()) {
String[] split = libraryProcessorsProperty.get().split(":");
enabledProcessors.addAll(Arrays.asList(split));
}
return Collections.unmodifiableList(enabledProcessors);
}
public void provide() {
final LoomGradleExtension extension = LoomGradleExtension.get(project);
final MinecraftJarConfiguration jarConfiguration = extension.getMinecraftJarConfiguration().get();
final boolean provideClient = jarConfiguration.supportedEnvironments().contains("client");
final boolean provideServer = jarConfiguration.supportedEnvironments().contains("server");
assert provideClient || provideServer;
if (provideClient) {
provideClientLibraries();
}
if (provideServer) {
provideServerLibraries();
}
if (extension.isCollectingDependencyVerificationMetadata()) {
resolveAllLibraries();
}
}
private void provideClientLibraries() {
final List<Library> libraries = MinecraftLibraryHelper.getLibrariesForPlatform(minecraftProvider.getVersionInfo(), platform);
final List<Library> processLibraries = processLibraries(libraries);
processLibraries.forEach(this::applyClientLibrary);
// After Minecraft 1.19-pre1 the natives should be on the runtime classpath.
if (!minecraftProvider.getVersionInfo().hasNativesToExtract()) {
project.getConfigurations().named(Constants.Configurations.MINECRAFT_RUNTIME_LIBRARIES, configuration -> configuration.extendsFrom(project.getConfigurations().getByName(Constants.Configurations.MINECRAFT_NATIVES)));
}
}
private void provideServerLibraries() {
final BundleMetadata serverBundleMetadata = minecraftProvider.getServerBundleMetadata();
final List<Library> libraries = serverBundleMetadata != null ? MinecraftLibraryHelper.getServerLibraries(serverBundleMetadata) : Collections.emptyList();
final List<Library> processLibraries = processLibraries(libraries);
processLibraries.forEach(this::applyServerLibrary);
}
/**
* When Gradle is writing dependency verification metadata, we need to resolve all libraries across all platforms,
* to ensure that they are captured.
*/
private void resolveAllLibraries() {
project.getLogger().info("Resolving all libraries for dependency verification metadata generation");
final List<Library> libraries = MinecraftLibraryHelper.getAllLibraries(minecraftProvider.getVersionInfo());
Configuration detachedConfiguration = project.getConfigurations().detachedConfiguration(
libraries.stream()
.map(library -> project.getDependencies().create(library.mavenNotation()))
.toArray(Dependency[]::new)
);
detachedConfiguration.getFiles();
}
private List<Library> processLibraries(List<Library> libraries) {
final LibraryContext libraryContext = new LibraryContext(minecraftProvider.getVersionInfo(), getTargetRuntimeJavaVersion());
return processorManager.processLibraries(libraries, libraryContext);
}
private JavaVersion getTargetRuntimeJavaVersion() {
final Object property = GradleUtils.getProperty(project, Constants.Properties.RUNTIME_JAVA_COMPATIBILITY_VERSION);
if (property != null) {
// This is very much a last ditch effort to allow users to set the runtime java version
// It's not recommended and will likely cause support confusion if it has been changed without good reason.
project.getLogger().warn("Runtime java compatibility version has manually been set to: %s".formatted(property));
return JavaVersion.toVersion(property);
}
return JavaVersion.current();
}
private void applyClientLibrary(Library library) {
switch (library.target()) {
case COMPILE -> addLibrary(Constants.Configurations.MINECRAFT_CLIENT_COMPILE_LIBRARIES, library);
case RUNTIME -> addLibrary(Constants.Configurations.MINECRAFT_CLIENT_RUNTIME_LIBRARIES, library);
case NATIVES -> addLibrary(Constants.Configurations.MINECRAFT_NATIVES, library);
case LOCAL_MOD -> applyLocalModLibrary(library);
}
}
private void applyServerLibrary(Library library) {
switch (library.target()) {
case COMPILE -> addLibrary(Constants.Configurations.MINECRAFT_SERVER_COMPILE_LIBRARIES, library);
case RUNTIME -> addLibrary(Constants.Configurations.MINECRAFT_SERVER_RUNTIME_LIBRARIES, library);
case LOCAL_MOD -> applyLocalModLibrary(library);
default -> throw new IllegalStateException("Target not supported for server library: %s".formatted(library));
}
}
private void applyLocalModLibrary(Library library) {
ExternalModuleDependency dependency = (ExternalModuleDependency) project.getDependencies().create(library.mavenNotation());
dependency.setTransitive(false);
String configuration = LoomGradleExtension.get(project).disableObfuscation() ? Constants.Configurations.LOCAL_RUNTIME : "modLocalRuntime";
project.getDependencies().add(configuration, dependency);
}
private void addLibrary(String configuration, Library library) {
addDependency(configuration, library.mavenNotation());
}
private void addDependency(String configuration, Object dependency) {
final Dependency created = project.getDependencies().add(configuration, dependency);
// The launcher doesn't download transitive deps, so neither will we.
// This will also prevent a LaunchWrapper library dependency from pulling in outdated ASM jars.
if (created instanceof ModuleDependency md) {
md.setTransitive(false);
}
}
}