-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
8 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
2Loader/src/main/java/me/xfl03/morecrashinfo/modlauncher/ModuleHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters