Skip to content

Commit

Permalink
Register superclass changers
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed Oct 10, 2023
1 parent 894ba0f commit ff56429
Showing 1 changed file with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +50,8 @@
import java.util.function.Supplier;

public class SpongeForgeTransformationService implements ITransformationService {
private static final Logger LOGGER = LogManager.getLogger();

private OptionSpec<Boolean> checkHashes;
private OptionSpec<String> librariesDirectoryName;

Expand Down Expand Up @@ -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<String> otherServices) {
}

@Override
public List<Resource> 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") // :(((((((((((((((
Expand Down

0 comments on commit ff56429

Please sign in to comment.