diff --git a/forge/src/applaunch/java/org/spongepowered/forge/applaunch/service/SpongeForgeTransformationService.java b/forge/src/applaunch/java/org/spongepowered/forge/applaunch/service/SpongeForgeTransformationService.java index 90db7a3c9bd..dd95ed7a463 100644 --- a/forge/src/applaunch/java/org/spongepowered/forge/applaunch/service/SpongeForgeTransformationService.java +++ b/forge/src/applaunch/java/org/spongepowered/forge/applaunch/service/SpongeForgeTransformationService.java @@ -24,17 +24,24 @@ */ package org.spongepowered.forge.applaunch.service; +import cpw.mods.jarhandling.SecureJar; +import cpw.mods.modlauncher.Environment; import cpw.mods.modlauncher.Launcher; import cpw.mods.modlauncher.api.IEnvironment; +import cpw.mods.modlauncher.api.IModuleLayerManager; import cpw.mods.modlauncher.api.ITransformationService; import cpw.mods.modlauncher.api.ITransformer; import cpw.mods.modlauncher.api.TypesafeMap; import joptsimple.OptionSpec; import joptsimple.OptionSpecBuilder; -import net.minecraftforge.fml.loading.FMLEnvironment; +import net.minecraftforge.fml.loading.LoadingModList; +import net.minecraftforge.fml.loading.moddiscovery.ModFileInfo; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.NonNull; import org.spongepowered.transformers.modlauncher.SuperclassChanger; +import java.net.MalformedURLException; import java.nio.file.Path; import java.util.Collections; import java.util.List; @@ -43,6 +50,8 @@ import java.util.function.Supplier; public class SpongeForgeTransformationService implements ITransformationService { + private static final Logger LOGGER = LogManager.getLogger(); + private OptionSpec checkHashes; private OptionSpec librariesDirectoryName; @@ -75,19 +84,40 @@ public void argumentValues(final OptionResult option) { @Override public void initialize(final IEnvironment environment) { - if (FMLEnvironment.production) { - // TODO actually read this from the jar manifest - environment.getProperty(SuperclassChanger.INSTANCE.get()).ifPresent(scc -> { - scc.offerResource(SpongeForgeTransformationService.class.getResource("/common.superclasschange"), "SpongeForge injected"); - scc.offerResource(SpongeForgeTransformationService.class.getResource("/forge.superclasschange"), "SpongeForge injected"); - }); - } } @Override public void onLoad(final IEnvironment env, final Set otherServices) { } + @Override + public List completeScan(IModuleLayerManager layerManager) { + final Environment env = Launcher.INSTANCE.environment(); + final SuperclassChanger superclassChanger = env.getProperty(SuperclassChanger.INSTANCE.get()).orElse(null); + + if (superclassChanger != null) { + // TODO move this inside SuperclassChanger when updating SpongeCommon to latest ModLauncher + for (ModFileInfo fileInfo : LoadingModList.get().getModFiles()) { + final SecureJar jar = fileInfo.getFile().getSecureJar(); + final String superclassChangeFiles = jar.moduleDataProvider().getManifest().getMainAttributes().getValue("Superclass-Transformer"); + if (superclassChangeFiles != null) { + final String jarFileName = fileInfo.getFile().getFileName(); + LOGGER.debug("Registering superclass changers from {} ...", jarFileName); + + for (final String superclassChangeFile : superclassChangeFiles.split(",")) { + try { + superclassChanger.offerResource(jar.getPath(superclassChangeFile).toUri().toURL(), superclassChangeFile); + } catch (final MalformedURLException e) { + LOGGER.warn("Failed to register superclass changer {} from {}", superclassChangeFile, jarFileName, e); + } + } + } + } + } + + return List.of(); + } + @NonNull @Override @SuppressWarnings("rawtypes") // :(((((((((((((((