Skip to content

Commit

Permalink
Better logging + fixed hijackers not applying to late mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
Rongmario committed Oct 28, 2024
1 parent b7529d4 commit 6ed2344
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 62 deletions.
118 changes: 69 additions & 49 deletions src/main/java/zone/rong/mixinbooter/MixinBooterPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ public final class MixinBooterPlugin implements IFMLLoadingPlugin {

public static final Logger LOGGER = LogManager.getLogger("MixinBooter");

private static final Map<String, IMixinConfigHijacker> configHijackers = new HashMap<>();

public static IMixinConfigHijacker getHijacker(String configName) {
return configHijackers.get(configName);
}

static String getMinecraftVersion() {
return (String) FMLInjectionData.data()[4];
}

public MixinBooterPlugin() {
addTransformationExclusions();
initialize();
LOGGER.info("Initializing Mixins...");
MixinBootstrap.init();
Mixins.addConfiguration("mixin.mixinbooter.init.json");
LOGGER.info("Initializing MixinExtras...");
MixinExtrasBootstrap.init();
MixinFixer.patchAncientModMixinsLoadingMethod();
this.addTransformationExclusions();
this.initialize();
}

@Override
Expand All @@ -60,47 +60,10 @@ public String getSetupClass() {
public void injectData(Map<String, Object> data) {
Object coremodList = data.get("coremodList");
if (coremodList instanceof List) {
Field fmlPluginWrapper$coreModInstance = null;
Map<String, IMixinConfigHijacker> hijackers = new HashMap<>();
Set<IEarlyMixinLoader> queuedLoaders = new LinkedHashSet<>();
for (Object coremod : (List) coremodList) {
try {
if (fmlPluginWrapper$coreModInstance == null) {
fmlPluginWrapper$coreModInstance = coremod.getClass().getField("coreModInstance");
fmlPluginWrapper$coreModInstance.setAccessible(true);
}
Object theMod = fmlPluginWrapper$coreModInstance.get(coremod);
if (theMod instanceof IMixinConfigHijacker) {
IMixinConfigHijacker interceptor = (IMixinConfigHijacker) theMod;
for (String hijacked : interceptor.getHijackedMixinConfigs()) {
hijackers.put(hijacked, interceptor);
}
}
if (theMod instanceof IEarlyMixinLoader) {
IEarlyMixinLoader loader = (IEarlyMixinLoader) theMod;
LOGGER.info("Grabbing {} for its mixins.", loader.getClass());
queuedLoaders.add(loader);
} else if ("org.spongepowered.mod.SpongeCoremod".equals(theMod.getClass().getName())) {
Launch.classLoader.registerTransformer("zone.rong.mixinbooter.fix.spongeforge.SpongeForgeFixer");
}
} catch (Throwable t) {
LOGGER.error("Unexpected error", t);
}
}
for (IEarlyMixinLoader queuedLoader : queuedLoaders) {
for (String mixinConfig : queuedLoader.getMixinConfigs()) {
if (queuedLoader.shouldMixinConfigQueue(mixinConfig)) {
IMixinConfigHijacker hijacker = hijackers.get(mixinConfig);
if (hijacker != null) {
LOGGER.info("Mixin configuration [{}] intercepted by [{}].", mixinConfig, hijacker.getClass().getName());
} else {
LOGGER.info("Adding {} mixin configuration.", mixinConfig);
Mixins.addConfiguration(mixinConfig);
queuedLoader.onMixinConfigQueued(mixinConfig);
}
}
}
}
Collection<IEarlyMixinLoader> earlyLoaders = this.gatherEarlyLoaders((List) coremodList);
this.loadEarlyLoaders(earlyLoaders);
} else {
throw new RuntimeException("Blackboard property 'coremodList' must be of type List, early loaders were not able to be gathered");
}
}

Expand All @@ -114,8 +77,65 @@ private void addTransformationExclusions() {
Launch.classLoader.addTransformerExclusion("com.llamalad7.mixinextras.");
}

private Collection<IEarlyMixinLoader> gatherEarlyLoaders(List coremodList) {
Field fmlPluginWrapper$coreModInstance = null;
Set<IEarlyMixinLoader> queuedLoaders = new LinkedHashSet<>();
for (Object coremod : coremodList) {
try {
if (fmlPluginWrapper$coreModInstance == null) {
fmlPluginWrapper$coreModInstance = coremod.getClass().getField("coreModInstance");
fmlPluginWrapper$coreModInstance.setAccessible(true);
}
Object theMod = fmlPluginWrapper$coreModInstance.get(coremod);
if (theMod instanceof IMixinConfigHijacker) {
IMixinConfigHijacker interceptor = (IMixinConfigHijacker) theMod;
for (String hijacked : interceptor.getHijackedMixinConfigs()) {
configHijackers.put(hijacked, interceptor);
}
}
if (theMod instanceof IEarlyMixinLoader) {
queuedLoaders.add((IEarlyMixinLoader) theMod);
} else if ("org.spongepowered.mod.SpongeCoremod".equals(theMod.getClass().getName())) {
LOGGER.info("Registering SpongeForgeFixer transformer to solve issues pertaining SpongeForge.");
Launch.classLoader.registerTransformer("zone.rong.mixinbooter.fix.spongeforge.SpongeForgeFixer");
}
} catch (Throwable t) {
LOGGER.error("Unexpected error", t);
}
}
return queuedLoaders;
}

private void loadEarlyLoaders(Collection<IEarlyMixinLoader> queuedLoaders) {
for (IEarlyMixinLoader queuedLoader : queuedLoaders) {
LOGGER.info("Loading early loader [{}] for its mixins.", queuedLoader.getClass().getName());
for (String mixinConfig : queuedLoader.getMixinConfigs()) {
if (queuedLoader.shouldMixinConfigQueue(mixinConfig)) {
IMixinConfigHijacker hijacker = getHijacker(mixinConfig);
if (hijacker != null) {
LOGGER.info("Mixin configuration [{}] intercepted by [{}].", mixinConfig, hijacker.getClass().getName());
} else {
LOGGER.info("Adding [{}] mixin configuration.", mixinConfig);
Mixins.addConfiguration(mixinConfig);
queuedLoader.onMixinConfigQueued(mixinConfig);
}
}
}
}
}

private void initialize() {
GlobalProperties.put(GlobalProperties.Keys.CLEANROOM_DISABLE_MIXIN_CONFIGS, new HashSet<>());

LOGGER.info("Initializing Mixins...");
MixinBootstrap.init();

Mixins.addConfiguration("mixin.mixinbooter.init.json");

LOGGER.info("Initializing MixinExtras...");
MixinExtrasBootstrap.init();

MixinFixer.patchAncientModMixinsLoadingMethod();
}

public static class Container extends DummyModContainer {
Expand Down
44 changes: 31 additions & 13 deletions src/main/java/zone/rong/mixinbooter/mixin/LoadControllerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,41 @@ private void beforeConstructing(LoaderState state, Object[] eventData, CallbackI

if (!annotatedData.isEmpty()) {
for (ASMDataTable.ASMData annotated : annotatedData) {
Class<?> clazz = Class.forName(annotated.getClassName());
MixinBooterPlugin.LOGGER.info("Instantiating @MixinLoader annotated class: " + clazz);
Object instance = clazz.newInstance();
if (instance instanceof ILateMixinLoader) {
lateLoaders.add((ILateMixinLoader) instance);
try {
Class<?> clazz = Class.forName(annotated.getClassName());
MixinBooterPlugin.LOGGER.info("Loading annotated late loader [{}] for its mixins.", clazz.getName());
Object instance = clazz.newInstance();
if (instance instanceof ILateMixinLoader) {
lateLoaders.add((ILateMixinLoader) instance);
}
} catch (Throwable t) {
throw new RuntimeException("Unexpected error.", t);
}
}
}

// Instantiate all ILateMixinLoader implemented classes
if (!interfaceData.isEmpty()) {
for (ASMDataTable.ASMData itf : interfaceData) {
Class<?> clazz = Class.forName(itf.getClassName().replace('/', '.'));
MixinBooterPlugin.LOGGER.info("Instantiating ILateMixinLoader class: " + clazz);
lateLoaders.add((ILateMixinLoader) clazz.newInstance());
try {
Class<?> clazz = Class.forName(itf.getClassName().replace('/', '.'));
MixinBooterPlugin.LOGGER.info("Loading late loader [{}] for its mixins.", clazz.getName());
lateLoaders.add((ILateMixinLoader) clazz.newInstance());
} catch (Throwable t) {
throw new RuntimeException("Unexpected error.", t);
}
}
for (ILateMixinLoader lateLoader : lateLoaders) {
for (String mixinConfig : lateLoader.getMixinConfigs()) {
if (lateLoader.shouldMixinConfigQueue(mixinConfig)) {
MixinBooterPlugin.LOGGER.info("Adding " + mixinConfig + " mixin configuration.");
Mixins.addConfiguration(mixinConfig);
lateLoader.onMixinConfigQueued(mixinConfig);
IMixinConfigHijacker hijacker = MixinBooterPlugin.getHijacker(mixinConfig);
if (hijacker != null) {
MixinBooterPlugin.LOGGER.info("Mixin configuration [{}] intercepted by [{}].", mixinConfig, hijacker.getClass().getName());
} else {
MixinBooterPlugin.LOGGER.info("Adding [{}] mixin configuration.", mixinConfig);
Mixins.addConfiguration(mixinConfig);
lateLoader.onMixinConfigQueued(mixinConfig);
}
}
}
}
Expand All @@ -83,8 +96,13 @@ private void beforeConstructing(LoaderState state, Object[] eventData, CallbackI
if (!unconventionalConfigs.isEmpty()) {
MixinBooterPlugin.LOGGER.info("Appending unconventional mixin configurations...");
for (String unconventionalConfig : unconventionalConfigs) {
MixinBooterPlugin.LOGGER.info("Adding " + unconventionalConfig + " mixin configuration.");
Mixins.addConfiguration(unconventionalConfig);
IMixinConfigHijacker hijacker = MixinBooterPlugin.getHijacker(unconventionalConfig);
if (hijacker != null) {
MixinBooterPlugin.LOGGER.info("Mixin configuration [{}] intercepted by [{}].", unconventionalConfig, hijacker.getClass().getName());
} else {
MixinBooterPlugin.LOGGER.info("Adding [{}] mixin configuration.", unconventionalConfig);
Mixins.addConfiguration(unconventionalConfig);
}
}
}

Expand Down

0 comments on commit 6ed2344

Please sign in to comment.