Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LiteLoader Compat for LATE Mixins #22

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion module-gtnhmixins/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
extraVersion=2.1.15
extraVersion=2.2.0
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import com.gtnewhorizon.gtnhmixins.ILateMixinLoader;
import com.gtnewhorizon.gtnhmixins.LateMixin;
import com.gtnewhorizon.gtnhmixins.Reflection;
import com.gtnewhorizon.gtnhmixins.core.GTNHMixinsCore;

import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ModClassLoader;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.discovery.ASMDataTable;
import cpw.mods.fml.common.event.FMLConstructionEvent;
import net.minecraft.launchwrapper.Launch;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -21,7 +23,12 @@
import org.spongepowered.asm.mixin.transformer.Proxy;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


Expand All @@ -45,8 +52,11 @@ private void beforeConstructing(Object[] eventData, CallbackInfo ci) throws Thro
ASMDataTable asmDataTable = (ASMDataTable)eventData[1];
final Loader loader = Loader.instance();

final Set<String> loadedMods = loader.getIndexedModList().keySet();
GTNHMixinsCore.LOGGER.info("LoadedMods {}", loadedMods.toString());
final Set<String> loadedModsTemp = new HashSet<>();
loadedModsTemp.addAll(loader.getIndexedModList().keySet());
loadedModsTemp.addAll(getLiteLoaderMods());
final Set<String> loadedMods = Collections.unmodifiableSet(loadedModsTemp);
GTNHMixins.LOGGER.info("LoadedMods {}", loadedMods.toString());

for (ASMDataTable.ASMData asmData : asmDataTable.getAll(LateMixin.class.getName())) {
modClassLoader.addFile(asmData.getCandidate().getModContainer()); // Add to path before `newInstance`
Expand Down Expand Up @@ -91,4 +101,28 @@ private void beforeConstructing(Object[] eventData, CallbackInfo ci) throws Thro
Reflection.invokeSelectConfigs(transformer, env);
Reflection.invokePrepareConfigs(transformer, env);
}

@SuppressWarnings("unchecked")
private static Set<String> getLiteLoaderMods() {
Set<String> mods = new HashSet<>();
try {
Class<?> LiteLoaderTweaker = Class.forName("com.mumfrey.liteloader.launch.LiteLoaderTweaker");
Method hasValidMetaData = Class.forName("com.mumfrey.liteloader.interfaces.LoadableMod").getMethod("hasValidMetaData");
Object instance = FieldUtils.readDeclaredStaticField(LiteLoaderTweaker, "instance", true);
Object bootstrap = FieldUtils.readDeclaredField(instance, "bootstrap", true);
Object enumerator = FieldUtils.readDeclaredField(bootstrap, "enumerator", true);
Map<String, ?> enabledContainers = (Map<String, ?>) FieldUtils.readDeclaredField(enumerator, "enabledContainers", true);
GTNHMixins.LOGGER.info("LiteLoader present, adding its mods to the list.");
for(Entry<String, ?> e : enabledContainers.entrySet()) {
if((boolean) hasValidMetaData.invoke(e.getValue())) {
mods.add(e.getKey());
}
}
} catch (ClassNotFoundException e) {
GTNHMixins.LOGGER.info("LiteLoader not present.");
} catch (Exception e) {
GTNHMixins.LOGGER.error("Failed to get LiteLoader mods.", e);
}
return mods;
}
}