From 894ba0f66a56f9336e246bce8c22b51ab7be4bcd Mon Sep 17 00:00:00 2001 From: Yeregorix Date: Tue, 10 Oct 2023 18:38:22 +0200 Subject: [PATCH] Package services, lang and mod into a single jar --- forge/build.gradle.kts | 125 ++++++++++++------ .../loading/moddiscovery/ForgeBootstrap.java | 103 --------------- .../moddiscovery/SpongeForgeModLocator.java | 87 ++++++++++++ ... => SpongeForgeTransformationService.java} | 17 +-- ...ods.modlauncher.api.ITransformationService | 2 +- ...necraftforge.forgespi.locating.IModLocator | 2 +- 6 files changed, 175 insertions(+), 161 deletions(-) delete mode 100644 forge/src/applaunch/java/org/spongepowered/forge/applaunch/loading/moddiscovery/ForgeBootstrap.java create mode 100644 forge/src/applaunch/java/org/spongepowered/forge/applaunch/loading/moddiscovery/SpongeForgeModLocator.java rename forge/src/applaunch/java/org/spongepowered/forge/applaunch/service/{ForgeProductionBootstrap.java => SpongeForgeTransformationService.java} (82%) diff --git a/forge/build.gradle.kts b/forge/build.gradle.kts index 94d15ac3980..eea8c258f30 100644 --- a/forge/build.gradle.kts +++ b/forge/build.gradle.kts @@ -1,3 +1,4 @@ +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import net.fabricmc.loom.api.LoomGradleExtensionAPI import net.fabricmc.loom.LoomGradleExtension import net.fabricmc.loom.task.RemapJarTask @@ -41,17 +42,21 @@ repositories { } // SpongeForge libraries -val forgeServiceLibrariesConfig: NamedDomainObjectProvider = configurations.register("spongeServiceLibraries") -val forgeGameLibrariesConfig: NamedDomainObjectProvider = configurations.register("spongeGameLibraries") -val forgeGameOnlyLibrariesConfig: NamedDomainObjectProvider = configurations.register("spongeGameOnlyLibraries") +val serviceLibrariesConfig: NamedDomainObjectProvider = configurations.register("spongeServiceLibraries") +val gameLibrariesConfig: NamedDomainObjectProvider = configurations.register("spongeGameLibraries") + +val gameManagedLibrariesConfig: NamedDomainObjectProvider = configurations.register("spongeGameManagedLibraries") + +val serviceShadedLibrariesConfig: NamedDomainObjectProvider = configurations.register("spongeServiceShadedLibraries") +val gameShadedLibrariesConfig: NamedDomainObjectProvider = configurations.register("spongeGameShadedLibraries") configurations.named("forgeRuntimeLibrary") { - extendsFrom(forgeServiceLibrariesConfig.get()) + extendsFrom(serviceLibrariesConfig.get()) } // ModLauncher layers val serviceLayerConfig: NamedDomainObjectProvider = configurations.register("serviceLayer") { - extendsFrom(forgeServiceLibrariesConfig.get()) + extendsFrom(serviceLibrariesConfig.get()) extendsFrom(configurations.getByName("forgeDependencies")) } val langLayerConfig: NamedDomainObjectProvider = configurations.register("langLayer") { @@ -60,7 +65,7 @@ val langLayerConfig: NamedDomainObjectProvider = configurations.r val gameLayerConfig: NamedDomainObjectProvider = configurations.register("gameLayer") { extendsFrom(serviceLayerConfig.get()) extendsFrom(langLayerConfig.get()) - extendsFrom(forgeGameLibrariesConfig.get()) + extendsFrom(gameLibrariesConfig.get()) afterEvaluate { extendsFrom(configurations.getByName("minecraftNamed")) @@ -186,7 +191,8 @@ extensions.configure(LoomGradleExtensionAPI::class) { sourceSet(forgeAccessors) sourceSet(forgeLaunch) - configuration(forgeGameOnlyLibrariesConfig.get()) + configuration(gameManagedLibrariesConfig.get()) + configuration(gameShadedLibrariesConfig.get()) } create("sponge") { @@ -224,10 +230,7 @@ dependencies { forgeMixins.implementationConfigurationName(project(commonProject.path)) - val serviceLibraries = forgeServiceLibrariesConfig.name - val gameLibraries = forgeGameLibrariesConfig.name - val gameOnlyLibraries = forgeGameOnlyLibrariesConfig.name - + val serviceLibraries = serviceLibrariesConfig.name serviceLibraries("org.spongepowered:plugin-spi:$apiPluginSpiVersion") serviceLibraries(project(transformersProject.path)) serviceLibraries(platform("org.spongepowered:configurate-bom:$apiConfigurateVersion")) @@ -242,19 +245,23 @@ dependencies { exclude(group = "org.spongepowered", module = "configurate-core") exclude(group = "org.checkerframework", module = "checker-qual") } - serviceLibraries("org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion") + val gameLibraries = gameLibrariesConfig.name gameLibraries("org.spongepowered:spongeapi:$apiVersion") gameLibraries("javax.inject:javax.inject:1") gameLibraries("com.zaxxer:HikariCP:2.7.8") gameLibraries(platform("net.kyori:adventure-bom:$apiAdventureVersion")) gameLibraries("net.kyori:adventure-serializer-configurate4") - // due to dependency substitution we must add SpongeAPI manually - gameOnlyLibraries("org.spongepowered:spongeapi:$apiVersion") { isTransitive = false } + val serviceShadedLibraries = serviceShadedLibrariesConfig.name + serviceShadedLibraries(project(transformersProject.path)) { isTransitive = false } + + val gameShadedLibraries = gameShadedLibrariesConfig.name + gameShadedLibraries("org.spongepowered:spongeapi:$apiVersion") { isTransitive = false } afterEvaluate { - spongeImpl.copyModulesExcludingProvided(forgeGameLibrariesConfig.get(), serviceLayerConfig.get(), forgeGameOnlyLibrariesConfig.get()) + spongeImpl.copyModulesExcludingProvided(serviceLibrariesConfig.get(), configurations.getByName("forgeDependencies"), serviceShadedLibrariesConfig.get()) + spongeImpl.copyModulesExcludingProvided(gameLibrariesConfig.get(), serviceLayerConfig.get(), gameManagedLibrariesConfig.get()) } testplugins?.also { @@ -308,17 +315,6 @@ tasks { manifest.from(forgeManifest) from(forgeMixins.output) } - - val forgeAppLaunchServicesJar by registering(Jar::class) { - archiveClassifier.set("applaunch-services") - manifest.from(forgeManifest) - - from(commonProject.sourceSets.named("applaunch").map { it.output }) - from(forgeAppLaunch.output) - - duplicatesStrategy = DuplicatesStrategy.WARN - } - val forgeLangJar by registering(Jar::class) { archiveClassifier.set("lang") manifest { @@ -328,9 +324,19 @@ tasks { from(forgeLang.output) } + val forgeServicesDevJar by registering(Jar::class) { + archiveClassifier.set("services-dev") + manifest.from(forgeManifest) + + from(commonProject.sourceSets.named("applaunch").map { it.output }) + from(forgeAppLaunch.output) + + duplicatesStrategy = DuplicatesStrategy.WARN + } + afterEvaluate { withType(net.fabricmc.loom.task.AbstractRunTask::class) { - classpath += files(mods, forgeAppLaunchServicesJar, forgeLangJar, forgeAppLaunch.runtimeClasspath) + classpath += files(mods, forgeServicesDevJar, forgeLangJar) argumentProviders += CommandLineArgumentProvider { mixinConfigs.asSequence() @@ -349,7 +355,8 @@ tasks { val emitDependencies by registering(org.spongepowered.gradle.impl.OutputDependenciesToJson::class) { group = "sponge" - this.dependencies("main", forgeGameOnlyLibrariesConfig) + this.dependencies("main", gameManagedLibrariesConfig) + this.excludedDependencies(gameShadedLibrariesConfig) outputFile.set(installerResources.map { it.file("org/spongepowered/forge/applaunch/loading/moddiscovery/libraries.json") }) } @@ -357,48 +364,78 @@ tasks { dependsOn(emitDependencies) } - shadowJar { + val forgeServicesShadowJar by register("servicesShadowJar", ShadowJar::class) { + group = "shadow" + archiveClassifier.set("services") + mergeServiceFiles() + configurations = listOf(serviceShadedLibrariesConfig.get()) + exclude("META-INF/INDEX.LIST", "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA", "module-info.class") + + manifest { + attributes("Multi-Release" to true) + from(forgeManifest) + } + + from(commonProject.sourceSets.named("applaunch").map { it.output }) + from(forgeAppLaunch.output) + + // Make sure to relocate access widener so that we don't conflict with other coremods + relocate("net.fabricmc.accesswidener", "org.spongepowered.forge.libs.accesswidener") + } + + shadowJar { group = "shadow" + archiveClassifier.set("mod-dev") - configurations = listOf() + mergeServiceFiles() + configurations = listOf(gameShadedLibrariesConfig.get()) - archiveClassifier.set("universal-dev") manifest { - attributes(mapOf( + attributes( "Superclass-Transformer" to "common.superclasschange,forge.superclasschange", - "Multi-Release" to true, "MixinConfigs" to mixinConfigs.joinToString(",") - )) + ) from(forgeManifest) } + from(commonProject.sourceSets.main.map { it.output }) from(commonProject.sourceSets.named("mixins").map {it.output }) from(commonProject.sourceSets.named("accessors").map {it.output }) from(commonProject.sourceSets.named("launch").map {it.output }) - from(transformersProject.sourceSets.named("main").map { it.output }) from(forgeLaunch.output) from(forgeAccessors.output) from(forgeMixins.output) - - // TODO jarjar service and lang - - // Make sure to relocate access widener so that we don't conflict with other - // coremods also using access widener - relocate("net.fabricmc.accesswidener", "org.spongepowered.forge.libs.accesswidener") } val remapShadowJar = register("remapShadowJar", RemapJarTask::class) { group = "loom" + archiveClassifier.set("mod") + inputFile.set(shadowJar.flatMap { it.archiveFile }) - archiveClassifier.set("universal") - dependsOn(shadowJar) atAccessWideners.add("common.accesswidener") } + val universalJar = register("universalJar", Jar::class) { + group = "build" + archiveClassifier.set("universal") + + manifest.from(forgeManifest) + + from(forgeServicesShadowJar.archiveFile.map { zipTree(it) }) + + into("jars") { + from(remapShadowJar) + rename("spongeforge-(.*)-mod.jar", "spongeforge-mod.jar") + + from(forgeLangJar) + rename("spongeforge-(.*)-lang.jar", "spongeforge-lang.jar") + } + } + assemble { - dependsOn(remapShadowJar) + dependsOn(universalJar) } templateResources { diff --git a/forge/src/applaunch/java/org/spongepowered/forge/applaunch/loading/moddiscovery/ForgeBootstrap.java b/forge/src/applaunch/java/org/spongepowered/forge/applaunch/loading/moddiscovery/ForgeBootstrap.java deleted file mode 100644 index 7bd3da041c1..00000000000 --- a/forge/src/applaunch/java/org/spongepowered/forge/applaunch/loading/moddiscovery/ForgeBootstrap.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * This file is part of Sponge, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * Copyright (c) contributors - * - * 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 org.spongepowered.forge.applaunch.loading.moddiscovery; - -import cpw.mods.jarhandling.SecureJar; -import cpw.mods.modlauncher.Environment; -import cpw.mods.modlauncher.Launcher; -import net.minecraftforge.fml.loading.FMLEnvironment; -import net.minecraftforge.fml.loading.moddiscovery.AbstractJarFileModProvider; -import net.minecraftforge.forgespi.locating.IModFile; -import net.minecraftforge.forgespi.locating.IModLocator; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.spongepowered.forge.applaunch.loading.moddiscovery.library.LibraryManager; -import org.spongepowered.forge.applaunch.loading.moddiscovery.library.LibraryModFileFactory; -import org.spongepowered.forge.applaunch.loading.moddiscovery.library.LibraryModFileInfoParser; -import org.spongepowered.forge.applaunch.plugin.ForgePluginPlatform; -import org.spongepowered.forge.applaunch.service.ForgeProductionBootstrap; - -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -// works with ForgeProductionBootstrap to make this whole thing go -public final class ForgeBootstrap extends AbstractJarFileModProvider implements IModLocator { - - private static final Logger LOGGER = LogManager.getLogger(); - - private LibraryManager libraryManager; - - private final Set modFiles = ConcurrentHashMap.newKeySet(); - - @Override - public List scanMods() { - final List jars = new ArrayList<>(); - - // Add Sponge-specific libraries - if (FMLEnvironment.production) { - try { - this.libraryManager.validate(); - } catch (final Exception ex) { - throw new RuntimeException("Failed to download and validate Sponge libraries", ex); // todo: more specific? - } - this.libraryManager.finishedProcessing(); - for (final LibraryManager.Library library : this.libraryManager.getAll().values()) { - final Path path = library.getFile(); - ForgeBootstrap.LOGGER.debug("Adding jar {} to classpath as a library", path); - final IModFile file = LibraryModFileFactory.INSTANCE.build(SecureJar.from(path), this, LibraryModFileInfoParser.INSTANCE); - this.modFiles.add(file); - jars.add(new ModFileOrException(file, null)); - } - } - - return jars; - } - - @Override - public String name() { - return "spongeforge"; - } - - @Override - public boolean isValid(final IModFile modFile) { - return this.modFiles.contains(modFile); - } - - @Override - public void initArguments(final Map arguments) { - final Environment env = Launcher.INSTANCE.environment(); - ForgePluginPlatform.bootstrap(env); - this.libraryManager = new LibraryManager( - env.getProperty(ForgeProductionBootstrap.Keys.CHECK_LIBRARY_HASHES.get()).orElse(true), - env.getProperty(ForgeProductionBootstrap.Keys.LIBRARIES_DIRECTORY.get()) - .orElseThrow(() -> new IllegalStateException("no libraries available")), - ForgeBootstrap.class.getResource("libraries.json") - ); - } -} diff --git a/forge/src/applaunch/java/org/spongepowered/forge/applaunch/loading/moddiscovery/SpongeForgeModLocator.java b/forge/src/applaunch/java/org/spongepowered/forge/applaunch/loading/moddiscovery/SpongeForgeModLocator.java new file mode 100644 index 00000000000..1b77edd6235 --- /dev/null +++ b/forge/src/applaunch/java/org/spongepowered/forge/applaunch/loading/moddiscovery/SpongeForgeModLocator.java @@ -0,0 +1,87 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * 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 org.spongepowered.forge.applaunch.loading.moddiscovery; + +import com.google.common.collect.ImmutableMap; +import cpw.mods.modlauncher.Environment; +import cpw.mods.modlauncher.Launcher; +import net.minecraftforge.fml.loading.FMLEnvironment; +import net.minecraftforge.fml.loading.moddiscovery.AbstractJarFileModLocator; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.spongepowered.forge.applaunch.plugin.ForgePluginPlatform; + +import java.net.URI; +import java.net.URL; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Locale; +import java.util.Map; +import java.util.stream.Stream; + +public final class SpongeForgeModLocator extends AbstractJarFileModLocator { + private static final Logger LOGGER = LogManager.getLogger(); + + @Override + public Stream scanCandidates() { + if (!FMLEnvironment.production) { + return Stream.of(); + } + + try { + URL rootJar = SpongeForgeModLocator.class.getProtectionDomain().getCodeSource().getLocation(); + FileSystem fs = FileSystems.getFileSystem(rootJar.toURI()); // FML has already opened a file system for this jar + return Files.list(fs.getPath("jars")) + .filter(path -> path.getFileName().toString().toLowerCase(Locale.ROOT).endsWith(".jar")) + .map(path -> { + try { + URI jij = new URI("jij:" + path.toAbsolutePath().toUri().getRawSchemeSpecificPart()).normalize(); + final Map env = ImmutableMap.of("packagePath", path); + FileSystem jijFS = FileSystems.newFileSystem(jij, env); + return jijFS.getPath("/"); // root of the archive to load + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } catch (Exception e) { + LOGGER.error("Failed to scan mod candidates", e); + } + + return Stream.of(); + } + + @Override + public String name() { + return "spongeforge"; + } + + @Override + public void initArguments(Map arguments) { + final Environment env = Launcher.INSTANCE.environment(); + ForgePluginPlatform.bootstrap(env); + } +} diff --git a/forge/src/applaunch/java/org/spongepowered/forge/applaunch/service/ForgeProductionBootstrap.java b/forge/src/applaunch/java/org/spongepowered/forge/applaunch/service/SpongeForgeTransformationService.java similarity index 82% rename from forge/src/applaunch/java/org/spongepowered/forge/applaunch/service/ForgeProductionBootstrap.java rename to forge/src/applaunch/java/org/spongepowered/forge/applaunch/service/SpongeForgeTransformationService.java index 2e3f7ec00d7..90db7a3c9bd 100644 --- a/forge/src/applaunch/java/org/spongepowered/forge/applaunch/service/ForgeProductionBootstrap.java +++ b/forge/src/applaunch/java/org/spongepowered/forge/applaunch/service/SpongeForgeTransformationService.java @@ -33,7 +33,6 @@ import joptsimple.OptionSpecBuilder; import net.minecraftforge.fml.loading.FMLEnvironment; import org.checkerframework.checker.nullness.qual.NonNull; -import org.spongepowered.transformers.modlauncher.AccessWidenerTransformationService; import org.spongepowered.transformers.modlauncher.SuperclassChanger; import java.nio.file.Path; @@ -43,17 +42,14 @@ import java.util.function.BiFunction; import java.util.function.Supplier; -public class ForgeProductionBootstrap implements ITransformationService { - - public static final String NAME = "spongeforge"; - +public class SpongeForgeTransformationService implements ITransformationService { private OptionSpec checkHashes; private OptionSpec librariesDirectoryName; @NonNull @Override public String name() { - return ForgeProductionBootstrap.NAME; + return "spongeforge"; } @Override @@ -80,13 +76,10 @@ public void argumentValues(final OptionResult option) { @Override public void initialize(final IEnvironment environment) { if (FMLEnvironment.production) { - // Register SF as an AW - // todo: actually read this from the jar manifest - environment.getProperty(AccessWidenerTransformationService.INSTANCE.get()).ifPresent(aWTS -> - aWTS.offerResource(ForgeProductionBootstrap.class.getResource("/common.accesswidener"), "SpongeForge injected")); + // TODO actually read this from the jar manifest environment.getProperty(SuperclassChanger.INSTANCE.get()).ifPresent(scc -> { - scc.offerResource(ForgeProductionBootstrap.class.getResource("/common.superclasschange"), "SpongeForge injected"); - scc.offerResource(ForgeProductionBootstrap.class.getResource("/forge.superclasschange"), "SpongeForge injected"); + scc.offerResource(SpongeForgeTransformationService.class.getResource("/common.superclasschange"), "SpongeForge injected"); + scc.offerResource(SpongeForgeTransformationService.class.getResource("/forge.superclasschange"), "SpongeForge injected"); }); } } diff --git a/forge/src/applaunch/resources/META-INF/services/cpw.mods.modlauncher.api.ITransformationService b/forge/src/applaunch/resources/META-INF/services/cpw.mods.modlauncher.api.ITransformationService index fbe5cafc617..03f805df7ac 100644 --- a/forge/src/applaunch/resources/META-INF/services/cpw.mods.modlauncher.api.ITransformationService +++ b/forge/src/applaunch/resources/META-INF/services/cpw.mods.modlauncher.api.ITransformationService @@ -1 +1 @@ -org.spongepowered.forge.applaunch.service.ForgeProductionBootstrap +org.spongepowered.forge.applaunch.service.SpongeForgeTransformationService diff --git a/forge/src/applaunch/resources/META-INF/services/net.minecraftforge.forgespi.locating.IModLocator b/forge/src/applaunch/resources/META-INF/services/net.minecraftforge.forgespi.locating.IModLocator index c5a33b8529a..209d16f683b 100644 --- a/forge/src/applaunch/resources/META-INF/services/net.minecraftforge.forgespi.locating.IModLocator +++ b/forge/src/applaunch/resources/META-INF/services/net.minecraftforge.forgespi.locating.IModLocator @@ -1,3 +1,3 @@ -org.spongepowered.forge.applaunch.loading.moddiscovery.ForgeBootstrap +org.spongepowered.forge.applaunch.loading.moddiscovery.SpongeForgeModLocator org.spongepowered.forge.applaunch.loading.moddiscovery.locator.ClasspathPluginLocator org.spongepowered.forge.applaunch.loading.moddiscovery.locator.PluginsFolderLocator \ No newline at end of file