Skip to content

Commit

Permalink
Add: add module to SERVICE layer
Browse files Browse the repository at this point in the history
  • Loading branch information
xfl03 committed Jan 27, 2023
1 parent 48beebe commit e06d71a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package me.xfl03.morecrashinfo.modlauncher;

import cpw.mods.jarhandling.SecureJar;
import cpw.mods.modlauncher.Launcher;
import cpw.mods.modlauncher.ModuleLayerHandler;
import cpw.mods.modlauncher.api.IModuleLayerManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.EnumMap;

public class ModuleHelper {
public static void addModule(IModuleLayerManager.Layer layer, Path jar) {
if (!Launcher.INSTANCE.findLayerManager().isPresent()) {
throw new IllegalStateException("LayerManager not found.");
}
IModuleLayerManager handler = Launcher.INSTANCE.findLayerManager().get();
if (!(handler instanceof ModuleLayerHandler)) {
throw new IllegalArgumentException("IModuleLayerManager not supported: " + handler);
}
ModuleLayerHandler moduleLayerHandler = (ModuleLayerHandler) handler;
removeCompletedLayers(moduleLayerHandler, layer);
addToLayer(moduleLayerHandler, layer, jar);
moduleLayerHandler.buildLayer(layer);
TransformerService.logger.info("Added {} to {}", jar, layer);
}

private static void removeCompletedLayers(ModuleLayerHandler handler, IModuleLayerManager.Layer layer) {
try {
Field field = ModuleLayerHandler.class.getDeclaredField("completedLayers");
field.setAccessible(true);
EnumMap<IModuleLayerManager.Layer, ?> completedLayer =
(EnumMap<IModuleLayerManager.Layer, ?>) field.get(handler);
completedLayer.remove(layer);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

private static void addToLayer(ModuleLayerHandler handler, IModuleLayerManager.Layer layer, Path jar) {
try {
Method method = ModuleLayerHandler.class.getDeclaredMethod(
"addToLayer", IModuleLayerManager.Layer.class, SecureJar.class);
method.setAccessible(true);
method.invoke(handler, layer, SecureJar.from(jar));
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package me.xfl03.morecrashinfo.modlauncher;

import cpw.mods.modlauncher.Launcher;
import cpw.mods.modlauncher.api.IEnvironment;
import cpw.mods.modlauncher.api.ITransformationService;
import cpw.mods.modlauncher.api.ITransformer;
import cpw.mods.modlauncher.api.IncompatibleEnvironmentException;
import cpw.mods.modlauncher.api.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -70,10 +66,9 @@ public void initialize(IEnvironment environment) {
logger.warn(e);
}
} else {
//After 1.17, we need to add jar as module to PlatformClassLoader
//After 1.17, we need to add jar as module to SERVICE Layer
try {
Method method = ClassLoader.class.getMethod("getPlatformClassLoader");
ClasspathHelper.add((ClassLoader) method.invoke(null), mapped);
ModuleHelper.addModule(IModuleLayerManager.Layer.SERVICE, mapped);
} catch (Throwable e) {
logger.warn("Error while appendToClassPath getPlatformClassLoader");
logger.warn(e);
Expand Down

0 comments on commit e06d71a

Please sign in to comment.